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