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

« back to all changes in this revision

Viewing changes to lucene/src/java/org/apache/lucene/analysis/StopwordAnalyzerBase.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
 
/**
2
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 
 * contributor license agreements.  See the NOTICE file distributed with
4
 
 * this work for additional information regarding copyright ownership.
5
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 
 * (the "License"); you may not use this file except in compliance with
7
 
 * the License.  You may obtain a copy of the License at
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
11
 
 * Unless required by applicable law or agreed to in writing, software
12
 
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
 * See the License for the specific language governing permissions and
15
 
 * limitations under the License.
16
 
 */
17
 
 
18
 
package org.apache.lucene.analysis;
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.CharArraySet;
26
 
import org.apache.lucene.analysis.ReusableAnalyzerBase;
27
 
import org.apache.lucene.analysis.WordlistLoader;
28
 
import org.apache.lucene.util.IOUtils;
29
 
import org.apache.lucene.util.Version;
30
 
 
31
 
/**
32
 
 * Base class for Analyzers that need to make use of stopword sets. 
33
 
 * 
34
 
 */
35
 
public abstract class StopwordAnalyzerBase extends ReusableAnalyzerBase {
36
 
 
37
 
  /**
38
 
   * An immutable stopword set
39
 
   */
40
 
  protected final CharArraySet stopwords;
41
 
 
42
 
  protected final Version matchVersion;
43
 
 
44
 
  /**
45
 
   * Returns the analyzer's stopword set or an empty set if the analyzer has no
46
 
   * stopwords
47
 
   * 
48
 
   * @return the analyzer's stopword set or an empty set if the analyzer has no
49
 
   *         stopwords
50
 
   */
51
 
  public Set<?> getStopwordSet() {
52
 
    return stopwords;
53
 
  }
54
 
 
55
 
  /**
56
 
   * Creates a new instance initialized with the given stopword set
57
 
   * 
58
 
   * @param version
59
 
   *          the Lucene version for cross version compatibility
60
 
   * @param stopwords
61
 
   *          the analyzer's stopword set
62
 
   */
63
 
  protected StopwordAnalyzerBase(final Version version, final Set<?> stopwords) {
64
 
    matchVersion = version;
65
 
    // analyzers should use char array set for stopwords!
66
 
    this.stopwords = stopwords == null ? CharArraySet.EMPTY_SET : CharArraySet
67
 
        .unmodifiableSet(CharArraySet.copy(version, stopwords));
68
 
  }
69
 
 
70
 
  /**
71
 
   * Creates a new Analyzer with an empty stopword set
72
 
   * 
73
 
   * @param version
74
 
   *          the Lucene version for cross version compatibility
75
 
   */
76
 
  protected StopwordAnalyzerBase(final Version version) {
77
 
    this(version, null);
78
 
  }
79
 
 
80
 
  /**
81
 
   * Creates a CharArraySet from a file resource associated with a class. (See
82
 
   * {@link Class#getResourceAsStream(String)}).
83
 
   * 
84
 
   * @param ignoreCase
85
 
   *          <code>true</code> if the set should ignore the case of the
86
 
   *          stopwords, otherwise <code>false</code>
87
 
   * @param aClass
88
 
   *          a class that is associated with the given stopwordResource
89
 
   * @param resource
90
 
   *          name of the resource file associated with the given class
91
 
   * @param comment
92
 
   *          comment string to ignore in the stopword file
93
 
   * @return a CharArraySet containing the distinct stopwords from the given
94
 
   *         file
95
 
   * @throws IOException
96
 
   *           if loading the stopwords throws an {@link IOException}
97
 
   */
98
 
  protected static CharArraySet loadStopwordSet(final boolean ignoreCase,
99
 
      final Class<? extends ReusableAnalyzerBase> aClass, final String resource,
100
 
      final String comment) throws IOException {
101
 
    Reader reader = null;
102
 
    try {
103
 
      reader = IOUtils.getDecodingReader(aClass.getResourceAsStream(resource), IOUtils.CHARSET_UTF_8);
104
 
      return WordlistLoader.getWordSet(reader, comment, new CharArraySet(Version.LUCENE_31, 16, ignoreCase));
105
 
    } finally {
106
 
      IOUtils.close(reader);
107
 
    }
108
 
    
109
 
  }
110
 
  
111
 
  /**
112
 
   * Creates a CharArraySet from a file.
113
 
   * 
114
 
   * @param stopwords
115
 
   *          the stopwords file to load
116
 
   * 
117
 
   * @param matchVersion
118
 
   *          the Lucene version for cross version compatibility
119
 
   * @return a CharArraySet containing the distinct stopwords from the given
120
 
   *         file
121
 
   * @throws IOException
122
 
   *           if loading the stopwords throws an {@link IOException}
123
 
   */
124
 
  protected static CharArraySet loadStopwordSet(File stopwords,
125
 
      Version matchVersion) throws IOException {
126
 
    Reader reader = null;
127
 
    try {
128
 
      reader = IOUtils.getDecodingReader(stopwords, IOUtils.CHARSET_UTF_8);
129
 
      return WordlistLoader.getWordSet(reader, matchVersion);
130
 
    } finally {
131
 
      IOUtils.close(reader);
132
 
    }
133
 
  }
134
 
  
135
 
  /**
136
 
   * Creates a CharArraySet from a file.
137
 
   * 
138
 
   * @param stopwords
139
 
   *          the stopwords reader to load
140
 
   * 
141
 
   * @param matchVersion
142
 
   *          the Lucene version for cross version compatibility
143
 
   * @return a CharArraySet containing the distinct stopwords from the given
144
 
   *         reader
145
 
   * @throws IOException
146
 
   *           if loading the stopwords throws an {@link IOException}
147
 
   */
148
 
  protected static CharArraySet loadStopwordSet(Reader stopwords,
149
 
      Version matchVersion) throws IOException {
150
 
    try {
151
 
      return WordlistLoader.getWordSet(stopwords, matchVersion);
152
 
    } finally {
153
 
      IOUtils.close(stopwords);
154
 
    }
155
 
  }
156
 
}