-
Notifications
You must be signed in to change notification settings - Fork 0
/
Autocorrect.cpp
116 lines (103 loc) · 2.34 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
#include <list>
#include <string>
#include <tuple>
#include <utility>
#include "Autocorrect.h"
using std::string, std::tuple, std::tie, std::list;
constexpr tuple<char,char> homophones[] = {
{'a', 'e'},
{'b', 'p'},
{'c', 'k'},
{'c', 's'},
{'d', 't'},
{'e', 'i'},
{'e', 'y'},
{'f', 'v'},
{'g', 'j'},
{'i', 'j'},
{'i', 'y'},
{'k', 'q'},
{'l', 'r'},
{'m', 'n'},
{'o', 'u'},
{'u', 'w'},
{'x', 'z'}
};
static list<string> findHomophonicWordsBy(string word, char a, char b) {
list<string> results;
size_t idx = word.find(a);
while (idx != string::npos) {
results.push_back(word.substr(0, idx) + b + word.substr(idx+1, string::npos));
idx = word.find(a, idx+1);
}
return results;
}
string Autocorrect::findLetterDoubledWords(string word) {
for (size_t i = 0; i < word.length()-1; ++i) {
if (word[i] == word[i+1]) {
string mod_word = word.substr(0, i) + word.substr(i+1, string::npos);
if (m_dict.lookup(mod_word)) {
return mod_word;
}
}
}
return "";
}
string Autocorrect::findDoubledroppedWords(string word) {
for (size_t i = 0; i < word.length(); ++i) {
string mod_word = word.substr(0, i) + word[i] + word.substr(i, string::npos);
if (m_dict.lookup(mod_word)) {
return mod_word;
}
}
return "";
}
string Autocorrect::findHomophonicWords(string word) {
for (auto t : homophones) {
char a, b;
tie(a, b) = t;
for (auto mod_word : findHomophonicWordsBy(word, a, b)) {
if (m_dict.lookup(mod_word)) {
return mod_word;
}
}
std::swap(a, b);
for (auto mod_word : findHomophonicWordsBy(word, a, b)) {
if (m_dict.lookup(mod_word)) {
return mod_word;
}
}
}
return "";
}
string Autocorrect::findSwapLetteredWords(string word) {
for (size_t i = 0; i < word.length()-1; ++i) {
string mod_word = word.substr(0, i) + word[i+1] + word[i] + word.substr(i+2, string::npos);
if (m_dict.lookup(mod_word)) {
return mod_word;
}
}
return "";
}
Autocorrect::Autocorrect(const Hashtable<string>& dict)
: m_dict(dict) {}
Autocorrect::~Autocorrect() {}
string Autocorrect::attemptAutocorrect(string word) {
auto res = findLetterDoubledWords(word);
if (res != "") {
return res;
}
res = findSwapLetteredWords(word);
if (res != "") {
return res;
}
res = findDoubledroppedWords(word);
if (res != "") {
return res;
}
res = findHomophonicWords(word);
if (res != "") {
return res;
}
return "";
}