pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / ngram / NGramTokenFilter.java
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 java.io.IOException;
21
22 import org.apache.lucene.analysis.TokenFilter;
23 import org.apache.lucene.analysis.TokenStream;
24 import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
25 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
26
27 /**
28  * Tokenizes the input into n-grams of the given size(s).
29  */
30 public final class NGramTokenFilter extends TokenFilter {
31   public static final int DEFAULT_MIN_NGRAM_SIZE = 1;
32   public static final int DEFAULT_MAX_NGRAM_SIZE = 2;
33
34   private int minGram, maxGram;
35   
36   private char[] curTermBuffer;
37   private int curTermLength;
38   private int curGramSize;
39   private int curPos;
40   private int tokStart;
41   
42   private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
43   private final OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
44
45   /**
46    * Creates NGramTokenFilter with given min and max n-grams.
47    * @param input {@link TokenStream} holding the input to be tokenized
48    * @param minGram the smallest n-gram to generate
49    * @param maxGram the largest n-gram to generate
50    */
51   public NGramTokenFilter(TokenStream input, int minGram, int maxGram) {
52     super(input);
53     if (minGram < 1) {
54       throw new IllegalArgumentException("minGram must be greater than zero");
55     }
56     if (minGram > maxGram) {
57       throw new IllegalArgumentException("minGram must not be greater than maxGram");
58     }
59     this.minGram = minGram;
60     this.maxGram = maxGram;
61   }
62
63   /**
64    * Creates NGramTokenFilter with default min and max n-grams.
65    * @param input {@link TokenStream} holding the input to be tokenized
66    */
67   public NGramTokenFilter(TokenStream input) {
68     this(input, DEFAULT_MIN_NGRAM_SIZE, DEFAULT_MAX_NGRAM_SIZE);
69   }
70
71   /** Returns the next token in the stream, or null at EOS. */
72   @Override
73   public final boolean incrementToken() throws IOException {
74     while (true) {
75       if (curTermBuffer == null) {
76         if (!input.incrementToken()) {
77           return false;
78         } else {
79           curTermBuffer = termAtt.buffer().clone();
80           curTermLength = termAtt.length();
81           curGramSize = minGram;
82           curPos = 0;
83           tokStart = offsetAtt.startOffset();
84         }
85       }
86       while (curGramSize <= maxGram) {
87         while (curPos+curGramSize <= curTermLength) {     // while there is input
88           clearAttributes();
89           termAtt.copyBuffer(curTermBuffer, curPos, curGramSize);
90           offsetAtt.setOffset(tokStart + curPos, tokStart + curPos + curGramSize);
91           curPos++;
92           return true;
93         }
94         curGramSize++;                         // increase n-gram size
95         curPos = 0;
96       }
97       curTermBuffer = null;
98     }
99   }
100
101   @Override
102   public void reset() throws IOException {
103     super.reset();
104     curTermBuffer = null;
105   }
106 }