1 package org.apache.lucene.analysis.fa;
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.
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.util.Hashtable;
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;
38 import org.apache.lucene.util.Version;
41 * {@link Analyzer} for Persian.
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.
48 public final class PersianAnalyzer extends StopwordAnalyzerBase {
51 * File containing default Persian stopwords.
53 * Default stopword list is from
54 * http://members.unine.ch/jacques.savoy/clef/index.html The stopword list is
58 public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
61 * The comment character in the stopwords file. All lines prefixed with this
64 public static final String STOPWORDS_COMMENT = "#";
67 * Returns an unmodifiable instance of the default stop-words set.
68 * @return an unmodifiable instance of the default stop-words set.
70 public static Set<?> getDefaultStopSet(){
71 return DefaultSetHolder.DEFAULT_STOP_SET;
75 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
76 * accesses the static final set the first time.;
78 private static class DefaultSetHolder {
79 static final Set<?> DEFAULT_STOP_SET;
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
87 throw new RuntimeException("Unable to load default stopword set");
93 * Builds an analyzer with the default stop words:
94 * {@link #DEFAULT_STOPWORD_FILE}.
96 public PersianAnalyzer(Version matchVersion) {
97 this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
101 * Builds an analyzer with the given stop words
103 * @param matchVersion
104 * lucene compatibility version
108 public PersianAnalyzer(Version matchVersion, Set<?> stopwords){
109 super(matchVersion, stopwords);
113 * Builds an analyzer with the given stop words.
114 * @deprecated use {@link #PersianAnalyzer(Version, Set)} instead
117 public PersianAnalyzer(Version matchVersion, String... stopwords) {
118 this(matchVersion, StopFilter.makeStopSet(matchVersion, stopwords));
122 * Builds an analyzer with the given stop words.
123 * @deprecated use {@link #PersianAnalyzer(Version, Set)} instead
126 public PersianAnalyzer(Version matchVersion, Hashtable<?, ?> stopwords) {
127 this(matchVersion, stopwords.keySet());
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
136 public PersianAnalyzer(Version matchVersion, File stopwords) throws IOException {
137 this(matchVersion, WordlistLoader.getWordSet(stopwords, STOPWORDS_COMMENT));
142 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
143 * used to tokenize all the text in the provided {@link Reader}.
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
151 protected TokenStreamComponents createComponents(String fieldName,
153 final Tokenizer source;
154 if (matchVersion.onOrAfter(Version.LUCENE_31)) {
155 source = new StandardTokenizer(matchVersion, reader);
157 source = new ArabicLetterTokenizer(matchVersion, reader);
159 TokenStream result = new LowerCaseFilter(matchVersion, source);
160 result = new ArabicNormalizationFilter(result);
161 /* additional persian-specific normalization */
162 result = new PersianNormalizationFilter(result);
164 * the order here is important: the stopword list is normalized with the
167 return new TokenStreamComponents(source, new StopFilter(matchVersion, result, stopwords));
171 * Wraps the Reader with {@link PersianCharFilter}
174 protected Reader initReader(Reader reader) {
175 return matchVersion.onOrAfter(Version.LUCENE_31) ?
176 new PersianCharFilter(CharReader.get(reader)) :