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.Version;
39 import org.tartarus.snowball.ext.ItalianStemmer;
42 * {@link Analyzer} for Italian.
45 * <p>You must specify the required {@link Version}
46 * compatibility when creating ItalianAnalyzer:
48 * <li> As of 3.2, ElisionFilter with a set of Italian
49 * contractions is used by default.
52 public final class ItalianAnalyzer extends StopwordAnalyzerBase {
53 private final Set<?> stemExclusionSet;
55 /** File containing default Italian stopwords. */
56 public final static String DEFAULT_STOPWORD_FILE = "italian_stop.txt";
58 private static final CharArraySet DEFAULT_ARTICLES = CharArraySet.unmodifiableSet(
59 new CharArraySet(Version.LUCENE_CURRENT,
61 "c", "l", "all", "dall", "dell", "nell", "sull", "coll", "pell",
62 "gl", "agl", "dagl", "degl", "negl", "sugl", "un", "m", "t", "s", "v", "d"
66 * Returns an unmodifiable instance of the default stop words set.
67 * @return default stop words set.
69 public static Set<?> getDefaultStopSet(){
70 return DefaultSetHolder.DEFAULT_STOP_SET;
74 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
75 * accesses the static final set the first time.;
77 private static class DefaultSetHolder {
78 static final Set<?> DEFAULT_STOP_SET;
82 DEFAULT_STOP_SET = WordlistLoader.getSnowballWordSet(SnowballFilter.class,
83 DEFAULT_STOPWORD_FILE);
84 } catch (IOException ex) {
85 // default set should always be present as it is part of the
87 throw new RuntimeException("Unable to load default stopword set");
93 * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
95 public ItalianAnalyzer(Version matchVersion) {
96 this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
100 * Builds an analyzer with the given stop words.
102 * @param matchVersion lucene compatibility version
103 * @param stopwords a stopword set
105 public ItalianAnalyzer(Version matchVersion, Set<?> stopwords) {
106 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
110 * Builds an analyzer with the given stop words. If a non-empty stem exclusion set is
111 * provided this analyzer will add a {@link KeywordMarkerFilter} before
114 * @param matchVersion lucene compatibility version
115 * @param stopwords a stopword set
116 * @param stemExclusionSet a set of terms not to be stemmed
118 public ItalianAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
119 super(matchVersion, stopwords);
120 this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
121 matchVersion, stemExclusionSet));
126 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
127 * which tokenizes all the text in the provided {@link Reader}.
130 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
131 * built from an {@link StandardTokenizer} filtered with
132 * {@link StandardFilter}, {@link ElisionFilter}, {@link LowerCaseFilter}, {@link StopFilter}
133 * , {@link KeywordMarkerFilter} if a stem exclusion set is
134 * provided and {@link SnowballFilter}.
137 protected TokenStreamComponents createComponents(String fieldName,
139 final Tokenizer source = new StandardTokenizer(matchVersion, reader);
140 TokenStream result = new StandardFilter(matchVersion, source);
141 if (matchVersion.onOrAfter(Version.LUCENE_32)) {
142 result = new ElisionFilter(matchVersion, result, DEFAULT_ARTICLES);
144 result = new LowerCaseFilter(matchVersion, result);
145 result = new StopFilter(matchVersion, result, stopwords);
146 if(!stemExclusionSet.isEmpty())
147 result = new KeywordMarkerFilter(result, stemExclusionSet);
148 result = new SnowballFilter(result, new ItalianStemmer());
149 return new TokenStreamComponents(source, result);