1 package org.apache.lucene.analysis.ro;
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.
20 import java.io.IOException;
21 import java.io.Reader;
24 import org.apache.lucene.analysis.Analyzer;
25 import org.apache.lucene.analysis.CharArraySet;
26 import org.apache.lucene.analysis.KeywordMarkerFilter;
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.TokenStream;
31 import org.apache.lucene.analysis.Tokenizer;
32 import org.apache.lucene.analysis.snowball.SnowballFilter;
33 import org.apache.lucene.analysis.standard.StandardFilter;
34 import org.apache.lucene.analysis.standard.StandardTokenizer;
35 import org.apache.lucene.util.Version;
36 import org.tartarus.snowball.ext.RomanianStemmer;
39 * {@link Analyzer} for Romanian.
41 public final class RomanianAnalyzer extends StopwordAnalyzerBase {
42 private final Set<?> stemExclusionSet;
44 /** File containing default Romanian stopwords. */
45 public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
47 * The comment character in the stopwords file.
48 * All lines prefixed with this will be ignored.
50 private static final String STOPWORDS_COMMENT = "#";
53 * Returns an unmodifiable instance of the default stop words set.
54 * @return default stop words set.
56 public static Set<?> getDefaultStopSet(){
57 return DefaultSetHolder.DEFAULT_STOP_SET;
61 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
62 * accesses the static final set the first time.;
64 private static class DefaultSetHolder {
65 static final Set<?> DEFAULT_STOP_SET;
69 DEFAULT_STOP_SET = loadStopwordSet(false, RomanianAnalyzer.class,
70 DEFAULT_STOPWORD_FILE, STOPWORDS_COMMENT);
71 } catch (IOException ex) {
72 // default set should always be present as it is part of the
74 throw new RuntimeException("Unable to load default stopword set");
80 * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
82 public RomanianAnalyzer(Version matchVersion) {
83 this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
87 * Builds an analyzer with the given stop words.
89 * @param matchVersion lucene compatibility version
90 * @param stopwords a stopword set
92 public RomanianAnalyzer(Version matchVersion, Set<?> stopwords) {
93 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
97 * Builds an analyzer with the given stop words. If a non-empty stem exclusion set is
98 * provided this analyzer will add a {@link KeywordMarkerFilter} before
101 * @param matchVersion lucene compatibility version
102 * @param stopwords a stopword set
103 * @param stemExclusionSet a set of terms not to be stemmed
105 public RomanianAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
106 super(matchVersion, stopwords);
107 this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
108 matchVersion, stemExclusionSet));
113 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
114 * which tokenizes all the text in the provided {@link Reader}.
117 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
118 * built from an {@link StandardTokenizer} filtered with
119 * {@link StandardFilter}, {@link LowerCaseFilter}, {@link StopFilter}
120 * , {@link KeywordMarkerFilter} if a stem exclusion set is
121 * provided and {@link SnowballFilter}.
124 protected TokenStreamComponents createComponents(String fieldName,
126 final Tokenizer source = new StandardTokenizer(matchVersion, reader);
127 TokenStream result = new StandardFilter(matchVersion, source);
128 result = new LowerCaseFilter(matchVersion, result);
129 result = new StopFilter(matchVersion, result, stopwords);
130 if(!stemExclusionSet.isEmpty())
131 result = new KeywordMarkerFilter(result, stemExclusionSet);
132 result = new SnowballFilter(result, new RomanianStemmer());
133 return new TokenStreamComponents(source, result);