2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.lucene.analysis.cn.smart;
20 import java.io.IOException;
21 import java.io.Reader;
22 import java.util.Collections;
25 import org.apache.lucene.analysis.Analyzer;
26 import org.apache.lucene.analysis.PorterStemFilter;
27 import org.apache.lucene.analysis.StopFilter;
28 import org.apache.lucene.analysis.TokenStream;
29 import org.apache.lucene.analysis.Tokenizer;
30 import org.apache.lucene.analysis.WordlistLoader;
31 import org.apache.lucene.analysis.cn.smart.SentenceTokenizer;
32 import org.apache.lucene.analysis.cn.smart.WordTokenFilter;
33 import org.apache.lucene.analysis.CharArraySet;
34 import org.apache.lucene.util.IOUtils;
35 import org.apache.lucene.util.Version;
39 * SmartChineseAnalyzer is an analyzer for Chinese or mixed Chinese-English text.
40 * The analyzer uses probabilistic knowledge to find the optimal word segmentation for Simplified Chinese text.
41 * The text is first broken into sentences, then each sentence is segmented into words.
44 * Segmentation is based upon the <a href="http://en.wikipedia.org/wiki/Hidden_Markov_Model">Hidden Markov Model</a>.
45 * A large training corpus was used to calculate Chinese word frequency probability.
48 * This analyzer requires a dictionary to provide statistical data.
49 * SmartChineseAnalyzer has an included dictionary out-of-box.
52 * The included dictionary data is from <a href="http://www.ictclas.org">ICTCLAS1.0</a>.
53 * Thanks to ICTCLAS for their hard work, and for contributing the data under the Apache 2 License!
55 * @lucene.experimental
57 public final class SmartChineseAnalyzer extends Analyzer {
59 private final Set<?> stopWords;
61 private static final String DEFAULT_STOPWORD_FILE = "stopwords.txt";
63 private static final String STOPWORD_FILE_COMMENT = "//";
66 * Returns an unmodifiable instance of the default stop-words set.
67 * @return an unmodifiable instance of the default stop-words set.
69 public static CharArraySet getDefaultStopSet(){
70 return DefaultSetHolder.DEFAULT_STOP_SET;
74 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
75 * accesses the static final set the first time.;
77 private static class DefaultSetHolder {
78 static final CharArraySet DEFAULT_STOP_SET;
82 DEFAULT_STOP_SET = loadDefaultStopWordSet();
83 } catch (IOException ex) {
84 // default set should always be present as it is part of the
86 throw new RuntimeException("Unable to load default stopword set");
90 static CharArraySet loadDefaultStopWordSet() throws IOException {
91 // make sure it is unmodifiable as we expose it in the outer class
92 return org.apache.lucene.analysis.CharArraySet.unmodifiableSet(WordlistLoader.getWordSet(IOUtils
93 .getDecodingReader(SmartChineseAnalyzer.class, DEFAULT_STOPWORD_FILE,
94 IOUtils.CHARSET_UTF_8), STOPWORD_FILE_COMMENT,
95 Version.LUCENE_CURRENT));
99 private final Version matchVersion;
102 * Create a new SmartChineseAnalyzer, using the default stopword list.
104 public SmartChineseAnalyzer(Version matchVersion) {
105 this(matchVersion, true);
110 * Create a new SmartChineseAnalyzer, optionally using the default stopword list.
113 * The included default stopword list is simply a list of punctuation.
114 * If you do not use this list, punctuation will not be removed from the text!
117 * @param useDefaultStopWords true to use the default stopword list.
119 public SmartChineseAnalyzer(Version matchVersion, boolean useDefaultStopWords) {
120 stopWords = useDefaultStopWords ? DefaultSetHolder.DEFAULT_STOP_SET
121 : Collections.EMPTY_SET;
122 this.matchVersion = matchVersion;
127 * Create a new SmartChineseAnalyzer, using the provided {@link Set} of stopwords.
130 * Note: the set should include punctuation, unless you want to index punctuation!
132 * @param stopWords {@link Set} of stopwords to use.
134 public SmartChineseAnalyzer(Version matchVersion, Set stopWords) {
135 this.stopWords = stopWords==null?Collections.EMPTY_SET:stopWords;
136 this.matchVersion = matchVersion;
140 public TokenStream tokenStream(String fieldName, Reader reader) {
141 TokenStream result = new SentenceTokenizer(reader);
142 result = new WordTokenFilter(result);
143 // result = new LowerCaseFilter(result);
144 // LowerCaseFilter is not needed, as SegTokenFilter lowercases Basic Latin text.
145 // The porter stemming is too strict, this is not a bug, this is a feature:)
146 result = new PorterStemFilter(result);
147 if (!stopWords.isEmpty()) {
148 result = new StopFilter(matchVersion, result, stopWords, false);
153 private static final class SavedStreams {
154 Tokenizer tokenStream;
155 TokenStream filteredTokenStream;
159 public TokenStream reusableTokenStream(String fieldName, Reader reader)
161 SavedStreams streams = (SavedStreams) getPreviousTokenStream();
162 if (streams == null) {
163 streams = new SavedStreams();
164 setPreviousTokenStream(streams);
165 streams.tokenStream = new SentenceTokenizer(reader);
166 streams.filteredTokenStream = new WordTokenFilter(streams.tokenStream);
167 streams.filteredTokenStream = new PorterStemFilter(streams.filteredTokenStream);
168 if (!stopWords.isEmpty()) {
169 streams.filteredTokenStream = new StopFilter(matchVersion, streams.filteredTokenStream, stopWords, false);
172 streams.tokenStream.reset(reader);
173 streams.filteredTokenStream.reset(); // reset WordTokenFilter's state
176 return streams.filteredTokenStream;