Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a spelling checker by using a hash table. Your program should take two

ID: 3775469 • Letter: I

Question

Implement a spelling checker by using a hash table. Your program should take two command line arguments: the name of a dictionary file and the name of the file that contains the text you wish to spellcheck. Output all misspelled words and the line numbers in which they occur. Numbers and contractions are considered valid words. Only strip leading and trailing punctuation. Also, for each misspelled word, list any words in the dictionary that are obtainable by applying any of the following rules:

a. Add one character.
b. Remove one character.
c. Exchange adjacent characters.

Explanation / Answer

#include <fstream>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cstdio>
#include "CPUTimer.h"
#include "mem2.h"
#include "speller.h"

#ifdef DOS
#include <sysstat.h>
#else
#include <sys/stat.h>
#endif

int *ptr7;
int used7 = 0;
int maxRAM7 = 0;

using namespace std;

char* readDictionary(char *filename, char **dictionary)
{
int i = 0, fileSize;
char *s;
struct stat statbuf;

stat(filename, &statbuf);
fileSize = statbuf.st_size;
s = new char[fileSize + 1];
ifstream inf(filename);
inf.read(s, fileSize);
inf.close();
s[fileSize] = '';
dictionary[0] = strtok(s, " ");

while(dictionary[i])
dictionary[++i] = strtok(NULL, " ");

return s;
} // readDictionary

void readDocument(char **document, int dictSize, int docSize, int seed)
{
char *s, filename[80];
int i = 0, fileSize;
struct stat statbuf;
sprintf(filename, "Doc-%d-%d-%d.txt", dictSize, docSize, seed);
stat(filename, &statbuf);
fileSize = statbuf.st_size;
s = new char[fileSize + 1];
ifstream inf(filename);
inf.read(s, fileSize);
inf.close();
s[fileSize] = '';

document[0] = strtok(s, " ");

while(document[i])
document[++i] = strtok(NULL, " ");
} // readDocument()

void readWrongs(int *wrongs, int dictSize, int docSize, int seed,
int &wrongCount)
{
char filename[80];
wrongCount = 0;

sprintf(filename, "Wrongs-%d-%d-%d.txt", dictSize, docSize, seed);
ifstream inf(filename);
while(inf >> wrongs[wrongCount++]);

wrongCount--;
} // readWrongs()

void checkAnswers(int wrongs[], int wrongCount, int misspelled[],
int misspelledCount)
{
for(int i = 0; i < wrongCount && i < misspelledCount; i++)
if(wrongs[i] < misspelled[i])
{
cout << "Speller missed misspelled word # " << wrongs[i] << endl;
return;
}
else
if(wrongs[i] > misspelled[i])
{
cout << "Speller thought correctly spelled word # " << misspelled[i]
<< " was wrong ";
return;
}

if(wrongCount != misspelledCount)
cout << "Speller found " << misspelledCount << " misspelled words when "
<< " there were really " << wrongCount << " misspelled words. ";
} // checkAnswers



int main(int argc, char* argv[])
{
char line[80], **dictionary, **document, *s;
int *wrongs, *misspelled, misspelledCount = 0, seed, dictSize, docSize,
wrongCount;
strcpy(line, argv[1]);
strtok(line, "-");
dictSize = atoi(strtok(NULL, "-"));
docSize = atoi(strtok(NULL, "-"));
seed = atoi(strtok(NULL, "."));
dictionary = new char*[dictSize + 3];
s = readDictionary(argv[1], dictionary);
document = new char*[docSize + 3];
readDocument(document, dictSize, docSize, seed);
wrongs = new int[docSize];
readWrongs(wrongs, dictSize, docSize, seed, wrongCount);
misspelled = new int[docSize];
CPUTimer ct;
ct.reset();
Speller *speller = (Speller*) new2(Speller(dictionary, dictSize));
delete [] dictionary;
delete [] s;
speller->check(document, docSize, misspelled, &misspelledCount);
cout << "CPU Time: " << ct.cur_CPUTime() << " Real RAM: " << maxRAM7 << endl;
checkAnswers(wrongs, wrongCount, misspelled, misspelledCount);
delete speller;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote