1 package org.apache.lucene.analysis.hi;
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.LowerCaseFilter;
25 import org.apache.lucene.analysis.CharArraySet;
26 import org.apache.lucene.analysis.KeywordMarkerFilter;
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.in.IndicNormalizationFilter;
32 import org.apache.lucene.analysis.in.IndicTokenizer;
33 import org.apache.lucene.util.Version;
38 public final class HindiAnalyzer extends StopwordAnalyzerBase {
39 private final Set<?> stemExclusionSet;
42 * File containing default Hindi stopwords.
44 * Default stopword list is from http://members.unine.ch/jacques.savoy/clef/index.html
45 * The stopword list is BSD-Licensed.
47 public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
48 private static final String STOPWORDS_COMMENT = "#";
51 * Returns an unmodifiable instance of the default stop-words set.
52 * @return an unmodifiable instance of the default stop-words set.
54 public static Set<?> getDefaultStopSet(){
55 return DefaultSetHolder.DEFAULT_STOP_SET;
59 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
60 * accesses the static final set the first time.;
62 private static class DefaultSetHolder {
63 static final Set<?> DEFAULT_STOP_SET;
67 DEFAULT_STOP_SET = loadStopwordSet(false, HindiAnalyzer.class, DEFAULT_STOPWORD_FILE, STOPWORDS_COMMENT);
68 } catch (IOException ex) {
69 // default set should always be present as it is part of the
71 throw new RuntimeException("Unable to load default stopword set");
77 * Builds an analyzer with the given stop words
79 * @param version lucene compatibility version
80 * @param stopwords a stopword set
81 * @param stemExclusionSet a stemming exclusion set
83 public HindiAnalyzer(Version version, Set<?> stopwords, Set<?> stemExclusionSet) {
84 super(version, stopwords);
85 this.stemExclusionSet = CharArraySet.unmodifiableSet(
86 CharArraySet.copy(matchVersion, stemExclusionSet));
90 * Builds an analyzer with the given stop words
92 * @param version lucene compatibility version
93 * @param stopwords a stopword set
95 public HindiAnalyzer(Version version, Set<?> stopwords) {
96 this(version, stopwords, CharArraySet.EMPTY_SET);
100 * Builds an analyzer with the default stop words:
101 * {@link #DEFAULT_STOPWORD_FILE}.
103 public HindiAnalyzer(Version version) {
104 this(version, DefaultSetHolder.DEFAULT_STOP_SET);
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 a {@link IndicTokenizer} filtered with
114 * {@link LowerCaseFilter}, {@link IndicNormalizationFilter},
115 * {@link HindiNormalizationFilter}, {@link KeywordMarkerFilter}
116 * if a stem exclusion set is provided, {@link HindiStemFilter}, and
120 protected TokenStreamComponents createComponents(String fieldName,
122 final Tokenizer source = new IndicTokenizer(matchVersion, reader);
123 TokenStream result = new LowerCaseFilter(matchVersion, source);
124 if (!stemExclusionSet.isEmpty())
125 result = new KeywordMarkerFilter(result, stemExclusionSet);
126 result = new IndicNormalizationFilter(result);
127 result = new HindiNormalizationFilter(result);
128 result = new StopFilter(matchVersion, result, stopwords);
129 result = new HindiStemFilter(result);
130 return new TokenStreamComponents(source, result);