1 package org.apache.lucene.analysis.no;
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.WordlistLoader;
33 import org.apache.lucene.analysis.snowball.SnowballFilter;
34 import org.apache.lucene.analysis.standard.StandardFilter;
35 import org.apache.lucene.analysis.standard.StandardTokenizer;
36 import org.apache.lucene.util.Version;
37 import org.tartarus.snowball.ext.NorwegianStemmer;
40 * {@link Analyzer} for Norwegian.
42 public final class NorwegianAnalyzer extends StopwordAnalyzerBase {
43 private final Set<?> stemExclusionSet;
45 /** File containing default Norwegian stopwords. */
46 public final static String DEFAULT_STOPWORD_FILE = "norwegian_stop.txt";
49 * Returns an unmodifiable instance of the default stop words set.
50 * @return default stop words set.
52 public static Set<?> getDefaultStopSet(){
53 return DefaultSetHolder.DEFAULT_STOP_SET;
57 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
58 * accesses the static final set the first time.;
60 private static class DefaultSetHolder {
61 static final Set<?> DEFAULT_STOP_SET;
65 DEFAULT_STOP_SET = WordlistLoader.getSnowballWordSet(SnowballFilter.class,
66 DEFAULT_STOPWORD_FILE);
67 } catch (IOException ex) {
68 // default set should always be present as it is part of the
70 throw new RuntimeException("Unable to load default stopword set");
76 * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
78 public NorwegianAnalyzer(Version matchVersion) {
79 this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
83 * Builds an analyzer with the given stop words.
85 * @param matchVersion lucene compatibility version
86 * @param stopwords a stopword set
88 public NorwegianAnalyzer(Version matchVersion, Set<?> stopwords) {
89 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
93 * Builds an analyzer with the given stop words. If a non-empty stem exclusion set is
94 * provided this analyzer will add a {@link KeywordMarkerFilter} before
97 * @param matchVersion lucene compatibility version
98 * @param stopwords a stopword set
99 * @param stemExclusionSet a set of terms not to be stemmed
101 public NorwegianAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
102 super(matchVersion, stopwords);
103 this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
104 matchVersion, stemExclusionSet));
109 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
110 * which tokenizes all the text in the provided {@link Reader}.
113 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
114 * built from an {@link StandardTokenizer} filtered with
115 * {@link StandardFilter}, {@link LowerCaseFilter}, {@link StopFilter}
116 * , {@link KeywordMarkerFilter} if a stem exclusion set is
117 * provided and {@link SnowballFilter}.
120 protected TokenStreamComponents createComponents(String fieldName,
122 final Tokenizer source = new StandardTokenizer(matchVersion, reader);
123 TokenStream result = new StandardFilter(matchVersion, source);
124 result = new LowerCaseFilter(matchVersion, result);
125 result = new StopFilter(matchVersion, result, stopwords);
126 if(!stemExclusionSet.isEmpty())
127 result = new KeywordMarkerFilter(result, stemExclusionSet);
128 result = new SnowballFilter(result, new NorwegianStemmer());
129 return new TokenStreamComponents(source, result);