pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / analysis / StopAnalyzer.java
1 package org.apache.lucene.analysis;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.util.Arrays;
24 import java.util.Set;
25 import java.util.List;
26
27 import org.apache.lucene.util.IOUtils;
28 import org.apache.lucene.util.Version;
29
30 /** Filters {@link LetterTokenizer} with {@link LowerCaseFilter} and {@link StopFilter}.
31  *
32  * <a name="version"/>
33  * <p>You must specify the required {@link Version}
34  * compatibility when creating StopAnalyzer:
35  * <ul>
36  *    <li> As of 3.1, StopFilter correctly handles Unicode 4.0
37  *         supplementary characters in stopwords
38  *   <li> As of 2.9, position increments are preserved
39  * </ul>
40 */
41
42 public final class StopAnalyzer extends StopwordAnalyzerBase {
43   
44   /** An unmodifiable set containing some common English words that are not usually useful
45   for searching.*/
46   public static final Set<?> ENGLISH_STOP_WORDS_SET;
47   
48   static {
49     final List<String> stopWords = Arrays.asList(
50       "a", "an", "and", "are", "as", "at", "be", "but", "by",
51       "for", "if", "in", "into", "is", "it",
52       "no", "not", "of", "on", "or", "such",
53       "that", "the", "their", "then", "there", "these",
54       "they", "this", "to", "was", "will", "with"
55     );
56     final CharArraySet stopSet = new CharArraySet(Version.LUCENE_CURRENT, 
57         stopWords.size(), false);
58     stopSet.addAll(stopWords);  
59     ENGLISH_STOP_WORDS_SET = CharArraySet.unmodifiableSet(stopSet); 
60   }
61   
62   /** Builds an analyzer which removes words in
63    *  {@link #ENGLISH_STOP_WORDS_SET}.
64    * @param matchVersion See <a href="#version">above</a>
65    */
66   public StopAnalyzer(Version matchVersion) {
67     this(matchVersion, ENGLISH_STOP_WORDS_SET);
68   }
69
70   /** Builds an analyzer with the stop words from the given set.
71    * @param matchVersion See <a href="#version">above</a>
72    * @param stopWords Set of stop words */
73   public StopAnalyzer(Version matchVersion, Set<?> stopWords) {
74     super(matchVersion, stopWords);
75   }
76
77   /** Builds an analyzer with the stop words from the given file.
78    * @see WordlistLoader#getWordSet(Reader, Version)
79    * @param matchVersion See <a href="#version">above</a>
80    * @param stopwordsFile File to load stop words from */
81   public StopAnalyzer(Version matchVersion, File stopwordsFile) throws IOException {
82     this(matchVersion, WordlistLoader.getWordSet(IOUtils.getDecodingReader(stopwordsFile,
83         IOUtils.CHARSET_UTF_8), matchVersion));
84   }
85
86   /** Builds an analyzer with the stop words from the given reader.
87    * @see WordlistLoader#getWordSet(Reader, Version)
88    * @param matchVersion See <a href="#version">above</a>
89    * @param stopwords Reader to load stop words from */
90   public StopAnalyzer(Version matchVersion, Reader stopwords) throws IOException {
91     this(matchVersion, WordlistLoader.getWordSet(stopwords, matchVersion));
92   }
93
94   /**
95    * Creates
96    * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
97    * used to tokenize all the text in the provided {@link Reader}.
98    * 
99    * @return {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
100    *         built from a {@link LowerCaseTokenizer} filtered with
101    *         {@link StopFilter}
102    */
103   @Override
104   protected TokenStreamComponents createComponents(String fieldName,
105       Reader reader) {
106     final Tokenizer source = new LowerCaseTokenizer(matchVersion, reader);
107     return new TokenStreamComponents(source, new StopFilter(matchVersion,
108           source, stopwords));
109   }
110 }
111