KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > analysis > StopAnalyzer


1 package org.apache.lucene.analysis;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22 import java.util.Set JavaDoc;
23
24 /** Filters LetterTokenizer with LowerCaseFilter and StopFilter. */
25
26 public final class StopAnalyzer extends Analyzer {
27   private Set JavaDoc stopWords;
28
29   /** An array containing some common English words that are not usually useful
30     for searching. */

31   public static final String JavaDoc[] ENGLISH_STOP_WORDS = {
32     "a", "an", "and", "are", "as", "at", "be", "but", "by",
33     "for", "if", "in", "into", "is", "it",
34     "no", "not", "of", "on", "or", "s", "such",
35     "t", "that", "the", "their", "then", "there", "these",
36     "they", "this", "to", "was", "will", "with"
37   };
38
39   /** Builds an analyzer which removes words in ENGLISH_STOP_WORDS. */
40   public StopAnalyzer() {
41     stopWords = StopFilter.makeStopSet(ENGLISH_STOP_WORDS);
42   }
43
44   /** Builds an analyzer with the stop words from the given set.
45    */

46   public StopAnalyzer(Set JavaDoc stopWords) {
47     this.stopWords = stopWords;
48   }
49
50   /** Builds an analyzer which removes words in the provided array. */
51   public StopAnalyzer(String JavaDoc[] stopWords) {
52     this.stopWords = StopFilter.makeStopSet(stopWords);
53   }
54   
55   /** Builds an analyzer with the stop words from the given file.
56    * @see WordlistLoader#getWordSet(File)
57    */

58   public StopAnalyzer(File JavaDoc stopwordsFile) throws IOException JavaDoc {
59     stopWords = WordlistLoader.getWordSet(stopwordsFile);
60   }
61
62   /** Builds an analyzer with the stop words from the given reader.
63    * @see WordlistLoader#getWordSet(Reader)
64    */

65   public StopAnalyzer(Reader JavaDoc stopwords) throws IOException JavaDoc {
66     stopWords = WordlistLoader.getWordSet(stopwords);
67   }
68
69   /** Filters LowerCaseTokenizer with StopFilter. */
70   public TokenStream tokenStream(String JavaDoc fieldName, Reader JavaDoc reader) {
71     return new StopFilter(new LowerCaseTokenizer(reader), stopWords);
72   }
73 }
74
75
Popular Tags