/The language is c++,by using this psudo code, creat a function that will compute the probability of each word appearing in the text file. #include #include #include //SRP //Every program when become a process //A running program is a process std::vector tokenize(std::string& line) { std::vector toks; //...... Your code here //...... // ..... return toks; } std::vector read_file(std::string& filename) { std::vector lines; // ..... Your code here // ..... // ..... return lines; } // this is my car // ["this", "is", "my", "car"] void histgram(std::vector& tok, std::map& mp) { } std::map histgram(std::string& filename) { std::vector lines = read_file(filename); std::map mp; for (auto line : lines) { std::vector toks = tokenize(line); for (auto tok : toks) { mp[tok] += 1; } } return mp; } void print(std::map& m) { for (auto e : m) { std::cout << e.first << "Count:" << e.second << std::endl; } } std::map probability_distribution(std::map& mp) { } //Compute probability of each word. // total number of all words in the document //each word count/total number of words //6 face //1= 1/6, 2 = 1/6, 3=1/6 //1/6+1/6+1/6+1/6+1/6+1/6 = 1 //Probability is always between = 0 - 1 //classifier int main() { std::map mp = hitorgram("lab2.txt"); std::map wp = probability_distribution(mp); print(wp); return 0; } lab2.txt: this is a car it is my car I have a truck it runs and in good condition I have a bicycle too
//The language is c++,by using this psudo code, creat a function that will compute the probability of each word appearing in the text file.
#include <
#include <string>
#include <iostream>
//SRP
//Every program when become a process
//A running program is a process
std::vector<std::string> tokenize(std::string& line)
{
std::vector<std::string> toks;
//...... Your code here
//......
// .....
return toks;
}
std::vector<std::string> read_file(std::string& filename)
{
std::vector<std::string> lines;
// ..... Your code here
// .....
// .....
return lines;
}
// this is my car
// ["this", "is", "my", "car"]
void histgram(std::vector<std::string>& tok, std::map<std::string, int>& mp)
{
}
std::map<std::string, int> histgram(std::string& filename) {
std::vector<std::string> lines = read_file(filename);
std::map<std::string, int> mp;
for (auto line : lines) {
std::vector<std::string> toks = tokenize(line);
for (auto tok : toks) {
mp[tok] += 1;
}
}
return mp;
}
void print(std::map<std::string, int>& m)
{
for (auto e : m) {
std::cout << e.first << "Count:" << e.second << std::endl;
}
}
std::map<std::string, double> probability_distribution(std::map<std::string, int>& mp)
{
}
//Compute probability of each word.
// total number of all words in the document
//each word count/total number of words
//6 face
//1= 1/6, 2 = 1/6, 3=1/6
//1/6+1/6+1/6+1/6+1/6+1/6 = 1
//Probability is always between = 0 - 1
//classifier
int main() {
std::map<std::string, int> mp = hitorgram("lab2.txt");
std::map<std::string, double> wp = probability_distribution(mp);
print(wp);
return 0;
}
lab2.txt:
this is a car
it is my car
I have a truck
it runs and in good condition
I have a bicycle too
Trending now
This is a popular solution!
Step by step
Solved in 2 steps