1 package org.apache.lucene.analysis.ru;
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;
22 import java.util.Arrays;
26 import org.apache.lucene.analysis.Analyzer;
27 import org.apache.lucene.analysis.CharArraySet;
28 import org.apache.lucene.analysis.LowerCaseFilter;
29 import org.apache.lucene.analysis.snowball.SnowballFilter;
30 import org.apache.lucene.analysis.standard.StandardFilter;
31 import org.apache.lucene.analysis.standard.StandardTokenizer;
32 import org.apache.lucene.analysis.KeywordMarkerFilter;
33 import org.apache.lucene.analysis.StopFilter;
34 import org.apache.lucene.analysis.StopwordAnalyzerBase;
35 import org.apache.lucene.analysis.TokenStream;
36 import org.apache.lucene.analysis.Tokenizer;
37 import org.apache.lucene.analysis.WordlistLoader;
38 import org.apache.lucene.util.Version;
41 * {@link Analyzer} for Russian language.
43 * Supports an external list of stopwords (words that
44 * will not be indexed at all).
45 * A default set of stopwords is used unless an alternative list is specified.
48 * <p>You must specify the required {@link Version}
49 * compatibility when creating RussianAnalyzer:
51 * <li> As of 3.1, StandardTokenizer is used, Snowball stemming is done with
52 * SnowballFilter, and Snowball stopwords are used by default.
55 public final class RussianAnalyzer extends StopwordAnalyzerBase
58 * List of typical Russian stopwords. (for backwards compatibility)
59 * @deprecated Remove this for LUCENE 5.0
62 private static final String[] RUSSIAN_STOP_WORDS_30 = {
63 "а", "без", "более", "бы", "был", "была", "были", "было", "быть", "в",
64 "вам", "вас", "весь", "во", "вот", "все", "всего", "всех", "вы", "где",
65 "да", "даже", "для", "до", "его", "ее", "ей", "ею", "если", "есть",
66 "еще", "же", "за", "здесь", "и", "из", "или", "им", "их", "к", "как",
67 "ко", "когда", "кто", "ли", "либо", "мне", "может", "мы", "на", "надо",
68 "наш", "не", "него", "нее", "нет", "ни", "них", "но", "ну", "о", "об",
69 "однако", "он", "она", "они", "оно", "от", "очень", "по", "под", "при",
70 "с", "со", "так", "также", "такой", "там", "те", "тем", "то", "того",
71 "тоже", "той", "только", "том", "ты", "у", "уже", "хотя", "чего", "чей",
72 "чем", "что", "чтобы", "чье", "чья", "эта", "эти", "это", "я"
75 /** File containing default Russian stopwords. */
76 public final static String DEFAULT_STOPWORD_FILE = "russian_stop.txt";
78 private static class DefaultSetHolder {
79 /** @deprecated remove this for Lucene 5.0 */
81 static final Set<?> DEFAULT_STOP_SET_30 = CharArraySet
82 .unmodifiableSet(new CharArraySet(Version.LUCENE_CURRENT,
83 Arrays.asList(RUSSIAN_STOP_WORDS_30), false));
84 static final Set<?> DEFAULT_STOP_SET;
89 WordlistLoader.getSnowballWordSet(SnowballFilter.class, DEFAULT_STOPWORD_FILE);
90 } catch (IOException ex) {
91 // default set should always be present as it is part of the
93 throw new RuntimeException("Unable to load default stopword set");
98 private final Set<?> stemExclusionSet;
101 * Returns an unmodifiable instance of the default stop-words set.
103 * @return an unmodifiable instance of the default stop-words set.
105 public static Set<?> getDefaultStopSet() {
106 return DefaultSetHolder.DEFAULT_STOP_SET;
109 public RussianAnalyzer(Version matchVersion) {
111 matchVersion.onOrAfter(Version.LUCENE_31) ? DefaultSetHolder.DEFAULT_STOP_SET
112 : DefaultSetHolder.DEFAULT_STOP_SET_30);
116 * Builds an analyzer with the given stop words.
117 * @deprecated use {@link #RussianAnalyzer(Version, Set)} instead
120 public RussianAnalyzer(Version matchVersion, String... stopwords) {
121 this(matchVersion, StopFilter.makeStopSet(matchVersion, stopwords));
125 * Builds an analyzer with the given stop words
127 * @param matchVersion
128 * lucene compatibility version
132 public RussianAnalyzer(Version matchVersion, Set<?> stopwords){
133 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
137 * Builds an analyzer with the given stop words
139 * @param matchVersion
140 * lucene compatibility version
143 * @param stemExclusionSet a set of words not to be stemmed
145 public RussianAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet){
146 super(matchVersion, stopwords);
147 this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(matchVersion, stemExclusionSet));
152 * Builds an analyzer with the given stop words.
153 * TODO: create a Set version of this ctor
154 * @deprecated use {@link #RussianAnalyzer(Version, Set)} instead
157 public RussianAnalyzer(Version matchVersion, Map<?,?> stopwords)
159 this(matchVersion, stopwords.keySet());
164 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
165 * used to tokenize all the text in the provided {@link Reader}.
167 * @return {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
168 * built from a {@link StandardTokenizer} filtered with
169 * {@link StandardFilter}, {@link LowerCaseFilter}, {@link StopFilter}
170 * , {@link KeywordMarkerFilter} if a stem exclusion set is
171 * provided, and {@link SnowballFilter}
174 protected TokenStreamComponents createComponents(String fieldName,
176 if (matchVersion.onOrAfter(Version.LUCENE_31)) {
177 final Tokenizer source = new StandardTokenizer(matchVersion, reader);
178 TokenStream result = new StandardFilter(matchVersion, source);
179 result = new LowerCaseFilter(matchVersion, result);
180 result = new StopFilter(matchVersion, result, stopwords);
181 if (!stemExclusionSet.isEmpty()) result = new KeywordMarkerFilter(
182 result, stemExclusionSet);
183 result = new SnowballFilter(result, new org.tartarus.snowball.ext.RussianStemmer());
184 return new TokenStreamComponents(source, result);
186 final Tokenizer source = new RussianLetterTokenizer(matchVersion, reader);
187 TokenStream result = new LowerCaseFilter(matchVersion, source);
188 result = new StopFilter(matchVersion, result, stopwords);
189 if (!stemExclusionSet.isEmpty()) result = new KeywordMarkerFilter(
190 result, stemExclusionSet);
191 return new TokenStreamComponents(source, new RussianStemFilter(result));