1 package org.apache.lucene.analysis.pl;
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.IOException;
21 import java.io.Reader;
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;
41 * {@link Analyzer} for Polish.
43 public final class PolishAnalyzer extends StopwordAnalyzerBase {
44 private final Set<?> stemExclusionSet;
45 private final Trie stemTable;
47 /** File containing default Polish stopwords. */
48 public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
50 /** File containing default Polish stemmer table. */
51 public final static String DEFAULT_STEMMER_FILE = "stemmer_20000.tbl";
54 * Returns an unmodifiable instance of the default stop words set.
55 * @return default stop words set.
57 public static Set<?> getDefaultStopSet(){
58 return DefaultsHolder.DEFAULT_STOP_SET;
62 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
63 * accesses the static final set the first time.;
65 private static class DefaultsHolder {
66 static final Set<?> DEFAULT_STOP_SET;
67 static final Trie DEFAULT_TABLE;
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
76 throw new RuntimeException("Unable to load default stopword set", ex);
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
84 throw new RuntimeException("Unable to load default stemming tables", ex);
90 * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
92 public PolishAnalyzer(Version matchVersion) {
93 this(matchVersion, DefaultsHolder.DEFAULT_STOP_SET);
97 * Builds an analyzer with the given stop words.
99 * @param matchVersion lucene compatibility version
100 * @param stopwords a stopword set
102 public PolishAnalyzer(Version matchVersion, Set<?> stopwords) {
103 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
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
111 * @param matchVersion lucene compatibility version
112 * @param stopwords a stopword set
113 * @param stemExclusionSet a set of terms not to be stemmed
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));
124 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
125 * which tokenizes all the text in the provided {@link Reader}.
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}.
135 protected TokenStreamComponents createComponents(String fieldName,
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);