1 package org.apache.lucene.analysis.ca;
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 java.io.IOException;
21 import java.io.Reader;
24 import org.apache.lucene.analysis.Analyzer;
25 import org.apache.lucene.analysis.LowerCaseFilter;
26 import org.apache.lucene.analysis.StopFilter;
27 import org.apache.lucene.analysis.KeywordMarkerFilter;
28 import org.apache.lucene.analysis.TokenStream;
29 import org.apache.lucene.analysis.Tokenizer;
30 import org.apache.lucene.analysis.snowball.SnowballFilter;
31 import org.apache.lucene.analysis.standard.StandardFilter;
32 import org.apache.lucene.analysis.standard.StandardTokenizer;
33 import org.apache.lucene.analysis.CharArraySet;
34 import org.apache.lucene.analysis.StopwordAnalyzerBase;
35 import org.apache.lucene.util.Version;
36 import org.tartarus.snowball.ext.CatalanStemmer;
39 * {@link Analyzer} for Catalan.
41 public final class CatalanAnalyzer extends StopwordAnalyzerBase {
42 private final Set<?> stemExclusionSet;
44 /** File containing default Catalan stopwords. */
45 public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
48 * Returns an unmodifiable instance of the default stop words set.
49 * @return default stop words set.
51 public static Set<?> getDefaultStopSet(){
52 return DefaultSetHolder.DEFAULT_STOP_SET;
56 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
57 * accesses the static final set the first time.;
59 private static class DefaultSetHolder {
60 static final Set<?> DEFAULT_STOP_SET;
64 DEFAULT_STOP_SET = loadStopwordSet(false,
65 CatalanAnalyzer.class, DEFAULT_STOPWORD_FILE, "#");
66 } catch (IOException ex) {
67 // default set should always be present as it is part of the
69 throw new RuntimeException("Unable to load default stopword set");
75 * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
77 public CatalanAnalyzer(Version matchVersion) {
78 this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
82 * Builds an analyzer with the given stop words.
84 * @param matchVersion lucene compatibility version
85 * @param stopwords a stopword set
87 public CatalanAnalyzer(Version matchVersion, Set<?> stopwords) {
88 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
92 * Builds an analyzer with the given stop words. If a non-empty stem exclusion set is
93 * provided this analyzer will add a {@link KeywordMarkerFilter} before
96 * @param matchVersion lucene compatibility version
97 * @param stopwords a stopword set
98 * @param stemExclusionSet a set of terms not to be stemmed
100 public CatalanAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
101 super(matchVersion, stopwords);
102 this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
103 matchVersion, stemExclusionSet));
108 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
109 * which tokenizes all the text in the provided {@link Reader}.
112 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
113 * built from an {@link StandardTokenizer} filtered with
114 * {@link StandardFilter}, {@link LowerCaseFilter}, {@link StopFilter}
115 * , {@link KeywordMarkerFilter} if a stem exclusion set is
116 * provided and {@link SnowballFilter}.
119 protected TokenStreamComponents createComponents(String fieldName,
121 final Tokenizer source = new StandardTokenizer(matchVersion, reader);
122 TokenStream result = new StandardFilter(matchVersion, source);
123 result = new LowerCaseFilter(matchVersion, result);
124 result = new StopFilter(matchVersion, result, stopwords);
125 if(!stemExclusionSet.isEmpty())
126 result = new KeywordMarkerFilter(result, stemExclusionSet);
127 result = new SnowballFilter(result, new CatalanStemmer());
128 return new TokenStreamComponents(source, result);