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

c++ n grams - map_to_string: returns a single string of the map<string,long>, wh

ID: 3863773 • Letter: C

Question

c++ n grams

- map_to_string: returns a single string of the map<string,long>, where each element is printed as string:long, comma separated elements. No comma at the end

•vector_to_string: returns a single string of the argument vector<pair<string,long>>,where each element is printed as string:long, comma

separated elements. No comma at the end! This vector is convenient for sorting, (can't sort a map)

•clean_string: for the provided string argument returns a new string where the only contents are alphabetic characters in lower case of the argument string

•generate_ngrams: for the provided argument string, generates all the n grams of a given nfor that string. Returns a vector<string>

of all the ngrams found (no counting at this point, just avector of ngrams, could have repeats.

input 1:           ( ouput 1: thisisatest)

1

This is a test!!!

input 2:           (output2: hel, ell, llo, low, owo, wor, orl, rld, )

input 3:               ( output3: a:3, b:2, c:1)

3

input 4:                  (output4:hin:2, his:1, ing:1, int:1, ist:1, nth:1, sth:1, thi:3)

Explanation / Answer

Given below are the functions asked in the question. Also a main() function is written to show the usage of the functions. Please do rate the answer if it helped. Thank you very much.

#include <iostream>
#include <vector>
//#include <ctype>
#include <map>
using namespace std;
string clean_string(string s);
string map_to_string(map<string, long> m);
string vector_to_string(vector<pair<string, long>> v);
vector<string> generate_ngrams(string s, size_t n);

int main()
{
string s1 = "This is a test!!!";
cout << "s1 = " << s1 << endl;
cout << "clean_string(s1) = " << clean_string(s1) << endl;
  
string s2 = "this thin thing!";
cout << "s2 = " << s2 << endl;
  
s2 = clean_string(s2);
cout << "clean_string(s2) = " << clean_string(s2) << endl;
  
vector<string> ng = generate_ngrams(s2, 3);
cout << "ngrams = " ;
  
for(int i = 0; i < ng.size(); i++)
cout << ng[i] << " ";
cout << endl;
  
map<string, long> mp;
  
for(int i = 0; i < ng.size(); i++)
mp[ng[i]] ++;
  
cout << "ngrams map = " << map_to_string(mp) << endl;
return 0;
}

//retain only alphabet and convert them to lower case
string clean_string(string s)
{
char ch;
string str = "";
for(int i = 0; i < s.length(); i++)
{
ch = tolower(s[i]);
if(isalpha(ch))
str += ch;
}
return str;
}
string map_to_string(map<string, long> m)
{
string str = "";
if(m.empty())
return str;
  
map<string,long>::iterator it = m.begin();
str = it->first + ":" + to_string(it->second);
it++;
while(it != m.end())
{
str += "," + it->first + ":" + to_string(it->second);
it++;
}
  
return str;

}
string vector_to_string(vector<pair<string, long>> v)
{
string str = "";
if(v.empty())
return str;
str = v[0].first + ":" + to_string(v[0].second);
for(int i = 1; i < v.size(); i++)
{
str += "," + v[i].first + ":" + to_string(v[i].second);
}
return str;
}
vector<string> generate_ngrams(string s, size_t n)
{
vector<string> ngrams;
int last = s.size() - n;
for(int i = 0; i <= last; i++)
ngrams.push_back(s.substr(i, n));
  
return ngrams;
}

output

s1 = This is a test!!!
clean_string(s1) = thisisatest
s2 = this thin thing!
clean_string(s2) = thisthinthing
ngrams = thi his ist sth thi hin int nth thi hin ing
ngrams map = hin:2,his:1,ing:1,int:1,ist:1,nth:1,sth:1,thi:3

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