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.IOUtils;
22 import org.apache.lucene.util.Version;
25 import java.io.IOException;
26 import java.io.Reader;
30 * Filters {@link StandardTokenizer} with {@link StandardFilter}, {@link
31 * LowerCaseFilter} and {@link StopFilter}, using a list of
35 * <p>You must specify the required {@link Version}
36 * compatibility when creating StandardAnalyzer:
38 * <li> As of 3.4, Hiragana and Han characters are no longer wrongly split
39 * from their combining characters. If you use a previous version number,
40 * you get the exact broken behavior for backwards compatibility.
41 * <li> As of 3.1, StandardTokenizer implements Unicode text segmentation,
42 * and StopFilter correctly handles Unicode 4.0 supplementary characters
43 * in stopwords. {@link ClassicTokenizer} and {@link ClassicAnalyzer}
44 * are the pre-3.1 implementations of StandardTokenizer and
46 * <li> As of 2.9, StopFilter preserves position increments
47 * <li> As of 2.4, Tokens incorrectly identified as acronyms
48 * are corrected (see <a href="https://issues.apache.org/jira/browse/LUCENE-1068">LUCENE-1068</a>)
51 public final class StandardAnalyzer extends StopwordAnalyzerBase {
53 /** Default maximum allowed token length */
54 public static final int DEFAULT_MAX_TOKEN_LENGTH = 255;
56 private int maxTokenLength = DEFAULT_MAX_TOKEN_LENGTH;
59 * Specifies whether deprecated acronyms should be replaced with HOST type.
60 * See {@linkplain "https://issues.apache.org/jira/browse/LUCENE-1068"}
62 private final boolean replaceInvalidAcronym;
64 /** An unmodifiable set containing some common English words that are usually not
65 useful for searching. */
66 public static final Set<?> STOP_WORDS_SET = StopAnalyzer.ENGLISH_STOP_WORDS_SET;
68 /** Builds an analyzer with the given stop words.
69 * @param matchVersion Lucene version to match See {@link
70 * <a href="#version">above</a>}
71 * @param stopWords stop words */
72 public StandardAnalyzer(Version matchVersion, Set<?> stopWords) {
73 super(matchVersion, stopWords);
74 replaceInvalidAcronym = matchVersion.onOrAfter(Version.LUCENE_24);
77 /** Builds an analyzer with the default stop words ({@link
79 * @param matchVersion Lucene version to match See {@link
80 * <a href="#version">above</a>}
82 public StandardAnalyzer(Version matchVersion) {
83 this(matchVersion, STOP_WORDS_SET);
86 /** Builds an analyzer with the stop words from the given file.
87 * @see WordlistLoader#getWordSet(Reader, Version)
88 * @param matchVersion Lucene version to match See {@link
89 * <a href="#version">above</a>}
90 * @param stopwords File to read stop words from */
91 public StandardAnalyzer(Version matchVersion, File stopwords) throws IOException {
92 this(matchVersion, WordlistLoader.getWordSet(IOUtils.getDecodingReader(stopwords,
93 IOUtils.CHARSET_UTF_8), matchVersion));
96 /** Builds an analyzer with the stop words from the given reader.
97 * @see WordlistLoader#getWordSet(Reader, Version)
98 * @param matchVersion Lucene version to match See {@link
99 * <a href="#version">above</a>}
100 * @param stopwords Reader to read stop words from */
101 public StandardAnalyzer(Version matchVersion, Reader stopwords) throws IOException {
102 this(matchVersion, WordlistLoader.getWordSet(stopwords, matchVersion));
106 * Set maximum allowed token length. If a token is seen
107 * that exceeds this length then it is discarded. This
108 * setting only takes effect the next time tokenStream or
109 * reusableTokenStream is called.
111 public void setMaxTokenLength(int length) {
112 maxTokenLength = length;
116 * @see #setMaxTokenLength
118 public int getMaxTokenLength() {
119 return maxTokenLength;
123 protected TokenStreamComponents createComponents(final String fieldName, final Reader reader) {
124 final StandardTokenizer src = new StandardTokenizer(matchVersion, reader);
125 src.setMaxTokenLength(maxTokenLength);
126 src.setReplaceInvalidAcronym(replaceInvalidAcronym);
127 TokenStream tok = new StandardFilter(matchVersion, src);
128 tok = new LowerCaseFilter(matchVersion, tok);
129 tok = new StopFilter(matchVersion, tok, stopwords);
130 return new TokenStreamComponents(src, tok) {
132 protected boolean reset(final Reader reader) throws IOException {
133 src.setMaxTokenLength(StandardAnalyzer.this.maxTokenLength);
134 return super.reset(reader);