add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / fa / PersianAnalyzer.java
1 package org.apache.lucene.analysis.fa;
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.File;
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.util.Hashtable;
24 import java.util.Set;
25
26 import org.apache.lucene.analysis.Analyzer;
27 import org.apache.lucene.analysis.LowerCaseFilter;
28 import org.apache.lucene.analysis.StopFilter;
29 import org.apache.lucene.analysis.StopwordAnalyzerBase;
30 import org.apache.lucene.analysis.CharReader;
31 import org.apache.lucene.analysis.TokenStream;
32 import org.apache.lucene.analysis.Tokenizer;
33 import org.apache.lucene.analysis.WordlistLoader;
34 import org.apache.lucene.analysis.ar.ArabicLetterTokenizer;
35 import org.apache.lucene.analysis.ar.ArabicNormalizationFilter;
36 import org.apache.lucene.analysis.standard.StandardTokenizer;
37
38 import org.apache.lucene.util.Version;
39
40 /**
41  * {@link Analyzer} for Persian.
42  * <p>
43  * This Analyzer uses {@link ArabicLetterTokenizer} which implies tokenizing around
44  * zero-width non-joiner in addition to whitespace. Some persian-specific variant forms (such as farsi
45  * yeh and keheh) are standardized. "Stemming" is accomplished via stopwords.
46  * </p>
47  */
48 public final class PersianAnalyzer extends StopwordAnalyzerBase {
49
50   /**
51    * File containing default Persian stopwords.
52    * 
53    * Default stopword list is from
54    * http://members.unine.ch/jacques.savoy/clef/index.html The stopword list is
55    * BSD-Licensed.
56    * 
57    */
58   public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
59
60   /**
61    * The comment character in the stopwords file. All lines prefixed with this
62    * will be ignored
63    */
64   public static final String STOPWORDS_COMMENT = "#";
65   
66   /**
67    * Returns an unmodifiable instance of the default stop-words set.
68    * @return an unmodifiable instance of the default stop-words set.
69    */
70   public static Set<?> getDefaultStopSet(){
71     return DefaultSetHolder.DEFAULT_STOP_SET;
72   }
73   
74   /**
75    * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class 
76    * accesses the static final set the first time.;
77    */
78   private static class DefaultSetHolder {
79     static final Set<?> DEFAULT_STOP_SET;
80
81     static {
82       try {
83         DEFAULT_STOP_SET = loadStopwordSet(false, PersianAnalyzer.class, DEFAULT_STOPWORD_FILE, STOPWORDS_COMMENT);
84       } catch (IOException ex) {
85         // default set should always be present as it is part of the
86         // distribution (JAR)
87         throw new RuntimeException("Unable to load default stopword set");
88       }
89     }
90   }
91
92   /**
93    * Builds an analyzer with the default stop words:
94    * {@link #DEFAULT_STOPWORD_FILE}.
95    */
96   public PersianAnalyzer(Version matchVersion) {
97     this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
98   }
99   
100   /**
101    * Builds an analyzer with the given stop words 
102    * 
103    * @param matchVersion
104    *          lucene compatibility version
105    * @param stopwords
106    *          a stopword set
107    */
108   public PersianAnalyzer(Version matchVersion, Set<?> stopwords){
109     super(matchVersion, stopwords);
110   }
111
112   /**
113    * Builds an analyzer with the given stop words.
114    * @deprecated use {@link #PersianAnalyzer(Version, Set)} instead
115    */
116   @Deprecated
117   public PersianAnalyzer(Version matchVersion, String... stopwords) {
118     this(matchVersion, StopFilter.makeStopSet(matchVersion, stopwords));
119   }
120
121   /**
122    * Builds an analyzer with the given stop words.
123    * @deprecated use {@link #PersianAnalyzer(Version, Set)} instead
124    */
125   @Deprecated
126   public PersianAnalyzer(Version matchVersion, Hashtable<?, ?> stopwords) {
127     this(matchVersion, stopwords.keySet());
128   }
129
130   /**
131    * Builds an analyzer with the given stop words. Lines can be commented out
132    * using {@link #STOPWORDS_COMMENT}
133    * @deprecated use {@link #PersianAnalyzer(Version, Set)} instead
134    */
135   @Deprecated
136   public PersianAnalyzer(Version matchVersion, File stopwords) throws IOException {
137     this(matchVersion, WordlistLoader.getWordSet(stopwords, STOPWORDS_COMMENT));
138   }
139
140   /**
141    * Creates
142    * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
143    * used to tokenize all the text in the provided {@link Reader}.
144    * 
145    * @return {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
146    *         built from a {@link StandardTokenizer} filtered with
147    *         {@link LowerCaseFilter}, {@link ArabicNormalizationFilter},
148    *         {@link PersianNormalizationFilter} and Persian Stop words
149    */
150   @Override
151   protected TokenStreamComponents createComponents(String fieldName,
152       Reader reader) {
153     final Tokenizer source;
154     if (matchVersion.onOrAfter(Version.LUCENE_31)) {
155       source = new StandardTokenizer(matchVersion, reader);
156     } else {
157       source = new ArabicLetterTokenizer(matchVersion, reader);
158     }
159     TokenStream result = new LowerCaseFilter(matchVersion, source);
160     result = new ArabicNormalizationFilter(result);
161     /* additional persian-specific normalization */
162     result = new PersianNormalizationFilter(result);
163     /*
164      * the order here is important: the stopword list is normalized with the
165      * above!
166      */
167     return new TokenStreamComponents(source, new StopFilter(matchVersion, result, stopwords));
168   }
169   
170   /** 
171    * Wraps the Reader with {@link PersianCharFilter}
172    */
173   @Override
174   protected Reader initReader(Reader reader) {
175     return matchVersion.onOrAfter(Version.LUCENE_31) ? 
176        new PersianCharFilter(CharReader.get(reader)) :
177        reader;
178   }
179 }