From a694a1414f58afb9f95bb503c456b46f87c6a1ab Mon Sep 17 00:00:00 2001 From: Michael Abed Date: Tue, 27 Nov 2012 12:23:43 -0500 Subject: add lab 5 --- main.cpp | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 main.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..9f85fa2 --- /dev/null +++ b/main.cpp @@ -0,0 +1,171 @@ + +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + + +vector *getfilenames() +{ + vector *books = new vector(5); + cout << "Enter filenames for books (Q to quit)" << endl; + + string inp; + cout << ">> "; + for (cin >> inp; inp != "Q"; cin >> inp) { + cout << ">> "; + books->push_back(string(inp)); + } + return books; +} + +bool invalidchar(char x) {return x < 'a' || x > 'z';} +void normalize(string *st) +{ + transform(st->begin(), st->end(), st->begin(), ::tolower); + string::iterator end = remove_if(st->begin(), st->end(), invalidchar); + st->erase(end, st->end()); +} + +set *readDictionary() +{ + ifstream file; + set *dict = new set(); + string word; + file.open("dictionary.txt"); + while (file.good()) { + file >> word; + normalize(&word); + dict->insert(string(word)); + } + file.close(); + return dict; +} + +map *readfiles(vector *books) +{ + map *frequency = new map(); + + ifstream file; + string curword; + vector::iterator it; + for(it = books->begin(); it < books->end(); it++) { + file.open(it->c_str()); + while (file.good()) { + file >> curword; + normalize(&curword); + if (curword.size() != 0) + (*frequency)[string(curword)] += 1; + } + file.close(); + } + return frequency; +} + +int getUserChoice() +{ + int choice; + do { + cout << "Please pick an operation" << endl; + cout << "1: Show number of times a word occurs" << endl; + cout << "2: Show which words occur more than some number of times" << endl; + cout << "3: quit and show nonexistant words found in the books" << endl; + cout << ">> "; + cin >> choice; + } while(choice < 1 && choice >3); + return choice; +} + +string *getUserWord() +{ + string *input = new string(); + cout << "Enter a word: "; + cin >> *input; + normalize(input); + return input; +} + +void examineSingleWord(map *freqs) +{ + string *word = getUserWord(); + int count = (*freqs)[*word]; + cout << "The word \" " << *word << "\" occurs " << count << " times" << endl; + delete word; +} + +int getUserCount() +{ + int input; + do { + cout << "Enter a number: "; + cin >> input; + } while (input <= 0); + return input; +} + +void showWordsMoreFrequentThan(map *freqs) +{ + int count = getUserCount(); + map::iterator it; + for (it = freqs->begin(); it != freqs->end(); it++) + if ((*it).second >= count) + cout << (*it).first << ": " << (*it).second << endl; +} + +vector *nonExistantWords(set *keys, set *dict) +{ + vector *diff = new vector(); + set_difference(keys->begin(), keys->end(), dict->begin(), dict->end(),inserter(*diff, diff->begin())); + return diff; +} + +void printUnrealWords(vector *fakes) +{ + cout << "The following words were used in the books, but are not real" << endl; + vector::iterator it; + for (it = fakes->begin(); it != fakes->end(); it++) { + cout << *it << endl; + } +} + +set *getKeys(map *freqs) +{ + map::iterator it; + set *keys = new set(); + for (it = freqs->begin(); it != freqs->end(); it++) { + keys->insert((*it).first); + } + return keys; +} + +int main(int argc, const char *argv[]) +{ + set *dict = readDictionary(); + vector *books = getfilenames(); + map::iterator it; + map *freqs = readfiles(books); + + int choice; + do { + choice = getUserChoice(); + switch (choice) { + case 1: + examineSingleWord(freqs); + break; + case 2: + showWordsMoreFrequentThan(freqs); + break; + } + } while(choice != 3); + + set *keys = getKeys(freqs); + vector *fakes = nonExistantWords(keys, dict); + printUnrealWords(fakes); + cout << "There are " << fakes->size() << " nonreal words" << endl; + return 0; +} -- cgit v1.2.3-54-g00ecf