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