-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdataworker.pl
1060 lines (843 loc) · 27.5 KB
/
pdataworker.pl
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
#!/usr/bin/perl -w
#pdataworker.pl is the computing part of PEXACC.
#
# (C) ICIA 2011, Radu ION.
#ver 1.8, 08.08.2011, Radu ION: added word normalization (for string similarity) as a function of languages.
#ver 1.9, 09.08.2011, Radu ION: modified scoreSentPair() scoring.
#ver 2.0, 10.08.2011, Radu ION: added lemmatization.
#ver 2.1, 23.08.2011, Radu ION: added user support.
#ver 2.2, 12.09.2011, Radu ION: added phrase alignment when consecutive phrases map.
#ver 2.3, 22.09.2011, Radu ION: added a tokenization function.
#ver 2.4, 04.10.2011, Radu ION: Windows/Unix portable.
#ver 2.5, 04.10.2011, Radu ION: Bug (Marcis): phrases which do not have the same numbers, should not align.
#ver 3.0, 11.10.2011, Radu ION: a complete new way of parallel computing. Functionality remains at ver 2.5.
#ver 3.01, 3.11.2011, Radu ION: added selective debug messages.
#ver 3.1, 19.11.2011, Radu ION: output a phrase pair only if its prob >= OUTPUTTHR (keep NFS traffic low)
#ver 4.0, 06.12.2011, Radu ION: no NFS drive, scp outfiles back to the master.
#ver 4.1, 15.12.2011, Radu ION: added filtering such that 10. -> 10) would not be aligned anymore.
#ver 4.41, 15.12.2011, Radu ION: fixed portableRemoteCopy.
#ver 5.0, 15.12.2011, Radu ION: PEXACC similarity measure is now symmetrical.
#ver 5.1, 18.12.2011, Radu ION: added Romanian diacrtics handling.
#ver 5.2, 18.12.2011, Radu ION: modified PEXACC similarity measure to have lists of content words only.
use strict;
use warnings;
use strsim;
use IO::Handle;
use Time::HiRes qw( time alarm sleep );
use File::Spec;
use File::Path;
sub portableRemoteCopy( $$$$ );
sub portableRemoveFile( $ );
sub portableCopyFileToDir( $$ );
sub scorePPhrases( $$ );
sub tokenizeText( $ );
sub readGIZAPPDict( $$$$ );
sub normalizeWord( $$ );
sub pairInDict( $$$ );
sub combineDictionaries( $$ );
sub readStopWordsList( $ );
sub scoreSentPair( $$ );
sub scoreSentPairLP( $$$ );
sub isNamedEntity( $ );
sub readSplitMarkers( $ );
sub readInputParams( $ );
sub lemmatizeWord( $$ );
sub readInflectionList( $ );
sub featStartOrEndWithTranslations( $$$ );
sub featAlignNotScrambled( $$$ );
sub boolHaveSameNumbers( $$ );
sub featHaveFinalPunct( $$ );
if ( scalar( @ARGV ) != 1 ) {
die( "pdataworker.pl <file from pexacc2.pl>\n" );
}
my( $outfilebasename, $pexaccconf ) = readInputParams( $ARGV[0] );
####### CONFIG #############
#Please do not modify here.
#Use pdataextractconf.pm so that the same data is available for the master process, pdataextract-p.pl
my( $DEBUG ) = $pexaccconf->{"DEBUG"};
my( $SRCL ) = $pexaccconf->{"SRCL"};
my( $TRGL ) = $pexaccconf->{"TRGL"};
my( $GIZAPPTHR ) = $pexaccconf->{"GIZAPPTHR"};
my( $NEWGIZAPPTHR ) = $pexaccconf->{"NEWGIZAPPTHR"};
my( $SUREGIZAPPTHR ) = $pexaccconf->{"SUREGIZAPPTHR"};
my( $SENTRATIO ) = $pexaccconf->{"SENTRATIO"};
my( $PEXACCWORKINGDIR ) = $pexaccconf->{"PEXACCWORKINGDIR"};
my( $MASTERIP ) = $pexaccconf->{"MASTERIP"};
my( $TMPDIR ) = $pexaccconf->{"TMPDIR"};
#Try to create the directory.
mkpath( $TMPDIR );
my( $LEMMAS ) = $pexaccconf->{"LEMMAS"};
my( %INFLEN ) = readInflectionList( $pexaccconf->{"INFLENFILE"} );
my( %INFLRO ) = readInflectionList( $pexaccconf->{"INFLROFILE"} );
my( %ENSTOPWORDS ) = readStopWordsList( $pexaccconf->{"ENSTOPWORDSFILE"} );
my( %ROSTOPWORDS ) = readStopWordsList( $pexaccconf->{"ROSTOPWORDSFILE"} );
#Create resource 'objects' with all resources.
my( %ENRORES ) = (
"enLang" => $SRCL,
"roLang" => $TRGL,
"enInflections" => \%INFLEN,
"roInflections" => \%INFLRO,
"enStopWords" => \%ENSTOPWORDS,
"roStopWords" => \%ROSTOPWORDS,
"enroDict" => {}
);
my( %ROENRES ) = (
"enLang" => $TRGL,
"roLang" => $SRCL,
"enInflections" => \%INFLRO,
"roInflections" => \%INFLEN,
"enStopWords" => \%ROSTOPWORDS,
"roStopWords" => \%ENSTOPWORDS,
"enroDict" => {}
);
my( $ENRODICTBASE ) = {};
my( $ROENDICTBASE ) = {};
readGIZAPPDict( $pexaccconf->{"DICTFILEST"}, $ENRODICTBASE, $GIZAPPTHR, \%ENRORES );
readGIZAPPDict( $pexaccconf->{"DICTFILETS"}, $ROENDICTBASE, $GIZAPPTHR, \%ROENRES );
#Reading the additional learnt dictionary (always from SRCL to TRGL).
#Strategy to be determined (what happens if we already have a pair of translation equivalents?)
#Determined: linear combination.
my( $DICTWEIGHTMAIN ) = $pexaccconf->{"DICTWEIGHTMAIN"};
my( $DICTWEIGHTLEARNT ) = $pexaccconf->{"DICTWEIGHTLEARNT"};
my( $ENRODICTLEARNT ) = {};
my( $ROENDICTLEARNT ) = {};
if ( -f $pexaccconf->{"LEARNTDICTFILEST"} && -f $pexaccconf->{"LEARNTDICTFILETS"} ) {
readGIZAPPDict( $pexaccconf->{"LEARNTDICTFILEST"}, $ENRODICTLEARNT, $NEWGIZAPPTHR, \%ENRORES );
readGIZAPPDict( $pexaccconf->{"LEARNTDICTFILETS"}, $ROENDICTLEARNT, $NEWGIZAPPTHR, \%ROENRES );
}
#Combine dictionaries...
my( $ENRODICT ) = combineDictionaries( $ENRODICTBASE, $ENRODICTLEARNT );
my( $ROENDICT ) = combineDictionaries( $ROENDICTBASE, $ROENDICTLEARNT );
my( $SSTHR ) = $pexaccconf->{"SSTHR"};
my( $REMOTEWORKER ) = $pexaccconf->{"REMOTEWORKER"};
my( $OUTPUTTHR ) = $pexaccconf->{"OUTPUTTHR"};
####### End Config #########
#Update objects with the dictionaries...
$ENRORES{"enroDict"} = $ENRODICT;
$ROENRES{"enroDict"} = $ROENDICT;
#Reopen STDERR to see what's wrong.
my( $errfileL ) = File::Spec->catfile( $TMPDIR, $outfilebasename . ".err" );
open( FERROR, "> $errfileL" ) or die( "pdataworker::main: cannot open FERROR!\n" );
STDERR->fdopen( \*FERROR, 'w' ) or die( "pdataworker::main: cannot reopen STDERR!\n" );
STDERR->autoflush( 1 );
binmode( STDERR, ":utf8" );
my( $outfileL ) = File::Spec->catfile( $TMPDIR, $outfilebasename . ".out" );
open( OUT, "> $outfileL" ) or die( "pdataworker::main: cannot open file '$outfileL' because '$!' !\n" );
binmode( OUT, ":utf8" );
OUT->autoflush( 1 );
scorePPhrases( $ARGV[0], *OUT{"IO"} );
close( OUT );
close( FERROR );
#If result obtained remotely
if ( $REMOTEWORKER ) {
#Move RESULT on the Master in PEXACCWORKINGDIR
portableRemoteCopy( $outfileL, "rion", $MASTERIP, $PEXACCWORKINGDIR );
}
#else, simply copy in the proper location
else {
#Copy RESULT in PEXACCWORKINGDIR
portableCopyFileToDir( $outfileL, $PEXACCWORKINGDIR );
}
#Clean
portableRemoveFile( $errfileL );
portableRemoveFile( $outfileL );
#Guard against reading incomplete files.
my( $readyfileL ) = File::Spec->catfile( $TMPDIR, $outfilebasename . ".ready" );
open( RDY, "> $readyfileL" ) or die( "pdataworker::main: cannot open file '$readyfileL' !\n" );
close( RDY );
if ( $REMOTEWORKER ) {
#Move READY onto NFS...
portableRemoteCopy( $readyfileL, "rion", $MASTERIP, $PEXACCWORKINGDIR );
}
else {
portableCopyFileToDir( $readyfileL, $PEXACCWORKINGDIR );
}
#Clean
portableRemoveFile( $readyfileL );
########################### from pexacc2conf.pm ###########################################
sub portableRemoteCopy( $$$$ ) {
my( $localfile, $remoteuser, $remotemachine, $remotedir ) = @_;
if ( $^O =~ /^Linux$/i || $^O =~ /^Cygwin$/i || $^O =~ /^MSys$/i ) {
if ( $DEBUG ) {
qx/scp ${localfile} ${remoteuser}\@${remotemachine}:${remotedir} 1>&2/;
}
else {
qx/scp -q ${localfile} ${remoteuser}\@${remotemachine}:${remotedir}/;
}
}
else {
die( "pexacc2conf::portableRemoteCopy: unsupported operating system '$^O' !\n" );
}
}
sub portableRemoveFile( $ ) {
my( $file ) = $_[0];
#Windows run
if ( $^O =~ /^MSWin(?:32|64)$/i ) {
if ( $DEBUG ) {
warn( "`del \/F \/Q ${file}'\n" );
}
qx/del \/F \/Q ${file}/;
}
#Linux run
elsif ( $^O =~ /^Linux$/i || $^O =~ /^Cygwin$/i || $^O =~ /^MSys$/i ) {
if ( $DEBUG ) {
qx/rm -fv ${file} 1>&2/;
}
else {
qx/rm -f ${file}/;
}
}
else {
die( "pexacc2conf::portableRemoveFile: unsupported operating system '$^O' !\n" );
}
}
sub portableCopyFileToDir( $$ ) {
my( $file, $dir ) = @_;
#Windows run
if ( $^O =~ /^MSWin(?:32|64)$/i ) {
if ( $DEBUG ) {
warn( "`copy \/Y ${file} ${dir}\\'\n" );
}
qx/copy \/Y ${file} ${dir}\\/;
}
#Linux run
elsif ( $^O =~ /^Linux$/i || $^O =~ /^Cygwin$/i || $^O =~ /^MSys$/i ) {
if ( $DEBUG ) {
qx/cp -fv ${file} ${dir}\/ 1>&2/;
}
else {
qx/cp -f ${file} ${dir}\//;
}
}
else {
die( "pexacc2conf::portableCopyFileToDir: unsupported operating system '$^O' !\n" );
}
}
########################### end from pexacc2conf.pm #######################################
sub scorePPhrases( $$ ) {
my( $infile, $outfhndl ) = @_;
my( $outfbn );
open( IN, "< " . $infile ) or die( "pdataworker::scorePPhrases: cannot open file " . $infile . " !\n" );
binmode( IN, ":utf8" );
$outfbn = <IN>;
$outfbn =~ s/^\s+//;
$outfbn =~ s/\s+$//;
#For all phrase pairs
while ( my $line = <IN> ) {
$line =~ s/^\s+//;
$line =~ s/\s+$//;
next if ( $line eq "" );
next if ( $line =~ /^--param\s/ );
my( $i, $j, $srcphr, $trgphr ) = split( /#SPLIT-HERE#/, $line );
my( @srcphrtok ) = tokenizeText( $srcphr );
my( @trgphrtok ) = tokenizeText( $trgphr );
my( $pprob ) = scoreSentPair( \@srcphrtok, \@trgphrtok );
print( $outfhndl $i . "#SPLIT-HERE#" . $j . "#SPLIT-HERE#" . $srcphr . "#SPLIT-HERE#" . $trgphr . "#SPLIT-HERE#" . $pprob . "\n" )
if ( $pprob >= $OUTPUTTHR );
}
close( IN );
}
sub readGIZAPPDict( $$$$ ) {
my( $gizafile, $gizapp, $probthr, $resources ) = @_;
my( $lcnt ) = 0;
print( STDERR "pdataworker::readGIZAPPDict: reading file '$gizafile'...\n" )
if ( $DEBUG );
open( DICT, "< $gizafile" ) or die( "pdataworker::readGIZAPPDict : cannot open file \'$gizafile\' !\n" );
binmode( DICT, ":utf8" );
while ( my $line = <DICT> ) {
$lcnt++;
print( STDERR "pdataworker::readGIZAPPDict: read $lcnt lines...\n" )
if ( $DEBUG && $lcnt % 100000 == 0 );
$line =~ s/^\s+//;
$line =~ s/\s+$//;
next if ( $line =~ /^$/ );
my( $sprx ) = '\s+';
$sprx = '\t+' if ( $line =~ /\t/ && $line =~ / / );
my( @toks ) = split( /$sprx/, $line );
#en - SRC, ro - TRG
my( $enw ) = lc( $toks[0] );
my( $row ) = lc( $toks[1] );
my( $score ) = $toks[2];
next if ( $score < $probthr );
my( $subreplshtz ) = sub {
my( $word ) = @_;
my( %variants ) = ();
#Sh and tz variations for Romanian
do {
my( $templ ) = $word;
$templ =~ s/\x{0163}/\x{021B}/g;
$variants{$templ} = 1;
$templ =~ s/\x{015F}/\x{0219}/g;
$variants{$templ} = 1;
};
do {
my( $templ ) = $word;
$templ =~ s/\x{0163}/\x{021B}/g;
$variants{$templ} = 1;
$templ =~ s/\x{0219}/\x{015F}/g;
$variants{$templ} = 1;
};
do {
my( $templ ) = $word;
$templ =~ s/\x{021B}/\x{0163}/g;
$variants{$templ} = 1;
$templ =~ s/\x{015F}/\x{0219}/g;
$variants{$templ} = 1;
};
do {
my( $templ ) = $word;
$templ =~ s/\x{021B}/\x{0163}/g;
$variants{$templ} = 1;
$templ =~ s/\x{0219}/\x{015F}/g;
$variants{$templ} = 1;
};
$variants{$word} = 1;
return keys( %variants );
};
foreach my $e ( $subreplshtz->( $enw ) ) {
foreach my $r ( $subreplshtz->( $row ) ) {
my( $le ) = lemmatizeWord( $e, $resources->{"enInflections"} );
my( $lr ) = lemmatizeWord( $r, $resources->{"roInflections"} );
my( $tpair );
if ( $LEMMAS ) {
$tpair = $le . "#" . $lr;
}
else {
$tpair = $e . "#" . $r;
}
#Due to lemmatization, we may have multiple entries... select the maximum score.
if ( ! exists( $gizapp->{$tpair} ) ) {
$gizapp->{$tpair} = $score;
}
elsif ( $gizapp->{$tpair} < $score ) {
$gizapp->{$tpair} = $score;
}
} #end all ro variants
} #end all en variants
} #end dict
close( DICT );
return $gizapp;
}
#Union combination.
sub combineDictionaries( $$ ) {
my( $base, $learnt ) = @_;
my( $combined ) = {};
#Only if the pair is in both dictionaries, combine the score.
#Else, keep the score from each dictionary.
foreach my $wp ( keys( %{ $base } ) ) {
if ( exists( $learnt->{$wp} ) ) {
$combined->{$wp} = $base->{$wp} * $DICTWEIGHTMAIN + $learnt->{$wp} * $DICTWEIGHTLEARNT;
delete( $learnt->{$wp} );
}
else {
$combined->{$wp} = $base->{$wp};
}
}
foreach my $wp ( keys( %{ $learnt } ) ) {
$combined->{$wp} = $learnt->{$wp};
}
return $combined;
}
# symmetrical ready
sub pairInDict( $$$ ) {
my( $srcw, $trgw, $res ) = @_;
my( $gizapp ) = $res->{"enroDict"};
$srcw = lc( $srcw );
$trgw = lc( $trgw );
my( $lsrcw ) = lemmatizeWord( $srcw, $res->{"enInflections"} );
my( $ltrgw ) = lemmatizeWord( $trgw, $res->{"roInflections"} );
foreach my $d ( $gizapp ) {
my( $tpair ) = $srcw . "#" . $trgw;
$tpair = $lsrcw . "#" . $ltrgw if ( $LEMMAS );
return $d->{$tpair} if ( exists( $d->{$tpair} ) );
return $d->{lc( $tpair )} if ( exists( $d->{lc( $tpair )} ) );
}
return 0;
}
sub scoreSentPair( $$ ) {
my( $srcsentin, $trgsentin ) = @_;
my( $score1 ) = scoreSentPairLP( $srcsentin, $trgsentin, \%ENRORES );
my( $score2 ) = scoreSentPairLP( $trgsentin, $srcsentin, \%ROENRES );
#The minimum between the two.
#return ( ( $score1 <= $score2 ) ? $score1 : $score2 );
return ( $score1 + $score2 ) / 2;
}
#This is the object with all the resources.
#my( %ENRORES ) = (
# "enLang" => $SRCL,
# "roLang" => $TRGL,
# "enInflections" => \%INFLEN,
# "roInflections" => \%INFLRO,
# "enStopWords" => \%ENSTOPWORDS,
# "roStopWords" => \%ROSTOPWORDS,
# "enroDict" => $ENRODICT
#);
#symmetrical measure ready
sub scoreSentPairLP( $$$ ) {
my( $srcsentin, $trgsentin, $resources ) = @_;
#Set the resources...
my( $srclang ) = $resources->{"enLang"};
my( $trglang ) = $resources->{"roLang"};
my( $srcstopw ) = $resources->{"enStopWords"};
my( $trgstopw ) = $resources->{"roStopWords"};
my( @srcsentarr ) = @{ $srcsentin };
my( @trgsentarr ) = @{ $trgsentin };
return 0 if ( scalar( @srcsentarr ) == 0 || scalar( @trgsentarr ) == 0 );
pop( @srcsentarr ) if ( $srcsentarr[$#srcsentarr] eq "#EOS#" );
pop( @trgsentarr ) if ( $trgsentarr[$#trgsentarr] eq "#EOS#" );
return 0 if ( scalar( @srcsentarr ) == 0 || scalar( @trgsentarr ) == 0 );
return 0 if ( scalar( @srcsentarr ) / scalar( @trgsentarr ) > $SENTRATIO ||
scalar( @trgsentarr ) / scalar( @srcsentarr ) > $SENTRATIO );
#Filtering, ver 4.1
my( $srcsentstr ) = join( "", @srcsentarr );
my( $trgsentstr ) = join( "", @trgsentarr );
return 0 if ( $srcsentstr !~ /\p{IsAlpha}/ || $trgsentstr !~ /\p{IsAlpha}/ );
#my( $srcsent ) = \@srcsentarr;
#my( $trgsent ) = \@trgsentarr;
#Only content words in there...
my( $srcsent ) = [];
my( $trgsent ) = [];
foreach my $w ( @srcsentarr ) {
next if ( $w =~ /^[^[:alnum:]]+$/ );
next if ( exists( $srcstopw->{lc( $w )} ) );
push( @{ $srcsent }, $w );
}
foreach my $w ( @trgsentarr ) {
next if ( $w =~ /^[^[:alnum:]]+$/ );
next if ( exists( $trgstopw->{lc( $w )} ) );
push( @{ $trgsent }, $w );
}
my( $probsum ) = 0;
my( @probs ) = ();
my( $HALFWINCONTENT ) = 5;
my( $foundcontentword ) = 0;
my( %maxjskip ) = ();
my( $srcsentlennopunct ) = 0;
my( @foundteqlines ) = ();
for( my $i = 0; $i < scalar( @{ $srcsent } ); $i++ ) {
my( $w1 ) = $srcsent->[$i];
#next if ( $w1 =~ /^[^[:alnum:]]+$/ );
#next if ( exists( $srcstopw->{lc( $w1 )} ) );
$srcsentlennopunct++;
my( $halfwin ) = $HALFWINCONTENT;
my( $trglandingj ) = int( ( scalar( @{ $trgsent } ) / scalar( @{ $srcsent } ) ) * $i );
my( $trgleftidx ) = $trglandingj - $halfwin;
my( $trgrightidx ) = $trglandingj + $halfwin;
#Small bug fixed. From C# version of this measure.
my( $adjustright ) = 0;
my( $adjustleft ) = 0;
if ( $trgleftidx < 0 ) {
$trgleftidx = 0;
$adjustright = 1;
}
if ( $trgrightidx >= scalar( @{ $trgsent } ) ) {
$trgrightidx = scalar( @{ $trgsent } ) - 1;
$adjustleft = 1;
}
if ( $adjustright ) {
while ( $trgrightidx - $trgleftidx < 2 * $halfwin ) {
$trgrightidx++;
}
if ( $trgrightidx >= scalar( @{ $trgsent } ) ) {
$trgrightidx = scalar( @{ $trgsent } ) - 1;
}
}
if ( $adjustleft ) {
while ( $trgrightidx - $trgleftidx < 2 * $halfwin ) {
$trgleftidx--;
}
if ( $trgleftidx < 0 ) {
$trgleftidx = 0;
}
}
#end of small bug
my( $maxp ) = 0;
my( $maxj ) = -1;
my( $foundteq ) = 0;
for ( my $j = $trgleftidx; $j <= $trgrightidx; $j++ ) {
next if ( exists( $maxjskip{$j} ) );
my( $w2 ) = $trgsent->[$j];
#next if ( $w2 =~ /^[^[:alnum:]]+$/ );
#next if ( exists( $trgstopw->{lc( $w2 )} ) );
my( $tpprob ) = 0;
my( $w1w2ss ) = strsim::similarity( lc( normalizeWord( $w1, $srclang ) ), lc( normalizeWord( $w2, $trglang ) ) );
if ( $w1 eq $w2 ) {
$tpprob = 1;
$foundteq = 1;
push( @foundteqlines, "pdataworker::scoreSentPair[$srclang-$trglang]: found <'$w1', '$w2'> with prob = $tpprob.\n" );
}
elsif ( $w1w2ss >= $SSTHR ) {
$tpprob = $w1w2ss;
$foundteq = 1;
push( @foundteqlines, "pdataworker::scoreSentPair[$srclang-$trglang]: found <'$w1', '$w2'> with prob = $tpprob.\n" );
}
else {
my( $w1w2prob ) = pairInDict( $w1, $w2, $resources );
if ( $w1w2prob > 0 ) {
$tpprob = $w1w2prob;
$foundteq = 1;
push( @foundteqlines, "pdataworker::scoreSentPair[$srclang-$trglang]: found <'$w1', '$w2'> with prob = $tpprob.\n" );
}
}
if ( $maxp < $tpprob ) {
$maxp = $tpprob;
$maxj = $j;
}
} #end trg
if ( $foundteq ) {
$probsum += $maxp;
push( @probs, $maxp );
$foundcontentword = 1;
$maxjskip{$maxj} = [ $i, $maxp ] if ( $maxj >= 0 );
}
} #end src
my( $score ) = 0;
my( @heurprintlines ) = ();
if ( scalar( @probs ) > 0 && $foundcontentword ) {
#How many TEQs did we find
my( $T ) = scalar( @probs );
#How long is the source sentence (no punctuation)
my( $S ) = $srcsentlennopunct;
my( $averagesum ) = 0;
foreach my $p ( @probs ) {
$averagesum += $p;
}
#Arithmetic mean destabilized by how many words from the source sentence have translations...
my( $score1 ) = ( ( $T / $S ) ** ( $S / $T ) ) * ( $averagesum / $T );
my( $score2 ) = 0;
my( $featTranslationStrength ) = sub {
return $score1;
};
my( @heuristics ) = (
\&featStartOrEndWithTranslations,
\&featHaveFinalPunct,
\&featAlignNotScrambled,
$featTranslationStrength
);
my( @rejectheuristics ) = (
[ \&boolHaveSameNumbers, "boolHaveSameNumbers" ]
);
my( @heuristicsnamesandweights ) = (
[ "featStartOrEndWithTranslations", 0.14 ],
[ "featHaveFinalPunct", 0.02 ],
[ "featAlignNotScrambled", 0.14 ],
[ "featTranslationStrength", 0.7 ]
);
foreach my $hn ( @rejectheuristics ) {
my( $heur, $hname ) = @{ $hn };
if ( ! $heur->( $srcsent, $trgsent ) ) {
if ( $DEBUG ) {
print( STDERR "pdataworker::scoreSentPair[$srclang-$trglang]:-----------------------------------------------------------\n" );
print( STDERR join( "", @foundteqlines ) );
print( STDERR "pdataworker::scoreSentPair[$srclang-$trglang]:\n" );
print( STDERR "\t" . join( " ", @{ $srcsent } ) . "\n" );
print( STDERR "\t" . join( " ", @{ $trgsent } ) . "\n" );
print( STDERR "\t" . "REJECTED by '$hname'" . "\n" );
print( STDERR "pdataworker::scoreSentPair[$srclang-$trglang]:-----------------------------------------------------------\n" );
}
return 0;
}
}
for ( my $k = 0; $k < scalar( @heuristics ); $k++ ) {
my( $h ) = $heuristics[$k];
my( $hn ) = $heuristicsnamesandweights[$k]->[0];
my( $hw ) = $heuristicsnamesandweights[$k]->[1];
my( $hval ) = 0;
if ( $hn eq "featAlignNotScrambled" || $hn eq "featStartOrEndWithTranslations" ) {
$hval = $h->( \%maxjskip, scalar( @{ $srcsent } ) - 1, scalar( @{ $trgsent } ) - 1 );
}
else {
$hval = $h->( $srcsent, $trgsent );
}
$score2 += $hval * $hw;
push( @heurprintlines, "pdataworker::scoreSentPair[$srclang-$trglang]: '$hn' is '" . $hval * $hw . "'.\n" );
}
$score = $score2;
}
if ( $score > 0 ) {
if ( $DEBUG ) {
print( STDERR "pdataworker::scoreSentPair[$srclang-$trglang]:-----------------------------------------------------------\n" );
print( STDERR join( "", @foundteqlines ) );
print( STDERR join( "", @heurprintlines ) );
print( STDERR "pdataworker::scoreSentPair[$srclang-$trglang]:\n" );
print( STDERR "\t" . join( " ", @{ $srcsent } ) . "\n" );
print( STDERR "\t" . join( " ", @{ $trgsent } ) . "\n" );
print( STDERR "\t" . $score . "\n" );
print( STDERR "pdataworker::scoreSentPair[$srclang-$trglang]:-----------------------------------------------------------\n" );
}
}
return $score;
}
sub isNamedEntity( $ ) {
return 1 if ( $_[0] =~ /^[0-9:,.]+$/ );
return 1 if ( $_[0] =~ /^\p{IsUpper}\p{IsLower}+$/ );
return 1 if ( $_[0] =~ /^\p{IsUpper}+$/ );
return 0;
}
#0 or 1
sub boolHaveSameNumbers( $$ ) {
my( $srcsent, $trgsent ) = @_;
my( %foundsrcent ) = ();
for ( my $i = 0; $i < scalar( @{ $srcsent } ); $i++ ) {
my( $srcw ) = $srcsent->[$i];
if ( $srcw =~ /^[0-9][0-9\/:;.,-]*[0-9]$/ || $srcw =~ /^[0-9]$/ ) {
$foundsrcent{$srcw} = 1;
}
}
for ( my $i = 0; $i < scalar( @{ $trgsent } ); $i++ ) {
my( $trgw ) = $trgsent->[$i];
if ( $trgw =~ /^[0-9][0-9\/:;.,-]*[0-9]$/ || $trgw =~ /^[0-9]$/ ) {
if ( ! exists( $foundsrcent{$trgw} ) ) {
return 0;
}
else {
delete( $foundsrcent{$trgw} );
}
}
}
return 0 if ( scalar( keys( %foundsrcent ) ) > 0 );
return 1;
}
#0 or 1
sub featHaveFinalPunct( $$ ) {
my( @srcsent ) = @{ $_[0] };
my( @trgsent ) = @{ $_[1] };
my( $lastsrcw ) = pop( @srcsent );
my( $lasttrgw ) = pop( @trgsent );
return 0 if ( $lastsrcw =~ /^[^[:alnum:]]+$/ && $lasttrgw !~ /^[^[:alnum:]]+$/ );
return 0 if ( $lastsrcw !~ /^[^[:alnum:]]+$/ && $lasttrgw =~ /^[^[:alnum:]]+$/ );
return 1;
}
#0 or 1
#Structure:
#{ target js to source is along with teq prob }.
# j => [ i, 0.3 ]
sub featStartOrEndWithTranslations( $$$ ) {
my( $maxjskip, $maxi, $maxj ) = @_;
my( @sortj ) = sort { $a <=> $b } keys( %{ $maxjskip } );
my( $lefttranslate ) = 0;
my( $righttranslate ) = 0;
my( $j ) = 0;
while ( $j <= $#sortj && $sortj[$j] <= 2 ) {
if ( $maxjskip->{$sortj[$j]}->[0] <= 2 ) {
$lefttranslate = 1 if ( $maxjskip->{$sortj[$j]}->[1] >= $SUREGIZAPPTHR );
last;
}
$j++;
}
$j = $#sortj;
while ( $j >= 0 && abs( $maxj - $sortj[$j] ) <= 2 ) {
if ( abs( $maxi - $maxjskip->{$sortj[$j]}->[0] ) <= 2 ) {
$righttranslate = 1 if ( $maxjskip->{$sortj[$j]}->[1] >= $SUREGIZAPPTHR );
last;
}
$j--;
}
return $lefttranslate & $righttranslate;
}
#0..1 (close to 1 not scrambled)
sub featAlignNotScrambled( $$$ ) {
my( $maxjskip, $maxi, $maxj ) = @_;
return 0 if ( scalar( keys( %{ $maxjskip } ) ) == 0 );
return 0 if ( $maxi == 0 || $maxj == 0 );
my( $scrambled ) = 0;
foreach my $j ( keys( %{ $maxjskip } ) ) {
$scrambled += abs( $j / $maxj - $maxjskip->{$j}->[0] / $maxi );
}
my( $alno ) = scalar( keys( %{ $maxjskip } ) );
return 1 - $scrambled / $alno;
}
#3.0 ok.
sub readStopWordsList( $ ) {
my( %swl ) = ();
open( SWL, "< $_[0]" ) or die( "pdataworker::readStopWordsList: cannot open file '$_[0]' !\n" );
binmode( SWL, ":utf8" );
while ( my $line = <SWL> ) {
$line =~ s/^\s+//;
$line =~ s/\s+$//;
#Sh and tz variations for Romanian
do {
my( $templ ) = $line;
$templ =~ s/\x{0163}/\x{021B}/g;
$swl{lc( $templ )} = 1;
$templ =~ s/\x{015F}/\x{0219}/g;
$swl{lc( $templ )} = 1;
};
do {
my( $templ ) = $line;
$templ =~ s/\x{0163}/\x{021B}/g;
$swl{lc( $templ )} = 1;
$templ =~ s/\x{0219}/\x{015F}/g;
$swl{lc( $templ )} = 1;
};
do {
my( $templ ) = $line;
$templ =~ s/\x{021B}/\x{0163}/g;
$swl{lc( $templ )} = 1;
$templ =~ s/\x{015F}/\x{0219}/g;
$swl{lc( $templ )} = 1;
};
do {
my( $templ ) = $line;
$templ =~ s/\x{021B}/\x{0163}/g;
$swl{lc( $templ )} = 1;
$templ =~ s/\x{0219}/\x{015F}/g;
$swl{lc( $templ )} = 1;
};
$swl{lc( $line )} = 1;
} #end all stop words
close( SWL );
return %swl;
}
#3.0 ok.
sub normalizeWord( $$ ) {
my( $word ) = $_[0];
my( $lang ) = $_[1];
if ( ( $SRCL eq "en" && $TRGL eq "ro" ) || ( $SRCL eq "ro" && $TRGL eq "en" ) ) {
#en-ro specific normalizations...
#acirc
$word =~ s/\x{00E2}/a/g if ( $lang eq "ro" );
#Acirc
$word =~ s/\x{00C2}/A/g if ( $lang eq "ro" );
#icirc
$word =~ s/\x{00EE}/i/g if ( $lang eq "ro" );
#Icirc
$word =~ s/\x{00CE}/I/g if ( $lang eq "ro" );
#abreve
$word =~ s/\x{0103}/a/g if ( $lang eq "ro" );
#Abreve
$word =~ s/\x{0102}/A/g if ( $lang eq "ro" );
#scedil
$word =~ s/\x{015F}/s/g if ( $lang eq "ro" );
$word =~ s/\x{0219}/s/g if ( $lang eq "ro" );
#Scedil
$word =~ s/\x{015E}/S/g if ( $lang eq "ro" );
$word =~ s/\x{0218}/S/g if ( $lang eq "ro" );
#tcedil
$word =~ s/\x{0163}/t/g if ( $lang eq "ro" );
$word =~ s/\x{021B}/t/g if ( $lang eq "ro" );
#Tcedil
$word =~ s/\x{0162}/T/g if ( $lang eq "ro" );
$word =~ s/\x{021A}/T/g if ( $lang eq "ro" );
#'f' for 'ph'
$word =~ s/[pP][hH]/f/g if ( $lang eq "en" );
#remove 'h' in front of a consonant
$word =~ s/[Hh][^aeiouy]//g if ( $lang eq "en" );
#remove double consonant
$word =~ s/([^aeiouy])\1/$1/g if ( $lang eq "en" );
#other things go here...
}
return $word;
}
#3.0 ok.
sub lemmatizeWord( $$ ) {
my( $word, $infllist ) = @_;
#Endings are in lowercase
$word = lc( $word );
my( @wordlett ) = split( //, $word );
my( $lemword ) = $word;
#Match the longest suffix ...
for ( my $i = $#wordlett; $i >= 1; $i-- ) {
my( $crtsfx ) = join( "", @wordlett[$i .. $#wordlett] );
if ( exists( $infllist->{$crtsfx} ) ) {
$lemword = $word;
$lemword =~ s/${crtsfx}$//;
}
}
return $lemword;
}
#3.0 ok.
sub readInflectionList( $ ) {
my( %infl ) = ( "LONGEST" => 0 );
open( INF, "< $_[0]" ) or die( "pdataworker::readInflectionList: cannot open file '$_[0]' !\n" );
binmode( INF, ":utf8" );
while ( my $line = <INF> ) {
$line =~ s/^\s+//;
$line =~ s/\s+$//;
#Sh and tz variations for Romanian
do {
my( $templ ) = $line;
$templ =~ s/\x{0163}/\x{021B}/g;
$infl{lc( $templ )} = length( lc( $templ ) );
$templ =~ s/\x{015F}/\x{0219}/g;
$infl{lc( $templ )} = length( lc( $templ ) );
};
do {
my( $templ ) = $line;
$templ =~ s/\x{0163}/\x{021B}/g;
$infl{lc( $templ )} = length( lc( $templ ) );
$templ =~ s/\x{0219}/\x{015F}/g;
$infl{lc( $templ )} = length( lc( $templ ) );
};
do {
my( $templ ) = $line;
$templ =~ s/\x{021B}/\x{0163}/g;
$infl{lc( $templ )} = length( lc( $templ ) );
$templ =~ s/\x{015F}/\x{0219}/g;
$infl{lc( $templ )} = length( lc( $templ ) );
};
do {
my( $templ ) = $line;
$templ =~ s/\x{021B}/\x{0163}/g;
$infl{lc( $templ )} = length( lc( $templ ) );
$templ =~ s/\x{0219}/\x{015F}/g;
$infl{lc( $templ )} = length( lc( $templ ) );
};
$infl{lc( $line )} = length( lc( $line ) );
if ( $infl{"LONGEST"} < length( lc( $line ) ) ) {
$infl{"LONGEST"} = length( lc( $line ) );
}
}
close( INF );
return %infl;
}
#3.0 ok.
sub tokenizeText( $ ) {
my( $text ) = $_[0];
my( @toktext ) = split( /\s+/, $text );
my( @finaltoktext ) = ();