Tags:
create new tag
view all tags

Emily Dinkenson Example

main.cpp

#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;
}

WordCount.h

#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

WordCount.cpp

#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);
    }
Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2018-04-19 - JimSkon
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2018 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback