pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / analysis / PorterStemFilter.java
1 package org.apache.lucene.analysis;
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.tokenattributes.KeywordAttribute;
23 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
24
25 /** Transforms the token stream as per the Porter stemming algorithm.
26     Note: the input to the stemming filter must already be in lower case,
27     so you will need to use LowerCaseFilter or LowerCaseTokenizer farther
28     down the Tokenizer chain in order for this to work properly!
29     <P>
30     To use this filter with other analyzers, you'll want to write an
31     Analyzer class that sets up the TokenStream chain as you want it.
32     To use this with LowerCaseTokenizer, for example, you'd write an
33     analyzer like this:
34     <P>
35     <PRE>
36     class MyAnalyzer extends Analyzer {
37       public final TokenStream tokenStream(String fieldName, Reader reader) {
38         return new PorterStemFilter(new LowerCaseTokenizer(reader));
39       }
40     }
41     </PRE>
42     <p>
43     Note: This filter is aware of the {@link KeywordAttribute}. To prevent
44     certain terms from being passed to the stemmer
45     {@link KeywordAttribute#isKeyword()} should be set to <code>true</code>
46     in a previous {@link TokenStream}.
47     </p>
48 */
49 public final class PorterStemFilter extends TokenFilter {
50   private final PorterStemmer stemmer = new PorterStemmer();
51   private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
52   private final KeywordAttribute keywordAttr = addAttribute(KeywordAttribute.class);
53
54   public PorterStemFilter(TokenStream in) {
55     super(in);
56   }
57
58   @Override
59   public final boolean incrementToken() throws IOException {
60     if (!input.incrementToken())
61       return false;
62
63     if ((!keywordAttr.isKeyword()) && stemmer.stem(termAtt.buffer(), 0, termAtt.length()))
64       termAtt.copyBuffer(stemmer.getResultBuffer(), 0, stemmer.getResultLength());
65     return true;
66   }
67 }