1 package org.apache.lucene.analysis.cn;
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 java.io.IOException;
21 import java.util.Arrays;
23 import org.apache.lucene.analysis.CharArraySet;
24 import org.apache.lucene.analysis.TokenFilter;
25 import org.apache.lucene.analysis.TokenStream;
26 import org.apache.lucene.analysis.StopFilter;
27 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
28 import org.apache.lucene.util.Version;
31 * A {@link TokenFilter} with a stop word table.
33 * <li>Numeric tokens are removed.
34 * <li>English tokens must be larger than 1 character.
35 * <li>One Chinese character as one Chinese word.
39 * <li>Add Chinese stop words, such as \ue400
40 * <li>Dictionary based Chinese word extraction
41 * <li>Intelligent Chinese word extraction
45 * @deprecated Use {@link StopFilter} instead, which has the same functionality.
46 * This filter will be removed in Lucene 5.0
49 public final class ChineseFilter extends TokenFilter {
52 // Only English now, Chinese to be added later.
53 public static final String[] STOP_WORDS = {
54 "and", "are", "as", "at", "be", "but", "by",
55 "for", "if", "in", "into", "is", "it",
56 "no", "not", "of", "on", "or", "such",
57 "that", "the", "their", "then", "there", "these",
58 "they", "this", "to", "was", "will", "with"
62 private CharArraySet stopTable;
64 private CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
66 public ChineseFilter(TokenStream in) {
69 stopTable = new CharArraySet(Version.LUCENE_CURRENT, Arrays.asList(STOP_WORDS), false);
73 public boolean incrementToken() throws IOException {
75 while (input.incrementToken()) {
76 char text[] = termAtt.buffer();
77 int termLength = termAtt.length();
79 // why not key off token type here assuming ChineseTokenizer comes first?
80 if (!stopTable.contains(text, 0, termLength)) {
81 switch (Character.getType(text[0])) {
83 case Character.LOWERCASE_LETTER:
84 case Character.UPPERCASE_LETTER:
86 // English word/token should larger than 1 character.
91 case Character.OTHER_LETTER:
93 // One Chinese character as one Chinese word.
94 // Chinese word extraction to be added later here.