Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

USE JAVA FOR THE FOLLOWING PROGRAM A spelling checker reads the words in a document and outputs a list of misspelled words. For each misspelled

USE JAVA FOR THE FOLLOWING PROGRAM

A spelling checker reads the words in a document and outputs a list of misspelled words. For each misspelled word, the program outputs the word followed by a list of the line numbers where the word is found in the document.

The program decides which words are misspelled by using a dictionary of correct words. The program searches the dictionary for each word in the document. If a word from the document cannot be found in the dictionary, the word is added to the list of misspelled words.

Project Files

The following files are required to complete the project. You should download these files from Google Drive. You are not allowed to modify these files in any way except for implementing methods that were intentionally left empty. Specifically, interface definitions may not be modified, and all code must remain in the spellcheck folder.

---------------------------------------------------------------------------------------------------------------

(SpellCheckInterface.java)

import java.io.*; public interface SpellCheckInterface { /** * Loads the dictionary contained in the specified file * * @param filename The name of the dictionary file to be loaded * @return true If the file was successfully loaded, false * if the file could not be loaded or was invalid * @throws IOException if filename does not exist */ public boolean loadDictionary(String fileName) throws IOException; /** * Check the document for misspelled words * * @return true if the file was successfully spell checked, false * if the file had misspelled words * @throws IllegalArgumentException if filename is null */ public boolean checkDocument(String fileName) throws IOException;

}

---------------------------------------------------------------------------------------------------------------

(Word)

