1 package org.apache.lucene.analysis;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.util.Arrays;
25 import java.util.List;
27 import org.apache.lucene.util.Version;
29 /** Filters {@link LetterTokenizer} with {@link LowerCaseFilter} and {@link StopFilter}.
32 * <p>You must specify the required {@link Version}
33 * compatibility when creating StopAnalyzer:
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
41 public final class StopAnalyzer extends StopwordAnalyzerBase {
43 /** An unmodifiable set containing some common English words that are not usually useful
45 public static final Set<?> ENGLISH_STOP_WORDS_SET;
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"
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);
61 /** Builds an analyzer which removes words in
62 * {@link #ENGLISH_STOP_WORDS_SET}.
63 * @param matchVersion See <a href="#version">above</a>
65 public StopAnalyzer(Version matchVersion) {
66 this(matchVersion, ENGLISH_STOP_WORDS_SET);
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);
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));
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));
94 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
95 * used to tokenize all the text in the provided {@link Reader}.
97 * @return {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
98 * built from a {@link LowerCaseTokenizer} filtered with
102 protected TokenStreamComponents createComponents(String fieldName,
104 final Tokenizer source = new LowerCaseTokenizer(matchVersion, reader);
105 return new TokenStreamComponents(source, new StopFilter(matchVersion,