Load our word lists from the classpath.

git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@303 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Michael Bayne
2007-05-24 00:26:29 +00:00
parent bf47702336
commit 211e161604
@@ -29,8 +29,9 @@ import com.threerings.crowd.server.CrowdServer;
import com.threerings.presents.client.InvocationService; import com.threerings.presents.client.InvocationService;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.IOException;
import java.io.File; import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.HashMap; import java.util.HashMap;
@@ -56,10 +57,13 @@ public class DictionaryManager
{ {
/** /**
* Creates the singleton instance of the dictionary service. * Creates the singleton instance of the dictionary service.
*
* @param prefix used to locate dictionary files in the classpath. A dictionary for "en_US" for
* example, would be searched for as "prefix/en_US.wordlist".
*/ */
public static void init (File dictionaryRoot) public static void init (String prefix)
{ {
_singleton = new DictionaryManager(dictionaryRoot); _singleton = new DictionaryManager(prefix);
} }
/** /**
@@ -131,9 +135,9 @@ public class DictionaryManager
/** /**
* Protected constructor. * Protected constructor.
*/ */
protected DictionaryManager (File dictionaryRoot) protected DictionaryManager (String prefix)
{ {
_dictionaryRoot = dictionaryRoot; _prefix = prefix;
} }
/** /**
@@ -144,16 +148,14 @@ public class DictionaryManager
{ {
locale = locale.toLowerCase(); locale = locale.toLowerCase();
if (!_dictionaries.containsKey(locale)) { if (!_dictionaries.containsKey(locale)) {
String path = _prefix + "/" + locale + ".wordlist";
try { try {
// Make a file name InputStream in = getClass().getClassLoader().getResourceAsStream(path);
String filename = locale + ".wordlist"; _dictionaries.put(locale, new Dictionary(locale, in));
File file = new File(_dictionaryRoot, filename);
_dictionaries.put(locale, new Dictionary(file));
} catch (Exception e) { } catch (Exception e) {
log.log(Level.WARNING, "Failed to load language file", e); log.log(Level.WARNING, "Failed to load dictionary [path=" + path + "].", e);
} }
} }
return _dictionaries.get(locale); return _dictionaries.get(locale);
} }
@@ -167,45 +169,33 @@ public class DictionaryManager
* Constructor, loads up the word list and initializes storage. This naive version assumes * Constructor, loads up the word list and initializes storage. This naive version assumes
* language files are simple list of words, with one word per line. * language files are simple list of words, with one word per line.
*/ */
public Dictionary (File wordfile) public Dictionary (String locale, InputStream words)
throws IOException
{ {
try {
CountHashMap <Character> letters = new CountHashMap <Character>(); CountHashMap <Character> letters = new CountHashMap <Character>();
if (wordfile.exists() && wordfile.isFile() && wordfile.canRead()) { if (words != null) {
// Read it line by line BufferedReader reader = new BufferedReader(new InputStreamReader(words));
BufferedReader reader = new BufferedReader(new FileReader(wordfile));
String line = null; String line = null;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
String word = line.toLowerCase(); String word = line.toLowerCase();
// Add the word to the dictionary // add the word to the dictionary
_words.add(word); _words.add(word);
// then count characters
// count characters
for (int ii = word.length() - 1; ii >= 0; ii--) { for (int ii = word.length() - 1; ii >= 0; ii--) {
char ch = word.charAt(ii); char ch = word.charAt(ii);
letters.incrementCount(ch, 1); letters.incrementCount(ch, 1);
} }
} }
} else {
log.warning("Missing dictionary file [locale=" + locale + "].");
}
initializeLetterCounts(letters); initializeLetterCounts(letters);
log.log(Level.INFO, log.fine("Loaded dictionary [locale=" + locale + ", words=" + _words.size() +
"Loaded dictionary file " + wordfile.getName() + ", letters=" + letters + "].");
" with " + _words.size() + " entries, " +
_letters.length + " letters.");
}
else {
log.log(Level.WARNING,
"Could not access dictionary file " + wordfile.getAbsolutePath());
}
} catch (Exception ex) {
log.log(Level.WARNING, "Failed to load dictionary file", ex);
_words.clear(); // dump everything
_letters = new char[] { };
_counts = new float[] { };
}
} }
/** Checks if the specified word exists in the word list */ /** Checks if the specified word exists in the word list */
@@ -262,12 +252,12 @@ public class DictionaryManager
protected float[] _counts = new float[] { }; protected float[] _counts = new float[] { };
} }
/** Root directory where we find dictionary files */ /** Used to locate dictionaries in the classpath. */
protected File _dictionaryRoot; protected String _prefix;
/** Map from locale name to Dictionary object */ /** Map from locale name to Dictionary object. */
protected HashMap <String, Dictionary> _dictionaries = new HashMap <String, Dictionary>(); protected HashMap <String, Dictionary> _dictionaries = new HashMap <String, Dictionary>();
/** Singleton instance pointer */ /** Singleton instance pointer. */
protected static DictionaryManager _singleton; protected static DictionaryManager _singleton;
} }