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.IOUtils;
39 import org.apache.lucene.util.Version;
42 * {@link Analyzer} for Persian.
44 * This Analyzer uses {@link ArabicLetterTokenizer} which implies tokenizing around
45 * zero-width non-joiner in addition to whitespace. Some persian-specific variant forms (such as farsi
46 * yeh and keheh) are standardized. "Stemming" is accomplished via stopwords.
49 public final class PersianAnalyzer extends StopwordAnalyzerBase {
52 * File containing default Persian stopwords.
54 * Default stopword list is from
55 * http://members.unine.ch/jacques.savoy/clef/index.html The stopword list is
59 public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
62 * The comment character in the stopwords file. All lines prefixed with this
65 public static final String STOPWORDS_COMMENT = "#";
68 * Returns an unmodifiable instance of the default stop-words set.
69 * @return an unmodifiable instance of the default stop-words set.
71 public static Set<?> getDefaultStopSet(){
72 return DefaultSetHolder.DEFAULT_STOP_SET;
76 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
77 * accesses the static final set the first time.;
79 private static class DefaultSetHolder {
80 static final Set<?> DEFAULT_STOP_SET;
84 DEFAULT_STOP_SET = loadStopwordSet(false, PersianAnalyzer.class, DEFAULT_STOPWORD_FILE, STOPWORDS_COMMENT);
85 } catch (IOException ex) {
86 // default set should always be present as it is part of the
88 throw new RuntimeException("Unable to load default stopword set");
94 * Builds an analyzer with the default stop words:
95 * {@link #DEFAULT_STOPWORD_FILE}.
97 public PersianAnalyzer(Version matchVersion) {
98 this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
102 * Builds an analyzer with the given stop words
104 * @param matchVersion
105 * lucene compatibility version
109 public PersianAnalyzer(Version matchVersion, Set<?> stopwords){
110 super(matchVersion, stopwords);
114 * Builds an analyzer with the given stop words.
115 * @deprecated use {@link #PersianAnalyzer(Version, Set)} instead
118 public PersianAnalyzer(Version matchVersion, String... stopwords) {
119 this(matchVersion, StopFilter.makeStopSet(matchVersion, stopwords));
123 * Builds an analyzer with the given stop words.
124 * @deprecated use {@link #PersianAnalyzer(Version, Set)} instead
127 public PersianAnalyzer(Version matchVersion, Hashtable<?, ?> stopwords) {
128 this(matchVersion, stopwords.keySet());
132 * Builds an analyzer with the given stop words. Lines can be commented out
133 * using {@link #STOPWORDS_COMMENT}
134 * @deprecated use {@link #PersianAnalyzer(Version, Set)} instead
137 public PersianAnalyzer(Version matchVersion, File stopwords) throws IOException {
138 this(matchVersion, WordlistLoader.getWordSet(
139 IOUtils.getDecodingReader(stopwords, IOUtils.CHARSET_UTF_8),
140 STOPWORDS_COMMENT, matchVersion));
145 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
146 * used to tokenize all the text in the provided {@link Reader}.
148 * @return {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
149 * built from a {@link StandardTokenizer} filtered with
150 * {@link LowerCaseFilter}, {@link ArabicNormalizationFilter},
151 * {@link PersianNormalizationFilter} and Persian Stop words
154 protected TokenStreamComponents createComponents(String fieldName,
156 final Tokenizer source;
157 if (matchVersion.onOrAfter(Version.LUCENE_31)) {
158 source = new StandardTokenizer(matchVersion, reader);
160 source = new ArabicLetterTokenizer(matchVersion, reader);
162 TokenStream result = new LowerCaseFilter(matchVersion, source);
163 result = new ArabicNormalizationFilter(result);
164 /* additional persian-specific normalization */
165 result = new PersianNormalizationFilter(result);
167 * the order here is important: the stopword list is normalized with the
170 return new TokenStreamComponents(source, new StopFilter(matchVersion, result, stopwords));
174 * Wraps the Reader with {@link PersianCharFilter}
177 protected Reader initReader(Reader reader) {
178 return matchVersion.onOrAfter(Version.LUCENE_31) ?
179 new PersianCharFilter(CharReader.get(reader)) :