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