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