forked from liyuanfang/mhqg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mteval-v14.pl
1179 lines (1068 loc) · 40.8 KB
/
mteval-v14.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/env perl
use warnings;
use strict;
use utf8;
use Encode;
use XML::Twig;
use Sort::Naturally;
binmode STDOUT, ":utf8";
binmode STDERR, ":utf8";
#################################
# History:
#
# version 14
# (2016-03-29 [email protected])
# * Fixed warning message in case seg-id is a string, by sorting in correct order using Sort::Naturally.
#
# version 13b
# * Fixed die 'bug' in case seg->id = 0
#
# version 13a
# * modified the scoring functions to prevent division-by-zero errors when a system segment is empty
# * affected methods: 'bleu_score' and 'bleu_score_smoothing'
#
# version 13
# * Uses a XML parser to read data (only when extension is .xml)
# * Smoothing of the segment-level BLEU scores, done by default
# * smoothing method similar to that of bleu-1.04.pl (IBM)
# * see comments above the 'bleu_score' method for more details on how the smoothing is computed
# * added a '--no-smoothing' option to simulate old scripts behavior
# * Introduction of the 'brevity-penalty' option, taking one of two values:
# * 'closest' (default) : act as IBM BLEU (taking the closest reference translation length)
# * in case two reference translations are at the same distance, will take the shortest one
# * for more details regarding how the BP is computed, see comments of the 'brevity_penalty_closest' function
# * 'shortest' : act as previous versions of the script (taking shortest reference translation length)
# * Introduction of the 'international-tokenization' option, boolean, disabled by default
# by default (when the option is not provided), uses 11b's tokenization function
# when option specified, uses v12's tokenization function
# * Introduction of a 'Metrics MATR output' flag (option '--metricsMATR')
# when used, creates three files for both BLEU score and NIST score:
# * BLEU-seg.scr and NIST-seg.scr: contain segment-level scores
# * BLEU-doc.scr and NIST-doc.scr: contain document-level scores
# * BLEU-sys.scr and NIST-sys.scr: contain system-level scores
# * SGML parsing
# * script will halt if source, reference and test files don't share the same setid attribute value (used for metricsMATR output)
# * correct segment IDs extracted from the files (was previously using an array, and using the index as a segID for output)
# * detailed output flag (-d) can now be used when running both BLEU and NIST
#
# version 12
# * Text normalization changes:
# * convert entity references (only the entities declared in the DTD)
# * now uses unicode categories
# * tokenize punctuation unless followed AND preceded by digits
# * tokenize symbols
# * UTF-8 handling:
# * files are now read using utf8 mode
# * Added the '-e' command-line option to enclose non-ASCII characters between spaces
#
# version 11b -- text normalization modified:
# * take out the join digit line because it joins digits
# when it shouldn't have
# $norm_text =~ s/(\d)\s+(?=\d)/$1/g; #join digits
#
# version 11a -- corrected output of individual n-gram precision values
#
# version 11 -- bug fixes:
# * make filehandle operate in binary mode to prevent Perl from operating
# (by default in Red Hat 9) in UTF-8
# * fix failure on joining digits
# version 10 -- updated output to include more details of n-gram scoring.
# Defaults to generate both NIST and BLEU scores. Use -b for BLEU
# only, use -n for NIST only
#
# version 09d -- bug fix (for BLEU scoring, ngrams were fixed at 4
# being the max, regardless what was entered on the command line.)
#
# version 09c -- bug fix (During the calculation of ngram information,
# each ngram was being counted only once for each segment. This has
# been fixed so that each ngram is counted correctly in each segment.)
#
# version 09b -- text normalization modified:
# * option flag added to preserve upper case
# * non-ASCII characters left in place.
#
# version 09a -- text normalization modified:
# * " and & converted to "" and &, respectively
# * non-ASCII characters kept together (bug fix)
#
# version 09 -- modified to accommodate sgml tag and attribute
# names revised to conform to default SGML conventions.
#
# version 08 -- modifies the NIST metric in accordance with the
# findings on the 2001 Chinese-English dry run corpus. Also
# incorporates the BLEU metric as an option and supports the
# output of ngram detail.
#
# version 07 -- in response to the MT meeting on 28 Jan 2002 at ISI
# Keep strings of non-ASCII characters together as one word
# (rather than splitting them into one-character words).
# Change length penalty so that translations that are longer than
# the average reference translation are not penalized.
#
# version 06
# Prevent divide-by-zero when a segment has no evaluation N-grams.
# Correct segment index for level 3 debug output.
#
# version 05
# improve diagnostic error messages
#
# version 04
# tag segments
#
# version 03
# add detailed output option (intermediate document and segment scores)
#
# version 02
# accommodation of modified sgml tags and attributes
#
# version 01
# same as bleu version 15, but modified to provide formal score output.
#
# original IBM version
# Author: Kishore Papineni
# Date: 06/10/2001
#################################
######
# Intro
my ($date, $time) = date_time_stamp();
print "MT evaluation scorer began on $date at $time\n";
print "\ncommand line: ", $0, " ", join(" ", @ARGV), "\n";
my $usage = "\n\nUsage: $0 -r <ref_file> -s <src_file> -t <tst_file>\n\n".
"Description: This Perl script evaluates MT system performance.\n".
"\n".
"Required arguments:\n".
" -r <ref_file> is a file containing the reference translations for\n".
" the documents to be evaluated.\n".
" -s <src_file> is a file containing the source documents for which\n".
" translations are to be evaluated\n".
" -t <tst_file> is a file containing the translations to be evaluated\n".
"\n".
"Optional arguments:\n".
" -h prints this help message to STDOUT\n".
" -c preserves upper-case alphabetic characters\n".
" -b generate BLEU scores only\n".
" -n generate NIST scores only\n".
" -d detailed output flag:\n".
" 0 (default) for system-level score only\n".
" 1 to include document-level scores\n".
" 2 to include segment-level scores\n".
" 3 to include ngram-level scores\n".
" -e enclose non-ASCII characters between spaces\n".
" --brevity-penalty ( closest | shortest )\n" .
" closest (default) : acts as IBM BLEU (takes the closest reference translation length)\n" .
" shortest : acts as previous versions of the script (takes the shortest reference translation length)\n" .
" --international-tokenization\n" .
" when specified, uses Unicode-based (only) tokenization rules\n" .
" when not specified (default), uses default tokenization (some language-dependant rules)\n" .
" --metricsMATR : create three files for both BLEU scores and NIST scores:\n" .
" BLEU-seg.scr and NIST-seg.scr : segment-level scores\n" .
" BLEU-doc.scr and NIST-doc.scr : document-level scores\n" .
" BLEU-sys.scr and NIST-sys.scr : system-level scores\n" .
" --no-smoothing : disable smoothing on BLEU scores\n" .
"\n";
use vars qw ($opt_r $opt_s $opt_t $opt_d $opt_h $opt_b $opt_n $opt_c $opt_x $opt_e);
use Getopt::Long;
my $ref_file = '';
my $src_file = '';
my $tst_file = '';
my $detail = 0;
my $help = '';
my $preserve_case = '';
my $split_non_ASCII = '';
my $brevity_penalty = 'closest';
my $international_tokenization;
my $metricsMATR_output = '';
my $no_smoothing = '';
our $opt_x = '';
our $opt_b = '';
our $opt_n = '';
GetOptions(
'r=s' => \$ref_file,
's=s' => \$src_file,
't=s' => \$tst_file,
'd:i' => \$detail,
'h|help' => \$help,
'b',
'n',
'c' => \$preserve_case,
'x:s',
'e' => \$split_non_ASCII,
'brevity-penalty:s' => \$brevity_penalty,
'international-tokenization' => \$international_tokenization,
'metricsMATR-output' => \$metricsMATR_output,
'no-smoothing' => \$no_smoothing
);
die $usage if $help;
die "Error in command line: ref_file not defined$usage" unless ( $ref_file );
die "Error in command line: src_file not defined$usage" unless ( $src_file );
die "Error in command line: tst_file not defined$usage" unless ( $tst_file );
my $BLEU_BP;
if ( !( $brevity_penalty cmp 'closest' ) )
{
$BLEU_BP = \&brevity_penalty_closest;
}
elsif ( !( $brevity_penalty cmp 'shortest' ) )
{
$BLEU_BP = \&brevity_penalty_shortest;
}
else
{
die "Incorrect value supplied for 'brevity_penalty'$usage";
}
my $TOKENIZATION = \&tokenization;
$TOKENIZATION = \&tokenization_international if ( $international_tokenization );
my $BLEU_SCORE = \&bleu_score;
$BLEU_SCORE = \&bleu_score_nosmoothing if ( $no_smoothing );
my $max_Ngram = 9;
my $METHOD = "BOTH";
if ( $opt_b ) { $METHOD = "BLEU"; }
if ( $opt_n ) { $METHOD = "NIST"; }
my $method;
######
# Global variables
my ($src_lang, $tgt_lang, @tst_sys, @ref_sys); # evaluation parameters
my (%tst_data, %ref_data); # the data -- with structure: {system}{document}{segments}
my ($src_id, $ref_id, $tst_id); # unique identifiers for ref and tst translation sets
my %eval_docs; # document information for the evaluation data set
my %ngram_info; # the information obtained from (the last word in) the ngram
######
# Get source document ID's
($src_id) = get_source_info ($src_file);
######
# Get reference translations
($ref_id) = get_MT_data (\%ref_data, "RefSet", $ref_file);
compute_ngram_info ();
######
# Get translations to evaluate
($tst_id) = get_MT_data (\%tst_data, "TstSet", $tst_file);
######
# Check data for completeness and correctness
check_MT_data ();
######
#
my %NISTmt;
my %NISTOverall;
my %BLEUmt;
my %BLEUOverall;
######
# Evaluate
print "\nEvaluation of $src_lang-to-$tgt_lang translation using:\n";
my $cum_seg = 0;
foreach my $doc (sort keys %eval_docs)
{
$cum_seg += scalar( keys( %{$eval_docs{$doc}{SEGS}} ) );
}
print " src set \"$src_id\" (", scalar keys %eval_docs, " docs, $cum_seg segs)\n";
print " ref set \"$ref_id\" (", scalar keys %ref_data, " refs)\n";
print " tst set \"$tst_id\" (", scalar keys %tst_data, " systems)\n\n";
foreach my $sys (sort @tst_sys)
{
for (my $n=1; $n<=$max_Ngram; $n++)
{
$NISTmt{$n}{$sys}{cum} = 0;
$NISTmt{$n}{$sys}{ind} = 0;
$BLEUmt{$n}{$sys}{cum} = 0;
$BLEUmt{$n}{$sys}{ind} = 0;
}
if ( ($METHOD eq "BOTH") || ($METHOD eq "NIST") )
{
$method="NIST";
score_system ($sys, \%NISTmt, \%NISTOverall);
}
if ( ($METHOD eq "BOTH") || ($METHOD eq "BLEU") )
{
$method="BLEU";
score_system ($sys, \%BLEUmt, \%BLEUOverall);
}
}
######
printout_report ();
if ( $metricsMATR_output )
{
outputMetricsMATR( 'NIST', %NISTOverall ) if ( ( $METHOD eq 'BOTH' ) || ( $METHOD eq 'NIST' ) );
outputMetricsMATR( 'BLEU', %BLEUOverall ) if ( ( $METHOD eq 'BOTH' ) || ( $METHOD eq 'BLEU' ) );
}
($date, $time) = date_time_stamp();
print "\nMT evaluation scorer ended on $date at $time\n";
exit 0;
#################################
sub get_source_info
{
my ($file) = @_;
my ($name, $id, $src, $doc, $seg);
my ($data, $tag, $span);
# Extension of the file determines the parser used:
# .xml : XML::Twig
# otherwise : simple SGML parsing functions
if ( $file =~ /\.xml$/i )
{
my $twig = XML::Twig->new();
$twig->parsefile( $file );
my $root = $twig->root;
my $currentSet = $root->first_child( 'srcset' );
die "Source XML file '$file' does not contain the 'srcset' element" if ( not $currentSet );
$id = $currentSet->{ 'att' }->{ 'setid' } or die "No 'setid' attribute value in '$file'";
$src = $currentSet->{ 'att' }->{ 'srclang' } or die "No srcset 'srclang' attribute value in '$file'";
die "Not the same srclang attribute values across sets" unless ( not defined $src_lang or $src eq $src_lang );
$src_lang = $src;
foreach my $currentDoc ( $currentSet->get_xpath( './/doc' ) )
{
my $docID = $currentDoc->{ 'att' }->{ 'docid' } or die "No document 'docid' attribute value in '$file'";
foreach my $currentSeg ( $currentDoc->get_xpath( './/seg' ) )
{
my $segID = $currentSeg->{ 'att' }->{ 'id' };
die "No segment 'id' attribute value in '$file'" if (! defined $segID);
my $segData = $currentSeg->text;
($eval_docs{$docID}{SEGS}{$segID}) = &{ $TOKENIZATION }( $segData );
}
}
}
else
{
#read data from file
open (FILE, $file) or die "\nUnable to open translation data file '$file'", $usage;
binmode FILE, ":utf8";
$data .= $_ while <FILE>;
close (FILE);
#get source set info
die "\n\nFATAL INPUT ERROR: no 'src_set' tag in src_file '$file'\n\n"
unless ($tag, $span, $data) = extract_sgml_tag_and_span ("SrcSet", $data);
die "\n\nFATAL INPUT ERROR: no tag attribute '$name' in file '$file'\n\n"
unless ($id) = extract_sgml_tag_attribute ($name="SetID", $tag);
die "\n\nFATAL INPUT ERROR: no tag attribute '$name' in file '$file'\n\n"
unless ($src) = extract_sgml_tag_attribute ($name="SrcLang", $tag);
die "\n\nFATAL INPUT ERROR: $name ('$src') in file '$file' inconsistent\n"
." with $name in previous input data ('$src_lang')\n\n"
unless (not defined $src_lang or $src eq $src_lang);
$src_lang = $src;
#get doc info -- ID and # of segs
$data = $span;
while (($tag, $span, $data) = extract_sgml_tag_and_span ("Doc", $data))
{
die "\n\nFATAL INPUT ERROR: no tag attribute '$name' in file '$file'\n\n"
unless ($doc) = extract_sgml_tag_attribute ($name="DocID", $tag);
die "\n\nFATAL INPUT ERROR: duplicate '$name' in file '$file'\n\n"
if defined $eval_docs{$doc};
$span =~ s/[\s\n\r]+/ /g; # concatenate records
my $nseg=0, my $seg_data = $span;
while (($tag, $span, $seg_data) = extract_sgml_tag_and_span ("Seg", $seg_data))
{
die "\n\nFATAL INPUT ERROR: no attribute '$name' in file '$file'\n\n"
unless ($seg) = extract_sgml_tag_attribute( $name='id', $tag );
($eval_docs{$doc}{SEGS}{$seg}) = &{ $TOKENIZATION }( $span );
$nseg++;
}
die "\n\nFATAL INPUT ERROR: no segments in document '$doc' in file '$file'\n\n"
if $nseg == 0;
}
die "\n\nFATAL INPUT ERROR: no documents in file '$file'\n\n"
unless keys %eval_docs > 0;
}
return $id;
}
#################################
sub get_MT_data
{
my ($docs, $set_tag, $file) = @_;
my ($name, $id, $src, $tgt, $sys, $doc, $seg);
my ($tag, $span, $data);
# Extension of the file determines the parser used:
# .xml : XML::Twig
# otherwise : simple SGML parsing functions
if ( $file =~ /\.xml$/i )
{
my $twig = XML::Twig->new();
$twig->parsefile( $file );
my $root = $twig->root;
foreach my $currentSet ( $root->get_xpath( 'refset' ), $root->get_xpath( 'tstset' ) )
{
$id = $currentSet->{ 'att' }->{ 'setid' } or die "No 'setid' attribute value in '$file'";
$src = $currentSet->{ 'att' }->{ 'srclang' } or die "No 'srclang' attribute value in '$file'";
$tgt = $currentSet->{ 'att' }->{ 'trglang' } or die "No 'trglang' attribute value in '$file'";
die "Not the same 'srclang' attribute value across sets" unless ( $src eq $src_lang );
die "Not the same 'trglang' attribute value across sets" unless ( ( not defined $tgt_lang ) or ( $tgt = $tgt_lang ) );
$tgt_lang = $tgt;
my $sys;
if ( $currentSet->name eq 'tstset' )
{
$sys = $currentSet->{ 'att' }->{ 'sysid' } or die "No 'sysid' attribute value in '$file'";
}
else
{
$sys = $currentSet->{ 'att' }->{ 'refid' } or die "No 'refid' attribute value in '$file'";
}
foreach my $currentDoc ( $currentSet->get_xpath( './/doc' ) )
{
my $docID = $currentDoc->{ 'att' }->{ 'docid' } or die "No document 'docid' attribute value in '$file'";
$docs->{ $sys }{ $docID }{ FILE } = $file;
foreach my $currentSeg ( $currentDoc->get_xpath( './/seg' ) )
{
my $segID = $currentSeg->{ 'att' }->{ 'id' };
die "No segment 'id' attribute value in '$file'" if (! defined $segID);
my $segData = $currentSeg->text;
($docs->{$sys}{$docID}{SEGS}{$segID}) = &{ $TOKENIZATION }( $segData );
}
}
}
}
else
{
#read data from file
open (FILE, $file) or die "\nUnable to open translation data file '$file'", $usage;
binmode FILE, ":utf8";
$data .= $_ while <FILE>;
close (FILE);
#get tag info
while (($tag, $span, $data) = extract_sgml_tag_and_span ($set_tag, $data))
{
die "\n\nFATAL INPUT ERROR: no tag attribute '$name' in file '$file'\n\n"
unless ($id) = extract_sgml_tag_attribute ($name="SetID", $tag);
die "\n\nFATAL INPUT ERROR: no tag attribute '$name' in file '$file'\n\n"
unless ($src) = extract_sgml_tag_attribute ($name="SrcLang", $tag);
die "\n\nFATAL INPUT ERROR: $name ('$src') in file '$file' inconsistent\n"
." with $name of source ('$src_lang')\n\n"
unless $src eq $src_lang;
die "\n\nFATAL INPUT ERROR: no tag attribute '$name' in file '$file'\n\n"
unless ($tgt) = extract_sgml_tag_attribute ($name="TrgLang", $tag);
die "\n\nFATAL INPUT ERROR: $name ('$tgt') in file '$file' inconsistent\n"
." with $name of the evaluation ('$tgt_lang')\n\n"
unless (not defined $tgt_lang or $tgt eq $tgt_lang);
$tgt_lang = $tgt;
my $mtdata = $span;
while (($tag, $span, $mtdata) = extract_sgml_tag_and_span ("Doc", $mtdata))
{
die "\n\nFATAL INPUT ERROR: no tag attribute '$name' in file '$file'\n\n"
unless (my $sys) = extract_sgml_tag_attribute ($name="SysID", $tag);
die "\n\nFATAL INPUT ERROR: no tag attribute '$name' in file '$file'\n\n"
unless $doc = extract_sgml_tag_attribute ($name="DocID", $tag);
die "\n\nFATAL INPUT ERROR: document '$doc' for system '$sys' in file '$file'\n"
." previously loaded from file '$docs->{$sys}{$doc}{FILE}'\n\n"
unless (not defined $docs->{$sys}{$doc});
$span =~ s/[\s\n\r]+/ /g; # concatenate records
my $nseg=0, my $seg_data = $span;
while (($tag, $span, $seg_data) = extract_sgml_tag_and_span ("Seg", $seg_data))
{
die "\n\nFATAIL INPUT ERROR: no tag attribute '$name' in file '$file'\n\n"
unless $seg = extract_sgml_tag_attribute( $name="id", $tag );
($docs->{$sys}{$doc}{SEGS}{$seg}) = &{ $TOKENIZATION }( $span );
$nseg++;
}
die "\n\nFATAL INPUT ERROR: no segments in document '$doc' in file '$file'\n\n" if $nseg == 0;
$docs->{$sys}{$doc}{FILE} = $file;
}
}
}
return $id;
}
#################################
sub check_MT_data
{
@tst_sys = sort keys %tst_data;
@ref_sys = sort keys %ref_data;
die "Not the same 'setid' attribute values across files" unless ( ( $src_id eq $tst_id ) && ( $src_id eq $ref_id ) );
#every evaluation document must be represented for every system and every reference
foreach my $doc (sort keys %eval_docs)
{
my $nseg_source = scalar( keys( %{$eval_docs{$doc}{SEGS}} ) );
foreach my $sys (@tst_sys)
{
die "\n\nFATAL ERROR: no document '$doc' for system '$sys'\n\n" unless defined $tst_data{$sys}{$doc};
my $nseg = scalar( keys( %{$tst_data{$sys}{$doc}{SEGS}} ) );
die "\n\nFATAL ERROR: translated documents must contain the same # of segments as the source, but\n"
." document '$doc' for system '$sys' contains $nseg segments, while\n"
." the source document contains $nseg_source segments.\n\n"
unless $nseg == $nseg_source;
}
foreach my $sys (@ref_sys)
{
die "\n\nFATAL ERROR: no document '$doc' for reference '$sys'\n\n" unless defined $ref_data{$sys}{$doc};
my $nseg = scalar( keys( %{$ref_data{$sys}{$doc}{SEGS}} ) );
die "\n\nFATAL ERROR: translated documents must contain the same # of segments as the source, but\n"
." document '$doc' for system '$sys' contains $nseg segments, while\n"
." the source document contains $nseg_source segments.\n\n"
unless $nseg == $nseg_source;
}
}
}
#################################
sub compute_ngram_info
{
my ($ref, $doc, $seg);
my (@wrds, $tot_wrds, %ngrams, $ngram, $mgram);
my (%ngram_count, @tot_ngrams);
foreach $ref (keys %ref_data)
{
foreach $doc (keys %{$ref_data{$ref}})
{
foreach $seg ( keys %{$ref_data{$ref}{$doc}{SEGS}})
{
@wrds = split /\s+/, $ref_data{ $ref }{ $doc }{ SEGS }{ $seg };
$tot_wrds += @wrds;
%ngrams = %{Words2Ngrams (@wrds)};
foreach $ngram (keys %ngrams)
{
$ngram_count{$ngram} += $ngrams{$ngram};
}
}
}
}
foreach $ngram (keys %ngram_count)
{
@wrds = split / /, $ngram;
pop @wrds, $mgram = join " ", @wrds;
$ngram_info{$ngram} = - log ($mgram ? $ngram_count{$ngram}/$ngram_count{$mgram} : $ngram_count{$ngram}/$tot_wrds) / log 2;
if (defined $opt_x and $opt_x eq "ngram info")
{
@wrds = split / /, $ngram;
printf "ngram info:%9.4f%6d%6d%8d%3d %s\n", $ngram_info{$ngram}, $ngram_count{$ngram},
$mgram ? $ngram_count{$mgram} : $tot_wrds, $tot_wrds, scalar @wrds, $ngram;
}
}
}
#################################
sub score_system
{
my ($sys, $ref, $doc, $SCOREmt, $overallScore);
($sys, $SCOREmt, $overallScore) = @_;
my ($ref_length, $match_cnt, $tst_cnt, $ref_cnt, $tst_info, $ref_info);
my ($cum_ref_length, @cum_match, @cum_tst_cnt, @cum_ref_cnt, @cum_tst_info, @cum_ref_info);
$cum_ref_length = 0;
for (my $j=1; $j<=$max_Ngram; $j++)
{
$cum_match[$j] = $cum_tst_cnt[$j] = $cum_ref_cnt[$j] = $cum_tst_info[$j] = $cum_ref_info[$j] = 0;
}
foreach $doc (sort keys %eval_docs)
{
($ref_length, $match_cnt, $tst_cnt, $ref_cnt, $tst_info, $ref_info) = score_document ($sys, $doc, $overallScore);
if ( $method eq "NIST" )
{
my %DOCmt = ();
my $docScore = nist_score( scalar( @ref_sys ), $match_cnt, $tst_cnt, $ref_cnt, $tst_info, $ref_info, $sys, \%DOCmt );
$overallScore->{ $sys }{ 'documents' }{ $doc }{ 'score' } = $docScore;
if ( $detail >= 1 )
{
printf "$method score using 5-grams = %.4f for system \"$sys\" on document \"$doc\" (%d segments, %d words)\n",
$docScore, scalar keys %{$tst_data{$sys}{$doc}{SEGS}}, $tst_cnt->[1];
}
}
if ( $method eq "BLEU" )
{
my %DOCmt = ();
my $docScore = &{$BLEU_SCORE}( $ref_length, $match_cnt, $tst_cnt, $sys, \%DOCmt );
$overallScore->{ $sys }{ 'documents' }{ $doc }{ 'score' } = $docScore;
if ( $detail >= 1 )
{
printf "$method score using 4-grams = %.4f for system \"$sys\" on document \"$doc\" (%d segments, %d words)\n",
$docScore, scalar keys %{$tst_data{$sys}{$doc}{SEGS}}, $tst_cnt->[1];
}
}
$cum_ref_length += $ref_length;
for (my $j=1; $j<=$max_Ngram; $j++)
{
$cum_match[$j] += $match_cnt->[$j];
$cum_tst_cnt[$j] += $tst_cnt->[$j];
$cum_ref_cnt[$j] += $ref_cnt->[$j];
$cum_tst_info[$j] += $tst_info->[$j];
$cum_ref_info[$j] += $ref_info->[$j];
printf "document info: $sys $doc %d-gram %d %d %d %9.4f %9.4f\n", $j, $match_cnt->[$j],
$tst_cnt->[$j], $ref_cnt->[$j], $tst_info->[$j], $ref_info->[$j]
if (defined $opt_x and $opt_x eq "document info");
}
}
if ($method eq "BLEU")
{
$overallScore->{ $sys }{ 'score' } = &{$BLEU_SCORE}($cum_ref_length, \@cum_match, \@cum_tst_cnt, $sys, $SCOREmt);
}
if ($method eq "NIST")
{
$overallScore->{ $sys }{ 'score' } = nist_score (scalar @ref_sys, \@cum_match, \@cum_tst_cnt, \@cum_ref_cnt, \@cum_tst_info, \@cum_ref_info, $sys, $SCOREmt);
}
}
#################################
sub score_document
{
my ($sys, $ref, $doc, $overallScore);
($sys, $doc, $overallScore) = @_;
my ($ref_length, $match_cnt, $tst_cnt, $ref_cnt, $tst_info, $ref_info);
my ($cum_ref_length, @cum_match, @cum_tst_cnt, @cum_ref_cnt, @cum_tst_info, @cum_ref_info);
$cum_ref_length = 0;
for (my $j=1; $j<=$max_Ngram; $j++)
{
$cum_match[$j] = $cum_tst_cnt[$j] = $cum_ref_cnt[$j] = $cum_tst_info[$j] = $cum_ref_info[$j] = 0;
}
# score each segment
foreach my $seg ( nsort keys( %{$tst_data{$sys}{$doc}{SEGS}} ) )
{
my @ref_segments = ();
foreach $ref (@ref_sys)
{
push @ref_segments, $ref_data{$ref}{$doc}{SEGS}{$seg};
if ( $detail >= 3 )
{
printf "ref '$ref', seg $seg: %s\n", $ref_data{$ref}{$doc}{SEGS}{$seg}
}
}
printf "sys '$sys', seg $seg: %s\n", $tst_data{$sys}{$doc}{SEGS}{$seg} if ( $detail >= 3 );
($ref_length, $match_cnt, $tst_cnt, $ref_cnt, $tst_info, $ref_info) = score_segment ($tst_data{$sys}{$doc}{SEGS}{$seg}, @ref_segments);
if ( $method eq "BLEU" )
{
my %DOCmt = ();
my $segScore = &{$BLEU_SCORE}($ref_length, $match_cnt, $tst_cnt, $sys, %DOCmt);
$overallScore->{ $sys }{ 'documents' }{ $doc }{ 'segments' }{ $seg }{ 'score' } = $segScore;
if ( $detail >= 2 )
{
printf " $method score using 4-grams = %.4f for system \"$sys\" on segment $seg of document \"$doc\" (%d words)\n", $segScore, $tst_cnt->[1]
}
}
if ( $method eq "NIST" )
{
my %DOCmt = ();
my $segScore = nist_score (scalar @ref_sys, $match_cnt, $tst_cnt, $ref_cnt, $tst_info, $ref_info, $sys, %DOCmt);
$overallScore->{ $sys }{ 'documents' }{ $doc }{ 'segments' }{ $seg }{ 'score' } = $segScore;
if ( $detail >= 2 )
{
printf " $method score using 5-grams = %.4f for system \"$sys\" on segment $seg of document \"$doc\" (%d words)\n", $segScore, $tst_cnt->[1];
}
}
$cum_ref_length += $ref_length;
for (my $j=1; $j<=$max_Ngram; $j++)
{
$cum_match[$j] += $match_cnt->[$j];
$cum_tst_cnt[$j] += $tst_cnt->[$j];
$cum_ref_cnt[$j] += $ref_cnt->[$j];
$cum_tst_info[$j] += $tst_info->[$j];
$cum_ref_info[$j] += $ref_info->[$j];
}
}
return ($cum_ref_length, [@cum_match], [@cum_tst_cnt], [@cum_ref_cnt], [@cum_tst_info], [@cum_ref_info]);
}
###############################################################################################################################
# function returning the shortest reference length
# takes as input:
# - currentLength : the current (shortest) reference length
# - referenceSentenceLength : the current reference sentence length
# - candidateSentenceLength : the current candidate sentence length (unused)
###############################################################################################################################
sub brevity_penalty_shortest
{
my ( $currentLength, $referenceSentenceLength, $candidateSentenceLength ) = @_;
return ( $referenceSentenceLength < $currentLength ? $referenceSentenceLength : $currentLength );
}
###############################################################################################################################
# function returning the closest reference length (to the candidate sentence length)
# takes as input:
# - currentLength: the current (closest) reference length.
# - candidateSentenceLength : the current reference sentence length
# - candidateSentenceLength : the current candidate sentence length
# when two reference sentences are at the same distance, it will return the shortest reference sentence length
# example of 4 iterations, given:
# - one candidate sentence containing 7 tokens
# - one reference translation containing 11 tokens
# - one reference translation containing 8 tokens
# - one reference translation containing 6 tokens
# - one reference translation containing 7 tokens
# the multiple invokations will return:
# - currentLength is set to 11 (outside of this function)
# - brevity_penalty_closest( 11, 8, 7 ) returns 8, since abs( 8 - 7 ) < abs( 11 - 7 )
# - brevity_penalty_closest( 8, 6, 7 ) returns 6, since abs( 8 - 7 ) == abs( 6 - 7 ) AND 6 < 8
# - brevity_penalty_closest( 7, 6, 7 ) returns 7, since abs( 7 - 7 ) < abs( 6 - 7 )
###############################################################################################################################
sub brevity_penalty_closest
{
my ( $currentLength, $referenceSentenceLength, $candidateSentenceLength ) = @_;
my $result = $currentLength;
if ( abs( $candidateSentenceLength - $referenceSentenceLength ) <= abs( $candidateSentenceLength - $currentLength ) )
{
if ( abs( $candidateSentenceLength - $referenceSentenceLength ) == abs( $candidateSentenceLength - $currentLength ) )
{
if ( $currentLength > $referenceSentenceLength )
{
$result = $referenceSentenceLength;
}
}
else
{
$result = $referenceSentenceLength;
}
}
return $result;
}
#################################
sub score_segment
{
my ($tst_seg, @ref_segs) = @_;
my (@tst_wrds, %tst_ngrams, @match_count, @tst_count, @tst_info);
my (@ref_wrds, $ref_seg, %ref_ngrams, %ref_ngrams_max, @ref_count, @ref_info);
my ($ngram);
my (@nwrds_ref);
my $ref_length;
for (my $j=1; $j<= $max_Ngram; $j++)
{
$match_count[$j] = $tst_count[$j] = $ref_count[$j] = $tst_info[$j] = $ref_info[$j] = 0;
}
# get the ngram counts for the test segment
@tst_wrds = split /\s+/, $tst_seg;
%tst_ngrams = %{Words2Ngrams (@tst_wrds)};
for (my $j=1; $j<=$max_Ngram; $j++)
{
# compute ngram counts
$tst_count[$j] = $j<=@tst_wrds ? (@tst_wrds - $j + 1) : 0;
}
# get the ngram counts for the reference segments
foreach $ref_seg (@ref_segs)
{
@ref_wrds = split /\s+/, $ref_seg;
%ref_ngrams = %{Words2Ngrams (@ref_wrds)};
foreach $ngram (keys %ref_ngrams)
{
# find the maximum # of occurrences
my @wrds = split / /, $ngram;
$ref_info[@wrds] += $ngram_info{$ngram};
$ref_ngrams_max{$ngram} = defined $ref_ngrams_max{$ngram} ? max ($ref_ngrams_max{$ngram}, $ref_ngrams{$ngram}) : $ref_ngrams{$ngram};
}
for (my $j=1; $j<=$max_Ngram; $j++)
{
# update ngram counts
$ref_count[$j] += $j<=@ref_wrds ? (@ref_wrds - $j + 1) : 0;
}
if ( not defined( $ref_length ) )
{
$ref_length = scalar( @ref_wrds );
}
else
{
$ref_length = &{$BLEU_BP}( $ref_length, scalar( @ref_wrds ), scalar( @tst_wrds ) );
}
}
# accumulate scoring stats for tst_seg ngrams that match ref_seg ngrams
foreach $ngram (keys %tst_ngrams)
{
next unless defined $ref_ngrams_max{$ngram};
my @wrds = split / /, $ngram;
$tst_info[@wrds] += $ngram_info{$ngram} * min($tst_ngrams{$ngram},$ref_ngrams_max{$ngram});
$match_count[@wrds] += my $count = min($tst_ngrams{$ngram},$ref_ngrams_max{$ngram});
printf "%.2f info for each of $count %d-grams = '%s'\n", $ngram_info{$ngram}, scalar @wrds, $ngram
if $detail >= 3;
}
return ($ref_length, [@match_count], [@tst_count], [@ref_count], [@tst_info], [@ref_info]);
}
#################################
sub bleu_score_nosmoothing
{
my ($ref_length, $matching_ngrams, $tst_ngrams, $sys, $SCOREmt) = @_;
my $score = 0;
my $iscore = 0;
for ( my $j = 1; $j <= $max_Ngram; ++$j )
{
if ($matching_ngrams->[ $j ] == 0)
{
$SCOREmt->{ $j }{ $sys }{ cum }=0;
}
else
{
my $len_score = min (0, 1-$ref_length/$tst_ngrams->[1]);
# Cumulative N-Gram score
$score += log( $matching_ngrams->[ $j ] / $tst_ngrams->[ $j ] );
$SCOREmt->{ $j }{ $sys }{ cum } = exp( $score / $j + $len_score );
# Individual N-Gram score
$iscore = log( $matching_ngrams->[ $j ] / $tst_ngrams->[ $j ] );
$SCOREmt->{ $j }{ $sys }{ ind } = exp( $iscore );
}
}
return $SCOREmt->{ 4 }{ $sys }{ cum };
}
###############################################################################################################################
# Default method used to compute the BLEU score, using smoothing.
# Note that the method used can be overridden using the '--no-smoothing' command-line argument
# The smoothing is computed by taking 1 / ( 2^k ), instead of 0, for each precision score whose matching n-gram count is null
# k is 1 for the first 'n' value for which the n-gram match count is null
# For example, if the text contains:
# - one 2-gram match
# - and (consequently) two 1-gram matches
# the n-gram count for each individual precision score would be:
# - n=1 => prec_count = 2 (two unigrams)
# - n=2 => prec_count = 1 (one bigram)
# - n=3 => prec_count = 1/2 (no trigram, taking 'smoothed' value of 1 / ( 2^k ), with k=1)
# - n=4 => prec_count = 1/4 (no fourgram, taking 'smoothed' value of 1 / ( 2^k ), with k=2)
###############################################################################################################################
sub bleu_score
{
my ($ref_length, $matching_ngrams, $tst_ngrams, $sys, $SCOREmt) = @_;
my $score = 0;
my $iscore = 0;
my $exp_len_score = 0;
$exp_len_score = exp( min (0, 1 - $ref_length / $tst_ngrams->[ 1 ] ) ) if ( $tst_ngrams->[ 1 ] > 0 );
my $smooth = 1;
for ( my $j = 1; $j <= $max_Ngram; ++$j )
{
if ( $tst_ngrams->[ $j ] == 0 )
{
$iscore = 0;
}
elsif ( $matching_ngrams->[ $j ] == 0 )
{
$smooth *= 2;
$iscore = log( 1 / ( $smooth * $tst_ngrams->[ $j ] ) );
}
else
{
$iscore = log( $matching_ngrams->[ $j ] / $tst_ngrams->[ $j ] );
}
$SCOREmt->{ $j }{ $sys }{ ind } = exp( $iscore );
$score += $iscore;
$SCOREmt->{ $j }{ $sys }{ cum } = exp( $score / $j ) * $exp_len_score;
}
return $SCOREmt->{ 4 }{ $sys }{ cum };
}
#################################
sub nist_score
{
my ($nsys, $matching_ngrams, $tst_ngrams, $ref_ngrams, $tst_info, $ref_info, $sys, $SCOREmt) = @_;
my $score = 0;
my $iscore = 0;
for (my $n=1; $n<=$max_Ngram; $n++)
{
$score += $tst_info->[$n]/max($tst_ngrams->[$n],1);
$SCOREmt->{$n}{$sys}{cum} = $score * nist_length_penalty($tst_ngrams->[1]/($ref_ngrams->[1]/$nsys));
$iscore = $tst_info->[$n]/max($tst_ngrams->[$n],1);
$SCOREmt->{$n}{$sys}{ind} = $iscore * nist_length_penalty($tst_ngrams->[1]/($ref_ngrams->[1]/$nsys));
}
return $SCOREmt->{5}{$sys}{cum};
}
#################################
sub Words2Ngrams
{
#convert a string of words to an Ngram count hash
my %count = ();
for (; @_; shift)
{
my ($j, $ngram, $word);
for ($j=0; $j<$max_Ngram and defined($word=$_[$j]); $j++)
{
$ngram .= defined $ngram ? " $word" : $word;
$count{$ngram}++;
}
}
return {%count};
}
#################################
sub tokenization
{
my ($norm_text) = @_;
# language-independent part:
$norm_text =~ s/<skipped>//g; # strip "skipped" tags
$norm_text =~ s/-\n//g; # strip end-of-line hyphenation and join lines
$norm_text =~ s/\n/ /g; # join lines
$norm_text =~ s/"/"/g; # convert SGML tag for quote to "
$norm_text =~ s/&/&/g; # convert SGML tag for ampersand to &
$norm_text =~ s/</</g; # convert SGML tag for less-than to >
$norm_text =~ s/>/>/g; # convert SGML tag for greater-than to <
# language-dependent part (assuming Western languages):
$norm_text = " $norm_text ";
$norm_text =~ tr/[A-Z]/[a-z]/ unless $preserve_case;
$norm_text =~ s/([\{-\~\[-\` -\&\(-\+\:-\@\/])/ $1 /g; # tokenize punctuation
$norm_text =~ s/([^0-9])([\.,])/$1 $2 /g; # tokenize period and comma unless preceded by a digit
$norm_text =~ s/([\.,])([^0-9])/ $1 $2/g; # tokenize period and comma unless followed by a digit
$norm_text =~ s/([0-9])(-)/$1 $2 /g; # tokenize dash when preceded by a digit
$norm_text =~ s/\s+/ /g; # one space only between words
$norm_text =~ s/^\s+//; # no leading space
$norm_text =~ s/\s+$//; # no trailing space
return $norm_text;
}
sub tokenization_international
{
my ($norm_text) = @_;
$norm_text =~ s/<skipped>//g; # strip "skipped" tags
#$norm_text =~ s/\p{Hyphen}\p{Zl}//g; # strip end-of-line hyphenation and join lines
$norm_text =~ s/\p{Zl}/ /g; # join lines
# replace entities
$norm_text =~ s/"/\"/g; # quote to "
$norm_text =~ s/&/&/g; # ampersand to &
$norm_text =~ s/</</g; # less-than to <
$norm_text =~ s/>/>/g; # greater-than to >
$norm_text =~ s/'/\'/g; # apostrophe to '
$norm_text = lc( $norm_text ) unless $preserve_case; # lowercasing if needed
$norm_text =~ s/([^[:ascii:]])/ $1 /g if ( $split_non_ASCII );
# punctuation: tokenize any punctuation unless followed AND preceded by a digit
$norm_text =~ s/(\P{N})(\p{P})/$1 $2 /g;
$norm_text =~ s/(\p{P})(\P{N})/ $1 $2/g;
$norm_text =~ s/(\p{S})/ $1 /g; # tokenize symbols
$norm_text =~ s/\p{Z}+/ /g; # one space only between words
$norm_text =~ s/^\p{Z}+//; # no leading space
$norm_text =~ s/\p{Z}+$//; # no trailing space
return $norm_text;
}
#################################
sub nist_length_penalty
{
my ($ratio) = @_;
return 1 if $ratio >= 1;
return 0 if $ratio <= 0;
my $ratio_x = 1.5;
my $score_x = 0.5;
my $beta = -log($score_x)/log($ratio_x)/log($ratio_x);
return exp (-$beta*log($ratio)*log($ratio));
}
#################################
sub date_time_stamp