1 package org.apache.lucene.analysis.br;
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.
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.util.Collections;
24 import java.util.HashSet;
28 import org.apache.lucene.analysis.Analyzer;
29 import org.apache.lucene.analysis.CharArraySet;
30 import org.apache.lucene.analysis.LowerCaseFilter;
31 import org.apache.lucene.analysis.KeywordMarkerFilter;
32 import org.apache.lucene.analysis.StopFilter;
33 import org.apache.lucene.analysis.StopwordAnalyzerBase;
34 import org.apache.lucene.analysis.TokenStream;
35 import org.apache.lucene.analysis.Tokenizer;
36 import org.apache.lucene.analysis.WordlistLoader;
37 import org.apache.lucene.analysis.standard.StandardAnalyzer;
38 import org.apache.lucene.analysis.standard.StandardFilter;
39 import org.apache.lucene.analysis.standard.StandardTokenizer;
40 import org.apache.lucene.util.Version;
43 * {@link Analyzer} for Brazilian Portuguese language.
45 * Supports an external list of stopwords (words that
46 * will not be indexed at all) and an external list of exclusions (words that will
47 * not be stemmed, but indexed).
50 * <p><b>NOTE</b>: This class uses the same {@link Version}
51 * dependent settings as {@link StandardAnalyzer}.</p>
53 public final class BrazilianAnalyzer extends StopwordAnalyzerBase {
56 * List of typical Brazilian Portuguese stopwords.
57 * @deprecated use {@link #getDefaultStopSet()} instead
60 public final static String[] BRAZILIAN_STOP_WORDS = {
61 "a","ainda","alem","ambas","ambos","antes",
62 "ao","aonde","aos","apos","aquele","aqueles",
63 "as","assim","com","como","contra","contudo",
64 "cuja","cujas","cujo","cujos","da","das","de",
65 "dela","dele","deles","demais","depois","desde",
66 "desta","deste","dispoe","dispoem","diversa",
67 "diversas","diversos","do","dos","durante","e",
68 "ela","elas","ele","eles","em","entao","entre",
69 "essa","essas","esse","esses","esta","estas",
70 "este","estes","ha","isso","isto","logo","mais",
71 "mas","mediante","menos","mesma","mesmas","mesmo",
72 "mesmos","na","nas","nao","nas","nem","nesse","neste",
73 "nos","o","os","ou","outra","outras","outro","outros",
74 "pelas","pelas","pelo","pelos","perante","pois","por",
75 "porque","portanto","proprio","propios","quais","qual",
76 "qualquer","quando","quanto","que","quem","quer","se",
77 "seja","sem","sendo","seu","seus","sob","sobre","sua",
78 "suas","tal","tambem","teu","teus","toda","todas","todo",
79 "todos","tua","tuas","tudo","um","uma","umas","uns"};
81 /** File containing default Brazilian Portuguese stopwords. */
82 public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
85 * Returns an unmodifiable instance of the default stop-words set.
86 * @return an unmodifiable instance of the default stop-words set.
88 public static Set<?> getDefaultStopSet(){
89 return DefaultSetHolder.DEFAULT_STOP_SET;
92 private static class DefaultSetHolder {
93 static final Set<?> DEFAULT_STOP_SET;
97 DEFAULT_STOP_SET = CharArraySet.unmodifiableSet(new CharArraySet(
98 Version.LUCENE_CURRENT, WordlistLoader.getWordSet(BrazilianAnalyzer.class,
99 DEFAULT_STOPWORD_FILE, "#"), false));
100 } catch (IOException ex) {
101 // default set should always be present as it is part of the
102 // distribution (JAR)
103 throw new RuntimeException("Unable to load default stopword set");
110 * Contains words that should be indexed but not stemmed.
112 // TODO make this private in 3.1
113 private Set<?> excltable = Collections.emptySet();
116 * Builds an analyzer with the default stop words ({@link #getDefaultStopSet()}).
118 public BrazilianAnalyzer(Version matchVersion) {
119 this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
123 * Builds an analyzer with the given stop words
125 * @param matchVersion
126 * lucene compatibility version
130 public BrazilianAnalyzer(Version matchVersion, Set<?> stopwords) {
131 super(matchVersion, stopwords);
135 * Builds an analyzer with the given stop words and stemming exclusion words
137 * @param matchVersion
138 * lucene compatibility version
142 public BrazilianAnalyzer(Version matchVersion, Set<?> stopwords,
143 Set<?> stemExclusionSet) {
144 this(matchVersion, stopwords);
145 excltable = CharArraySet.unmodifiableSet(CharArraySet
146 .copy(matchVersion, stemExclusionSet));
150 * Builds an analyzer with the given stop words.
151 * @deprecated use {@link #BrazilianAnalyzer(Version, Set)} instead
154 public BrazilianAnalyzer(Version matchVersion, String... stopwords) {
155 this(matchVersion, StopFilter.makeStopSet(matchVersion, stopwords));
159 * Builds an analyzer with the given stop words.
160 * @deprecated use {@link #BrazilianAnalyzer(Version, Set)} instead
163 public BrazilianAnalyzer(Version matchVersion, Map<?,?> stopwords) {
164 this(matchVersion, stopwords.keySet());
168 * Builds an analyzer with the given stop words.
169 * @deprecated use {@link #BrazilianAnalyzer(Version, Set)} instead
172 public BrazilianAnalyzer(Version matchVersion, File stopwords)
174 this(matchVersion, WordlistLoader.getWordSet(stopwords));
178 * Builds an exclusionlist from an array of Strings.
179 * @deprecated use {@link #BrazilianAnalyzer(Version, Set, Set)} instead
182 public void setStemExclusionTable( String... exclusionlist ) {
183 excltable = StopFilter.makeStopSet( matchVersion, exclusionlist );
184 setPreviousTokenStream(null); // force a new stemmer to be created
187 * Builds an exclusionlist from a {@link Map}.
188 * @deprecated use {@link #BrazilianAnalyzer(Version, Set, Set)} instead
191 public void setStemExclusionTable( Map<?,?> exclusionlist ) {
192 excltable = new HashSet<Object>(exclusionlist.keySet());
193 setPreviousTokenStream(null); // force a new stemmer to be created
196 * Builds an exclusionlist from the words contained in the given file.
197 * @deprecated use {@link #BrazilianAnalyzer(Version, Set, Set)} instead
200 public void setStemExclusionTable( File exclusionlist ) throws IOException {
201 excltable = WordlistLoader.getWordSet( exclusionlist );
202 setPreviousTokenStream(null); // force a new stemmer to be created
207 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
208 * used to tokenize all the text in the provided {@link Reader}.
210 * @return {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
211 * built from a {@link StandardTokenizer} filtered with
212 * {@link LowerCaseFilter}, {@link StandardFilter}, {@link StopFilter}
213 * , and {@link BrazilianStemFilter}.
216 protected TokenStreamComponents createComponents(String fieldName,
218 Tokenizer source = new StandardTokenizer(matchVersion, reader);
219 TokenStream result = new LowerCaseFilter(matchVersion, source);
220 result = new StandardFilter(matchVersion, result);
221 result = new StopFilter(matchVersion, result, stopwords);
222 if(excltable != null && !excltable.isEmpty())
223 result = new KeywordMarkerFilter(result, excltable);
224 return new TokenStreamComponents(source, new BrazilianStemFilter(result));