add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / ro / RomanianAnalyzer.java
1 package org.apache.lucene.analysis.ro;
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.Set;
23
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.snowball.SnowballFilter;
33 import org.apache.lucene.analysis.standard.StandardFilter;
34 import org.apache.lucene.analysis.standard.StandardTokenizer;
35 import org.apache.lucene.util.Version;
36 import org.tartarus.snowball.ext.RomanianStemmer;
37
38 /**
39  * {@link Analyzer} for Romanian.
40  */
41 public final class RomanianAnalyzer extends StopwordAnalyzerBase {
42   private final Set<?> stemExclusionSet;
43   
44   /** File containing default Romanian stopwords. */
45   public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
46   /**
47    * The comment character in the stopwords file.  
48    * All lines prefixed with this will be ignored.
49    */
50   private static final String STOPWORDS_COMMENT = "#";
51   
52   /**
53    * Returns an unmodifiable instance of the default stop words set.
54    * @return default stop words set.
55    */
56   public static Set<?> getDefaultStopSet(){
57     return DefaultSetHolder.DEFAULT_STOP_SET;
58   }
59   
60   /**
61    * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class 
62    * accesses the static final set the first time.;
63    */
64   private static class DefaultSetHolder {
65     static final Set<?> DEFAULT_STOP_SET;
66
67     static {
68       try {
69         DEFAULT_STOP_SET = loadStopwordSet(false, RomanianAnalyzer.class, 
70             DEFAULT_STOPWORD_FILE, STOPWORDS_COMMENT);
71       } catch (IOException ex) {
72         // default set should always be present as it is part of the
73         // distribution (JAR)
74         throw new RuntimeException("Unable to load default stopword set");
75       }
76     }
77   }
78
79   /**
80    * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
81    */
82   public RomanianAnalyzer(Version matchVersion) {
83     this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
84   }
85   
86   /**
87    * Builds an analyzer with the given stop words.
88    * 
89    * @param matchVersion lucene compatibility version
90    * @param stopwords a stopword set
91    */
92   public RomanianAnalyzer(Version matchVersion, Set<?> stopwords) {
93     this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
94   }
95
96   /**
97    * Builds an analyzer with the given stop words. If a non-empty stem exclusion set is
98    * provided this analyzer will add a {@link KeywordMarkerFilter} before
99    * stemming.
100    * 
101    * @param matchVersion lucene compatibility version
102    * @param stopwords a stopword set
103    * @param stemExclusionSet a set of terms not to be stemmed
104    */
105   public RomanianAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
106     super(matchVersion, stopwords);
107     this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
108         matchVersion, stemExclusionSet));
109   }
110
111   /**
112    * Creates a
113    * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
114    * which tokenizes all the text in the provided {@link Reader}.
115    * 
116    * @return A
117    *         {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
118    *         built from an {@link StandardTokenizer} filtered with
119    *         {@link StandardFilter}, {@link LowerCaseFilter}, {@link StopFilter}
120    *         , {@link KeywordMarkerFilter} if a stem exclusion set is
121    *         provided and {@link SnowballFilter}.
122    */
123   @Override
124   protected TokenStreamComponents createComponents(String fieldName,
125       Reader reader) {
126     final Tokenizer source = new StandardTokenizer(matchVersion, reader);
127     TokenStream result = new StandardFilter(matchVersion, source);
128     result = new LowerCaseFilter(matchVersion, result);
129     result = new StopFilter(matchVersion, result, stopwords);
130     if(!stemExclusionSet.isEmpty())
131       result = new KeywordMarkerFilter(result, stemExclusionSet);
132     result = new SnowballFilter(result, new RomanianStemmer());
133     return new TokenStreamComponents(source, result);
134   }
135 }