Implement a spelling checker in Java by using a hash table. Your program should
ID: 3775490 • Letter: I
Question
Implement a spelling checker in Java by using a hash table. Your program should take two command line arguments: the name of a dictionary file and the name of the file that contains the text you wish to spellcheck. Output all misspelled words and the line numbers in which they occur. Numbers and contractions are considered valid words. Only strip leading and trailing punctuation. Also, for each misspelled word, list any words in the dictionary that are obtainable by applying any of the following rules: a. Add one character. b. Remove one character. c. Exchange adjacent characters.
Explanation / Answer
The following program will help you to solve your most of the task Please check :-
spellchecker.java
import java.io.*;
import java.util.*;
public class spellchecker {
Hashtable<String,String> dictionary; // Store words of the dictionary
boolean suggestWord ; // Indicate word spelled correctly or not.
public static void main(String [] args)
{
spellchecker checker = new spellchecker();
}
public spellchecker()
{
dictionary = new Hashtable<String,String>();
System.out.println("Spell checker check every line from input file and then give suggestions ");
try
{
//Read and store the words of the dictionary
BufferedReader dictReader = new BufferedReader(new FileReader("dictionaryFile.txt"));
while (dictReader.ready())
{
String dictInput = dictReader.readLine() ;
String [] dict = dictInput.split("\s");
for(int i = 0; i < dict.length;i++)
{
// key and value are identical
dictionary.put(dict[i], dict[i]);
}
}
dictReader.close();
String file = "inputFile.txt";
// Read and check the input from the text file
BufferedReader inputFile = new BufferedReader(new FileReader(file));
System.out.println("Reading from "+file);
// Initialising a spelling suggest object
spellingsuggest suggest = new spellingsuggest("wordDatabase.txt");
// Reads input lines one by one
while ( inputFile.ready() )
{
String s = inputFile.readLine() ;
System.out.println (s);
String[] result = s.split("\s");
for (int x=0; x<result.length; x++)
{
suggestWord = true;
String outputWord = checkWord(result[x]);
if(suggestWord)
{
System.out.println("Suggestion for "+result[x]+" is: "+suggest.correct(outputWord)+" ");
}
}
}
inputFile.close();
}
catch (IOException e)
{
System.out.println("IOException.");
e.printStackTrace();
// System.exit(-1);
}
}
public String checkWord(String wordToCheck)
{
String wordCheck, unpunctWord;
String word = wordToCheck.toLowerCase();
// if word is found in dictionary then it spelt correctly, so return as it is.
if ((wordCheck = (String)dictionary.get(word)) != null)
{
suggestWord = false;
return wordCheck;
}
// Removing punctuations at end of word and giving it a shot ("." or "." or "?!")
int length = word.length();
//Checking for the beginning of quotes(example: "she )
if (length > 1 && word.substring(0,1).equals("""))
{
unpunctWord = word.substring(1, length);
if ((wordCheck = (String)dictionary.get(unpunctWord)) != null)
{
suggestWord = false;
return wordCheck ;
}
else // not found
return unpunctWord;
// removing the punctuations and returning
}
// Checking if "." or ",",etc.. at the end is the problem(example: book. when book is present in the dictionary).
if( word.substring(length - 1).equals(".") || word.substring(length - 1).equals(",") || word.substring(length - 1).equals("!")
|| word.substring(length - 1).equals(";") || word.substring(length - 1).equals(":"))
{
unpunctWord = word.substring(0, length-1);
if ((wordCheck = (String)dictionary.get(unpunctWord)) != null)
{
suggestWord = false;
return wordCheck ;
}
else
{
return unpunctWord;
// removing the punctuations and returning
}
}
// Checking for "!"",etc ... in the problem (example: watch!" when watch is present in the dictionary)
if (length > 2 && word.substring(length-2).equals(","") || word.substring(length-2).equals("."")
|| word.substring(length-2).equals("?"") || word.substring(length-2).equals("!"") )
{
unpunctWord = word.substring(0, length-2);
if ((wordCheck = (String)dictionary.get(unpunctWord)) != null)
{
suggestWord = false;
return wordCheck ;
}
else // not found
return unpunctWord;
// removing the inflections and returning
}
// All these checks too, word could not be corrected, hence it must be misspelt word. return and ask for suggestions
return word;
}
}
spellsuggest.java
import java.io.*;
import java.util.*;
import java.util.regex.*;
class spellingsuggest {
private final HashMap<String, Integer> DBWords = new HashMap<String, Integer>();
public spellingsuggest(String file) throws IOException
{
try
{
BufferedReader in = new BufferedReader(new FileReader(file));
Pattern p = Pattern.compile("\w+");
for(String temp = ""; temp != null; temp = in.readLine() )
// Reading dictionary and updating the probabalistic values accordingly
{
// of the words according.
Matcher m = p.matcher(temp.toLowerCase());
while(m.find())
{
DBWords.put( (temp = m.group()), DBWords.containsKey(temp) ? DBWords.get(temp) + 1 : 1 );
// This will serve as an indicator to
}
// probability of a word
}
in.close();
}
catch(IOException e)
{
System.out.println("Exception occure Please check");
e.printStackTrace();
}
}
// Return an array containing all possible corrections to the word passed.
private final ArrayList<String> edits(String word)
{
ArrayList<String> result = new ArrayList<String>();
for(int i=0; i < word.length(); ++i)
{
result.add(word.substring(0, i) + word.substring(i+1));
}
for(int i=0; i < word.length()-1; ++i)
{
result.add(word.substring(0, i) + word.substring(i+1, i+2) + word.substring(i, i+1) + word.substring(i+2));
}
for(int i=0; i < word.length(); ++i)
{
for(char c='a'; c <= 'z'; ++c)
{
result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i+1));
}
}
for(int i=0; i <= word.length(); ++i)
{
for(char c='a'; c <= 'z'; ++c)
{
result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i));
}
}
return result;
}
public final String correct(String word)
{
if(DBWords.containsKey(word))
{
return word;
//safe word.
}
ArrayList<String> list_edits = edits(word);
HashMap<Integer, String> candidates = new HashMap<Integer, String>();
for(String s : list_edits)
// Iterating through the list of all possible corrections to the word.
{
if(DBWords.containsKey(s))
{
candidates.put(DBWords.get(s),s);
}
}
// In the first stage of error correction, any of the possible corrections from the list_edits are found in our word database DBWords
// then we return the one verified correction with maximum probability.
if(candidates.size() > 0)
{
return candidates.get(Collections.max(candidates.keySet()));
}
// In the second stage we apply the first stage method on the possible collections of the list_edits.By the second stage statistics
// suggest we obtain an accuracy of about 98% !!
for(String s : list_edits)
{
for(String w : edits(s))
{
if(DBWords.containsKey(w))
{
candidates.put(DBWords.get(w),w);
}
}
}
return candidates.size() > 0 ? candidates.get(Collections.max(candidates.keySet())) : "No correction found";
}
public static void main(String [] args) throws IOException
{
if(args.length > 0)
{
System.out.println((new spellingsuggest("wordDatabase.txt")).correct(args[0]));
}
}
}
If you have any query feel free to reach out.
Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.