import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
/*
* Created on 29.12.2004
*
* This is a quick-and-dirty version of a scrabble word-checker.
* It depends on the common OS X infrastructure i.e. the dictionary
* (and is therefore a little bit focused on english ;))
*/
/**
* @author ?=?
*
* The main Class to start . . .
*/
public class WordChecker {
private static final String DICT_FILE_NAME = "/usr/share/dict/words";
private static final int WORD_COUNT = 250000; // derived from "wc -l DICT_FILE_NAME"
public static void main(String[] args) {
// first of all get the words into RAM!
try {
FileReader fr = new FileReader(DICT_FILE_NAME);
BufferedReader br = new BufferedReader(fr);
Dictionary dict = getDictFromReader(br);
System.out.println("Abbruch durch Eingabe einer Leerzeile.");
// Jump into dialog loop . . .
while(anotherRoundRequested(dict) == true) {};
br.close();
fr.close();
} catch (FileNotFoundException e) {
System.out.println("Sorry, but I wasn't able to access the dictionary \"" + DICT_FILE_NAME + "\"");
} catch (IOException e) {
System.out.println("Sorry, but I wasn't able to complete dictionary access for \"" + DICT_FILE_NAME + "\"");
}
} // End of main
/**
* This routine has two purposes: First tell the caller when it's time to finish,
* second handle the dialog with the user to request a character-combination and return
* the found words if any.
* @param dict The special dictionary in which to search
* @return False if the user whishes to end the cycle, else true.
*/
private static boolean anotherRoundRequested(Dictionary dict) {
boolean result = true;
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println();
System.out.print("Zu suchende Buchstaben: ");
try {
String chars = (br.readLine()).trim();
if (chars.equals("")) {
result = false;
} else {
String key = getKeyFor(chars);
Set resultingWords = (Set)dict.get(key);
if (resultingWords != null) {
for (Iterator iter = resultingWords.iterator(); iter.hasNext();) {
String word = (String) iter.next();
System.out.println(word);
}
} else {
System.out.println("Es wurden keine passenden Wörter gefunden.");
}
}
} catch (IOException e) {
result = false;
}
if (result == false) {
System.out.println("Das Programm wird nun beendet.");
}
return result;
} // End of anotherRoundRequested
/**
* Build a special dictionary datastructure from the read input which will enable
* us to quickly reply to search requests.
* @param reader The reader from which the input word list is read.
* @return The build dictionary.
* @throws IOException
*/
private static Dictionary getDictFromReader(BufferedReader reader) throws IOException {
Hashtable table = new Hashtable(WORD_COUNT);
System.out.println("Bitte warten Sie einige Sekunden das Einlesen der Wörterverzeichnisses ab.");
while (reader.ready()) {
String word;
word = reader.readLine();
String key = getKeyFor(word);
SortedSet ss = (SortedSet) table.get(key);
if (ss != null){
ss.add(word);
} else {
ss = new TreeSet();
ss.add(word);
table.put(key, ss);
}
} // while reader is ready
return table;
} // End of getDictFromStream
/**
* Produce a key/hash value for the given string.
* Some special characteristic of the key is that he contains all characters from the value in sorted sequence.
* @param value The value to get hashed.
* @return The hashed key.
*/
private static String getKeyFor(String value) {
String work = value.toLowerCase();
StringBuffer key = new StringBuffer(work.length());
Object ar = Array.newInstance(Character.TYPE, work.length());
char[] car = (char[])ar;
for (int i = 0; i < car.length; i++) {
car[i] = work.charAt(i);
}
Arrays.sort(car);
for (int i = 0; i < car.length; i++) {
key.append(car[i]);
}
return key.toString();
} // End of getKey
} // End of class