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

Please do the following project in C++ programming language. You can use a bag t

ID: 3883783 • Letter: P

Question

Please do the following project in C++ programming language.

You can use a bag to create a spell checker. The bag serves as a dictionary and contains a collection of correctly of correctly spelled workds. To see whether a word is spelled correctly, you see whether it is contained in the dictionary. Use this scheme to create a spell checker for the words in an external file. To simplify your task, restrict your dictionary to a manageable size.

The dictionary to add to your bag is attached Your program should read in the contents of this file and put them into a bag. Then, read a second file (not supplied, you chose) and check to see if the words from the file appear in your bag. If so, assume the word is spelled correctly. If not, assume the word is misspelled. Your program should print out the misspelled words.

For this program, you are free to create other functions (in addition to main()) as needed. (In chapter 1, the guessing game problem asks for an ADT, so you should probably have multiple functions/methods!) Can probably be completed in a single source code file (in addition to the Bag source code). I'm open to more complex implementations, but for the purposes of this assignment, it's not necessary.

Apple Apricot Avocado Banana Bilberry Blackberry Blackcurrant Blueberry Boysenberry Currant Cherry Cherimoya Cloudberry Coconut Cranberry Cucumber Damsorn Date Peach Pear Persimmon Plantain Plum Prune Pineapple Pomegranate Pomelo Quince Raspberry Salmonberry Redcurrant Salak Satsuma Soursop Strawberry Durian Elderberry Fig Gooseberry Grape Raisin Grapefruit Guava Honeyberry Huckleberry Tamarind Yuzu Avocado Cucumber Eggplant Olive Pea Squash Tomato Jackfruit Jambul Jujube Kiwifruit Kumquat Lemon Lime Loquat Langan Lychee Mango Melon Cantaloupe Honeydew Watermelon Mulberry Nectarine Nance Olive Orange Clementine Tangerine Papaya Passionfruit

Explanation / Answer

Spell.h

#ifndef _SPELL_H_

#define _SPELL_H_

#include <vector>

#include <map>

class Spell

{

private:

typedef std::vector<std::string> Vector;

typedef std::map<std::string, int> Dictionary;

Dictionary dict;

void editing(const std::string& wrds, Vector& res);

void knowing(Vector& results, Dictionary& cnd);

public:

void loading(const std::string& fName);

std::string correcting(const std::string& wrds);

};

#endif

Spell.cpp

#include <fstream>
#include <string>
#include <algorithm>
#include <iostream>

#include "Spell.h"

using namespace std;

bool sortingSec(const pair<std::string, int>& lt, const pair<std::string, int>& rt)
{
return lt.second < rt.second;
}

char filterNonAlph(char& lett)
{
if (lett < 0)
return '-';
if (isalpha(lett))
return tolower(lett);
return '-';
}

void Spell::loading(const std::string& fName)
{
ifstream file(fName.c_str(), ios_base::binary | ios_base::in);

file.seekg(0, ios_base::end);
std::streampos length = file.tellg();
file.seekg(0, ios_base::beg);

string data(static_cast<std::size_t>(length), '');

file.read(&data[0], length);

transform(data.begin(), data.end(), data.begin(), filterNonAlph);

for (string::size_type uu = 0; uu != string::npos;)
{
const string::size_type nonFilt = data.find_first_not_of('-', uu + 1);
if (nonFilt == string::npos)
break;

const string::size_type end = data.find('-', nonFilt);
dict[data.substr(nonFilt, end - nonFilt)]++;

uu = end;
}
}

string Spell::correcting(const std::string& wrds)
{
Vector res;
Dictionary cnd;

if (dict.find(wrds) != dict.end()) { return wrds; }

editing(wrds, res);
knowing(res, cnd);

if (cnd.size() > 0) { return max_element(cnd.begin(), cnd.end(), sortingSec)->first; }

for (unsigned int uu = 0;uu < res.size();uu++)
{
Vector subRes;

editing(res[uu], subRes);
knowing(subRes, cnd);
}

if (cnd.size() > 0) { return max_element(cnd.begin(), cnd.end(), sortingSec)->first; }

return "";
}

void Spell::knowing(Vector& results, Dictionary& cnd)
{
Dictionary::iterator end = dict.end();

for (unsigned int uu = 0;uu < results.size();uu++)
{
Dictionary::iterator value = dict.find(results[uu]);

if (value != end) cnd[value->first] = value->second;
}
}

void Spell::editing(const std::string& wrds, Vector& res)
{
for (string::size_type uu = 0;uu < wrds.size(); uu++) res.push_back(wrds.substr(0, uu) + wrds.substr(uu + 1));
for (string::size_type uu = 0;uu < wrds.size() - 1;uu++) res.push_back(wrds.substr(0, uu) + wrds[uu + 1] + wrds[uu] + wrds.substr(uu + 2));

for (char qq = 'a';qq <= 'z';++qq)
{
for (string::size_type uu = 0;uu < wrds.size(); uu++) res.push_back(wrds.substr(0, uu) + qq + wrds.substr(uu + 1));
for (string::size_type uu = 0;uu < wrds.size() + 1;uu++) res.push_back(wrds.substr(0, uu) + qq + wrds.substr(uu));
}
}

main.cpp

#include <iostream>

#include <string>

#include "Spell.h"

using namespace std;

Spell corr;

void Correctings(const std::string& incorrect, const std::string& corrected)

{

const bool isRight = corr.correcting(incorrect) == corrected;

std::cout << "(" << incorrect << ") == (" << corrected << ") = (" << std::boolalpha << isRight << ")" << std::endl;

}

int main()

{

corr.loading("big.txt");

Correctings("speling", "spelling");

Correctings("korrectud", "corrected");

Correctings("bycycle", "bicycle");

Correctings("inconvient", "inconvenient");

Correctings("arrainged", "arranged");

Correctings("peotry", "poetry");

Correctings("peotryy", "poetry");

Correctings("wrds", "wrds");

Correctings("quintessential", "");

string req;

while (req != "quit")

{

cout << "Enter a wrds ";

cin >> req;

string correcting(corr.correcting(req));

if (correcting != "")

cout << "You meant: " << correcting << "? ";

else

cout << "No correction suggestion ";

}

cin.get();

return 0;

}

Rate an upvote......Thankyou

Hope this helps....