JAVA problem In this exercise, you will create a simple spell checker program. Y
ID: 3856100 • 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
"
I have the dictionary.txt file but can someone please help me achieve an output that looks something like this(below) when i input these files into the command line?
OUTPUT:
Explanation / Answer
import java.io.*; import java.util.*; /** * A custom Spell Checker program which uses a Trie as the data structure * * The executable builds it's dictionary by reading in a defined dictionary file, * or "/usr/share/dict/words" as default, and then prompts the user to type a word. * Based on the input word, 3 rules are checked to see if a match is found or a * suggestion can be made. If no word is found, then NO SUGGESTION is printed. * * The current rules this software checks for are: * 1. ImPROper cAPITalIZAioN. The entire dictionary is treated as lower case and all * inputs are suggested as lowercase. * 2. Misplacad vuwals. If the input word is not found, the vowels A,E,I,O,U are * substituted until a suggestion is found. Through a brute force method, all * vowel combinations are attempted. For example, in the input "tost", "test", * "tast", "tist", "tust" are all attempted words. While "toast" tries "toest", * "toust", "teost", "teust", etc... * 3. Reeeepeeeated Letttters. Any repeated letters are removed and the word is * checked again. * * Any combination of these rules is acceptable. Capitalizations can occur on the same * section of the string as repeated letters or incorrect vowels. * * @author Dan Moore * @version 3.0 */ public class Spellchecker { private static final char PROMPT = '>'; private static Trie dict = new Trie(); private static Scanner input = new Scanner(System.in); private static boolean DEBUG = false; /** * The command line program which prompts a user for words, read in using new lines * @param args[0] Debug Mode (true or false) */ public static void main(String[] args) { if(args.length > 0) DEBUG = Boolean.parseBoolean(args[0]); String word, found; if(DEBUG) { buildDictionary("/home/user/workspace/spellchecker/src/words.txt"); printDictionary(); } else { buildDictionary("/usr/share/dict/words"); } try { while(true) { word = getWord().toLowerCase(); found = dict.find(word); if(DEBUG) System.out.print("Trying: "+word+". Got: "); if(found != null) { System.out.println(found); } else { System.out.println("NO SUGGESTION"); } } } catch(Exception e) { //ignore fatals, when terminating loop } } /** * Prompt and get the next word from the user * @return the word entered by the user */ private static String getWord() { System.out.print(PROMPT+""); return input.next(); } /** * Builds a dictionary file into a Trie * @param fileName the dictionary file */ private static void buildDictionary(String fileName) { try { BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(fileName)))); String line; while ((line = br.readLine()) != null) { dict.insert(line.toLowerCase()); //System.out.println("Added to dictionary: "+line); } } catch (Exception e) { //File not found, rebuild dictionary with default dictionary file System.err.println(e.getMessage()); //System.err.println("File not found: "+fileName); //System.err.println("Using standard system dictionary: /usr/share/dict/words"); //buildDictionary("/usr/share/dict/words"); } } private static void printDictionary() { System.out.println(dict.toString()); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.