KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > analysis > standard > StandardAnalyzer


1 package org.apache.lucene.analysis.standard;
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 org.apache.lucene.analysis.*;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  * Filters {@link StandardTokenizer} with {@link StandardFilter}, {@link
28  * LowerCaseFilter} and {@link StopFilter}, using a list of English stop words.
29  *
30  * @version $Id: StandardAnalyzer.java 219090 2005-07-14 20:36:28Z dnaber $
31  */

32 public class StandardAnalyzer extends Analyzer {
33   private Set JavaDoc stopSet;
34
35   /** An array containing some common English words that are usually not
36   useful for searching. */

37   public static final String JavaDoc[] STOP_WORDS = StopAnalyzer.ENGLISH_STOP_WORDS;
38
39   /** Builds an analyzer with the default stop words ({@link #STOP_WORDS}). */
40   public StandardAnalyzer() {
41     this(STOP_WORDS);
42   }
43
44   /** Builds an analyzer with the given stop words. */
45   public StandardAnalyzer(Set JavaDoc stopWords) {
46     stopSet = stopWords;
47   }
48
49   /** Builds an analyzer with the given stop words. */
50   public StandardAnalyzer(String JavaDoc[] stopWords) {
51     stopSet = StopFilter.makeStopSet(stopWords);
52   }
53
54   /** Builds an analyzer with the stop words from the given file.
55    * @see WordlistLoader#getWordSet(File)
56    */

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

64   public StandardAnalyzer(Reader JavaDoc stopwords) throws IOException JavaDoc {
65     stopSet = WordlistLoader.getWordSet(stopwords);
66   }
67
68   /** Constructs a {@link StandardTokenizer} filtered by a {@link
69   StandardFilter}, a {@link LowerCaseFilter} and a {@link StopFilter}. */

70   public TokenStream tokenStream(String JavaDoc fieldName, Reader JavaDoc reader) {
71     TokenStream result = new StandardTokenizer(reader);
72     result = new StandardFilter(result);
73     result = new LowerCaseFilter(result);
74     result = new StopFilter(result, stopSet);
75     return result;
76   }
77 }
78
Popular Tags