1 package org.apache.lucene.analysis.tr;
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.CharArraySet;
26 import org.apache.lucene.analysis.KeywordMarkerFilter;
27 import org.apache.lucene.analysis.StopFilter;
28 import org.apache.lucene.analysis.StopwordAnalyzerBase;
29 import org.apache.lucene.analysis.TokenStream;
30 import org.apache.lucene.analysis.Tokenizer;
31 import org.apache.lucene.analysis.snowball.SnowballFilter;
32 import org.apache.lucene.analysis.standard.StandardFilter;
33 import org.apache.lucene.analysis.standard.StandardTokenizer;
34 import org.apache.lucene.util.Version;
35 import org.tartarus.snowball.ext.TurkishStemmer;
38 * {@link Analyzer} for Turkish.
40 public final class TurkishAnalyzer extends StopwordAnalyzerBase {
41 private final Set<?> stemExclusionSet;
43 /** File containing default Turkish stopwords. */
44 public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
46 * The comment character in the stopwords file.
47 * All lines prefixed with this will be ignored.
49 private static final String STOPWORDS_COMMENT = "#";
52 * Returns an unmodifiable instance of the default stop words set.
53 * @return default stop words set.
55 public static Set<?> getDefaultStopSet(){
56 return DefaultSetHolder.DEFAULT_STOP_SET;
60 * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class
61 * accesses the static final set the first time.;
63 private static class DefaultSetHolder {
64 static final Set<?> DEFAULT_STOP_SET;
68 DEFAULT_STOP_SET = loadStopwordSet(false, TurkishAnalyzer.class,
69 DEFAULT_STOPWORD_FILE, STOPWORDS_COMMENT);
70 } catch (IOException ex) {
71 // default set should always be present as it is part of the
73 throw new RuntimeException("Unable to load default stopword set");
79 * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
81 public TurkishAnalyzer(Version matchVersion) {
82 this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
86 * Builds an analyzer with the given stop words.
88 * @param matchVersion lucene compatibility version
89 * @param stopwords a stopword set
91 public TurkishAnalyzer(Version matchVersion, Set<?> stopwords) {
92 this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
96 * Builds an analyzer with the given stop words. If a non-empty stem exclusion set is
97 * provided this analyzer will add a {@link KeywordMarkerFilter} before
100 * @param matchVersion lucene compatibility version
101 * @param stopwords a stopword set
102 * @param stemExclusionSet a set of terms not to be stemmed
104 public TurkishAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
105 super(matchVersion, stopwords);
106 this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
107 matchVersion, stemExclusionSet));
112 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
113 * which tokenizes all the text in the provided {@link Reader}.
116 * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
117 * built from an {@link StandardTokenizer} filtered with
118 * {@link StandardFilter}, {@link TurkishLowerCaseFilter},
119 * {@link StopFilter}, {@link KeywordMarkerFilter} if a stem
120 * exclusion set is provided and {@link SnowballFilter}.
123 protected TokenStreamComponents createComponents(String fieldName,
125 final Tokenizer source = new StandardTokenizer(matchVersion, reader);
126 TokenStream result = new StandardFilter(matchVersion, source);
127 result = new TurkishLowerCaseFilter(result);
128 result = new StopFilter(matchVersion, result, stopwords);
129 if(!stemExclusionSet.isEmpty())
130 result = new KeywordMarkerFilter(result, stemExclusionSet);
131 result = new SnowballFilter(result, new TurkishStemmer());
132 return new TokenStreamComponents(source, result);