using std::map; using std::set; using std::pair; using std::string; using std::i
ID: 3804693 • Letter: U
Question
using std::map;
using std::set;
using std::pair;
using std::string;
using std::ifstream;
using std::stringstream;
using std::getline;
SpellChecker::SpellChecker() {}
SpellChecker::SpellChecker(string lang) {}
SpellChecker::SpellChecker(string lang, string valid_words_filename, string misspelled_filename)
{
ifstream correct_words(valid_words_filename.c_str());
if (correct_words)
{
string s;
while (getline(correct_words, s))
{
valid_words.insert(s);
}
}
ifstream wrong_words(misspelled_filename.c_str());
if (wrong_words)
{
string s1, s2;
while (wrong_words >> s1)
{
getline(wrong_words, s2);
misspelled.insert(pair<string, string>(s1, s2));
}
}
}
bool SpellChecker::loadValidWords(string filename)
{
ifstream correct_words(filename.c_str());
if (correct_words)
{
valid_words.clear();
string s;
while (getline(correct_words, s))
{
valid_words.insert(s);
}
return true;
}
return false;
}
bool SpellChecker::loadMisspelledWords(string filename)
{
ifstream wrong_words(filename.c_str());
if (wrong_words)
{
misspelled.clear();
string s1, s2;
while (wrong_words >> s1)
{
getline(wrong_words, s2);
s2.erase(s2.begin());
misspelled.insert(pair<string, string>(s1, s2));
}
return true;
}
return false;
}
void SpellChecker::setBeginMarker(char begin)
{
begin_mark = begin;
}
void SpellChecker::setEndMarker(char end)
{
end_mark = end;
}
char SpellChecker::getBeginMarker()
{
return begin_mark;
}
char SpellChecker::getEndMarker()
{
return end_mark;
}
string SpellChecker::fixUp(string sentence)
{
stringstream buff(sentence);
string result;
string tmp;
while (buff >> tmp)
{
string word;
for (int i = 0; i < tmp.size(); i++)
{
if (tmp[i] >= 'A' && tmp[i] <= 'Z')
word.push_back(tmp[i] - ('Z' - 'z'));
else
word.push_back(tmp[i]);
}
// if word is in correct words we just add it to result
if (valid_words.find(word) != valid_words.end()) result += word + ' '; else
:
#include"WordCounts.h"
#include<sstream>
#include<vector>
#include<algorithm>
using std::pair;
using std::vector;
using std::stringstream;
using std::sort;
void WordCounts::countWords(string sentence)
{
stringstream buff(sentence);
string tmp;
while (buff >> tmp)
{
string word;
for (int i = 0; i < tmp.size(); i++)
{
if (tmp[i] >= 'A' && tmp[i] <= 'Z')
word.push_back(tmp[i] - ('Z' - 'z'));
else
if (tmp[i] >= 'a' && tmp[i] <= 'z')
word.push_back(tmp[i]);
}
if (words.find(word) == words.end())
words.insert(pair<string, int>(word, 1));
else words[word]++;
}
}
int WordCounts::getCount(string word)
{
return words[word];
}
void WordCounts::resetCounts()
{
words.clear();
}
bool myfunction(pair<int, string> i, pair<int, string> j) { return (i>j); }
int WordCounts::mostCommon(string commonWords[], int wordCount[], int n)
{
// data stracture to sort words and count, by count
vector<pair<int, string> > v;
for (map<string, int>::iterator it = words.begin(); it != words.end(); it++)
{
v.push_back(pair<int, string>(it->second, it->first));
}
sort(v.begin(), v.end(), myfunction);
for (int i = 0; i < v.size(); i++)
{
if (i == n) return n;
wordCount[i] = v[i].first;
commonWords[i] = v[i].second;
}
return v.size();
}
Explanation / Answer
//modify your constructors as follows
SpellChecker::SpellChecker() {
this->language="English"; //initializing to some default values
this->begin_mark='~';
this->end_mark='~';
}
SpellChecker::SpellChecker(string lang) {
this->language=lang;;
this->begin_mark='~';
this->end_mark='~';
}
SpellChecker::SpellChecker(.............)
{
this->language=lang;;
this->begin_mark='~';
this->end_mark='~';
}
//add this constructor in wordcount class
WordCounts::WordCounts()
{
//add some data memebers like
// initialize your vector words here
// and any data memeber you whom you woul like to give some default values
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.