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.LowerCaseFilter;
21 import org.apache.lucene.analysis.StopAnalyzer;
22 import org.apache.lucene.analysis.StopFilter;
23 import org.apache.lucene.analysis.StopwordAnalyzerBase;
24 import org.apache.lucene.analysis.TokenStream;
25 import org.apache.lucene.analysis.WordlistLoader;
26 import org.apache.lucene.util.Version;
29 import java.io.IOException;
30 import java.io.Reader;
34 * Filters {@link ClassicTokenizer} with {@link ClassicFilter}, {@link
35 * LowerCaseFilter} and {@link StopFilter}, using a list of
39 * <p>You must specify the required {@link Version}
40 * compatibility when creating ClassicAnalyzer:
42 * <li> As of 3.1, StopFilter correctly handles Unicode 4.0
43 * supplementary characters in stopwords
44 * <li> As of 2.9, StopFilter preserves position
46 * <li> As of 2.4, Tokens incorrectly identified as acronyms
47 * are corrected (see <a href="https://issues.apache.org/jira/browse/LUCENE-1068">LUCENE-1068</a>)
50 * ClassicAnalyzer was named StandardAnalyzer in Lucene versions prior to 3.1.
51 * As of 3.1, {@link StandardAnalyzer} implements Unicode text segmentation,
52 * as specified by UAX#29.
54 public final class ClassicAnalyzer extends StopwordAnalyzerBase {
56 /** Default maximum allowed token length */
57 public static final int DEFAULT_MAX_TOKEN_LENGTH = 255;
59 private int maxTokenLength = DEFAULT_MAX_TOKEN_LENGTH;
62 * Specifies whether deprecated acronyms should be replaced with HOST type.
63 * See {@linkplain "https://issues.apache.org/jira/browse/LUCENE-1068"}
65 private final boolean replaceInvalidAcronym;
67 /** An unmodifiable set containing some common English words that are usually not
68 useful for searching. */
69 public static final Set<?> STOP_WORDS_SET = StopAnalyzer.ENGLISH_STOP_WORDS_SET;
71 /** Builds an analyzer with the given stop words.
72 * @param matchVersion Lucene version to match See {@link
73 * <a href="#version">above</a>}
74 * @param stopWords stop words */
75 public ClassicAnalyzer(Version matchVersion, Set<?> stopWords) {
76 super(matchVersion, stopWords);
77 replaceInvalidAcronym = matchVersion.onOrAfter(Version.LUCENE_24);
80 /** Builds an analyzer with the default stop words ({@link
82 * @param matchVersion Lucene version to match See {@link
83 * <a href="#version">above</a>}
85 public ClassicAnalyzer(Version matchVersion) {
86 this(matchVersion, STOP_WORDS_SET);
89 /** Builds an analyzer with the stop words from the given file.
90 * @see WordlistLoader#getWordSet(File)
91 * @param matchVersion Lucene version to match See {@link
92 * <a href="#version">above</a>}
93 * @param stopwords File to read stop words from */
94 public ClassicAnalyzer(Version matchVersion, File stopwords) throws IOException {
95 this(matchVersion, WordlistLoader.getWordSet(stopwords));
98 /** Builds an analyzer with the stop words from the given reader.
99 * @see WordlistLoader#getWordSet(Reader)
100 * @param matchVersion Lucene version to match See {@link
101 * <a href="#version">above</a>}
102 * @param stopwords Reader to read stop words from */
103 public ClassicAnalyzer(Version matchVersion, Reader stopwords) throws IOException {
104 this(matchVersion, WordlistLoader.getWordSet(stopwords));
108 * Set maximum allowed token length. If a token is seen
109 * that exceeds this length then it is discarded. This
110 * setting only takes effect the next time tokenStream or
111 * reusableTokenStream is called.
113 public void setMaxTokenLength(int length) {
114 maxTokenLength = length;
118 * @see #setMaxTokenLength
120 public int getMaxTokenLength() {
121 return maxTokenLength;
125 protected TokenStreamComponents createComponents(final String fieldName, final Reader reader) {
126 final ClassicTokenizer src = new ClassicTokenizer(matchVersion, reader);
127 src.setMaxTokenLength(maxTokenLength);
128 src.setReplaceInvalidAcronym(replaceInvalidAcronym);
129 TokenStream tok = new ClassicFilter(src);
130 tok = new LowerCaseFilter(matchVersion, tok);
131 tok = new StopFilter(matchVersion, tok, stopwords);
132 return new TokenStreamComponents(src, tok) {
134 protected boolean reset(final Reader reader) throws IOException {
135 src.setMaxTokenLength(ClassicAnalyzer.this.maxTokenLength);
136 return super.reset(reader);