pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / cn / ChineseFilter.java
1 package org.apache.lucene.analysis.cn;
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 import java.util.Arrays;
22
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;
29
30 /**
31  * A {@link TokenFilter} with a stop word table.  
32  * <ul>
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.
36  * </ul>
37  * TO DO:
38  * <ol>
39  * <li>Add Chinese stop words, such as \ue400
40  * <li>Dictionary based Chinese word extraction
41  * <li>Intelligent Chinese word extraction
42  * </ol>
43  * 
44  * @version 1.0
45  * @deprecated Use {@link StopFilter} instead, which has the same functionality.
46  * This filter will be removed in Lucene 5.0
47  */
48 @Deprecated
49 public final class ChineseFilter extends TokenFilter {
50
51
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"
59     };
60
61
62     private CharArraySet stopTable;
63
64     private CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
65     
66     public ChineseFilter(TokenStream in) {
67         super(in);
68
69         stopTable = new CharArraySet(Version.LUCENE_CURRENT, Arrays.asList(STOP_WORDS), false);
70     }
71
72     @Override
73     public boolean incrementToken() throws IOException {
74
75         while (input.incrementToken()) {
76             char text[] = termAtt.buffer();
77             int termLength = termAtt.length();
78
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])) {
82
83                 case Character.LOWERCASE_LETTER:
84                 case Character.UPPERCASE_LETTER:
85
86                     // English word/token should larger than 1 character.
87                     if (termLength>1) {
88                         return true;
89                     }
90                     break;
91                 case Character.OTHER_LETTER:
92
93                     // One Chinese character as one Chinese word.
94                     // Chinese word extraction to be added later here.
95
96                     return true;
97                 }
98
99             }
100
101         }
102         return false;
103     }
104
105 }