1 package org.apache.lucene.analysis.ar;
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.CharArraySet;
29 import org.apache.lucene.analysis.KeywordMarkerFilter;
30 import org.apache.lucene.analysis.StopFilter;
31 import org.apache.lucene.analysis.StopwordAnalyzerBase;
32 import org.apache.lucene.analysis.TokenStream;
33 import org.apache.lucene.analysis.Tokenizer;
34 import org.apache.lucene.analysis.WordlistLoader;
35 import org.apache.lucene.analysis.standard.StandardTokenizer;
36 import org.apache.lucene.util.Version;
39 * {@link Analyzer} for Arabic.
41 * This analyzer implements light-stemming as specified by:
43 * Light Stemming for Arabic Information Retrieval
45 * http://www.mtholyoke.edu/~lballest/Pubs/arab_stem05.pdf
47 * The analysis package contains three primary components:
49 * <li>{@link ArabicNormalizationFilter}: Arabic orthographic normalization.
50 * <li>{@link ArabicStemFilter}: Arabic light stemming
51 * <li>Arabic stop words file: a set of default Arabic stop words.
55 public final class ArabicAnalyzer extends StopwordAnalyzerBase {
58 * File containing default Arabic stopwords.
60 * Default stopword list is from http://members.unine.ch/jacques.savoy/clef/index.html
61 * The stopword list is BSD-Licensed.
63 public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
66 * The comment character in the stopwords file. All lines prefixed with this will be ignored
67 * @deprecated use {@link WordlistLoader#getWordSet(File, String)} directly
69 // TODO make this private
71 public static final String STOPWORDS_COMMENT = "#";
74 * Returns an unmodifiable instance of the default stop-words set.
75 * @return an unmodifiable instance of the default stop-words set.
77 public static Set<?> getDefaultStopSet(){
78 return DefaultSetHolder.DEFAULT_STOP_SET;
82 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
83 * accesses the static final set the first time.;
85 private static class DefaultSetHolder {
86 static final Set<?> DEFAULT_STOP_SET;
90 DEFAULT_STOP_SET = loadStopwordSet(false, ArabicAnalyzer.class, DEFAULT_STOPWORD_FILE, STOPWORDS_COMMENT);
91 } catch (IOException ex) {
92 // default set should always be present as it is part of the
94 throw new RuntimeException("Unable to load default stopword set");
99 private final Set<?> stemExclusionSet;
102 * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
104 public ArabicAnalyzer(Version matchVersion) {
105 this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
109 * Builds an analyzer with the given stop words
111 * @param matchVersion
112 * lucene compatibility version
116 public ArabicAnalyzer(Version matchVersion, Set<?> stopwords){
117 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
121 * Builds an analyzer with the given stop word. If a none-empty stem exclusion set is
122 * provided this analyzer will add a {@link KeywordMarkerFilter} before
123 * {@link ArabicStemFilter}.
125 * @param matchVersion
126 * lucene compatibility version
129 * @param stemExclusionSet
130 * a set of terms not to be stemmed
132 public ArabicAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet){
133 super(matchVersion, stopwords);
134 this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
135 matchVersion, stemExclusionSet));
139 * Builds an analyzer with the given stop words.
140 * @deprecated use {@link #ArabicAnalyzer(Version, Set)} instead
143 public ArabicAnalyzer( Version matchVersion, String... stopwords ) {
144 this(matchVersion, StopFilter.makeStopSet(matchVersion, stopwords ));
148 * Builds an analyzer with the given stop words.
149 * @deprecated use {@link #ArabicAnalyzer(Version, Set)} instead
152 public ArabicAnalyzer( Version matchVersion, Hashtable<?,?> stopwords ) {
153 this(matchVersion, stopwords.keySet());
157 * Builds an analyzer with the given stop words. Lines can be commented out using {@link #STOPWORDS_COMMENT}
158 * @deprecated use {@link #ArabicAnalyzer(Version, Set)} instead
161 public ArabicAnalyzer( Version matchVersion, File stopwords ) throws IOException {
162 this(matchVersion, WordlistLoader.getWordSet( stopwords, STOPWORDS_COMMENT));
167 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
168 * used to tokenize all the text in the provided {@link Reader}.
170 * @return {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
171 * built from an {@link StandardTokenizer} filtered with
172 * {@link LowerCaseFilter}, {@link StopFilter},
173 * {@link ArabicNormalizationFilter}, {@link KeywordMarkerFilter}
174 * if a stem exclusion set is provided and {@link ArabicStemFilter}.
177 protected TokenStreamComponents createComponents(String fieldName,
179 final Tokenizer source = matchVersion.onOrAfter(Version.LUCENE_31) ?
180 new StandardTokenizer(matchVersion, reader) : new ArabicLetterTokenizer(matchVersion, reader);
181 TokenStream result = new LowerCaseFilter(matchVersion, source);
182 // the order here is important: the stopword list is not normalized!
183 result = new StopFilter( matchVersion, result, stopwords);
184 // TODO maybe we should make ArabicNormalization filter also KeywordAttribute aware?!
185 result = new ArabicNormalizationFilter(result);
186 if(!stemExclusionSet.isEmpty()) {
187 result = new KeywordMarkerFilter(result, stemExclusionSet);
189 return new TokenStreamComponents(source, new ArabicStemFilter(result));