#27987 - add exceptions to WordlistLoader to missing file causes error instead of silently failing

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150256 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2004-03-29 16:53:35 +00:00
parent 00d94b7319
commit 338499e86b
2 changed files with 26 additions and 23 deletions

View File

@ -62,6 +62,7 @@ import org.apache.lucene.analysis.standard.StandardTokenizer;
import java.io.File; import java.io.File;
import java.io.Reader; import java.io.Reader;
import java.io.IOException;
import java.util.HashSet; import java.util.HashSet;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Set; import java.util.Set;
@ -82,14 +83,14 @@ public class GermanAnalyzer extends Analyzer {
*/ */
private String[] GERMAN_STOP_WORDS = { private String[] GERMAN_STOP_WORDS = {
"einer", "eine", "eines", "einem", "einen", "einer", "eine", "eines", "einem", "einen",
"der", "die", "das", "dass", "daß", "der", "die", "das", "dass", "da<EFBFBD>",
"du", "er", "sie", "es", "du", "er", "sie", "es",
"was", "wer", "wie", "wir", "was", "wer", "wie", "wir",
"und", "oder", "ohne", "mit", "und", "oder", "ohne", "mit",
"am", "im", "in", "aus", "auf", "am", "im", "in", "aus", "auf",
"ist", "sein", "war", "wird", "ist", "sein", "war", "wird",
"ihr", "ihre", "ihres", "ihr", "ihre", "ihres",
"als", "für", "von", "mit", "als", "f<EFBFBD>r", "von", "mit",
"dich", "dir", "mich", "mir", "dich", "dir", "mich", "mir",
"mein", "sein", "kein", "mein", "sein", "kein",
"durch", "wegen", "wird" "durch", "wegen", "wird"
@ -129,7 +130,7 @@ public class GermanAnalyzer extends Analyzer {
/** /**
* Builds an analyzer with the given stop words. * Builds an analyzer with the given stop words.
*/ */
public GermanAnalyzer(File stopwords) { public GermanAnalyzer(File stopwords) throws IOException {
stopSet = new HashSet(WordlistLoader.getWordtable(stopwords).keySet()); stopSet = new HashSet(WordlistLoader.getWordtable(stopwords).keySet());
} }
@ -150,7 +151,7 @@ public class GermanAnalyzer extends Analyzer {
/** /**
* Builds an exclusionlist from the words contained in the given file. * Builds an exclusionlist from the words contained in the given file.
*/ */
public void setStemExclusionTable(File exclusionlist) { public void setStemExclusionTable(File exclusionlist) throws IOException {
exclusionSet = new HashSet(WordlistLoader.getWordtable(exclusionlist).keySet()); exclusionSet = new HashSet(WordlistLoader.getWordtable(exclusionlist).keySet());
} }

View File

@ -62,20 +62,18 @@ import java.util.Hashtable;
/** /**
* Loads a text file and adds every line as an entry to a Hashtable. Every line * Loads a text file and adds every line as an entry to a Hashtable. Every line
* should contain only one word. If the file is not found or on any error, an * should contain only one word.
* empty table is returned.
*
* @author Gerhard Schwarz
* @version $Id$
* *
* @author Gerhard Schwarz
* @version $Id$
* @todo refactor to convert to Sets instead of Hashtable * @todo refactor to convert to Sets instead of Hashtable
*/ */
public class WordlistLoader { public class WordlistLoader {
/** /**
* @param path Path to the wordlist * @param path Path to the wordlist
* @param wordfile Name of the wordlist * @param wordfile Name of the wordlist
*/ */
public static Hashtable getWordtable(String path, String wordfile) { public static Hashtable getWordtable(String path, String wordfile) throws IOException {
if (path == null || wordfile == null) { if (path == null || wordfile == null) {
return new Hashtable(); return new Hashtable();
} }
@ -83,9 +81,9 @@ public class WordlistLoader {
} }
/** /**
* @param wordfile Complete path to the wordlist * @param wordfile Complete path to the wordlist
*/ */
public static Hashtable getWordtable(String wordfile) { public static Hashtable getWordtable(String wordfile) throws IOException {
if (wordfile == null) { if (wordfile == null) {
return new Hashtable(); return new Hashtable();
} }
@ -93,16 +91,19 @@ public class WordlistLoader {
} }
/** /**
* @param wordfile File containing the wordlist * @param wordfile File containing the wordlist
* @todo Create a Set version of this method * @todo Create a Set version of this method
*/ */
public static Hashtable getWordtable(File wordfile) { public static Hashtable getWordtable(File wordfile) throws IOException {
if (wordfile == null) { if (wordfile == null) {
return new Hashtable(); return new Hashtable();
} }
Hashtable result = null; Hashtable result = null;
FileReader freader = null;
LineNumberReader lnr = null;
try { try {
LineNumberReader lnr = new LineNumberReader(new FileReader(wordfile)); freader = new FileReader(wordfile);
lnr = new LineNumberReader(freader);
String word = null; String word = null;
String[] stopwords = new String[100]; String[] stopwords = new String[100];
int wordcount = 0; int wordcount = 0;
@ -116,10 +117,11 @@ public class WordlistLoader {
stopwords[wordcount - 1] = word; stopwords[wordcount - 1] = word;
} }
result = makeWordTable(stopwords, wordcount); result = makeWordTable(stopwords, wordcount);
} } finally {
// On error, use an empty table if (lnr != null)
catch (IOException e) { lnr.close();
result = new Hashtable(); if (freader != null)
freader.close();
} }
return result; return result;
} }
@ -127,8 +129,8 @@ public class WordlistLoader {
/** /**
* Builds the wordlist table. * Builds the wordlist table.
* *
* @param words Word that where read * @param words Word that where read
* @param length Amount of words that where read into <tt>words</tt> * @param length Amount of words that where read into <tt>words</tt>
*/ */
private static Hashtable makeWordTable(String[] words, int length) { private static Hashtable makeWordTable(String[] words, int length) {
Hashtable table = new Hashtable(length); Hashtable table = new Hashtable(length);