summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Abed <michaelabed@gmail.com>2012-11-27 12:23:43 -0500
committerMichael Abed <michaelabed@gmail.com>2012-11-27 12:23:43 -0500
commita694a1414f58afb9f95bb503c456b46f87c6a1ab (patch)
tree6cfe1f4f93432751ef2806e4636c95b02c85e997
downloadec327-lab5-master.tar.gz
ec327-lab5-master.tar.bz2
ec327-lab5-master.zip
add lab 5HEADmaster
-rw-r--r--main.cpp171
1 files changed, 171 insertions, 0 deletions
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 <iostream>
+#include <vector>
+#include <string>
+#include <map>
+#include <algorithm>
+#include <fstream>
+#include <set>
+
+using namespace std;
+
+
+vector<string> *getfilenames()
+{
+ vector<string> *books = new vector<string>(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<string> *readDictionary()
+{
+ ifstream file;
+ set<string> *dict = new set<string>();
+ string word;
+ file.open("dictionary.txt");
+ while (file.good()) {
+ file >> word;
+ normalize(&word);
+ dict->insert(string(word));
+ }
+ file.close();
+ return dict;
+}
+
+map<string,int> *readfiles(vector<string> *books)
+{
+ map<string,int> *frequency = new map<string,int>();
+
+ ifstream file;
+ string curword;
+ vector<string>::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<string, int> *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<string,int> *freqs)
+{
+ int count = getUserCount();
+ map<string,int>::iterator it;
+ for (it = freqs->begin(); it != freqs->end(); it++)
+ if ((*it).second >= count)
+ cout << (*it).first << ": " << (*it).second << endl;
+}
+
+vector<string> *nonExistantWords(set<string> *keys, set<string> *dict)
+{
+ vector<string> *diff = new vector<string>();
+ set_difference(keys->begin(), keys->end(), dict->begin(), dict->end(),inserter(*diff, diff->begin()));
+ return diff;
+}
+
+void printUnrealWords(vector<string> *fakes)
+{
+ cout << "The following words were used in the books, but are not real" << endl;
+ vector<string>::iterator it;
+ for (it = fakes->begin(); it != fakes->end(); it++) {
+ cout << *it << endl;
+ }
+}
+
+set<string> *getKeys(map<string,int> *freqs)
+{
+ map<string,int>::iterator it;
+ set<string> *keys = new set<string>();
+ for (it = freqs->begin(); it != freqs->end(); it++) {
+ keys->insert((*it).first);
+ }
+ return keys;
+}
+
+int main(int argc, const char *argv[])
+{
+ set<string> *dict = readDictionary();
+ vector<string> *books = getfilenames();
+ map<string,int>::iterator it;
+ map<string,int> *freqs = readfiles(books);
+
+ int choice;
+ do {
+ choice = getUserChoice();
+ switch (choice) {
+ case 1:
+ examineSingleWord(freqs);
+ break;
+ case 2:
+ showWordsMoreFrequentThan(freqs);
+ break;
+ }
+ } while(choice != 3);
+
+ set<string> *keys = getKeys(freqs);
+ vector<string> *fakes = nonExistantWords(keys, dict);
+ printUnrealWords(fakes);
+ cout << "There are " << fakes->size() << " nonreal words" << endl;
+ return 0;
+}