import java.util.*; import java.io.*; /** * This program starts by reading a dictionary created by buildDict. It then parses text files that * may contain incorrectly spelled word, identifies these and interacts with the user to get a * correct replacement. * * @author: Sriram Pemmaraju * @date: Nov 18th, 2008 */ public class spellCheck{ /** /* * @param: a BufferedReader in from which the dictionary words are read * @return: a wordList object containing the dictionary */ public static wordList makeDictionary(BufferedReader in) { // Define a wordList object wordList dictionary = new wordList(); String word; try{ // This loop reads all the words from the dictionary file // and inserts each word into the dictionary while( (word = in.readLine()) != null ) dictionary.insert(word); } catch(IOException e){} return dictionary; } /** /* * @param: the dictionary and a word to be searched for in the dictionary * @return: either the given word, as is, or a replacement word provided by * the user */ public static String processWord(wordList dictionary, String word) { // Search for the word in the dictionary and if found, then just // it as is if(dictionary.search(word)) return word; // Otherwise, get a replacement and return that replacement else { System.out.println("Mispelled word: " + word); System.out.println("Type replacement."); // open up standard input BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // Define a replacementWord String replacementWord = ""; // try-catch block for reading a replacement word try { replacementWord = br.readLine(); } catch (IOException e) {} // Read a replacement word return replacementWord; } } public static void main(String args[]) { try { // This program starts by reading the dictionary of words from words.dat BufferedReader in = new BufferedReader(new FileReader("words.dat")); // Making a dictionary to store the words wordList dictionary = makeDictionary(in); // This expects the input that needs to be corrected to come from input.txt in = new BufferedReader(new FileReader("input.txt")); // The corrected output is written onto a file called output.txt PrintStream p = new PrintStream(new FileOutputStream("output.txt")); // These variables will be used in the loop below String line; // a string to store each line of the text String word; // a string to store each word extracted from a line // This loop reads all the lines in the input file and processes each line // separately while( (line = in.readLine()) != null ) { word = ""; // Now I process each character of the line one by one for(int i = 0; i < line.length(); i++) { char ch = line.charAt(i); // if the character being processed is a letter, add it to the // word currently being constructed if(Character.isLetter(ch)) word = word+ch; // If the character being processed is not a letter, then the word // we just constructed is complete and needs to be processed else if(!word.equals("")) { // processes the word by searching for it in the dictionary, // either noting that it is correct or finding a replacement // for it String newWord = processWord(dictionary, word); // Write newWord, which may be identical to the old word // into the output file p.print(newWord); // Write the character following the word p.print(ch); // Reset the word for the next iteration word = ""; } else // This is when a non-letter character has been encountered, // but a word has not been just completed p.print(ch); } // end for-loop // There might have been a word at the very end of line and this needs to // be flushed out before we begin the next line if(!word.equals("")) { // This is almost the same block of code as above String newWord = processWord(dictionary, word); // Write newWord followed by a end-of-line character p.println(newWord); word = ""; } } // end while-loop in.close(); p.close(); } catch (IOException e) {} } }