~ubuntu-branches/ubuntu/trusty/pylucene/trusty

« back to all changes in this revision

Viewing changes to lucene-java-3.5.0/lucene/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ngram/EdgeNGramTokenFilter.java

  • Committer: Package Import Robot
  • Author(s): Dmitry Nezhevenko
  • Date: 2012-04-23 16:43:55 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120423164355-grqtepnwtecdjfk2
Tags: 3.5.0-1
* New maintainer (closes: 670179)
* New upstream release
* Switch to dpkg-source 3.0 (quilt) format
* Switch to machine-readable debian/copyright
* Bump debian/compat to 8, drop debian/pycompat
* Switch from cdbs to dh
* Add watch file
* Build for all supported versions of python2 (closes: 581198, 632240)
* Rename binary package to python-lucene (closes: 581197)
* Add -dbg package

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.apache.lucene.analysis.ngram;
 
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 org.apache.lucene.analysis.TokenFilter;
 
21
import org.apache.lucene.analysis.TokenStream;
 
22
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
 
23
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
 
24
 
 
25
import java.io.IOException;
 
26
 
 
27
/**
 
28
 * Tokenizes the given token into n-grams of given size(s).
 
29
 * <p>
 
30
 * This {@link TokenFilter} create n-grams from the beginning edge or ending edge of a input token.
 
31
 * </p>
 
32
 */
 
33
public final class EdgeNGramTokenFilter extends TokenFilter {
 
34
  public static final Side DEFAULT_SIDE = Side.FRONT;
 
35
  public static final int DEFAULT_MAX_GRAM_SIZE = 1;
 
36
  public static final int DEFAULT_MIN_GRAM_SIZE = 1;
 
37
 
 
38
  /** Specifies which side of the input the n-gram should be generated from */
 
39
  public static enum Side {
 
40
 
 
41
    /** Get the n-gram from the front of the input */
 
42
    FRONT {
 
43
      @Override
 
44
      public String getLabel() { return "front"; }
 
45
    },
 
46
 
 
47
    /** Get the n-gram from the end of the input */
 
48
    BACK  {
 
49
      @Override
 
50
      public String getLabel() { return "back"; }
 
51
    };
 
52
 
 
53
    public abstract String getLabel();
 
54
 
 
55
    // Get the appropriate Side from a string
 
56
    public static Side getSide(String sideName) {
 
57
      if (FRONT.getLabel().equals(sideName)) {
 
58
        return FRONT;
 
59
      }
 
60
      if (BACK.getLabel().equals(sideName)) {
 
61
        return BACK;
 
62
      }
 
63
      return null;
 
64
    }
 
65
  }
 
66
 
 
67
  private final int minGram;
 
68
  private final int maxGram;
 
69
  private Side side;
 
70
  private char[] curTermBuffer;
 
71
  private int curTermLength;
 
72
  private int curGramSize;
 
73
  private int tokStart;
 
74
  
 
75
  private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
 
76
  private final OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
 
77
 
 
78
  /**
 
79
   * Creates EdgeNGramTokenFilter that can generate n-grams in the sizes of the given range
 
80
   *
 
81
   * @param input {@link TokenStream} holding the input to be tokenized
 
82
   * @param side the {@link Side} from which to chop off an n-gram
 
83
   * @param minGram the smallest n-gram to generate
 
84
   * @param maxGram the largest n-gram to generate
 
85
   */
 
86
  public EdgeNGramTokenFilter(TokenStream input, Side side, int minGram, int maxGram) {
 
87
    super(input);
 
88
 
 
89
    if (side == null) {
 
90
      throw new IllegalArgumentException("sideLabel must be either front or back");
 
91
    }
 
92
 
 
93
    if (minGram < 1) {
 
94
      throw new IllegalArgumentException("minGram must be greater than zero");
 
95
    }
 
96
 
 
97
    if (minGram > maxGram) {
 
98
      throw new IllegalArgumentException("minGram must not be greater than maxGram");
 
99
    }
 
100
 
 
101
    this.minGram = minGram;
 
102
    this.maxGram = maxGram;
 
103
    this.side = side;
 
104
  }
 
105
 
 
106
  /**
 
107
   * Creates EdgeNGramTokenFilter that can generate n-grams in the sizes of the given range
 
108
   *
 
109
   * @param input {@link TokenStream} holding the input to be tokenized
 
110
   * @param sideLabel the name of the {@link Side} from which to chop off an n-gram
 
111
   * @param minGram the smallest n-gram to generate
 
112
   * @param maxGram the largest n-gram to generate
 
113
   */
 
114
  public EdgeNGramTokenFilter(TokenStream input, String sideLabel, int minGram, int maxGram) {
 
115
    this(input, Side.getSide(sideLabel), minGram, maxGram);
 
116
  }
 
117
 
 
118
  @Override
 
119
  public final boolean incrementToken() throws IOException {
 
120
    while (true) {
 
121
      if (curTermBuffer == null) {
 
122
        if (!input.incrementToken()) {
 
123
          return false;
 
124
        } else {
 
125
          curTermBuffer = termAtt.buffer().clone();
 
126
          curTermLength = termAtt.length();
 
127
          curGramSize = minGram;
 
128
          tokStart = offsetAtt.startOffset();
 
129
        }
 
130
      }
 
131
      if (curGramSize <= maxGram) {
 
132
        if (! (curGramSize > curTermLength         // if the remaining input is too short, we can't generate any n-grams
 
133
            || curGramSize > maxGram)) {       // if we have hit the end of our n-gram size range, quit
 
134
          // grab gramSize chars from front or back
 
135
          int start = side == Side.FRONT ? 0 : curTermLength - curGramSize;
 
136
          int end = start + curGramSize;
 
137
          clearAttributes();
 
138
          offsetAtt.setOffset(tokStart + start, tokStart + end);
 
139
          termAtt.copyBuffer(curTermBuffer, start, curGramSize);
 
140
          curGramSize++;
 
141
          return true;
 
142
        }
 
143
      }
 
144
      curTermBuffer = null;
 
145
    }
 
146
  }
 
147
 
 
148
  @Override
 
149
  public void reset() throws IOException {
 
150
    super.reset();
 
151
    curTermBuffer = null;
 
152
  }
 
153
}