C++, How to create a Spellchecker class . . . Part I In this part of the assignm
ID: 3864789 • Letter: C
Question
C++, How to create a Spellchecker class . . .
Part I In this part of the assignment, you are to create a class, SpellChecker. You will define some class data members, member methods and helper functions. The class methods will be used to check and correct the spelling of words. Elements of this assignment are intentionally vague; at this point in the semester, you should be able to make your own decisions about appropriate data structures for storing and looking up data, as well as defining helper functions. You can assume that your code will never be storing more than 10,000 valid or misspelled words. Spell Checker should have at least the following Public members: string language: the name of the language this spell checker is using (i.e. "English", "Spanish", Spell Checker should have at least the following Private members: char begin mark: used for marking the beginning of an unknown word in a string char end mark: used for marking the end of an unknown word. SpellChecker should have three constructors (set the object's data members to some default valu Default Constructor, the one with no parameters. Second constructor that takes a string parameter for the object's language Third constructor that takes a string for the object's language and two filenames as parameters. The first filename specifies the file with correctly spelled words and the second filename specifies the misspelled words with their corrections. You will be dealing with two different file types: The data in the first filename supplies a list of correctly spelled words, one word per l aardvark apple acquireExplanation / Answer
//SpellRunner.cpp
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cstdio>
#include "CPUTimer.h"
#include "mem2.h"
#include "speller.h"
// uncomment line below for DOS/Windows OS.
// #define DOS
#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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.