pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / bg / BulgarianAnalyzer.java
1 package org.apache.lucene.analysis.bg;
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.File;
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.util.Set;
24
25 import org.apache.lucene.analysis.Analyzer;
26 import org.apache.lucene.analysis.LowerCaseFilter;
27 import org.apache.lucene.analysis.CharArraySet;
28 import org.apache.lucene.analysis.KeywordMarkerFilter;
29 import org.apache.lucene.analysis.StopFilter;
30 import org.apache.lucene.analysis.StopwordAnalyzerBase;
31 import org.apache.lucene.analysis.TokenStream;
32 import org.apache.lucene.analysis.Tokenizer;
33 import org.apache.lucene.analysis.WordlistLoader;
34 import org.apache.lucene.analysis.standard.StandardFilter;
35 import org.apache.lucene.analysis.standard.StandardTokenizer;
36 import org.apache.lucene.util.Version;
37
38 /**
39  * {@link Analyzer} for Bulgarian.
40  * <p>
41  * This analyzer implements light-stemming as specified by: <i> Searching
42  * Strategies for the Bulgarian Language </i>
43  * http://members.unine.ch/jacques.savoy/Papers/BUIR.pdf
44  * <p>
45  */
46 public final class BulgarianAnalyzer extends StopwordAnalyzerBase {
47   
48   /**
49    * File containing default Bulgarian stopwords.
50    * 
51    * Default stopword list is from
52    * http://members.unine.ch/jacques.savoy/clef/index.html The stopword list is
53    * BSD-Licensed.
54    */
55   public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
56   
57   /**
58    * The comment character in the stopwords file. All lines prefixed with this
59    * will be ignored
60    * @deprecated use {@link WordlistLoader#getWordSet(Reader, String, Version)} directly
61    */
62   //TODO make this private
63   @Deprecated
64   public static final String STOPWORDS_COMMENT = "#";
65   
66   /**
67    * Returns an unmodifiable instance of the default stop-words set.
68    * 
69    * @return an unmodifiable instance of the default stop-words set.
70    */
71   public static Set<?> getDefaultStopSet() {
72     return DefaultSetHolder.DEFAULT_STOP_SET;
73   }
74   
75   /**
76    * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer
77    * class accesses the static final set the first time.;
78    */
79   private static class DefaultSetHolder {
80     static final Set<?> DEFAULT_STOP_SET;
81     
82     static {
83       try {
84         DEFAULT_STOP_SET = loadStopwordSet(false, BulgarianAnalyzer.class, DEFAULT_STOPWORD_FILE, STOPWORDS_COMMENT);
85       } catch (IOException ex) {
86         // default set should always be present as it is part of the
87         // distribution (JAR)
88         throw new RuntimeException("Unable to load default stopword set");
89       }
90     }
91   }
92   
93   private final Set<?> stemExclusionSet;
94    
95   /**
96    * Builds an analyzer with the default stop words:
97    * {@link #DEFAULT_STOPWORD_FILE}.
98    */
99   public BulgarianAnalyzer(Version matchVersion) {
100     this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
101   }
102   
103   /**
104    * Builds an analyzer with the given stop words.
105    */
106   public BulgarianAnalyzer(Version matchVersion, Set<?> stopwords) {
107     this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
108   }
109   
110   /**
111    * Builds an analyzer with the given stop words and a stem exclusion set.
112    * If a stem exclusion set is provided this analyzer will add a {@link KeywordMarkerFilter} 
113    * before {@link BulgarianStemFilter}.
114    */
115   public BulgarianAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
116     super(matchVersion, stopwords);
117     this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
118         matchVersion, stemExclusionSet));  }
119
120   /**
121    * Creates a
122    * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
123    * which tokenizes all the text in the provided {@link Reader}.
124    * 
125    * @return A
126    *         {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
127    *         built from an {@link StandardTokenizer} filtered with
128    *         {@link StandardFilter}, {@link LowerCaseFilter}, {@link StopFilter}
129    *         , {@link KeywordMarkerFilter} if a stem exclusion set is
130    *         provided and {@link BulgarianStemFilter}.
131    */
132   @Override
133   public TokenStreamComponents createComponents(String fieldName, Reader reader) {
134     final Tokenizer source = new StandardTokenizer(matchVersion, reader);
135     TokenStream result = new StandardFilter(matchVersion, source);
136     result = new LowerCaseFilter(matchVersion, result);
137     result = new StopFilter(matchVersion, result, stopwords);
138     if(!stemExclusionSet.isEmpty())
139       result = new KeywordMarkerFilter(result, stemExclusionSet);
140     result = new BulgarianStemFilter(result);
141     return new TokenStreamComponents(source, result);
142   }
143 }