-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathpreprocessor.py
1124 lines (976 loc) · 33.4 KB
/
preprocessor.py
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
#%%
"""
Generated files/folders (depending on the flags set on __main__) summary:
`Folders`
- `tmp/Initial` > Contains files generated by initial hardline filtering
- `tmp/pattern` > Contains files generated by character map replacements
- `tmp/patch` > Contains files generated by replacing patches of identical non-bangla texts on both sides
- `tmp/transilaterate`> Contains files generated by transliterating dangling characters on bangla side
- `Final` > Contains files generated by applying previous transformations and some final postprocessing
`Files(prefixes)`
- `saved` > Transformed Lines that pass hardline filtering after applying transformation
- `savedOriginal` > Original Lines that pass hardline filtering after applying transformation
- `filtered` > Lines that don't pass hardline filtering at a stage
- `cleaned` > Lines that pass hardline filtering at a stage (Initial/Previous passing Lines + saved)
"""
import re
import os
import difflib
import time
from subprocess import check_output
from aksharamukha import transliterate
from itertools import chain
import shutil
import sys
import uuid
import multiprocessing
from multiprocessing import Pool
import argparse
import glob
from tqdm import tqdm
def globalize(func):
def result(*args, **kwargs):
return func(*args, **kwargs)
result.__name__ = result.__qualname__ = uuid.uuid4().hex
setattr(sys.modules[result.__module__], result.__name__, result)
return result
REPLACE_UNICODE_PUNCTUATION = [
(u"\u09F7", u"\u0964"),
(u",", u","),
(u"、", u","),
(u"”", u'"'),
(u"“", u'"'),
(u"∶", u":"),
(u":", u":"),
(u"?", u"?"),
(u"《", u'"'),
(u"》", u'"'),
(u")", u")"),
(u"!", u"!"),
(u"(", u"("),
(u";", u";"),
(u"」", u'"'),
(u"「", u'"'),
(u"0", u"0"),
(u"1", u'1'),
(u"2", u"2"),
(u"3", u"3"),
(u"4", u"4"),
(u"5", u"5"),
(u"6", u"6"),
(u"7", u"7"),
(u"8", u"8"),
(u"9", u"9"),
(u"~", u"~"),
(u"’", u"'"),
(u"…", u"..."),
(u"━", u"-"),
(u"〈", u"<"),
(u"〉", u">"),
(u"【", u"["),
(u"】", u"]"),
(u"%", u"%"),
]
NORMALIZE_UNICODE = [
('\u00AD', ''),
('\u09AF\u09BC', '\u09DF'),
('\u09A2\u09BC', '\u09DD'),
('\u09A1\u09BC', '\u09DC'),
('\u09AC\u09BC', '\u09B0'),
('\u09C7\u09BE', '\u09CB'),
('\u09C7\u09D7', '\u09CC'),
('\u0985\u09BE', '\u0986'),
('\u09C7\u0981\u09D7', '\u09CC\u0981'),
('\u09C7\u0981\u09BE', '\u09CB\u0981'),
('\u09C7([^\u09D7])\u09D7', "\g<1>\u09CC"),
('\\xa0', ' '),
('\u200B', u''),
('\u2060', u''),
(u'„', r'"'),
(u'“', r'"'),
(u'”', r'"'),
(u'–', r'-'),
(u'—', r' - '),
(r' +', r' '),
(u'´', r"'"),
(u'([a-zA-Z])‘([a-zA-Z])', r"\g<1>'\g<2>"),
(u'([a-zA-Z])’([a-zA-Z])', r"\g<1>'\g<2>"),
(u'‘', r"'"),
(u'‚', r"'"),
(u'’', r"'"),
(u'´´', r'"'),
(u'…', r'...'),
]
FRENCH_QUOTES = [
(u'\u00A0«\u00A0', r'"'),
(u'«\u00A0', r'"'),
(u'«', r'"'),
(u'\u00A0»\u00A0', r'"'),
(u'\u00A0»', r'"'),
(u'»', r'"'),
]
SUBSTITUTIONS = [NORMALIZE_UNICODE, FRENCH_QUOTES, REPLACE_UNICODE_PUNCTUATION]
SUBSTITUTIONS = list(chain(*SUBSTITUTIONS))
BANGLA_CHARS = (
r'['
r'\u0981-\u0983'
r'\u0985-\u098B'
r'\u098F-\u0990'
r'\u0993-\u09A8'
r'\u09AA-\u09B0'
r'\u09B2'
r'\u09B6-\u09B9'
r'\u09BC'
r'\u09BE-\u09C3'
r'\u09C7-\u09C8'
r'\u09CB-\u09CC'
r'\u09CE'
r'\u09D7'
r'\u09DC-\u09DD'
r'\u09DF'
r'\u09E6-\u09EF'
r'\u09F3'
r'\u0964'
r']'
)
NEUTRAL_CHARS = (
r'['
r'\s'
r'\u09CD'
r'\u0021-\u002F'
r'\u003A-\u0040'
r'\u005B-\u0060'
r'\u007B-\u007E'
r'\u00A0'
r'\u00A3'
r'\u00B0'
r'\u2000-\u2014'
r'\u2018-\u201D'
r'\u2028-\u202F'
r'\u2032-\u2033'
r'\u2035-\u2036'
r'\u2060-\u206F'
r']'
)
ENGLISH_CHARS = (
r'[a-zA-Z0-9]'
)
NON_BANGLA_PATCH = (
r'['
r'^'
r'\u0981-\u0983'
r'\u0985-\u098B'
r'\u098F-\u0990'
r'\u0993-\u09A8'
r'\u09AA-\u09B0'
r'\u09B2'
r'\u09B6-\u09B9'
r'\u09BC'
r'\u09BE-\u09C3'
r'\u09C7-\u09C8'
r'\u09CB-\u09CC'
r'\u09CE'
r'\u09D7'
r'\u09DC-\u09DD'
r'\u09DF'
r'\u09E6-\u09EF'
r'\u09F3'
r'\u0964'
r'\s'
r'\u09CD'
r'\u0021-\u002F'
r'\u003A-\u0040'
r'\u005B-\u0060'
r'\u007B-\u007E'
r'\u00A0'
r'\u00A3'
r'\u00B0'
r'\u2000-\u2014'
r'\u2018-\u201D'
r'\u2028-\u202F'
r'\u2032-\u2033'
r'\u2035-\u2036'
r'\u2060-\u206F'
r']'
r'+'
)
NON_ENGLISH_PATCH = (
r'['
r'^'
r'a-z'
r'A-Z'
r'0-9'
r'\s'
r'\u09CD'
r'\u0021-\u002F'
r'\u003A-\u0040'
r'\u005B-\u0060'
r'\u007B-\u007E'
r'\u00A0'
r'\u00A3'
r'\u00B0'
r'\u2000-\u2014'
r'\u2018-\u201D'
r'\u2028-\u202F'
r'\u2032-\u2033'
r'\u2035-\u2036'
r'\u2060-\u206F'
r']'
r'+'
)
WHITESPACE_PUNCTATION = (
r'[\(\[\{'
r'\u0021-\u0027'
r'\u002A-\u002F'
r'\u003A-\u0040'
r'\u005C'
r'\u005E-\u0060'
r'\u007C'
r'\u007E'
r'\u02B9-\u02DD'
r'\u09F7'
r'\u0964'
r'\u0965'
r'\u2010-\u201F'
r'\s\t'
r'\)\]\}]'
r'+'
)
INCLUSIVE_NON_BANGLA_PATCH = (
r'[\(\[\{'
r'a-zA-Z'
r'\u00A1-\u00AC'
r'\u00AE-\u02FF'
r'\u0300-\u07BF'
r'\u0900'
r'\u0904-\u094D'
r'\u094E-\u0950'
r'\u0955-\u0963'
r'\u0966-\u097F'
r'\u0A00-\u1FFF'
r'\u2020-\u2027'
r'\u2030-\u2031'
r'\u203B-\u205E'
r'\u2070-\uFE4F'
r'\uFE70-\uFEFF'
r'\uFF21-\uFF3A'
r'\uFF41-\uFF5A'
r'\uFF5F-\uFFEF'
r'\uFFF9-\uFFFF'
r'\u0021-\u0027'
r'\u002A-\u002F'
r'\u003A-\u0040'
r'\u005C'
r'\u005E-\u0060'
r'\u007C'
r'\u007E'
r'\u02B9-\u02DD'
r'\u2010-\u201F'
r'0-9'
r'\s\t'
r'\)\]\}]'
'+'
)
BRACKETED_SPANS = (
r'[\(\[\{]'
r'['
r'\u0021-\u0027'
r'\u002A-\u002F'
r'\u003A-\u0040'
r'\u005C'
r'\u005E-\u0060'
r'\u007C'
r'\u007E'
r'\u02B9-\u02DD'
r'\u2010-\u201F'
r'\s\t'
r']'
r'*'
r'[\)\]\}]'
)
def normalize(text):
for regexp, replacement in SUBSTITUTIONS:
text = re.sub(regexp, replacement, text, flags=re.UNICODE)
text = re.sub(r'\s+', ' ', text)
return text.strip()
def readFile(filename):
with open(filename) as f:
lines = [line.strip() for line in f.readlines()]
return lines
def readFilePair(bnFile, enFile):
bnLines = readFile(bnFile)
enLines = readFile(enFile)
return zip(bnLines, enLines)
def readReplacePatterns(filename):
"""
Patterns should have the following form in each line:
`Pattern:enReplacement(:optional bnReplacement)`
Both Pattern and Replacement can contain arbitrary no of spaces.
Be careful not to place unnecessary spaces.
In absence of bnReplacement, bnReplacement = enReplacement
"""
enPatternMap, bnPatternMap = {}, {}
with open(filename) as f:
for line in f.readlines():
try:
splitLine = line.rstrip('\n').split(":")
pattern = splitLine[0]
enReplacement = splitLine[1]
if len(splitLine) == 3:
bnPatternMap[pattern] = splitLine[2]
else:
bnPatternMap[pattern] = enReplacement
enPatternMap[pattern] = enReplacement
except:
continue
return enPatternMap, bnPatternMap
def hasNonBangla(line):
return len(line) - countBanglaChars(line) - countNeutralChars(line)
def hasNonEnglish(line):
return len(line) - countEnglishChars(line) - countNeutralChars(line)
def hasOOV(line):
return len(line) - countBanglaChars(line) - countEnglishChars(line) - countNeutralChars(line)
def countBanglaChars(line):
chars = re.findall(
BANGLA_CHARS,
line,
flags=re.UNICODE
)
return len(chars)
def countEnglishChars(line):
chars = re.findall(
ENGLISH_CHARS,
line,
flags=re.UNICODE
)
return len(chars)
def countNeutralChars(line):
chars = re.findall(
NEUTRAL_CHARS,
line,
flags=re.UNICODE
)
return len(chars)
def getNonBanglaPatches(line):
return re.findall(
NON_BANGLA_PATCH,
line,
flags=re.UNICODE
)
def getNonEnglishPatches(line):
return re.findall(
NON_ENGLISH_PATCH,
line,
flags=re.UNICODE
)
def replaceEmojis(*lines):
outputLines = []
for line in lines:
line = re.sub(
r'\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff]',
"",
line,
flags=re.UNICODE
)
line = re.sub(r'[\U00010000-\U0010ffff]', "", line, flags=re.UNICODE)
outputLines.append(line)
return tuple(outputLines)
def isValidProcessedPair(bnLine, enLine):
if bnLine.strip() == "" or enLine.strip() == "":
return False
# check if either side contains only punctuations and whitespaces
if (
re.sub(WHITESPACE_PUNCTATION, "", bnLine.strip(), flags=re.UNICODE) == "" or
re.sub(WHITESPACE_PUNCTATION, "", enLine.strip(), flags=re.UNICODE) == ""
):
return False
return True
def writeFilePairs(dir, validPairs, foreignPairs, savedPairs=None):
os.makedirs(dir, exist_ok=True)
with open(os.path.join(dir, "cleaned.bn"), 'w') as bn, \
open(os.path.join(dir, "cleaned.en"), 'w') as en:
for bnLine, enLine in validPairs:
print(bnLine, file=bn)
print(enLine, file=en)
with open(os.path.join(dir, "filtered.bn"), 'w') as bn, \
open(os.path.join(dir, "filtered.en"), 'w') as en:
for bnLine, enLine in foreignPairs:
print(bnLine, file=bn)
print(enLine, file=en)
if savedPairs:
with open(os.path.join(dir, "savedOrignal.bn"), 'w') as bnO, \
open(os.path.join(dir, "savedOrignal.en"), 'w') as enO, \
open(os.path.join(dir, "saved.bn"), 'w') as bnS, \
open(os.path.join(dir, "saved.en"), 'w') as enS:
for bnOriginal, enOriginal, bnSaved, enSaved in savedPairs:
print(bnOriginal, file=bnO)
print(enOriginal, file=enO)
print(bnSaved, file=bnS)
print(enSaved, file=enS)
def convertNumerals(bnLine, enLine):
newBnLine, newEnLine = bnLine, enLine
for enNumeral in re.findall(r'[0-9]+', newBnLine, flags=re.UNICODE):
newBnLine = newBnLine.replace(enNumeral, transliterate.process('RomanReadable', 'Bengali', enNumeral), 1)
for bnNumeral in re.findall(r'[০-৯]+', newEnLine, flags=re.UNICODE):
newEnLine = newEnLine.replace(bnNumeral, transliterate.process('Bengali', 'RomanReadable', bnNumeral), 1)
return newBnLine, newEnLine
def applyPatterns(line, patternMap):
"""
Returns:
patternFound(bool) : Whether any of the patterns were found in the line
line(str) : Transformed line using pattern replacements
"""
patternFound = False
orignalLine = line
for pattern, replacement in patternMap.items():
line = line.replace(pattern, replacement)
if orignalLine != line:
patternFound = True
return patternFound, line
def patternHandler(bnLine, enLine, verbose):
_, newEnLine = applyPatterns(enLine, enPatternMap)
_, newBnLine = applyPatterns(bnLine, bnPatternMap)
if verbose:
# if there is foreign text in the linepair after applying replacements
if hasNonBangla(newBnLine) or hasNonEnglish(newEnLine):
patternHandler.foreignPairs.append((newBnLine, newEnLine))
else:
# if the original linepair had foreign characters
if hasNonBangla(bnLine) or hasNonEnglish(enLine):
patternHandler.savedPairs.append((bnLine, enLine, newBnLine, newEnLine))
patternHandler.validPairs.append((newBnLine, newEnLine))
return newBnLine, newEnLine
def patchHandler(bnLine, enLine, verbose):
"""
finds continous patches of non bangla text on bangla side and removes
a found patch from both sides if it is also present in english side
"""
minPatchLength = 2 # this shouldn't be too small
newBnLine, newEnLine = bnLine, enLine
nonBanglaPatches = re.findall(
INCLUSIVE_NON_BANGLA_PATCH,
newBnLine,
flags=re.UNICODE
)
for patch in nonBanglaPatches:
patch = patch.strip()
# ignore whitespace only patches
if patch == "":
continue
# ignore number only patches
try:
float(patch)
continue
except:
pass
# patch shouldnt end in starting braces
if patch[-1] in ['[', '{', '(']:
patch = patch[:-1].strip()
# patch shouldnt start with ending braces/common punctuations
if patch and patch[0] in [']', '}', ')', ',', '.']:
patch = patch[1:].strip()
# should the patch length be counted with spaces included??
# should all matching patches be removed in english side?
if (
len(patch) >= minPatchLength and
(
patch in newEnLine or
patch.upper() in newEnLine or
patch.lower() in newEnLine or
patch.capitalize() in newEnLine
)
):
newBnLine = newBnLine.replace(patch, "").strip()
newEnLine = newEnLine.replace(
patch, ""
).replace(
patch.lower(), ""
).replace(
patch.upper(), ""
).replace(
patch.capitalize(), ""
).strip()
if verbose:
# if there is foreign text in the linepair after applying replacements
if hasNonBangla(newBnLine) or hasNonEnglish(newEnLine):
patchHandler.foreignPairs.append((newBnLine, newEnLine))
else:
# if the original linepair had foreign characters
if hasNonBangla(bnLine) or hasNonEnglish(enLine):
patchHandler.savedPairs.append((bnLine, enLine, newBnLine, newEnLine))
patchHandler.validPairs.append((newBnLine, newEnLine))
return newBnLine, newEnLine
def alternatePatchHandler(bnLine, enLine, verbose):
"""
finds common patches in both lines and removes a patch
from bothsides if the found patch is non bangla
"""
def isValidPatch(patch):
patch = re.sub(
INCLUSIVE_NON_BANGLA_PATCH,
"",
patch,
flags=re.UNICODE
).strip()
return patch == ""
minPatchLength = 2 # this shouldn't be too small
newBnLine, newEnLine = bnLine, enLine
matcher = difflib.SequenceMatcher(None, newBnLine, newEnLine)
potentialPatches = [newBnLine[match.a : match.a + match.size] for match in matcher.get_matching_blocks() if match.size > 0]
for patch in potentialPatches:
patch = patch.strip()
# ignore whitespace only patches
if patch == "":
continue
# ignore number only patches
try:
float(patch)
continue
except:
pass
# patch shouldnt end in starting braces
if patch[-1] in ['[', '{', '(']:
patch = patch[:-1].strip()
# patch shouldnt start with ending braces/common punctuations
if patch and patch[0] in [']', '}', ')', ',', '.']:
patch = patch[1:].strip()
# should the patch length be counted with spaces included??
# should all matching patches be removed in english side?
if (
isValidPatch(patch) and
len(patch) >= minPatchLength and
(
patch in newEnLine or
patch.upper() in newEnLine or
patch.lower() in newEnLine or
patch.capitalize() in newEnLine
)
):
newBnLine = newBnLine.replace(patch, "").strip()
newEnLine = newEnLine.replace(
patch, ""
).replace(
patch.lower(), ""
).replace(
patch.upper(), ""
).replace(
patch.capitalize(), ""
).strip()
if verbose:
# if there is foreign text in the linepair after applying replacements
if hasNonBangla(newBnLine) or hasNonEnglish(newEnLine):
alternatePatchHandler.foreignPairs.append((newBnLine, newEnLine))
else:
# if the original linepair had foreign characters
if hasNonBangla(bnLine) or hasNonEnglish(enLine):
alternatePatchHandler.savedPairs.append((bnLine, enLine, newBnLine, newEnLine))
alternatePatchHandler.validPairs.append((newBnLine, newEnLine))
return newBnLine, newEnLine
def bpediaPatchHandler(bnLine, enLine, verbose):
"""
removes non bangla patches from first-bracketed spans on bangla side.
Specific to Banglapedia.
"""
newBnLine, newEnLine = bnLine, enLine
bracketedSpans = re.findall(r'\([^\)]*?\)', newBnLine, flags=re.UNICODE)
for span in bracketedSpans:
if hasNonBangla(span):
# if span contains no bangla text, remove it
if not countBanglaChars(span):
newBnLine = newBnLine.replace(span, "").strip()
else:
nonBanglaPatches = re.findall(
INCLUSIVE_NON_BANGLA_PATCH,
span,
flags=re.UNICODE
)
newSpan = span
for patch in nonBanglaPatches:
if isValidProcessedPair(patch, patch):
newSpan = newSpan.replace(patch.strip(), "")
newBnLine = newBnLine.replace(span, newSpan.strip()).strip()
if verbose:
# if there is foreign text in the linepair after applying replacements
if hasNonBangla(newBnLine) or hasNonEnglish(newEnLine):
bpediaPatchHandler.foreignPairs.append((newBnLine, newEnLine))
else:
# if the original linepair had foreign characters
if hasNonBangla(bnLine) or hasNonEnglish(enLine):
bpediaPatchHandler.savedPairs.append((bnLine, enLine, newBnLine, newEnLine))
bpediaPatchHandler.validPairs.append((newBnLine, newEnLine))
return newBnLine, newEnLine
def transliterateHandler(bnLine, enLine, verbose):
newBnLine, newEnLine = bnLine, enLine
# convert numerals to appropriate script
newBnLine, newEnLine = convertNumerals(newBnLine, newEnLine)
charConvertMap = {
'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' : 'জেড',
}
def customReplace(match):
caughtChar = match.group(1).strip()
return charConvertMap[caughtChar.lower()]
newBnLine = re.sub(r'(\b[a-zA-Z]\b)', customReplace, newBnLine, flags=re.UNICODE)
if verbose:
# if there is foreign text in the linepair after applying replacements
if hasNonBangla(newBnLine) or hasNonEnglish(newEnLine):
transliterateHandler.foreignPairs.append((newBnLine, newEnLine))
else:
# if the original linepair had foreign characters
if hasNonBangla(bnLine) or hasNonEnglish(enLine):
transliterateHandler.savedPairs.append((bnLine, enLine, newBnLine, newEnLine))
transliterateHandler.validPairs.append((newBnLine, newEnLine))
return newBnLine, newEnLine
def ratioHandler(bnLine, enLine, verbose):
newBnLine, newEnLine = bnLine, enLine
bnLowerThresh = .001
bnUpperThresh = 10.0
enLowerThresh = .001
enUpperThresh = 10.0
bnNativeChars = countBanglaChars(bnLine)
bnForeignChars = len(bnLine) - bnNativeChars - countNeutralChars(bnLine)
bnRatio = bnForeignChars/bnNativeChars
enNativeChars = countEnglishChars(enLine)
enForeignChars = len(enLine) - enNativeChars - countNeutralChars(enLine)
enRatio = enForeignChars/enNativeChars
if verbose:
# if there is foreign text in the linepair after applying replacements
if (
bnRatio >= bnUpperThresh or
enRatio >= enUpperThresh or
bnRatio <= bnLowerThresh or
enRatio <= enLowerThresh or
countBanglaChars(enLine) or countEnglishChars(bnLine)
):
ratioHandler.foreignPairs.append((newBnLine, newEnLine))
else:
# if the original linepair had foreign characters
if bnForeignChars or enForeignChars:
ratioHandler.savedPairs.append((bnLine, enLine, newBnLine, newEnLine))
ratioHandler.validPairs.append((newBnLine, newEnLine))
return newBnLine, newEnLine
def cleanSentencePairs(linePairs, options, output_dir, verbose=True):
for option, choice in options.items():
if choice:
globals()[f'{option}Handler'].validPairs = multiprocessing.Manager().list()
globals()[f'{option}Handler'].foreignPairs = multiprocessing.Manager().list()
globals()[f'{option}Handler'].savedPairs = multiprocessing.Manager().list()
global finalValidPairs, finalForeignPairs, finalSavedPairs, initialValidPairs, initialForeignPairs
finalValidPairs, finalForeignPairs, finalSavedPairs = (
multiprocessing.Manager().list(),
multiprocessing.Manager().list(),
multiprocessing.Manager().list()
)
initialValidPairs, initialForeignPairs = (
multiprocessing.Manager().list(),
multiprocessing.Manager().list()
)
@globalize
def processPair(bnLine, enLine):
validPair = False
if bnLine == enLine or (not countBanglaChars(bnLine) or not countEnglishChars(enLine)):
return
if hasNonBangla(bnLine) or hasNonEnglish(enLine):
initialForeignPairs.append((bnLine, enLine))
else:
initialValidPairs.append((bnLine, enLine))
validPair = True
# apply all selected transformations
newBnLine, newEnLine = bnLine, enLine
newBnLine = normalize(newBnLine).strip()
newEnLine = normalize(newEnLine).strip()
newBnLine, newEnLine = replaceEmojis(newBnLine, newEnLine)
# remove unnecessary bracketed spans; this needs to be done first for patch removal to work
newBnLine = re.sub(
BRACKETED_SPANS,
"",
newBnLine,
flags=re.UNICODE
)
newEnLine = re.sub(
BRACKETED_SPANS,
"",
newEnLine,
flags=re.UNICODE
)
for option, choice in options.items():
if choice and not validPair:
newBnLine, newEnLine = globals()[f'{option}Handler'](newBnLine, newEnLine, verbose)
# do some final postprocessing
if not isValidProcessedPair(newBnLine, newEnLine):
return
# remove unnecessary bracketed spans (after patch deletion)
newBnLine = re.sub(
BRACKETED_SPANS,
"",
newBnLine,
flags=re.UNICODE
)
newEnLine = re.sub(
BRACKETED_SPANS,
"",
newEnLine,
flags=re.UNICODE
)
newBnLine = re.sub(r'\s+', ' ', newBnLine)
newEnLine = re.sub(r'\s+', ' ', newEnLine)
# if there is foreign text in the linepair after applying transformations
if hasNonBangla(newBnLine) or hasNonEnglish(newEnLine):
finalForeignPairs.append((bnLine, enLine))
else:
# if the original linepair had foreign characters
if hasNonBangla(bnLine) or hasNonEnglish(enLine):
finalSavedPairs.append((bnLine, enLine, newBnLine, newEnLine))
finalValidPairs.append((newBnLine, newEnLine))
print('\tStarting processes...')
with Pool() as pool:
pool.starmap(processPair, linePairs)
print('\tWriting logs and outputs...')
# write the final and temporary filepairs to appropriate directories
writeFilePairs(
os.path.join(output_dir, 'Final'),
finalValidPairs, finalForeignPairs, finalSavedPairs
)
if verbose:
writeFilePairs(
os.path.join(output_dir, "tmp", "Initial"),
initialValidPairs, initialForeignPairs)
for option, choice in options.items():
if choice:
funcName = globals()[f'{option}Handler']
writeFilePairs(
os.path.join(output_dir, "tmp", option),
funcName.validPairs,
funcName.foreignPairs,
funcName.savedPairs
)
def col(id):
if id == 1: return "\033[32m"
if id == 2: return "\033[33m"
if id == 3: return "\033[31m"
return "\033[0m"
def cleanup(dirname):
for sub_dir in ["Final", "tmp"]:
if os.path.isdir(os.path.join(dirname, sub_dir)):
shutil.rmtree(os.path.join(dirname, sub_dir))
def _merge(input_files, output_file):
with open(output_file, 'w') as outf:
for input_file in input_files:
with open(input_file) as inpf:
for line in inpf:
print(line.strip(), file=outf)
def _train(input_file, output_prefix, coverage, vocab_size, model_type):
cmd = [
f"spm_train --input=\"{input_file}\"",
f"--model_prefix=\"{output_prefix}\"",
f"--vocab_size={vocab_size}",
f"--character_coverage={coverage}",
"--train_extremely_large_corpus"
]
os.system(" ".join(cmd))
def main(args):
global enPatternMap, bnPatternMap
enPatternMap, bnPatternMap = readReplacePatterns(
os.path.join(os.path.dirname(__file__), "replacePatterns.txt")
)
if os.path.isdir(args.output_dir):
shutil.rmtree(args.output_dir)
if args.normalize:
iterator = tqdm(
glob.glob(os.path.join(args.input_dir, "**", "*.bn"), recursive=True) +
glob.glob(os.path.join(args.input_dir, "**", "*.en"), recursive=True),
desc="Normalizing files"
)
for input_file in iterator:
output_file = input_file.replace(
os.path.normpath(args.input_dir),
os.path.normpath(args.output_dir)
)
os.makedirs(os.path.dirname(output_file), exist_ok=True)
with open(output_file, 'w') as outf:
with open(input_file) as inpf:
for line in inpf:
line = normalize(line)
_, line = applyPatterns(line, enPatternMap)
_, line = applyPatterns(line, bnPatternMap)
print(line.strip(), file=outf)
else:
cleanup(args.output_dir)
os.makedirs(os.path.join(args.output_dir, "data"), exist_ok=True)
linePair_list = []
for bnFile in glob.glob(os.path.join(args.input_dir, "**", f"*.bn"), recursive=True):
enFile = bnFile[:-3] + ".en"
if not os.path.isfile(enFile):
continue
linePair_list.append(readFilePair(bnFile, enFile))
linePairs = list(chain.from_iterable(linePair_list))
print(col(2) + 'Starting Stage 1...' + col(0))
cleanSentencePairs(linePairs, {'pattern': True}, args.output_dir)
shutil.copy(
os.path.join(args.output_dir, "Final", "cleaned.bn"),
os.path.join(args.output_dir, "data", "data1.bn")
)
shutil.copy(
os.path.join(args.output_dir, "Final", "cleaned.en"),
os.path.join(args.output_dir, "data", "data1.en")
)
shutil.copy(
os.path.join(args.output_dir, "tmp", "pattern", "filtered.bn"),
os.path.join(args.output_dir, "data", "stage1Filtered.bn")
)
shutil.copy(
os.path.join(args.output_dir, "tmp", "pattern", "filtered.en"),
os.path.join(args.output_dir, "data", "stage1Filtered.en")
)
print(col(2) + 'Starting Stage 2...' + col(0))
cleanup(args.output_dir)
linePairs = readFilePair(
os.path.join(args.output_dir, "data", "stage1Filtered.bn"),
os.path.join(args.output_dir, "data", "stage1Filtered.en")
)
cleanSentencePairs(linePairs, {'ratio': True}, args.output_dir)
shutil.copy(
os.path.join(args.output_dir, "tmp", "ratio", "cleaned.bn"),
os.path.join(args.output_dir, "data", "data2.bn")
)
shutil.copy(
os.path.join(args.output_dir, "tmp", "ratio", "cleaned.en"),
os.path.join(args.output_dir, "data", "data2.en")
)
shutil.copy(
os.path.join(args.output_dir, "tmp", "ratio", "filtered.bn"),
os.path.join(args.output_dir, "data", "stage2Filtered.bn")
)
shutil.copy(
os.path.join(args.output_dir, "tmp", "ratio", "filtered.en"),
os.path.join(args.output_dir, "data", "stage2Filtered.en")
)