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