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

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/analysis/DoubleMetaphoneFilter.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.io.IOException;
20
 
import java.util.LinkedList;
21
 
 
22
 
import org.apache.commons.codec.language.DoubleMetaphone;
23
 
import org.apache.lucene.analysis.TokenFilter;
24
 
import org.apache.lucene.analysis.TokenStream;
25
 
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
26
 
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
27
 
 
28
 
public final class DoubleMetaphoneFilter extends TokenFilter {
29
 
 
30
 
  private static final String TOKEN_TYPE = "DoubleMetaphone";
31
 
  
32
 
  private final LinkedList<State> remainingTokens = new LinkedList<State>();
33
 
  private final DoubleMetaphone encoder = new DoubleMetaphone();
34
 
  private final boolean inject;
35
 
  private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
36
 
  private final PositionIncrementAttribute posAtt = addAttribute(PositionIncrementAttribute.class);
37
 
 
38
 
  protected DoubleMetaphoneFilter(TokenStream input, int maxCodeLength, boolean inject) {
39
 
    super(input);
40
 
    this.encoder.setMaxCodeLen(maxCodeLength);
41
 
    this.inject = inject;
42
 
  }
43
 
 
44
 
  @Override
45
 
  public boolean incrementToken() throws IOException {
46
 
    for(;;) {
47
 
 
48
 
      if (!remainingTokens.isEmpty()) {
49
 
        // clearAttributes();  // not currently necessary
50
 
        restoreState(remainingTokens.removeFirst());
51
 
        return true;
52
 
      }
53
 
 
54
 
      if (!input.incrementToken()) return false;
55
 
 
56
 
      int len = termAtt.length();
57
 
      if (len==0) return true; // pass through zero length terms
58
 
      
59
 
      int firstAlternativeIncrement = inject ? 0 : posAtt.getPositionIncrement();
60
 
 
61
 
      String v = termAtt.toString();
62
 
      String primaryPhoneticValue = encoder.doubleMetaphone(v);
63
 
      String alternatePhoneticValue = encoder.doubleMetaphone(v, true);
64
 
 
65
 
      // a flag to lazily save state if needed... this avoids a save/restore when only
66
 
      // one token will be generated.
67
 
      boolean saveState=inject;
68
 
 
69
 
      if (primaryPhoneticValue!=null && primaryPhoneticValue.length() > 0 && !primaryPhoneticValue.equals(v)) {
70
 
        if (saveState) {
71
 
          remainingTokens.addLast(captureState());
72
 
        }
73
 
        posAtt.setPositionIncrement( firstAlternativeIncrement );
74
 
        firstAlternativeIncrement = 0;
75
 
        termAtt.setEmpty().append(primaryPhoneticValue);
76
 
        saveState = true;
77
 
      }
78
 
 
79
 
      if (alternatePhoneticValue!=null && alternatePhoneticValue.length() > 0
80
 
              && !alternatePhoneticValue.equals(primaryPhoneticValue)
81
 
              && !primaryPhoneticValue.equals(v)) {
82
 
        if (saveState) {
83
 
          remainingTokens.addLast(captureState());
84
 
          saveState = false;
85
 
        }
86
 
        posAtt.setPositionIncrement( firstAlternativeIncrement );
87
 
        termAtt.setEmpty().append(alternatePhoneticValue);
88
 
        saveState = true;
89
 
      }
90
 
 
91
 
      // Just one token to return, so no need to capture/restore
92
 
      // any state, simply return it.
93
 
      if (remainingTokens.isEmpty()) {
94
 
        return true;
95
 
      }
96
 
 
97
 
      if (saveState) {
98
 
        remainingTokens.addLast(captureState());
99
 
      }
100
 
    }
101
 
  }
102
 
 
103
 
  @Override
104
 
  public void reset() throws IOException {
105
 
    input.reset();
106
 
    remainingTokens.clear();
107
 
  }
108
 
}