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

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/analysis/HunspellStemFilterFactory.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
 
package org.apache.solr.analysis;
2
 
 
3
 
/**
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
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
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.
18
 
 */
19
 
 
20
 
import java.io.InputStream;
21
 
import java.util.ArrayList;
22
 
import java.util.List;
23
 
 
24
 
import org.apache.lucene.analysis.TokenStream;
25
 
import org.apache.lucene.analysis.hunspell.HunspellDictionary;
26
 
import org.apache.lucene.analysis.hunspell.HunspellStemFilter;
27
 
import org.apache.solr.common.ResourceLoader;
28
 
import org.apache.solr.common.SolrException;
29
 
import org.apache.solr.common.SolrException.ErrorCode;
30
 
import org.apache.solr.util.plugin.ResourceLoaderAware;
31
 
 
32
 
/**
33
 
 * TokenFilterFactory that creates instances of {@link org.apache.lucene.analysis.hunspell.HunspellStemFilter}.
34
 
 * Example config for British English including a custom dictionary, case insensitive matching:
35
 
 * <pre class="prettyprint" >
36
 
 * &lt;filter class=&quot;solr.HunspellStemFilterFactory&quot;
37
 
 *    dictionary=&quot;en_GB.dic,my_custom.dic&quot;
38
 
 *    affix=&quot;en_GB.aff&quot;
39
 
 *    ignoreCase=&quot;true&quot; /&gt;</pre>
40
 
 * Both parameters dictionary and affix are mandatory.
41
 
 * <br/>
42
 
 * The parameter ignoreCase (true/false) controls whether matching is case sensitive or not. Default false.
43
 
 * <br/> 
44
 
 * Dictionaries for many languages are available through the OpenOffice project.
45
 
 * 
46
 
 * See <a href="http://wiki.apache.org/solr/Hunspell">http://wiki.apache.org/solr/Hunspell</a>
47
 
 */
48
 
public class HunspellStemFilterFactory extends BaseTokenFilterFactory implements ResourceLoaderAware {
49
 
  
50
 
  private static final String PARAM_DICTIONARY = "dictionary";
51
 
  private static final String PARAM_AFFIX = "affix";
52
 
  private static final String PARAM_IGNORE_CASE = "ignoreCase";
53
 
  private static final String TRUE = "true";
54
 
  private static final String FALSE = "false";
55
 
  
56
 
  private HunspellDictionary dictionary;
57
 
  private boolean ignoreCase = false;
58
 
 
59
 
  /**
60
 
   * Loads the hunspell dictionary and affix files defined in the configuration
61
 
   *  
62
 
   * @param loader ResourceLoader used to load the files
63
 
   */
64
 
  public void inform(ResourceLoader loader) {
65
 
    assureMatchVersion();
66
 
    String dictionaryFiles[] = args.get(PARAM_DICTIONARY).split(",");
67
 
    String affixFile = args.get(PARAM_AFFIX);
68
 
    String pic = args.get(PARAM_IGNORE_CASE);
69
 
    if(pic != null) {
70
 
      if(pic.equalsIgnoreCase(TRUE)) ignoreCase = true;
71
 
      else if(pic.equalsIgnoreCase(FALSE)) ignoreCase = false;
72
 
      else throw new SolrException(ErrorCode.UNKNOWN, "Unknown value for "+PARAM_IGNORE_CASE+": "+pic+". Must be true or false");
73
 
    }
74
 
 
75
 
    try {
76
 
      List<InputStream> dictionaries = new ArrayList<InputStream>();
77
 
      for (String file : dictionaryFiles) {
78
 
        dictionaries.add(loader.openResource(file));
79
 
      }
80
 
      this.dictionary = new HunspellDictionary(loader.openResource(affixFile), dictionaries, luceneMatchVersion, ignoreCase);
81
 
    } catch (Exception e) {
82
 
      throw new RuntimeException("Unable to load hunspell data! [dictionary=" + args.get("dictionary") + ",affix=" + affixFile + "]", e);
83
 
    }
84
 
  }
85
 
 
86
 
  /**
87
 
   * Creates an instance of {@link org.apache.lucene.analysis.hunspell.HunspellStemFilter} that will filter the given
88
 
   * TokenStream
89
 
   *
90
 
   * @param tokenStream TokenStream that will be filtered
91
 
   * @return HunspellStemFilter that filters the TokenStream 
92
 
   */
93
 
  public TokenStream create(TokenStream tokenStream) {
94
 
    return new HunspellStemFilter(tokenStream, dictionary);
95
 
  }
96
 
}