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