~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/analysis/SnowballPorterFilterFactory.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
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
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
package org.apache.solr.analysis;
18
 
 
19
 
import java.util.Map;
20
 
import java.io.IOException;
21
 
 
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;
30
 
 
31
 
/**
32
 
 * Factory for {@link SnowballFilter}, with configurable language
33
 
 * <p>
34
 
 * Note: Use of the "Lovins" stemmer is not recommended, as it is implemented with reflection.
35
 
 * <pre class="prettyprint" >
36
 
 * &lt;fieldType name="text_snowballstem" class="solr.TextField" positionIncrementGap="100"&gt;
37
 
 *   &lt;analyzer&gt;
38
 
 *     &lt;tokenizer class="solr.StandardTokenizerFactory"/&gt;
39
 
 *     &lt;filter class="solr.LowerCaseFilterFactory"/&gt;
40
 
 *     &lt;filter class="solr.SnowballPorterFilterFactory" protected="protectedkeyword.txt" language="English"/&gt;
41
 
 *   &lt;/analyzer&gt;
42
 
 * &lt;/fieldType&gt;</pre>
43
 
 * 
44
 
 * @version $Id: SnowballPorterFilterFactory.java 1074243 2011-02-24 18:07:16Z rmuir $
45
 
 */
46
 
public class SnowballPorterFilterFactory extends BaseTokenFilterFactory implements ResourceLoaderAware {
47
 
  public static final String PROTECTED_TOKENS = "protected";
48
 
 
49
 
  private String language = "English";
50
 
  private Class<?> stemClass;
51
 
 
52
 
 
53
 
  public void inform(ResourceLoader loader) {
54
 
    String wordFiles = args.get(PROTECTED_TOKENS);
55
 
    if (wordFiles != null) {
56
 
      try {
57
 
        protectedWords = getWordSet(loader, wordFiles, false);
58
 
      } catch (IOException e) {
59
 
        throw new RuntimeException(e);
60
 
      }
61
 
    }
62
 
  }
63
 
 
64
 
  private CharArraySet protectedWords = null;
65
 
 
66
 
  @Override
67
 
  public void init(Map<String, String> args) {
68
 
    super.init(args);
69
 
    final String cfgLanguage = args.get("language");
70
 
    if(cfgLanguage!=null) language = cfgLanguage;
71
 
 
72
 
    try {
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);
76
 
    }
77
 
  }
78
 
  
79
 
  public TokenFilter create(TokenStream input) {
80
 
    SnowballProgram program;
81
 
    try {
82
 
      program = (SnowballProgram)stemClass.newInstance();
83
 
    } catch (Exception e) {
84
 
      throw new RuntimeException("Error instantiating stemmer for language " + language + "from class " +stemClass, e);
85
 
    }
86
 
 
87
 
    if (protectedWords != null)
88
 
      input = new KeywordMarkerFilter(input, protectedWords);
89
 
    return new SnowballFilter(input, program);
90
 
  }
91
 
}
92