-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmulti-bleu.perl
executable file
·221 lines (203 loc) · 7.35 KB
/
multi-bleu.perl
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
#!/usr/bin/env perl
#
# This file is part of moses. Its use is licensed under the GNU Lesser General
# Public License version 2.1 or, at your option, any later version.
# $Id$
use warnings;
use strict;
my $lowercase = 0;
if ($ARGV[0] eq "-lc") {
$lowercase = 1;
shift;
}
my $stem = $ARGV[0];
if (!defined $stem) {
print STDERR "usage: multi-bleu.pl [-lc] reference < hypothesis\n";
print STDERR "Reads the references from reference or reference0, reference1, ...\n";
exit(1);
}
$stem .= ".ref" if !-e $stem && !-e $stem."0" && -e $stem.".ref0";
my @REF;
my $ref = 0;
while(-e "$stem$ref") {
&add_to_ref("$stem$ref", \@REF);
$ref++;
}
&add_to_ref($stem, \@REF) if -e $stem;
die("ERROR: could not find reference file $stem") unless scalar @REF;
# add additional references explicitly specified on the command line
shift;
foreach my $stem (@ARGV) {
&add_to_ref($stem, \@REF) if -e $stem;
}
sub add_to_ref {
my ($file, $REF) = @_;
my $s = 0;
if ($file =~ /.gz$/) {
open(REF, "gzip -dc $file|") or die "Can't read $file";
} else {
open(REF, $file) or die "Can't read $file";
}
while(<REF>) {
chop;
push @{$$REF[$s++]}, $_;
}
close(REF);
}
my (@CORRECT, @TOTAL, $length_translation, $length_reference);
my $s = 0;
my ($hypo_sen, $ref_sen);
while(<STDIN>) {
chop;
$_ = lc if $lowercase;
my @WORD = split;
my %REF_NGRAM = ();
my $length_translation_this_sentence = scalar(@WORD);
my ($closest_diff, $closest_length) = (9999, 9999);
$hypo_sen = $_;
foreach my $reference (@{$REF[$s]}) {
# deal with multiple referece, but we usually use only one reference
$ref_sen = $reference;
# print "$s $_ <=> $reference\n";
$reference = lc($reference) if $lowercase;
my @WORD = split(' ', $reference);
my $length = scalar(@WORD);
my $diff = abs($length_translation_this_sentence - $length);
if ($diff < $closest_diff) {
$closest_diff = $diff;
$closest_length = $length;
# print STDERR "$s: closest diff ".abs($length_translation_this_sentence - $length)." = abs($length_translation_this_sentence-$length), setting len: $closest_length\n";
} elsif ($diff == $closest_diff) {
$closest_length = $length if $length < $closest_length;
# from two references with the same closeness to me
# take the *shorter* into account, not the "first" one.
}
for (my $n = 1; $n <= 4; $n++) {
my %REF_NGRAM_N = ();
for (my $start = 0; $start <= $#WORD - ($n - 1); $start++) {
my $ngram = "$n";
for (my $w = 0; $w < $n; $w++) {
$ngram .= " ".$WORD[$start + $w];
}
$REF_NGRAM_N{$ngram}++;
}
foreach my $ngram (keys %REF_NGRAM_N) {
if (!defined($REF_NGRAM{$ngram}) ||
$REF_NGRAM{$ngram} < $REF_NGRAM_N{$ngram}) {
$REF_NGRAM{$ngram} = $REF_NGRAM_N{$ngram};
# print "$i: REF_NGRAM{$ngram} = $REF_NGRAM{$ngram}<BR>\n";
}
}
}
}
$length_translation += $length_translation_this_sentence;
$length_reference += $closest_length;
my (@SENCORRECT, @SENTOTAL);
for (my $n = 1; $n <= 4; $n++) {
my %T_NGRAM = ();
for (my $start = 0; $start <= $#WORD - ($n - 1); $start++) {
my $ngram = "$n";
for (my $w = 0; $w < $n; $w++) {
$ngram .= " ".$WORD[$start + $w];
}
$T_NGRAM{$ngram}++;
}
foreach my $ngram (keys %T_NGRAM) {
$ngram =~ /^(\d+) /;
my $n = $1;
# my $corr = 0;
# print "$i e $ngram $T_NGRAM{$ngram}<BR>\n";
$TOTAL[$n] += $T_NGRAM{$ngram};
$SENTOTAL[$n] += $T_NGRAM{$ngram};
if (defined($REF_NGRAM{$ngram})) {
if ($REF_NGRAM{$ngram} >= $T_NGRAM{$ngram}) {
$CORRECT[$n] += $T_NGRAM{$ngram};
$SENCORRECT[$n] += $T_NGRAM{$ngram};
# $corr = $T_NGRAM{$ngram};
# print "$i e correct1 $T_NGRAM{$ngram}<BR>\n";
}
else {
$CORRECT[$n] += $REF_NGRAM{$ngram};
$SENCORRECT[$n] += $REF_NGRAM{$ngram};
# $corr = $REF_NGRAM{$ngram};
# print "$i e correct2 $REF_NGRAM{$ngram}<BR>\n";
}
}
# $REF_NGRAM{$ngram} = 0 if !defined $REF_NGRAM{$ngram};
# print STDERR "$ngram: {$s, $REF_NGRAM{$ngram}, $T_NGRAM{$ngram}, $corr}\n"
}
}
$s++;
# calculate sentence bleu score
my $brevity_penalty = 1;
my $bleu = 0;
my @bleu = ();
for (my $n = 1; $n <= 4; $n++) {
if (defined ($SENTOTAL[$n]) && defined ($SENCORRECT[$n])) {
# print STDERR "SENCORRECT[$n]:$SENCORRECT[$n] SENTOTAL[$n]:$SENTOTAL[$n]\n";
$bleu[$n] = ($SENTOTAL[$n]) ? $SENCORRECT[$n] / $SENTOTAL[$n] : 0;
} else {
$bleu[$n] = 0;
}
}
if ($closest_length == 0) {
printf "BLEU = 0, 0/0/0/0 (BP=0, ratio=0, hyp_len=0, ref_len=0)\n";
exit(1);
}
if ($length_translation_this_sentence < $closest_length) {
$brevity_penalty = exp(1 - $closest_length / $length_translation_this_sentence);
}
my @log_bleu = ();
$bleu = $brevity_penalty * exp((my_log($bleu[1]) + my_log($bleu[2]) + my_log($bleu[3]) + my_log($bleu[4])) / 4);
printf "%.2f\t", 100 * $bleu;
print "line_num:$s\t$hypo_sen <==> $ref_sen\t";
printf "BLEU = %.2f, %.1f/%.1f/%.1f/%.1f (BP=%.3f, ratio=%.3f, hyp_len=%d, ref_len=%d)\n",
100 * $bleu,
100 * $bleu[1],
100 * $bleu[2],
100 * $bleu[3],
100 * $bleu[4],
$brevity_penalty,
$length_translation_this_sentence / $closest_length,
$length_translation_this_sentence,
$closest_length;
}
my $brevity_penalty = 1;
my $bleu = 0;
my @bleu = ();
for (my $n = 1; $n <= 4; $n++) {
if (defined ($TOTAL[$n])) {
$bleu[$n] = ($TOTAL[$n]) ? $CORRECT[$n] / $TOTAL[$n] : 0;
# print STDERR "CORRECT[$n]:$CORRECT[$n] TOTAL[$n]:$TOTAL[$n]\n";
} else {
$bleu[$n] = 0;
}
}
if ($length_reference == 0) {
printf "BLEU = 0, 0/0/0/0 (BP=0, ratio=0, hyp_len=0, ref_len=0)\n";
exit(1);
}
if ($length_translation < $length_reference) {
$brevity_penalty = exp(1 - $length_reference / $length_translation);
}
$bleu = $brevity_penalty * exp((my_log( $bleu[1] ) +
my_log( $bleu[2] ) +
my_log( $bleu[3] ) +
my_log( $bleu[4] ) ) / 4);
#printf "===================================================================================\n";
#printf "= Total BLEU =\n";
#printf "===================================================================================\n";
printf "-1============= Document BLEU = %.2f, %.1f/%.1f/%.1f/%.1f (BP=%.3f, ratio=%.3f, hyp_len=%d, ref_len=%d)=============\n",
100 * $bleu,
100 * $bleu[1],
100 * $bleu[2],
100 * $bleu[3],
100 * $bleu[4],
$brevity_penalty,
$length_translation / $length_reference,
$length_translation,
$length_reference;
sub my_log {
return -9999999999 unless $_[0];
return log($_[0]);
}