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.*;
21 import org.apache.lucene.util.Version;
24 import java.io.IOException;
25 import java.io.Reader;
29 * Filters {@link StandardTokenizer} with {@link StandardFilter}, {@link
30 * LowerCaseFilter} and {@link StopFilter}, using a list of
34 * <p>You must specify the required {@link Version}
35 * compatibility when creating StandardAnalyzer:
37 * <li> As of 3.4, Hiragana and Han characters are no longer wrongly split
38 * from their combining characters. If you use a previous version number,
39 * you get the exact broken behavior for backwards compatibility.
40 * <li> As of 3.1, StandardTokenizer implements Unicode text segmentation,
41 * and StopFilter correctly handles Unicode 4.0 supplementary characters
42 * in stopwords. {@link ClassicTokenizer} and {@link ClassicAnalyzer}
43 * are the pre-3.1 implementations of StandardTokenizer and
45 * <li> As of 2.9, StopFilter preserves position increments
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 public final class StandardAnalyzer extends StopwordAnalyzerBase {
52 /** Default maximum allowed token length */
53 public static final int DEFAULT_MAX_TOKEN_LENGTH = 255;
55 private int maxTokenLength = DEFAULT_MAX_TOKEN_LENGTH;
58 * Specifies whether deprecated acronyms should be replaced with HOST type.
59 * See {@linkplain "https://issues.apache.org/jira/browse/LUCENE-1068"}
61 private final boolean replaceInvalidAcronym;
63 /** An unmodifiable set containing some common English words that are usually not
64 useful for searching. */
65 public static final Set<?> STOP_WORDS_SET = StopAnalyzer.ENGLISH_STOP_WORDS_SET;
67 /** Builds an analyzer with the given stop words.
68 * @param matchVersion Lucene version to match See {@link
69 * <a href="#version">above</a>}
70 * @param stopWords stop words */
71 public StandardAnalyzer(Version matchVersion, Set<?> stopWords) {
72 super(matchVersion, stopWords);
73 replaceInvalidAcronym = matchVersion.onOrAfter(Version.LUCENE_24);
76 /** Builds an analyzer with the default stop words ({@link
78 * @param matchVersion Lucene version to match See {@link
79 * <a href="#version">above</a>}
81 public StandardAnalyzer(Version matchVersion) {
82 this(matchVersion, STOP_WORDS_SET);
85 /** Builds an analyzer with the stop words from the given file.
86 * @see WordlistLoader#getWordSet(File)
87 * @param matchVersion Lucene version to match See {@link
88 * <a href="#version">above</a>}
89 * @param stopwords File to read stop words from */
90 public StandardAnalyzer(Version matchVersion, File stopwords) throws IOException {
91 this(matchVersion, WordlistLoader.getWordSet(stopwords));
94 /** Builds an analyzer with the stop words from the given reader.
95 * @see WordlistLoader#getWordSet(Reader)
96 * @param matchVersion Lucene version to match See {@link
97 * <a href="#version">above</a>}
98 * @param stopwords Reader to read stop words from */
99 public StandardAnalyzer(Version matchVersion, Reader stopwords) throws IOException {
100 this(matchVersion, WordlistLoader.getWordSet(stopwords));
104 * Set maximum allowed token length. If a token is seen
105 * that exceeds this length then it is discarded. This
106 * setting only takes effect the next time tokenStream or
107 * reusableTokenStream is called.
109 public void setMaxTokenLength(int length) {
110 maxTokenLength = length;
114 * @see #setMaxTokenLength
116 public int getMaxTokenLength() {
117 return maxTokenLength;
121 protected TokenStreamComponents createComponents(final String fieldName, final Reader reader) {
122 final StandardTokenizer src = new StandardTokenizer(matchVersion, reader);
123 src.setMaxTokenLength(maxTokenLength);
124 src.setReplaceInvalidAcronym(replaceInvalidAcronym);
125 TokenStream tok = new StandardFilter(matchVersion, src);
126 tok = new LowerCaseFilter(matchVersion, tok);
127 tok = new StopFilter(matchVersion, tok, stopwords);
128 return new TokenStreamComponents(src, tok) {
130 protected boolean reset(final Reader reader) throws IOException {
131 src.setMaxTokenLength(StandardAnalyzer.this.maxTokenLength);
132 return super.reset(reader);