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