1 package org.apache.lucene.analysis.ngram;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
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;
28 * Tokenizes the input into n-grams of the given size(s).
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;
34 private int minGram, maxGram;
36 private char[] curTermBuffer;
37 private int curTermLength;
38 private int curGramSize;
42 private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
43 private final OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
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
51 public NGramTokenFilter(TokenStream input, int minGram, int maxGram) {
54 throw new IllegalArgumentException("minGram must be greater than zero");
56 if (minGram > maxGram) {
57 throw new IllegalArgumentException("minGram must not be greater than maxGram");
59 this.minGram = minGram;
60 this.maxGram = maxGram;
64 * Creates NGramTokenFilter with default min and max n-grams.
65 * @param input {@link TokenStream} holding the input to be tokenized
67 public NGramTokenFilter(TokenStream input) {
68 this(input, DEFAULT_MIN_NGRAM_SIZE, DEFAULT_MAX_NGRAM_SIZE);
71 /** Returns the next token in the stream, or null at EOS. */
73 public final boolean incrementToken() throws IOException {
75 if (curTermBuffer == null) {
76 if (!input.incrementToken()) {
79 curTermBuffer = termAtt.buffer().clone();
80 curTermLength = termAtt.length();
81 curGramSize = minGram;
83 tokStart = offsetAtt.startOffset();
86 while (curGramSize <= maxGram) {
87 while (curPos+curGramSize <= curTermLength) { // while there is input
89 termAtt.copyBuffer(curTermBuffer, curPos, curGramSize);
90 offsetAtt.setOffset(tokStart + curPos, tokStart + curPos + curGramSize);
94 curGramSize++; // increase n-gram size
102 public void reset() throws IOException {
104 curTermBuffer = null;