KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > analysis > fr > FrenchAnalyzer


1 package org.apache.lucene.analysis.fr;
2
3 /**
4  * Copyright 2004-2005 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.lucene.analysis.Analyzer;
20 import org.apache.lucene.analysis.LowerCaseFilter;
21 import org.apache.lucene.analysis.StopFilter;
22 import org.apache.lucene.analysis.TokenStream;
23 import org.apache.lucene.analysis.WordlistLoader;
24 import org.apache.lucene.analysis.standard.StandardFilter;
25 import org.apache.lucene.analysis.standard.StandardTokenizer;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.Reader JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.util.Set JavaDoc;
33
34 /**
35  * Analyzer for French language. Supports an external list of stopwords (words that
36  * will not be indexed at all) and an external list of exclusions (word that will
37  * not be stemmed, but indexed).
38  * A default set of stopwords is used unless an alternative list is specified, the
39  * exclusion list is empty by default.
40  *
41  * @author Patrick Talbot (based on Gerhard Schwarz's work for German)
42  * @version $Id: FrenchAnalyzer.java 178832 2005-05-27 23:00:49Z dnaber $
43  */

44 public final class FrenchAnalyzer extends Analyzer {
45
46   /**
47    * Extended list of typical French stopwords.
48    */

49   public final static String JavaDoc[] FRENCH_STOP_WORDS = {
50     "a", "afin", "ai", "ainsi", "après", "attendu", "au", "aujourd", "auquel", "aussi",
51     "autre", "autres", "aux", "auxquelles", "auxquels", "avait", "avant", "avec", "avoir",
52     "c", "car", "ce", "ceci", "cela", "celle", "celles", "celui", "cependant", "certain",
53     "certaine", "certaines", "certains", "ces", "cet", "cette", "ceux", "chez", "ci",
54     "combien", "comme", "comment", "concernant", "contre", "d", "dans", "de", "debout",
55     "dedans", "dehors", "delà", "depuis", "derrière", "des", "désormais", "desquelles",
56     "desquels", "dessous", "dessus", "devant", "devers", "devra", "divers", "diverse",
57     "diverses", "doit", "donc", "dont", "du", "duquel", "durant", "dès", "elle", "elles",
58     "en", "entre", "environ", "est", "et", "etc", "etre", "eu", "eux", "excepté", "hormis",
59     "hors", "hélas", "hui", "il", "ils", "j", "je", "jusqu", "jusque", "l", "la", "laquelle",
60     "le", "lequel", "les", "lesquelles", "lesquels", "leur", "leurs", "lorsque", "lui", "là",
61     "ma", "mais", "malgré", "me", "merci", "mes", "mien", "mienne", "miennes", "miens", "moi",
62     "moins", "mon", "moyennant", "même", "mêmes", "n", "ne", "ni", "non", "nos", "notre",
63     "nous", "néanmoins", "nôtre", "nôtres", "on", "ont", "ou", "outre", "où", "par", "parmi",
64     "partant", "pas", "passé", "pendant", "plein", "plus", "plusieurs", "pour", "pourquoi",
65     "proche", "près", "puisque", "qu", "quand", "que", "quel", "quelle", "quelles", "quels",
66     "qui", "quoi", "quoique", "revoici", "revoilà", "s", "sa", "sans", "sauf", "se", "selon",
67     "seront", "ses", "si", "sien", "sienne", "siennes", "siens", "sinon", "soi", "soit",
68     "son", "sont", "sous", "suivant", "sur", "ta", "te", "tes", "tien", "tienne", "tiennes",
69     "tiens", "toi", "ton", "tous", "tout", "toute", "toutes", "tu", "un", "une", "va", "vers",
70     "voici", "voilà", "vos", "votre", "vous", "vu", "vôtre", "vôtres", "y", "à", "ça", "ès",
71     "été", "être", "ô"
72   };
73
74   /**
75    * Contains the stopwords used with the StopFilter.
76    */

77   private Set JavaDoc stoptable = new HashSet JavaDoc();
78   /**
79    * Contains words that should be indexed but not stemmed.
80    */

81   private Set JavaDoc excltable = new HashSet JavaDoc();
82
83   /**
84    * Builds an analyzer with the default stop words ({@link #FRENCH_STOP_WORDS}).
85    */

86   public FrenchAnalyzer() {
87     stoptable = StopFilter.makeStopSet(FRENCH_STOP_WORDS);
88   }
89
90   /**
91    * Builds an analyzer with the given stop words.
92    */

93   public FrenchAnalyzer(String JavaDoc[] stopwords) {
94     stoptable = StopFilter.makeStopSet(stopwords);
95   }
96
97   /**
98    * Builds an analyzer with the given stop words.
99    *
100    * @deprecated
101    */

102   public FrenchAnalyzer(Hashtable JavaDoc stopwords) {
103     stoptable = new HashSet JavaDoc(stopwords.keySet());
104   }
105
106   /**
107    * Builds an analyzer with the given stop words.
108    * @throws IOException
109    */

110   public FrenchAnalyzer(File JavaDoc stopwords) throws IOException JavaDoc {
111     stoptable = new HashSet JavaDoc(WordlistLoader.getWordSet(stopwords));
112   }
113
114   /**
115    * Builds an exclusionlist from an array of Strings.
116    */

117   public void setStemExclusionTable(String JavaDoc[] exclusionlist) {
118     excltable = StopFilter.makeStopSet(exclusionlist);
119   }
120
121   /**
122    * Builds an exclusionlist from a Hashtable.
123    */

124   public void setStemExclusionTable(Hashtable JavaDoc exclusionlist) {
125     excltable = new HashSet JavaDoc(exclusionlist.keySet());
126   }
127
128   /**
129    * Builds an exclusionlist from the words contained in the given file.
130    * @throws IOException
131    */

132   public void setStemExclusionTable(File JavaDoc exclusionlist) throws IOException JavaDoc {
133     excltable = new HashSet JavaDoc(WordlistLoader.getWordSet(exclusionlist));
134   }
135
136   /**
137    * Creates a TokenStream which tokenizes all the text in the provided Reader.
138    *
139    * @return A TokenStream build from a StandardTokenizer filtered with
140    * StandardFilter, StopFilter, FrenchStemFilter and LowerCaseFilter
141    */

142   public final TokenStream tokenStream(String JavaDoc fieldName, Reader JavaDoc reader) {
143
144     if (fieldName == null) throw new IllegalArgumentException JavaDoc("fieldName must not be null");
145     if (reader == null) throw new IllegalArgumentException JavaDoc("reader must not be null");
146
147     TokenStream result = new StandardTokenizer(reader);
148     result = new StandardFilter(result);
149     result = new StopFilter(result, stoptable);
150     result = new FrenchStemFilter(result, excltable);
151     // Convert to lowercase after stemming!
152
result = new LowerCaseFilter(result);
153     return result;
154   }
155 }
156
157
Popular Tags