KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > analysis > de > WordlistLoader


1 package org.apache.lucene.analysis.de;
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.FileReader JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.LineNumberReader JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 /**
28  * Loader for text files that represent a list of stopwords.
29  *
30  * @deprecated Use {@link org.apache.lucene.analysis.WordlistLoader} instead
31  *
32  * @author Gerhard Schwarz
33  * @version $Id: WordlistLoader.java 150527 2004-09-20 19:27:01Z dnaber $
34  */

35 public class WordlistLoader {
36
37   /**
38    * Loads a text file and adds every line as an entry to a HashSet (omitting
39    * leading and trailing whitespace). Every line of the file should contain only
40    * one word. The words need to be in lowercase if you make use of an
41    * Analyzer which uses LowerCaseFilter (like GermanAnalyzer).
42    *
43    * @param wordfile File containing the wordlist
44    * @return A HashSet with the file's words
45    */

46   public static HashSet JavaDoc getWordSet(File JavaDoc wordfile) throws IOException JavaDoc {
47     HashSet JavaDoc result = new HashSet JavaDoc();
48     FileReader JavaDoc freader = null;
49     LineNumberReader JavaDoc lnr = null;
50     try {
51       freader = new FileReader JavaDoc(wordfile);
52       lnr = new LineNumberReader JavaDoc(freader);
53       String JavaDoc word = null;
54       while ((word = lnr.readLine()) != null) {
55         result.add(word.trim());
56       }
57     }
58     finally {
59       if (lnr != null)
60         lnr.close();
61       if (freader != null)
62         freader.close();
63     }
64     return result;
65   }
66
67   /**
68    * @param path Path to the wordlist
69    * @param wordfile Name of the wordlist
70    *
71    * @deprecated Use {@link #getWordSet(File)} getWordSet(File)} instead
72    */

73   public static Hashtable JavaDoc getWordtable(String JavaDoc path, String JavaDoc wordfile) throws IOException JavaDoc {
74     return getWordtable(new File JavaDoc(path, wordfile));
75   }
76
77   /**
78    * @param wordfile Complete path to the wordlist
79    *
80    * @deprecated Use {@link #getWordSet(File)} getWordSet(File)} instead
81    */

82   public static Hashtable JavaDoc getWordtable(String JavaDoc wordfile) throws IOException JavaDoc {
83     return getWordtable(new File JavaDoc(wordfile));
84   }
85
86   /**
87    * @param wordfile File object that points to the wordlist
88    *
89    * @deprecated Use {@link #getWordSet(File)} getWordSet(File)} instead
90    */

91   public static Hashtable JavaDoc getWordtable(File JavaDoc wordfile) throws IOException JavaDoc {
92     HashSet JavaDoc wordSet = (HashSet JavaDoc)getWordSet(wordfile);
93     Hashtable JavaDoc result = makeWordTable(wordSet);
94     return result;
95   }
96
97   /**
98    * Builds a wordlist table, using words as both keys and values
99    * for backward compatibility.
100    *
101    * @param wordSet stopword set
102    */

103   private static Hashtable JavaDoc makeWordTable(HashSet JavaDoc wordSet) {
104     Hashtable JavaDoc table = new Hashtable JavaDoc();
105     for (Iterator JavaDoc iter = wordSet.iterator(); iter.hasNext();) {
106       String JavaDoc word = (String JavaDoc)iter.next();
107       table.put(word, word);
108     }
109     return table;
110   }
111 }
112
Popular Tags