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.IOUtils;
43
import org.apache.lucene.util.Version;
44
import org.tartarus.snowball.ext.German2Stemmer;
47
* {@link Analyzer} for German language.
49
* Supports an external list of stopwords (words that
50
* will not be indexed at all) and an external list of exclusions (word that will
51
* not be stemmed, but indexed).
52
* A default set of stopwords is used unless an alternative list is specified, but the
53
* exclusion list is empty by default.
57
* <p>You must specify the required {@link Version}
58
* compatibility when creating GermanAnalyzer:
60
* <li> As of 3.1, Snowball stemming is done with SnowballFilter, and
61
* Snowball stopwords are used by default.
62
* <li> As of 2.9, StopFilter preserves position
66
* <p><b>NOTE</b>: This class uses the same {@link Version}
67
* dependent settings as {@link StandardAnalyzer}.</p>
69
public final class GermanAnalyzer extends StopwordAnalyzerBase {
72
* List of typical german stopwords.
73
* @deprecated use {@link #getDefaultStopSet()} instead
76
public final static String[] GERMAN_STOP_WORDS = {
77
"einer", "eine", "eines", "einem", "einen",
78
"der", "die", "das", "dass", "daß",
79
"du", "er", "sie", "es",
80
"was", "wer", "wie", "wir",
81
"und", "oder", "ohne", "mit",
82
"am", "im", "in", "aus", "auf",
83
"ist", "sein", "war", "wird",
84
"ihr", "ihre", "ihres",
85
"als", "für", "von", "mit",
86
"dich", "dir", "mich", "mir",
87
"mein", "sein", "kein",
88
"durch", "wegen", "wird"
91
/** File containing default German stopwords. */
92
public final static String DEFAULT_STOPWORD_FILE = "german_stop.txt";
95
* Returns a set of default German-stopwords
96
* @return a set of default German-stopwords
98
public static final Set<?> getDefaultStopSet(){
99
return DefaultSetHolder.DEFAULT_SET;
102
private static class DefaultSetHolder {
103
/** @deprecated remove in Lucene 5.0 */
105
private static final Set<?> DEFAULT_SET_30 = CharArraySet.unmodifiableSet(new CharArraySet(
106
Version.LUCENE_CURRENT, Arrays.asList(GERMAN_STOP_WORDS), false));
107
private static final Set<?> DEFAULT_SET;
110
DEFAULT_SET = WordlistLoader.getSnowballWordSet(IOUtils.getDecodingReader(SnowballFilter.class,
111
DEFAULT_STOPWORD_FILE, IOUtils.CHARSET_UTF_8), Version.LUCENE_CURRENT);
112
} catch (IOException ex) {
113
// default set should always be present as it is part of the
114
// distribution (JAR)
115
throw new RuntimeException("Unable to load default stopword set");
121
* Contains the stopwords used with the {@link StopFilter}.
125
* Contains words that should be indexed but not stemmed.
127
// TODO make this final in 3.1
128
private Set<?> exclusionSet;
131
* Builds an analyzer with the default stop words:
132
* {@link #getDefaultStopSet()}.
134
public GermanAnalyzer(Version matchVersion) {
136
matchVersion.onOrAfter(Version.LUCENE_31) ? DefaultSetHolder.DEFAULT_SET
137
: DefaultSetHolder.DEFAULT_SET_30);
141
* Builds an analyzer with the given stop words
143
* @param matchVersion
144
* lucene compatibility version
148
public GermanAnalyzer(Version matchVersion, Set<?> stopwords) {
149
this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
153
* Builds an analyzer with the given stop words
155
* @param matchVersion
156
* lucene compatibility version
159
* @param stemExclusionSet
160
* a stemming exclusion set
162
public GermanAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
163
super(matchVersion, stopwords);
164
exclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(matchVersion, stemExclusionSet));
168
* Builds an analyzer with the given stop words.
169
* @deprecated use {@link #GermanAnalyzer(Version, Set)}
172
public GermanAnalyzer(Version matchVersion, String... stopwords) {
173
this(matchVersion, StopFilter.makeStopSet(matchVersion, stopwords));
177
* Builds an analyzer with the given stop words.
178
* @deprecated use {@link #GermanAnalyzer(Version, Set)}
181
public GermanAnalyzer(Version matchVersion, Map<?,?> stopwords) {
182
this(matchVersion, stopwords.keySet());
187
* Builds an analyzer with the given stop words.
188
* @deprecated use {@link #GermanAnalyzer(Version, Set)}
191
public GermanAnalyzer(Version matchVersion, File stopwords) throws IOException {
192
this(matchVersion, WordlistLoader.getWordSet(
193
IOUtils.getDecodingReader(stopwords, IOUtils.CHARSET_UTF_8), matchVersion));
197
* Builds an exclusionlist from an array of Strings.
198
* @deprecated use {@link #GermanAnalyzer(Version, Set, Set)} instead
201
public void setStemExclusionTable(String[] exclusionlist) {
202
exclusionSet = StopFilter.makeStopSet(matchVersion, exclusionlist);
203
setPreviousTokenStream(null); // force a new stemmer to be created
207
* Builds an exclusionlist from a {@link Map}
208
* @deprecated use {@link #GermanAnalyzer(Version, Set, Set)} instead
211
public void setStemExclusionTable(Map<?,?> exclusionlist) {
212
exclusionSet = new HashSet<Object>(exclusionlist.keySet());
213
setPreviousTokenStream(null); // force a new stemmer to be created
217
* Builds an exclusionlist from the words contained in the given file.
218
* @deprecated use {@link #GermanAnalyzer(Version, Set, Set)} instead
221
public void setStemExclusionTable(File exclusionlist) throws IOException {
222
exclusionSet = WordlistLoader.getWordSet(IOUtils.getDecodingReader(exclusionlist,
223
IOUtils.CHARSET_UTF_8), matchVersion);
224
setPreviousTokenStream(null); // force a new stemmer to be created
229
* {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
230
* used to tokenize all the text in the provided {@link Reader}.
232
* @return {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
233
* built from a {@link StandardTokenizer} filtered with
234
* {@link StandardFilter}, {@link LowerCaseFilter}, {@link StopFilter}
235
* , {@link KeywordMarkerFilter} if a stem exclusion set is
236
* provided, and {@link SnowballFilter}
239
protected TokenStreamComponents createComponents(String fieldName,
241
final Tokenizer source = new StandardTokenizer(matchVersion, reader);
242
TokenStream result = new StandardFilter(matchVersion, source);
243
result = new LowerCaseFilter(matchVersion, result);
244
result = new StopFilter( matchVersion, result, stopwords);
245
result = new KeywordMarkerFilter(result, exclusionSet);
246
if (matchVersion.onOrAfter(Version.LUCENE_31))
247
result = new SnowballFilter(result, new German2Stemmer());
249
result = new GermanStemFilter(result);
250
return new TokenStreamComponents(source, result);