-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpexacc2.pl
1186 lines (929 loc) · 32.7 KB
/
pexacc2.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
# PEXACC2: a Parallel phrase EXtrActor from Comparable Corpora
# Parallelism level is now on the scoring parallel phrases level.
#
# (C) ICIA 2011, Radu Ion.
#
# ver 0.1, 22.08.2011, Radu ION: added \t separator in document pairs.
# ver 0.2, 04.10.2011, Radu ION: Windows/Unix portable.
# ver 2.0, 11.10.2011, Radu ION: a complete new way of parallel computation. The functionality remains.
# ver 2.01, 31.10.2011, Radu ION: added document pair info to output.
# ver 2.02, 3.11.2011, Radu ION: added selective debugging messages.
# ver 2.5, 19.11.2011, Radu ION: added local file reading for the master node.
# ver 2.6, 19.11.2011, Radu ION: resulting phrase file is now smaller (output phrase pair only if >= OUTPUTTHR)
# ver 2.7, 19.11.2011, Radu ION: fixed a sentence splitting bug (empty sentences were generated).
# ver 3.0, 23.11.2011, Radu ION: heavy modifications: no NFS, scp between master and worker, all files copied on each cluster node.
# ver 3.1, 15.12.2011, Radu ION: added identification of remote worker
# ver 4.0, 15.12.2011, Radu ION: added symmetrical similarity measure
use strict;
use warnings;
use strsim;
use IO::Handle;
use File::Spec;
use File::Path;
use Sys::Hostname;
use Time::HiRes qw( time alarm sleep );
#Modifiy this file to get new config values.
#Or, pass the appropriate values by command line (see below).
use pexacc2conf;
sub readDocPairs( $ );
sub readCluster( $ );
sub readClusterIPs( $ );
sub distributeEvenly( $$$ );
sub runInParallel( $$$$ );
sub extractGIZAPPDict( $$ );
sub cleanTemp();
sub normalizeLang( $ );
sub readCmdLineArguments( @ );
sub readSplitMarkers( $ );
sub splitSentences( $ );
sub readDocument( $$ );
sub splitSentencesAgain( $$ );
sub parallelScorePPairs( $$$$$ );
sub extractPPhrases( $$$$$ );
sub findClusters( $$$$ );
sub tokenizeText( $ );
if ( scalar( @ARGV ) < 2 ) {
die( "Usage: pexacc2.pl \\
[--source en] [--target ro] \\
[--param GIZAPPEXE=/usr/local/giza++-1.0.5/bin/GIZA++] \\
[--param PLAIN2SNTEXE=/usr/local/giza++-1.0.5/bin/plain2snt.out] \\
[--param CLUSTERFILE=generate] \\
[--param SENTRATIO=1.5] \\
[--param SPLITMODE=chunk] \\
[--param OUTPUTTHR=0.2] \\
[--param GIZAPPITERATIONS=3] \\
--input <--output file from emacc.pl or equivalent> \\
[--output <output file>]\n" );
}
my( $cmdlineconf ) = readCmdLineArguments( @ARGV );
my( $pexaccconf ) = pexacc2conf->new( $cmdlineconf );
####### CONFIG #############
#PLEASE DO NOT MODIFY HERE!!
#Use the pdataextractconf.pm file so as the changes are also visibile in pdataworker.pl
my( $SRCL ) = $pexaccconf->{"SRCL"};
my( $TRGL ) = $pexaccconf->{"TRGL"};
my( $PEXACCWORKINGDIR ) = $pexaccconf->{"PEXACCWORKINGDIR"};
my( $TMPDIR ) = $pexaccconf->{"TMPDIR"};
my( $DOCPAIRS ) = readDocPairs( $cmdlineconf->{"INPUTFILE"} );
my( $CORPUSNAME ) = $pexaccconf->{"CORPUSNAME"};
my( $OUTFILEBN ) = $pexaccconf->{"OUTFILEBN"};
my( $OUTFILE ) = $pexaccconf->{"OUTFILE"};
my( $CLUSTERFILE ) = $pexaccconf->{"CLUSTERFILE"};
my( $LEARNTDICTFILEST ) = $pexaccconf->{"LEARNTDICTFILEST"};
my( $LEARNTDICTFILETS ) = $pexaccconf->{"LEARNTDICTFILETS"};
my( $GIZAPPNEWDICTDIR ) = $pexaccconf->{"GIZAPPNEWDICTDIR"};
my( $ITERATIONS ) = $pexaccconf->{"GIZAPPITERATIONS"};
my( $DICTTHR ) = $pexaccconf->{"GIZAPPPARALLELTHR"};
my( $GIZAEXE ) = $pexaccconf->{"GIZAPPEXE"};
my( $GIZACFG ) = $pexaccconf->{"GIZAPPCONF"};
my( $PLN2SNT ) = $pexaccconf->{"PLAIN2SNTEXE"};
### Config from old pdataworker
my( %ENMARKERS ) = readSplitMarkers( $pexaccconf->{"ENMARKERSFILE"} );
my( %ROMARKERS ) = readSplitMarkers( $pexaccconf->{"ROMARKERSFILE"} );
my( $SPLITMODE ) = $pexaccconf->{"SPLITMODE"};
my( $OUTPUTTHR ) = $pexaccconf->{"OUTPUTTHR"};
my( $IDENTICALPHRTHR ) = $pexaccconf->{"IDENTICALPHRTHR"};
my( $CLUSTERLIM ) = $pexaccconf->{"CLUSTERLIM"};
my( $SENTRATIO ) = $pexaccconf->{"SENTRATIO"};
my( $DEBUG ) = $pexaccconf->{"DEBUG"};
####### End Config #########
#################### Start main ######################################################################
cleanTemp();
my( $LastOutFileName ) = "";
#GIZA++ iterations
for ( my $it = 1; $it <= $ITERATIONS; $it++ ) {
my( $dpnumber ) = 0;
#Collect the results (parallel phrases)
my( $outfilename ) = $OUTFILEBN . "-${it}.txt";
$LastOutFileName = $outfilename;
open( OUTF, ">", $outfilename ) or die( "pexacc2::main[$it]: cannot open file '$outfilename' because '$!' !\n" );
binmode( OUTF, ":utf8" );
OUTF->autoflush( 1 );
#For each document pair, parallel compute the comparability threshold for each pair of phrases.
foreach my $dp ( @{ $DOCPAIRS } ) {
$dpnumber++;
print( STDERR "pexacc2::main[$it]: processing document pair no. $dpnumber out of " . scalar( @{ $DOCPAIRS } ) . " document pairs.\n" );
my( $srcd, $trgd ) = @{ $dp };
extractPPhrases( $srcd, $trgd, *OUTF{"IO"}, $dpnumber, $it );
} #end all document pairs.
close( OUTF );
#3. GIZA++ training and new dictionary generation
extractGIZAPPDict( $it, $outfilename ) if ( $it + 1 <= $ITERATIONS );
} #end GIZA++ interations.
#Copy the last output file in the designated config file.
if ( defined( $OUTFILE ) && $OUTFILE ne "" ) {
pexacc2conf::portableCopyFile2File( $LastOutFileName, $OUTFILE );
}
##################### End new main ###################################################################
#Doing general clean-up from a previous run so as to start clean.
#pexacc2 ok.
sub cleanTemp() {
pexacc2conf::portableRemoveFileFromDir( $PEXACCWORKINGDIR, "*.in" );
pexacc2conf::portableRemoveFileFromDir( $PEXACCWORKINGDIR, "*.ready" );
pexacc2conf::portableRemoveFileFromDir( $PEXACCWORKINGDIR, "*.out" );
pexacc2conf::portableRemoveAllFilesFromDir( $GIZAPPNEWDICTDIR );
pexacc2conf::portableRemoveAllFilesFromDir( $TMPDIR );
}
#pexacc2 ok.
sub readDocPairs( $ ) {
my( @pairs ) = ();
my( $lcnt ) = 0;
open( DL, "< $_[0]" ) or die( "pexacc2::readDocList: cannot open file $_[0] !\n" );
binmode( DL, ":utf8" );
while ( my $line = <DL> ) {
$lcnt++;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
my( $srcd, $trgd, $score ) = split( /\t+/, $line );
do {
warn( "pexacc2::readDocList: not defined source document @ line $lcnt !\n" );
next;
}
if ( ! defined( $srcd ) || $srcd eq "" || ( ! -f( $srcd ) ) );
do {
warn( "pexacc2::readDocList: not defined target document @ line $lcnt !\n" );
next;
}
if ( ! defined( $trgd ) || $trgd eq "" || ( ! -f( $trgd ) ) );
push( @pairs, [ $srcd, $trgd ] );
}
close( DL );
return \@pairs;
}
#pexacc2 ok.
sub readCluster( $ ) {
my( %cluster ) = ();
open( CLST, "< $_[0]" ) or die( "pexacc2::readCluster: cannot open file '$_[0]' !\n" );
while ( my $line = <CLST> ) {
$line =~ s/^\s+//;
$line =~ s/\s+$//;
next if ( $line =~ /^#/ );
next if ( $line =~ /^$/ );
my( $hostname, $ip, $cpuid ) = split( /\s+/, $line );
$cluster{$hostname . "." . $cpuid} = $ip;
}
close( CLST );
return %cluster;
}
#pexacc2 ok.
sub readClusterIPs( $ ) {
my( %cluster ) = ();
open( CLST, "< $_[0]" ) or die( "pexacc2::readClusterIPs: cannot open file '$_[0]' !\n" );
while ( my $line = <CLST> ) {
$line =~ s/^\s+//;
$line =~ s/\s+$//;
next if ( $line =~ /^#/ );
next if ( $line =~ /^$/ );
my( $hostname, $ip, $cpuid ) = split( /\s+/, $line );
$cluster{$ip} = $hostname;
}
close( CLST );
return %cluster;
}
#pexacc2 ok.
sub extractGIZAPPDict( $$ ) {
my( $it, $pphrfile ) = @_;
#Phrases that are the same (both in English for instance), are not to be considered in GIZA++ training.
#This is the string similarity threshold.
my( $IDENTICALPHRTHR ) = 0.95;
#If to GIZA++ reapeating pairs of phrases.
#Put 1 if you want to skip repeating pairs of phrases.
my( $DONOTOUTPUTREPEATPHR ) = 0;
#1. Read the output of pexacc2.pl
my( @corpus ) = ();
my( $srcsent, $trgsent, $score ) = ( "", "", 0 );
my( $lcnt ) = 0;
open( TXT, "< $pphrfile" ) or die( "pexacc2::extractGIZAPPDict[$it]: cannot open file '$pphrfile' !\n" );
binmode( TXT, ":utf8" );
while ( my $line = <TXT> ) {
$lcnt++;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
if ( $line eq "" ) {
my( $tstsrcs ) = $srcsent;
my( $tsttrgs ) = $trgsent;
$tstsrcs =~ s/[^[:alpha:]]//g;
$tsttrgs =~ s/[^[:alpha:]]//g;
$tstsrcs = lc( $tstsrcs );
$tsttrgs = lc( $tsttrgs );
#Only if the two fragments are not identical.
if ( strsim::similarity( $tstsrcs, $tsttrgs ) < $IDENTICALPHRTHR ) {
#Only if the parallel threshold exceeds the limit.
if ( $score >= $DICTTHR ) {
push( @corpus, [ $srcsent, $trgsent, $score ] );
}
}
$srcsent = "";
$trgsent = "";
$score = 0;
}
else {
if ( $srcsent eq "" ) {
$srcsent = $line;
}
elsif ( $trgsent eq "" ) {
$trgsent = $line;
}
elsif ( $score == 0 && $line =~ /^[0-9]+(?:\.[0-9]+)?$/ ) {
$score = $line;
}
else {
warn( "pexacc2::extractGIZAPPDict[$it]: line format error in '$line' @ $lcnt !\n" );
}
}
}
close( TXT );
my( @deletethesefiles ) = ();
#2. Write GIZA++ text files...
my( $gizasrctxtbn ) = $OUTFILEBN . "-${it}_${SRCL}";
my( $gizasrctxtfile ) = $gizasrctxtbn . ".txt";
my( $gizatrgtxtbn ) = $OUTFILEBN . "-${it}_${TRGL}";
my( $gizatrgtxtfile ) = $gizatrgtxtbn . ".txt";
push( @deletethesefiles, $gizasrctxtfile );
push( @deletethesefiles, $gizatrgtxtfile );
open( SRC, ">", $gizasrctxtfile ) or die( "pexacc2::extractGIZAPPDict[$it]: cannot open file '$gizasrctxtfile' !\n" );
open( TRG, ">", $gizatrgtxtfile ) or die( "pexacc2::extractGIZAPPDict[$it]: cannot open file '$gizatrgtxtfile' !\n" );
binmode( SRC, ":utf8" );
binmode( TRG, ":utf8" );
my( %alreadyout ) = ();
foreach my $ct ( @corpus ) {
if ( $DONOTOUTPUTREPEATPHR ) {
my( $key ) = $ct->[0] . "#" . $ct->[1];
if ( ! exists( $alreadyout{$key} ) ) {
print( SRC $ct->[0] . "\n" );
print( TRG $ct->[1] . "\n" );
$alreadyout{$key} = 1;
}
}
else {
print( SRC $ct->[0] . "\n" );
print( TRG $ct->[1] . "\n" );
}
}
close( TRG );
close( SRC );
#3. Prepare GIZA++ internal artefacts...
#3.1 plain2snt.out
my( $gizacmd1 ) = "$PLN2SNT $gizasrctxtfile $gizatrgtxtfile";
warn( "pexacc2::extractGIZAPPDict[$it]: running `$gizacmd1`\n" )
if ( $DEBUG );
pexacc2conf::portableVerboseSystem( $gizacmd1 );
my( $gizasrcvocabfile ) = $gizasrctxtbn . ".vcb";
#Check for plain2snt.out artefacts...
die( "pexacc2::extractGIZAPPDict[$it]: vocabulary file '" . $gizasrcvocabfile . "' does not exist!\n" )
if ( ! -f( $gizasrcvocabfile ) );
push( @deletethesefiles, $gizasrcvocabfile );
my( $gizatrgvocabfile ) = $gizatrgtxtbn . ".vcb";
die( "pexacc2::extractGIZAPPDict[$it]: vocabulary file '" . $gizatrgvocabfile . "' does not exist!\n" )
if ( ! -f( $gizatrgvocabfile ) );
push( @deletethesefiles, $gizatrgvocabfile );
#With different versions of GIZA++, this may not be the case!
my( $gizacorpusfile ) = $gizatrgtxtbn . "_" . $gizasrctxtbn . ".snt";
die( "pexacc2::extractGIZAPPDict[$it]: corpus file '" . $gizacorpusfile . "' does not exist!\n" )
if ( ! -f( $gizacorpusfile ) );
push( @deletethesefiles, $gizacorpusfile );
#3.2 write the .gizacfg temp file using the master config...
my( $gizatempcfgfile ) = $GIZACFG;
$gizatempcfgfile =~ s/\.[^\.]+$//;
$gizatempcfgfile .= "-temp.gizacfg";
pexacc2conf::portableCopyFile2File( $GIZACFG, $gizatempcfgfile );
my( $gizaprefix ) = "$SRCL-$TRGL-gizapp-$CORPUSNAME-$it";
open( GZCFG, ">>", $gizatempcfgfile ) or die( "pexacc2::extractGIZAPPDict[$it]: cannot open file '$gizatempcfgfile' !\n" );
print( GZCFG "l\n" );
print( GZCFG "o $gizaprefix\n" );
print( GZCFG "c $gizacorpusfile\n" );
print( GZCFG "d\n" );
print( GZCFG "s $gizatrgvocabfile\n" );
print( GZCFG "t $gizasrcvocabfile\n" );
print( GZCFG "tc\n" );
close( GZCFG );
push( @deletethesefiles, $gizatempcfgfile );
#4. Running GIZA++
my( $gizacmd2 ) = "$GIZAEXE $gizatempcfgfile";
warn( "pexacc2::extractGIZAPPDict[$it]: running `$gizacmd2`\n" )
if ( $DEBUG );
#Let's see the GIZA++ output...
pexacc2conf::portableVerboseSystem( $gizacmd2 );
#5. Writing new dictionary for pdataworker.pl to read in.
my( $gizanewdict ) = $gizaprefix . ".actual.ti.final";
open( NEWD, "<", $gizanewdict ) or die( "pexacc2::extractGIZAPPDict[$it]: cannot open file '$gizanewdict' !\n" );
binmode( NEWD, ":utf8" );
#A single dictionary per iteration.
#The master and the learnt dictionaries will be linearly combined.
#Source to target...
my( $newlearntdictfilest ) = $LEARNTDICTFILEST;
#WARNING: the name of the new dictionary MUST begin with $SRCL-$TRGL-... or $TRGL-$SRCL-...
open( NEWDD, ">", $newlearntdictfilest ) or die( "pexacc2::extractGIZAPPDict[$it]: cannot open file '$newlearntdictfilest' !\n" );
binmode( NEWDD, ":utf8" );
while ( my $line = <NEWD> ) {
$line =~ s/^\s+//;
$line =~ s/\s+$//;
my( $srcw, $trgw, $prob ) = split( /\s+/, $line );
next if ( $srcw eq "NULL" || $trgw eq "NULL" );
print( NEWDD $srcw . "\t" . $trgw . "\t" . $prob . "\n" );
}
close( NEWDD );
close( NEWD );
open( NEWD, "<", $gizanewdict ) or die( "pexacc2::extractGIZAPPDict[$it]: cannot open file '$gizanewdict' !\n" );
binmode( NEWD, ":utf8" );
#Target to source...
my( $newlearntdictfilets ) = $LEARNTDICTFILETS;
#WARNING: the name of the new dictionary MUST begin with $SRCL-$TRGL-... or $TRGL-$SRCL-...
open( NEWDD, ">", $newlearntdictfilets ) or die( "pexacc2::extractGIZAPPDict[$it]: cannot open file '$newlearntdictfilets' !\n" );
binmode( NEWDD, ":utf8" );
while ( my $line = <NEWD> ) {
$line =~ s/^\s+//;
$line =~ s/\s+$//;
my( $srcw, $trgw, $prob ) = split( /\s+/, $line );
next if ( $srcw eq "NULL" || $trgw eq "NULL" );
print( NEWDD $trgw . "\t" . $srcw . "\t" . $prob . "\n" );
}
close( NEWDD );
close( NEWD );
push( @deletethesefiles, $gizanewdict );
#6. Clean up...
foreach my $f ( @deletethesefiles ) {
pexacc2conf::portableRemoveFile( $f );
}
pexacc2conf::portableRemoveFile( "${gizaprefix}.*" );
pexacc2conf::portableRemoveFile( "*.vcb" );
pexacc2conf::portableRemoveFile( "*.snt" );
#7. Copy the new learnt dictionary to all worker nodes...
my( %clusterinfo ) = readClusterIPs( $CLUSTERFILE );
foreach my $ip ( keys( %clusterinfo ) ) {
if ( $ip ne "127.0.0.1" && $ip ne $pexaccconf->{"MASTERIP"} ) {
pexacc2conf::portableRemoteCopy( $newlearntdictfilest, "rion", $ip, $GIZAPPNEWDICTDIR );
pexacc2conf::portableRemoteCopy( $newlearntdictfilets, "rion", $ip, $GIZAPPNEWDICTDIR );
}
}
} #end extractGIZAPPDict
#pexacc2 ready.
sub readCmdLineArguments( @ ) {
my( @args ) = @_;
my( %clconf ) = ();
my( %allowedparams ) = (
"GIZAPPEXE" => 1,
"PLAIN2SNTEXE" => 1,
"CLUSTERFILE" => 1,
"SENTRATIO" => 1,
"SPLITMODE" => 1,
"OUTPUTTHR" => 1,
"GIZAPPITERATIONS" => 1
);
while ( scalar( @args ) > 0 ) {
my( $opt ) = shift( @args );
SWOPT: {
$opt eq "--source" and do {
$clconf{"SRCL"} = normalizeLang( shift( @args ) );
last;
};
$opt eq "--target" and do {
$clconf{"TRGL"} = normalizeLang( shift( @args ) );
last;
};
$opt eq "--param" and do {
my( $param, $value ) = split( /\s*=\s*/, shift( @args ) );
$param = uc( $param );
die( "pexacc2::readCmdLineArguments: unknown parameter '$param' !\n" )
if ( ! exists( $allowedparams{$param} ) );
$clconf{$param} = $value;
last;
};
$opt eq "--input" and do {
$clconf{"INPUTFILE"} = shift( @args );
last;
};
$opt eq "--output" and do {
$clconf{"OUTFILE"} = shift( @args );
last;
};
die( "pexacc2::readCmdLineArguments: unknown option '$opt' !\n" );
}
}
return \%clconf;
}
#pexacc2 ready.
sub normalizeLang( $ ) {
my( $lang ) = lc( $_[0] );
my( %accuratlanguages ) = (
#1
"romanian" => "ro",
"rum" => "ro",
"ron" => "ro",
"ro" => "ro",
#2
"english" => "en",
"eng" => "en",
"en" => "en",
#3
"estonian" => "et",
"est" => "et",
"et" => "et",
#4
"german" => "de",
"ger" => "de",
"deu" => "de",
"de" => "de",
#5
"greek" => "el",
"gre" => "el",
"ell" => "el",
"el" => "el",
#6
"croatian" => "hr",
"hrv" => "hr",
"hr" => "hr",
#7
"latvian" => "lv",
"lav" => "lv",
"lv" => "lv",
#8
"lithuanian" => "lt",
"lit" => "lt",
"lt" => "lt",
#9
"slovenian" => "sl",
"slv" => "sl",
"sl" => "sl"
);
return $accuratlanguages{$lang} if ( exists( $accuratlanguages{$lang} ) );
die( "pexacc2::normalizeLang: unknown language '$lang' !\n" );
}
############################ Code from pdataworker that I need in PEXACC2 #########################################
#pexacc2 ready.
sub readSplitMarkers( $ ) {
my( %mark ) = ();
open( SMK, "< $_[0]" ) or die( "pexacc2::readSplitMarkers : cannot open file \'$_[0]\' !\n" );
binmode( SMK, ":utf8" );
while ( my $line = <SMK> ) {
$line =~ s/^\s+//;
$line =~ s/\s+$//;
next if ( $line =~ /_/ );
next if ( $line =~ /^$/ );
$mark{$line} = 1;
}
close( SMK );
return %mark;
}
#pexacc2 ready.
sub splitSentences( $ ) {
my( $text ) = $_[0];
#$text =~ s/((?:\p{IsAlpha}|\p{IsDigit})[.?!:]+)(\s*["'<>\(\[\{]?\s*\p{IsUpper})/$1#CUT#$2/g;
$text =~ s/(.+?(?<![\s\.]\p{IsUpper})(?<![\s\.]\p{IsUpper}[bcdfgjklmnprstvxz])(?<![\s\.]\p{IsUpper}[bcdfgjklmnprstvxz][bcdfgjklmnprstvxz])[\.?!]+)((?=\s*[\p{IsUpper}\[\(\"\']))/$1#CUT#$2/g;
$text =~ s/(?:\r?\n)+/ #CUT# /g;
my( @sentences ) = split( /#CUT#/, $text );
my( @sentences2 ) = ();
foreach my $s ( @sentences ) {
$s =~ s/^\s+//;
$s =~ s/\s+$//;
push( @sentences2, $s ) if ( defined( $s ) && $s ne "" );
}
return @sentences2;
}
sub readDocument( $$ ) {
my( $doctext ) = "";
my( $markers ) = $_[1];
open( DOC, "< $_[0]" ) or die( "pexacc2::readDocument: cannot open file '$_[0]' !\n" );
binmode( DOC, ":utf8" );
while ( my $line = <DOC> ) {
$doctext .= $line;
}
close( DOC );
my( @sentences ) = splitSentences( $doctext );
if ( $SPLITMODE eq "sent" ) {
return @sentences;
}
elsif ( $SPLITMODE eq "chunk" ) {
my( @sentenceparts ) = ();
foreach my $s ( @sentences ) {
push( @sentenceparts, splitSentencesAgain( $s, $markers ) );
}
return @sentenceparts;
}
else {
die( "pexacc2::readDocument: unknown split mode ! May be 'sent' or 'chunk' !\n" );
}
}
#pexacc2 ready.
sub splitSentencesAgain( $$ ) {
my( $sentence, $markers ) = @_;
return () if ( ! defined( $sentence ) || $sentence eq "" );
my( @swords ) = split( /\s+/, $sentence );
my( @swords2 ) = ();
my( @sparts ) = ( [] );
my( @sparts2 ) = ();
foreach my $w ( @swords ) {
if ( $w !~ /[[:alnum:]]/ ) {
push( @swords2, $w );
next;
}
my( $antepunct ) = ( $w =~ /^([^[:alnum:]]+)/ );
my( $postpunct ) = ( $w =~ /([^[:alnum:]]+)$/ );
my( $wnopunct ) = $w;
$wnopunct =~ s/^[^[:alnum:]]+//;
$wnopunct =~ s/[^[:alnum:]]+$//;
push( @swords2, $antepunct ) if ( defined( $antepunct ) && $antepunct ne "" );
push( @swords2, $wnopunct ) if ( defined( $wnopunct ) && $wnopunct ne "" );
push( @swords2, $postpunct ) if ( defined( $postpunct ) && $postpunct ne "" );
}
my( $lastmarker ) = 0;
foreach my $w ( @swords2 ) {
if ( exists( $markers->{$w} ) || exists( $markers->{lc( $w )} ) ) {
if ( ! $lastmarker ) {
my( @temp ) = ( $w );
push( @sparts, \@temp );
}
else {
push( @{ $sparts[$#sparts] }, $w );
}
$lastmarker = 1;
}
else {
push( @{ $sparts[$#sparts] }, $w );
$lastmarker = 0;
}
}
foreach my $sp ( @sparts ) {
$sp = join( " ", @{ $sp } );
if ( $sp ne "" ) {
push( @sparts2, $sp );
}
}
return ( @sparts2, "#EOS#" );
}
#pexacc2 ok.
sub distributeEvenly( $$$ ) {
my( $srcphr, $trgphr, $totalcpu ) = @_;
my( @batchfiles ) = ();
my( $phrcount ) = 0;
my( $howmany ) = 0;
my( $cpucount ) = 1;
my( $crtbfile ) = File::Spec->catfile( $TMPDIR, "$SRCL-$TRGL-phrase-batch-$cpucount.pp" );
foreach my $s ( @{ $srcphr } ) {
next if ( $s eq "#EOS#" );
foreach my $t ( @{ $trgphr } ) {
next if ( $t eq "#EOS#" );
$howmany++;
}
}
print( STDERR "pexacc2::distributeEvenly: writing to file '$crtbfile'...\n" )
if ( $DEBUG );
open( PAIRS, ">", $crtbfile ) or die( "pexacc2::distributeEvenly: cannot open file '$crtbfile' !\n" );
binmode( PAIRS, ":utf8" );
for ( my $i = 0; $i < scalar( @{ $srcphr } ); $i++ ) {
my( $sp ) = $srcphr->[$i];
next if ( $sp eq "#EOS#" );
next if ( ! defined( $sp ) || $sp eq "" );
for ( my $j = 0; $j < scalar( @{ $trgphr } ); $j++ ) {
my( $tp ) = $trgphr->[$j];
next if ( $tp eq "#EOS#" );
next if ( ! defined( $tp ) || $tp eq "" );
$phrcount++;
if ( $phrcount >= $howmany / $totalcpu && $cpucount < $totalcpu ) {
push( @batchfiles, [ $crtbfile, $phrcount ] );
close( PAIRS );
$phrcount = 1;
$cpucount++;
$crtbfile = File::Spec->catfile( $TMPDIR, "$SRCL-$TRGL-phrase-batch-$cpucount.pp" );
print( STDERR "pexacc2::distributeEvenly: writing to file '$crtbfile'...\n" )
if ( $DEBUG );
open( PAIRS, ">", $crtbfile ) or die( "pexacc2::distributeEvenly: cannot open file '$crtbfile' !\n" );
binmode( PAIRS, ":utf8" );
}
print( PAIRS $i . "#SPLIT-HERE#" . $j . "#SPLIT-HERE#" . $sp . "#SPLIT-HERE#" . $tp . "\n" );
}
}
push( @batchfiles, [ $crtbfile, $phrcount ] );
close( PAIRS );
return @batchfiles;
}
#pexacc2 ready.
sub runInParallel( $$$$ ) {
my( $ppbatchfile ) = $_[0];
my( $mach, $machip ) = ( $_[1], $_[2] );
my( $inoutbasename ) = $_[3];
my( $thishostname ) = hostname();
my( $infile ) = File::Spec->catfile( $PEXACCWORKINGDIR, $inoutbasename . ".in" );
#Identify the remote status of the pdataworker process
if ( $mach !~ /^${thishostname}/ ) {
$pexaccconf->{"REMOTEWORKER"} = 1;
}
else {
$pexaccconf->{"REMOTEWORKER"} = 0;
}
#1. Write output for worker...
open( IN, "> " . $infile ) or die( "pexacc2::runInParallel: cannot open file " . $infile . " !\n" );
binmode( IN, ":utf8" );
#Random code
print( IN $inoutbasename . "\n" );
#Print all parameters (including those from the command line):
foreach my $p ( keys( %{ $pexaccconf } ) ) {
print( IN "--param $p" . "=" . $pexaccconf->{$p} . "\n" );
}
open( PAIRS, "<", $ppbatchfile ) or die( "pexacc2::runInParallel: cannot open batch file '$ppbatchfile' !\n" );
binmode( PAIRS, ":utf8" );
while ( my $line = <PAIRS> ) {
print( IN $line );
}
close( PAIRS );
close( IN );
#Do work.
if ( $mach !~ /^${thishostname}/ ) {
#1. Copy the work input file to the worker in $PEXACCWORKINGDIR
pexacc2conf::portableRemoteCopy( $infile, "rion", $machip, $PEXACCWORKINGDIR );
#2. Linux only. Fork and detach. $infile must be locally available!!
system( "ssh rion\@${machip} '.\/pdataworker.pl ${infile}' &" );
}
else {
warn( "pexacc2::runInParallel: executing on localhost ...\n" )
if ( $DEBUG );
#If we are on this host, no ssh is needed.
pexacc2conf::portableForkAndDetach( "perl pdataworker.pl ${infile}" );
}
}
#pexacc2 ready.
sub parallelScorePPairs( $$$$$ ) {
my( $srcsent, $trgsent, $outfileh, $dpnumber, $it ) = @_;
my( %clusterinfo ) = readCluster( $CLUSTERFILE );
my( $totalcpuno ) = scalar( keys( %clusterinfo ) );
my( @cluster ) = keys( %clusterinfo );
#Distribute the work for this document pair...
my( %checkoutfiles ) = ();
my( @phrasepairsfiles ) = distributeEvenly( $srcsent, $trgsent, $totalcpuno );
for ( my $cnt = 0; $cnt < scalar( @phrasepairsfiles ); $cnt++ ) {
my( $ppbatchfile, $loadfactor ) = @{ $phrasepairsfiles[$cnt] };
#Here we run computing on the cluster ...
my( $mach ) = shift( @cluster );
my( $inoutfbname ) =
$loadfactor . "-" .
$cnt . "-" .
$dpnumber . "-" .
$mach . "-" .
$SRCL . "-" .
$TRGL . "-" .
$CORPUSNAME;
print( STDERR "pexacc2::parallelScorePPairs[it=$it,dp=$dpnumber]: starting worker '$inoutfbname' on machine '$mach' ...\n" )
if ( $DEBUG );
$checkoutfiles{$inoutfbname} = 1;
runInParallel( $ppbatchfile, $mach, $clusterinfo{$mach}, $inoutfbname );
#Free some memory
pexacc2conf::portableRemoveFile( $ppbatchfile );
} #end all processes
#Here we must do a simple concatenation of the results...
#Wait for partial results...
my( $tries ) = 0;
my( $checkoutno ) = scalar( keys( %checkoutfiles ) );
while ( scalar( keys( %checkoutfiles ) ) > 0 ) {
foreach my $io ( keys( %checkoutfiles ) ) {
my( $infile ) = File::Spec->catfile( $PEXACCWORKINGDIR, $io . ".in" );
my( $readyfile ) = File::Spec->catfile( $PEXACCWORKINGDIR, $io . ".ready" );
my( $pdatafile ) = File::Spec->catfile( $PEXACCWORKINGDIR, $io . ".out" );
if ( -f( $readyfile ) ) {
open( PDAT, "<", $pdatafile ) or die( "pexacc2::parallelScorePPairs[it=$it,dp=$dpnumber]: cannot open file '$pdatafile' because '$!' !\n" );
binmode( PDAT, ":utf8" );
while ( my $line = <PDAT> ) {
$line =~ s/^\s+//;
$line =~ s/\s+$//;
my( $i, $j, $ss, $ts, $pprob ) = split( /#SPLIT-HERE#/, $line );
print( $outfileh $line . "\n" ) if ( $pprob >= $OUTPUTTHR );
}
close( PDAT );
print( STDERR "pexacc2::parallelScorePPairs[it=$it,dp=$dpnumber]: " . ( 1 - scalar( keys( %checkoutfiles ) ) / $checkoutno ) . "%" )
if ( $DEBUG );
pexacc2conf::portableRemoveFile( $infile );
pexacc2conf::portableRemoveFile( $readyfile );
pexacc2conf::portableRemoveFile( $pdatafile );
delete( $checkoutfiles{$io} );
}
}
$tries++;
#Sometimes files are deleted but PEXACC stucks in this loop.
#This is for breaking.
if ( $tries >= 1000 ) {
my( @filesin ) = pexacc2conf::portableListFiles( File::Spec->catfile( $PEXACCWORKINGDIR, "*.in" ) );
my( @filesout ) = pexacc2conf::portableListFiles( File::Spec->catfile( $PEXACCWORKINGDIR, "*.out" ) );
my( @filesready ) = pexacc2conf::portableListFiles( File::Spec->catfile( $PEXACCWORKINGDIR, "*.ready" ) );
if ( scalar( @filesin ) == 0 && scalar( @filesout ) == 0 ) {
print( STDERR "pexacc2::parallelScorePPairs[it=$it,dp=$dpnumber]: force break from loop.\n" )
if ( 1 );
last;
}
$tries = 0;
}
$tries++;
} #end all partial results.
} #end parallelScorePPairs.
#pexacc2 ready.
sub extractPPhrases( $$$$$ ) {
my( $srcd, $trgd, $outfile, $dpnumber, $it ) = @_;
my( @srcsent ) = readDocument( $srcd, \%ENMARKERS );
my( @trgsent ) = readDocument( $trgd, \%ROMARKERS );
my( $pphrasesfile ) = File::Spec->catfile( $TMPDIR, "$it-$SRCL-$TRGL-$dpnumber-$CORPUSNAME-extracted.pdat" );
open( PDEX, ">", $pphrasesfile ) or die( "pexacc2::extractPPhrases[it=$it,dp=$dpnumber]: cannot open file '$pphrasesfile' for writing!\n" );
binmode( PDEX, ":utf8" );
parallelScorePPairs( \@srcsent, \@trgsent, *PDEX{"IO"}, $dpnumber, $it );
close( PDEX );
my( %mappedtxtunits ) = ();
open( PDAT, "<", $pphrasesfile ) or die( "pexacc2::extractPPhrases[$it,dp=$dpnumber]: cannot open file '$pphrasesfile' for reading!\n" );
binmode( PDAT, ":utf8" );
while ( my $line = <PDAT> ) {
$line =~ s/^\s+//;
$line =~ s/\s+$//;
my( $i, $j, $ss, $ts, $pprob ) = split( /#SPLIT-HERE#/, $line );
SWPHRTYPE: {
( $SPLITMODE eq "sent" || $CLUSTERLIM == 0 ) and do {
print( $outfile $ss . "\n" . $ts . "\n" . $pprob . "\n\n" )
if (
$pprob >= $OUTPUTTHR &&
strsim::similarity( $ss, $ts ) < $IDENTICALPHRTHR
);
last;
};
( $SPLITMODE eq "chunk" && $CLUSTERLIM > 0 ) and do {
if ( $pprob >= $OUTPUTTHR ) {
if ( ! exists( $mappedtxtunits{$i} ) ) {
$mappedtxtunits{$i} = { $j => $pprob };
}
elsif ( ! exists( $mappedtxtunits{$i}->{$j} ) ) {
$mappedtxtunits{$i}->{$j} = $pprob;
}
}
last;
};
} #end splitmode
} #end all phrase pairs.
close( PDAT );
if ( $SPLITMODE eq "chunk" && $CLUSTERLIM > 0 ) {
my( $previ ) = -1;
my( @crticluster ) = ();
my( @crtjclusters ) = ();
#Search for adjacent mappings... and align.
foreach my $i ( sort { $a <=> $b } keys( %mappedtxtunits ) ) {
if ( $previ >= 0 ) {
my( $subeos ) = sub {
my( $eos ) = 0;
for ( my $k = $previ; $k <= $i; $k++ ) {
$eos = 1 if ( $srcsent[$k] eq "#EOS#" );
}
$eos;
};
#If the phrases are near each other in the source language...
if ( $i - $previ <= $CLUSTERLIM && ! $subeos->() ) {
my( $ijshash ) = { %{ $mappedtxtunits{$i} } };
my( $previjshash ) = $crtjclusters[$#crtjclusters];
my( $connect ) = 0;
LASTJ:
foreach my $j ( keys( %{ $ijshash } ) ) {
foreach my $pj ( keys( %{ $previjshash } ) ) {
if ( abs( $j - $pj ) <= $CLUSTERLIM ) {
$connect = 1;
last LASTJ;