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

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/analysis/HyphenationCompoundWordTokenFilterFactory.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
 
 
18
 
package org.apache.solr.analysis;
19
 
 
20
 
import org.apache.commons.io.IOUtils;
21
 
import org.apache.lucene.analysis.TokenStream;
22
 
import org.apache.lucene.analysis.compound.CompoundWordTokenFilterBase;
23
 
import org.apache.lucene.analysis.compound.HyphenationCompoundWordTokenFilter;
24
 
import org.apache.lucene.analysis.compound.hyphenation.HyphenationTree;
25
 
import org.apache.lucene.analysis.CharArraySet;
26
 
import org.apache.solr.analysis.BaseTokenFilterFactory;
27
 
import org.apache.solr.common.ResourceLoader;
28
 
import org.apache.solr.common.SolrException;
29
 
import org.apache.solr.util.plugin.ResourceLoaderAware;
30
 
 
31
 
import java.util.Map;
32
 
import java.io.InputStream;
33
 
import org.xml.sax.InputSource;
34
 
 
35
 
/**
36
 
 * Factory for {@link HyphenationCompoundWordTokenFilter}.
37
 
 * <p>
38
 
 * This factory accepts the following parameters:
39
 
 * <ul>
40
 
 *  <li><code>hyphenator</code> (mandatory): path to the FOP xml hyphenation pattern. 
41
 
 *  See <a href="http://offo.sourceforge.net/hyphenation/">http://offo.sourceforge.net/hyphenation/</a>.
42
 
 *  <li><code>encoding</code> (optional): encoding of the xml hyphenation file. defaults to UTF-8.
43
 
 *  <li><code>dictionary</code> (optional): dictionary of words. defaults to no dictionary.
44
 
 *  <li><code>minWordSize</code> (optional): minimal word length that gets decomposed. defaults to 5.
45
 
 *  <li><code>minSubwordSize</code> (optional): minimum length of subwords. defaults to 2.
46
 
 *  <li><code>maxSubwordSize</code> (optional): maximum length of subwords. defaults to 15.
47
 
 *  <li><code>onlyLongestMatch</code> (optional): if true, adds only the longest matching subword 
48
 
 *    to the stream. defaults to false.
49
 
 * </ul>
50
 
 * <p>
51
 
 * <pre class="prettyprint" >
52
 
 * &lt;fieldType name="text_hyphncomp" class="solr.TextField" positionIncrementGap="100"&gt;
53
 
 *   &lt;analyzer&gt;
54
 
 *     &lt;tokenizer class="solr.WhitespaceTokenizerFactory"/&gt;
55
 
 *     &lt;filter class="solr.HyphenationCompoundWordTokenFilterFactory" hyphenator="hyphenator.xml" encoding="UTF-8"
56
 
 *           dictionary="dictionary.txt" minWordSize="5" minSubwordSize="2" maxSubwordSize="15" onlyLongestMatch="false"/&gt;
57
 
 *   &lt;/analyzer&gt;
58
 
 * &lt;/fieldType&gt;</pre>
59
 
 * @version $Id$
60
 
 * @see HyphenationCompoundWordTokenFilter
61
 
 */
62
 
public class HyphenationCompoundWordTokenFilterFactory extends BaseTokenFilterFactory implements ResourceLoaderAware {
63
 
  private CharArraySet dictionary;
64
 
  private HyphenationTree hyphenator;
65
 
  private String dictFile;
66
 
  private String hypFile;
67
 
  private String encoding;
68
 
  private int minWordSize;
69
 
  private int minSubwordSize;
70
 
  private int maxSubwordSize;
71
 
  private boolean onlyLongestMatch;
72
 
  
73
 
  @Override
74
 
  public void init(Map<String, String> args) {
75
 
    super.init(args);
76
 
    assureMatchVersion();
77
 
    dictFile = args.get("dictionary");
78
 
    if (args.containsKey("encoding"))
79
 
      encoding = args.get("encoding");
80
 
    hypFile = args.get("hyphenator");
81
 
    if (null == hypFile) {
82
 
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
83
 
          "Missing required parameter: hyphenator");
84
 
    }
85
 
 
86
 
    minWordSize = getInt("minWordSize", CompoundWordTokenFilterBase.DEFAULT_MIN_WORD_SIZE);
87
 
    minSubwordSize = getInt("minSubwordSize", CompoundWordTokenFilterBase.DEFAULT_MIN_SUBWORD_SIZE);
88
 
    maxSubwordSize = getInt("maxSubwordSize", CompoundWordTokenFilterBase.DEFAULT_MAX_SUBWORD_SIZE);
89
 
    onlyLongestMatch = getBoolean("onlyLongestMatch", false);
90
 
  }
91
 
  
92
 
  public void inform(ResourceLoader loader) {
93
 
    InputStream stream = null;
94
 
    try {
95
 
      if (dictFile != null) // the dictionary can be empty.
96
 
        dictionary = getWordSet(loader, dictFile, false);
97
 
      // TODO: Broken, because we cannot resolve real system id
98
 
      // ResourceLoader should also supply method like ClassLoader to get resource URL
99
 
      stream = loader.openResource(hypFile);
100
 
      final InputSource is = new InputSource(stream);
101
 
      is.setEncoding(encoding); // if it's null let xml parser decide
102
 
      is.setSystemId(hypFile);
103
 
      hyphenator = HyphenationCompoundWordTokenFilter.getHyphenationTree(is);
104
 
    } catch (Exception e) { // TODO: getHyphenationTree really shouldn't throw "Exception"
105
 
      throw new RuntimeException(e);
106
 
    } finally {
107
 
      IOUtils.closeQuietly(stream);
108
 
    }
109
 
  }
110
 
  
111
 
  public HyphenationCompoundWordTokenFilter create(TokenStream input) {
112
 
    return new HyphenationCompoundWordTokenFilter(luceneMatchVersion, input, hyphenator, dictionary, minWordSize, minSubwordSize, maxSubwordSize, onlyLongestMatch);
113
 
  }
114
 
}