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.IOUtils;
28 import org.apache.lucene.util.Version;
30 /** Filters {@link LetterTokenizer} with {@link LowerCaseFilter} and {@link StopFilter}.
33 * <p>You must specify the required {@link Version}
34 * compatibility when creating StopAnalyzer:
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
42 public final class StopAnalyzer extends StopwordAnalyzerBase {
44 /** An unmodifiable set containing some common English words that are not usually useful
46 public static final Set<?> ENGLISH_STOP_WORDS_SET;
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"
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);
62 /** Builds an analyzer which removes words in
63 * {@link #ENGLISH_STOP_WORDS_SET}.
64 * @param matchVersion See <a href="#version">above</a>
66 public StopAnalyzer(Version matchVersion) {
67 this(matchVersion, ENGLISH_STOP_WORDS_SET);
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);
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));
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));
96 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
97 * used to tokenize all the text in the provided {@link Reader}.
99 * @return {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
100 * built from a {@link LowerCaseTokenizer} filtered with
104 protected TokenStreamComponents createComponents(String fieldName,
106 final Tokenizer source = new LowerCaseTokenizer(matchVersion, reader);
107 return new TokenStreamComponents(source, new StopFilter(matchVersion,