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

Can you please use the template below to create a simple spell checker program.

ID: 3820264 • Letter: C

Question

Can you please use the template below to create a simple spell checker program. Your program will have the following characteristics:

Your program will be capable of reading multiple files to be spell checked from command line arguments.

When your program starts up, you will read in the dictionary.txt file which contains a list of known words. Only read in this file once. Store these words in a HashSet data structure.

Create a TreeSet to store any miss-spelled words that you find.

For each file in the command line parameter list, print out the name of the file before reading in any words.

For each file parse out each of the words in the file and check to see if the word is in the HashSet you created with the dictionary.txt words or its already in your list of miss-spelled words.

If the word is in the dictionary HashSet or the miss-spelled word TreeSet then proceed to the next word.

If the word is not in the dictionary or the miss-spelled word list, give your user 2 choices: Add the word to the HashSet dictionary, or add the word to the list of miss-spelled words.

At the end of each file processed, dump out the list of miss-spelled words.

When you then start to read in the next file, make sure you clear the list of miss-spelled words. Keep any words added to the dictionary HashSet.

More details:

In parsing the words in these text files, you will need to filter out punctuation characters. I used the StringTokenizer, and I called the following constructor:
StringTokenizer st = new StringTokenizer(line, " ,.;:-%'"");

before checking the word against the dictionary, make sure you lower case the word

if the "word" doesn't start out with a letter (i.e. >= 'a' And <= 'z'), then skip it because it is likely a number

If the word couldn't be found in either list and ends with an 's', try removing the 's' to create a singular version of the word and try again to find the word in your dictionary or miss-spelled word list using the singular version of the word.

Only print out lines that contain a word needing a user response. See the Sample output below from the published answer.

Use the following dictionary file

dictionary.txt

You can find the input files to test from the following links:

Russia.txt

Ukraine.txt

Your command line arguments will be:

Russia.txt     Ukraine.txt

When you execute your program to publish your output, answer "n" to the question of whether to add the word to the dictionary for all words except the for the word "christianity" .

See the sample output below:

Some of the output from running this program should look something like:

First part of the output from running the published answer

For those of you who like to see a template of the published answer, here it is:

Explanation / Answer

//SpellCheck.java

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeSet;


public class SpellCheck {
  
// We could have also used TreeSet for the dictionary
private HashSet<String> dictionary = new HashSet<String>();

private TreeSet<String> miss_spelled_words = new TreeSet<String>();
  
public SpellCheck() throws IOException
{
FileReader fr = new FileReader("D:\dictionary.txt");
BufferedReader br = new BufferedReader(fr);

String line;
while((line=br.readLine())!=null){
   dictionary.add(line);
}

}
public void checkSpelling(String fileName) throws IOException
{
   Scanner ss = new Scanner(System.in);
System.out.println("======== Spell checking " + fileName + " =========");

// Clear miss_spelled_words
miss_spelled_words.clear();

// Read in each line from "fileName"
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
  
String line;
while((line=br.readLine())!=null){
   // For each line, break the line into words using the following StringTokenizer
   StringTokenizer st = new StringTokenizer(line, " ,.;:-%'"");
   while(st.hasMoreTokens()){
       // lower case each word obtained from the StringTokenizer
       String str = st.nextToken().toLowerCase();
       // skip word if the first character is not a letter
       if(str.charAt(0)>='a' && str.charAt(0)<='z'){
           // Skip over word if it can be found in either dictionary, or miss_spelled_words
           if(!dictionary.contains(str) && !miss_spelled_words.contains(str) ){
               // If word ends with 's', then try the singular version of the word in the dictionary and miss_spelled_words ... skip if found
               if(str.endsWith("s")){
                   StringBuffer sb = new StringBuffer(str);
                   sb.deleteCharAt(str.length()-1);
                   if(dictionary.contains(sb.toString())){
                       if(miss_spelled_words.contains(sb.toString())){
                           continue;
                       }else{
                           miss_spelled_words.add(sb.toString());
                       }
                   }else{
                       if(!miss_spelled_words.contains(sb.toString())){
                           // Ask the user if he wants the word added
                   //to the dictionary or the miss-spelled words
                   //and add word as specified by the user
                           System.out.println("Where do you wanna add the word "+sb.toString()+" to (D|M)?");
                   System.out.println("D-Dictionary OR M-Miss-spelledList");
                   String w = ss.next();
                   if(w.equalsIgnoreCase("D")){
                       dictionary.add(str);
                   }else{
                       miss_spelled_words.add(str);
                   }
                       }else{
                           continue;
                       }
                   }
               }else{
               // Ask the user if he wants the word added
                   //to the dictionary or the miss-spelled words
                   //and add word as specified by the user
                   System.out.println("Where do you wanna add the word "+str+" to (D|M)?");
                   System.out.println("D-Dictionary OR M-Miss-spelledList");
                   String w = ss.next();
                   if(w.equalsIgnoreCase("D")){
                       dictionary.add(str);
                   }else{
                       miss_spelled_words.add(str);
                   }
               }
           }else if(dictionary.contains(str) && !miss_spelled_words.contains(str)){
               continue;
           }
       }else{
           continue;
       }
   }
}
  
}
  
public void dump_miss_spelled_words()
{
   System.out.println("****Miss-Spelled Words are:");
for(String ms:miss_spelled_words){
   System.out.println(ms);
}
}
  
  
public static void main(String[] args) throws IOException {
  
try {
SpellCheck spellCheck = new SpellCheck();
  
for (int i=0; i < args.length; i++)
{
spellCheck.checkSpelling(args[i]);
spellCheck.dump_miss_spelled_words();
}
}
catch (FileNotFoundException e)
{
System.out.println(e);
}
  

}

}

//Dictionary.txt:

fine
doing
search
speak
working
night
early
morning
how
come
you
idea
with

//Russia.txt:

hello everyone, how are you doing
what you are in search for and how long you will
do search and what are your desires.

//Ukraine.txt

hi dude, with all the rights you have on the land
you people can speaks with all your rights and transfer them to night to have great transaction.

Output:

javac SpellCheck.java

java SpellCheck D:\Russia.txt D:\Ukraine.txt

======== Spell checking D:\Russia.txt =========
Where do you wanna add the word hello to (D|M)?
D-Dictionary OR M-Miss-spelledList
D
Where do you wanna add the word everyone to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word are to (D|M)?
D-Dictionary OR M-Miss-spelledList
D
Where do you wanna add the word what to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word in to (D|M)?
D-Dictionary OR M-Miss-spelledList
D
Where do you wanna add the word for to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word and to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word long to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word will to (D|M)?
D-Dictionary OR M-Miss-spelledList
D
Where do you wanna add the word do to (D|M)?
D-Dictionary OR M-Miss-spelledList
D
Where do you wanna add the word your to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word desire to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
****Miss-Spelled Words are:
and
desires
everyone
for
long
what
your
======== Spell checking D:\Ukraine.txt =========
Where do you wanna add the word hi to (D|M)?
D-Dictionary OR M-Miss-spelledList
D
Where do you wanna add the word dude to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word all to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word the to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word right to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word have to (D|M)?
D-Dictionary OR M-Miss-spelledList
D
Where do you wanna add the word on to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word land to (D|M)?
D-Dictionary OR M-Miss-spelledList
D
Where do you wanna add the word people to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word can to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word your to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word and to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word transfer to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word them to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word to to (D|M)?
D-Dictionary OR M-Miss-spelledList
M
Where do you wanna add the word great to (D|M)?
D-Dictionary OR M-Miss-spelledList
D
Where do you wanna add the word transaction to (D|M)?
D-Dictionary OR M-Miss-spelledList
D
****Miss-Spelled Words are:
all
and
can
dude
on
people
rights
speak
the
them
to
transfer
your

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