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

2. Spelling Checker Implement a spelling checker by using a hash table. Create a

ID: 3832622 • Letter: 2

Question

2. Spelling Checker 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

Explanation / Answer

hi,

Please see the class below:

Thanks.

Spellchecker.java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;


public class Spellchecker {
   private Hashtable<String,String> dictionary; // To store all the words of the dictionary


   public Spellchecker()
   {
       dictionary = new Hashtable<String,String>();

       try
       {  
           //Reading from the file

           BufferedReader dictReader = new BufferedReader(new FileReader("dictionary.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();
       }
       catch (IOException e)
       {
           System.out.println("IOException Occured! ");
           e.printStackTrace();
       }

   }


   public String suggest(String word){
       //1. Check by add one character to the beginning
       String newWord= "a"+word;
       if(dictionary.contains(newWord)){
           return newWord;
       }
       else {
           //2. Check by add one character to the end
           newWord= word+"a";
           if(dictionary.contains(newWord)){
               return newWord;
           }
           else {
               //3. Check by removing one character from the beginning
               newWord= word.substring(1);
               if(dictionary.contains(newWord)){
                   return newWord;
               }
               else{
                   //4.Check by removing one character from the end
                   newWord= word.replace(word.charAt(word.length()-1), ' ');
                   newWord.trim();
                   if(dictionary.contains(newWord)){
                       return newWord;
                   }
                   else{
                       //5.Exchange adjacent characters
                       char tmp =word.charAt(0);
                       newWord= word.replace(word.charAt(0),word.charAt(1));
                       newWord= word.replace(word.charAt(1),word.charAt(1));
                       if(dictionary.contains(newWord)){
                           return newWord;
                       }
                       else{
                           newWord ="";
                       }
                   }
               }
           }
       }
       return newWord;
   }
   public Hashtable<String, String> getDictionary() {
       return dictionary;
   }


   public void setDictionary(Hashtable<String, String> dictionary) {
       this.dictionary = dictionary;
   }
}

SpellCheckerDemo.java

import java.util.Scanner;


public class SpellCheckerDemo {
   public static void main(String [] args)
   {
       //Createing SpellChece object
   Spellchecker checker = new Spellchecker();
  
   //Reading the word from user
   Scanner scan = new Scanner(System.in);
   System.out.println("Please enter the word : ");
   String word = scan.nextLine();
   if(checker.getDictionary().contains(word)){
       System.out.println("No Mistakes found!");
   }
   else{
       System.out.println("Suggestion is: "+checker.suggest(word));
   }
  
          
   }
  
}

dictionary.txt

apple
ball
cat
dog
elephent
fan
girl
house
iron
jacket
kite
lemon
mouse
new
obstacle
park
queue
rabbit
sun
tiger
umbrella
victory
wine

Sample output1:

Please enter the word :
cat
No Mistakes found!

Sample output 2:

Please enter the word :
hgirl
Suggestion is: girl

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote