Question
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
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.
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.
SpellCheckerInterface.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.java
import java.util.*; public class Word implements Comparable
SpellChecker.java
import java.util.*;
import java.io.*;
public class SpellChecker implements SpellCheckInterface {
LinkedList
LinkedList
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); )
{
in.useDelimiter("[^A-Za-z]+"); // split input by words
while (in.hasNext()){
String myWord = in.next().toLowerCase();
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
}
}
} catch (IOException ex) {
System.out.println(ex);
return false;
// sort misspelled list
if(list.length==0)
return true;
else {
System.out.println("Misspelled words are");
for(Object w : list)
System.out.println(w);
return false;
}
}
}
Driver.java
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"); }
-----------------------------------------------------------------------------
Dictionary File Format (dictionary.txt)
The spelling checker reads one or more dictionaries of correct words. Each dictionary is a text file that contains a list of words, one word per line as shown in this example.
abalone abalones abandon abandoned abandoning abandonment
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.
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]
Sample output with short.txt file
No misspelled words found
NOTE:
-
Do not add any new classes
-
Do not alter the data files.
-
Do not change the algorithm.
-
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started