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