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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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), '\0');

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\n";

cin >> req;

string correcting(corr.correcting(req));

if (correcting != "")

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

else

cout << "No correction suggestion\n\n\n";

}

cin.get();

return 0;

}

Rate an upvote......Thankyou

Hope this helps....

Add a comment
Know the answer?
Add Answer to:
Please do the following project in C++ programming language. You can use a bag to create...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker. Set Methods For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined: set.size() -- Returns the number of items in the set. set.add(item) -- Adds the item to the...

  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker. Set Methods For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined: set.size() -- Returns the number of items in the set. set.add(item) -- Adds the item to the...

  • Write a Python equivalent program for the Unix spell utility. You can use the dictionary at /usr/...

    Write a Python equivalent program for the Unix spell utility. You can use the dictionary at /usr/share/dict/words (if you machine does not have it, you can copy it from a Linux machine such as npu29). The minimum requirement is to check if each word in the file exists in the dictionary as is (case insensitive). Your spell checker should inlcude at least two features: 1. Check the simple plural forms (add s or es). 2. Check the simple verb past...

  • In this assignment you will implement the second version of your spell checker. Using the randomi...

    In this assignment you will implement the second version of your spell checker. Using the randomized dictionary (random_dictionary.txt) given, read in the dictionary into an array of 26 Binary Search Trees (BST) , one for each letter of the alphabet. Thus the first BST would contain only those words starting with letter 'a', while the last would contain only those words starting with letter 'z'. Then, when you read in the book (oliver.txt), you examine the first character of each...

  • C++ program: can you help create a autocorrect code using the cpp code provided and the...

    C++ program: can you help create a autocorrect code using the cpp code provided and the words below using pairs, vectors and unordered map: Objectives To practice using C++ std::pair, std::vector, and std::unordered_map To tie together what we've learned into the context of a real-world application used by millions of people every day Instructions For Full Credit You're given a short list of words in known_words_short.txt that contains a handful of very different words. Assume this short list of words...

  • 26-ary tree for spell checker in JAVA You are asked to write some functionalities for a...

    26-ary tree for spell checker in JAVA You are asked to write some functionalities for a spelling checker inside Tree.java that has at least the following methods: /*Adds a word to a spelling checker’s collection of correctly spelled words*/ void add(String word) /*Returns true if the given word is spelled correctly*/ boolean check(String word) /*Returns back all words in the tree in alphabetical order*/ public String getDictionaryInAlphabeticalOrder() Store the collection of correctly spelled words in a 26-ary tree. The number...

  • This is for C programming: You will be given files in the following format: n word1...

    This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • Complete the following code: You are asked to write some functionalities for a spelling checker i...

    Complete the following code: You are asked to write some functionalities for a spelling checker inside Tree.java that has at least the following methods: /*Adds a word to a spelling checker’s collection of correctly spelled words*/ void add(String word) /*Returns true if the given word is spelled correctly*/ boolean check(String word) /*Returns back all words in the tree in alphabetical order*/ public String getDictionaryInAlphabeticalOrder() Store the collection of correctly spelled words in a 26-ary tree. The number 26 indicates that...

  • C++ Hangman (game) In this project, you are required to implement a program that can play...

    C++ Hangman (game) In this project, you are required to implement a program that can play the hangman game with the user. The hangman game is described in https://en.wikipedia.org/wiki/Hangman_(game) . Your program should support the following functionality: - Randomly select a word from a dictionary -- this dictionary should be stored in an ASCII text file (the format is up to you). The program then provides the information about the number of letters in this word for the user to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT