pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / it / ItalianAnalyzer.java
1 package org.apache.lucene.analysis.it;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.IOException;
21 import java.io.Reader;
22 import java.util.Arrays;
23 import java.util.Set;
24
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;
41
42 /**
43  * {@link Analyzer} for Italian.
44  * <p>
45  * <a name="version"/>
46  * <p>You must specify the required {@link Version}
47  * compatibility when creating ItalianAnalyzer:
48  * <ul>
49  *   <li> As of 3.2, ElisionFilter with a set of Italian 
50  *        contractions is used by default.
51  * </ul>
52  */
53 public final class ItalianAnalyzer extends StopwordAnalyzerBase {
54   private final Set<?> stemExclusionSet;
55   
56   /** File containing default Italian stopwords. */
57   public final static String DEFAULT_STOPWORD_FILE = "italian_stop.txt";
58   
59   private static final CharArraySet DEFAULT_ARTICLES = CharArraySet.unmodifiableSet(
60       new CharArraySet(Version.LUCENE_CURRENT, 
61           Arrays.asList(
62           "c", "l", "all", "dall", "dell", "nell", "sull", "coll", "pell", 
63           "gl", "agl", "dagl", "degl", "negl", "sugl", "un", "m", "t", "s", "v", "d"
64           ), true));
65
66   /**
67    * Returns an unmodifiable instance of the default stop words set.
68    * @return default stop words set.
69    */
70   public static Set<?> getDefaultStopSet(){
71     return DefaultSetHolder.DEFAULT_STOP_SET;
72   }
73   
74   /**
75    * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class 
76    * accesses the static final set the first time.;
77    */
78   private static class DefaultSetHolder {
79     static final Set<?> DEFAULT_STOP_SET;
80
81     static {
82       try {
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
87         // distribution (JAR)
88         throw new RuntimeException("Unable to load default stopword set");
89       }
90     }
91   }
92
93   /**
94    * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
95    */
96   public ItalianAnalyzer(Version matchVersion) {
97     this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
98   }
99   
100   /**
101    * Builds an analyzer with the given stop words.
102    * 
103    * @param matchVersion lucene compatibility version
104    * @param stopwords a stopword set
105    */
106   public ItalianAnalyzer(Version matchVersion, Set<?> stopwords) {
107     this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
108   }
109
110   /**
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
113    * stemming.
114    * 
115    * @param matchVersion lucene compatibility version
116    * @param stopwords a stopword set
117    * @param stemExclusionSet a set of terms not to be stemmed
118    */
119   public ItalianAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
120     super(matchVersion, stopwords);
121     this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
122         matchVersion, stemExclusionSet));
123   }
124
125   /**
126    * Creates a
127    * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
128    * which tokenizes all the text in the provided {@link Reader}.
129    * 
130    * @return A
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}.
136    */
137   @Override
138   protected TokenStreamComponents createComponents(String fieldName,
139       Reader reader) {
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);
144     }
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);
151   }
152 }