1 package org.apache.lucene.analysis.standard;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
25 /** Normalizes tokens extracted with {@link ClassicTokenizer}. */
27 public class ClassicFilter extends TokenFilter {
29 /** Construct filtering <i>in</i>. */
30 public ClassicFilter(TokenStream in) {
34 private static final String APOSTROPHE_TYPE = ClassicTokenizer.TOKEN_TYPES[ClassicTokenizer.APOSTROPHE];
35 private static final String ACRONYM_TYPE = ClassicTokenizer.TOKEN_TYPES[ClassicTokenizer.ACRONYM];
37 // this filters uses attribute type
38 private final TypeAttribute typeAtt = addAttribute(TypeAttribute.class);
39 private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
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.
46 public final boolean incrementToken() throws java.io.IOException {
47 if (!input.incrementToken()) {
51 final char[] buffer = termAtt.buffer();
52 final int bufferLength = termAtt.length();
53 final String type = typeAtt.type();
55 if (type == APOSTROPHE_TYPE && // remove 's
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
63 for(int i=0;i<bufferLength;i++) {
68 termAtt.setLength(upto);