1 package org.apache.lucene.analysis.bg;
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;
25 import org.apache.lucene.analysis.Analyzer;
26 import org.apache.lucene.analysis.LowerCaseFilter;
27 import org.apache.lucene.analysis.CharArraySet;
28 import org.apache.lucene.analysis.KeywordMarkerFilter;
29 import org.apache.lucene.analysis.StopFilter;
30 import org.apache.lucene.analysis.StopwordAnalyzerBase;
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.standard.StandardFilter;
35 import org.apache.lucene.analysis.standard.StandardTokenizer;
36 import org.apache.lucene.util.Version;
39 * {@link Analyzer} for Bulgarian.
41 * This analyzer implements light-stemming as specified by: <i> Searching
42 * Strategies for the Bulgarian Language </i>
43 * http://members.unine.ch/jacques.savoy/Papers/BUIR.pdf
46 public final class BulgarianAnalyzer extends StopwordAnalyzerBase {
49 * File containing default Bulgarian stopwords.
51 * Default stopword list is from
52 * http://members.unine.ch/jacques.savoy/clef/index.html The stopword list is
55 public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
58 * The comment character in the stopwords file. All lines prefixed with this
60 * @deprecated use {@link WordlistLoader#getWordSet(File, String)} directly
62 //TODO make this private
64 public static final String STOPWORDS_COMMENT = "#";
67 * 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
77 * class 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, BulgarianAnalyzer.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");
93 private final Set<?> stemExclusionSet;
96 * Builds an analyzer with the default stop words:
97 * {@link #DEFAULT_STOPWORD_FILE}.
99 public BulgarianAnalyzer(Version matchVersion) {
100 this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
104 * Builds an analyzer with the given stop words.
106 public BulgarianAnalyzer(Version matchVersion, Set<?> stopwords) {
107 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
111 * Builds an analyzer with the given stop words and a stem exclusion set.
112 * If a stem exclusion set is provided this analyzer will add a {@link KeywordMarkerFilter}
113 * before {@link BulgarianStemFilter}.
115 public BulgarianAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
116 super(matchVersion, stopwords);
117 this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
118 matchVersion, stemExclusionSet)); }
122 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
123 * which tokenizes all the text in the provided {@link Reader}.
126 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
127 * built from an {@link StandardTokenizer} filtered with
128 * {@link StandardFilter}, {@link LowerCaseFilter}, {@link StopFilter}
129 * , {@link KeywordMarkerFilter} if a stem exclusion set is
130 * provided and {@link BulgarianStemFilter}.
133 public TokenStreamComponents createComponents(String fieldName, Reader reader) {
134 final Tokenizer source = new StandardTokenizer(matchVersion, reader);
135 TokenStream result = new StandardFilter(matchVersion, source);
136 result = new LowerCaseFilter(matchVersion, result);
137 result = new StopFilter(matchVersion, result, stopwords);
138 if(!stemExclusionSet.isEmpty())
139 result = new KeywordMarkerFilter(result, stemExclusionSet);
140 result = new BulgarianStemFilter(result);
141 return new TokenStreamComponents(source, result);