1 package org.apache.lucene.analysis.de;
2 // This file is encoded in UTF-8
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements. See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 import java.io.IOException;
23 import java.io.Reader;
24 import java.util.Arrays;
25 import java.util.HashSet;
29 import org.apache.lucene.analysis.Analyzer;
30 import org.apache.lucene.analysis.CharArraySet;
31 import org.apache.lucene.analysis.LowerCaseFilter;
32 import org.apache.lucene.analysis.KeywordMarkerFilter;
33 import org.apache.lucene.analysis.StopFilter;
34 import org.apache.lucene.analysis.StopwordAnalyzerBase;
35 import org.apache.lucene.analysis.TokenStream;
36 import org.apache.lucene.analysis.Tokenizer;
37 import org.apache.lucene.analysis.WordlistLoader;
38 import org.apache.lucene.analysis.snowball.SnowballFilter;
39 import org.apache.lucene.analysis.standard.StandardAnalyzer;
40 import org.apache.lucene.analysis.standard.StandardFilter;
41 import org.apache.lucene.analysis.standard.StandardTokenizer;
42 import org.apache.lucene.util.Version;
43 import org.tartarus.snowball.ext.German2Stemmer;
46 * {@link Analyzer} for German language.
48 * Supports an external list of stopwords (words that
49 * will not be indexed at all) and an external list of exclusions (word that will
50 * not be stemmed, but indexed).
51 * A default set of stopwords is used unless an alternative list is specified, but the
52 * exclusion list is empty by default.
56 * <p>You must specify the required {@link Version}
57 * compatibility when creating GermanAnalyzer:
59 * <li> As of 3.1, Snowball stemming is done with SnowballFilter, and
60 * Snowball stopwords are used by default.
61 * <li> As of 2.9, StopFilter preserves position
65 * <p><b>NOTE</b>: This class uses the same {@link Version}
66 * dependent settings as {@link StandardAnalyzer}.</p>
68 public final class GermanAnalyzer extends StopwordAnalyzerBase {
71 * List of typical german stopwords.
72 * @deprecated use {@link #getDefaultStopSet()} instead
75 public final static String[] GERMAN_STOP_WORDS = {
76 "einer", "eine", "eines", "einem", "einen",
77 "der", "die", "das", "dass", "daß",
78 "du", "er", "sie", "es",
79 "was", "wer", "wie", "wir",
80 "und", "oder", "ohne", "mit",
81 "am", "im", "in", "aus", "auf",
82 "ist", "sein", "war", "wird",
83 "ihr", "ihre", "ihres",
84 "als", "für", "von", "mit",
85 "dich", "dir", "mich", "mir",
86 "mein", "sein", "kein",
87 "durch", "wegen", "wird"
90 /** File containing default German stopwords. */
91 public final static String DEFAULT_STOPWORD_FILE = "german_stop.txt";
94 * Returns a set of default German-stopwords
95 * @return a set of default German-stopwords
97 public static final Set<?> getDefaultStopSet(){
98 return DefaultSetHolder.DEFAULT_SET;
101 private static class DefaultSetHolder {
102 /** @deprecated remove in Lucene 5.0 */
104 private static final Set<?> DEFAULT_SET_30 = CharArraySet.unmodifiableSet(new CharArraySet(
105 Version.LUCENE_CURRENT, Arrays.asList(GERMAN_STOP_WORDS), false));
106 private static final Set<?> DEFAULT_SET;
110 WordlistLoader.getSnowballWordSet(SnowballFilter.class, DEFAULT_STOPWORD_FILE);
111 } catch (IOException ex) {
112 // default set should always be present as it is part of the
113 // distribution (JAR)
114 throw new RuntimeException("Unable to load default stopword set");
120 * Contains the stopwords used with the {@link StopFilter}.
124 * Contains words that should be indexed but not stemmed.
126 // TODO make this final in 3.1
127 private Set<?> exclusionSet;
130 * Builds an analyzer with the default stop words:
131 * {@link #getDefaultStopSet()}.
133 public GermanAnalyzer(Version matchVersion) {
135 matchVersion.onOrAfter(Version.LUCENE_31) ? DefaultSetHolder.DEFAULT_SET
136 : DefaultSetHolder.DEFAULT_SET_30);
140 * Builds an analyzer with the given stop words
142 * @param matchVersion
143 * lucene compatibility version
147 public GermanAnalyzer(Version matchVersion, Set<?> stopwords) {
148 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
152 * Builds an analyzer with the given stop words
154 * @param matchVersion
155 * lucene compatibility version
158 * @param stemExclusionSet
159 * a stemming exclusion set
161 public GermanAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
162 super(matchVersion, stopwords);
163 exclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(matchVersion, stemExclusionSet));
167 * Builds an analyzer with the given stop words.
168 * @deprecated use {@link #GermanAnalyzer(Version, Set)}
171 public GermanAnalyzer(Version matchVersion, String... stopwords) {
172 this(matchVersion, StopFilter.makeStopSet(matchVersion, stopwords));
176 * Builds an analyzer with the given stop words.
177 * @deprecated use {@link #GermanAnalyzer(Version, Set)}
180 public GermanAnalyzer(Version matchVersion, Map<?,?> stopwords) {
181 this(matchVersion, stopwords.keySet());
186 * Builds an analyzer with the given stop words.
187 * @deprecated use {@link #GermanAnalyzer(Version, Set)}
190 public GermanAnalyzer(Version matchVersion, File stopwords) throws IOException {
191 this(matchVersion, WordlistLoader.getWordSet(stopwords));
195 * Builds an exclusionlist from an array of Strings.
196 * @deprecated use {@link #GermanAnalyzer(Version, Set, Set)} instead
199 public void setStemExclusionTable(String[] exclusionlist) {
200 exclusionSet = StopFilter.makeStopSet(matchVersion, exclusionlist);
201 setPreviousTokenStream(null); // force a new stemmer to be created
205 * Builds an exclusionlist from a {@link Map}
206 * @deprecated use {@link #GermanAnalyzer(Version, Set, Set)} instead
209 public void setStemExclusionTable(Map<?,?> exclusionlist) {
210 exclusionSet = new HashSet<Object>(exclusionlist.keySet());
211 setPreviousTokenStream(null); // force a new stemmer to be created
215 * Builds an exclusionlist from the words contained in the given file.
216 * @deprecated use {@link #GermanAnalyzer(Version, Set, Set)} instead
219 public void setStemExclusionTable(File exclusionlist) throws IOException {
220 exclusionSet = WordlistLoader.getWordSet(exclusionlist);
221 setPreviousTokenStream(null); // force a new stemmer to be created
226 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
227 * used to tokenize all the text in the provided {@link Reader}.
229 * @return {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
230 * built from a {@link StandardTokenizer} filtered with
231 * {@link StandardFilter}, {@link LowerCaseFilter}, {@link StopFilter}
232 * , {@link KeywordMarkerFilter} if a stem exclusion set is
233 * provided, and {@link SnowballFilter}
236 protected TokenStreamComponents createComponents(String fieldName,
238 final Tokenizer source = new StandardTokenizer(matchVersion, reader);
239 TokenStream result = new StandardFilter(matchVersion, source);
240 result = new LowerCaseFilter(matchVersion, result);
241 result = new StopFilter( matchVersion, result, stopwords);
242 result = new KeywordMarkerFilter(result, exclusionSet);
243 if (matchVersion.onOrAfter(Version.LUCENE_31))
244 result = new SnowballFilter(result, new German2Stemmer());
246 result = new GermanStemFilter(result);
247 return new TokenStreamComponents(source, result);