add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / hi / HindiAnalyzer.java
1 package org.apache.lucene.analysis.hi;
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.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;
34
35 /**
36  * Analyzer for Hindi.
37  */
38 public final class HindiAnalyzer extends StopwordAnalyzerBase {
39   private final Set<?> stemExclusionSet;
40   
41   /**
42    * File containing default Hindi stopwords.
43    * 
44    * Default stopword list is from http://members.unine.ch/jacques.savoy/clef/index.html
45    * The stopword list is BSD-Licensed.
46    */
47   public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
48   private static final String STOPWORDS_COMMENT = "#";
49   
50   /**
51    * Returns an unmodifiable instance of the default stop-words set.
52    * @return an unmodifiable instance of the default stop-words set.
53    */
54   public static Set<?> getDefaultStopSet(){
55     return DefaultSetHolder.DEFAULT_STOP_SET;
56   }
57   
58   /**
59    * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class 
60    * accesses the static final set the first time.;
61    */
62   private static class DefaultSetHolder {
63     static final Set<?> DEFAULT_STOP_SET;
64
65     static {
66       try {
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
70         // distribution (JAR)
71         throw new RuntimeException("Unable to load default stopword set");
72       }
73     }
74   }
75   
76   /**
77    * Builds an analyzer with the given stop words
78    * 
79    * @param version lucene compatibility version
80    * @param stopwords a stopword set
81    * @param stemExclusionSet a stemming exclusion set
82    */
83   public HindiAnalyzer(Version version, Set<?> stopwords, Set<?> stemExclusionSet) {
84     super(version, stopwords);
85     this.stemExclusionSet = CharArraySet.unmodifiableSet(
86         CharArraySet.copy(matchVersion, stemExclusionSet));
87   }
88   
89   /**
90    * Builds an analyzer with the given stop words 
91    * 
92    * @param version lucene compatibility version
93    * @param stopwords a stopword set
94    */
95   public HindiAnalyzer(Version version, Set<?> stopwords) {
96     this(version, stopwords, CharArraySet.EMPTY_SET);
97   }
98   
99   /**
100    * Builds an analyzer with the default stop words:
101    * {@link #DEFAULT_STOPWORD_FILE}.
102    */
103   public HindiAnalyzer(Version version) {
104     this(version, DefaultSetHolder.DEFAULT_STOP_SET);
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 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
117    *         Hindi Stop words
118    */
119   @Override
120   protected TokenStreamComponents createComponents(String fieldName,
121       Reader reader) {
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);
131   }
132 }