pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / analysis / standard / ClassicFilter.java
1 package org.apache.lucene.analysis.standard;
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 org.apache.lucene.analysis.TokenFilter;
21 import org.apache.lucene.analysis.TokenStream;
22 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
23 import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
24
25 /** Normalizes tokens extracted with {@link ClassicTokenizer}. */
26
27 public class ClassicFilter extends TokenFilter {
28
29   /** Construct filtering <i>in</i>. */
30   public ClassicFilter(TokenStream in) {
31     super(in);
32   }
33
34   private static final String APOSTROPHE_TYPE = ClassicTokenizer.TOKEN_TYPES[ClassicTokenizer.APOSTROPHE];
35   private static final String ACRONYM_TYPE = ClassicTokenizer.TOKEN_TYPES[ClassicTokenizer.ACRONYM];
36
37   // this filters uses attribute type
38   private final TypeAttribute typeAtt = addAttribute(TypeAttribute.class);
39   private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
40   
41   /** Returns the next token in the stream, or null at EOS.
42    * <p>Removes <tt>'s</tt> from the end of words.
43    * <p>Removes dots from acronyms.
44    */
45   @Override
46   public final boolean incrementToken() throws java.io.IOException {
47     if (!input.incrementToken()) {
48       return false;
49     }
50
51     final char[] buffer = termAtt.buffer();
52     final int bufferLength = termAtt.length();
53     final String type = typeAtt.type();
54
55     if (type == APOSTROPHE_TYPE &&      // remove 's
56         bufferLength >= 2 &&
57         buffer[bufferLength-2] == '\'' &&
58         (buffer[bufferLength-1] == 's' || buffer[bufferLength-1] == 'S')) {
59       // Strip last 2 characters off
60       termAtt.setLength(bufferLength - 2);
61     } else if (type == ACRONYM_TYPE) {      // remove dots
62       int upto = 0;
63       for(int i=0;i<bufferLength;i++) {
64         char c = buffer[i];
65         if (c != '.')
66           buffer[upto++] = c;
67       }
68       termAtt.setLength(upto);
69     }
70
71     return true;
72   }
73 }