#include <iostream> #include <sstream> #include <string> #include <vector> #include <fstream> #include <algorithm> #include "WordCount.h" using namespace std; const string EMILY = "EmilyDickinson1.txt"; // Get words, put into vector vector<string> getWords(string s); // Remove special characters, and make everything lower case string RemoveSpecials(string str); int findWord(vector<WordCount> words, string word); int main() { vector<WordCount> wordList; ifstream poems; vector<string> words; int wordCount = 0; poems.open(EMILY.c_str()); string s; while (!poems.fail() && !poems.eof()) { getline(poems, s); s = RemoveSpecials(s); words = getWords(s); for (int i = 0; i < words.size(); i++) { string word = words.at(i); //cout << "w:" << word << endl; int index = findWord(wordList,word); if (index >= 0) { wordList.at(index).inc(); } else { WordCount w = WordCount(word); wordList.push_back(w); } } } sort(wordList.begin(),wordList.end()); for (int i =0;i<wordList.size(); i++) { cout << wordList[i].getWord() << ":" << wordList[i].getCount() << endl; } } vector<string> getWords(string s) { istringstream iss(s); vector<string> words; do { string word; iss >> word; if (word.length() > 0) { words.push_back(word); //cout << "w:" << word << endl; } } while (iss); return words; } string RemoveSpecials(string str) { int i = 0, len = str.length(); while (i < len) { char c = str[i]; if (((c >= '0')&&(c <= '9')) || ((c >= 'A')&&(c <= 'Z')) || ((c >= 'a')&&(c <= 'z')) || c == '\'' || c == ' ') { if ((c >= 'A')&&(c <= 'Z')) str[i] += 32; //Assuming dictionary contains small letters only. ++i; } else { str.erase(i, 1); --len; } } return str; } int findWord(vector<WordCount> words, string word) { for (int i = 0; i < words.size(); i++) { if (word == words[i].getWord()) { return i; } } return -1; }
#include <string> using namespace std; #ifndef WORDCOUNT_H #define WORDCOUNT_H class WordCount { private: string word; int count; public: WordCount (); WordCount(string W); void inc(); int getCount(); string getWord(); friend bool operator<(WordCount wc1, WordCount wc2); }; #endif
#include "WordCount.h" WordCount::WordCount (){ word = ""; count = 0; } WordCount::WordCount(string w){ word = w; count = 1; } void WordCount::inc() { count++; } int WordCount::getCount(){ return count; } string WordCount::getWord(){ return word; } bool operator<(WordCount wc1, WordCount wc2) { return (wc1.count>wc2.count); }