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.IOUtils;
39 import org.apache.lucene.util.Version;
42 * {@link Analyzer} for Russian language.
44 * Supports an external list of stopwords (words that
45 * will not be indexed at all).
46 * A default set of stopwords is used unless an alternative list is specified.
49 * <p>You must specify the required {@link Version}
50 * compatibility when creating RussianAnalyzer:
52 * <li> As of 3.1, StandardTokenizer is used, Snowball stemming is done with
53 * SnowballFilter, and Snowball stopwords are used by default.
56 public final class RussianAnalyzer extends StopwordAnalyzerBase
59 * List of typical Russian stopwords. (for backwards compatibility)
60 * @deprecated Remove this for LUCENE 5.0
63 private static final String[] RUSSIAN_STOP_WORDS_30 = {
64 "а", "без", "более", "бы", "был", "была", "были", "было", "быть", "в",
65 "вам", "вас", "весь", "во", "вот", "все", "всего", "всех", "вы", "где",
66 "да", "даже", "для", "до", "его", "ее", "ей", "ею", "если", "есть",
67 "еще", "же", "за", "здесь", "и", "из", "или", "им", "их", "к", "как",
68 "ко", "когда", "кто", "ли", "либо", "мне", "может", "мы", "на", "надо",
69 "наш", "не", "него", "нее", "нет", "ни", "них", "но", "ну", "о", "об",
70 "однако", "он", "она", "они", "оно", "от", "очень", "по", "под", "при",
71 "с", "со", "так", "также", "такой", "там", "те", "тем", "то", "того",
72 "тоже", "той", "только", "том", "ты", "у", "уже", "хотя", "чего", "чей",
73 "чем", "что", "чтобы", "чье", "чья", "эта", "эти", "это", "я"
76 /** File containing default Russian stopwords. */
77 public final static String DEFAULT_STOPWORD_FILE = "russian_stop.txt";
79 private static class DefaultSetHolder {
80 /** @deprecated remove this for Lucene 5.0 */
82 static final Set<?> DEFAULT_STOP_SET_30 = CharArraySet
83 .unmodifiableSet(new CharArraySet(Version.LUCENE_CURRENT,
84 Arrays.asList(RUSSIAN_STOP_WORDS_30), false));
85 static final Set<?> DEFAULT_STOP_SET;
89 DEFAULT_STOP_SET = WordlistLoader.getSnowballWordSet(IOUtils.getDecodingReader(SnowballFilter.class,
90 DEFAULT_STOPWORD_FILE, IOUtils.CHARSET_UTF_8), Version.LUCENE_CURRENT);
91 } catch (IOException ex) {
92 // default set should always be present as it is part of the
94 throw new RuntimeException("Unable to load default stopword set", ex);
99 private final Set<?> stemExclusionSet;
102 * Returns an unmodifiable instance of the default stop-words set.
104 * @return an unmodifiable instance of the default stop-words set.
106 public static Set<?> getDefaultStopSet() {
107 return DefaultSetHolder.DEFAULT_STOP_SET;
110 public RussianAnalyzer(Version matchVersion) {
112 matchVersion.onOrAfter(Version.LUCENE_31) ? DefaultSetHolder.DEFAULT_STOP_SET
113 : DefaultSetHolder.DEFAULT_STOP_SET_30);
117 * Builds an analyzer with the given stop words.
118 * @deprecated use {@link #RussianAnalyzer(Version, Set)} instead
121 public RussianAnalyzer(Version matchVersion, String... stopwords) {
122 this(matchVersion, StopFilter.makeStopSet(matchVersion, stopwords));
126 * Builds an analyzer with the given stop words
128 * @param matchVersion
129 * lucene compatibility version
133 public RussianAnalyzer(Version matchVersion, Set<?> stopwords){
134 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
138 * Builds an analyzer with the given stop words
140 * @param matchVersion
141 * lucene compatibility version
144 * @param stemExclusionSet a set of words not to be stemmed
146 public RussianAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet){
147 super(matchVersion, stopwords);
148 this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(matchVersion, stemExclusionSet));
153 * Builds an analyzer with the given stop words.
154 * TODO: create a Set version of this ctor
155 * @deprecated use {@link #RussianAnalyzer(Version, Set)} instead
158 public RussianAnalyzer(Version matchVersion, Map<?,?> stopwords)
160 this(matchVersion, stopwords.keySet());
165 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
166 * used to tokenize all the text in the provided {@link Reader}.
168 * @return {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
169 * built from a {@link StandardTokenizer} filtered with
170 * {@link StandardFilter}, {@link LowerCaseFilter}, {@link StopFilter}
171 * , {@link KeywordMarkerFilter} if a stem exclusion set is
172 * provided, and {@link SnowballFilter}
175 protected TokenStreamComponents createComponents(String fieldName,
177 if (matchVersion.onOrAfter(Version.LUCENE_31)) {
178 final Tokenizer source = new StandardTokenizer(matchVersion, reader);
179 TokenStream result = new StandardFilter(matchVersion, source);
180 result = new LowerCaseFilter(matchVersion, result);
181 result = new StopFilter(matchVersion, result, stopwords);
182 if (!stemExclusionSet.isEmpty()) result = new KeywordMarkerFilter(
183 result, stemExclusionSet);
184 result = new SnowballFilter(result, new org.tartarus.snowball.ext.RussianStemmer());
185 return new TokenStreamComponents(source, result);
187 final Tokenizer source = new RussianLetterTokenizer(matchVersion, reader);
188 TokenStream result = new LowerCaseFilter(matchVersion, source);
189 result = new StopFilter(matchVersion, result, stopwords);
190 if (!stemExclusionSet.isEmpty()) result = new KeywordMarkerFilter(
191 result, stemExclusionSet);
192 return new TokenStreamComponents(source, new RussianStemFilter(result));