~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/contrib/analyzers/common/src/java/org/apache/lucene/analysis/lv/LatvianAnalyzer.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.analysis.lv;
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.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.analysis.WordlistLoader;
36
 
import org.apache.lucene.util.IOUtils;
37
 
import org.apache.lucene.util.Version;
38
 
 
39
 
/**
40
 
 * {@link Analyzer} for Latvian.
41
 
 */
42
 
public final class LatvianAnalyzer extends StopwordAnalyzerBase {
43
 
  private final Set<?> stemExclusionSet;
44
 
  
45
 
  /** File containing default Latvian stopwords. */
46
 
  public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt";
47
 
  
48
 
  /**
49
 
   * Returns an unmodifiable instance of the default stop words set.
50
 
   * @return default stop words set.
51
 
   */
52
 
  public static Set<?> getDefaultStopSet(){
53
 
    return DefaultSetHolder.DEFAULT_STOP_SET;
54
 
  }
55
 
  
56
 
  /**
57
 
   * Atomically loads the DEFAULT_STOP_SET in a lazy fashion once the outer class 
58
 
   * accesses the static final set the first time.;
59
 
   */
60
 
  private static class DefaultSetHolder {
61
 
    static final Set<?> DEFAULT_STOP_SET;
62
 
 
63
 
    static {
64
 
      try {
65
 
        DEFAULT_STOP_SET = WordlistLoader.getWordSet(IOUtils.getDecodingReader(LatvianAnalyzer.class, 
66
 
            DEFAULT_STOPWORD_FILE, IOUtils.CHARSET_UTF_8), Version.LUCENE_CURRENT);
67
 
      } catch (IOException ex) {
68
 
        // default set should always be present as it is part of the
69
 
        // distribution (JAR)
70
 
        throw new RuntimeException("Unable to load default stopword set");
71
 
      }
72
 
    }
73
 
  }
74
 
 
75
 
  /**
76
 
   * Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.
77
 
   */
78
 
  public LatvianAnalyzer(Version matchVersion) {
79
 
    this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);
80
 
  }
81
 
  
82
 
  /**
83
 
   * Builds an analyzer with the given stop words.
84
 
   * 
85
 
   * @param matchVersion lucene compatibility version
86
 
   * @param stopwords a stopword set
87
 
   */
88
 
  public LatvianAnalyzer(Version matchVersion, Set<?> stopwords) {
89
 
    this(matchVersion, stopwords, CharArraySet.EMPTY_SET);
90
 
  }
91
 
 
92
 
  /**
93
 
   * Builds an analyzer with the given stop words. If a non-empty stem exclusion set is
94
 
   * provided this analyzer will add a {@link KeywordMarkerFilter} before
95
 
   * stemming.
96
 
   * 
97
 
   * @param matchVersion lucene compatibility version
98
 
   * @param stopwords a stopword set
99
 
   * @param stemExclusionSet a set of terms not to be stemmed
100
 
   */
101
 
  public LatvianAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {
102
 
    super(matchVersion, stopwords);
103
 
    this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(
104
 
        matchVersion, stemExclusionSet));
105
 
  }
106
 
 
107
 
  /**
108
 
   * Creates a
109
 
   * {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
110
 
   * which tokenizes all the text in the provided {@link Reader}.
111
 
   * 
112
 
   * @return A
113
 
   *         {@link org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents}
114
 
   *         built from an {@link StandardTokenizer} filtered with
115
 
   *         {@link StandardFilter}, {@link LowerCaseFilter}, {@link StopFilter}
116
 
   *         , {@link KeywordMarkerFilter} if a stem exclusion set is
117
 
   *         provided and {@link LatvianStemFilter}.
118
 
   */
119
 
  @Override
120
 
  protected TokenStreamComponents createComponents(String fieldName,
121
 
      Reader reader) {
122
 
    final Tokenizer source = new StandardTokenizer(matchVersion, reader);
123
 
    TokenStream result = new StandardFilter(matchVersion, source);
124
 
    result = new LowerCaseFilter(matchVersion, result);
125
 
    result = new StopFilter(matchVersion, result, stopwords);
126
 
    if(!stemExclusionSet.isEmpty())
127
 
      result = new KeywordMarkerFilter(result, stemExclusionSet);
128
 
    result = new LatvianStemFilter(result);
129
 
    return new TokenStreamComponents(source, result);
130
 
  }
131
 
}