-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOCR.cpp
400 lines (369 loc) · 13.2 KB
/
OCR.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include <iostream>
#include <sstream>
#include <fstream>
#include <cassert>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm> //for using sorting algorithm
#include <map>
#include <unordered_map>
#include <iterator>
#include <utility>
#include "editDistance.h"
#include "confusions.h"
// #include "newConfusions.h"
using namespace std;
/*
Legends:
lw = left_word
tp = train pairs
Cmap = Confusion Map
*/
/*
New findConfusions function
*/
vector<string> findNewConfusions(string ocr, string correct, vector<string>& vec){
vector<string> v;
size_t sz = ocr.size();
string ocrp = "";
size_t t = 0;
while(1){
string ocrn = "";
string correctn = "";
string s1 = ocr.substr(t,1), s2 = correct.substr(t,1);
//cout << "t = " << t << " " << sz << endl;
// deletion
if(s2 == " ") {
while(s1 != s2) {ocrn += s1; correctn += s2; t++; if(t >= sz) break; s1 = ocr.substr(t,1); s2 = correct.substr(t,1);}
if(ocrp != "") {
vec.push_back(removeSpaces(ocrp+ocrn) + " " + removeSpaces(ocrp+correctn));
v.push_back(removeSpaces(ocrp+ocrn) + " " + removeSpaces(ocrp+correctn));
}
else if(t < sz) {
vec.push_back(removeSpaces(ocrn + s1) + " " + removeSpaces(correctn + s1));
v.push_back(removeSpaces(ocrn + s1) + " " + removeSpaces(correctn + s1));
}
else {
vec.push_back(removeSpaces(ocrn) + " " + removeSpaces(correctn));
v.push_back(removeSpaces(ocrn) + " " + removeSpaces(correctn));
}
}
// addition
else if(s1 == " ") {
while(s1 != s2) {ocrn += s1; correctn += s2; t++; if(t >= sz) break; s1 = ocr.substr(t,1); s2 = correct.substr(t,1);}
if((ocrp != "")&&(isNonVowel(ocrp))) {
vec.push_back(removeSpaces(ocrp+ocrn) + " " + removeSpaces(ocrp+correctn));
v.push_back(removeSpaces(ocrp+ocrn) + " " + removeSpaces(ocrp+correctn));
}
else if(t < sz) {
vec.push_back(removeSpaces(ocrn + s1) + " " + removeSpaces(correctn + s1));
v.push_back(removeSpaces(ocrn + s1) + " " + removeSpaces(correctn + s1));
}
else {
vec.push_back(removeSpaces(ocrn) + " " + removeSpaces(correctn));
v.push_back(removeSpaces(ocrn) + " " + removeSpaces(correctn));
}
}
else if(s1 != s2) {
while(s1 != s2) {ocrn += s1; correctn += s2; t++; if(t >= sz) break; s1 = ocr.substr(t,1); s2 = correct.substr(t,1);}
if((ocrp != "")&&(isNonVowel(ocrp))) {
vec.push_back(removeSpaces(ocrp+ocrn) + " " + removeSpaces(ocrp+correctn));/*else if(t < sz) cout << "s " << ocrn + s1 << " " << correctn + s1<< endl;*/
v.push_back(removeSpaces(ocrp+ocrn) + " " + removeSpaces(ocrp+correctn));
}
else {
vec.push_back(removeSpaces(ocrn) + " " + removeSpaces(correctn));
v.push_back(removeSpaces(ocrn) + " " + removeSpaces(correctn));
}
} else t++;
ocrp = s1;
if(t >= sz) break;
}
return v;
}
void appendNewConfusionsPairs(string str1, string str2, vector<string>& vec, vector<string>& vec1){
str1 = "@" + toslp1(str1) + "#"; str2 = "@" + toslp1(str2) + "#";
string str3;
lcs(str1,str2,str3);
allignlcsnew(str1,str2,str3);
removeEndCommonSpaces(str1,str2);
vec1 = findNewConfusions(str1,str2,vec);
//return vec;
}
//function to load confusions
void loadNewConfusions(string& left_str, vector< pair <int, string> >& v_editDistance ,map< string, map<string, float> >& lw_ConfPmap){
vector<string> lw_ConfP, vec;
string str1, str2;
int edit_Distance;
str1 = left_str;
map<string, float> inner;
for( vector< pair <int, string> >::const_iterator eptr=v_editDistance.begin(); eptr!=v_editDistance.end(); eptr++)
{
edit_Distance = eptr->first;
str2 = eptr->second ;
appendNewConfusionsPairs((str1),(str2),lw_ConfP, vec);//toslp1
for(size_t t = 0; t< vec.size(); t++) {
lw_ConfPmap[str2][vec[t]] = 1 ;
}
// lw_ConfPmap.insert(make_pair(str2,inner));
// lw_ConfPmap[str2];
//cout << str1 << " " << str2 << endl;
//vec.clear();
}
//std::cout << "new confusions loop enetered " << '\n';
//loadvectomap(lw_ConfP,lw_ConfPmap);
}/*
void loadNewConfusions(string& left_str, vector< pair <int, string> >& v_editDistance ,map<string,int>& lw_ConfPmap){
vector<string> lw_ConfP;
string str1, str2;
int edit_Distance;
str1 = left_str;
for( vector< pair <int, string> >::const_iterator eptr=v_editDistance.begin(); eptr!=v_editDistance.end(); eptr++)
{
edit_Distance = eptr->first;
str2 = eptr->second ;
appendConfusionsPairs((str1),(str2),lw_ConfP);//toslp1
//cout << str1 << " " << str2 << endl;
//vec.clear();
}
//std::cout << "new confusions loop enetered " << '\n';
loadvectomap(lw_ConfP,lw_ConfPmap);
}
*/
/*
Funcition that computes total probabilityof the incorrect_word in recursion
*/
void computeProbMap(map< string, map<string, float> >& m1, std::map<string, float> finalMap) {
float prob = 1;
for (map< string, map<string, float> >::const_iterator i=m1.begin(); i!=m1.end(); i++) {
for( map<string,float>::const_iterator j=i->second.begin();j!=i->second.end(); j++){
prob *= m1[(i->first)][(j->first)];
}
finalMap[(i->first)] = prob ;
}
}
//function that takes dictionary and the left_word and return the top 10 edit distance as a vector
void load_editDistance(string fileName1, string& left_str,vector< pair <int, string> > v_editDistance)
{
string line, word;
ifstream dict(fileName1);
while (getline(dict, line)){
dict >> word;
v_editDistance.push_back( make_pair ( editDistance(word,left_str), word ) );
}
sort(v_editDistance.begin(), v_editDistance.end()); //sorting by edit distance
while (v_editDistance.size() > 10) { // top 10 edit distances only
v_editDistance.pop_back();
}
//std::cout << "I'm in vector function " << '\n';
}
/*
A Funtion that finds the corresponding probability for the confusions of the pairs taken from top 10 edit distances
with respect to the train pairs' confusions
*/
void loadProbMap(map<string,float>& tp_Cmap1, map< string, map<string, float> >& lw_map){
for (map< string, map<string, float> >::const_iterator eptr=lw_map.begin(); eptr!=lw_map.end(); eptr++) {
for( map<string,float>::const_iterator i=eptr->second.begin();i!=eptr->second.end(); i++){
for( map<string,float>::const_iterator j=tp_Cmap1.begin(); j!=tp_Cmap1.end(); j++ ) {
if ((j->first).compare((i->first)) == 0) {
lw_map[(eptr->first)][(i->first)] = (j->second);
//std::cout << (i->second) << '\n';
//std::cout << "/*-------------------------------- message */" << '\n';
//(i->second) = (j->second);
//((i->[second).second) = (j->second);
//for( map<string,float>::const_iterator eptr=i->second.begin();eptr!=i->second.end(); eptr++){
// (eptr->second) = (j->second);
}
}
}
}
}
void load_maxProb(map<string, float>& m1, map<string, float>& m2, map< float, map<string, string> >& fm){
float maxProb = 0, tempProb;
string localStr1, localStr2;
for (map<string, float>::const_iterator i=m1.begin(); i!=m1.end(); i++) {
for (map<string, float>::const_iterator j=m2.begin(); j!=m2.end(); j++) {
tempProb = (i->second)*(j->second);
if (tempProb > maxProb) {
maxProb = tempProb;
localStr1 = (i->first) ;
localStr2 = (j->first) ;
}else{
tempProb = 0;
}
}
}
//fm[localStr1][localStr2] = maxProb;
fm[maxProb][localStr1]=localStr2;
}
void compCorrectWord(map< float, map<string, string> >& m, string& cw){
float maxProb = 0, tempProb;
for (map<float, map<string, string> >::const_iterator a = m.begin(); a != m.end(); a++) {
for (map< std::string, std::string >::const_iterator b = (a->second).begin(); b != (a->second).end(); b++) {
tempProb = (a->first);
if (tempProb > maxProb) {
cw = (b->first) + (b->second);
}else{
tempProb = 0;
}
}
}
}
//Recursive function
string ocrword_to_correctword(string &incorrect_word, string fileName, map<string,float>& tp_Cmap)
{
vector< pair <int, string> > lv_editDistance, rv_editDistance;
string left_word, right_word, line, word, correctWord;
map< string, map<string, float> > lw_ConfPmap, rw_ConfPmap; // Main map can be accessed as
// mainMap[string1][string2] = "float Value";
map< float, map<string, string> > maxProbMap;
map<string, float> l_finalMap, r_finalMap;
int flag = 0;
ifstream dict(fileName);
if (dict.is_open()) {
while (getline(dict, line)){
dict >> word;
//std::cout << word << '\n';
if (incorrect_word.compare(word) == 0) flag = 1;
else flag =0;
}
// std::cout << flag << '\n';
if (flag == 1) { //if incorrect_word is in dictionary
return incorrect_word;
}else{
flag = 0;
for (size_t i = 0; i <= strlen(incorrect_word.c_str()); i++) {
left_word = incorrect_word.substr(0,i);
right_word = incorrect_word.substr(i, strlen(incorrect_word.c_str()));
// std::cout << left_word<< '+' << right_word << '=' + incorrect_word << '\n';
ifstream dict(fileName);
while (getline(dict, line)) {
dict >> word;
if ( left_word.compare(word) == 0 ) {
// std::cout << "I'm in left_word == word if " << '\n';
flag = 1;
// return a string to main
}else flag =0;
}
if (flag == 1) {
ocrword_to_correctword(right_word, fileName, tp_Cmap);
//std::cout << "/* message */" << '\n';
}else {
// for ledt word
load_editDistance(fileName, left_word, lv_editDistance);
loadNewConfusions(left_word, lv_editDistance, lw_ConfPmap);
loadProbMap(tp_Cmap, lw_ConfPmap);
computeProbMap(lw_ConfPmap, l_finalMap);
// for right word
load_editDistance(fileName, right_word, rv_editDistance);
loadNewConfusions(right_word, rv_editDistance, rw_ConfPmap);
loadProbMap(tp_Cmap, rw_ConfPmap);
computeProbMap(rw_ConfPmap, r_finalMap);
// std::cout << "/* message1 */" << '\n';
load_maxProb(l_finalMap, r_finalMap, maxProbMap);
}
}
// find the correct word from the max prob map
compCorrectWord(maxProbMap, correctWord);
}
}else{
std::cout << "file is not open" << '\n';
}
return correctWord;
}
float load_totalFreq(map<string, int>& m2){
float totalFreq;
for( map<string,int>::const_iterator eptr=m2.begin(); eptr!=m2.end(); eptr++)
{
totalFreq += (eptr->second);
}
return totalFreq;
}
void mapProbability (map<string,int>& m3, map<string, float>& m4, float totalFreq) {
for( map<string,int>::const_iterator eptr=m3.begin(); eptr!=m3.end(); eptr++)
{
m4[(eptr->first)]= (float)(eptr->second)/totalFreq;
}
//for( map<string,float>::const_iterator eptr=m4.begin(); eptr!=m4.end(); eptr++)
// {
// cout <<"(" << (eptr->first) << " " <<(eptr->second)<<")" << " ";
// }
}
//main function
int main ()
{
map<string, int> dict_map, confusion_map, sandhi_map, ConfPmap1;
map<string, float> mProb_dict, mProb_confusion;
string word, line;
int freq_word;
float totalFreqDict = 0, totalFreqConfusion= 0;
//dictionary file
std::ifstream dict("/Users/vaibhavagrawal/Desktop/OCR word correction/Data/Dict.txt");
if (dict.is_open())
{
while (getline(dict, line))
{
dict >> word;
//std::cout << word << '\n';
dict_map[word]++;
}
}
else{
std::cout << "file is not open" << endl;
}
totalFreqDict = load_totalFreq(dict_map);
//std::cout << totalFreqDict << '\n';
//std::cout << 1/totalFreqDict << '\n';
mapProbability(ConfPmap1, mProb_dict, totalFreqDict);
//for confusions.h file. Load confusions from train pairs
loadConfusions("/Users/vaibhavagrawal/Desktop/OCR word correction/Data/TrainPairs.txt",ConfPmap1);
totalFreqConfusion = load_totalFreq(ConfPmap1);
//std::cout << 1/totalFreqConfusion << '\n';
//printmapWFreq(ConfPmap1);
mapProbability(ConfPmap1, mProb_confusion, totalFreqConfusion);
string ocrWord, correctWord1; //Incorrect word in SLP1 format
ocrWord = "rAgeSbaram";
correctWord1 = ocrword_to_correctword(ocrWord, "Data/Dict.txt", mProb_confusion);
std::cout << correctWord1 << endl;
/*
confusion file
size_t totalFreq_confusion = 0;
std::ifstream confusion("confusion.txt");
if (confusion.is_open()) { //if file is open
while (getline(confusion, line))
{
confusion >> word;
confusion >> freq_word;
confusion_map[word] = freq_word ;
}
dict.close();
}
else{
std::cout << "file is not open" << '\n';
}
for( map<string,int>::const_iterator eptr=confusion_map.begin(); eptr!=confusion_map.end(); eptr++)
{
totalFreq += (eptr->second) ;
}
//sandhi file
size_t totalFreq_sandhi = 0;
std::ifstream sandhi("sandhi.txt");
if (sandhi.is_open()) { //if file is open
while (getline(sandhi, line))
{
sandhi >> word;
sandhi >> freq_word;
sandhi_map[word] = freq_word ;
}
dict.close();
}
else{ //if file is not open
std::cout << "file is not open" << '\n';
}
for( map<string,int>::const_iterator eptr=sandhi_map.begin(); eptr!=sandhi_map.end(); eptr++)
{
totalFreq += (eptr->second) ;
}
*/
return 0;
}