-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSA_Project.cpp
executable file
·502 lines (414 loc) · 13.4 KB
/
DSA_Project.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#include "pch.h"
#include <iostream>
#include <algorithm>
#include <cctype>
#include <string>
#include <fstream>
#include <vector>
#include <time.h>
#include <iomanip>
#define BACK_LIMIT 4
using namespace std;
class Meaning {
private:
string def, example, type;
vector<string> synonyms;
public:
Meaning(string def, string example, string type, vector<string> synonyms) {
this->def = def;
this->type = type;
this->synonyms = synonyms;
this->example = example;
}
void display() {
cout << "Definition: " << def << endl;
cout << "Example: " << example << endl;
cout << "Type: " << type << endl;
cout << "Synonyms: ";
for (int i = 0; i < synonyms.size(); i++) {
cout << synonyms.at(i) << " ";
}
cout << endl;
}
};
class Meanings {
public:
vector<Meaning> meanings;
Meanings(vector<Meaning> meanings) {
this->meanings = meanings;
}
};
class HashTable {
private:
vector<Meanings> allWordsMeanings;
public:
void insert(Meanings meanings) {
allWordsMeanings.push_back(meanings);
}
Meanings get(int pos) {
return allWordsMeanings.at(pos);
}
void display(int pos) {
vector<Meaning> meanings = (get(pos)).meanings;
for (int i = 0; i < meanings.size(); i++) {
Meaning meaning = meanings.at(i);
cout << endl << "Meaning # " << (i + 1) << ", " << endl;
meaning.display();
}
}
int getSize() {
return allWordsMeanings.size();
}
};
class LinkedList {
private:
class ListNode {
public:
string val;
ListNode *next;
ListNode(string val, ListNode *next = NULL) {
this->val = val;
this->next = next;
}
};
ListNode *head = NULL;
int len = 0;
public:
// Insert at Head
void insertHead(string val) {
ListNode* newHead = new ListNode(val, head); // Make new head
head = newHead; // Reassign Head
len++; // Increase length
}
void insertAtEnd(string val) {
if (head) {
ListNode* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = new ListNode(val);
len++;
} else {
insertHead(val);
}
}
void displayList() {
// Display list until last node
if (head != NULL) {
ListNode* temp = head;
while (temp) {
cout << temp->val << " ";
temp = temp->next;
}
}
}
};
class Queue {
private:
vector<string> elems;
public:
void pushBack(string element) {
if (elems.size() >= BACK_LIMIT) {
elems.pop_back();
}
elems.push_back(element);
};
string popFront() {
string elem = elems.front();
elems.erase(elems.begin());
return elem; // Return top element and then decrement top index
};
bool isEmpty() {
return elems.empty(); // No Elements
};
void clear() {
elems.clear();
};
string front() {
return elems.front();
};
};
class Stack {
private:
vector<string> elems;
public:
void push(string element) {
if (elems.size() >= BACK_LIMIT) {
elems.erase(elems.begin());
}
elems.push_back(element);
};
string pop() {
string elem = elems.back();
elems.pop_back();
return elem; // Return top element and then decrement top index
};
bool isEmpty() {
return elems.empty(); // No Elements
};
void clear() {
elems.clear();
};
string top() {
return elems.back();
};
};
class Node {
public:
char key;
int ind = -1;
Node *left, *mid, *right;
Node(char key, Node *left = NULL, Node *mid = NULL, Node *right = NULL, int ing = -1) {
this->key = key;
this->ind = ind;
this->left = left;
this->mid = mid;
this->right = right;
}
};
class TernarySearchTree {
private:
vector<string> searchResults;
int totalWords = 0;
public:
Node* insert(Node *root, const char* word, int ind) {
if (root == NULL) {
root = new Node(word[0]);
}
if (*word < root->key) {
root->left = insert(root->left, word, ind);
} else if (*word == root->key) {
if (*(word + 1)) {
root->mid = insert(root->mid, word + 1, ind);
} else {
root->ind = ind;
totalWords++;
}
} else {
root->right = insert(root->right, word, ind);
}
return root;
}
void display(Node *root, char* word, int level, bool withPrefix, const char *prefix) {
if (root) {
display(root->left, word, level, withPrefix, prefix);
word[level] = root->key;
if (root->ind != -1) {
word[level + 1] = '\0';
if (withPrefix) {
searchResults.push_back(string(prefix) + string(word));
cout << string(prefix) + string(word) << endl;
} else {
cout << string(word) << endl;
searchResults.push_back(string(word));
}
}
display(root->mid, word, level + 1, withPrefix, prefix);
display(root->right, word, level, withPrefix, prefix);
}
}
int search(Node *root, const char *word) {
while (root) {
if (*word < root->key) {
root = root->left;
} else if (*word == root->key) {
if ((root->ind != -1) && *(word + 1) == '\0') {
return root->ind;
}
word++;
root = root->mid;
} else {
root = root->right;
}
}
return -1;
}
vector<string> prefixSearch(Node *root, const char *prefix, const char *original, int len) {
while (*prefix != '\0') {
if (*prefix < root->key) {
root = root->left;
} else if (*prefix == root->key) {
prefix++;
root = root->mid;
} else {
root = root->right;
}
}
searchResults.clear();
char word[50];
display(root, word, 0, true, original);
return searchResults;
}
int getCurrentTotalWords() {
return totalWords;
}
};
void showAutoCompletePrefixMenu(TernarySearchTree tst, Node *root) {
system("clear");
cout << "AS Dictionary" << endl << endl;
string word;
cout << "Prefix: ";
cin >> word;
std::transform(word.begin(), word.end(), word.begin(),
[](unsigned char c) { return std::tolower(c); });
vector<string> result = tst.prefixSearch(root, word.c_str(), word.c_str(), word.length());
cout << endl << endl;
system("pause");
}
void showSearchMenu(TernarySearchTree tst, Node *root, HashTable ht) {
system("clear");
cout << "AS Dictionary" << endl << endl;
string word;
cout << "Search For: ";
cin >> word;
std::transform(word.begin(), word.end(), word.begin(),
[](unsigned char c) { return std::tolower(c); });
int res = tst.search(root, word.c_str());
if (res != -1) {
cout << "Word found..." << endl;
cout << "Found Meaning(s):- " << endl;
ht.display(res);
} else {
cout << "Word not found...";
}
cout << endl << endl;
system("pause");
}
void showInsertWordMenu(TernarySearchTree tst, Node *root, HashTable ht) {
system("clear");
cout << "AS Dictionary" << endl << endl;
string word, definition, type, example, synonyms;
cout << "Word: ";
cin >> word;
cout << "Definition: ";
cin >> definition;
cout << "Type: ";
cin >> type;
cout << "Example: ";
cin >> example;
cout << "Synonyms (Add multiple separated by ','): ";
cin >> synonyms;
string synonymsDelimiter = ",";
vector<string> synonymsArr;
size_t pos = 0;
string token;
while ((pos = synonyms.find(synonymsDelimiter)) != string::npos) {
token = synonyms.substr(0, pos);
synonymsArr.push_back(token);
synonyms.erase(0, pos + synonymsDelimiter.length());
}
synonymsArr.push_back(synonyms);
Meaning meaning(definition, example, type, synonymsArr);
vector<Meaning> meaningsArr;
meaningsArr.push_back(meaning);
Meanings meanings(meaningsArr);
cout << endl << endl << "Inserting new word in current session..." << endl;
clock_t startTime = clock();
tst.insert(root, word.c_str(), tst.getCurrentTotalWords() + 1);
ht.insert(meanings);
clock_t endTime = clock();
cout << "Total " << ((endTime - startTime) / (double)CLOCKS_PER_SEC) << " seconds taken for insertion of data in TST and HashTable..." << endl;
cout << tst.getCurrentTotalWords() << endl;
cout << ht.getSize() << endl;
system("pause");
}
void showMainMenu(TernarySearchTree tst, Node *root, HashTable ht) {
while (true) {
system("clear");
cout << "AS Dictionary" << endl << endl;
cout << "1) Insert new word," << endl;
cout << "2) Search for word," << endl;
cout << "3) Autocomplete prefix search," << endl;
cout << "4) View search log," << endl << endl;
int inp;
cout << "Option (0 to Exit): ";
cin >> inp;
switch (inp) {
case 1:
showInsertWordMenu(tst, root, ht);
break;
case 2:
showSearchMenu(tst, root, ht);
break;
case 3:
showAutoCompletePrefixMenu(tst, root);
break;
case 4:
break;
default:
exit(0);
}
}
}
int main() {
TernarySearchTree tst;
Node *root = NULL;
HashTable wordMeanings;
string meaningsDelimiter = "=_=";
string meaningInfoDelimiter = "=>";
string synonymsDelimiter = ",";
ifstream wordInpFile;
ifstream meaningsInpFile;
wordInpFile.open("words.txt");
meaningsInpFile.open("meanings.txt");
char* words[108117];
string tempFileInp;
string tempFileMeaningsInp;
int i = 0;
cout << "AS Dictionary" << endl << endl;
cout << "Loading 108,117 words with their meanings from file..." << endl;
clock_t startTime = clock();
if (wordInpFile.is_open() && meaningsInpFile.is_open()) {
while (!wordInpFile.eof() && !meaningsInpFile.eof()) {
getline(wordInpFile, tempFileInp);
getline(meaningsInpFile, tempFileMeaningsInp);
std::transform(tempFileInp.begin(), tempFileInp.end(), tempFileInp.begin(),
[](unsigned char c) { return std::tolower(c); });
root = tst.insert(root, tempFileInp.c_str(), i);
string s = tempFileMeaningsInp;
size_t pos = 0;
string token;
vector<string> meaningsStrs;
while ((pos = s.find(meaningsDelimiter)) != string::npos) {
token = s.substr(0, pos);
meaningsStrs.push_back(token);
s.erase(0, pos + meaningsDelimiter.length());
}
meaningsStrs.push_back(s);
vector<Meaning> meaningsArr;
for (int i = 0; i < meaningsStrs.size(); i++) {
vector<string> meaningInfoStrs;
string meaningStr = meaningsStrs.at(i);
pos = 0;
while ((pos = meaningStr.find(meaningInfoDelimiter)) != string::npos) {
token = meaningStr.substr(0, pos);
meaningInfoStrs.push_back(token);
meaningStr.erase(0, pos + meaningInfoDelimiter.length());
}
meaningInfoStrs.push_back(meaningStr);
string synonymsStr = meaningInfoStrs.at(3);
vector<string> synonyms;
pos = 0;
while ((pos = synonymsStr.find(synonymsDelimiter)) != string::npos) {
token = synonymsStr.substr(0, pos);
synonyms.push_back(token);
synonymsStr.erase(0, pos + synonymsDelimiter.length());
}
synonyms.push_back(synonymsStr);
Meaning meaning(meaningInfoStrs.at(0), meaningInfoStrs.at(1), meaningInfoStrs.at(2), synonyms);
meaningsArr.push_back(meaning);
}
Meanings meanings(meaningsArr);
wordMeanings.insert(meanings);
i++;
}
}
wordInpFile.close();
meaningsInpFile.close();
clock_t endTime = clock();
cout << "Total " << ((endTime - startTime) / (double)CLOCKS_PER_SEC) << " seconds taken for insertion of data in TST and HashTable..." << endl;
system("pause");
showMainMenu(tst, root, wordMeanings);
system("pause");
return 0;
}