-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpell_Checker.cpp
More file actions
303 lines (292 loc) · 9.28 KB
/
Spell_Checker.cpp
File metadata and controls
303 lines (292 loc) · 9.28 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
/*
Program : Spell Checker
Description : It is a spell checker program. It checks if the spelling of entered word is right or wrong.
if spelling is wrong then it gives the suggestions of correct spellings.
Author : Akshat Jain
Roll Number : 1918006
Email : akshat.kodia@gmail.com
github : github.com/akshatprogrammer
*/
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <ctype.h>
#include <conio.h>
using namespace std;
//lower case alphabets.
char lower_alpha[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
//function to show the correct spelling if arrangement of word is incorrect.
int incorrectArrangement(string input)
{
string line;
int found = 0;
ifstream words ("words.txt");
if (words)
{
while(getline(words,line))
{
string Xinput = input, Ninput, permutations, Tinput, Tline, Nline, Xline = line;
int len = Xinput.size(), flen = line.size();
if (len == flen)
{
for (int i=1; i<Xinput.length(); i++)
Ninput.push_back(input[i]);
for (int i = 1; i < flen; i++)
Nline.push_back(Xline[i]);
Xinput.resize(1);
Xline.resize(1);
sort(Nline.begin(),Nline.end());
sort(Ninput.begin(), Ninput.end());
Tinput = Xinput + Ninput;
Tline = Xline + Nline;
if (Tinput == Tline)
{
found = 1;
cout<<line<<endl;
break;
}
}
}
words.close();
}
else
{
cout<<endl<<"Unexpected error occurred......."<<endl;
}
return found;
}
//function to show correct spelling if exchanged character is present in the given word
int exchangedCharacters (string input)
{
string line, Xinput;
int found = 0;
ifstream words ("words.txt");
if (words)
{
while (getline(words,line))
{
int len = input.size(), flen = line.size();
if (len == flen)
{
for (int i = len-1; i >= 0 ; i--)
{
Xinput = input;
Xinput[i] = lower_alpha[0];
for (int j=0; j<26; j++)
{
if (Xinput == line)
{
found = 1;
cout<<line<<endl;
break;
}
Xinput[i] = lower_alpha[j];
}
if (found == 1 ) break;
else continue;
}
}
}
words.close();
}
else cout<<"\nUnexpected error occurred"<<endl;
return found;
}
//function to show correct spelling when there is a missing character in the given word.
int missingCharacter (string input)
{
string Xinput, line, Tinput, Tline, Xline;
int found = 0;
ifstream words ("words.txt");
if (words)
{
while (getline(words,line))
{
for (int i = 0; i < 26; i++)
{
int len = input.size(), flen = line.size();
Xinput = input;
Xline = line;
Xinput.resize(len+1,'a');
Xinput[len] = lower_alpha[i];
len = Xinput.size();
string Ninput, Nline;
if (len == flen)
{
for (int j = 1; j <=len; j++)
Ninput.push_back(Xinput[j]);
for (int j = 1; j <= flen; j++)
Nline.push_back(Xline[j]);
Xinput.resize(1);
Xline.resize(1);
sort(Nline.begin(),Nline.end());
sort(Ninput.begin(),Ninput.end());
Tinput = Xinput + Ninput;
Tline = Xline + Nline;
if (Tinput == Tline)
{
found = 1;
cout<<line<<endl;
break;
}
}
if (found == 1) break;
}
if (found == 1) break;
}
words.close();
}
else
{
cout<<"\nUnexpected error occurred\n";
}
return found;
}
//function to show correct spelling of there is an extra character in given word.
int extraCharacter (string input)
{
string Xinput, line, Ninput, Tinput;
int found = 0;
ifstream words ("words.txt");
if (words)
{
while (getline(words,line))
{
int len = input.size(), flen = line.size();
if ((len-1) == flen)
{
for (int i = 1; i < len; i++)
{
Xinput = input;
Xinput.erase(Xinput.begin()+i);
if (Xinput == line)
{
found = 1;
cout<<line<<endl;
break;
}
}
}
}
words.close();
}
else
{
cout<<"\nUnexpected error occurred\n";
}
return found;
}
//function to show right spelling when given word has wrong extra character and right one is missing.
int mixedExtraMissing (string input)
{
string Xinput, line, Xline;
int found = 0;
ifstream words ("words.txt");
if (words)
{
while (getline(words,line))
{
int len = input.size(), flen = line.size();
if (len == flen)
{
for (int i = 1; i < len; i++)
{
for (int j = 0; j < 26; j++)
{
Xinput = input; Xline = line;
Xinput.erase(Xinput.begin()+i);
Xinput.resize(len, 'a');
Xinput[len-1] = lower_alpha[j];
sort(Xinput.begin()+1,Xinput.end());
sort(Xline.begin()+1,Xline.end());
if (Xinput == Xline)
{
found = 1;
cout<<line<<endl;
break;
}
}
if (found == 1) break;
}
if (found == 1) break;
}
}
words.close();
}
return found;
}
int main()
{
cout << "\t\t\t\tWelcome to Spell Checker by Akshat"<< endl;
cout << "Press 1 to start -> ";
int choice,ch=1;
cin >> choice;
system("cls");
cout << "\t\t\t\tD E S C R I P T I O N\n";
cout << "\n\n\nIt is a spell checker program.\nIt checks if the spelling of entered word is right or wrong.\nif spelling is wrong then it gives the suggestions of correct spellings."<<endl;
cout << "\n\n\n\t\t\t\tAuthor : Akshat Jain\n\t\t\t\tRoll Number -> 1918006"<<endl;
cout << "\nPress 1 to start -> ";
cin >> choice;
system("cls");
getchar();
//cout << "We are ready for spell checking :)\n\n\n";
//system("cls");
while (ch) {
string input,line;
int len,flen,correct=0;
cout<<"Enter the word: ";
getchar();
getline(cin,input);
len = input.length();
for (int i=0; i < len; i++)
input[i] = tolower(input[i]);
ifstream words ("words.txt");
if(words)
{
while (getline(words,line))
{
flen = line.length();
if (len==flen)
{
if (line==input)
{
correct=1;
}
else continue;
}
else continue;
}
words.close();
if (correct==1)
{
cout<<endl<<"Spelling is correct"<<endl;
}
if (correct==0)
{
int missing = 0, extra = 0, mixed = 0, incorrect = 0, exchanged = 0;
cout<<endl<<"Spelling is wrong. Possible right spellings are given below:- "<<endl<<endl;
missing = missingCharacter(input);
extra = extraCharacter(input);
mixed = mixedExtraMissing(input);
incorrect = incorrectArrangement(input);
exchanged = exchangedCharacters(input);
if (missing == 0 && extra == 0 && mixed == 0 && incorrect == 0 && exchanged == 0)
{
cout<<endl<<"No such word exist"<<endl;
}
}
}
else
{
cout<<"Not able to open words.txt"<<endl;
}
cout<<endl<<"Press any key(number) to continue...and 0 to exit"<<endl<<endl;
cin >> ch;
system("cls");
//_getch();
}
cout << "\t\t\t\tT H A N K Y O U"<<endl;
cout << "\t\t\t\tCode by : Akshat Jain( Graphic Era University)";
return 0;
}