add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / fr / FrenchAnalyzer.java
1 package org.apache.lucene.analysis.fr;
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 org.apache.lucene.analysis.Analyzer;
21 import org.apache.lucene.analysis.CharArraySet;
22 import org.apache.lucene.analysis.LowerCaseFilter;
23 import org.apache.lucene.analysis.KeywordMarkerFilter;
24 import org.apache.lucene.analysis.StopFilter;
25 import org.apache.lucene.analysis.StopwordAnalyzerBase;
26 import org.apache.lucene.analysis.TokenStream;
27 import org.apache.lucene.analysis.Tokenizer;
28 import org.apache.lucene.analysis.WordlistLoader;
29 import org.apache.lucene.analysis.snowball.SnowballFilter;
30 import org.apache.lucene.analysis.standard.StandardFilter;
31 import org.apache.lucene.analysis.standard.StandardTokenizer;
32 import org.apache.lucene.analysis.standard.StandardAnalyzer;  // for javadoc
33 import org.apache.lucene.util.Version;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.io.Reader;
38 import java.util.Arrays;
39 import java.util.HashSet;
40 import java.util.Collections;
41 import java.util.Map;
42 import java.util.Set;
43
44 /**
45  * {@link Analyzer} for French language. 
46  * <p>
47  * Supports an external list of stopwords (words that
48  * will not be indexed at all) and an external list of exclusions (word that will
49  * not be stemmed, but indexed).
50  * A default set of stopwords is used unless an alternative list is specified, but the
51  * exclusion list is empty by default.
52  * </p>
53  *
54  * <a name="version"/>
55  * <p>You must specify the required {@link Version}
56  * compatibility when creating FrenchAnalyzer:
57  * <ul>
58  *   <li> As of 3.1, Snowball stemming is done with SnowballFilter, 
59  *        LowerCaseFilter is used prior to StopFilter, and ElisionFilter and 
60  *        Snowball stopwords are used by default.
61  *   <li> As of 2.9, StopFilter preserves position
62  *        increments
63  * </ul>
64  *
65  * <p><b>NOTE</b>: This class uses the same {@link Version}
66  * dependent settings as {@link StandardAnalyzer}.</p>
67  */
68 public final class FrenchAnalyzer extends StopwordAnalyzerBase {
69
70   /**
71    * Extended list of typical French stopwords.
72    * @deprecated use {@link #getDefaultStopSet()} instead
73    */
74   @Deprecated
75   public final static String[] FRENCH_STOP_WORDS = {
76     "a", "afin", "ai", "ainsi", "après", "attendu", "au", "aujourd", "auquel", "aussi",
77     "autre", "autres", "aux", "auxquelles", "auxquels", "avait", "avant", "avec", "avoir",
78     "c", "car", "ce", "ceci", "cela", "celle", "celles", "celui", "cependant", "certain",
79     "certaine", "certaines", "certains", "ces", "cet", "cette", "ceux", "chez", "ci",
80     "combien", "comme", "comment", "concernant", "contre", "d", "dans", "de", "debout",
81     "dedans", "dehors", "delà", "depuis", "derrière", "des", "désormais", "desquelles",
82     "desquels", "dessous", "dessus", "devant", "devers", "devra", "divers", "diverse",
83     "diverses", "doit", "donc", "dont", "du", "duquel", "durant", "dès", "elle", "elles",
84     "en", "entre", "environ", "est", "et", "etc", "etre", "eu", "eux", "excepté", "hormis",
85     "hors", "hélas", "hui", "il", "ils", "j", "je", "jusqu", "jusque", "l", "la", "laquelle",
86     "le", "lequel", "les", "lesquelles", "lesquels", "leur", "leurs", "lorsque", "lui", "là",
87     "ma", "mais", "malgré", "me", "merci", "mes", "mien", "mienne", "miennes", "miens", "moi",
88     "moins", "mon", "moyennant", "même", "mêmes", "n", "ne", "ni", "non", "nos", "notre",
89     "nous", "néanmoins", "nôtre", "nôtres", "on", "ont", "ou", "outre", "où", "par", "parmi",
90     "partant", "pas", "passé", "pendant", "plein", "plus", "plusieurs", "pour", "pourquoi",
91     "proche", "près", "puisque", "qu", "quand", "que", "quel", "quelle", "quelles", "quels",
92     "qui", "quoi", "quoique", "revoici", "revoilà", "s", "sa", "sans", "sauf", "se", "selon",
93     "seront", "ses", "si", "sien", "sienne", "siennes", "siens", "sinon", "soi", "soit",
94     "son", "sont", "sous", "suivant", "sur", "ta", "te", "tes", "tien", "tienne", "tiennes",
95     "tiens", "toi", "ton", "tous", "tout", "toute", "toutes", "tu", "un", "une", "va", "vers",
96     "voici", "voilà", "vos", "votre", "vous", "vu", "vôtre", "vôtres", "y", "à", "ça", "ès",
97     "été", "être", "ô"
98   };
99
100   /** File containing default French stopwords. */
101   public final static String DEFAULT_STOPWORD_FILE = "french_stop.txt";
102   
103   /**
104    * Contains words that should be indexed but not stemmed.
105    */
106   private Set<?> excltable = Collections.<Object>emptySet();
107
108   /**
109    * Returns an unmodifiable instance of the default stop-words set.
110    * @return an unmodifiable instance of the default stop-words set.
111    */
112   public static Set<?> getDefaultStopSet(){
113     return DefaultSetHolder.DEFAULT_STOP_SET;
114   }
115   
116   private static class DefaultSetHolder {
117     /** @deprecated remove this in Lucene 5.0 */
118     @Deprecated
119     static final Set<?> DEFAULT_STOP_SET_30 = CharArraySet
120         .unmodifiableSet(new CharArraySet(Version.LUCENE_CURRENT, Arrays.asList(FRENCH_STOP_WORDS),
121             false));
122     static final Set<?> DEFAULT_STOP_SET;
123     static {
124       try {
125         DEFAULT_STOP_SET = 
126           WordlistLoader.getSnowballWordSet(SnowballFilter.class, DEFAULT_STOPWORD_FILE);
127       } catch (IOException ex) {
128         // default set should always be present as it is part of the
129         // distribution (JAR)
130         throw new RuntimeException("Unable to load default stopword set");
131       }
132     }
133   }
134
135   /**
136    * Builds an analyzer with the default stop words ({@link #getDefaultStopSet}).
137    */
138   public FrenchAnalyzer(Version matchVersion) {
139     this(matchVersion,
140         matchVersion.onOrAfter(Version.LUCENE_31) ? DefaultSetHolder.DEFAULT_STOP_SET
141             : DefaultSetHolder.DEFAULT_STOP_SET_30);
142   }
143   
144   /**
145    * Builds an analyzer with the given stop words
146    * 
147    * @param matchVersion
148    *          lucene compatibility version
149    * @param stopwords
150    *          a stopword set
151    */
152   public FrenchAnalyzer(Version matchVersion, Set<?> stopwords){
153     this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
154   }
155   
156   /**
157    * Builds an analyzer with the given stop words
158    * 
159    * @param matchVersion
160    *          lucene compatibility version
161    * @param stopwords
162    *          a stopword set
163    * @param stemExclutionSet
164    *          a stemming exclusion set
165    */
166   public FrenchAnalyzer(Version matchVersion, Set<?> stopwords,
167       Set<?> stemExclutionSet) {
168     super(matchVersion, stopwords);
169     this.excltable = CharArraySet.unmodifiableSet(CharArraySet
170         .copy(matchVersion, stemExclutionSet));
171   }
172  
173
174   /**
175    * Builds an analyzer with the given stop words.
176    * @deprecated use {@link #FrenchAnalyzer(Version, Set)} instead
177    */
178   @Deprecated
179   public FrenchAnalyzer(Version matchVersion, String... stopwords) {
180     this(matchVersion, StopFilter.makeStopSet(matchVersion, stopwords));
181   }
182
183   /**
184    * Builds an analyzer with the given stop words.
185    * @throws IOException
186    * @deprecated use {@link #FrenchAnalyzer(Version, Set)} instead
187    */
188   @Deprecated
189   public FrenchAnalyzer(Version matchVersion, File stopwords) throws IOException {
190     this(matchVersion, WordlistLoader.getWordSet(stopwords));
191   }
192
193   /**
194    * Builds an exclusionlist from an array of Strings.
195    * @deprecated use {@link #FrenchAnalyzer(Version, Set, Set)} instead
196    */
197   @Deprecated
198   public void setStemExclusionTable(String... exclusionlist) {
199     excltable = StopFilter.makeStopSet(matchVersion, exclusionlist);
200     setPreviousTokenStream(null); // force a new stemmer to be created
201   }
202
203   /**
204    * Builds an exclusionlist from a Map.
205    * @deprecated use {@link #FrenchAnalyzer(Version, Set, Set)} instead
206    */
207   @Deprecated
208   public void setStemExclusionTable(Map<?,?> exclusionlist) {
209     excltable = new HashSet<Object>(exclusionlist.keySet());
210     setPreviousTokenStream(null); // force a new stemmer to be created
211   }
212
213   /**
214    * Builds an exclusionlist from the words contained in the given file.
215    * @throws IOException
216    * @deprecated use {@link #FrenchAnalyzer(Version, Set, Set)} instead
217    */
218   @Deprecated
219   public void setStemExclusionTable(File exclusionlist) throws IOException {
220     excltable = new HashSet<Object>(WordlistLoader.getWordSet(exclusionlist));
221     setPreviousTokenStream(null); // force a new stemmer to be created
222   }
223
224   /**
225    * Creates
226    * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
227    * used to tokenize all the text in the provided {@link Reader}.
228    * 
229    * @return {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
230    *         built from a {@link StandardTokenizer} filtered with
231    *         {@link StandardFilter}, {@link ElisionFilter},
232    *         {@link LowerCaseFilter}, {@link StopFilter},
233    *         {@link KeywordMarkerFilter} if a stem exclusion set is
234    *         provided, and {@link SnowballFilter}
235    */
236   @Override
237   protected TokenStreamComponents createComponents(String fieldName,
238       Reader reader) {
239     if (matchVersion.onOrAfter(Version.LUCENE_31)) {
240       final Tokenizer source = new StandardTokenizer(matchVersion, reader);
241       TokenStream result = new StandardFilter(matchVersion, source);
242       result = new ElisionFilter(matchVersion, result);
243       result = new LowerCaseFilter(matchVersion, result);
244       result = new StopFilter(matchVersion, result, stopwords);
245       if(!excltable.isEmpty())
246         result = new KeywordMarkerFilter(result, excltable);
247       result = new SnowballFilter(result, new org.tartarus.snowball.ext.FrenchStemmer());
248       return new TokenStreamComponents(source, result);
249     } else {
250       final Tokenizer source = new StandardTokenizer(matchVersion, reader);
251       TokenStream result = new StandardFilter(matchVersion, source);
252       result = new StopFilter(matchVersion, result, stopwords);
253       if(!excltable.isEmpty())
254         result = new KeywordMarkerFilter(result, excltable);
255       result = new FrenchStemFilter(result);
256       // Convert to lowercase after stemming!
257       return new TokenStreamComponents(source, new LowerCaseFilter(matchVersion, result));
258     }
259   }
260 }
261