Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » analysis » [javadoc | source]
    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.TermAttribute;
   23   
   24   /** Transforms the token stream as per the Porter stemming algorithm.
   25       Note: the input to the stemming filter must already be in lower case,
   26       so you will need to use LowerCaseFilter or LowerCaseTokenizer farther
   27       down the Tokenizer chain in order for this to work properly!
   28       <P>
   29       To use this filter with other analyzers, you'll want to write an
   30       Analyzer class that sets up the TokenStream chain as you want it.
   31       To use this with LowerCaseTokenizer, for example, you'd write an
   32       analyzer like this:
   33       <P>
   34       <PRE>
   35       class MyAnalyzer extends Analyzer {
   36         public final TokenStream tokenStream(String fieldName, Reader reader) {
   37           return new PorterStemFilter(new LowerCaseTokenizer(reader));
   38         }
   39       }
   40       </PRE>
   41   */
   42   public final class PorterStemFilter extends TokenFilter {
   43     private PorterStemmer stemmer;
   44     private TermAttribute termAtt;
   45   
   46     public PorterStemFilter(TokenStream in) {
   47       super(in);
   48       stemmer = new PorterStemmer();
   49       termAtt = addAttribute(TermAttribute.class);
   50     }
   51   
   52     @Override
   53     public final boolean incrementToken() throws IOException {
   54       if (!input.incrementToken())
   55         return false;
   56   
   57       if (stemmer.stem(termAtt.termBuffer(), 0, termAtt.termLength()))
   58         termAtt.setTermBuffer(stemmer.getResultBuffer(), 0, stemmer.getResultLength());
   59       return true;
   60     }
   61   }

Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » analysis » [javadoc | source]