-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
484 lines (410 loc) · 15.2 KB
/
Copy pathmain.cpp
File metadata and controls
484 lines (410 loc) · 15.2 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
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <cctype>
#include <sstream>
using namespace std;
// BK-Tree Node
struct BKNode{
string word;
long long frequency;
bool isDeleted;
unordered_map<int, BKNode*> children;
BKNode(string w, long long freq): word(w), frequency(freq), isDeleted(false){}
};
// Stores the results for sorting later
struct SearchResult{
string word;
int distance;
long long frequency;
long long contextScore;
};
// Helper function to convert a string to lowercase
void toLowerCase(string& str){
for (int i = 0; i < str.length(); i++){
str[i] = tolower(str[i]);
}
}
// Helper function to check if a word is just a number
bool isNumeric(const string& str) {
if (str.empty()) return false;
for (int i = 0; i < str.length(); i++){
if (!isdigit(str[i])) return false;
}
return true;
}
// Levenshtein Distance algo
int calculateLevenshteinDistance(const string& s1, const string& s2){
int m = s1.length();
int n = s2.length();
if (m > 99 || n > 99) return 999;
int arr[100][100];
for (int i = 0; i <= m; i++){
for (int j = 0; j <= n; j++){
if (i == 0) {
arr[i][j] = j;
} else if (j ==0){
arr[i][j] = i;
} else if (s1[i-1] == s2[j- 1]){
arr[i][j] = arr[i -1][j -1];
} else {
arr[i][j] = 1 + min({
arr[i][j -1], // insert
arr[i-1][j], // remove
arr[i-1][j-1] // replace
});
}
}
}
return arr[m][n];
}
class BKTree {
private:
BKNode* root;
public:
BKTree() : root(nullptr){}
void insert(const string& word, long long frequency){
if (root == nullptr) {
root = new BKNode(word, frequency);
return;
}
BKNode* current = root;
while (true) {
int distance = calculateLevenshteinDistance(current->word, word);
// Ignore duplicate words or "undelete" if it was previously marked as deleted
if (distance == 0){
current->isDeleted = false;
return;
}
// If no child exists at this distance, add the new node here
if (current->children.find(distance) == current->children.end()){
current->children[distance] = new BKNode(word, frequency);
break;
} else {
// Move down the tree
current = current->children[distance];
}
}
}
bool deleteWord(const string& word){
if (root == nullptr) return false;
BKNode* current = root;
while (true){
int distance = calculateLevenshteinDistance(current->word, word);
// If we found the word, mark it as deleted
if (distance == 0){
if (current->isDeleted){
return false;
}
current->isDeleted = true;
return true;
}
// If no child exists at this distance, the word is not in the tree
if (current->children.find(distance) != current->children.end()){
current = current->children[distance];
} else {
return false;
}
}
}
// Recursive function to find suggestions within the tolerance range
void searchHelper(BKNode* node, const string& query, int tolerance, vector<SearchResult>& results){
if (node == nullptr) return;
int dist = calculateLevenshteinDistance(node->word, query);
if (dist <= tolerance && !node->isDeleted){
results.push_back({node->word, dist, node->frequency});
}
// Triangle Inequality constraints
int minBound = dist- tolerance;
int maxBound = dist+tolerance;
// Explore children within the bounds
for (auto const& pair : node->children){
int edgeWeight = pair.first;
BKNode* childNode = pair.second;
// Only explore this child if it could potentially have valid suggestions
if (edgeWeight >= minBound && edgeWeight <= maxBound){
searchHelper(childNode, query, tolerance, results);
}
}
}
vector<SearchResult> getSuggestions(const string& query, int tolerance){
vector<SearchResult> results;
searchHelper(root, query, tolerance, results);
return results;
}
};
long long stringToNumber(const string& str){
long long result = 0;
for (int i = 0; i < str.length(); i++){
if (isdigit(str[i])){
result = (result * 10) + (str[i] - '0');
}
}
return result;
}
void loadDictionary(const string& filename, BKTree& tree){
ifstream file(filename);
string line;
if (!file.is_open()){
return;
}
while (getline(file, line)){
// traversing the line to separate the word and its frequency
string wordStr = "";
int i = 0;
while (i < line.length() && line[i] != ' ' && line[i] != '\t'){
wordStr += line[i];
i++;
}
toLowerCase(wordStr);
// extracting only the last frequency
string count = "";
int j = line.length() - 1;
while (j >= 0 && line[j] != ' ' && line[j] != '\t'){
count = line[j] + count;
j--;
}
long long frequency = stringToNumber(count);
if (frequency > 0 && wordStr.length() > 0){
tree.insert(wordStr, frequency);
}
}
file.close();
}
void loadBigrams(const string& filename, unordered_map<string, unordered_map<string, long long>>& bigramMap){
ifstream file(filename);
string line;
if (!file.is_open()) return;
while (getline(file, line)){
stringstream ss(line);
string w1, w2, count;
// It reads the first chunk into w1, the second into w2, and the third into count.
if (ss >> w1 >> w2 >> count){
toLowerCase(w1);
toLowerCase(w2);
long long frequency = stringToNumber(count);
if (frequency > 0){
bigramMap[w1][w2] = frequency;
}
}
}
file.close();
}
// void loadBigrams(const string& filename, unordered_map<string, unordered_map<string, long long>>& bigramMap){
// ifstream file(filename);
// string line;
// if (!file.is_open()) return;
// while (getline(file, line)){
// string w1 = "", w2 = "", count = "";
// int wordState = 0; // 0 = writing w1, 1 = writing w2, 2 = writing the number
// for (int i = 0; i < line.length(); i++) {
// char c = line[i];
// if (c == ' ' || c == '\t') {
// if (wordState == 0 && w1.length() > 0) wordState = 1;
// else if (wordState == 1 && w2.length() > 0) wordState = 2;
// } else {
// if (wordState == 0) w1 += c;
// else if (wordState == 1) w2 += c;
// else count += c;
// }
// }
// toLowerCase(w1);
// toLowerCase(w2);
// long long frequency = stringToNumber(count);
// if (frequency > 0 && w1.length() > 0 && w2.length() > 0) {
// bigramMap[w1][w2] = frequency;
// }
// }
// file.close();
// }
// primary sort by edit distance, secondary by context score
bool rankSuggestions(const SearchResult& a, const SearchResult& b){
if (a.distance != b.distance) {
return a.distance < b.distance;
}
return a.contextScore > b.contextScore;
}
void application1(string userLine, BKTree& tree, unordered_map<string, unordered_map<string, long long>>& bigramMap, int tolerance) {
string originalSentence[100];
string correctedSentence[100];
int wordCount = 0;
bool typoDetected = false;
string currentWord = "";
// traverse the user input character by character to separate words and handle them one by one
for (int i = 0; i <= userLine.length() && wordCount < 100; i++){
if (i == userLine.length() || userLine[i] == ' ' || userLine[i] == '\t'){
if (currentWord.length() > 0){
originalSentence[wordCount] = currentWord;
// If the word is numeric, we skip spellchecking and directly add it to the corrected sentence
if (isNumeric(currentWord)){
correctedSentence[wordCount] = currentWord;
wordCount++;
currentWord = "";
continue;
}
string searchWord = currentWord;
toLowerCase(searchWord);
vector<SearchResult> suggestions = tree.getSuggestions(searchWord, tolerance);
string previousWord = "";
if (wordCount > 0) {
previousWord = correctedSentence[wordCount - 1];
toLowerCase(previousWord);
}
// Calculate context score for each suggestion
for (int s = 0; s < suggestions.size(); s++){
long long wordPopularity = suggestions[s].frequency;
long long pairPop = 0;
if (previousWord != "") {
pairPop = bigramMap[previousWord][suggestions[s].word];
}
suggestions[s].contextScore = wordPopularity + (pairPop * 50000000LL);
}
sort(suggestions.begin(), suggestions.end(), rankSuggestions);
// If the best suggestion is an exact match, keep the original word
if (suggestions.empty() || suggestions[0].distance == 0){
correctedSentence[wordCount] = currentWord;
}else{
// otherwise, we have a typo and we replace it with the best suggestion
typoDetected = true;
correctedSentence[wordCount] = suggestions[0].word;
}
wordCount++;
currentWord = "";
}
}else{
currentWord += userLine[i];
}
}
if (typoDetected){
cout << "Did you mean: \"";
for (int i = 0; i < wordCount; i++){
cout << correctedSentence[i] << (i == wordCount - 1 ? "" : " ");
}
cout << "\"?";
}else{
cout << "All words spelled correctly!";
}
}
//Add a new word to personal dictionary
void application2(string userWord, BKTree& tree){
if (isNumeric(userWord)){
cout << "ERROR_NUMERIC";
return;
}
toLowerCase(userWord);
vector<SearchResult> exactMatch = tree.getSuggestions(userWord, 0);
if (!exactMatch.empty()){
cout << "EXISTS";
}else{
tree.insert(userWord, 50000000);
// Append the new word to the personal dictionary file
ofstream outFile("personal_dict.txt", ios::app);
if (outFile.is_open()){
outFile << userWord << " " << 50000000 << "\n";
outFile.close();
cout << "ADDED";
}else{
cout << "ERROR_FILE";
}
}
}
void application3(string query, BKTree& tree, unordered_map<string, unordered_map<string, long long>>& bigramMap, int tolerance){
string currentWord = "";
string previousWord = "";
vector<string> words;
string temp = "";
for (char c : query){
if (c == ' ' || c == '\t'){
if (!temp.empty()){
words.push_back(temp);
temp = "";
}
}else{
temp += c;
}
}
if (!temp.empty()){
words.push_back(temp);
}
if (words.empty()) return;
currentWord = words.back();
toLowerCase(currentWord);
if (words.size() > 1) {
previousWord = words[words.size() - 2];
toLowerCase(previousWord);
}
vector<SearchResult> suggestions = tree.getSuggestions(currentWord, tolerance);
for (int s = 0; s < suggestions.size(); s++){
long long wordPopularity = suggestions[s].frequency;
long long pairPop = 0;
if (previousWord != ""){
pairPop = bigramMap[previousWord][suggestions[s].word];
}
suggestions[s].contextScore = wordPopularity + (pairPop * 50000000LL);
}
sort(suggestions.begin(), suggestions.end(), rankSuggestions);
if (suggestions.empty() || suggestions[0].distance == 0){
cout << "CORRECT";
}else{
int displayCount = min(5, (int)suggestions.size());
for (int i = 0; i < displayCount; i++){
cout << suggestions[i].word << (i == displayCount - 1 ? "" : ",");
}
}
}
void application4(string previousWord, unordered_map<string, unordered_map<string, long long>>& bigramMap){
toLowerCase(previousWord);
// If the previous word is not in the bigram map or has no next words, we can't make any predictions
if (bigramMap.find(previousWord) == bigramMap.end() || bigramMap[previousWord].empty()) {
cout << "NO_PREDICTIONS";
return;
}
// Create a vector of pairs to sort the next words by their bigram frequency
vector<pair<string, long long>> nextWords;
for (auto const& pair : bigramMap[previousWord]){
nextWords.push_back(pair);
}
// Sort the next words by frequency in descending order
sort(nextWords.begin(), nextWords.end(), [](const pair<string, long long>& a, const pair<string, long long>& b){
return a.second > b.second;
});
// Display the top 5 predictions
int displayCount = min(5, (int)nextWords.size());
for (int i = 0; i < displayCount; i++){
cout << nextWords[i].first << (i == displayCount - 1 ? "" : ",");
}
}
int main(int argc, char* argv[]){
if (argc <= 1) {
return 0;
}
BKTree spellCheckerTree;
loadDictionary("years-100k.txt", spellCheckerTree);
ifstream checkFile("personal_dict.txt");
if (checkFile.is_open()) {
checkFile.close();
loadDictionary("personal_dict.txt", spellCheckerTree);
}
unordered_map<string, unordered_map<string, long long>> bigramMap;
loadBigrams("count_2w.txt", bigramMap);
int tolerance = 2;
string mode = argv[1];
string query = "";
for (int i = 2; i < argc; i++){
query += argv[i];
if (i < argc - 1) query += " ";
}
if (mode == "1") {
application1(query, spellCheckerTree, bigramMap, tolerance);
} else if (mode == "2") {
application2(query, spellCheckerTree);
} else if (mode == "3") {
application3(query, spellCheckerTree, bigramMap, tolerance);
} else if (mode == "4") {
application4(query, bigramMap);
}
return 0;
}