01: package org.apache.lucene.analysis;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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(Token result) throws IOException {
49: result = input.next(result);
50: if (result != null) {
51: if (stemmer.stem(result.termBuffer(), 0, result.termLength))
52: result.setTermBuffer(stemmer.getResultBuffer(), 0,
53: stemmer.getResultLength());
54: return result;
55: } else
56: return null;
57: }
58: }
|