add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / stempel / src / java / org / apache / lucene / analysis / pl / PolishAnalyzer.java
1 package org.apache.lucene.analysis.pl;
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.IOException;
21 import java.io.Reader;
22 import java.util.Set;
23
24 import org.apache.lucene.analysis.Analyzer;
25 import org.apache.lucene.analysis.CharArraySet;
26 import org.apache.lucene.analysis.KeywordMarkerFilter;
27 import org.apache.lucene.analysis.LowerCaseFilter;
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.WordlistLoader;
33 import org.apache.lucene.analysis.standard.StandardFilter;
34 import org.apache.lucene.analysis.standard.StandardTokenizer;
35 import org.apache.lucene.analysis.stempel.StempelStemmer;
36 import org.apache.lucene.analysis.stempel.StempelFilter;
37 import org.apache.lucene.util.Version;
38 import org.egothor.stemmer.Trie;
39
40 /**
41  * {@link Analyzer} for Polish.
42  */
43 public final class PolishAnalyzer extends StopwordAnalyzerBase {
44   private final Set<?> stemExclusionSet;
45   private final Trie stemTable;
46   
47   /** File containing default Polish stopwords. */
48   public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
49   
50   /** File containing default Polish stemmer table. */
51   public final static String DEFAULT_STEMMER_FILE = "stemmer_20000.tbl";
52   
53   /**
54    * Returns an unmodifiable instance of the default stop words set.
55    * @return default stop words set.
56    */
57   public static Set<?> getDefaultStopSet(){
58     return DefaultsHolder.DEFAULT_STOP_SET;
59   }
60   
61   /**
62    * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class 
63    * accesses the static final set the first time.;
64    */
65   private static class DefaultsHolder {
66     static final Set<?> DEFAULT_STOP_SET;
67     static final Trie DEFAULT_TABLE;
68     
69     static {
70       try {
71         DEFAULT_STOP_SET = WordlistLoader.getWordSet(PolishAnalyzer.class, 
72             DEFAULT_STOPWORD_FILE);
73       } catch (IOException ex) {
74         // default set should always be present as it is part of the
75         // distribution (JAR)
76         throw new RuntimeException("Unable to load default stopword set", ex);
77       }
78       
79       try {
80         DEFAULT_TABLE = StempelStemmer.load(PolishAnalyzer.class.getResourceAsStream(DEFAULT_STEMMER_FILE));
81       } catch (IOException ex) {
82         // default set should always be present as it is part of the
83         // distribution (JAR)
84         throw new RuntimeException("Unable to load default stemming tables", ex);
85       }
86     }
87   }
88
89   /**
90    * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
91    */
92   public PolishAnalyzer(Version matchVersion) {
93     this(matchVersion, DefaultsHolder.DEFAULT_STOP_SET);
94   }
95   
96   /**
97    * Builds an analyzer with the given stop words.
98    * 
99    * @param matchVersion lucene compatibility version
100    * @param stopwords a stopword set
101    */
102   public PolishAnalyzer(Version matchVersion, Set<?> stopwords) {
103     this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
104   }
105
106   /**
107    * Builds an analyzer with the given stop words. If a non-empty stem exclusion set is
108    * provided this analyzer will add a {@link KeywordMarkerFilter} before
109    * stemming.
110    * 
111    * @param matchVersion lucene compatibility version
112    * @param stopwords a stopword set
113    * @param stemExclusionSet a set of terms not to be stemmed
114    */
115   public PolishAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
116     super(matchVersion, stopwords);
117     this.stemTable = DefaultsHolder.DEFAULT_TABLE;
118     this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
119         matchVersion, stemExclusionSet));
120   }
121
122   /**
123    * Creates a
124    * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
125    * which tokenizes all the text in the provided {@link Reader}.
126    * 
127    * @return A
128    *         {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
129    *         built from an {@link StandardTokenizer} filtered with
130    *         {@link StandardFilter}, {@link LowerCaseFilter}, {@link StopFilter}
131    *         , {@link KeywordMarkerFilter} if a stem exclusion set is
132    *         provided and {@link StempelFilter}.
133    */
134   @Override
135   protected TokenStreamComponents createComponents(String fieldName,
136       Reader reader) {
137     final Tokenizer source = new StandardTokenizer(matchVersion, reader);
138     TokenStream result = new StandardFilter(matchVersion, source);
139     result = new LowerCaseFilter(matchVersion, result);
140     result = new StopFilter(matchVersion, result, stopwords);
141     if(!stemExclusionSet.isEmpty())
142       result = new KeywordMarkerFilter(result, stemExclusionSet);
143     result = new StempelFilter(result, new StempelStemmer(stemTable));
144     return new TokenStreamComponents(source, result);
145   }
146 }