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