~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/contrib/analyzers/smartcn/src/java/org/apache/lucene/analysis/cn/smart/SmartChineseAnalyzer.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
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
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
 
18
 
package org.apache.lucene.analysis.cn.smart;
19
 
 
20
 
import java.io.IOException;
21
 
import java.io.Reader;
22
 
import java.util.Collections;
23
 
import java.util.Set;
24
 
 
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;
36
 
 
37
 
/**
38
 
 * <p>
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.
42
 
 * </p>
43
 
 * <p>
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.
46
 
 * </p>
47
 
 * <p>
48
 
 * This analyzer requires a dictionary to provide statistical data. 
49
 
 * SmartChineseAnalyzer has an included dictionary out-of-box.
50
 
 * </p>
51
 
 * <p>
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!
54
 
 * </p>
55
 
 * @lucene.experimental
56
 
 */
57
 
public final class SmartChineseAnalyzer extends Analyzer {
58
 
 
59
 
  private final Set<?> stopWords;
60
 
  
61
 
  private static final String DEFAULT_STOPWORD_FILE = "stopwords.txt";
62
 
  
63
 
  private static final String STOPWORD_FILE_COMMENT = "//";
64
 
  
65
 
  /**
66
 
   * Returns an unmodifiable instance of the default stop-words set.
67
 
   * @return an unmodifiable instance of the default stop-words set.
68
 
   */
69
 
  public static CharArraySet getDefaultStopSet(){
70
 
    return DefaultSetHolder.DEFAULT_STOP_SET;
71
 
  }
72
 
  
73
 
  /**
74
 
   * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class 
75
 
   * accesses the static final set the first time.;
76
 
   */
77
 
  private static class DefaultSetHolder {
78
 
    static final CharArraySet DEFAULT_STOP_SET;
79
 
 
80
 
    static {
81
 
      try {
82
 
        DEFAULT_STOP_SET = loadDefaultStopWordSet();
83
 
      } catch (IOException ex) {
84
 
        // default set should always be present as it is part of the
85
 
        // distribution (JAR)
86
 
        throw new RuntimeException("Unable to load default stopword set");
87
 
      }
88
 
    }
89
 
 
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));
96
 
    }
97
 
  }
98
 
 
99
 
  private final Version matchVersion;
100
 
 
101
 
  /**
102
 
   * Create a new SmartChineseAnalyzer, using the default stopword list.
103
 
   */
104
 
  public SmartChineseAnalyzer(Version matchVersion) {
105
 
    this(matchVersion, true);
106
 
  }
107
 
 
108
 
  /**
109
 
   * <p>
110
 
   * Create a new SmartChineseAnalyzer, optionally using the default stopword list.
111
 
   * </p>
112
 
   * <p>
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!
115
 
   * </p>
116
 
   * 
117
 
   * @param useDefaultStopWords true to use the default stopword list.
118
 
   */
119
 
  public SmartChineseAnalyzer(Version matchVersion, boolean useDefaultStopWords) {
120
 
    stopWords = useDefaultStopWords ? DefaultSetHolder.DEFAULT_STOP_SET
121
 
      : Collections.EMPTY_SET;
122
 
    this.matchVersion = matchVersion;
123
 
  }
124
 
 
125
 
  /**
126
 
   * <p>
127
 
   * Create a new SmartChineseAnalyzer, using the provided {@link Set} of stopwords.
128
 
   * </p>
129
 
   * <p>
130
 
   * Note: the set should include punctuation, unless you want to index punctuation!
131
 
   * </p>
132
 
   * @param stopWords {@link Set} of stopwords to use.
133
 
   */
134
 
  public SmartChineseAnalyzer(Version matchVersion, Set stopWords) {
135
 
    this.stopWords = stopWords==null?Collections.EMPTY_SET:stopWords;
136
 
    this.matchVersion = matchVersion;
137
 
  }
138
 
 
139
 
  @Override
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);
149
 
    }
150
 
    return result;
151
 
  }
152
 
  
153
 
  private static final class SavedStreams {
154
 
    Tokenizer tokenStream;
155
 
    TokenStream filteredTokenStream;
156
 
  }
157
 
  
158
 
  @Override
159
 
  public TokenStream reusableTokenStream(String fieldName, Reader reader)
160
 
      throws IOException {
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);
170
 
      }
171
 
    } else {
172
 
      streams.tokenStream.reset(reader);
173
 
      streams.filteredTokenStream.reset(); // reset WordTokenFilter's state
174
 
    }
175
 
 
176
 
    return streams.filteredTokenStream;
177
 
  }
178
 
}