1 package org.apache.lucene.analysis.lv;
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.LowerCaseFilter;
26 import org.apache.lucene.analysis.StopFilter;
27 import org.apache.lucene.analysis.KeywordMarkerFilter;
28 import org.apache.lucene.analysis.TokenStream;
29 import org.apache.lucene.analysis.Tokenizer;
30 import org.apache.lucene.analysis.standard.StandardFilter;
31 import org.apache.lucene.analysis.standard.StandardTokenizer;
32 import org.apache.lucene.analysis.CharArraySet;
33 import org.apache.lucene.analysis.StopwordAnalyzerBase;
34 import org.apache.lucene.analysis.WordlistLoader;
35 import org.apache.lucene.util.Version;
38 * {@link Analyzer} for Latvian.
40 public final class LatvianAnalyzer extends StopwordAnalyzerBase {
41 private final Set<?> stemExclusionSet;
43 /** File containing default Latvian stopwords. */
44 public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
47 * Returns an unmodifiable instance of the default stop words set.
48 * @return default stop words set.
50 public static Set<?> getDefaultStopSet(){
51 return DefaultSetHolder.DEFAULT_STOP_SET;
55 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
56 * accesses the static final set the first time.;
58 private static class DefaultSetHolder {
59 static final Set<?> DEFAULT_STOP_SET;
63 DEFAULT_STOP_SET = WordlistLoader.getWordSet(LatvianAnalyzer.class,
64 DEFAULT_STOPWORD_FILE);
65 } catch (IOException ex) {
66 // default set should always be present as it is part of the
68 throw new RuntimeException("Unable to load default stopword set");
74 * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
76 public LatvianAnalyzer(Version matchVersion) {
77 this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
81 * Builds an analyzer with the given stop words.
83 * @param matchVersion lucene compatibility version
84 * @param stopwords a stopword set
86 public LatvianAnalyzer(Version matchVersion, Set<?> stopwords) {
87 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
91 * Builds an analyzer with the given stop words. If a non-empty stem exclusion set is
92 * provided this analyzer will add a {@link KeywordMarkerFilter} before
95 * @param matchVersion lucene compatibility version
96 * @param stopwords a stopword set
97 * @param stemExclusionSet a set of terms not to be stemmed
99 public LatvianAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
100 super(matchVersion, stopwords);
101 this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
102 matchVersion, stemExclusionSet));
107 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
108 * which tokenizes all the text in the provided {@link Reader}.
111 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
112 * built from an {@link StandardTokenizer} filtered with
113 * {@link StandardFilter}, {@link LowerCaseFilter}, {@link StopFilter}
114 * , {@link KeywordMarkerFilter} if a stem exclusion set is
115 * provided and {@link LatvianStemFilter}.
118 protected TokenStreamComponents createComponents(String fieldName,
120 final Tokenizer source = new StandardTokenizer(matchVersion, reader);
121 TokenStream result = new StandardFilter(matchVersion, source);
122 result = new LowerCaseFilter(matchVersion, result);
123 result = new StopFilter(matchVersion, result, stopwords);
124 if(!stemExclusionSet.isEmpty())
125 result = new KeywordMarkerFilter(result, stemExclusionSet);
126 result = new LatvianStemFilter(result);
127 return new TokenStreamComponents(source, result);