Question
Your task in this homework assignment is to create a `BagOfWords` class. In machine learning, one way to analysis text is to use a [Bag-of-words](https://en.wikipedia.org/wiki/Bag-of-words_model).
Your task in this homework assignment is to create a `BagOfWords` class.
In machine learning, one way to analysis text is to use a [Bag-of-words](https://en.wikipedia.org/wiki/Bag-of-words_model).
Our class will be creating this Bag-of-words that could then be used by a machine learning algorithm.
## Solution Specifications
Your solution to this problem must meet the following criteria.
1. Use the `map` container to store the word counts.
1. All words should be case-insensitive and not include any symbols.
1. Write the `BagOfWords` member functions:
```c++
void addWord(std::string);
std::string getTopWord();
int getUniqueWordCount();
int getWordCount(std::string word);
```
poem1.txt:
A Funny Computer Poem By unknown author
A computer was something on TV From a science fiction show of note A window was something you hated to clean And ram was the cousin of a goat.
Meg was the name of my girlfriend And gig was a job for the nights Now they all mean different things And that really mega bytes.
An application was for employment A program was a TV show A cursor used profanity A keyboard was a piano.
Memory was something that you lost with age A CD was a bank account And if you had a 3-inch Floppy You hoped nobody found out.
Compress was something you did to the garbage Not something you did to a file And if you unzipped anything in public You'd be in jail for a while.
Log on was adding wood to the fire Hard drive was a long trip on the road A mouse pad was where a mouse lived And a backup happened to your commode.
Cut you did with a pocket knife Paste you did with glue A web was a spider's home And a virus was the flu.
poem2.txt:
If By Rudyard Kipling
If you can keep your head when all about you Are losing theirs and blaming it on you; If you can trust yourself when all men doubt you, But make allowance for their doubting too: If you can wait and not be tired by waiting, Or, being lied about, don't deal in lies, Or being hated don't give way to hating, And yet don't look too good, nor talk too wise;
If you can dream - and not make dreams your master; If you can think - and not make thoughts your aim, If you can meet with Triumph and Disaster And treat those two impostors just the same:. If you can bear to hear the truth you've spoken Twisted by knaves to make a trap for fools, Or watch the things you gave your life to, broken, And stoop and build'em up with worn-out tools;
If you can make one heap of all your winnings And risk it on one turn of pitch-and-toss, And lose, and start again at your beginnings, And never breathe a word about your loss: If you can force your heart and nerve and sinew To serve your turn long after they are gone, And so hold on when there is nothing in you Except the Will which says to them: "Hold on!"
If you can talk with crowds and keep your virtue, Or walk with Kings - nor lose the common touch, If neither foes nor loving friends can hurt you, If all men count with you, but none too much: If you can fill the unforgiving minute With sixty seconds' worth of distance run, Yours is the Earth and everything that's in it, And - which is more - you'll be a Man, my son!
poem3.txt:
Still I Rise By Maya Angelou
You may write me down in history With your bitter, twisted lies, You may tread me in the very dirt But still, like dust, I'll rise.
Does my sassiness upset you? Why are you beset with gloom? 'Cause I walk like I've got oil wells Pumping in my living room.
Just like moons and like suns, With the certainty of tides, Just like hopes springing high, Still I'll rise.
Did you want to see me broken? Bowed head and lowered eyes? Shoulders falling down like teardrops. Weakened by my soulful cries.
Does my haughtiness offend you? Don't you take it awful hard 'Cause I laugh like I've got gold mines Diggin' in my own back yard.
You may shoot me with your words, You may cut me with your eyes, You may kill me with your hatefulness, But still, like air, I'll rise.
Does my sexiness upset you? Does it come as a surprise That I dance like I've got diamonds At the meeting of my thighs?
Out of the huts of history's shame I rise Up from a past that's rooted in pain I rise I'm a black ocean, leaping and wide, Welling and swelling I bear in the tide. Leaving behind nights of terror and fear I rise Into a daybreak that's wondrously clear I rise Bringing the gifts that my ancestors gave, I am the dream and the hope of the slave. I rise I rise I rise.
main.cpp:
#include "BagOfWords.h"
#include
#include
#include
int main() {
std::ifstream fin;
fin.open("poem1.txt");
if (!fin) {
std::cerr << "Unable to open file poem.txt";
exit(1); // call system to stop
}
BagOfWords bow;
std::string word;
while (fin >> word) {
if (word == "Source:") {
break;
}
bow.addWord(word);
}
// wc.printWordCount();
// std::cout << std::endl;
std::cout << "Unique words: " << bow.getUniqueWordCount() << std::endl;
std::cout << "Top word: " << bow.getTopWord() << std::endl;
std::cout << "Count for 'mouse': " << bow.getWordCount("mouse") << std::endl;
fin.close();
return 1;
}
edit BagOfWords.h:
#include
#include
class BagOfWords {
private:
std::map
//std::string normalizeWord(std::string);
public:
void addWord(std::string);
std::string getTopWord();
int getUniqueWordCount();
int getWordCount(std::string word);
//void printWordCount();
};
and creat a BagOfWords.cpp file
In c++ language.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started