pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / ru / RussianAnalyzer.java
1 package org.apache.lucene.analysis.ru;
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.Arrays;
23 import java.util.Map;
24 import java.util.Set;
25
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;
40
41 /**
42  * {@link Analyzer} for Russian language. 
43  * <p>
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.
47  * </p>
48  * <a name="version"/>
49  * <p>You must specify the required {@link Version}
50  * compatibility when creating RussianAnalyzer:
51  * <ul>
52  *   <li> As of 3.1, StandardTokenizer is used, Snowball stemming is done with
53  *        SnowballFilter, and Snowball stopwords are used by default.
54  * </ul>
55  */
56 public final class RussianAnalyzer extends StopwordAnalyzerBase
57 {
58     /**
59      * List of typical Russian stopwords. (for backwards compatibility)
60      * @deprecated Remove this for LUCENE 5.0
61      */
62     @Deprecated
63     private static final String[] RUSSIAN_STOP_WORDS_30 = {
64       "а", "без", "более", "бы", "был", "была", "были", "было", "быть", "в",
65       "вам", "вас", "весь", "во", "вот", "все", "всего", "всех", "вы", "где", 
66       "да", "даже", "для", "до", "его", "ее", "ей", "ею", "если", "есть", 
67       "еще", "же", "за", "здесь", "и", "из", "или", "им", "их", "к", "как",
68       "ко", "когда", "кто", "ли", "либо", "мне", "может", "мы", "на", "надо", 
69       "наш", "не", "него", "нее", "нет", "ни", "них", "но", "ну", "о", "об", 
70       "однако", "он", "она", "они", "оно", "от", "очень", "по", "под", "при", 
71       "с", "со", "так", "также", "такой", "там", "те", "тем", "то", "того", 
72       "тоже", "той", "только", "том", "ты", "у", "уже", "хотя", "чего", "чей", 
73       "чем", "что", "чтобы", "чье", "чья", "эта", "эти", "это", "я"
74     };
75     
76     /** File containing default Russian stopwords. */
77     public final static String DEFAULT_STOPWORD_FILE = "russian_stop.txt";
78     
79     private static class DefaultSetHolder {
80       /** @deprecated remove this for Lucene 5.0 */
81       @Deprecated
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;
86       
87       static {
88         try {
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
93           // distribution (JAR)
94           throw new RuntimeException("Unable to load default stopword set", ex);
95         }
96       }
97     }
98     
99     private final Set<?> stemExclusionSet;
100     
101     /**
102      * Returns an unmodifiable instance of the default stop-words set.
103      * 
104      * @return an unmodifiable instance of the default stop-words set.
105      */
106     public static Set<?> getDefaultStopSet() {
107       return DefaultSetHolder.DEFAULT_STOP_SET;
108     }
109
110     public RussianAnalyzer(Version matchVersion) {
111       this(matchVersion,
112         matchVersion.onOrAfter(Version.LUCENE_31) ? DefaultSetHolder.DEFAULT_STOP_SET
113             : DefaultSetHolder.DEFAULT_STOP_SET_30);
114     }
115   
116     /**
117      * Builds an analyzer with the given stop words.
118      * @deprecated use {@link #RussianAnalyzer(Version, Set)} instead
119      */
120     @Deprecated
121     public RussianAnalyzer(Version matchVersion, String... stopwords) {
122       this(matchVersion, StopFilter.makeStopSet(matchVersion, stopwords));
123     }
124     
125     /**
126      * Builds an analyzer with the given stop words
127      * 
128      * @param matchVersion
129      *          lucene compatibility version
130      * @param stopwords
131      *          a stopword set
132      */
133     public RussianAnalyzer(Version matchVersion, Set<?> stopwords){
134       this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
135     }
136     
137     /**
138      * Builds an analyzer with the given stop words
139      * 
140      * @param matchVersion
141      *          lucene compatibility version
142      * @param stopwords
143      *          a stopword set
144      * @param stemExclusionSet a set of words not to be stemmed
145      */
146     public RussianAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet){
147       super(matchVersion, stopwords);
148       this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(matchVersion, stemExclusionSet));
149     }
150    
151    
152     /**
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
156      */
157     @Deprecated
158     public RussianAnalyzer(Version matchVersion, Map<?,?> stopwords)
159     {
160       this(matchVersion, stopwords.keySet());
161     }
162
163   /**
164    * Creates
165    * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
166    * used to tokenize all the text in the provided {@link Reader}.
167    * 
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}
173    */
174     @Override
175     protected TokenStreamComponents createComponents(String fieldName,
176         Reader reader) {
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);
186       } else {
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));
193       }
194     }
195 }