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.IOUtils;
38 import org.apache.lucene.util.Version;
39 import org.egothor.stemmer.Trie;
42 * {@link Analyzer} for Polish.
44 public final class PolishAnalyzer extends StopwordAnalyzerBase {
45 private final Set<?> stemExclusionSet;
46 private final Trie stemTable;
48 /** File containing default Polish stopwords. */
49 public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
51 /** File containing default Polish stemmer table. */
52 public final static String DEFAULT_STEMMER_FILE = "stemmer_20000.tbl";
55 * Returns an unmodifiable instance of the default stop words set.
56 * @return default stop words set.
58 public static Set<?> getDefaultStopSet(){
59 return DefaultsHolder.DEFAULT_STOP_SET;
63 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
64 * accesses the static final set the first time.;
66 private static class DefaultsHolder {
67 static final Set<?> DEFAULT_STOP_SET;
68 static final Trie DEFAULT_TABLE;
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
77 throw new RuntimeException("Unable to load default stopword set", ex);
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
85 throw new RuntimeException("Unable to load default stemming tables", ex);
91 * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
93 public PolishAnalyzer(Version matchVersion) {
94 this(matchVersion, DefaultsHolder.DEFAULT_STOP_SET);
98 * Builds an analyzer with the given stop words.
100 * @param matchVersion lucene compatibility version
101 * @param stopwords a stopword set
103 public PolishAnalyzer(Version matchVersion, Set<?> stopwords) {
104 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
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
112 * @param matchVersion lucene compatibility version
113 * @param stopwords a stopword set
114 * @param stemExclusionSet a set of terms not to be stemmed
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));
125 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
126 * which tokenizes all the text in the provided {@link Reader}.
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}.
136 protected TokenStreamComponents createComponents(String fieldName,
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);