JAVA problem In this exercise, you will create a simple spell checker program. Y
ID: 3856451 • Letter: J
Question
JAVA problem
In this exercise, you will 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:
The text file for Russia.txt is below
"
The text file for Ukraine.txt is below
"
The dictionary.txt file is too large to post.
OUTPUT:
Please help me achieve an output that looks something like this(above) when i input these files into the command line?
Explanation / Answer
Here is the completed code for the program. Since I did not have the full dictionary, I tested with a test dictionary file and I am sure it work for you. In case of any issues, post a comment. If the answer helped, please rate it. Thank you.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class SpellCheck {
// We could hava also used TreeSet for the dictionary
private HashSet dictionary = new HashSet();
private TreeSet miss_spelled_words = new TreeSet();
public SpellCheck() throws FileNotFoundException
{
// Add all of the words from "dictionary.txt" to the dictionary HashSet
Scanner inFile = new Scanner(new File("dictionary.txt"));
while(inFile.hasNext())
{
dictionary.add(inFile.next());
}
inFile.close();
}
public void checkSpelling(String fileName) throws FileNotFoundException
{
System.out.println("======== Spell checking " + fileName + " =========");
// Clear miss_spelled_words
miss_spelled_words.clear();
// Read in each line from "fileName"
Scanner inFile = new Scanner(new File(fileName));
Scanner keybd = new Scanner(System.in);
String line, token, ans;
int lineNo = 1;
boolean displayed ;
while(inFile.hasNextLine())
{
line = inFile.nextLine();
displayed = false;
// For each line, break the line into words using the following StringTokenizer
StringTokenizer st = new StringTokenizer(line, " ,.;:-%'"");
// lower case each word obtained from the StringTokenizer
while(st.hasMoreTokens())
{
token = st.nextToken().toLowerCase();
// skip word if the first character is not a letter
if(!Character.isLetter(token.charAt(0)))
continue;
// Skip over word if it can be found in either dictionary, or miss_spelled_words
if(dictionary.contains(token) || miss_spelled_words.contains(token))
continue;
// If word ends with 's', then try the singular version of the word in the dictionary and miss_spelled_words ... skip if found
if(token.endsWith("s"))
{
String singular = token.substring(0, token.length() - 1);
if(singular.equals("") || dictionary.contains(singular) || miss_spelled_words.contains(singular))
continue;
}
// Print line containing miss-spelled word(make sure you only print it once if multiple miss-spelled words are found on this line)
if(!displayed)
{
System.out.println(lineNo +": " + line);
displayed = true;
}
// 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(token + " not in dictionary. Add to dictionary? (y/n)");
ans = keybd.next();
if(ans.equalsIgnoreCase("y"))
dictionary.add(token);
else
miss_spelled_words.add(token);
}
lineNo++;
}
inFile.close();
}
public void dump_miss_spelled_words()
{
// Print out the miss-spelled words
System.out.println("****** Miss spelled words *******");
for(Object word : miss_spelled_words)
System.out.println(word);
}
public static void main(String[] args) {
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);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.