forked from rohitsaluja22/OpenOCRCorrect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslpNPatternDict.h
More file actions
1497 lines (1286 loc) · 61.5 KB
/
slpNPatternDict.h
File metadata and controls
1497 lines (1286 loc) · 61.5 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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef SLPNPATTERNDICT_H
#define SLPNPATTERNDICT_H
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <map>
#include <unordered_map>
//#include <boost/serialization/map.hpp>
//#include <boost/serialization/vector.hpp>
//#include <boost/serialization/serialization.hpp>
//#include <boost/serialization/unordered_map.hpp>
//#include <boost/archive/text_iarchive.hpp>
//#include <boost/archive/text_oarchive.hpp>
//#include "toFromslp1.h"
using namespace std;
bool HinFlag = 0, SanFlag = 1;
string ReplaceString(string subject, const string& search, const string& replace)
{
size_t pos=0;
while((pos=subject.find(search, pos)) != string::npos)
{
subject.replace(pos, search.length(),replace);
pos+=1;
}
return subject;
}
string ReplaceStringRestricted(string subject, const string& search, const string& replace)
{
size_t pos=0;
char c;
string replace_new=replace+"a";
// replace_new=replace_new+m;
//cout<<"here "<<subject<<endl;
while((pos=subject.find(search, pos)) != string::npos)
{ if(pos == subject.size()) break;
//cout << subject<< " "<<pos <<" "<<subject.size()<< endl;
if( subject.size() == pos + 3) c=subject.at(pos+2); else c=subject.at(pos+3);
//CHANGE TO 3 IF EVERYTHING DOES NOT WORK
if(c=='A' || c=='i' || c=='I' || c=='u' || c=='U' || c=='f' || c=='F' || c=='x' || c=='X' || c=='e' || c=='E' || c=='o' || c=='O')
subject.replace(pos, search.length(),replace);
else
subject.replace(pos, search.length(),replace_new);
pos+=1;
}
//cout<<"here21 "<<subject<<endl;
return subject;
}
string toDev(string s)
{ //Hin:-
if (HinFlag){
string vowel_dn[]={"अ","आ","इ","ई","उ","ऊ","ऋ","ए","ऐ","ओ","औ","ऑ","ं","ः","ँ","ॅ"};
string vowel_dn_joiner[]={"","ा","ि","ी","ु","ू","ृ","े","ै","ो","ौ","ॉ"};
string vowel_slp1[]={"a","A","i","I","u","U","f","e","E","o","O","Z","M","H","*","~"};
string consonants_dn[]={"क","ख","ग","घ","ङ","च","छ","ज","झ","ञ","ट","ठ","ड","ढ","ण","त","थ","द","ध","न","प","फ","ब","भ","म","य","र","ल","व","श","ष","स","ह","क़","ख़","ग़","ज़","ड़","ढ़","ऩ","फ़","य़","ऱ","ळ"};
string consonants_dn_halanta[]={"क्","ख्","ग्","घ्","ङ्","च्","छ्","ज्","झ्","ञ्","ट्","ठ्","ड्","ढ्","ण्","त्","थ्","द्","ध्","न्","प्","फ्","ब्","भ्","म्","य्","र्","ल्","व्","श्","ष्","स्","ह्","क़्","ख़्","ग़्","ज़्","ड़्","ढ़्","ऩ्","फ़्","य़्","ऱ्","ळ्"};
string consonants_slp1[]={"k","K","g","G","N","c","C","j","J","Y","w","W","q","Q","R","t","T","d","D","n","p","P","b","B","m","y","r","l","v","S","z","s","h","@","#","$","F","x","X","%","^","L","V","&"};
string no_dn[]={"०","१","२","३","४","५","६","७","८","९","॥","।","–","—"};
string no_slp1[]={"0","1","2","3","4","5","6","7","8","9","||","|","-","-"};
for(int i=0;i<44;i++)
{
s=ReplaceString(s,consonants_slp1[i],consonants_dn_halanta[i]);
}
for(int i=0;i<12;i++)
{
s=ReplaceString(s,"्"+vowel_slp1[i],vowel_dn_joiner[i]);
}
for(int i=0;i<16;i++)
{
s=ReplaceString(s,vowel_slp1[i],vowel_dn[i]);
}
for(int i=0;i<13;i++)
{
s=ReplaceString(s,no_slp1[i],no_dn[i]);
}
}
else if(SanFlag ){
string vowel_dn[]={"अ","आ","इ","ई","उ","ऊ","ऋ","ॠ","ऌ","ॡ","ए","ऐ","ओ","औ","ं","ः","ँ","ᳲ","ᳳ","ऽ","ॐ"};
string vowel_dn_joiner[]={"","ा","ि","ी","ु","ू","ृ","ॄ","ॢ","ॣ","े","ै","ो","ौ"};
//string consonants_dn[]={"क","ख","ग","घ","ङ","च","छ","ज","झ","ञ","ट","ठ","ड","ढ","ण","त","थ","द","ध","न","प","फ","ब","भ","म","य","र","ल","व","श","ष","स","ह","ळ"};
string consonants_dn_halanta[]={"क्","ख्","ग्","घ्","ङ्","च्","छ्","ज्","झ्","ञ्","ट्","ठ्","ड्","ढ्","ण्","त्","थ्","द्","ध्","न्","प्","फ्","ब्","भ्","म्","य्","र्","ल्","व्","श्","ष्","स्","ह्","ळ्"};
string vowel_slp1[]={"a","A","i","I","u","U","f","F","x","X","e","E","o","O","M","H","~","Z","V","$","%"};
string consonants_slp1[]={"k","K","g","G","N","c","C","j","J","Y","w","W","q","Q","R","t","T","d","D","n","p","P","b","B","m","y","r","l","v","S","z","s","h","L"};
string numbers_etc_dn[]={"१","२","३","४","५","६","७","८","९","०","।","॥","॰","ऽ","’","‘","”","“","ॐ"};
string numbers_eng[]={"1","2","3","4","5","6","7","8","9","0","|","||","0","$","'","'","''","''","%"};
for(int i=0;i<34;i++)
{
s=ReplaceString(s,consonants_slp1[i],consonants_dn_halanta[i]);
}
//cout << "here1 " << s <<endl;
for(int i=0;i<14;i++)
{
s=ReplaceString(s,"्"+vowel_slp1[i],vowel_dn_joiner[i]);
}
//cout << "here2 " << s <<endl;
for(int i=0;i<21;i++)
{
s=ReplaceString(s,vowel_slp1[i],vowel_dn[i]);
}
//cout << "here3 " << s <<endl;
for(unsigned int i=0;i<(sizeof(numbers_etc_dn)/sizeof(numbers_etc_dn[0]));i++)
{
s=ReplaceString(s,numbers_eng[i],numbers_etc_dn[i]);
}
//cout << "here3 " << s <<endl;
//cout<<s<<endl;
}
return s;
}
string toslp1(string s)
{ //Hin:-
if (HinFlag){
string vowel_dn[]={"अ","आ","इ","ई","उ","ऊ","ऋ","ए","ऐ","ओ","औ","ऑ","ं","ः","ँ","ॅ"};
string vowel_dn_joiner[]={"ा","ि","ी","ु","ू","ृ","े","ै","ो","ौ","ॉ"};
string vowel_slp1[]={"a","A","i","I","u","U","f","e","E","o","O","Z","M","H","*","~"};
string consonants_dn[]={"क","ख","ग","घ","ङ","च","छ","ज","झ","ञ","ट","ठ","ड","ढ","ण","त","थ","द","ध","न","प","फ","ब","भ","म","य","र","ल","व","श","ष","स","ह","क़","ख़","ग़","ज़","ड़","ढ़","ऩ","फ़","य़","ऱ","ळ"};
string consonants_dn_halanta[]={"क्","ख्","ग्","घ्","ङ्","च्","छ्","ज्","झ्","ञ्","ट्","ठ्","ड्","ढ्","ण्","त्","थ्","द्","ध्","न्","प्","फ्","ब्","भ्","म्","य्","र्","ल्","व्","श्","ष्","स्","ह्","क़्","ख़्","ग़्","ज़्","ड़्","ढ़्","ऩ्","फ़्","य़्","ऱ्","ळ्"};
string consonants_slp1[]={"k","K","g","G","N","c","C","j","J","Y","w","W","q","Q","R","t","T","d","D","n","p","P","b","B","m","y","r","l","v","S","z","s","h","@","#","$","F","x","X","%","^","L","V","&"};
string no_dn[]={"०","१","२","३","४","५","६","७","८","९","।","॥","–","—"};
string no_slp1[]={"0","1","2","3","4","5","6","7","8","9","|","||","-","-"};
for(int i=0;i<44;i++)
{
s=ReplaceString(s,consonants_dn_halanta[i],consonants_slp1[i]);
}
for(int i=0;i<11;i++)
{
s=ReplaceString(s,vowel_dn_joiner[i],vowel_slp1[i+1]);
}
for(int i=0;i<16;i++)
{
s=ReplaceString(s,vowel_dn[i],vowel_slp1[i]);
}
for(int i=0;i<43;i++)
{
s=ReplaceStringRestricted(s,consonants_dn[i],consonants_slp1[i]);
}
for(int i=0;i<14;i++)
{
s=ReplaceString(s,no_dn[i],no_slp1[i]);
}
}
else if(SanFlag){
//San:-
string vowel_dn[]={"अ","आ","इ","ई","उ","ऊ","ऋ","ॠ","ऌ","ॡ","ए","ऐ","ओ","औ","ं","ः","ँ","ᳲ","ᳳ"};
string vowel_dn_joiner[]={"ा","ि","ी","ु","ू","ृ","ॄ","ॢ","ॣ","े","ै","ो","ौ"};
string consonants_dn[]={"क","ख","ग","घ","ङ","च","छ","ज","झ","ञ","ट","ठ","ड","ढ","ण","त","थ","द","ध","न","प","फ","ब","भ","म","य","र","ल","व","श","ष","स","ह","ळ"};
string consonants_dn_halanta[]={"क्","ख्","ग्","घ्","ङ्","च्","छ्","ज्","झ्","ञ्","ट्","ठ्","ड्","ढ्","ण्","त्","थ्","द्","ध्","न्","प्","फ्","ब्","भ्","म्","य्","र्","ल्","व्","श्","ष्","स्","ह्","ळ्"};
string vowel_slp1[]={"a","A","i","I","u","U","f","F","x","X","e","E","o","O","M","H","~","Z","V"};
string consonants_slp1[]={"k","K","g","G","N","c","C","j","J","Y","w","W","q","Q","R","t","T","d","D","n","p","P","b","B","m","y","r","l","v","S","z","s","h","L"};
//cout<<s<<endl;
size_t pos = 0;
while((pos=s.find("ॆ", pos)) != string::npos)
s.replace(pos, 3,"े");
pos = 0;
while((pos=s.find("ऎ", pos)) != string::npos)
s.replace(pos, 3,"एे");
size_t pos1 = 0;
while((pos1=s.find("ॊ", pos1)) != string::npos)
s.replace(pos1, 3,"ो");
//cout<<s<<endl;
pos1 = 0;
//cout<<s<<" "<<s.find("ऴ्", pos1)<<endl;
while((pos1=s.find("ऴ्", pos1)) != string::npos)//"ळ" is valid, dot below "ळ" is invalid char
{s.replace(pos1, 6,"ळ्"); }
//cout<<s<<endl;
pos1 = 0;
//cout<<s<<" "<<s.find("ऴ", pos1)<<endl;
while((pos1=s.find("ऴ", pos1)) != string::npos)//"ळ" is valid, dot below "ळ" is invalid char
{s.replace(pos1, 6,"ळ"); }
//cout<<s<<endl;
for(int i=0;i<34;i++)
{
s=ReplaceString(s,consonants_dn_halanta[i],consonants_slp1[i]);
}
for(int i=0;i<13;i++)
{
s=ReplaceString(s,vowel_dn_joiner[i],vowel_slp1[i+1]);
}
for(int i=0;i<19;i++)
{
s=ReplaceString(s,vowel_dn[i],vowel_slp1[i]);
}
//cout<<"here "<< s<<endl;
for(int i=0;i<34;i++)
{ //cout<< i << " " << consonants_dn[i] << " " << consonants_slp1[i] << endl;
s=ReplaceStringRestricted(s,consonants_dn[i],consonants_slp1[i]);
}
//cout<<"here1 "<< s<<endl;
//cout<<s<<"…"<<s.find("…", pos1)<<endl;
string numbers_etc_dn[]={"१","२","३","४","५","६","७","८","९","०","।","॥","॰","ऽ","’","‘","”","“","ॐ"};
string numbers_eng[]={"1","2","3","4","5","6","7","8","9","0","|","||","0","$","'","'","''","''","%"};
for(uint i=0;i<(sizeof(numbers_etc_dn)/sizeof(numbers_etc_dn[0]));i++)
{
s=ReplaceString(s,numbers_etc_dn[i],numbers_eng[i]);
}
} //ifSanflag over
return s;
}
void printmapinDev(map<string,int> m1){
/*std::ofstream ss1("ppi.bin");
boost::archive::text_oarchive oarch1(ss1);
oarch1 << PatWInst;*/
for( map<string,int>::const_iterator eptr=m1.begin(); eptr!=m1.end(); eptr++)
cout << toDev(eptr->first) << endl;
}
void printmap(map<string,int>& m1){
/*std::ofstream ss1("ppi.bin");
boost::archive::text_oarchive oarch1(ss1);
oarch1 << PatWInst;*/
for( map<string,int>::const_iterator eptr=m1.begin(); eptr!=m1.end(); eptr++)
cout << (eptr->first) << endl;
}
void printmapstrstr(map<string,string>& m1){
/*std::ofstream ss1("ppi.bin");
boost::archive::text_oarchive oarch1(ss1);
oarch1 << PatWInst;*/
for( map<string,string>::const_iterator eptr=m1.begin(); eptr!=m1.end(); eptr++)
cout << (eptr->first) << " " <<(eptr->second) << endl;
}
void printmapWFreq(map<string,int>& m1){
/*std::ofstream ss1("ppi.bin");
boost::archive::text_oarchive oarch1(ss1);
oarch1 << PatWInst;*/
for( map<string,int>::const_iterator eptr=m1.begin(); eptr!=m1.end(); eptr++)
cout << (eptr->first) << " " <<(eptr->second) << endl;
}
void loadCwordsPair(string wordL,string wordR, map<string, string>& CPair,map<string,int>& Dict,map<string,int>& PWords){
if ((Dict[wordL] ==0) && (PWords[wordL] == 0)) CPair[wordL] = wordR;
}
void loadCPair(string filename, map<string, string>& CPair,map<string,int>& Dict, map<string,int>& PWords){
ifstream myfile(filename);
if (myfile.is_open())
{ string str1, str2;
while (myfile >> str1 ) {myfile >> str2;
loadCwordsPair(toslp1(str1),toslp1(str2),CPair,Dict,PWords);}
cout <<"CPairs " << CPair.size() << " loaded" << endl;
} else cout <<"Error:" << filename << "CPair NOT there/loaded" << endl;
}
void loadMapNV(string fileName, map<string,int>& OCRWords, vector<string>& vec, string GBook){
ifstream myfile(fileName);
if (myfile.is_open())
{ string str1;
while (myfile >> str1 ) { str1 = toslp1(str1); OCRWords[str1] = OCRWords[str1] + 1;vec.push_back(str1);}
cout <<GBook <<" " <<OCRWords.size() <<" Words Loaded in mapNvector" << endl;
} else cout <<"Error:" << GBook<< "Words NOT Loaded in mapNvector" << endl;
}
int minIG(int a, int b){ if(a>b)return b; else return a;}
int maxIG(int a, int b){ if(a<b)return b; else return a;}
void loadMapPWords(vector<string>& vGBook,vector<string>& vIBook, map<string,int>& PWords){
int vGsz = vGBook.size(), vIsz = vIBook.size();
int win = vGsz - vIsz;
if(win<0) win = -1*win;
// search for a word(pre space, post space as well) in Indsenz within win sized window in GDocs and if found then add to PWords
for(int t = 0; t < vIsz;t++){
for(int t1 = maxIG(t-win,0); t1 < min(t+win,vGsz); t1++){
string s1 = vIBook[t]; //(vGBook[t1].find(s1) != string::npos) || (vGBook[t1] == s1)
if( (vGBook[t1].find(s1) != string::npos)|| (vGBook[t1].find(s1 + " ") != string::npos) || (vGBook[t1].find(" " + s1) != string::npos)) {PWords[s1]++; break;}
}
}
cout << PWords.size() << " words loaded in PWords" << endl;
}
string findDictEntries1(string s1, map<string, int>& m2, map<string, int>& m1, int size) { //unordered_
if((s1.size() == 0) || (s1 == "")) return "";
//cout << "s1 "<< s1 << endl;
//cout << s1 << endl;
//rAmaAnand
for(size_t j =0; j < s1.size() ; j++){ //- i +1 j= 0:8
for(size_t i = s1.size() - j; i > 0; i--){// j0 i = 9:1 rAmaAnand rAmaAnan rAmaAna.. , j1 8:2
string str = s1.substr(j,i); //&&((str.size() >= 3)|| ( (str.size()==2) && (str[1] != 'a') &&( size< 3) ) || ((str.size() ==1)&&( size< 2) ) ))
//cout << "str "<< str << endl;
if((m2[str]>0)||(m1[str]>0)) { //cout << "here "<< str << "L " << s1.substr(0,j) << "R " << s1.substr(j+i,s1.size()-i) << endl;
//colorFlag = !colorFlag;
//string strcolor = color[colorFlag];// << endl;
//cout << "str "<< str << endl;
return findDictEntries1(s1.substr(0,j),m2,m1,size) + "<font color=\'" + "green" + "\'>" + toDev(str) + "</font>" + findDictEntries1(s1.substr(j+i,s1.size()-i),m2,m1,size);
}
}
}
//cout << "here2 "<< str << "sd"<< endl;
return ("<font color=\'red\'>" + toDev(s1) + "</font>");
}
string findDictEntries(string s1, map<string, int>& m2, map<string, int>& m1, int size) { //unordered_
m2["rAma"]++;
m2["rAm"]++;
string s = findDictEntries1(s1,m2,m1, size);
string vowel_dn[]={"आ","इ","ई","उ","ऊ","ऋ","ॠ","ऌ","ॡ","ए","ऐ","ओ","औ"};
string vowel_dn_joiner[]={"ा","ि","ी","ु","ू","ृ","ॄ","ॢ","ॣ","े","ै","ो","ौ"};
//cout << s << endl;
size_t t1 = s.find("\'>");
string sL = s.substr(0,t1+3);
string sR = s.substr(t1+3,s.size()-t1-3);
//cout << sL << " " << sR << endl;
for(size_t t =0; t< 12; t++){
string tofind = "\'>" + vowel_dn[t];
while((sR.find(tofind) != string::npos)){
//cout << s << endl; cout << s.find(tofind) << endl;
sR.replace(sR.find(tofind),tofind.size(), "\'>" + vowel_dn_joiner[t]); //cout << s << endl;
}
}
return sL + sR;
}
bool hasM40PerAsci(string word1){
if(SanFlag||HinFlag){
string asc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
size_t sz = word1.size();
double cnt = 0;
for(size_t t =0; t < sz; t++){
if(asc.find(word1.substr(t,1)) != string::npos) cnt ++;
if (cnt/(0.4) >= sz) return 1;
}
}
return 0;
}
bool hasNoAsci(string word1){
string asc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
size_t sz = word1.size();
for(size_t t =0; t < sz; t++){
if(asc.find(word1.substr(t,1)) != string::npos) {return 0; }
}
return 1;
}
void loadMap(string fileName, map<string,int>& OCRWords, string GBook){
size_t szp = OCRWords.size();
ifstream myfile(fileName);
if (myfile.is_open())
{ string str1;
while (myfile >> str1 ) { if(!hasM40PerAsci(str1)) {str1 = toslp1(str1); /*cout <<str1<<endl;*/ OCRWords[str1] = OCRWords[str1] + 1;}}
cout << GBook <<" " <<OCRWords.size() - szp<<" Words Loaded" << endl;
} else cout <<"Error:" << GBook<< "Words NOT Loaded" << endl;
}
// ADDED FOR FEATYRE EXTRACTION
bool insertPatternstoMap(string str, map<string,int>& TPWordsP, size_t& count ,size_t& count6){ //map<string,int >& PWordsP
size_t sz = str.size();
if(sz == 0) return 1;
for(size_t i = 0; i < (sz+1); i++){
string s1 = str.substr(0,i);
if (s1.size()<9) count6++;
TPWordsP[s1]++; count++;//PWordsP[s1]++;
}
return insertPatternstoMap(str.substr(1,sz-1),TPWordsP,count,count6);//PWordsP
}
// ADDED FOR FEATYRE EXTRACTION
//loadPWordsPatternstoTrie(TPWordsP,PWordsP,PWords);
size_t loadDictPatternstoMap(map<string,int >& TPWordsP, map<string,int >& PWords,size_t& count6){ // arg1(strt from 0) ,map<string,int >& PWordsP
size_t count = 0,maxsiz = 0;
for( map<string,int >::const_iterator ptr=PWords.begin();
ptr!=PWords.end(); ptr++) {
insertPatternstoMap(ptr->first, TPWordsP,count,count6);
if (ptr->first.size() > maxsiz) maxsiz = ptr->first.size();
}
cout << count <<" patterns loaded " << endl;
//cout << "m2 size " << m2.size() << endl;
return maxsiz;
}
// ADDED FOR FEATYRE EXTRACTION
bool getNgramFeaturesinVect(string str,map<string,int>& Dict,vector<bool>& vb,vector<size_t>& vbf, size_t& count){
size_t sz = str.size();
//cout<<sz<<endl;
if(sz == 1) return 1;
for(size_t i = 2; i < (sz+1); i++){
string s1 = str.substr(0,i);
//cout<<s1<<endl;
if(s1.size() < 9){
if (Dict[s1]>0){vb.push_back(1); vbf.push_back(Dict[s1]);} else {vb.push_back(0); vbf.push_back(0);}
count++;
}
//cout << vb[count]<<endl;
}
return getNgramFeaturesinVect(str.substr(1,sz-1),Dict,vb,vbf, count);//PWordsP
}
// ADDED FOR FEATYRE EXTRACTION
//https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c
bool endsWith(const std::string& s, const std::string& suffix)
{
return s.size() >= suffix.size() &&
s.substr(s.size() - suffix.size()) == suffix;
}
// ADDED FOR FEATYRE EXTRACTION
std::vector<std::string> split(const std::string& s, const std::string& delimiter, const bool& removeEmptyEntries = false)
{
std::vector<std::string> tokens;
for (size_t start = 0, end; start < s.length(); start = end + delimiter.length())
{
size_t position = s.find(delimiter, start);
end = position != string::npos ? position : s.length();
std::string token = s.substr(start, end - start);
if (!removeEmptyEntries || !token.empty())
{
tokens.push_back(token);
}
}
if (!removeEmptyEntries &&
(s.empty() || endsWith(s, delimiter)))
{
tokens.push_back("");
}
return tokens;
}
//ADDED FOR ERROR DETECTION REPORT
bool searchS1inGVec(string s1,size_t iocrdone,vector<string>& gocr,size_t winig){
for(int t1 = maxIG(iocrdone-winig,0); t1 < min(iocrdone+winig,gocr.size()); t1++){
if (s1 == gocr[t1]) return 1;
}
return 0;
}
string findDictEntries(string s1, map<string, int>& m2, map<string, int>& m1) {//unordered_
if((s1.size() == 0) || (s1 == "")) return "";
//cout << s1 << endl;
for(size_t i = s1.size(); i > 0; i --){
for(size_t j =0; j < s1.size() - i +1; j++){
string str = s1.substr(j,i);
if((m1[str]>0) ) { //cout << "here "<< str << "L " << s1.substr(0,j) << "R " << s1.substr(j+i,s1.size()-i) << endl;
return findDictEntries(s1.substr(0,j),m2,m1) + "<font color=\'" + "green" + "\'>" + toDev(str) + "</font>" + findDictEntries(s1.substr(j+i,s1.size()-i),m2,m1);
} else if(m2[str]>4) {//cout << "here "<< str << "L " << s1.substr(0,j) << "R " << s1.substr(j+i,s1.size()-i) << endl;
return findDictEntries(s1.substr(0,j),m2,m1) + "<font color=\'" + "cyan" + "\'>" + toDev(str) + "</font>" + findDictEntries(s1.substr(j+i,s1.size()-i),m2,m1);
}
}
}
//cout << "here2 "<< str << "sd"<< endl;
return ("<font color=\'red\'>" + toDev(s1) + "</font>");//("#" + s1 + "#");//
}
void find_and_replace(string& source, string const& find, string const& replace)
{
for(string::size_type i = 0; (i = source.find(find, i)) != string::npos;)
{
source.replace(i, find.length(), replace);
i += replace.length();
}
}
string find_and_replace_oddInstancesblue(string source)
{
string find = "green";
string replace = "blue";
for(string::size_type i = 0; (i = source.find(find, i)) != string::npos;)
{
source.replace(i, find.length(), replace); //cout << source << endl;
if ( replace == "blue") replace = "green"; else replace = "blue";
i += replace.length();
}
return source;
}
string find_and_replace_oddInstancesorange(string source)
{
string find = "cyan";
string replace = "orange";
for(string::size_type i = 0; (i = source.find(find, i)) != string::npos;)
{
source.replace(i, find.length(), replace); //cout << source << endl;
if ( replace == "orange") replace = "cyan"; else replace = "orange";
i += replace.length();
}
return source;
}
// lcs code:-
/*
C++ implementation of
"A linear space algorithm for computing maximal common subsequences"
D. S. Hirschberg
http://portal.acm.org/citation.cfm?id=360861
See also: http://wordaligned.org/articles/longest-common-subsquence.html
*/
#include <algorithm>
#include <iterator>
#include <vector>
#include <iostream>
#include <cassert>
#include <string>
using namespace std;
void allignlcsnew(string& str1,string& str2,string& str3){
string str1new, str2new;
size_t t1 = 0;
size_t t2 =0;
size_t t3 =0;
string s1,s2,s3;
// while1
// a) addition if s1 == s3, and s2 differs add space to s1n, append t2 only so that we can reach close to s1 == s3 == s2
// b) deletion if s2 == s3, and s1 differs add space to s2n, append t1 only so that we can reach close to s2 == s3 == s1
// substitution if s1 != s2 != s3 s1n += s1, s2n += s2 append t1 and t2
// nothing if s1 == s2 == s3 s1n += s1, s2n += s2 append t1 and t2 and t3; if t3 == str3.size() Up3Flag = 1 break;
// if Up3Flag find which is smaller, add spaces in end of smaller smallstr = min(str1.size() - t1,str2.size() - t2);
while(1){
s1 = str1.substr(t1,1); s2 = str2.substr(t2,1); s3 = str3.substr(t3,1);
//a) add
//cout << t1 << " " << t2 << " " << t3<< endl;
//cout << s1 << " " << s2 << " " << s3<< endl;
if((s1 == s3) && (s2 != s3)) {str1new += " "; str2new += s2; t2++; /*cout << "a growing str1new " << str1new <<endl;*/ }
if((s2 == s3) && (s1 != s3)) {str2new += " "; str1new += s1; t1++; /*cout << "d growing str2new " << str2new <<endl;*/ } // b)
if((s2 != s3) && (s1 != s3)) {str1new += s1; str2new += s2; t1++; t2++; /*cout << "s growing str12new " << str1new<<" " <<str2new <<endl;*/ } //c)
if((s2 == s3) && (s1 == s3)) {str1new += s1; str2new += s2; t1++; t2++;t3++; /*cout << "n growing str12new " << str1new<<" " <<str2new <<endl;*/ if(t3 == str3.size()) {break;}} //d)
}
//cout << " " << str1new << " " << str2new << endl;
uint max = str1.size() ; if(max < str2.size() ) max = str2.size() ;
//cout << t1 << " " << t2 << " " << max<< endl;
while(1)
{ if((t1>=max) && (t2>=max)) break;
if(t1<str1.size()) s1 = str1.substr(t1,1); else s1 = " "; if(t2<str2.size()) s2 = str2.substr(t2,1); else s2 = " ";
//cout << t1 << " " << t2 << " " << max<< endl;
//cout << s1 << " " << s2 << " " << endl;
if(t1 < max) str1new += s1; else str1new += " ";
if(t2 < max) str2new += s2; else str2new += " ";
t1++; t2++;
}
str1 = str1new;
str2 = str2new;
}
typedef std::vector<int> lengths;
/*
The "members" type is used as a sparse set for LCS calculations.
Given a sequence, xs, and members, m, then
if m[i] is true, xs[i] is in the LCS.
*/
typedef std::vector<bool> members;
/*
Fill the LCS sequence from the members of a sequence, xs
x - an iterator into the sequence xs
xs_in_lcs - members of xs
lcs - an output results iterator
*/
template <typename it, typename ot>
void set_lcs(it x, members const & xs_in_lcs, ot lcs)
{
for (members::const_iterator xs_in = xs_in_lcs.begin();
xs_in != xs_in_lcs.end(); ++xs_in, ++x)
{
if (*xs_in)
{
*lcs++ = *x;
}
}
}
/*
Calculate LCS row lengths given iterator ranges into two sequences.
On completion, `lens` holds LCS lengths in the final row.
*/
template <typename it>
void lcs_lens(it xlo, it xhi, it ylo, it yhi, lengths & lens)
{
// Two rows of workspace.
// Careful! We need the 1 for the leftmost column.
lengths curr(1 + distance(ylo, yhi), 0);
lengths prev(curr);
for (it x = xlo; x != xhi; ++x)
{
swap(prev, curr);
int i = 0;
for (it y = ylo; y != yhi; ++y, ++i)
{
curr[i + 1] = *x == *y
? prev[i] + 1
: std::max(curr[i], prev[i + 1]);
}
}
swap(lens, curr);
}
/*
Recursive LCS calculation.
See Hirschberg for the theory!
This is a divide and conquer algorithm.
In the recursive case, we split the xrange in two.
Then, by calculating lengths of LCSes from the start and end
corners of the [xlo, xhi] x [ylo, yhi] grid, we determine where
the yrange should be split.
xo is the origin (element 0) of the xs sequence
xlo, xhi is the range of xs being processed
ylo, yhi is the range of ys being processed
Parameter xs_in_lcs holds the members of xs in the LCS.
*/
template <typename it>
void
calculate_lcs(it xo, it xlo, it xhi, it ylo, it yhi, members & xs_in_lcs)
{
unsigned const nx = distance(xlo, xhi);
if (nx == 0)
{
// empty range. all done
}
else if (nx == 1)
{
// single item in x range.
// If it's in the yrange, mark its position in the LCS
xs_in_lcs[distance(xo, xlo)] = find(ylo, yhi, *xlo) != yhi;
}
else
{
// split the xrange
it xmid = xlo + nx / 2;
// Find LCS lengths at xmid, working from both ends of the range
lengths ll_b, ll_e;
std::reverse_iterator<it> hix(xhi), midx(xmid), hiy(yhi), loy(ylo);
lcs_lens(xlo, xmid, ylo, yhi, ll_b);
lcs_lens(hix, midx, hiy, loy, ll_e);
// Find the optimal place to split the y range
lengths::const_reverse_iterator e = ll_e.rbegin();
int lmax = -1;
it y = ylo, ymid = ylo;
for (lengths::const_iterator b = ll_b.begin();
b != ll_b.end(); ++b, ++e)
{
if (*b + *e > lmax)
{
lmax = *b + *e;
ymid = y;
}
// Care needed here!
// ll_b and ll_e contain one more value than the range [ylo, yhi)
// As b and e range over dereferenceable values of ll_b and ll_e,
// y ranges over iterator positions [ylo, yhi] _including_ yhi.
// That's fine, y is used to split [ylo, yhi), we do not
// dereference it. However, y cannot go beyond ++yhi.
if (y != yhi)
{
++y;
}
}
// Split the range and recurse
calculate_lcs(xo, xlo, xmid, ylo, ymid, xs_in_lcs);
calculate_lcs(xo, xmid, xhi, ymid, yhi, xs_in_lcs);
}
}
// Calculate an LCS of (xs, ys), returning the result in an_lcs.
template <typename seq>
void lcs(seq const & xs, seq const & ys, seq & an_lcs)
{
members xs_in_lcs(xs.size(), false);
calculate_lcs(xs.begin(), xs.begin(), xs.end(),
ys.begin(), ys.end(), xs_in_lcs);
set_lcs(xs.begin(), xs_in_lcs, back_inserter(an_lcs));
}
bool isNonVowel(string ocrp){
string a = "aeiouAEIOU";
for(size_t t =0; t< a.size(); t ++){
if (ocrp == a.substr(t,1)) return 0;
}
return 1;
}
string removeSpaces(string input)
{ string output;
int length = input.length();
for (int i = 0; i < length; i++) {
if(input[i] != ' ') output+= input[i];
}
return output;
}
void findConfisions(string ocr, string correct, vector<string>& vec){
//vector<string> vec;
//ocrp = “”; correctp = “”;
// for( t = 0 t< s.size) // load ocrp correctp if t > 0 with substr(t-1,1)
//ocr == “”; correct = “”
// while(s1 != s2) ocr += s1; correct += s2; t++;// s1 s2 are chars from each word
// of ocrp == “”load ocrnext in ocrp wiht substr(t,1)
// subss add if ocr != “” :- // if ocrp is a non-vowel vec.push_back(ocrp+ocr, correctp+correct) else vec.push_back(ocr, correct)
//ocrp = s1; correctp = s2;
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)); else if(t < sz) vec.push_back(removeSpaces(ocrn + s1) + " " + removeSpaces(correctn + s1)); else vec.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)); else if(t < sz) vec.push_back(removeSpaces(ocrn + s1) + " " + removeSpaces(correctn + s1)); else vec.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;*/ else vec.push_back(removeSpaces(ocrn) + " " + removeSpaces(correctn));
} else t++;
ocrp = s1;
if(t >= sz) break;
}
//return vec;
}
void findConfisionsNindex(string ocr, string correct, vector<string>& vec, vector<int>& vind){
//vector<string> vec;
//ocrp = “”; correctp = “”;
// for( t = 0 t< s.size) // load ocrp correctp if t > 0 with substr(t-1,1)
//ocr == “”; correct = “”
// while(s1 != s2) ocr += s1; correct += s2; t++;// s1 s2 are chars from each word
// of ocrp == “”load ocrnext in ocrp wiht substr(t,1)
// subss add if ocr != “” :- // if ocrp is a non-vowel vec.push_back(ocrp+ocr, correctp+correct) else vec.push_back(ocr, correct)
//ocrp = s1; correctp = s2;
size_t sz = ocr.size();
string ocrp = "";
size_t t = 0;
int s1p;
while(1){
string ocrn ="";
string correctn ="";
string s1 = ocr.substr(t,1), s2 = correct.substr(t,1);
int s1t = t;
//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)); vind.push_back(s1p);}else if(t < sz) {vec.push_back(removeSpaces(ocrn + s1) + " " + removeSpaces(correctn + s1)); vind.push_back(s1t);} else {vec.push_back(removeSpaces(ocrn) + " " + removeSpaces(correctn)); vind.push_back(s1t);}
}
// 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)); vind.push_back(s1p);} else if(t < sz) {vec.push_back(removeSpaces(ocrn + s1) + " " + removeSpaces(correctn + s1)); vind.push_back(s1t);} else {vec.push_back(removeSpaces(ocrn) + " " + removeSpaces(correctn)); vind.push_back(s1t);}
}
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)); vind.push_back(s1p);}/*else if(t < sz) cout << "s " << ocrn + s1 << " " << correctn + s1<< endl;*/ else {vec.push_back(removeSpaces(ocrn) + " " + removeSpaces(correctn)); vind.push_back(s1t);}
} else {t++;}
ocrp = s1; s1p = s1t;
if(t >= sz) break;
}
//return vec;
}
void removeEndCommonSpaces(string& str1, string& str2){
size_t t = str1.size();
string s1 = str1.substr(t-1,1), s2 = str1.substr(t-1,1);
while((s1 == " ") && (s2 == " ")) {str1 = str1.substr(0,t-1); str2 = str2.substr(0,t-1); t = str1.size(); s1 = str1.substr(t-1,1), s2 = str1.substr(t-1,1);}
}
/*
vector<string> findConfisionsPair(string str1, string str2){
string str3;
lcs(str1,str2,str3);
allignlcsnew(str1,str2,str3);
removeEndCommonSpaces(str1,str2);
vector<string> vec = findConfisions(str1,str2);
return vec;
}*/
void appendConfusionsPairs(string str1, string str2, vector<string>& vec){
str1 = "@" + toslp1(str1) + "#"; str2 = "@" + toslp1(str2) + "#";
string str3;
lcs(str1,str2,str3);
allignlcsnew(str1,str2,str3);
removeEndCommonSpaces(str1,str2);
findConfisions(str1,str2,vec);
//return vec;
}
string appendConfusionsPairsNindex(string str1, string str2, vector<string>& vec, vector<int>& vecind){
str1 = "@" + toslp1(str1) + "#"; str2 = "@" + toslp1(str2) + "#";
string str3;
lcs(str1,str2,str3);
allignlcsnew(str1,str2,str3);
removeEndCommonSpaces(str1,str2);
findConfisionsNindex(str1,str2,vec,vecind);
return str1;
//return vec;
}
void loadvectomap(vector<string> ConfP, map<string,int>& ConfPmap){
for(size_t t = 0; t<ConfP.size(); t++) ConfPmap[ConfP[t]]++;
}
void printvecstr(vector<string> ConfP){
for(size_t t = 0; t<ConfP.size(); t++) cout << ConfP[t] << endl;
}
void printvecint(vector<int> ConfP){
for(size_t t = 0; t<ConfP.size(); t++) cout << ConfP[t] << endl;
}
//loadConfusions("1kECPairs",ConfPmap)
void loadConfusions(string fileName,map<string,int>& ConfPmap){
vector<string> ConfP;
string str1, str2;
ifstream myfile(fileName);
if (myfile.is_open())
{ while(myfile >> str1){
myfile >> str2;
appendConfusionsPairs((str1),(str2),ConfP);//toslp1
//cout << str1 << " " << str2 << endl;
//vec.clear();
} cout << ConfP.size() << " Confusions Loaded" << endl;} else cout <<"Error Confusions NOT Loaded" << endl;
loadvectomap(ConfP,ConfPmap);
}
void loadConfusions2(string fileName1,string fileName2,map<string,int>& ConfPmap){
vector<string> ConfP;
string str1, str2;
ifstream myfile(fileName1);
ifstream myfile2(fileName2);
if ((myfile.is_open()) && (myfile2.is_open()))
{ while(myfile >> str1){
myfile2 >> str2;
appendConfusionsPairs((str1),(str2),ConfP);//toslp1
//cout << str1 << " " << str2 << endl;
//vec.clear();
} cout <<ConfP.size() << " Confusions Loaded" << endl;} else cout <<"Error Confusions NOT Loaded" << endl;
loadvectomap(ConfP,ConfPmap);
}
void loadConfusionsFont(vector<string> fileName1,vector<string> fileName2,map<string,int>& ConfPmap){
vector<string> ConfP;
if (fileName1.size() > 0)
{ for(size_t i =0;i < fileName1.size(); i++ ){
appendConfusionsPairs(fileName1[i],fileName2[i],ConfP);//toslp1
} cout << ConfP.size() << " Confusions Loaded from" << fileName1.size() <<" pairs"<< endl;} else cout <<"Error Confusions NOT Loaded" << endl;
loadvectomap(ConfP,ConfPmap);
}
void generateCorrectionPairs(vector<string> &wrong,vector<string> &right,string localFilenameI,string localFilenameC){
vector<string> vecpI, vecpC;
std::ifstream sIpage(localFilenameI);
if (!(sIpage.is_open())) {cout << "cannot open inds/corrected file" <<endl; return;} // break the while loop for page_no
string localstr;
map<string, bool> isEngOrNOT;
while(sIpage >> localstr) vecpI.push_back(toslp1(localstr)); sIpage.close();
std::ifstream sCpage(localFilenameC);
while(sCpage >> localstr) { vecpC.push_back(toslp1(localstr)); sIpage.close(); if(hasM40PerAsci(localstr)) isEngOrNOT[toslp1(localstr)] = 1;}
int sizew = wrong.size();
// if 1st word is wrong generate suggestions
int vGsz = vecpC.size(), vIsz = vecpI.size();
//if (vGsz >vIsz ) mapTyping[page_no] = vGsz - vIsz;
//cout << vGsz << " " << vIsz << endl;
int win = vGsz - vIsz;
if(win<0) win = -1*win;
win = maxIG(win,5);
//cout << win << endl;
//float WER = 0;
// search for a word(pre space, post space as well) in Indsenz within win sized window in GDocs and if found then add to PWords
for(int t = 0; t < vIsz;t++){
size_t minedit = 1000;
string s1 = vecpI[t]; //(vGBook[t1].find(s1) != string::npos) || (vGBook[t1] == s1)
string sC;
for(int t1 = maxIG(t-win,0); t1 < min(t+win,vGsz); t1++){
string sCt1 = vecpC[t1];
size_t mineditIC = editDist(s1,sCt1);
if(mineditIC < minedit) {minedit = mineditIC; sC = sCt1; }
if (sCt1 == s1) {/*WER++;*/ break;}
}
if((s1 != sC) && (/*isEngOrNOT[sC] != */1)) {wrong.push_back(s1); right.push_back(sC); /*cout << s1 <<" " << sC << endl;*/}
}
cout << wrong.size() - sizew << " new correction pairs loaded" << endl;
}
void generatePairs(vector<string> &wrong,vector<string> &right,string localFilenameI,string localFilenameC){
vector<string> vecpI, vecpC;
std::ifstream sIpage(localFilenameI);
if (!(sIpage.is_open())) {cout << "cannot open inds/corrected file" <<endl; return;} // break the while loop for page_no
string localstr;
map<string, bool> isEngOrNOT;
while(sIpage >> localstr) vecpI.push_back(toslp1(localstr)); sIpage.close();
std::ifstream sCpage(localFilenameC);
while(sCpage >> localstr) { vecpC.push_back(toslp1(localstr)); sIpage.close(); if(hasM40PerAsci(localstr)) isEngOrNOT[toslp1(localstr)] = 1;}
int sizew = wrong.size();
// if 1st word is wrong generate suggestions
int vGsz = vecpC.size(), vIsz = vecpI.size();
//if (vGsz >vIsz ) mapTyping[page_no] = vGsz - vIsz;
//cout << vGsz << " " << vIsz << endl;
int win = vGsz - vIsz;
if(win<0) win = -1*win;
win = maxIG(win,5);
//cout << win << endl;
//float WER = 0;
// search for a word(pre space, post space as well) in Indsenz within win sized window in GDocs and if found then add to PWords
for(int t = 0; t < vIsz;t++){
size_t minedit = 1000,minediti;
string s1 = vecpI[t]; //(vGBook[t1].find(s1) != string::npos) || (vGBook[t1] == s1)
string sC;
for(int t1 = maxIG(t-win,0); t1 < min(t+win,vGsz); t1++){
string sCt1 = vecpC[t1];
size_t mineditIC = editDist(s1,sCt1);
if(mineditIC < minedit) {minedit = mineditIC; sC = sCt1; minediti = t1; }