2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.solr.analysis;
20
import java.io.IOException;
22
import org.apache.lucene.analysis.KeywordMarkerFilter;
23
import org.apache.lucene.analysis.TokenFilter;
24
import org.apache.lucene.analysis.TokenStream;
25
import org.apache.lucene.analysis.CharArraySet;
26
import org.apache.lucene.analysis.snowball.SnowballFilter;
27
import org.apache.solr.common.ResourceLoader;
28
import org.apache.solr.util.plugin.ResourceLoaderAware;
29
import org.tartarus.snowball.SnowballProgram;
32
* Factory for {@link SnowballFilter}, with configurable language
34
* Note: Use of the "Lovins" stemmer is not recommended, as it is implemented with reflection.
35
* <pre class="prettyprint" >
36
* <fieldType name="text_snowballstem" class="solr.TextField" positionIncrementGap="100">
38
* <tokenizer class="solr.StandardTokenizerFactory"/>
39
* <filter class="solr.LowerCaseFilterFactory"/>
40
* <filter class="solr.SnowballPorterFilterFactory" protected="protectedkeyword.txt" language="English"/>
42
* </fieldType></pre>
44
* @version $Id: SnowballPorterFilterFactory.java 1074243 2011-02-24 18:07:16Z rmuir $
46
public class SnowballPorterFilterFactory extends BaseTokenFilterFactory implements ResourceLoaderAware {
47
public static final String PROTECTED_TOKENS = "protected";
49
private String language = "English";
50
private Class<?> stemClass;
53
public void inform(ResourceLoader loader) {
54
String wordFiles = args.get(PROTECTED_TOKENS);
55
if (wordFiles != null) {
57
protectedWords = getWordSet(loader, wordFiles, false);
58
} catch (IOException e) {
59
throw new RuntimeException(e);
64
private CharArraySet protectedWords = null;
67
public void init(Map<String, String> args) {
69
final String cfgLanguage = args.get("language");
70
if(cfgLanguage!=null) language = cfgLanguage;
73
stemClass = Class.forName("org.tartarus.snowball.ext." + language + "Stemmer");
74
} catch (ClassNotFoundException e) {
75
throw new RuntimeException("Can't find class for stemmer language " + language, e);
79
public TokenFilter create(TokenStream input) {
80
SnowballProgram program;
82
program = (SnowballProgram)stemClass.newInstance();
83
} catch (Exception e) {
84
throw new RuntimeException("Error instantiating stemmer for language " + language + "from class " +stemClass, e);
87
if (protectedWords != null)
88
input = new KeywordMarkerFilter(input, protectedWords);
89
return new SnowballFilter(input, program);