-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnLEPOR-MT-evaluation-perl
386 lines (352 loc) · 18.9 KB
/
nLEPOR-MT-evaluation-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
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
#!/usr/bin/perl -w
########################################################################################################################################################################################
## For a detailed description of this evaluation metric and source code, please read: #####
## This code is to implement the Machine Translation Evaluation metric nLEPOR #####
## nLEPOR evaluation metric is proposed by Aaron Li-Feng Han, Derek F. Wong, Lidia S. Chao, Liangye He and Yi Lu in University of Macau #####
## This perl code is written by Aaron Li-Feng Han in university of macau, 2013.04 #####
## All Copyright (c) preserved by the authors. Corresponding author: Aaron Li-Feng Han < hanlifengaaron@gmail.com > #####
## Please cite paper below if you use the metric or source code in your research work #####
## "Unsupervised Quality Estimation Model for English to German Translation and Its Application in Extensive Supervised Evaluation". Aaron Li-Feng Han, Derek F. Wong, #####
## Lidia S. Chao, Liangye He and Yi Lu. The Scientific World Journal, Issue: Recent Advances in Information Technology. Hindawi Publishing Corporation. #####
## Source code website: https://github.com/aaronlifenghan/aaron-project-lepor #####
## Online paper: http://www.hindawi.com/journals/tswj/ #####
########################################################################################################################################################################################
## How to use this Perl code and how to assign the parameter weights of Precision and Recall: #####
## 1. Your system output translation documents and the reference translation document should contain the plain text only, each line containing one sentence, no empty line. #####
## 2. Put you system output translation documents under the address in Line 24, 52, 54 of this Perl code. #####
## 3. Put you reference translation document under the address in Line 28 of this Perl code. #####
## 4. Tune the Precision and Recall weights of LEPOR by the parameter value of a under the address in Line 163 of this Perl code. #####
## 5. The document containing evaluation score of LEPOR will be shown under the address in Line 55 of this Perl code. #####
## #####
########################################################################################################################################################################################
opendir (DIR, "D:\\call Papers\\paper\\AMTA201206.4\\program\\new adjustment corpora\\cz-en2.7\\output_cz-en\\segmented_cz-en") || die "can not open f:\\dealfiles!"; ## this file address puts your system output translation documents
@filename=readdir(DIR);
closedir (DIR);
open REF,"<:encoding(utf8)","D:\\call Papers\\paper\\AMTA201206.4\\program\\new adjustment corpora\\cz-en2.7\\segmented2.7_newstest2011-ref.en.txt" or die "can't open reference file\n"; ## this file address puts reference translation document##
$j=0;
$str1="";
@arry_r1=();
@arry_ref_length= ();
@arrytwo_ref_translation=();
$num_of_ref_sentence=0;
while($str1=<REF>) #### put the reference translation into a two dimension array @arrytwo_ref_translation
{
chomp($str1);
$str1= lc ($str1); ### when doing the matching, lower and upper case is concidered the same
@arry_r1= split(/\s+/,$str1);
$arry_ref_length[$j]=scalar(@arry_r1); #### @arry_ref_length store the lengths of every sentence(line) of the reference translation.
$j++;
push @arrytwo_ref_translation, [@arry_r1];
@arry_r1= ();
}
$num_of_ref_sentence=$j;
close REF;
foreach $file (@filename) ## go through all the system output translation documents
{
if(!(-d "D:\\call Papers\\paper\\AMTA201206.4\\program\\new adjustment corpora\\cz-en2.7\\output_cz-en\\segmented_cz-en\\$file")) ## this file address puts your system output translation documents
{
open (TEST,"<:encoding(utf8)","D:\\call Papers\\paper\\AMTA201206.4\\program\\new adjustment corpora\\cz-en2.7\\output_cz-en\\segmented_cz-en\\$file") || die "can not open system output file: $!";
open (RESULT,">D:\\call Papers\\paper\\AMTA201206.4\\program\\new adjustment corpora\\cz-en2.7\\output_cz-en\\segmented_cz-en\\LEPOR1P9R_$file.txt") || die "$!"; ## put the evaluation score in this file address
$i=0;
$str0="";
@arry_1= ();
@arry_sys_length= ();
@arrytwo_sys_translation= ();
$sentence_num=0;
while($str0=<TEST>) #### put the system output translation into a two dimension array @arrytwo_sys_translation
{
chomp($str0);
$str0= lc ($str0); ### system output translation is turned into lowwer case, too
@arry_1= split(/\s+/,$str0);
$arry_sys_length[$i]=scalar(@arry_1); #### @arry_sys_length store the lengths of every sentence(line) of the system translation.
$i++;
push @arrytwo_sys_translation, [@arry_1];
@arry_1= ();
}
$sentence_num=$i;
close TEST;
print RESULT 'length of sysoutput:',"\n","@arry_sys_length","\n",$sentence_num,"\n";
print RESULT 'length of reference:',"\n","@arry_ref_length","\n",$num_of_ref_sentence,"\n";
@LP= ();
for($k=0;$k<$sentence_num;$k++) ##### @LP store the longth penalty coefficient of every LP[i]
{
if($arry_sys_length[$k]>$arry_ref_length[$k])
{
$LP[$k]=exp(1-($arry_sys_length[$k]/$arry_ref_length[$k]));
}
else
{
if($arry_sys_length[$k]==0)
{
$LP[$k]=0;
}
elsif($arry_sys_length[$k]>0)
{
$LP[$k]=exp(1-($arry_ref_length[$k]/$arry_sys_length[$k]));
}
#$LP[$k]=exp(1-($arry_ref_length[$k]/$arry_sys_length[$k]));
}
}
print RESULT 'length penalty with longer or shorter:',"\n","@LP","\n",$k,"\n";
$Mean_LP= 0;
for($k=0;$k<$sentence_num;$k++)
{
$Mean_LP= $Mean_LP+$LP[$k];
}
$Mean_LP= $Mean_LP/$sentence_num;
##$Mean_LP= $Mean_LP/2051;
print RESULT 'mean of length penalty with longer or shorter:',"\n","$Mean_LP","\n",$k,"\n";
@common_num=();
for($i=0;$i<$sentence_num;$i++) ##### store the common number between sys and ref into @common_num
{
$m=0;@record_position=(); ####everytime,select one sentence from the sys, clear the record array
for($j=0;$j<$arry_sys_length[$i];$j++)
{
for($k=0;$k<$arry_ref_length[$i];$k++)
{
if($arrytwo_sys_translation[$i][$j] eq $arrytwo_ref_translation[$i][$k])
{
#if(!( any(@record_position) eq $k )) ####every word in the reference use not more than once to matched
if(!( grep(/^$k/,@record_position) )) ####every word in the reference use not more than once to matched
{
$common_num[$i]++;
$record_position[$m]=$k; $m++; ####record the position in the reference already matched
last; #### every word of the sys only match the reference once
}
}
}
}
}
print RESULT 'common number between sys and ref:',"\n","@common_num","\n",$i,"\n";
@P= ();
@R= ();
for($i=0;$i<$sentence_num;$i++) #####calculate the precision and recall into @P and @R
{
if(($common_num[$i])!= 0)
{
$P[$i]=$common_num[$i]/$arry_sys_length[$i];
$R[$i]=$common_num[$i]/$arry_ref_length[$i];
}
else
{
$P[$i]=0;
$R[$i]=0;
}
}
print RESULT 'precision of sys:',"\n","@P","\n",$i,"\n";
print RESULT 'recall of sys:',"\n","@R","\n",$i,"\n";
$Mean_precision=0;
$Mean_recall=0;
for($i=0;$i<$sentence_num;$i++)
{
$Mean_precision= $Mean_precision+$P[$i];
$Mean_recall= $Mean_recall+$R[$i];
}
$Mean_precision= $Mean_precision/$sentence_num;
$Mean_recall= $Mean_recall/$sentence_num;
print RESULT 'mean precision of sys:',"\n","$Mean_precision","\n",$i,"\n";
print RESULT 'mean recall of sys:',"\n","$Mean_recall","\n",$i,"\n";
$a=9; ##### $a is a varerble to be changed according to different language envirenment #### H(9R,P)
#$a=1/9; ##### $a is a varerble to be changed according to different language envirenment#### H(9P,R)
#$a=1; ##### $a is a varerble to be changed according to different language envirenment#### H(P,R)
@Harmonic_mean_PR=();
for($i=0;$i<$sentence_num;$i++) ####calculate the harmonic mean of P and a*R
{
if($P[$i]!=0 || $R[$i]!=0)
{
$Harmonic_mean_PR[$i]=((1+$a)*$P[$i]*$R[$i])/($R[$i]+$a*$P[$i]);
}
else
{
$Harmonic_mean_PR[$i]=0;
}
}
print RESULT 'harmonic of precision and recall:',"\n","@Harmonic_mean_PR","\n",$i,"\n";
$Mean_HarmonicMean=0;
for($i=0;$i<$sentence_num;$i++)
{
$Mean_HarmonicMean= $Mean_HarmonicMean+ $Harmonic_mean_PR[$i];
}
$Mean_HarmonicMean= $Mean_HarmonicMean/$sentence_num;
print RESULT 'mean of every sentences harmonic-mean of precision and recall:',"\n","$Mean_HarmonicMean","\n",$i,"\n";
@pos_dif=();
@pos_dif_record= ();
@pos_dif_record_flag= ();
@pos_dif_record_ref_flag= ();
for($i=0;$i<$sentence_num;$i++) ##### store the position-different value between sys and ref into @pos_dif
{
for($j=0;$j<$arry_sys_length[$i];$j++)
{
$pos_dif_record_flag[$i][$j]= "none_match"; ##firstly make every system translation word's flag equal to none
#$store_ref_pos=-1000;
for($k=0;$k<$arry_ref_length[$i];$k++)
{
$pos_dif_record_ref_flag[$i][$k]= "un_confirmed";
if($arrytwo_sys_translation[$i][$j] eq $arrytwo_ref_translation[$i][$k])
{
$pos_dif_record_flag[$i][$j]= "exist_match"; ##if there is match,then change the flag as exist_match
$flag_confirm=0;
if ($j eq 0) ###this word is in the begining of sys-output sentence,then check its next word match-condition
{
$condition=0;
for($count_num_sys=1;$count_num_sys<=2;$count_num_sys++) ##check the following two words' match
{
for($count_num_ref=1;$count_num_ref<=2;$count_num_ref++)##to match the reference following two words
{
if($arrytwo_sys_translation[$i][$j+$count_num_sys] eq $arrytwo_ref_translation[$i][$k+$count_num_ref])
{
$pos_dif_record_flag[$i][$j]= "confirm_match"; ##if the context is also matched then confirm this match
$pos_dif_record_ref_flag[$i][$k]= "is_confirmed";
$pos_dif_record[$i][$j]= $k; ####record the matched position
$flag_confirm=1;
$condition=1;
last;
}
}
if($condition==1) ##check whether it is matched in last loop
{
last;
}
}
}
elsif (($j eq ($arry_sys_length[$i]-1))|| ($j eq ($arry_sys_length[$i]-2))) ##this word is '.' or a word in the end of the sys-output sentence
{
$condition=0;
for($count_num_sys=1;$count_num_sys<=2;$count_num_sys++) ##check the before two words' match
{
for($count_num_ref=1;$count_num_ref<=2;$count_num_ref++)##to match the reference before two words
{
if($arrytwo_sys_translation[$i][$j-$count_num_sys] eq $arrytwo_ref_translation[$i][$k-$count_num_ref])
{
$pos_dif_record_flag[$i][$j]= "confirm_match"; ##if the context is also matched then confirm this match
$pos_dif_record_ref_flag[$i][$k]= "is_confirmed";
$pos_dif_record[$i][$j]= $k;###record the matched position
$flag_confirm=1;
$condition=1;
last;
}
}
if($condition==1) ##check whether it is matched in last loop
{
last;
}
}
}
else ### this word is in the middle of sys-output sentence,not beginnin and not end
{
$condition=0;
for($count_num_sys=-2;$count_num_sys<2;$count_num_sys++) ##check the former and back two words' match
{
for($count_num_ref=-2;$count_num_ref<=2;$count_num_ref++)##to match the former and back two words' match
{
if($arrytwo_sys_translation[$i][$j+$count_num_sys] eq $arrytwo_ref_translation[$i][$k+$count_num_ref])
{
$pos_dif_record_flag[$i][$j]= "confirm_match"; ##if the context is also matched then confirm this match
$pos_dif_record_ref_flag[$i][$k]= "is_confirmed";
$pos_dif_record[$i][$j]= $k;###record the matched position
$flag_confirm=1;
$condition=1;
last;
}
}
if($condition==1) ##check whether it is matched in last loop
{
last;
}
}
}
if($flag_confirm==1)##if confirm_match has been down,then the following words in ref neednot go through to match again
{
last;
}
}
}
}
for($j=0;$j<$arry_sys_length[$i];$j++)###after all the confirm_match has done,then deal with the exist but not confirmed match,using nearest-match
{
$store_ref_unconfirm_pos=-10000;
for($k=0;$k<$arry_ref_length[$i];$k++)
{
if($pos_dif_record_flag[$i][$j] eq "exist_match") #deal with the existed but not confirmed word in sys-output
{
if($arrytwo_sys_translation[$i][$j] eq $arrytwo_ref_translation[$i][$k])
{
#if(!( grep(/^$k/,@record_position) )) ####every word in the reference use not more than once to matched
#if(!(grep(/^$k/,@)))##check whether position k has been confirmed
if($pos_dif_record_ref_flag[$i][$k] eq "un_confirmed")##this ref-word has not been confirmed
{
if( abs($k-$j) < abs($store_ref_unconfirm_pos-$j)) ##select the nearest word from ref to match sys-word
{
$store_ref_unconfirm_pos=$k;
}
}
}
}
}
if($store_ref_unconfirm_pos>=0)
{
$pos_dif_record[$i][$j]= $store_ref_unconfirm_pos;###record the nearest matched position
}
}
for($j=0;$j<$arry_sys_length[$i];$j++)##after all the matched postion recored,then calculate each word's Pos-Diff value
{
if($pos_dif_record_flag[$i][$j] eq "none_match")
{
##$pos_dif[$i][$j]= abs(($store_ref_pos+1)/$arry_ref_length[$i]-($j+1)/$arry_sys_length[$i]);
$pos_dif[$i][$j]=0;
}
else ##calculate the matched word's PosDiff
{
$pos_dif[$i][$j]= abs((($j+1)/$arry_sys_length[$i])-(($pos_dif_record[$i][$j]+1)/$arry_ref_length[$i]));
}
}
}
@Pos_dif_sum= ();
@Pos_dif_value= ();
for($i=0;$i<$sentence_num;$i++)
{
for( $j=0; $j<$arry_sys_length[$i]; $j++ ) #### sum the Pos_dif_distance of one sentence,then divided by the lenth of the sentence
{
## $Pos_dif_sum[$i]= $Pos_dif_sum[$i] + $pos_dif[$i][$j]; ## this is original code by simply adding the pos_dif[][], without judging whether the sysout and ref are exact the same sentence.
if(($common_num[$i]==$arry_sys_length[$i])and($arry_sys_length[$i]==$arry_ref_length[$i])) ## if the output match othe reference exactly, then NPD=0 so that PosPenalty=1, i.e. no penalty. added in 20171013th
{
$Pos_dif_sum[$i]=0;
}
else
{
$Pos_dif_sum[$i]= $Pos_dif_sum[$i]+$pos_dif[$i][$j];
}
}
if($arry_sys_length[$i]>0)
{
$Pos_dif_sum[$i]=$Pos_dif_sum[$i]/$arry_sys_length[$i];
$Pos_dif_value[$i]= exp(-$Pos_dif_sum[$i]); #### calculate the every sentence's value of Pos_dif_value by taking the exp.
}
#$Pos_dif_sum[$i]=$Pos_dif_sum[$i]/$arry_sys_length[$i];
#$Pos_dif_value[$i]= exp(-$Pos_dif_sum[$i]); #### calculate the every sentence's value of Pos_dif_value by taking the exp.
}
print RESULT 'Position different penalty:',"\n","@Pos_dif_value","\n",$i,"\n";
$Mean_pos_dif_value=0;
for($i=0;$i<$sentence_num;$i++)
{
$Mean_pos_dif_value= $Mean_pos_dif_value+$Pos_dif_value[$i];
}
$Mean_pos_dif_value= $Mean_pos_dif_value/$sentence_num;
print RESULT 'mean Position different penalty:',"\n","$Mean_pos_dif_value","\n",$i,"\n";
@LEPOR_single_sentence=();
$LEPOR=0;
for($i=0;$i<$sentence_num;$i++) #### calculate the final evaluation value of LEPOR
{
$LEPOR_single_sentence[$i]= $LP[$i]*$Pos_dif_value[$i]*$Harmonic_mean_PR[$i];
$LEPOR= $LEPOR+$LEPOR_single_sentence[$i];
}
$LEPOR= $LEPOR/$sentence_num;
print RESULT 'evaluation value LEPOR of every single sentence:',"\n","@LEPOR_single_sentence","\n",$i,"\n";
print RESULT 'mean value LEPOR of all single sentence:',"\n","$LEPOR","\n";
#### another way to calculate the mean LEPOR of system_output(using all sentences' mean parameter-value)
$LEPOR_anotherway=0;
$LEPOR_anotherway= $Mean_LP*$Mean_pos_dif_value*$Mean_HarmonicMean;
print RESULT 'mean value LEPOR_anotherway of all single sentence:',"\n","$LEPOR_anotherway","\n";
close RESULT;
}
}