1 package org.apache.lucene.analysis.es;
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 java.io.IOException;
21 import java.io.Reader;
24 import org.apache.lucene.analysis.Analyzer;
25 import org.apache.lucene.analysis.CharArraySet;
26 import org.apache.lucene.analysis.KeywordMarkerFilter;
27 import org.apache.lucene.analysis.LowerCaseFilter;
28 import org.apache.lucene.analysis.StopFilter;
29 import org.apache.lucene.analysis.StopwordAnalyzerBase;
30 import org.apache.lucene.analysis.TokenStream;
31 import org.apache.lucene.analysis.Tokenizer;
32 import org.apache.lucene.analysis.WordlistLoader;
33 import org.apache.lucene.analysis.snowball.SnowballFilter;
34 import org.apache.lucene.analysis.standard.StandardFilter;
35 import org.apache.lucene.analysis.standard.StandardTokenizer;
36 import org.apache.lucene.util.IOUtils;
37 import org.apache.lucene.util.Version;
38 import org.tartarus.snowball.ext.SpanishStemmer;
41 * {@link Analyzer} for Spanish.
43 public final class SpanishAnalyzer extends StopwordAnalyzerBase {
44 private final Set<?> stemExclusionSet;
46 /** File containing default Spanish stopwords. */
47 public final static String DEFAULT_STOPWORD_FILE = "spanish_stop.txt";
50 * Returns an unmodifiable instance of the default stop words set.
51 * @return default stop words set.
53 public static Set<?> getDefaultStopSet(){
54 return DefaultSetHolder.DEFAULT_STOP_SET;
58 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
59 * accesses the static final set the first time.;
61 private static class DefaultSetHolder {
62 static final Set<?> DEFAULT_STOP_SET;
66 DEFAULT_STOP_SET = WordlistLoader.getSnowballWordSet(IOUtils.getDecodingReader(SnowballFilter.class,
67 DEFAULT_STOPWORD_FILE, IOUtils.CHARSET_UTF_8), Version.LUCENE_CURRENT);
68 } catch (IOException ex) {
69 // default set should always be present as it is part of the
71 throw new RuntimeException("Unable to load default stopword set");
77 * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
79 public SpanishAnalyzer(Version matchVersion) {
80 this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
84 * Builds an analyzer with the given stop words.
86 * @param matchVersion lucene compatibility version
87 * @param stopwords a stopword set
89 public SpanishAnalyzer(Version matchVersion, Set<?> stopwords) {
90 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
94 * Builds an analyzer with the given stop words. If a non-empty stem exclusion set is
95 * provided this analyzer will add a {@link KeywordMarkerFilter} before
98 * @param matchVersion lucene compatibility version
99 * @param stopwords a stopword set
100 * @param stemExclusionSet a set of terms not to be stemmed
102 public SpanishAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
103 super(matchVersion, stopwords);
104 this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
105 matchVersion, stemExclusionSet));
110 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
111 * which tokenizes all the text in the provided {@link Reader}.
114 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
115 * built from an {@link StandardTokenizer} filtered with
116 * {@link StandardFilter}, {@link LowerCaseFilter}, {@link StopFilter}
117 * , {@link KeywordMarkerFilter} if a stem exclusion set is
118 * provided and {@link SnowballFilter}.
121 protected TokenStreamComponents createComponents(String fieldName,
123 final Tokenizer source = new StandardTokenizer(matchVersion, reader);
124 TokenStream result = new StandardFilter(matchVersion, source);
125 result = new LowerCaseFilter(matchVersion, result);
126 result = new StopFilter(matchVersion, result, stopwords);
127 if(!stemExclusionSet.isEmpty())
128 result = new KeywordMarkerFilter(result, stemExclusionSet);
129 result = new SnowballFilter(result, new SpanishStemmer());
130 return new TokenStreamComponents(source, result);