Question
We will create a spell checker program. This program willuse a txt file as a dictionary to look up properlyspelled words. When the program starts,
We will create a spell checker program. This program willuse a txt file as a dictionary to look up properlyspelled words. When the program starts, it will open the dictionaryfile (name it "Words.txt"). You will utilize a hash function youhave created to hash the words into an externally-chainedhash table you have created. The hashtable will be of a size that you will determine. You willreport the following statistics in the output:
maximum chain length
average chain length
percentage of buckets to which one or more words hashed
Your program will then open an input file named"inputTextFile.txt." When the checking proceeds, you will extract aword from the file to be checked, hash it into the table, anddetermine if it exists. You will continue this process until youhave checked all the words in the file. Each time you find a wordthat you cannot match in the dictionary, you will let the user knowand you will attempt to generate a list of suggested words. Youwill generate the list by assembling similar words via fourmethods:
One letter missing. You assume that one letter has been left outof the word. You can assemble new words to check by adding lettersa..z in each of the positions in the word from thestart to the end of the word.
One letter added. You assume the word has an extra letter. Youscan through the word deleting each of the letters in turn, andlooking up the word formed by the remaining letters.
Two letters reversed. You swap letters in positions0..1, 1..2, 2..3, ... , n-2..n-1, to form newwords which you look up.
One letter changed. You try each letter at each position in theword to see if one letter was simply mis-typed.
Each time you find a legal word from any of the four methods,you add it to the suggestion ArrayList, which you present to theuser when you have finished this part of the process. If you cannotidentify any suggestions, let the user know.
Example file:
Hello. My plan is to hav a test fiile that has UPPer and LOWERcase words. All fuor cises of misspellings will be represented. Thefile will encompass more than one line and will have no otherpuncktuation than commas, and the dot at the end of a line.
I NEED TO MAKE THIS PROGRAM REPORT THE FOLLOWINGSTATISTICS IN THE OUTPUT:
maximum chain length
average chain length
percentage of buckets to which one or more wordshashed
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class SpellChecker {
private DictionaryClass dictionary;
final static String dictionaryFilePath ="Words.txt";
final static char[] alphabet ="abcdefghijklmnopqrstuvwxyz".toCharArray();
SpellChecker() throws FileNotFoundException{
dictionary = newDictionaryClass();
dictionary.build(dictionaryFilePath);
}
void run() throws FileNotFoundException{
Scanner scan = newScanner(new File("inputFile.txt"));
boolean done = false;
String input="";
String suggestions="";
while (scan.hasNext()){
input=scan.next();
if(input.equals("")) {
break;
}
if(!dictionary.contains(input)) {
suggestions+="" + input+" is not spelledcorrectly, ";
suggestions+=findSuggestions(input);
suggestions+="";
}
}
System.out.println(suggestions);
}
String findSuggestions(String word) {
StringBuilder strbuld = newStringBuilder();
ArrayListsuggestions = makeSuggestions(word);
if (suggestions.size() == 0){
return"and I have no idea what word you could mean.";
}
strbuld.append("perhaps youmeant:");
for (String s : suggestions){
strbuld.append(" - " + s);
}
returnstrbuld.toString();
}
private ArrayListmakeSuggestions(String word) {
ArrayListresult = new ArrayList<>();
result.addAll(charAppended(word));
result.addAll(charMissing(word));
result.addAll(charsSwapped(word));
return result;
}
private ArrayListcharAppended(String word) {
ArrayListresult = new ArrayList<>();
for (char c : alphabet){
StringatFront = c + word;
StringatBack = word + c;
if(dictionary.contains(atFront)) {
result.add(atFront);
}
if(dictionary.contains(atBack)) {
result.add(atBack);
}
}
return result;
}
private ArrayListcharMissing(String word) {
ArrayListresult = new ArrayList<>();
int len = word.length() -1;
//try removing char from thefront
if(dictionary.contains(word.substring(1))) {
result.add(word.substring(1));
}
for (int i = 1; i < len;i++) {
//tryremoving each char between (not including) the first and last
Stringworking = word.substring(0, i);
working =working.concat(word.substring((i + 1), word.length()));
if(dictionary.contains(working)) {
result.add(working);
}
}
if(dictionary.contains(word.substring(0, len))) {
result.add(word.substring(0, len));
}
return result;
}
private ArrayListcharsSwapped(String word) {
ArrayListresult = new ArrayList<>();
for (int i = 0; i Stringworking = word.substring(0, i);
working =working + word.charAt(i + 1);
working =working + word.charAt(i);
working =working.concat(word.substring((i + 2)));
if(dictionary.contains(working)) {
result.add(working);
}
}
return result;
}
public static void main(String[] args) throwsFileNotFoundException {
SpellChecker scObj = newSpellChecker();
scObj.run();
}
}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class DictionaryClass {
private int primeNum = 65000;//primeNumber
final private Bucket[] bucket;
public DictionaryClass() {
this.primeNum = primeNum;
bucket = newBucket[primeNum];
for (int i = 0; i bucket[i]= new Bucket();
}
}
private int hash(String k) {
return (k.hashCode() &0x7fffffff) % primeNum;
}
//call hash() to decide which bucket to putit in, do it.
public void add(String k) {
bucket[hash(k)].put(k);
}
//call hash() to find what bucket it's in,get it from that bucket.
public boolean contains(String word) {
word =word.toLowerCase();
returnbucket[hash(word)].get(word);
}
public void build(String filePath) throwsFileNotFoundException {
try {
BufferedReader reader = new BufferedReader(newFileReader("Words.txt"));
Stringword;
while((word = reader.readLine()) != null) {
add(word);
}
} catch (IOException ioe){
ioe.printStackTrace();
}
}
//this method is used in my unit tests
public String[] getRandomEntries(int num){
String[] toRet = newString[num];
for (int i = 0; i < num;i++){
//pick arandom bucket, go out a random number
Node node= bucket[(int) (Math.random() * primeNum)].first;
int rand =(int) (Math.random() * (int)Math.sqrt(num));
for(intj = 0; j toRet[i]=node.word;
}
return toRet;
}
class Bucket {
private Node first;
public boolean get(Stringin) { //return key true if key exists
Node next= first;
while(next != null) {
if (next.word.equals(in)) {
return true;
}
next = next.next;
}
returnfalse;
}
public void put(String k){
for (Nodecurr = first; curr != null; curr = curr.next) {
if (k.equals(curr.word)) {
return; //search hit:return
}
}
first =new Node(k, first); //search miss: add new node
}
}
class Node {
String word;
Node next;
public Node(String k, Nodenext) {
word =k;
this.next= next;
}
}
}
Step by Step Solution
3.32 Rating (146 Votes )
There are 3 Steps involved in it
Step: 1
The provided code is an example of a spell checker program in Java The program uses a text file as a dictionary and uses a hash function to hash the w...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