-
Notifications
You must be signed in to change notification settings - Fork 1
/
autocorrect.cpp
333 lines (321 loc) · 13 KB
/
autocorrect.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
#include "autocorrect.h"
#include <iostream>
#include <fstream>
#include <algorithm>
#include <set>
// constructor
AutoCorrect::AutoCorrect(const std::string &dictionary_filename) noexcept : dictionary_filename(dictionary_filename) { }
// setter method for the word to check
void AutoCorrect::setWord(const std::string &wrong_word) noexcept {
this->wrong_word = wrong_word;
}
// getter method for the word to check
std::string AutoCorrect::getWord() const noexcept {
return wrong_word;
}
// getter method for the name of the dictionary file we are using
std::string AutoCorrect::getDictionaryFilename() const noexcept {
return dictionary_filename;
}
// checks the spelling of the given word, returns true if the spelling is correct, false otherwise
bool AutoCorrect::checkSpelling() const noexcept {
std::ifstream dictionary(dictionary_filename);
for(std::string dict_word;dictionary >> dict_word;)
if(wrong_word.front() > dict_word.front())
continue;
else if(wrong_word.front() < dict_word.front()) {
dictionary.close();
return false;
}
else if(wrong_word == dict_word) {
dictionary.close();
return true;
}
dictionary.close();
return false;
}
// gives suggestions with different letter arrangements, returns true if any suggestions are found, false otherwise
// e.g., "cwro" is misspelled, so "crow" is a possible correct suggestion because the letters of "cwro" are disarranged
bool AutoCorrect::checkLetterArrangement() const noexcept {
bool found = false;
unsigned int word_count = 0;
std::ifstream dictionary(dictionary_filename);
std::cout << "*** Incorrect Arrangement ***\t:\t";
std::string wrong_word_copy = wrong_word;
sort(wrong_word_copy.begin() + 1, wrong_word_copy.end());
for(std::string dict_word;dictionary >> dict_word;)
if(wrong_word.front() > dict_word.front())
continue;
else if(wrong_word.front() < dict_word.front())
break;
else if(wrong_word.length() == dict_word.length()) {
std::string dict_word_copy = dict_word;
sort(dict_word_copy.begin() + 1, dict_word_copy.end());
if(wrong_word_copy == dict_word_copy) {
std::cout << dict_word << ", ";
found = true;
++word_count;
}
}
std::cout << "\b\b\t(" << word_count << ")" << std::endl;
dictionary.close();
return found;
}
// gives suggestions assuming the given word is including wrong letters in certain positions, returns true if any suggestions are found, false otherwise
// e.g., "cfotn" is misspelled, so "crown" or "clown" are 2 possible correct suggestions because "cfotn" needs 2 letters to be exchanged
bool AutoCorrect::checkExchangedLetters(const unsigned int &EXCHANGED) const noexcept {
switch(EXCHANGED) {
case 0:
return false;
case 1:
std::cout << "*** Exchanged Character ***\t:\t";
break;
default:
std::cout << "*** Exchanged " << EXCHANGED << " Characters ***\t:\t";
}
bool found = false;
unsigned int word_count = 0;
std::ifstream dictionary(dictionary_filename);
for(std::string dict_word;dictionary >> dict_word;)
if(wrong_word.front() > dict_word.front())
continue;
else if(wrong_word.front() < dict_word.front())
break;
else if(wrong_word.length() == dict_word.length()) {
unsigned int uncommon_letter_count = 0;
for(unsigned int i=1;i<wrong_word.length();++i)
if(wrong_word[i] != dict_word[i]) ++uncommon_letter_count;
if(uncommon_letter_count == EXCHANGED) {
std::cout << dict_word << ", ";
found = true;
++word_count;
}
}
std::cout << "\b\b\t(" << word_count << ")" << std::endl;
dictionary.close();
return found;
}
// gives suggestions assuming the given word is missing some letters, returns true if any suggestions are found, false otherwise
// e.g. "acomodation" is misspelled, so "accommodation" is a possible correct suggestion because "acomodation" is missing 2 letters (ac(c)om(m)odation)
bool AutoCorrect::checkMissingLetters(const unsigned int &MISSING) const noexcept {
switch(MISSING) {
case 0:
return false;
case 1:
std::cout << "*** Missing Character ***\t:\t";
break;
default:
std::cout << "*** Missing " << MISSING << " Characters ***\t:\t";
}
bool found = false;
unsigned int word_count = 0;
std::ifstream dictionary(dictionary_filename);
for(std::string dict_word;dictionary >> dict_word;)
if(wrong_word.front() > dict_word.front()) continue;
else if(wrong_word.front() < dict_word.front()) break;
else if(wrong_word.length() + MISSING == dict_word.length()) {
unsigned int missing_letter_count = 0;
auto wrong_letter = wrong_word.cbegin() + 1;
for(const auto &dict_letter : dict_word.substr(1))
if(*wrong_letter == dict_letter)
++wrong_letter;
else
++missing_letter_count;
if(wrong_letter == wrong_word.cend() && missing_letter_count == MISSING) {
std::cout << dict_word << ", ";
found = true;
++word_count;
}
}
std::cout << "\b\b\t(" << word_count << ")" << std::endl;
dictionary.close();
return found;
}
// gives suggestions assuming the given word has some extra letters, returns true if any suggestions are found, false otherwise
// e.g. "acccommodddation" is misspelled, so "accommodation" is a possible correct suggestion because "acccommodddation" has 3 extra letters (acc/c/ommod/dd/ation)
bool AutoCorrect::checkExtraLetters(const unsigned int &EXTRA) const noexcept {
switch(EXTRA) {
case 0:
return false;
case 1:
std::cout << "*** Extra Character ***\t\t:\t";
break;
default:
std::cout << "*** Extra " << EXTRA << " Characters ***\t:\t";
}
bool found = false;
unsigned int word_count = 0;
std::ifstream dictionary(dictionary_filename);
for(std::string dict_word;dictionary >> dict_word;)
if(wrong_word.front() > dict_word.front())
continue;
else if(wrong_word.front() < dict_word.front())
break;
else if(wrong_word.length() == dict_word.length() + EXTRA) {
unsigned int extra_letter_count = 0;
auto dict_letter = dict_word.cbegin() + 1;
for(const auto &wrong_letter : wrong_word.substr(1))
if(wrong_letter == *dict_letter)
++dict_letter;
else
++extra_letter_count;
if(dict_letter == dict_word.cend() && extra_letter_count == EXTRA) {
std::cout << dict_word << ", ";
found = true;
++word_count;
}
}
std::cout << "\b\b\t(" << word_count << ")" << std::endl;
dictionary.close();
return found;
}
// gives suggestions assuming the given word is missing some letters and has some extra letters, returns true if any suggestions are found, false otherwise
// e.g. "acomoddation" is misspelled, so "accommodation" is a possible correct suggestion because "acomoddation" is missing 2 letters (ac(c)om(m)oddation) and has 1 extra letter (accommod/d/ation)
// "acomoddation" => "ac(c)om(m)oddation" => "accommoddation" => "accommod/d/ation" => "accommodation"
bool AutoCorrect::checkMissingAndExtraLetters(const unsigned int &MISSING, const unsigned int &EXTRA) const noexcept {
if(!(MISSING && EXTRA))
return false;
std::cout << "*** " << MISSING << " Missing, " << EXTRA << " Extra Characters ***\t:\t";
bool found = false;
unsigned int word_count = 0;
std::ifstream dictionary(dictionary_filename);
for(std::string dict_word;dictionary >> dict_word;)
if(wrong_word.front() > dict_word.front())
continue;
else if(wrong_word.front() < dict_word.front())
break;
else if(wrong_word.length() + MISSING == dict_word.length() + EXTRA) {
unsigned int missing_letter_count = 0, extra_letter_count = 0;
auto dict_letter = dict_word.cbegin() + 1, wrong_letter = wrong_word.cbegin() + 1;
for(;wrong_letter != wrong_word.cend();++wrong_letter)
if(*wrong_letter == *dict_letter)
++dict_letter;
else if(dict_letter != dict_word.cend()) {
auto dict_letter_2 = dict_letter + 1;
for(;dict_letter_2 != dict_word.cend();++dict_letter_2)
if(*wrong_letter == *dict_letter_2) {
missing_letter_count += distance(dict_letter, dict_letter_2);
dict_letter = dict_letter_2 + 1;
break;
}
if(dict_letter_2 == dict_word.cend())
++extra_letter_count;
}
else
break;
extra_letter_count += distance(wrong_letter, wrong_word.cend());
missing_letter_count += distance(dict_letter, dict_word.cend());
if(missing_letter_count == MISSING && extra_letter_count == EXTRA) {
std::cout << dict_word << ", ";
found = true;
++word_count;
}
}
std::cout << "\b\b\t(" << word_count << ")" << std::endl;
dictionary.close();
return found;
}
// gives suggestions assuming the given word is missing some letters, has some extra letters, and also might be disarranged
// returns true if any suggestions are found, false otherwise
bool AutoCorrect::checkAll(const unsigned int &MISSING, const unsigned int &EXTRA, const bool &is_disarranged) const noexcept {
if(!(MISSING && EXTRA))
return false;
bool found = false;
unsigned int word_count = 0;
std::ifstream dictionary(dictionary_filename);
if(is_disarranged) {
std::cout << "*** Max " << MISSING << " Missing, " << EXTRA << " Extra, Disarranged Characters ***\t:\t";
std::string wrong_word_copy = wrong_word;
sort(wrong_word_copy.begin() + 1, wrong_word_copy.end());
for(std::string dict_word;dictionary >> dict_word;)
if(wrong_word.front() > dict_word.front())
continue;
else if(wrong_word.front() < dict_word.front())
break;
else if((wrong_word.length() <= dict_word.length() + EXTRA) && (wrong_word.length() + MISSING >= dict_word.length())) {
std::string dict_word_copy = dict_word;
sort(dict_word_copy.begin() + 1, dict_word_copy.end());
unsigned int missing_letter_count = 0, extra_letter_count = 0;
auto dict_letter = dict_word_copy.cbegin() + 1, wrong_letter = wrong_word_copy.cbegin() + 1;
for(;wrong_letter != wrong_word_copy.cend();++wrong_letter)
if(*wrong_letter == *dict_letter)
++dict_letter;
else if(dict_letter != dict_word_copy.cend()) {
auto dict_letter_2 = dict_letter + 1;
for(;dict_letter_2 != dict_word_copy.cend();++dict_letter_2)
if(*wrong_letter == *dict_letter_2) {
missing_letter_count += distance(dict_letter, dict_letter_2);
dict_letter = dict_letter_2 + 1;
break;
}
if(dict_letter_2 == dict_word_copy.cend())
++extra_letter_count;
}
else
break;
extra_letter_count += distance(wrong_letter, wrong_word_copy.cend());
missing_letter_count += distance(dict_letter, dict_word_copy.cend());
if(missing_letter_count <= MISSING && extra_letter_count <= EXTRA) {
std::cout << dict_word << ", ";
found = true;
++word_count;
}
}
}
else {
std::cout << "*** Max " << MISSING << " Missing, " << EXTRA << " Extra Characters ***\t:\t";
for(std::string dict_word;dictionary >> dict_word;)
if(wrong_word.front() > dict_word.front())
continue;
else if(wrong_word.front() < dict_word.front())
break;
else if((wrong_word.length() <= dict_word.length() + EXTRA) && (wrong_word.length() + MISSING >= dict_word.length())) {
unsigned int missing_letter_count = 0, extra_letter_count = 0;
auto dict_letter = dict_word.cbegin() + 1, wrong_letter = wrong_word.cbegin() + 1;
for(;wrong_letter != wrong_word.cend();++wrong_letter)
if(*wrong_letter == *dict_letter)
++dict_letter;
else if(dict_letter != dict_word.cend()) {
auto dict_letter_2 = dict_letter + 1;
for(;dict_letter_2 != dict_word.cend();++dict_letter_2)
if(*wrong_letter == *dict_letter_2) {
missing_letter_count += distance(dict_letter, dict_letter_2);
dict_letter = dict_letter_2 + 1;
break;
}
if(dict_letter_2 == dict_word.cend())
++extra_letter_count;
}
else
break;
extra_letter_count += distance(wrong_letter, wrong_word.cend());
missing_letter_count += distance(dict_letter, dict_word.cend());
if(missing_letter_count <= MISSING && extra_letter_count <= EXTRA) {
std::cout << dict_word << ", ";
found = true;
++word_count;
}
}
}
std::cout << "\b\b\t(" << word_count << ")" << std::endl;
dictionary.close();
return found;
}
// sorts the dictionary file according to alphabetical order, then writes it in a different file
void AutoCorrect::sortDictionary(const char *unsorted_dictionary_filename, const char *sorted_dictionary_filename) noexcept(false) {
try {
std::ifstream unsorted_dictionary(unsorted_dictionary_filename);
unsorted_dictionary.exceptions(std::ifstream::badbit);
std::set<std::string> sorted_words;
for(std::string dict_word; unsorted_dictionary >> dict_word; sorted_words.emplace(dict_word));
unsorted_dictionary.close();
std::ofstream sorted_dictionary(sorted_dictionary_filename);
sorted_dictionary.exceptions(std::ofstream::failbit | std::ofstream::badbit);
for(const auto &word : sorted_words)
sorted_dictionary << word << std::endl;
sorted_dictionary.close();
}
catch(const std::exception &e) {
std::cerr << "[!ERROR!]: " << e.what() << std::endl;
}
}