pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / tr / TurkishAnalyzer.java
1 package org.apache.lucene.analysis.tr;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.IOException;
21 import java.io.Reader;
22 import java.util.Set;
23
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;
36
37 /**
38  * {@link Analyzer} for Turkish.
39  */
40 public final class TurkishAnalyzer extends StopwordAnalyzerBase {
41   private final Set<?> stemExclusionSet;
42   
43   /** File containing default Turkish stopwords. */
44   public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
45   /**
46    * The comment character in the stopwords file.  
47    * All lines prefixed with this will be ignored.
48    */
49   private static final String STOPWORDS_COMMENT = "#";
50   
51   /**
52    * Returns an unmodifiable instance of the default stop words set.
53    * @return default stop words set.
54    */
55   public static Set<?> getDefaultStopSet(){
56     return DefaultSetHolder.DEFAULT_STOP_SET;
57   }
58   
59   /**
60    * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class 
61    * accesses the static final set the first time.;
62    */
63   private static class DefaultSetHolder {
64     static final Set<?> DEFAULT_STOP_SET;
65
66     static {
67       try {
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
72         // distribution (JAR)
73         throw new RuntimeException("Unable to load default stopword set");
74       }
75     }
76   }
77
78   /**
79    * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
80    */
81   public TurkishAnalyzer(Version matchVersion) {
82     this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
83   }
84   
85   /**
86    * Builds an analyzer with the given stop words.
87    * 
88    * @param matchVersion lucene compatibility version
89    * @param stopwords a stopword set
90    */
91   public TurkishAnalyzer(Version matchVersion, Set<?> stopwords) {
92     this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
93   }
94
95   /**
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
98    * stemming.
99    * 
100    * @param matchVersion lucene compatibility version
101    * @param stopwords a stopword set
102    * @param stemExclusionSet a set of terms not to be stemmed
103    */
104   public TurkishAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
105     super(matchVersion, stopwords);
106     this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
107         matchVersion, stemExclusionSet));
108   }
109
110   /**
111    * Creates a
112    * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
113    * which tokenizes all the text in the provided {@link Reader}.
114    * 
115    * @return A
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}.
121    */
122   @Override
123   protected TokenStreamComponents createComponents(String fieldName,
124       Reader reader) {
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);
133   }
134 }