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