1 package org.apache.lucene.analysis.id;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
21 import java.io.Reader;
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;
36 * Analyzer for Indonesian (Bahasa)
38 public final class IndonesianAnalyzer extends StopwordAnalyzerBase {
39 /** File containing default Indonesian stopwords. */
40 public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
43 * Returns an unmodifiable instance of the default stop-words set.
44 * @return an unmodifiable instance of the default stop-words set.
46 public static Set<?> getDefaultStopSet(){
47 return DefaultSetHolder.DEFAULT_STOP_SET;
51 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
52 * accesses the static final set the first time.;
54 private static class DefaultSetHolder {
55 static final Set<?> DEFAULT_STOP_SET;
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
63 throw new RuntimeException("Unable to load default stopword set");
68 private final Set<?> stemExclusionSet;
71 * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
73 public IndonesianAnalyzer(Version matchVersion) {
74 this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
78 * Builds an analyzer with the given stop words
81 * lucene compatibility version
85 public IndonesianAnalyzer(Version matchVersion, Set<?> stopwords){
86 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
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}.
95 * lucene compatibility version
98 * @param stemExclusionSet
99 * a set of terms not to be stemmed
101 public IndonesianAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet){
102 super(matchVersion, stopwords);
103 this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
104 matchVersion, stemExclusionSet));
109 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
110 * used to tokenize all the text in the provided {@link Reader}.
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}.
119 protected TokenStreamComponents createComponents(String fieldName,
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);
128 return new TokenStreamComponents(source, new IndonesianStemFilter(result));