-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
347 lines (278 loc) · 10.7 KB
/
main.cpp
File metadata and controls
347 lines (278 loc) · 10.7 KB
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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <ctime>
#include "BTree.h"
#include "LinkedList.h"
using namespace std;
int main(int argc, const char * argv[]) {
bool debug = true;
if (argc != 4 && debug == false) {
cout << "Invalid input. Exiting program..." << endl;
return 0;
}
LinkedList lexiconLL;
LinkedList inputFile;
LinkedList removalFile;
BTree<3> lexiconB;
BTree<9> lexiconBM;
try {
// **** Input lexicon file into LinkedList, BTree<3>, and BTree<M> (M = 9) ****
ifstream inFile(argv[2], ios::in);
if (!inFile) {
cout << argv[0] << endl;
cout << "Input: " << argv[2] << endl;
string error = "Cannot open file. Exiting program...";
throw error;
}
//cout << "File opened successfully!" << endl;
while (!inFile.eof()) {
string str;
while (inFile >> str) {
transform(str.begin(), str.end(), str.begin(), ::tolower);
// Linked list
Node* n = new Node(str);
lexiconLL.add(n);
// BTree
lexiconB.insert(lexiconB.root, str);
// BTree of size M
lexiconBM.insert(lexiconBM.root, str);
}
}
inFile.close();
// ********* Input input file into LinkedList *********
ifstream inFile2(argv[1], ios::in);
if (!inFile2) {
cout << argv[0] << endl;
cout << "Input: " << argv[1] << endl;
string error = "Cannot open file. Exiting program...";
throw error;
}
//cout << "File opened successfully!" << endl;
while (!inFile2.eof()) {
string str;
while (inFile2 >> str) {
transform(str.begin(), str.end(), str.begin(), ::tolower);
if (ispunct(str[str.size()-1])) {
str = str.substr(0,str.size()-1);
}
// Linked list
Node* n = new Node(str);
inputFile.add(n);
}
}
inFile2.close();
// ********* Input removal file into LinkedList *********
ifstream inFile3(argv[3], ios::in);
if (!inFile3) {
cout << argv[0] << endl;
cout << "Input: " << argv[3] << endl;
string error = "Cannot open file. Exiting program...";
throw error;
}
//cout << "File opened successfully!" << endl;
while (!inFile3.eof()) {
string str;
while (inFile3 >> str) {
transform(str.begin(), str.end(), str.begin(), ::tolower);
if (ispunct(str[str.size()-1])) {
str = str.substr(0,str.size()-1);
}
// Linked list
Node* n = new Node(str);
removalFile.add(n);
}
}
inFile3.close();
} catch(string error) {
cout << error << endl;
return 0;
}
// ************** End input *****************
LinkedList lexiconLL1 = lexiconLL;
//1. Lexicon stored in Linked List
clock_t timeBegin1, timeEnd1;
double totalTime1 = 0.0;
timeBegin1 = clock();
int numberMisspelled = 0;
Node* ptr = inputFile.head;
while (ptr != NULL) {
string word = ptr->toString();
if (! (lexiconLL1.findAndMoveFront(word)) ) {
numberMisspelled++;
//cout << "Misspelling: " << word << endl;
}
ptr = ptr->next;
}
timeEnd1 = clock();
totalTime1 = (double)(timeEnd1 - timeBegin1)/CLOCKS_PER_SEC;
string highestFreqWord = "";
int highestFreqWordCount = 0;
ptr = lexiconLL1.head;
while (ptr != NULL) {
if (ptr->getFrequency() > highestFreqWordCount) {
highestFreqWord = ptr->getWord();
highestFreqWordCount = ptr->getFrequency();
}
ptr = ptr->next;
}
cout << "====================== Output #1 ==========================" << endl;
cout << "Total number of misspelled words: " << numberMisspelled << endl;
cout << "Runtime (approx): " << totalTime1<< endl;
cout << "Most frequently occuring word: " << highestFreqWord << endl;
cout << "Frequency of most common word: " << highestFreqWordCount << endl;
cout << "===========================================================" << endl;
cout << endl;
cout << endl;
//2. Lexicon stored in BTree
clock_t timeBegin2, timeEnd2;
double totalTime2 = 0.0;
timeBegin2 = clock();
int numberMisspelled2 = 0;
Node* ptr2 = inputFile.head;
while (ptr2 != NULL) {
string word = ptr2->toString();
if (! (lexiconB.search(lexiconB.root, word)) ) {
numberMisspelled2++;
//cout << "Misspelling: " << word << endl;
}
ptr2 = ptr2->next;
}
timeEnd2 = clock();
totalTime2 = (double)(timeEnd2 - timeBegin2)/CLOCKS_PER_SEC;
cout << "====================== Output #2 ==========================" << endl;
cout << "Total number of misspelled words: " << numberMisspelled2 << endl;
cout << "Runtime (approx): " << totalTime2 << endl;
cout << "Most frequently occuring word: " << highestFreqWord << endl;
cout << "Frequency of most common word: " << highestFreqWordCount << endl;
cout << "===========================================================" << endl;
cout << endl;
cout << endl;
//3. Lexicon stored in BTree (of determined size M)
clock_t timeBegin3, timeEnd3;
double totalTime3 = 0.0;
timeBegin3 = clock();
int numberMisspelled3 = 0;
Node* ptr3 = inputFile.head;
while (ptr3 != NULL) {
string word = ptr3->toString();
if (! (lexiconBM.search(lexiconBM.root, word)) ) {
numberMisspelled3++;
//cout << "Misspelling: " << word << endl;
}
ptr3 = ptr3->next;
}
timeEnd3 = clock();
totalTime3 = (double)(timeEnd3 - timeBegin3)/CLOCKS_PER_SEC;
cout << "====================== Output #3 ==========================" << endl;
cout << "Total number of misspelled words: " << numberMisspelled3 << endl;
cout << "Runtime (approx): " << totalTime3 << endl;
cout << "Most frequently occuring word: " << highestFreqWord << endl;
cout << "Frequency of most common word: " << highestFreqWordCount << endl;
cout << "===========================================================" << endl;
cout << endl;
cout << endl;
cout << "***********************************************************" << endl;
cout << "****************** Output after Removals ******************" << endl;
cout << "***********************************************************" << endl;
cout << endl;
cout << endl;
// Remove words in removalFile from lexiconLL, lexiconB, and lexiconBM
Node* removalPtr = removalFile.head;
while (removalPtr != NULL) {
string wordToRemove = removalPtr->getWord();
// Remove from lexiconLL
lexiconLL.remove(wordToRemove);
// Remove from lexiconB
lexiconB.remove(lexiconB.root, wordToRemove);
// Remove from lexiconBM
lexiconBM.remove(lexiconBM.root, wordToRemove);
removalPtr = removalPtr->next;
}
LinkedList lexiconLL2 = lexiconLL;
//1. Lexicon stored in Linked List
clock_t timeBegin4, timeEnd4;
double totalTime4 = 0.0;
timeBegin4 = clock();
int numberMisspelled4 = 0;
Node* ptr4 = inputFile.head;
while (ptr4 != NULL) {
string word = ptr4->toString();
if (! (lexiconLL2.findAndMoveFront(word)) ) {
numberMisspelled4++;
//cout << "Misspelling: " << word << endl;
}
ptr4 = ptr4->next;
}
timeEnd4 = clock();
totalTime4 = (double)(timeEnd4 - timeBegin4)/CLOCKS_PER_SEC;
string highestFreqWord2 = "";
int highestFreqWordCount2 = 0;
ptr4 = lexiconLL2.head;
while (ptr4 != NULL) {
if (ptr4->getFrequency() > highestFreqWordCount2) {
highestFreqWord2 = ptr4->getWord();
highestFreqWordCount2 = ptr4->getFrequency();
}
ptr4 = ptr4->next;
}
cout << "====================== Output #1 ==========================" << endl;
cout << "Total number of misspelled words: " << numberMisspelled4 << endl;
cout << "Runtime (approx): " << totalTime4 << endl;
cout << "Most frequently occuring word: " << highestFreqWord2 << endl;
cout << "Frequency of most common word: " << highestFreqWordCount2 << endl;
cout << "===========================================================" << endl;
cout << endl;
cout << endl;
//2. Lexicon stored in BTree
clock_t timeBegin5, timeEnd5;
double totalTime5 = 0.0;
timeBegin5 = clock();
int numberMisspelled5 = 0;
Node* ptr5 = inputFile.head;
while (ptr5 != NULL) {
string word = ptr5->toString();
if (! (lexiconB.search(lexiconB.root, word)) ) {
numberMisspelled5++;
//cout << "Misspelling: " << word << endl;
}
ptr5 = ptr5->next;
}
timeEnd5 = clock();
totalTime5 = (double)(timeEnd5 - timeBegin5)/CLOCKS_PER_SEC;
cout << "====================== Output #2 ==========================" << endl;
cout << "Total number of misspelled words: " << numberMisspelled5 << endl;
cout << "Runtime (approx): " << totalTime5 << endl;
cout << "Most frequently occuring word: " << highestFreqWord2 << endl;
cout << "Frequency of most common word: " << highestFreqWordCount2 << endl;
cout << "===========================================================" << endl;
cout << endl;
cout << endl;
//3. Lexicon stored in BTree (of determined size M)
clock_t timeBegin6, timeEnd6;
double totalTime6 = 0.0;
timeBegin6 = clock();
int numberMisspelled6 = 0;
Node* ptr6 = inputFile.head;
while (ptr6 != NULL) {
string word = ptr6->toString();
if (! (lexiconBM.search(lexiconBM.root, word)) ) {
numberMisspelled6++;
//cout << "Misspelling: " << word << endl;
}
ptr6 = ptr6->next;
}
timeEnd6 = clock();
totalTime6 = (double)(timeEnd6 - timeBegin6)/CLOCKS_PER_SEC;
cout << "====================== Output #3 ==========================" << endl;
cout << "Total number of misspelled words: " << numberMisspelled6 << endl;
cout << "Runtime (approx): " << totalTime6 << endl;
cout << "Most frequently occuring word: " << highestFreqWord2 << endl;
cout << "Frequency of most common word: " << highestFreqWordCount2 << endl;
cout << "===========================================================" << endl;
cout << endl;
cout << endl;
return 0;
}