1 package org.apache.lucene.analysis.it;
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;
22 import java.util.Arrays;
25 import org.apache.lucene.analysis.Analyzer;
26 import org.apache.lucene.analysis.CharArraySet;
27 import org.apache.lucene.analysis.KeywordMarkerFilter;
28 import org.apache.lucene.analysis.LowerCaseFilter;
29 import org.apache.lucene.analysis.StopFilter;
30 import org.apache.lucene.analysis.StopwordAnalyzerBase;
31 import org.apache.lucene.analysis.fr.ElisionFilter;
32 import org.apache.lucene.analysis.TokenStream;
33 import org.apache.lucene.analysis.Tokenizer;
34 import org.apache.lucene.analysis.WordlistLoader;
35 import org.apache.lucene.analysis.snowball.SnowballFilter;
36 import org.apache.lucene.analysis.standard.StandardFilter;
37 import org.apache.lucene.analysis.standard.StandardTokenizer;
38 import org.apache.lucene.util.IOUtils;
39 import org.apache.lucene.util.Version;
40 import org.tartarus.snowball.ext.ItalianStemmer;
43 * {@link Analyzer} for Italian.
46 * <p>You must specify the required {@link Version}
47 * compatibility when creating ItalianAnalyzer:
49 * <li> As of 3.2, ElisionFilter with a set of Italian
50 * contractions is used by default.
53 public final class ItalianAnalyzer extends StopwordAnalyzerBase {
54 private final Set<?> stemExclusionSet;
56 /** File containing default Italian stopwords. */
57 public final static String DEFAULT_STOPWORD_FILE = "italian_stop.txt";
59 private static final CharArraySet DEFAULT_ARTICLES = CharArraySet.unmodifiableSet(
60 new CharArraySet(Version.LUCENE_CURRENT,
62 "c", "l", "all", "dall", "dell", "nell", "sull", "coll", "pell",
63 "gl", "agl", "dagl", "degl", "negl", "sugl", "un", "m", "t", "s", "v", "d"
67 * Returns an unmodifiable instance of the default stop words set.
68 * @return default stop words set.
70 public static Set<?> getDefaultStopSet(){
71 return DefaultSetHolder.DEFAULT_STOP_SET;
75 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
76 * accesses the static final set the first time.;
78 private static class DefaultSetHolder {
79 static final Set<?> DEFAULT_STOP_SET;
83 DEFAULT_STOP_SET = WordlistLoader.getSnowballWordSet(IOUtils.getDecodingReader(SnowballFilter.class,
84 DEFAULT_STOPWORD_FILE, IOUtils.CHARSET_UTF_8), Version.LUCENE_CURRENT);
85 } catch (IOException ex) {
86 // default set should always be present as it is part of the
88 throw new RuntimeException("Unable to load default stopword set");
94 * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
96 public ItalianAnalyzer(Version matchVersion) {
97 this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
101 * Builds an analyzer with the given stop words.
103 * @param matchVersion lucene compatibility version
104 * @param stopwords a stopword set
106 public ItalianAnalyzer(Version matchVersion, Set<?> stopwords) {
107 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
111 * Builds an analyzer with the given stop words. If a non-empty stem exclusion set is
112 * provided this analyzer will add a {@link KeywordMarkerFilter} before
115 * @param matchVersion lucene compatibility version
116 * @param stopwords a stopword set
117 * @param stemExclusionSet a set of terms not to be stemmed
119 public ItalianAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
120 super(matchVersion, stopwords);
121 this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
122 matchVersion, stemExclusionSet));
127 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
128 * which tokenizes all the text in the provided {@link Reader}.
131 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
132 * built from an {@link StandardTokenizer} filtered with
133 * {@link StandardFilter}, {@link ElisionFilter}, {@link LowerCaseFilter}, {@link StopFilter}
134 * , {@link KeywordMarkerFilter} if a stem exclusion set is
135 * provided and {@link SnowballFilter}.
138 protected TokenStreamComponents createComponents(String fieldName,
140 final Tokenizer source = new StandardTokenizer(matchVersion, reader);
141 TokenStream result = new StandardFilter(matchVersion, source);
142 if (matchVersion.onOrAfter(Version.LUCENE_32)) {
143 result = new ElisionFilter(matchVersion, result, DEFAULT_ARTICLES);
145 result = new LowerCaseFilter(matchVersion, result);
146 result = new StopFilter(matchVersion, result, stopwords);
147 if(!stemExclusionSet.isEmpty())
148 result = new KeywordMarkerFilter(result, stemExclusionSet);
149 result = new SnowballFilter(result, new ItalianStemmer());
150 return new TokenStreamComponents(source, result);