2 * Copyright 2004 The Apache Software Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 * not use this file except in compliance with the License. You may obtain a
6 * copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
17 package org.apache.lucene.analysis.stempel;
19 import java.io.IOException;
21 import org.apache.lucene.analysis.TokenFilter;
22 import org.apache.lucene.analysis.TokenStream;
23 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
24 import org.apache.lucene.analysis.tokenattributes.KeywordAttribute;
27 * Transforms the token stream as per the stemming algorithm.
29 * Note: the input to the stemming filter must already be in lower case, so you
30 * will need to use LowerCaseFilter or LowerCaseTokenizer farther down the
31 * Tokenizer chain in order for this to work properly!
33 public final class StempelFilter extends TokenFilter {
34 private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
35 private final KeywordAttribute keywordAtt = addAttribute(KeywordAttribute.class);
36 private final StempelStemmer stemmer;
37 private final int minLength;
40 * Minimum length of input words to be processed. Shorter words are returned
43 public static final int DEFAULT_MIN_LENGTH = 3;
46 * Create filter using the supplied stemming table.
48 * @param in input token stream
49 * @param stemmer stemmer
51 public StempelFilter(TokenStream in, StempelStemmer stemmer) {
52 this(in, stemmer, DEFAULT_MIN_LENGTH);
56 * Create filter using the supplied stemming table.
58 * @param in input token stream
59 * @param stemmer stemmer
60 * @param minLength For performance reasons words shorter than minLength
61 * characters are not processed, but simply returned.
63 public StempelFilter(TokenStream in, StempelStemmer stemmer, int minLength) {
65 this.stemmer = stemmer;
66 this.minLength = minLength;
69 /** Returns the next input Token, after being stemmed */
71 public boolean incrementToken() throws IOException {
72 if (input.incrementToken()) {
73 if (!keywordAtt.isKeyword() && termAtt.length() > minLength) {
74 StringBuilder sb = stemmer.stem(termAtt);
75 if (sb != null) // if we can't stem it, return unchanged
76 termAtt.setEmpty().append(sb);