import java.util.*; public class Word implements Comparable { private String word; // the misspelled word private ArrayList lines; // line numbers public Word() { lines = new ArrayList<>(); } public Word(String w, int lineNumber) { word = w; lines = new ArrayList<>(); lines.add(lineNumber); } public String getWord() { return word; } public ArrayList getLines() { return lines; } public void addLine(int lineNumber) { lines.add(lineNumber); } public String toString() { return word + " : "+lines.toString(); } /** *@return true if two words are equal, false otherwise */ @Override public boolean equals(Object w) { // Left as exercise } /** *@compare two words and return the corresponding integer value (<0, 0, >0) */ @Override public int compareTo(Word w) { return this.word.compareTo(w.word); } }

---------------------------------------------------------------------------------------------------------------

(SpellChecker)

import java.util.*; import java.io.*; public class SpellChecker implements SpellCheckInterface { LinkedList dict; LinkedList misspelled; public SpellChecker() { dict = new LinkedList<>(); misspelled = new LinkedList(); } /** * Loads the dictionary contained in the specified file * * @param filename The name of the dictionary file to be loaded * @return true If the file was successfully loaded, false * if the file could not be loaded or was invalid * @throws IOException if filename does not exist */ @Override public boolean loadDictionary(String fileName) throws IOException { // Left as exercise return true; } /** * Check the document for misspelled words * * @return A list of misspelled words and * the line numbers where they occur. * @throws @throws IOException if filename does not exist */ @Override public boolean checkDocument(String fileName) throws IOException { misspelled = new LinkedList<>(); // Initialize for each file int lineNumber = 0; try { File infile = new File(fileName); System.out.println(" File Name:"+ fileName); try ( Scanner in = new Scanner(infile); ) { /* wrong code shold be deleted in.useDelimiter("[^A-Za-z]+"); // split input by words while (in.hasNext()){ String myWord = in.next().toLowerCase(); lineNumber++; */ // Correct code to be added while (in.hasNextLine()){ String line = in.nextLine().toLowerCase(); lineNumber++; String[] tokens = line.split("[ \\P{Alpha}]+"); for(int i=0;i if (tokens[i].length()==0) // ignore blank lines continue; if(!dict.contains(tokens[i])) { Word word = new Word(tokens[i],lineNumber); // Left as exercise // if dictionary does not contain myWord: // myWord is misspelled // if misspelled list contains myWord, update the lineNumber // otherwise add a new Word to misspelled list } // end if } // end for } // end while } // end try } catch (IOException ex) { System.out.println(ex); return false; } // end catch // convert misspelled to arra list aand then sort the list Object[] list ; if(list.length==0) return true; else { System.out.println("Misspelled words are"); for(Object w : list) System.out.println(w); return false; } //else } // checkDocument } // class

---------------------------------------------------------------------------------------------------------------

(Driver) import java.io.*; public class Driver {

public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub SpellCheckInterface check = new SpellChecker(); check.loadDictionary("dictionary.txt"); if(check.checkDocument("test.txt")) System.out.println("No misspelled words found"); if(check.checkDocument("short.txt")) System.out.println("No misspelled words found"); }

---------------------------------------------------------------------------------------------------------------

Document Files

short.txt

test.txt

The documents to be spell checked are just regular text files (i.e., sequences of characters). The spelling checker reads in the file to be spell checked and extracts all of the words from the file. Each word in the file is checked against the words in the currently loaded dictionaries in order to detect misspelled words. If a word is not found in the currently loaded dictionaries, it is considered to be misspelled. A document file might contain English prose, or it could just be a list of words. The program does the same thing in either case:

1) extracts the words in the file and

2) checks to see if they're in the dictionary.

How does the program find the words in the document file? Words are defined as sequences of letters that are separated by characters that are not letters. The characters that you need to check for are numbers, white space (spaces, tabs, new lines, etc.), periods, commas, colons, semi-colons, parentheses, apostrophes, hyphens, equals, pluses, percents, slashes, quotes, asterisks, square braces, curly braces, less than, greater than, question marks, exclamation marks, ampersands, vertical bars and underscores.

The output of the spelling checker is a sorted list of misspelled words, one word per line. Use a character to indicate a new line. Each word is lowercase and followed by a colon and a comma-separated list of line numbers where the word is found in the document. Make sure to remember the space after the comma.

---------------------------------------------------------------------------------------------------------------

short.txt should contain:

This is a very short document.

Sample output with short.txt file

No misspelled words found

---------------------------------------------------------------------------------------------------------------

test.txt should contain:

HAY VERSION Foeur score and seven years ago our fathers brought forth, upon this continent, a new natiion, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any natiion so conceived, and so dedicated, can long endure. We are met here on a great batlefield of that war. We have come to dedicate a portion of it as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But in a larger sense we can not dedicate -- we can not consecrate -- we can not hallow this ground. The brave men, living and dead, who struggled, here, have consecrated it far above our poor power to add or detract. The world will little note, nor long remember, what we say here, but can never forget what they did here. It is foor us, the living, rather to be dedicated here to the unfinished work which they have, thus far, so nobly carried on. It is rather foor us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they here gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain; that this nation shall have a new birth of freedom; and that this government of the people, by the people, for the people, shall not perish from the earth. NICOLAY VERSION Foor score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived, and so dedicated, can long endure. We are met on a great battle field of that war. We come to dedicate a portion of it, as a final resting place for those who died here, that the nation might live. This we may, in all propriety do. But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow, this ground -- The brave men, living and dead, who struggled here, have hallowed it, far above our poor power to add or detract. The world will little note, nor long remember what we say here; while it can never forget what they did here. It is rather for us, the living, we here be dedcated to the great task remaining before us -- that, from these honored dead we take increased devotion to that cause for which they here, gave the last full measure of devotion -- that we here highly resolve these dead shall not have died in vain; that the nation, shall have a new birth of freedom, and that government of the peple by the peple for the people, shall not perish from the earth.

Sample output with test.txt file

as : [10, 40]

batlefield : [9]

be : [20, 22, 49]

by : [27, 54]

dedcated : [49]

foeur : [3]

foor : [20, 22, 33]

natiion : [4, 8]

nicolay : [31]

peple : [54, 54]

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

DO NOT ADD/IMPORT ANY NEW CLASSES/METHODS ONLY THE ONES LEFT AS AN EXERCISE SHOULD BE DONE.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

Tips

  1. Remember that Java is case-sensitive. If your dictionary is all in lower-case, and your file has some words in upper-case, the upper-case words won't be in the dictionary!

  2. When using the Scanner, you want to tokenize by non-letter characters. You tell the Scanner to delimit by non-letter characters by using the following pattern: "[^A-Za-z]+". See the API for how to use patterns with the Scanner class.

  3. Remember, it should be possible to use the same instantiated SpellingChecker object to check several documents, make sure that misspelled words found in one run of checkDocument() do not show up in the list of misspelled words for subsequent runs unless they really should be there (ie. make sure that you clear your list of misspelled words every time you scan a new document)

NOTE:

  1. Do not add any new classes

  2. Do not alter the data files.

  3. Do not change the algorithm.

  4. Your program should work with the given specification and algorithm to get credit.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Explain the various methods of job evaluation

Answered: 1 week ago

Question

Differentiate Personnel Management and Human Resource Management

Answered: 1 week ago

Question

Describe the functions of Human resource management

Answered: 1 week ago