pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / en / EnglishAnalyzer.java
1 package org.apache.lucene.analysis.en;
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.Reader;
21 import java.util.Set;
22
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;
36
37 /**
38  * {@link Analyzer} for English.
39  */
40 public final class EnglishAnalyzer extends StopwordAnalyzerBase {
41   private final Set<?> stemExclusionSet;
42    
43   /**
44    * Returns an unmodifiable instance of the default stop words set.
45    * @return default stop words set.
46    */
47   public static Set<?> getDefaultStopSet(){
48     return DefaultSetHolder.DEFAULT_STOP_SET;
49   }
50   
51   /**
52    * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class 
53    * accesses the static final set the first time.;
54    */
55   private static class DefaultSetHolder {
56     static final Set<?> DEFAULT_STOP_SET = StandardAnalyzer.STOP_WORDS_SET;
57   }
58
59   /**
60    * Builds an analyzer with the default stop words: {@link #getDefaultStopSet}.
61    */
62   public EnglishAnalyzer(Version matchVersion) {
63     this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
64   }
65   
66   /**
67    * Builds an analyzer with the given stop words.
68    * 
69    * @param matchVersion lucene compatibility version
70    * @param stopwords a stopword set
71    */
72   public EnglishAnalyzer(Version matchVersion, Set<?> stopwords) {
73     this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
74   }
75
76   /**
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
79    * stemming.
80    * 
81    * @param matchVersion lucene compatibility version
82    * @param stopwords a stopword set
83    * @param stemExclusionSet a set of terms not to be stemmed
84    */
85   public EnglishAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
86     super(matchVersion, stopwords);
87     this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
88         matchVersion, stemExclusionSet));
89   }
90
91   /**
92    * Creates a
93    * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
94    * which tokenizes all the text in the provided {@link Reader}.
95    * 
96    * @return A
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}.
102    */
103   @Override
104   protected TokenStreamComponents createComponents(String fieldName,
105       Reader reader) {
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);
117   }
118 }