-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhppr algorithm source code
640 lines (555 loc) · 29.5 KB
/
hppr algorithm source code
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
#!/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 HPPR #####
## HPPR evaluation metric is proposed by Aaron Li-Feng Han, Derek F. Wong, Lidia S. Chao, Liangye He, Shuo Li, and Ling Zhu in University of Macau #####
## This perl code is written by Aaron Li-Feng Han in university of macau, 2013.02 #####
## 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 #####
## "Phrase Tagset Mapping for French and English Treebanks and Its Application in Machine Translation Evaluation". 2013. Aaron Li-Feng Han, Derek F. Wong, Lidia S. Chao, #####
## Liangye He, Shuo Li, and Ling Zhu. Proceedings of the GSCL 2013, Germany. LNCS Vol. 8105, pp. 119–131. Volume Editors: Iryna Gurevych, Chris Biemann and Torsten Zesch. #####
## Source code website: https://github.com/aaronlifenghan/aaron-project-hppr #####
## Online paper: http://www.springer.com/computer/database+management+%26+information+retrieval/book/978-3-642-40721-5 #####
########################################################################################################################################################################################
## How to use this Perl code and how to assign the weights of sub-factors, e.g. n-gram position difference, n-gram Precision, n-gram Recall, and the ratio of Ps:Pr:Rc. #####
## 1. Your system output translation documents and the reference translation document should contain the plain text only, each line containing one sentence. #####
## 2. Put you system output translation documents under the address in Line 24, 60, 63 of this Perl code. #####
## 3. Put you reference translation document under the address in Line 30 of this Perl code. #####
## 4. Tune the ngram positon difference factor in Line 437,439;ngram precision and recall weights in Line 340, Ps:Pr:Rc value in Line 602-604 of this Perl code. #####
## 5. The document containing evaluation score of HPPR will be shown under the address in Line 66 of this Perl code. #####
## #####
########################################################################################################################################################################################
opendir (DIR, "D:\\AaronLiFengHan\\test NLEPOMS2\\UniDeal_ppos2012\\system-outputs\\newstest2012\\fr-en\\parsed.ppos") || die "can not open output file!"; ##Put you system output translation documents under the address
@filename=readdir(DIR);
closedir (DIR);
open REF,"<:encoding(utf8)","D:\\AaronLiFengHan\\test NLEPOMS2\\UniDeal_ppos2012\\src\\newstest2012-src.fr\\ppos\\Uni_ppos_newstest2012-ref.fr.tok.parsed.ppos.txt" or die "can't open reference\n"; ##Put you reference translation document under the address
$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;
#print RESULT 'length of reference:',"\n","@arry_ref_length","\n",$j,"\n";
#print 'lines number of reference:',$j,"\n";
close REF;
foreach $file (@filename)
{
if(!(-d "D:\\AaronLiFengHan\\test NLEPOMS2\\UniDeal_ppos2012\\system-outputs\\newstest2012\\fr-en\\parsed.ppos\\$file")) ##Put you system output translation documents under the address
{
open (TEST,"<:encoding(utf8)","D:\\AaronLiFengHan\\test NLEPOMS2\\UniDeal_ppos2012\\system-outputs\\newstest2012\\fr-en\\parsed.ppos\\$file") || die "can not open file: $!"; ##Put you system output translation documents under the address
open (RESULT,">:encoding(utf8)","D:\\AaronLiFengHan\\test NLEPOMS2\\UniDeal_ppos2012\\system-outputs\\newstest2012\\fr-en\\parsed.ppos\\2NP3Pr3R_191_$file.txt") || die "$!"; ##The document containing evaluation score of HPPR will be shown under the address
$i=0;
$str0="";
@arry_1= ();
@arry_sys_length= ();
@arrytwo_sys_translation= ();
$sentence_num=0;
while($str0=<TEST>) #### put the system translation into a two dimension array @arrytwo_sys_translation
{
chomp($str0);
$str0= lc ($str0); ### both reference and system output translation is turned into lowwer case
@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]>0 && $arry_ref_length[$k]>0)
{
if($arry_sys_length[$k]>$arry_ref_length[$k])
{
if($arry_ref_length[$k]>0)
{
$LP[$k]=exp(1-($arry_sys_length[$k]/$arry_ref_length[$k]));
}
else
{
$LP[$k]=0;
}
}
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 unigram 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 'unigram common number between sys and ref:',"\n","@common_num","\n",$i,"\n";
@Similarity=(); ##### calculate the traditional unigram similarity and modified similarity scores
@MSimilarity=();
for($i=0;$i<$sentence_num;$i++)
{
if(($common_num[$i])!= 0)
{
$Similarity[$i]=2*$common_num[$i]/($arry_sys_length[$i]+$arry_ref_length[$i]);
$MSimilarity[$i]=$common_num[$i]*$common_num[$i]/($arry_sys_length[$i]*$arry_ref_length[$i]);
}
else
{
$Similarity[$i]=0;
$MSimilarity[$i]=0;
}
}
print RESULT 'unigram similarity of sys:',"\n","@Similarity","\n",$i,"\n";
print RESULT 'modified unigram similarity of sys:',"\n","@MSimilarity","\n",$i,"\n";
$Mean_Similarity=0;
$Mean_MSimilarity=0;
for($i=0;$i<$sentence_num;$i++)
{
$Mean_Similarity= $Mean_Similarity+$Similarity[$i];
$Mean_MSimilarity= $Mean_MSimilarity+$MSimilarity[$i];
}
$Mean_Similarity= $Mean_Similarity/$sentence_num;
$Mean_MSimilarity= $Mean_MSimilarity/$sentence_num;
print RESULT 'mean unigram similarity of sys:',"\n","$Mean_Similarity","\n",$i,"\n";
print RESULT 'mean modified unigram similarity of sys:',"\n","$Mean_MSimilarity","\n",$i,"\n";
@P= ();
@R= ();
for($i=0;$i<$sentence_num;$i++) #####calculate the unigram 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 'unigram precision of sys:',"\n","@P","\n",$i,"\n";
print RESULT 'unigram 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 unigram precision of sys:',"\n","$Mean_precision","\n",$i,"\n";
print RESULT 'mean unigram recall of sys:',"\n","$Mean_recall","\n",$i,"\n";
@bigram_common_num=();
for($i=0;$i<$sentence_num;$i++) ##### store the bigram common number between sys and ref into @bigram_common_num
{
$m=0;@record_bigram_position=(); ####everytime,select one sentence from the sys, clear the record array
for($j=0;$j<$arry_sys_length[$i]-1;$j++)
{
for($k=0;$k<$arry_ref_length[$i]-1;$k++)
{
if($arrytwo_sys_translation[$i][$j] eq $arrytwo_ref_translation[$i][$k] && $arrytwo_sys_translation[$i][$j+1] eq $arrytwo_ref_translation[$i][$k+1])
{
#if(!( any(@record_position) eq $k )) ####every word in the reference use not more than once to matched
if(!( grep(/^$k/,@record_bigram_position) )) ####every word in the reference use not more than once to matched
{
$bigram_common_num[$i]++;
$record_bigram_position[$m]=$k; $m++; ####record the position in the reference already matched
last; #### every word of the sys only match the reference once (if this word in systemout has been aligned to one word in reference, then it must stop to align other word in reference)
}
}
}
}
}
print RESULT 'bigram common number between sys and ref:',"\n","@bigram_common_num","\n",$i,"\n";
@bigram_P= ();
@bigram_R= ();
for($i=0;$i<$sentence_num;$i++) #####calculate the bigram precision and recall into @bigram_P and @bigram_R
{
if(($bigram_common_num[$i])!= 0)
{
$bigram_P[$i]=$bigram_common_num[$i]/($arry_sys_length[$i]-1);
$bigram_R[$i]=$bigram_common_num[$i]/($arry_ref_length[$i]-1);
}
else
{
$bigram_P[$i]=0;
$bigram_R[$i]=0;
}
}
print RESULT 'bigram precision of sys:',"\n","@bigram_P","\n",$i,"\n";
print RESULT 'bigram recall of sys:',"\n","@bigram_R","\n",$i,"\n";
$Mean_bigram_precision=0;
$Mean_bigram_recall=0;
for($i=0;$i<$sentence_num;$i++)
{
$Mean_bigram_precision= $Mean_bigram_precision+$bigram_P[$i];
$Mean_bigram_recall= $Mean_bigram_recall+$bigram_R[$i];
}
$Mean_bigram_precision= $Mean_bigram_precision/$sentence_num;
$Mean_bigram_recall= $Mean_bigram_recall/$sentence_num;
print RESULT 'mean bigram precision of sys:',"\n","$Mean_bigram_precision","\n",$i,"\n";
print RESULT 'mean bigram recall of sys:',"\n","$Mean_bigram_recall","\n",$i,"\n";
@trigram_common_num=();
for($i=0;$i<$sentence_num;$i++) ##### store the trigram common number between sys and ref into @trigram_common_num
{
$m=0;@record_trigram_position=(); ####everytime,select one sentence from the sys, clear the record array
for($j=0;$j<$arry_sys_length[$i]-2;$j++)
{
for($k=0;$k<$arry_ref_length[$i]-2;$k++)
{
if(($arrytwo_sys_translation[$i][$j] eq $arrytwo_ref_translation[$i][$k]) && ($arrytwo_sys_translation[$i][$j+1] eq $arrytwo_ref_translation[$i][$k+1]) && ($arrytwo_sys_translation[$i][$j+2] eq $arrytwo_ref_translation[$i][$k+2]))
{
#if(!( any(@record_position) eq $k )) ####every word in the reference use not more than once to matched
if(!( grep(/^$k/,@record_trigram_position) )) ####every word in the reference use not more than once to matched
{
$trigram_common_num[$i]++;
$record_trigram_position[$m]=$k; $m++; ####record the position in the reference already matched
last; #### every word of the sys only match the reference once (if this word in systemout has been aligned to one word in reference, then it must stop to align other word in reference)
}
}
}
}
}
print RESULT 'trigram common number between sys and ref:',"\n","@trigram_common_num","\n",$i,"\n";
@trigram_P= ();
@trigram_R= ();
for($i=0;$i<$sentence_num;$i++) #####calculate the trigram precision and recall into @P and @R
{
if(($trigram_common_num[$i])!= 0)
{
$trigram_P[$i]=$trigram_common_num[$i]/($arry_sys_length[$i]-2);
$trigram_R[$i]=$trigram_common_num[$i]/($arry_ref_length[$i]-2);
}
else
{
$trigram_P[$i]=0;
$trigram_R[$i]=0;
}
}
print RESULT 'trigram precision of sys:',"\n","@trigram_P","\n",$i,"\n";
print RESULT 'trigram recall of sys:',"\n","@trigram_R","\n",$i,"\n";
$Mean_trigram_precision=0;
$Mean_trigram_recall=0;
for($i=0;$i<$sentence_num;$i++)
{
$Mean_trigram_precision= $Mean_trigram_precision+$trigram_P[$i];
$Mean_trigram_recall= $Mean_trigram_recall+$trigram_R[$i];
}
$Mean_trigram_precision= $Mean_trigram_precision/$sentence_num;
$Mean_trigram_recall= $Mean_trigram_recall/$sentence_num;
print RESULT 'mean trigram precision of sys:',"\n","$Mean_trigram_precision","\n",$i,"\n";
print RESULT 'mean trigram recall of sys:',"\n","$Mean_trigram_recall","\n",$i,"\n";
@merged_uni_bi_tri_gram_P= (); #####calculate the merged (weighted) unigram, bigram, trigram precision and recall into @P and @R
@merged_uni_bi_tri_gram_R= ();
$weight_uni_gram= 1/(1+2+3); ## tune the unigram, bigram and trigram precision (recall) weights
$weight_bi_gram= 2/(1+2+3);
$weight_tri_gram= 3/(1+2+3);
for($i=0;$i<$sentence_num;$i++)
{
if((($P[$i])>0)||(($bigram_P[$i])>0)||(($trigram_P[$i])>0))
{
$merged_uni_bi_tri_gram_P[$i]=$weight_uni_gram*$P[$i]+$weight_bi_gram*$bigram_P[$i]+$weight_tri_gram*$trigram_P[$i];
}
else
{
$merged_uni_bi_tri_gram_P[$i]=0;
}
}
for($i=0;$i<$sentence_num;$i++)
{
if((($R[$i])>0)||(($bigram_R[$i])>0)||(($trigram_R[$i])>0))
{
$merged_uni_bi_tri_gram_R[$i]=$weight_uni_gram*$R[$i]+$weight_bi_gram*$bigram_R[$i]+$weight_tri_gram*$trigram_R[$i];
}
else
{
$merged_uni_bi_tri_gram_R[$i]=0;
}
}
print RESULT 'merged uni_bi_tri_gram precision of sys:',"\n","@merged_uni_bi_tri_gram_P","\n",$i,"\n";
print RESULT 'merged uni_bi_tri_gram recall of sys:',"\n","@merged_uni_bi_tri_gram_R","\n",$i,"\n";
$Mean_uni_bi_tri_gram_precision=0;
$Mean_uni_bi_tri_gram_recall=0;
for($i=0;$i<$sentence_num;$i++)
{
$Mean_uni_bi_tri_gram_precision= $Mean_uni_bi_tri_gram_precision+$merged_uni_bi_tri_gram_P[$i];
$Mean_uni_bi_tri_gram_recall= $Mean_uni_bi_tri_gram_recall+$merged_uni_bi_tri_gram_R[$i];
}
$Mean_uni_bi_tri_gram_precision= $Mean_uni_bi_tri_gram_precision/$sentence_num;
$Mean_uni_bi_tri_gram_recall= $Mean_uni_bi_tri_gram_recall/$sentence_num;
print RESULT 'mean uni_bi_tri_gram precision of sys:',"\n","$Mean_uni_bi_tri_gram_precision","\n",$i,"\n";
print RESULT 'mean uni_bi_tri_gram recall of sys:',"\n","$Mean_uni_bi_tri_gram_recall","\n",$i,"\n";
#$a=9; #####$a is a varerble to be changed according to different language envirenment #### H(P,9R)
#$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)
#$a=6/4; #####$a is a varerble to be changed according to different language envirenment#### H(4P,6R)
@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 ## tune the ngram position difference factor
{
for($count_num_ref=1;$count_num_ref<=2;$count_num_ref++)##to match the reference following two words ## tune the ngram position difference factor
{
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;
$pos_dif[$i][$j]= abs((($j+1)/$arry_sys_length[$i])-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];
}
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";
$Mean_uni_bi_tri_gram_precision=0;
$Mean_uni_bi_tri_gram_recall=0;
for($i=0;$i<$sentence_num;$i++)
{
$Mean_uni_bi_tri_gram_precision= $Mean_uni_bi_tri_gram_precision+$merged_uni_bi_tri_gram_P[$i];
$Mean_uni_bi_tri_gram_recall= $Mean_uni_bi_tri_gram_recall+$merged_uni_bi_tri_gram_R[$i];
}
$Mean_uni_bi_tri_gram_precision= $Mean_uni_bi_tri_gram_precision/$sentence_num;
$Mean_uni_bi_tri_gram_recall= $Mean_uni_bi_tri_gram_recall/$sentence_num;
print RESULT 'mean uni_bi_tri_gram precision of sys:',"\n","$Mean_uni_bi_tri_gram_precision","\n",$i,"\n";
print RESULT 'mean uni_bi_tri_gram recall of sys:',"\n","$Mean_uni_bi_tri_gram_recall","\n",$i,"\n";
@NPosP_Preci_Recall_single_sentence=(); ###NHarPPO_merge3grampricision n-gram based harmonic of 3gram Precision and bigram-Position Order.
$Har_NPosP_Preci_Recall=0;
##$weight_mergeRecall=0;
$weight_mergeRecall=1; ### tune the Ps:Pr:Rc (Ps means ngram Position difference penalty, Pr means merged ngram Precision, Rc means merged ngram Recall)
$weight_mergePrecision=9; ### tune the Ps:Pr:Rc (Ps means ngram Position difference penalty, Pr means merged ngram Precision, Rc means merged ngram Recall)
$weight_Pos=1; ### tune the Ps:Pr:Rc (Ps means ngram Position difference penalty, Pr means merged ngram Precision, Rc means merged ngram Recall)
for($i=0;$i<$sentence_num;$i++) #### calculate the final evaluation value of Har_NPosP_Preci_Recall
{
if($merged_uni_bi_tri_gram_P[$i]>0 && $Pos_dif_value[$i]>0 && $merged_uni_bi_tri_gram_R[$i]>0)
{
$NPosP_Preci_Recall_single_sentence[$i]= ($weight_mergePrecision+$weight_Pos+$weight_mergeRecall)/($weight_mergePrecision/$merged_uni_bi_tri_gram_P[$i]+$weight_Pos/$Pos_dif_value[$i]+$weight_mergeRecall/$merged_uni_bi_tri_gram_R[$i]);
}
else
{
$NPosP_Preci_Recall_single_sentence[$i]=0;
}
$Har_NPosP_Preci_Recall= $Har_NPosP_Preci_Recall+$NPosP_Preci_Recall_single_sentence[$i];
}
$Har_NPosP_Preci_Recall= $Har_NPosP_Preci_Recall/$sentence_num;
print RESULT 'evaluation value Har_NPosP_Preci_Recall of every single sentence:',"\n","@NPosP_Preci_Recall_single_sentence","\n",$i,"\n";
print RESULT 'mean value Har_NPosP_Preci_Recall of all single sentence:',"\n","$Har_NPosP_Preci_Recall","\n";
#### another way to calculate the mean Har_NPosP_Preci_Recall of system_output(using all sentences' mean parameter-value)
$Har_NPosP_Preci_Recall_anotherway=0;
if($Mean_uni_bi_tri_gram_precision>0 && $Mean_pos_dif_value>0 && $Mean_uni_bi_tri_gram_recall>0)
{
$Har_NPosP_Preci_Recall_anotherway= ($weight_mergePrecision+$weight_Pos+$weight_mergeRecall)/($weight_mergePrecision/$Mean_uni_bi_tri_gram_precision+$weight_Pos/$Mean_pos_dif_value+$weight_mergeRecall/$Mean_uni_bi_tri_gram_recall);
}
else
{
$Har_NPosP_Preci_Recall_anotherway=0;
}
print RESULT 'mean value Har_NPosP_Preci_Recall_anotherway of all single sentence:',"\n","$Har_NPosP_Preci_Recall_anotherway","\n";
close RESULT;
}
}