-
Notifications
You must be signed in to change notification settings - Fork 2
/
autocorrector.cpp
162 lines (131 loc) · 3.43 KB
/
autocorrector.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
#include <bits/stdc++.h>
using namespace std;
bool autocorrect;
vector<string> dictionary;
const string path = "qwerty/";
// Loading the dictionary in vector.
void LoadDictonary()
{
ifstream infile("large_dictionary.txt");
string line;
while (getline(infile, line))
{
istringstream iss(line);
dictionary.push_back(line);
}
sort(dictionary.begin(),dictionary.end());
}
// Binary search for searching words in huge dictionary
bool BinarySearch(string word)
{
if (binary_search(dictionary.begin(), dictionary.end(), word))
return true;
return false;
}
/*Editing the word by replacing the two levels character of qwerty keyboard,
adding character, rearranging them and deleting the characters.*/
set <string> WordEditing(string word)
{
set <string> words;
string edited_word;
// Adding a-z before every character
for (int i = 0; i < word.length() + 1; i++)
{
for (char c = 'a'; c <= 'z'; c++)
{
edited_word = word.substr(0, i) + c + word.substr(i);
words.insert(edited_word);
}
}
// Replacing every character by max 2 level of qwerty keyboard
for (int i = 0; i < word.length(); i++)
{
vector<char> replace;
string filename = path+word[i]+".txt";
ifstream infile(filename.c_str());
string line;
while (getline(infile, line))
{
istringstream iss(line);
replace.push_back(line[0]);
}
for(vector<char>::iterator it = replace.begin(); it != replace.end() ; it++)
{
edited_word = word.substr(0, i) + *it + word.substr(i + 1);
words.insert(edited_word);
}
replace.clear();
}
// Rearranging
for (int i = 0; i < word.length() - 1; i++)
{
edited_word = word.substr(0, i) + word[i + 1] + word[i] + word.substr(i + 2);
words.insert(edited_word);
}
// Deleting each character one by one
for (int i = 0; i < word.length(); i++)
{
edited_word = word.substr(0, i) + word.substr(i + 1);
words.insert(edited_word);
}
return words;
}
vector <string> AutoSuggestions(string word)
{
vector <string> corrected_word_list;
if(BinarySearch(word))
{
corrected_word_list.push_back(word);
return corrected_word_list;
}
autocorrect = true;
set <string> edit1 = WordEditing(word);
for (set<string>::iterator it = edit1.begin() ; it != edit1.end() ; it++)
{
if(BinarySearch(*it))
corrected_word_list.push_back(*it);
}
if (corrected_word_list.size() > 0)
return corrected_word_list;
corrected_word_list.clear();
for (set<string>::iterator it = edit1.begin() ; it != edit1.end() ; it++)
{
set <string> edit2 = WordEditing(*it);
for (set<string>::iterator it2 = edit1.begin() ; it2 != edit1.end() ; it2++)
{
if(BinarySearch(*it2))
corrected_word_list.push_back(*it2);
}
}
if (corrected_word_list.size() > 0)
return corrected_word_list;
autocorrect = false;
corrected_word_list.clear();
corrected_word_list.push_back(word);
return corrected_word_list;
}
void PrintOutput(string word)
{
vector<string> final_list = AutoSuggestions(word);
int i = 0 ;
for(vector<string>::iterator it = final_list.begin() ; i < 5 && it != final_list.end() ; i++, it++)
cout<<i+1<<". "<<*it<<endl;
}
int main(void)
{
// Buliding a dictionary from file
LoadDictonary();
int t;
// t is number of test cases.
cin>>t;
while(t--)
{
// Input the string
string word;
autocorrect = false;
cin>>word;
PrintOutput(word);
autocorrect ? cout<<"Autocorrected: Yes\n" : cout<<"Autocorrected: No\n";
cout<<endl;
}
}