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

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/analysis/ShingleFilterFactory.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
 
/**
3
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
4
 
 * contributor license agreements.  See the NOTICE file distributed with
5
 
 * this work for additional information regarding copyright ownership.
6
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
7
 
 * (the "License"); you may not use this file except in compliance with
8
 
 * the License.  You may obtain a copy of the License at
9
 
 *
10
 
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 
 *
12
 
 * Unless required by applicable law or agreed to in writing, software
13
 
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 
 * See the License for the specific language governing permissions and
16
 
 * limitations under the License.
17
 
 */
18
 
 
19
 
 
20
 
package org.apache.solr.analysis;
21
 
 
22
 
import org.apache.lucene.analysis.shingle.ShingleFilter;
23
 
import org.apache.lucene.analysis.TokenStream;
24
 
import org.apache.solr.common.SolrException;
25
 
import org.apache.solr.common.SolrException.ErrorCode;
26
 
 
27
 
import java.util.Map;
28
 
 
29
 
/** 
30
 
 * Factory for {@link ShingleFilter}.
31
 
 * <pre class="prettyprint" >
32
 
 * &lt;fieldType name="text_shingle" class="solr.TextField" positionIncrementGap="100"&gt;
33
 
 *   &lt;analyzer&gt;
34
 
 *     &lt;tokenizer class="solr.WhitespaceTokenizerFactory"/&gt;
35
 
 *     &lt;filter class="solr.ShingleFilterFactory" minShingleSize="2" maxShingleSize="2"
36
 
 *             outputUnigrams="true" outputUnigramsIfNoShingles="false" tokenSeparator=" "/&gt;
37
 
 *   &lt;/analyzer&gt;
38
 
 * &lt;/fieldType&gt;</pre>
39
 
 * @version $Id: ShingleFilterFactory.java 1074011 2011-02-24 01:49:49Z koji $
40
 
 */
41
 
public class ShingleFilterFactory extends BaseTokenFilterFactory {
42
 
  private int minShingleSize;
43
 
  private int maxShingleSize;
44
 
  private boolean outputUnigrams;
45
 
  private boolean outputUnigramsIfNoShingles;
46
 
  private String tokenSeparator;
47
 
 
48
 
  @Override
49
 
  public void init(Map<String, String> args) {
50
 
    super.init(args);
51
 
    maxShingleSize = getInt("maxShingleSize", 
52
 
                            ShingleFilter.DEFAULT_MAX_SHINGLE_SIZE);
53
 
    if (maxShingleSize < 2) {
54
 
      throw new SolrException(ErrorCode.SERVER_ERROR,
55
 
                              "Invalid maxShingleSize (" + maxShingleSize
56
 
                              + ") - must be at least 2");
57
 
    }
58
 
    minShingleSize = getInt("minShingleSize",
59
 
                            ShingleFilter.DEFAULT_MIN_SHINGLE_SIZE);
60
 
    if (minShingleSize < 2) {
61
 
      throw new SolrException(ErrorCode.SERVER_ERROR,
62
 
                              "Invalid minShingleSize (" + minShingleSize
63
 
                              + ") - must be at least 2");
64
 
    }
65
 
    if (minShingleSize > maxShingleSize) {
66
 
      throw new SolrException(ErrorCode.SERVER_ERROR,
67
 
                              "Invalid minShingleSize (" + minShingleSize
68
 
                              + ") - must be no greater than maxShingleSize ("
69
 
                              + maxShingleSize + ")");
70
 
    }
71
 
    outputUnigrams = getBoolean("outputUnigrams", true);
72
 
    outputUnigramsIfNoShingles = getBoolean("outputUnigramsIfNoShingles", false);
73
 
    tokenSeparator = args.containsKey("tokenSeparator")
74
 
                     ? args.get("tokenSeparator")
75
 
                     : ShingleFilter.TOKEN_SEPARATOR;
76
 
  }
77
 
  public ShingleFilter create(TokenStream input) {
78
 
    ShingleFilter r = new ShingleFilter(input, minShingleSize, maxShingleSize);
79
 
    r.setOutputUnigrams(outputUnigrams);
80
 
    r.setOutputUnigramsIfNoShingles(outputUnigramsIfNoShingles);
81
 
    r.setTokenSeparator(tokenSeparator);
82
 
    return r;
83
 
  }
84
 
}
85