1 package org.apache.lucene.analysis.en;
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.
20 import java.io.Reader;
23 import org.apache.lucene.analysis.Analyzer;
24 import org.apache.lucene.analysis.CharArraySet;
25 import org.apache.lucene.analysis.KeywordMarkerFilter;
26 import org.apache.lucene.analysis.LowerCaseFilter;
27 import org.apache.lucene.analysis.PorterStemFilter;
28 import org.apache.lucene.analysis.StopFilter;
29 import org.apache.lucene.analysis.StopwordAnalyzerBase;
30 import org.apache.lucene.analysis.TokenStream;
31 import org.apache.lucene.analysis.Tokenizer;
32 import org.apache.lucene.analysis.standard.StandardAnalyzer;
33 import org.apache.lucene.analysis.standard.StandardFilter;
34 import org.apache.lucene.analysis.standard.StandardTokenizer;
35 import org.apache.lucene.util.Version;
38 * {@link Analyzer} for English.
40 public final class EnglishAnalyzer extends StopwordAnalyzerBase {
41 private final Set<?> stemExclusionSet;
44 * Returns an unmodifiable instance of the default stop words set.
45 * @return default stop words set.
47 public static Set<?> getDefaultStopSet(){
48 return DefaultSetHolder.DEFAULT_STOP_SET;
52 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
53 * accesses the static final set the first time.;
55 private static class DefaultSetHolder {
56 static final Set<?> DEFAULT_STOP_SET = StandardAnalyzer.STOP_WORDS_SET;
60 * Builds an analyzer with the default stop words: {@link #getDefaultStopSet}.
62 public EnglishAnalyzer(Version matchVersion) {
63 this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
67 * Builds an analyzer with the given stop words.
69 * @param matchVersion lucene compatibility version
70 * @param stopwords a stopword set
72 public EnglishAnalyzer(Version matchVersion, Set<?> stopwords) {
73 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
77 * Builds an analyzer with the given stop words. If a non-empty stem exclusion set is
78 * provided this analyzer will add a {@link KeywordMarkerFilter} before
81 * @param matchVersion lucene compatibility version
82 * @param stopwords a stopword set
83 * @param stemExclusionSet a set of terms not to be stemmed
85 public EnglishAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
86 super(matchVersion, stopwords);
87 this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
88 matchVersion, stemExclusionSet));
93 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
94 * which tokenizes all the text in the provided {@link Reader}.
97 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
98 * built from an {@link StandardTokenizer} filtered with
99 * {@link StandardFilter}, {@link LowerCaseFilter}, {@link StopFilter}
100 * , {@link KeywordMarkerFilter} if a stem exclusion set is
101 * provided and {@link PorterStemFilter}.
104 protected TokenStreamComponents createComponents(String fieldName,
106 final Tokenizer source = new StandardTokenizer(matchVersion, reader);
107 TokenStream result = new StandardFilter(matchVersion, source);
108 // prior to this we get the classic behavior, standardfilter does it for us.
109 if (matchVersion.onOrAfter(Version.LUCENE_31))
110 result = new EnglishPossessiveFilter(result);
111 result = new LowerCaseFilter(matchVersion, result);
112 result = new StopFilter(matchVersion, result, stopwords);
113 if(!stemExclusionSet.isEmpty())
114 result = new KeywordMarkerFilter(result, stemExclusionSet);
115 result = new PorterStemFilter(result);
116 return new TokenStreamComponents(source, result);