-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDictionary.cpp
301 lines (284 loc) · 12.2 KB
/
Dictionary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//Project 3 Main
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <chrono>
#include <thread>
#include <math.h>
#include "Dictionary.h"
using namespace std;
using namespace this_thread; // sleep_for, sleep_until
using namespace chrono; // nanoseconds, system_clock, seconds
//read dictionary and add to Trie
Trie* readFiletoTrie(Trie* root, vector<string>* ptrTrieList) {
ifstream file;
file.open("DictionaryFull.csv", ios::in);
string line, word, partOfSpeech, definition;
while (getline(file, line)) {
stringstream s(line); //entire line of csv
getline(s, word, ',');
word.erase(remove(word.begin(),word.end(),'\"'),word.end()); //word without quotations
getline(s, partOfSpeech, ',');
partOfSpeech.erase(remove(partOfSpeech.begin(),partOfSpeech.end(),'\"'),partOfSpeech.end()); //PoS without quotations
getline(s, definition);
definition.erase(remove(definition.begin(),definition.end(),'\"'),definition.end()); //definition without quotations
insertTrie(root, word, partOfSpeech, definition); //add to Trie
ptrTrieList->push_back(word); //add to vector
}
file.close();
return root;
}
//read dictionary and add to AVL AVL
Node* readFiletoAVL(Node* root) {
fstream file;
file.open("DictionaryFull.csv", ios::in);
string line, word, partOfSpeech, definition;
while (getline(file, line)) {
stringstream s(line);
getline(s, word, ',');
word.erase(remove(word.begin(),word.end(),'\"'),word.end());
getline(s, partOfSpeech, ',');
partOfSpeech.erase(remove(partOfSpeech.begin(),partOfSpeech.end(),'\"'),partOfSpeech.end());
getline(s, definition);
definition.erase(remove(definition.begin(),definition.end(),'\"'),definition.end());
root = insert(root, word, partOfSpeech, definition); //adds to AVL Tree
}
file.close();
return root;
}
//compares time between using Trie and AVL
void timeComp(microseconds timeTrie, microseconds timeAVL) {
float percentAVL = ((timeTrie.count()-timeAVL.count())/(float)timeTrie.count())*100.0;
float percentTrie = ((timeAVL.count()-timeTrie.count())/(float)timeAVL.count())*100.0;
if (timeTrie.count() > timeAVL.count()) {
cout << "The AVL Tree is " << percentAVL << "% faster." << endl;
} else {
cout << "The Trie is " << percentTrie << "% faster." << endl;
}
}
/*
spell checker submenu
runs continuously until \back
similar to definition finder
*/
void spellChecker(Node* root, string mode, vector<string>* ptrTrieList) {
string spellCheck = "";
while (spellCheck != "#Back" || spellCheck != "#back") {
cout << "\nWeclome to the Spell Checker Submenu!";
cout << "\n\tType '#Back' to return to the Main Menu";
cout << "\n\tEnter your input for spellchecking:" << endl;
cin >> spellCheck;
if (spellCheck == "#Back" || spellCheck == "#back") {
return;
}
string correction = spellCheck;
microseconds spellCheckDurationTrie, spellCheckDurationAVL;
if (mode == "Trie" || mode == "Comparison") {
auto start = high_resolution_clock::now();
correction = spellCheckTrie(spellCheck, ptrTrieList);
auto stop = high_resolution_clock::now();
spellCheckDurationTrie = duration_cast<microseconds>(stop - start);
}
if (mode == "AVL" || mode == "Comparison") {
auto start2 = high_resolution_clock::now();
correction = spellCheckAVL(root, spellCheck);
auto stop2 = high_resolution_clock::now();
spellCheckDurationAVL = duration_cast<microseconds>(stop2 - start2);
}
if (correction == spellCheck) {
cout << "Your input, '" << spellCheck << "', is spelled correctly." << endl;
}
else {
cout << "Correct spelling: " << correction << endl;
}
if (mode == "Trie" || mode == "Comparison") {
cout << "It took " << spellCheckDurationTrie.count() << " microseconds using Trie mode." << endl;
}
if (mode == "AVL" || mode == "Comparison") {
cout << "It took " << spellCheckDurationAVL.count() << " microseconds using AVL mode." << endl;
}
if (mode == "Comparison") {
timeComp(spellCheckDurationTrie, spellCheckDurationAVL);
}
}
return;
}
//spell checker without the submenu
//has different logic at the correction output stage?
void spellChecker(Node* root, string mode, string input, vector<string>* ptrTrieList) {
string correction = input;
microseconds spellCheckDurationTrie, spellCheckDurationAVL;
if (mode == "Trie" || mode == "Comparison") {
auto start = high_resolution_clock::now();
correction = spellCheckTrie(input, ptrTrieList);
auto stop = high_resolution_clock::now();
spellCheckDurationTrie = duration_cast<microseconds>(stop - start);
}
if (mode == "AVL" || mode == "Comparison") {
auto start2 = high_resolution_clock::now();
correction = spellCheckAVL(root, input);
auto stop2 = high_resolution_clock::now();
spellCheckDurationAVL = duration_cast<microseconds>(stop2 - start2);
}
cout << "Correct spelling: " << correction << endl;
if (mode == "Trie" || mode == "Comparison") {
cout << "It took " << spellCheckDurationTrie.count() << " microseconds using Trie mode." << endl;
}
if (mode == "AVL" || mode == "Comparison") {
cout << "It took " << spellCheckDurationAVL.count() << " microseconds using AVL mode." << endl;
}
if (mode == "Comparison") {
timeComp(spellCheckDurationTrie, spellCheckDurationAVL);
}
}
void definitionFinder(Node* root, Trie* root2, string mode, string input, vector<string>* ptrTrieList) {
string def = "DNE";
microseconds findDefDurationTrie, findDefDurationAVL;
if (mode == "Trie" || mode == "Comparison") {
auto start = high_resolution_clock::now();
def = findDefinitionTrie(root2, input); //**simple, returns definition
auto stop = high_resolution_clock::now();
findDefDurationTrie = duration_cast<microseconds>(stop - start);
}
if (mode == "AVL" || mode == "Comparison") {
//find using AVL *comparison will run both
auto start2 = high_resolution_clock::now();
def = findDefinitionAVL(root, input); //**simple, returns definition
auto stop2 = high_resolution_clock::now();
findDefDurationAVL = duration_cast<microseconds>(stop2 - start2);
}
if (def == "DNE") {//if word does not exist
char ans;
cout << "Your input, '" << input << "', was not found." << endl;
if (mode == "Trie" || mode == "Comparison") {
cout << "It took " << findDefDurationTrie.count() << " microseconds using Trie mode." << endl;
}
if (mode == "AVL" || mode == "Comparison") {
cout << "It took " << findDefDurationAVL.count() << " microseconds using AVL mode." << endl;
}
if (mode == "Comparison") {
timeComp(findDefDurationTrie, findDefDurationAVL);
}
cout << "Would you like to enter Spell Checker mode? (Y/N)" << endl;
cin >> ans;
if (ans == 'Y' || ans == 'y') {
spellChecker(root, mode, input, ptrTrieList);
}
}
else {//if word exists
cout << def << endl;
if (mode == "Trie" || mode == "Comparison") {
cout << "It took " << findDefDurationTrie.count() << " microseconds to find the defintion using Trie mode." << endl;
}
if (mode == "AVL" || mode == "Comparison") {
cout << "It took " << findDefDurationAVL.count() << " microseconds to find the defintion using AVL mode." << endl;
}
if (mode == "Comparison") {
timeComp(findDefDurationTrie, findDefDurationAVL);
}
}
}
int main() {
Node* rootAVL = NULL;
Trie* rootTrie = nullptr;
vector <string> trieList;
vector<string>* ptrTrieList = &trieList;
//reads file and adds to Data Structures, tracking time it takes
auto start = high_resolution_clock::now();
Trie* at = readFiletoTrie(rootTrie, ptrTrieList);
auto stop = high_resolution_clock::now();
auto TrieDuration = duration_cast<microseconds>(stop - start);
cout << TrieDuration.count()/1000000.0 << " seconds to create the Trie dictionary." << endl;
auto start2 = high_resolution_clock::now();
rootAVL = readFiletoAVL(rootAVL);
auto stop2 = high_resolution_clock::now();
auto AVLDuration = duration_cast<microseconds>(stop2 - start2);
cout << AVLDuration.count()/1000000.0 << " seconds to create the AVL dictionary." << endl;
timeComp(TrieDuration, AVLDuration);
string mode = "Trie";
string input = "";
while (input != "#Exit") {
cout << "\nWelcome to the Language Master's Dictionary and Spell Checker!";
cout << "\n\tCurrent Mode: " << mode;
cout << "\n\tType “#Help” for more information"; //#1
cout << "\n\tBegin typing to search for a word or definition:" << endl; //#8
cin >> input;
if (input == "#Help" || input == "#help") {
cout << "\nInstructions for using Dictionary and Spell Checker:";
cout << "\n\tType '[a word]' to check if it is a word and find the definition"; //#8
cout << "\n\t\tNote: You must capitalize the first letter of the searched word.";
cout << "\n\t\tIf you are searching for a suffix or prefix, make sure to add the appropriate hyphen.";
cout << "\n\tType '#SpellChecker' to enter words, sentences, or longer"; //#2
cout << "\n\tType '1' for Trie dictionary mode"; //#3
cout << "\n\tType '2' for AVL AVL dictionary mode"; //#4
cout << "\n\tType '3' for a comparison mode of Trie and AVL AVL Structures"; //#5
cout << "\n\tType '#Exit' to exit the program anytime" << endl; //#7
}
else if (input == "#SpellChecker" || input == "#spellchecker" || input == "#Spellchecker" || input == "#spellChecker") {
spellChecker(rootAVL, mode, ptrTrieList);//enter spell checker mode
}
else if (input == "1") {//change to Trie mode
if (mode == "Trie") {
cout << "It is already in Trie mode!" << endl;
}
else {
mode = "Trie";
cout << "Changing mode to Trie";
fflush(stdout);
sleep_for(milliseconds(500));
cout << ".";
fflush(stdout);
sleep_for(milliseconds(500));
cout << ".";
fflush(stdout);
sleep_for(milliseconds(500));
cout << ".\n";
}
}
else if (input == "2") {//change to AVL mode
if (mode == "AVL") {
cout << "It is already in AVL AVL mode!" << endl;
}
else {
mode = "AVL";
cout << "Changing mode to AVL";
fflush(stdout);
sleep_for(milliseconds(500));
cout << ".";
fflush(stdout);
sleep_for(milliseconds(500));
cout << ".";
fflush(stdout);
sleep_for(milliseconds(500));
cout << ".\n";
}
}
else if (input == "3") {//change to comparison mode
if (mode == "Comparison") {
cout << "It is already in Comparison mode!" << endl;
}
else {
mode = "Comparison";
cout << "Changing mode to Comparison";
fflush(stdout);
sleep_for(milliseconds(500));
cout << ".";
fflush(stdout);
sleep_for(milliseconds(500));
cout << ".";
fflush(stdout);
sleep_for(milliseconds(500));
cout << ".\n";
}
}
else if (input == "#Exit" || input == "#exit") {//exit program
return 0;
}
else {//finds definition
definitionFinder(rootAVL, at, mode, input, ptrTrieList);
}
}
return 0;
}