-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
145 lines (116 loc) · 3.59 KB
/
main.cpp
File metadata and controls
145 lines (116 loc) · 3.59 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
#include <bits/stdc++.h>
using namespace std;
class SpellingCorrector
{
private:
typedef vector<string> Vector;
typedef map<string, int> Dictionary;
Dictionary dictionary;
void edits(const string& word, Vector& result);
void known(Vector& results, Dictionary& candidates);
public:
void load(const string& filename);
string correct(const string& word);
};
bool sortBySecond(const pair<string, int>& left, const pair<string, int>& right)
{
return left.second < right.second;
}
char filterNonAlphabetic(char& letter)
{
if (letter < 0)
return '-';
if (isalpha(letter))
return tolower(letter);
return '-';
}
void SpellingCorrector::load(const string& filename)
{
FILE *fp=fopen("big.txt","r");
char str[1000];
string ss;
while(fscanf(fp,"%s",str) != EOF)
{
char temp_buff[50];
int i,index = 0;
for(i = 0 ; str[i] ; ++i)
{
if(str[i] >= 'a' && str[i] <= 'z')
temp_buff[index++] = str[i];
else if(str[i] >= 'A' && str[i] <= 'Z')
temp_buff[index++] = str[i]+32;
}
temp_buff[index] = '\0';
ss=temp_buff;
dictionary[ss]++;
}
}
string SpellingCorrector::correct(const string& word)
{
Vector result;
Dictionary candidates;
if (dictionary.find(word) != dictionary.end()) { return word; }
edits(word, result);
known(result, candidates);
if (candidates.size() > 0) { return max_element(candidates.begin(), candidates.end(), sortBySecond)->first; }
for (unsigned int i = 0;i < result.size();i++)
{
Vector subResult;
edits(result[i], subResult);
known(subResult, candidates);
}
if (candidates.size() > 0) { return max_element(candidates.begin(), candidates.end(), sortBySecond)->first; }
return "";
}
void SpellingCorrector::known(Vector& results, Dictionary& candidates)
{
Dictionary::iterator end = dictionary.end();
for (unsigned int i = 0;i < results.size();i++)
{
Dictionary::iterator value = dictionary.find(results[i]);
if (value != end) candidates[value->first] = value->second;
}
}
void SpellingCorrector::edits(const string& word, Vector& result)
{
for (string::size_type i = 0;i < word.size(); i++) result.push_back(word.substr(0, i) + word.substr(i + 1)); //deletions
for (string::size_type i = 0;i < word.size() - 1;i++) result.push_back(word.substr(0, i) + word[i + 1] + word[i] + word.substr(i + 2)); //transposition
for (char j = 'a';j <= 'z';++j)
{
for (string::size_type i = 0;i < word.size(); i++) result.push_back(word.substr(0, i) + j + word.substr(i + 1)); //alterations
for (string::size_type i = 0;i < word.size() + 1;i++) result.push_back(word.substr(0, i) + j + word.substr(i)); //insertion
}
}
SpellingCorrector corrector;
void Correct(const string& wrong, const string& expected)
{
const bool isCorrect = corrector.correct(wrong) == expected;
cout << "(" << wrong << ") == (" << expected << ") = (" << boolalpha << isCorrect << ")" << endl;
}
int main()
{
corrector.load("big.txt");
/* Correct("speling", "spelling"); // insert
Correct("korrectud", "corrected"); // replace 2
Correct("bycycle", "bicycle"); // replace
Correct("inconvient", "inconvenient"); // insert 2
Correct("arrainged", "arranged"); // delete
Correct("peotry", "poetry"); // transpose
Correct("peotryy", "poetry"); // transpose + delete
Correct("word", "word"); // known
Correct("quintessential", ""); // unknown
*/
string request;
while (request != "quit")
{
cout << "Enter a word\n";
cin >> request;
string correct(corrector.correct(request));
if (correct != "")
cout << "You meant: " << correct << "?\n\n\n";
else
cout << "No correction suggestion\n\n\n";
}
cin.get();
return 0;
}