add --shared
[pylucene.git] / lucene-java-3.4.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.Version;
28
29 /** Filters {@link LetterTokenizer} with {@link LowerCaseFilter} and {@link StopFilter}.
30  *
31  * <a name="version"/>
32  * <p>You must specify the required {@link Version}
33  * compatibility when creating StopAnalyzer:
34  * <ul>
35  *    <li> As of 3.1, StopFilter correctly handles Unicode 4.0
36  *         supplementary characters in stopwords
37  *   <li> As of 2.9, position increments are preserved
38  * </ul>
39 */
40
41 public final class StopAnalyzer extends StopwordAnalyzerBase {
42   
43   /** An unmodifiable set containing some common English words that are not usually useful
44   for searching.*/
45   public static final Set<?> ENGLISH_STOP_WORDS_SET;
46   
47   static {
48     final List<String> stopWords = Arrays.asList(
49       "a", "an", "and", "are", "as", "at", "be", "but", "by",
50       "for", "if", "in", "into", "is", "it",
51       "no", "not", "of", "on", "or", "such",
52       "that", "the", "their", "then", "there", "these",
53       "they", "this", "to", "was", "will", "with"
54     );
55     final CharArraySet stopSet = new CharArraySet(Version.LUCENE_CURRENT, 
56         stopWords.size(), false);
57     stopSet.addAll(stopWords);  
58     ENGLISH_STOP_WORDS_SET = CharArraySet.unmodifiableSet(stopSet); 
59   }
60   
61   /** Builds an analyzer which removes words in
62    *  {@link #ENGLISH_STOP_WORDS_SET}.
63    * @param matchVersion See <a href="#version">above</a>
64    */
65   public StopAnalyzer(Version matchVersion) {
66     this(matchVersion, ENGLISH_STOP_WORDS_SET);
67   }
68
69   /** Builds an analyzer with the stop words from the given set.
70    * @param matchVersion See <a href="#version">above</a>
71    * @param stopWords Set of stop words */
72   public StopAnalyzer(Version matchVersion, Set<?> stopWords) {
73     super(matchVersion, stopWords);
74   }
75
76   /** Builds an analyzer with the stop words from the given file.
77    * @see WordlistLoader#getWordSet(File)
78    * @param matchVersion See <a href="#version">above</a>
79    * @param stopwordsFile File to load stop words from */
80   public StopAnalyzer(Version matchVersion, File stopwordsFile) throws IOException {
81     this(matchVersion, WordlistLoader.getWordSet(stopwordsFile));
82   }
83
84   /** Builds an analyzer with the stop words from the given reader.
85    * @see WordlistLoader#getWordSet(Reader)
86    * @param matchVersion See <a href="#version">above</a>
87    * @param stopwords Reader to load stop words from */
88   public StopAnalyzer(Version matchVersion, Reader stopwords) throws IOException {
89     this(matchVersion, WordlistLoader.getWordSet(stopwords));
90   }
91
92   /**
93    * Creates
94    * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
95    * used to tokenize all the text in the provided {@link Reader}.
96    * 
97    * @return {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
98    *         built from a {@link LowerCaseTokenizer} filtered with
99    *         {@link StopFilter}
100    */
101   @Override
102   protected TokenStreamComponents createComponents(String fieldName,
103       Reader reader) {
104     final Tokenizer source = new LowerCaseTokenizer(matchVersion, reader);
105     return new TokenStreamComponents(source, new StopFilter(matchVersion,
106           source, stopwords));
107   }
108 }
109