Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Data structure and algorithm. Implement a spelling checker by using a hash table

ID: 3838833 • Letter: D

Question

Data structure and algorithm. Implement a spelling checker by using a hash table. Create a dictionary of correctly spelled words. You can read the words from a file. Then write a driver program that prompts you to type a word and checks for misspelled words. If the word is spelled correctly it should output "no mistakes found". For misspelled words it should list any words in the dictionary that are obtainable by applying any of the following rules: a. Add one character to the beginning b. Add one character to the end c. Remove one character to the beginning d. Remove one character fro m end E. exchange Adjacent characters

Explanation / Answer


import java.io.*;
import java.util.Hashtable;
import java.util.StringTokenizer;

public class Test {

Hashtable hashtable;

public static void main(String argv[]) {
   Test checker = new Test();
}

public Test() {
   hashtable = new Hashtable(45);

try {
String s, tokens;

// Read dictionary from file
BufferedReader bf = new BufferedReader(new FileReader("hashtable.dat"));

while ((s = bf.readLine())!= null) {
StringTokenizer st = new StringTokenizer(s);

while (st.hasMoreTokens()) {
tokens = st.nextToken();
// key and value are identical
hashtable.put(tokens, tokens);
}
}
bf.close();
BufferedReader misspell = new BufferedReader(new FileReader("hashtable.dat"));
String wrongSpell, rightSpell;

while ((s = misspell.readLine()) != null) {
StringTokenizer st = new StringTokenizer(s);
wrongSpell = st.nextToken();
rightSpell = st.nextToken();
hashtable.put(wrongSpell, rightSpell);

}
misspell.close();

BufferedReader inputFiles = new BufferedReader(new FileReader("testfile.dat"));
BufferedWriter outputFiles = new BufferedWriter(new FileWriter("checked.dat"));

while ((s = inputFiles.readLine()) != null) {
StringTokenizer st = new StringTokenizer(s);

while (st.hasMoreTokens()) {
String inputWord = st.nextToken();
String outputWord = spellCheckWord(inputWord);
outputFiles.write(outputWord+" ");
}
outputFiles.newLine();
}

inputFiles.close();
outputFiles.close();
}
catch (IOException e) {
System.out.println("Error -- " + e.toString());
e.printStackTrace();
System.exit(-1);
}
}

public String spellCheckWord(String wordToCheck) {
String lookups, uninflectWord;
String word = wordToCheck.toLowerCase();
if ((lookups = (String)hashtable.get(word)) != null)
return lookups;

// Remove inflections at end of word and try again ("es", "s", "ing", "ed")
int length = word.length();

if (length > 1 && word.substring(length - 1).equals("s")) {
uninflectWord = word.substring(0, length-1);

if ((lookups = (String)hashtable.get(uninflectWord)) != null)
return lookups + "s";

}

if (length > 2 && word.substring(length-2).equals("es")) {
uninflectWord = word.substring(0, length-2);

if ((lookups = (String)hashtable.get(uninflectWord)) != null)
return lookups + "es";
else
return word.toUpperCase();
}

if (length > 3 && word.substring(length - 3).equals("ing")) {
uninflectWord = word.substring(0, length-3);

if ((lookups = (String)hashtable.get(uninflectWord)) != null)
return lookups + "ing";
else
return word.toUpperCase();
}

if (length > 2 && word.substring(length - 2).equals("ed")) {
uninflectWord = word.substring(0, length-2);

if ((lookups = (String)hashtable.get(uninflectWord)) != null)
return lookups + "ed";
else
return word.toUpperCase();
}

return word.toUpperCase();
}

}

Hint:-In the above program you will get

Error -- java.io.FileNotFoundException: hashtable.dat (The system cannot find the file specified).Beacause of u will give specific file path location then you will get out properlly.