-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathROUGE-WE-1.0.0.pl
executable file
·3271 lines (3140 loc) · 96.5 KB
/
ROUGE-WE-1.0.0.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
# Version: ROUGE-WE v1.0
# Date: 2015 Aug 3
# Author: Jun-Ping Ng (http://wing.comp.nus.edu.sg/~junping)
# Download:
# The latest version of this code base can always be downloaded from
# https://github.com/ng-j-p/rouge-we
# Description:
# This script is the implementation for ROUGe-WE, as described in
# Better Summarization Evaluation with Word Embeddings for ROUGE
# Jun-Ping Ng and Viktoria Abrecht
# In Proceedings of Conference on Empirical Methods in Natural
# Language Processing (EMNLP 2015)
#
# It is based on ROUGE v1.5.5 (http://www.berouge.com/) by
# Chin-Yew Lin.
# Original license of ROUGE is found below this pre-amble.
#
# Licensing:
# ROUGE-WE v1.0 is released under the MIT license (https://github.com/ng-j-p/rouge-we/blob/master/LICENSE)
#
# Disclaimer:
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
## Original license distributed with ROUGE v1.5.5
#################################################
# Version: ROUGE v1.5.5
# Date: 05/26/2005,05/19/2005,04/26/2005,04/03/2005,10/28/2004,10/25/2004,10/21/2004
# Author: Chin-Yew Lin
# Description: Given an evaluation description file, for example: test.xml,
# this script computes the averages of the average ROUGE scores for
# the evaluation pairs listed in the ROUGE evaluation configuration file.
# For more information, please see:
# http://www.isi.edu/~cyl/ROUGE
# For more information about Basic Elements, please see:
# http://www.isi.edu/~cyl/BE
#
# COPYRIGHT (C) UNIVERSITY OF SOUTHERN CALIFORNIA, 2002,2003,2004
# University of Southern California
# Information Sciences Institute
# 4676 Admiralty Way
# Marina Del Rey, California 90292-6695
#
# This software was partially developed under SPAWAR Grant No.
# N66001-00-1-8916 , and the Government holds license rights under
# DAR 7-104.9(a)(c)(1). It is
# transmitted outside of the University of Southern California only under
# written license agreements or software exchange agreements, and its use
# is limited by these agreements. At no time shall any recipient use
# this software in any manner which conflicts or interferes with the
# governmental license rights or other provisions of the governing
# agreement under which it is obtained. It is supplied "AS IS," without
# any warranties of any kind. It is furnished only on the basis that any
# party who receives it indemnifies and holds harmless the parties who
# furnish and originate it against any claims, demands or liabilities
# connected with using it, furnishing it to others or providing it to a
# third party. THIS NOTICE MUST NOT BE REMOVED FROM THE SOFTWARE,
# AND IN THE EVENT THAT THE SOFTWARE IS DIVIDED, IT SHOULD BE
# ATTACHED TO EVERY PART.
#
# Contributor to its design is Chin-Yew Lin.
##################################
# End original license from ROUGE v1.5.5
#################################
## Edits from ROUGE-1.5.5 marked out with EDIT-WE
##
## EDIT-WE
use LWP::UserAgent;
use HTTP::Request::Common;
use HTTP::Response;
use JSON qw( decode_json );
## EDIT-WE
use XML::DOM;
use DB_File;
use Getopt::Std;
use Scalar::Util qw( looks_like_number );
#-------------------------------------------------------------------------------------
use vars qw($opt_a $opt_b $opt_c $opt_d $opt_e $opt_f $opt_h $opt_H $opt_m $opt_n $opt_p $opt_s $opt_t $opt_l $opt_v $opt_w $opt_2 $opt_u $opt_x $opt_U $opt_3 $opt_M $opt_z);
my $usageFull="$0\n [-a (evaluate all systems)]
[-c cf]
[-d (print per evaluation scores)]
[-e ROUGE_EVAL_HOME]
[-h (usage)]
[-H (detailed usage)]
[-b n-bytes|-l n-words]
[-m (use Porter stemmer)]
[-n max-ngram]
[-s (remove stopwords)]
[-r number-of-samples (for resampling)]
[-2 max-gap-length (if < 0 then no gap length limit)]
[-3 <H|HM|HMR|HM1|HMR1|HMR2> (for scoring based on BE)]
[-u (include unigram in skip-bigram) default no)]
[-U (same as -u but also compute regular skip-bigram)]
[-w weight (weighting factor for WLCS)]
[-v (verbose)]
[-x (do not calculate ROUGE-L)]
[-f A|B (scoring formula)]
[-p alpha (0 <= alpha <=1)]
[-t 0|1|2 (count by token instead of sentence)]
[-z <SEE|SPL|ISI|SIMPLE>]
<ROUGE-eval-config-file> [<systemID>]\n
".
"ROUGE-eval-config-file: Specify the evaluation setup. Three files come with the ROUGE evaluation package, i.e.\n".
" ROUGE-test.xml, verify.xml, and verify-spl.xml are good examples.\n".
"systemID: Specify which system in the ROUGE-eval-config-file to perform the evaluation.\n".
" If '-a' option is used, then all systems are evaluated and users do not need to\n".
" provide this argument.\n".
"Default:\n".
" When running ROUGE without supplying any options (except -a), the following defaults are used:\n".
" (1) ROUGE-L is computed;\n".
" (2) 95% confidence interval;\n".
" (3) No stemming;\n".
" (4) Stopwords are inlcuded in the calculations;\n".
" (5) ROUGE looks for its data directory first through the ROUGE_EVAL_HOME environment variable. If\n".
" it is not set, the current directory is used.\n".
" (6) Use model average scoring formula.\n".
" (7) Assign equal importance of ROUGE recall and precision in computing ROUGE f-measure, i.e. alpha=0.5.\n".
" (8) Compute average ROUGE by averaging sentence (unit) ROUGE scores.\n".
"Options:\n".
" -2: Compute skip bigram (ROGUE-S) co-occurrence, also specify the maximum gap length between two words (skip-bigram)\n".
" -u: Compute skip bigram as -2 but include unigram, i.e. treat unigram as \"start-sentence-symbol unigram\"; -2 has to be specified.\n".
" -3: Compute BE score. Currently only SIMPLE BE triple format is supported.\n".
" H -> head only scoring (does not applied to Minipar-based BEs).\n".
" HM -> head and modifier pair scoring.\n".
" HMR -> head, modifier and relation triple scoring.\n".
" HM1 -> H and HM scoring (same as HM for Minipar-based BEs).\n".
" HMR1 -> HM and HMR scoring (same as HMR for Minipar-based BEs).\n".
" HMR2 -> H, HM and HMR scoring (same as HMR for Minipar-based BEs).\n".
" -a: Evaluate all systems specified in the ROUGE-eval-config-file.\n".
" -c: Specify CF\% (0 <= CF <= 100) confidence interval to compute. The default is 95\% (i.e. CF=95).\n".
" -d: Print per evaluation average score for each system.\n".
" -e: Specify ROUGE_EVAL_HOME directory where the ROUGE data files can be found.\n".
" This will overwrite the ROUGE_EVAL_HOME specified in the environment variable.\n".
" -f: Select scoring formula: 'A' => model average; 'B' => best model\n".
" -h: Print usage information.\n".
" -H: Print detailed usage information.\n".
" -b: Only use the first n bytes in the system/peer summary for the evaluation.\n".
" -l: Only use the first n words in the system/peer summary for the evaluation.\n".
" -m: Stem both model and system summaries using Porter stemmer before computing various statistics.\n".
" -n: Compute ROUGE-N up to max-ngram length will be computed.\n".
" -p: Relative importance of recall and precision ROUGE scores. Alpha -> 1 favors precision, Alpha -> 0 favors recall.\n".
" -s: Remove stopwords in model and system summaries before computing various statistics.\n".
" -t: Compute average ROUGE by averaging over the whole test corpus instead of sentences (units).\n".
" 0: use sentence as counting unit, 1: use token as couting unit, 2: same as 1 but output raw counts\n".
" instead of precision, recall, and f-measure scores. 2 is useful when computation of the final,\n".
" precision, recall, and f-measure scores will be conducted later.\n".
" -r: Specify the number of sampling point in bootstrap resampling (default is 1000).\n".
" Smaller number will speed up the evaluation but less reliable confidence interval.\n".
" -w: Compute ROUGE-W that gives consecutive matches of length L in an LCS a weight of 'L^weight' instead of just 'L' as in LCS.\n".
" Typically this is set to 1.2 or other number greater than 1.\n".
" -v: Print debugging information for diagnositic purpose.\n".
" -x: Do not calculate ROUGE-L.\n".
" -z: ROUGE-eval-config-file is a list of peer-model pair per line in the specified format (SEE|SPL|ISI|SIMPLE).\n";
my $usage="$0\n [-a (evaluate all systems)]
[-c cf]
[-d (print per evaluation scores)]
[-e ROUGE_EVAL_HOME]
[-h (usage)]
[-H (detailed usage)]
[-b n-bytes|-l n-words]
[-m (use Porter stemmer)]
[-n max-ngram]
[-s (remove stopwords)]
[-r number-of-samples (for resampling)]
[-2 max-gap-length (if < 0 then no gap length limit)]
[-3 <H|HM|HMR|HM1|HMR1|HMR2> (for scoring based on BE)]
[-u (include unigram in skip-bigram) default no)]
[-U (same as -u but also compute regular skip-bigram)]
[-w weight (weighting factor for WLCS)]
[-v (verbose)]
[-x (do not calculate ROUGE-L)]
[-f A|B (scoring formula)]
[-p alpha (0 <= alpha <=1)]
[-t 0|1|2 (count by token instead of sentence)]
[-z <SEE|SPL|ISI|SIMPLE>]
<ROUGE-eval-config-file> [<systemID>]
";
getopts('ahHb:c:de:f:l:mMn:p:st:r:2:3:w:uUvxz:');
my $systemID;
## EDIT-WE
my $ua = LWP::UserAgent->new;
my $server_endpoint = "http://127.0.0.1:8888/word2vecdiff";
## EDIT-WE
die $usageFull if defined($opt_H);
die $usage if defined($opt_h)||@ARGV==0;
die "Please specify the ROUGE configuration file or use option '-h' for help\n" if(@ARGV==0);
if(@ARGV==1&&defined($opt_z)) {
$systemID="X"; # default system ID
}
elsif(@ARGV==1&&!defined($opt_a)) {
die "Please specify a system ID to evaluate or use option '-a' to evaluate all systems. For more information, use option '-h'.\n";
}
elsif(@ARGV==2) {
$systemID=$ARGV[1];
}
if(defined($opt_e)) {
$stopwords="$opt_e/smart_common_words.txt";
$wordnetDB="$opt_e/WordNet-2.0.exc.db";
}
else {
if(exists($ENV{"ROUGE_EVAL_HOME"})) {
$stopwords="$ENV{\"ROUGE_EVAL_HOME\"}/smart_common_words.txt";
$wordnetDB="$ENV{\"ROUGE_EVAL_HOME\"}/WordNet-2.0.exc.db";
}
elsif(exists($ENV{"RED_EVAL_HOME"})) {
$stopwords="$ENV{\"RED_EVAL_HOME\"}/smart_common_words.txt";
$wordnetDB="$ENV{\"RED_EVAL_HOME\"}/WordNet-2.0.exc.db";
}
else {
# if no environment variable exists then assume data files are in the current directory
$stopwords="smart_common_words.txt";
$wordnetDB="WordNet-2.0.exc.db";
}
}
if(defined($opt_s)) {
$useStopwords=0; # do not use stop words
}
else {
$useStopwords=1; # use stop words
}
if(defined($opt_l)&&defined($opt_b)) {
die "Please specify length limit in words or bytes but not both.\n";
}
if(defined($opt_l)) {
$lengthLimit=$opt_l;
$byteLimit=0; # no byte limit
}
elsif(defined($opt_b)) {
$lengthLimit=0; # no length limit in words
$byteLimit=$opt_b;
}
else {
$byteLimit=0; # no byte limit
$lengthLimit=0; # no length limit
}
unless(defined($opt_c)) {
$opt_c=95;
}
else {
if($opt_c<0||$opt_c>100) {
die "Confidence interval should be within 0 and 100. Use option -h for more details.\n";
}
}
if(defined($opt_w)) {
if($opt_w>0) {
$weightFactor=$opt_w;
}
else {
die "ROUGE-W weight factor must greater than 0.\n";
}
}
#unless(defined($opt_n)) {
# $opt_n=4; # default maximum ngram is 4
#}
if(defined($opt_v)) {
$debug=1;
}
else {
$debug=0;
}
if(defined($opt_r)) {
$numOfResamples=$opt_r;
}
else {
$numOfResamples=1000;
}
if(defined($opt_2)) {
$skipDistance=$opt_2;
}
if(defined($opt_3)) {
$BEMode=$opt_3;
}
if(defined($opt_f)) {
$scoreMode=$opt_f;
}
else {
$scoreMode="A"; # default: use model average scoring formula
}
if(defined($opt_p)) {
$alpha=$opt_p;
if($alpha<0||
$alpha>1) {
die "Relative importance of ROUGE recall and precision has to be between 0 and 1 inclusively.\n";
}
}
else {
$alpha=0.5; # default is equal importance of ROUGE recall and precision
}
if(defined($opt_t)) {
# make $opt_t as undef when appropriate option is given
# when $opt_t is undef, sentence level average will be used
if($opt_t==0) {
$opt_t=undef;
}
elsif($opt_t!=1&&
$opt_t!=2) {
$opt_t=undef; # other than 1 or 2, let $opt_t to be undef
}
}
if(defined($opt_z)) {
# If opt_z is specified, the user has to specify a system ID that
# is used for identification therefore -a option is not allowed.
# Here we make it undef.
$opt_a=undef;
}
#-------------------------------------------------------------------------------------
# Setup ROUGE scoring parameters
%ROUGEParam=(); # ROUGE scoring parameter
if(defined($lengthLimit)) {
$ROUGEParam{"LENGTH"}=$lengthLimit;
}
else {
$ROUGEParam{"LENGTH"}=undef;
}
if(defined($byteLimit)) {
$ROUGEParam{"BYTE"}=$byteLimit;
}
else {
$ROUGEParam{"BYTE"}=undef;
}
if(defined($opt_n)) { # ngram size
$ROUGEParam{"NSIZE"}=$opt_n;
}
else {
$ROUGEParam{"NSIZE"}=undef;
}
if(defined($weightFactor)) {
$ROUGEParam{"WEIGHT"}=$weightFactor;
}
else {
$ROUGEParam{"WEIGHT"}=undef;
}
if(defined($skipDistance)) {
$ROUGEParam{"SD"}=$skipDistance;
}
else {
$ROUGEParam{"SD"}=undef;
}
if(defined($scoreMode)) {
$ROUGEParam{"SM"}=$scoreMode;
}
else {
$ROUGEParam{"SM"}=undef;
}
if(defined($alpha)) {
$ROUGEParam{"ALPHA"}=$alpha;
}
else {
$ROUGEParam{"ALPHA"}=undef;
}
if(defined($opt_t)) {
$ROUGEParam{"AVERAGE"}=$opt_t;
}
else {
$ROUGEParam{"AVERAGE"}=undef;
}
if(defined($opt_3)) {
$ROUGEParam{"BEMODE"}=$opt_3;
}
else {
$ROUGEParam{"BEMODE"}=undef;
}
#-------------------------------------------------------------------------------------
# load stopwords
%stopwords=();
open(STOP,$stopwords)||die "Cannot open $stopwords\n";
while(defined($line=<STOP>)) {
chomp($line);
$stopwords{$line}=1;
}
close(STOP);
# load WordNet database
if(-e "$wordnetDB") {
tie %exceptiondb,'DB_File',"$wordnetDB",O_RDONLY,0440,$DB_HASH or
die "Cannot open exception db file for reading: $wordnetDB\n";
}
else {
die "Cannot open exception db file for reading: $wordnetDB\n";
}
#-------------------------------------------------------------------------------------
# Initialize Porter Stemmer
&initialise();
#-------------------------------------------------------------------------------------
# Read and parse the document
my $parser = new XML::DOM::Parser;
my $doc;
unless(defined($opt_z)) {
$doc=$parser->parsefile($ARGV[0]);
}
else {
open($doc,$ARGV[0])||die "Cannot open $ARGV[0]\n";
}
%ROUGEEvals=();
@ROUGEEvalIDs=();
%ROUGEPeerIDTable=();
@allPeerIDs=();
%knownMissing=(); # remember missing submission already known
if(defined($doc)) {
# read evaluation description file
&readEvals(\%ROUGEEvals,\@ROUGEEvalIDs,\%ROUGEPeerIDTable,$doc,undef);
# print evaluation configuration
if(defined($opt_z)) {
if(defined($ARGV[1])) {
$systemID=$ARGV[1];
}
else {
$systemID="X"; # default system ID in BE file list evaluation mode
}
push(@allPeerIDs,$systemID);
}
else {
unless(defined($opt_a)) {
$systemID=$ARGV[1];
push(@allPeerIDs,$systemID);
}
else {
# run evaluation for each peer listed in the description file
@allPeerIDs=sort (keys %ROUGEPeerIDTable);
}
}
foreach $peerID (@allPeerIDs) {
%testIDs=();
# print "\@PEER($peerID)--------------------------------------------------\n";
if(defined($opt_n)) {
# evaluate a specific peer
# compute ROUGE score up to $opt_n-gram
for($n=1;$n<=$opt_n;$n++) {
my (%ROUGEScores,%ROUGEAverages);
%ROUGEScores=();
foreach $e (@ROUGEEvalIDs) {
if($debug) {
print "\@Eval ($e)\n";
}
$ROUGEParam{"NSIZE"}=$n;
&computeROUGEX("N",\%ROUGEScores,$e,$ROUGEEvals{$e},$peerID,\%ROUGEParam);
}
# compute averages
%ROUGEAverages=();
&computeAverages(\%ROUGEScores,\%ROUGEAverages,$opt_t);
&printResults($peerID,\%ROUGEAverages,\%ROUGEScores,"ROUGE-$n",$opt_c,$opt_t,$opt_d);
}
}
unless(defined($opt_x)||defined($opt_3)) {
#-----------------------------------------------
# compute LCS score
%ROUGEScores=();
foreach $e (@ROUGEEvalIDs) {
&computeROUGEX("L",\%ROUGEScores,$e,$ROUGEEvals{$e},$peerID,\%ROUGEParam);
}
# compute averages
%ROUGEAverages=();
&computeAverages(\%ROUGEScores,\%ROUGEAverages,$opt_t);
&printResults($peerID,\%ROUGEAverages,\%ROUGEScores,"ROUGE-L",$opt_c,$opt_t,$opt_d);
}
if(defined($opt_w)) {
#-----------------------------------------------
# compute WLCS score
%ROUGEScores=();
foreach $e (@ROUGEEvalIDs) {
&computeROUGEX("W",\%ROUGEScores,$e,$ROUGEEvals{$e},$peerID,\%ROUGEParam);
}
# compute averages
%ROUGEAverages=();
&computeAverages(\%ROUGEScores,\%ROUGEAverages,$opt_t);
&printResults($peerID,\%ROUGEAverages,\%ROUGEScores,"ROUGE-W-$weightFactor",$opt_c,$opt_t,$opt_d);
}
if(defined($opt_2)) {
#-----------------------------------------------
# compute skip bigram score
%ROUGEScores=();
foreach $e (@ROUGEEvalIDs) {
&computeROUGEX("S",\%ROUGEScores,$e,$ROUGEEvals{$e},$peerID,\%ROUGEParam);
}
# compute averages
%ROUGEAverages=();
&computeAverages(\%ROUGEScores,\%ROUGEAverages,$opt_t);
if($skipDistance>=0) {
if(defined($opt_u)) {
&printResults($peerID,\%ROUGEAverages,\%ROUGEScores,"ROUGE-SU$skipDistance",$opt_c,$opt_t,$opt_d);
}
elsif(defined($opt_U)) {
# print regular skip bigram results
&printResults($peerID,\%ROUGEAverages,\%ROUGEScores,"ROUGE-S$skipDistance",$opt_c,$opt_t,$opt_d);
#-----------------------------------------------
# compute skip bigram with unigram extension score
$opt_u=1;
%ROUGEScores=();
foreach $e (@ROUGEEvalIDs) {
&computeROUGEX("S",\%ROUGEScores,$e,$ROUGEEvals{$e},$peerID,\%ROUGEParam);
}
$opt_u=undef;
# compute averages
%ROUGEAverages=();
&computeAverages(\%ROUGEScores,\%ROUGEAverages,$opt_t);
&printResults($peerID,\%ROUGEAverages,\%ROUGEScores,"ROUGE-SU$skipDistance",$opt_c,$opt_t,$opt_d);
}
else {
&printResults($peerID,\%ROUGEAverages,\%ROUGEScores,"ROUGE-S$skipDistance",$opt_c,$opt_t,$opt_d);
}
}
else {
if(defined($opt_u)) {
&printResults($peerID,\%ROUGEAverages,\%ROUGEScores,"ROUGE-SU*",$opt_c,$opt_t,$opt_d);
}
else {
&printResults($peerID,\%ROUGEAverages,\%ROUGEScores,"ROUGE-S*",$opt_c,$opt_t,$opt_d);
if(defined($opt_U)) {
#-----------------------------------------------
# compute skip bigram with unigram extension score
$opt_u=1;
%ROUGEScores=();
foreach $e (@ROUGEEvalIDs) {
&computeROUGEX("S",\%ROUGEScores,$e,$ROUGEEvals{$e},$peerID,\%ROUGEParam);
}
$opt_u=undef;
# compute averages
%ROUGEAverages=();
&computeAverages(\%ROUGEScores,\%ROUGEAverages,$opt_t);
&printResults($peerID,\%ROUGEAverages,\%ROUGEScores,"ROUGE-SU*",$opt_c,$opt_t,$opt_d);
}
}
}
}
if(defined($opt_3)) {
#-----------------------------------------------
# compute Basic Element triple score
%ROUGEScores=();
foreach $e (@ROUGEEvalIDs) {
&computeROUGEX("BE",\%ROUGEScores,$e,$ROUGEEvals{$e},$peerID,\%ROUGEParam);
}
# compute averages
%ROUGEAverages=();
&computeAverages(\%ROUGEScores,\%ROUGEAverages,$opt_t);
&printResults($peerID,\%ROUGEAverages,\%ROUGEScores,"ROUGE-BE-$BEMode",$opt_c,$opt_t,$opt_d);
}
}
}
else {
die "Document undefined\n";
}
if(defined($opt_z)) {
close($doc);
}
untie %exceptiondb;
sub printResults {
my $peerID=shift;
my $ROUGEAverages=shift;
my $ROUGEScores=shift;
my $methodTag=shift;
my $opt_c=shift;
my $opt_t=shift;
my $opt_d=shift;
print "---------------------------------------------\n";
if(!defined($opt_t)||$opt_t==1) {
print "$peerID $methodTag Average_R: $ROUGEAverages->{'AvgR'} ";
print "($opt_c\%-conf.int. $ROUGEAverages->{'CIAvgL_R'} - $ROUGEAverages->{'CIAvgU_R'})\n";
print "$peerID $methodTag Average_P: $ROUGEAverages->{'AvgP'} ";
print "($opt_c\%-conf.int. $ROUGEAverages->{'CIAvgL_P'} - $ROUGEAverages->{'CIAvgU_P'})\n";
print "$peerID $methodTag Average_F: $ROUGEAverages->{'AvgF'} ";
print "($opt_c\%-conf.int. $ROUGEAverages->{'CIAvgL_F'} - $ROUGEAverages->{'CIAvgU_F'})\n";
}
else {
print "$peerID $methodTag M_count: ";
print int($ROUGEAverages->{'M_cnt'});
print " P_count: ";
print int($ROUGEAverages->{'P_cnt'});
print " H_count: ";
print int($ROUGEAverages->{'H_cnt'});
print "\n";
}
if(defined($opt_d)) {
print ".............................................\n";
&printPerEvalData($ROUGEScores,"$peerID $methodTag Eval");
}
}
sub bootstrapResampling {
my $scores=shift;
my $instances=shift;
my $seed=shift;
my $opt_t=shift;
my $sample;
my ($i,$ridx);
# Use $seed to seed the random number generator to make sure
# we have the same random sequence every time, therefore a
# consistent estimation of confidence interval in different runs.
# This is not necessary. To ensure a consistent result in reporting
# results using ROUGE, this is implemented.
srand($seed);
for($i=0;$i<@{$instances};$i++) {
# generate a random index
$ridx=int(rand(@{$instances}));
unless(defined($sample)) {
# setup the resampling array
$sample=[];
push(@$sample,$scores->{$instances->[$ridx]}[0]);
push(@$sample,$scores->{$instances->[$ridx]}[1]);
push(@$sample,$scores->{$instances->[$ridx]}[2]);
}
else {
# update the resampling array
$sample->[0]+=$scores->{$instances->[$ridx]}[0];
$sample->[1]+=$scores->{$instances->[$ridx]}[1];
$sample->[2]+=$scores->{$instances->[$ridx]}[2];
}
}
# compute the average result for this resampling procedure
unless(defined($opt_t)) {
# per instance or sentence average
if(@{$instances}>0) {
$sample->[0]/=@{$instances};
$sample->[1]/=@{$instances};
$sample->[2]/=@{$instances};
}
else {
$sample->[0]=0;
$sample->[1]=0;
$sample->[2]=0;
}
}
else {
if($opt_t==1) {
# per token or corpus level average
# output recall, precision, and f-measure score
my ($tmpR,$tmpP,$tmpF);
if($sample->[0]>0) {
$tmpR=$sample->[2]/$sample->[0]; # recall
}
else {
$tmpR=0;
}
if($sample->[1]>0) {
$tmpP=$sample->[2]/$sample->[1]; # precision
}
else {
$tmpP=0;
}
if((1-$alpha)*$tmpP+$alpha*$tmpR>0) {
$tmpF=($tmpR*$tmpP)/((1-$alpha)*$tmpP+$alpha*$tmpR); # f-measure
}
else {
$tmpF=0;
}
$sample->[0]=$tmpR;
$sample->[1]=$tmpP;
$sample->[2]=$tmpF;
}
else {
# $opt_t!=1 => output raw model token count, peer token count, and hit count
# do nothing, just return $sample
}
}
return $sample;
}
sub by_value {
$a<=>$b;
}
sub printPerEvalData {
my $ROUGEScores=shift;
my $tag=shift; # tag to identify each evaluation
my (@instances,$i,$j);
@instances=sort by_evalID (keys %$ROUGEScores);
foreach $i (@instances) {
# print average per evaluation score
print "$tag $i R:$ROUGEScores->{$i}[0] P:$ROUGEScores->{$i}[1] F:$ROUGEScores->{$i}[2]\n";
}
}
sub by_evalID {
my ($a1,$b1);
if($a=~/^([0-9]+)/o) {
$a1=$1;
}
if($b=~/^([0-9]+)/o) {
$b1=$1;
}
if(defined($a1)&&defined($b1)) {
return $a1<=>$b1;
}
else {
return $a cmp $b;
}
}
sub computeAverages {
my $ROUGEScores=shift;
my $ROUGEAverages=shift;
my $opt_t=shift;
my ($avgAvgROUGE_R,$resampleAvgROUGE_R);
my ($avgAvgROUGE_P,$resampleAvgROUGE_P);
my ($avgAvgROUGE_F,$resampleAvgROUGE_F);
my ($ciU,$ciL);
my (@instances,$i,$j,@rankedArray_R,@rankedArray_P,@RankedArray_F);
@instances=sort (keys %$ROUGEScores);
$avgAvgROUGE_R=0;
$avgAvgROUGE_P=0;
$avgAvgROUGE_F=0;
$resampleAvgROUGE_R=0;
$resampleAvgROUGE_P=0;
$resampleAvgROUGE_F=0;
# compute totals
foreach $i (@instances) {
$avgAvgROUGE_R+=$ROUGEScores->{$i}[0]; # recall ; or model token count
$avgAvgROUGE_P+=$ROUGEScores->{$i}[1]; # precision ; or peer token count
$avgAvgROUGE_F+=$ROUGEScores->{$i}[2]; # f1-measure ; or match token count (hit)
}
# compute averages
unless(defined($opt_t)) {
# per sentence average
if((scalar @instances)>0) {
$avgAvgROUGE_R=sprintf("%7.5f",$avgAvgROUGE_R/(scalar @instances));
$avgAvgROUGE_P=sprintf("%7.5f",$avgAvgROUGE_P/(scalar @instances));
$avgAvgROUGE_F=sprintf("%7.5f",$avgAvgROUGE_F/(scalar @instances));
}
else {
$avgAvgROUGE_R=sprintf("%7.5f",0);
$avgAvgROUGE_P=sprintf("%7.5f",0);
$avgAvgROUGE_F=sprintf("%7.5f",0);
}
}
else {
if($opt_t==1) {
# per token average on corpus level
my ($tmpR,$tmpP,$tmpF);
if($avgAvgROUGE_R>0) {
$tmpR=$avgAvgROUGE_F/$avgAvgROUGE_R;
}
else {
$tmpR=0;
}
if($avgAvgROUGE_P>0) {
$tmpP=$avgAvgROUGE_F/$avgAvgROUGE_P;
}
else {
$tmpP=0;
}
if((1-$alpha)*$tmpP+$alpha*$tmpR>0) {
$tmpF=($tmpR+$tmpP)/((1-$alpha)*$tmpP+$alpha*$tmpR);
}
else {
$tmpF=0;
}
$avgAvgROUGE_R=sprintf("%7.5f",$tmpR);
$avgAvgROUGE_P=sprintf("%7.5f",$tmpP);
$avgAvgROUGE_F=sprintf("%7.5f",$tmpF);
}
}
if(!defined($opt_t)||$opt_t==1) {
# compute confidence intervals using bootstrap resampling
@ResamplingArray=();
for($i=0;$i<$numOfResamples;$i++) {
my $sample;
$sample=&bootstrapResampling($ROUGEScores,\@instances,$i,$opt_t);
# sample contains average sum of the sample
if(@ResamplingArray==0) {
# setup the resampling array for Avg
my $s;
$s=[];
push(@$s,$sample->[0]);
push(@ResamplingArray,$s);
$s=[];
push(@$s,$sample->[1]);
push(@ResamplingArray,$s);
$s=[];
push(@$s,$sample->[2]);
push(@ResamplingArray,$s);
}
else {
$rsa=$ResamplingArray[0];
push(@{$rsa},$sample->[0]);
$rsa=$ResamplingArray[1];
push(@{$rsa},$sample->[1]);
$rsa=$ResamplingArray[2];
push(@{$rsa},$sample->[2]);
}
}
# sort resampling results
{
# recall
@rankedArray_R=sort by_value (@{$ResamplingArray[0]});
$ResamplingArray[0]=\@rankedArray_R;
for($x=0;$x<=$#rankedArray_R;$x++) {
$resampleAvgROUGE_R+=$rankedArray_R[$x];
# print "*R ($x): $rankedArray_R[$x]\n";
}
$resampleAvgROUGE_R=sprintf("%7.5f",$resampleAvgROUGE_R/(scalar @rankedArray_R));
# precision
@rankedArray_P=sort by_value (@{$ResamplingArray[1]});
$ResamplingArray[1]=\@rankedArray_P;
for($x=0;$x<=$#rankedArray_P;$x++) {
$resampleAvgROUGE_P+=$rankedArray_P[$x];
# print "*P ($x): $rankedArray_P[$x]\n";
}
$resampleAvgROUGE_P=sprintf("%7.5f",$resampleAvgROUGE_P/(scalar @rankedArray_P));
# f1-measure
@rankedArray_F=sort by_value (@{$ResamplingArray[2]});
$ResamplingArray[2]=\@rankedArray_F;
for($x=0;$x<=$#rankedArray_F;$x++) {
$resampleAvgROUGE_F+=$rankedArray_F[$x];
# print "*F ($x): $rankedArray_F[$x]\n";
}
$resampleAvgROUGE_F=sprintf("%7.5f",$resampleAvgROUGE_F/(scalar @rankedArray_F));
}
# $ciU=999-int((100-$opt_c)*10/2); # upper bound index
# $ciL=int((100-$opt_c)*10/2); # lower bound index
$delta=$numOfResamples*((100-$opt_c)/2.0)/100.0;
$ciUa=int($numOfResamples-$delta-1); # upper confidence interval lower index
$ciUb=$ciUa+1; # upper confidence interval upper index
$ciLa=int($delta); # lower confidence interval lower index
$ciLb=$ciLa+1; # lower confidence interval upper index
$ciR=$numOfResamples-$delta-1-$ciUa; # ratio bewteen lower and upper indexes
# $ROUGEAverages->{"AvgR"}=$avgAvgROUGE_R;
#-------
# recall
$ROUGEAverages->{"AvgR"}=$resampleAvgROUGE_R;
# find condifence intervals; take maximum distance from the mean
$ROUGEAverages->{"CIAvgL_R"}=sprintf("%7.5f",$ResamplingArray[0][$ciLa]+
($ResamplingArray[0][$ciLb]-$ResamplingArray[0][$ciLa])*$ciR);
$ROUGEAverages->{"CIAvgU_R"}=sprintf("%7.5f",$ResamplingArray[0][$ciUa]+
($ResamplingArray[0][$ciUb]-$ResamplingArray[0][$ciUa])*$ciR);
#-------
# precision
$ROUGEAverages->{"AvgP"}=$resampleAvgROUGE_P;
# find condifence intervals; take maximum distance from the mean
$ROUGEAverages->{"CIAvgL_P"}=sprintf("%7.5f",$ResamplingArray[1][$ciLa]+
($ResamplingArray[1][$ciLb]-$ResamplingArray[1][$ciLa])*$ciR);
$ROUGEAverages->{"CIAvgU_P"}=sprintf("%7.5f",$ResamplingArray[1][$ciUa]+
($ResamplingArray[1][$ciUb]-$ResamplingArray[1][$ciUa])*$ciR);
#-------
# f1-measure
$ROUGEAverages->{"AvgF"}=$resampleAvgROUGE_F;
# find condifence intervals; take maximum distance from the mean
$ROUGEAverages->{"CIAvgL_F"}=sprintf("%7.5f",$ResamplingArray[2][$ciLa]+
($ResamplingArray[2][$ciLb]-$ResamplingArray[2][$ciLa])*$ciR);
$ROUGEAverages->{"CIAvgU_F"}=sprintf("%7.5f",$ResamplingArray[2][$ciUa]+
($ResamplingArray[2][$ciUb]-$ResamplingArray[2][$ciUa])*$ciR);
$ROUGEAverages->{"M_cnt"}=$avgAvgROUGE_R; # model token count
$ROUGEAverages->{"P_cnt"}=$avgAvgROUGE_P; # peer token count
$ROUGEAverages->{"H_cnt"}=$avgAvgROUGE_F; # hit token count
}
else {
# $opt_t==2 => output raw count instead of precision, recall, and f-measure values
# in this option, no resampling is necessary, just output the raw counts
$ROUGEAverages->{"M_cnt"}=$avgAvgROUGE_R; # model token count
$ROUGEAverages->{"P_cnt"}=$avgAvgROUGE_P; # peer token count
$ROUGEAverages->{"H_cnt"}=$avgAvgROUGE_F; # hit token count
}
}
sub computeROUGEX {
my $metric=shift; # which ROUGE metric to compute?
my $ROUGEScores=shift;
my $evalID=shift;
my $ROUGEEval=shift; # one particular evaluation pair
my $peerID=shift; # a specific peer ID
my $ROUGEParam=shift; # ROUGE scoring parameters
my $lengthLimit; # lenght limit in words
my $byteLimit; # length limit in bytes
my $NSIZE; # ngram size for ROUGE-N
my $weightFactor; # weight factor for ROUGE-W
my $skipDistance; # skip distance for ROUGE-S
my $scoreMode; # scoring mode: A = model average; B = best model
my $alpha; # relative importance between recall and precision
my $opt_t; # ROUGE score counting mode
my $BEMode; # Basic Element scoring mode
my ($c,$cx,@modelPaths,$modelIDs,$modelRoot,$inputFormat);
$lengthLimit=$ROUGEParam->{"LENGTH"};
$byteLimit=$ROUGEParam->{"BYTE"};
$NSIZE=$ROUGEParam->{"NSIZE"};
$weightFactor=$ROUGEParam->{"WEIGHT"};
$skipDistance=$ROUGEParam->{"SD"};
$scoreMode=$ROUGEParam->{"SM"};
$alpha=$ROUGEParam->{"ALPHA"};
$opt_t=$ROUGEParam->{"AVERAGE"};
$BEMode=$ROUGEParam->{"BEMODE"};
# Check to see if this evaluation trial contains this $peerID.
# Sometimes not every peer provides response for each
# evaluation trial.
unless(exists($ROUGEEval->{"Ps"}{$peerID})) {
unless(exists($knownMissing{$evalID})) {
$knownMissing{$evalID}={};
}
unless(exists($knownMissing{$evalID}{$peerID})) {
print STDERR "\*ROUGE Warning: test instance for peer $peerID does not exist for evaluation $evalID\n";
$knownMissing{$evalID}{$peerID}=1;
}
return;
}
unless(defined($opt_z)) {
$peerPath=$ROUGEEval->{"PR"}."/".$ROUGEEval->{"Ps"}{$peerID};
}
else {
# if opt_z is set then peerPath is read from a file list that
# includes the path to the peer.
$peerPath=$ROUGEEval->{"Ps"}{$peerID};
}
if(defined($ROUGEEval->{"MR"})) {
$modelRoot=$ROUGEEval->{"MR"};
}
else {
# if opt_z is set then modelPath is read from a file list that
# includes the path to the model.
$modelRoot="";
}
$modelIDs=$ROUGEEval->{"MIDList"};
$inputFormat=$ROUGEEval->{"IF"};
# construct combined model
@modelPaths=(); # reset model paths
for($cx=0;$cx<=$#{$modelIDs};$cx++) {
my $modelID;
$modelID=$modelIDs->[$cx];
unless(defined($opt_z)) {
$modelPath="$modelRoot/$ROUGEEval->{\"Ms\"}{$modelID}"; # get full model path
}
else {
# if opt_z is set then modelPath is read from a file list that
# includes the full path to the model.
$modelPath="$ROUGEEval->{\"Ms\"}{$modelID}"; # get full model path
}
if(-e "$modelPath") {
# print "*$modelPath\n";
}
else {
die "Cannot find model summary: $modelPath\n";
}
push(@modelPaths,$modelPath);
}
#---------------------------------------------------------------
# evaluate peer
{
my (@results);
my ($testID,$avgROUGE,$avgROUGE_P,$avgROUGE_F);
@results=();
if($metric eq "N") {
&computeNGramScore(\@modelPaths,$peerPath,\@results,$NSIZE,$lengthLimit,$byteLimit,$inputFormat,$scoreMode,$alpha);
}
elsif($metric eq "L") {
&computeLCSScore(\@modelPaths,$peerPath,\@results,$lengthLimit,$byteLimit,$inputFormat,$scoreMode,$alpha);
}
elsif($metric eq "W") {
&computeWLCSScore(\@modelPaths,$peerPath,\@results,$lengthLimit,$byteLimit,$inputFormat,$weightFactor,$scoreMode,$alpha);
}
elsif($metric eq "S") {
&computeSkipBigramScore(\@modelPaths,$peerPath,\@results,$skipDistance,$lengthLimit,$byteLimit,$inputFormat,$scoreMode,$alpha);
}
elsif($metric eq "BE") {
&computeBEScore(\@modelPaths,$peerPath,\@results,$BEMode,$lengthLimit,$byteLimit,$inputFormat,$scoreMode,$alpha);
}
else {
die "Unknown ROUGE metric ID: $metric, has to be N, L, W, or S\n";
}
unless(defined($opt_t)) {
# sentence level average
$avgROUGE=sprintf("%7.5f",$results[2]);
$avgROUGE_P=sprintf("%7.5f",$results[4]);
$avgROUGE_F=sprintf("%7.5f",$results[5]);
}
else {
# corpus level per token average
$avgROUGE=$results[0]; # total model token count
$avgROUGE_P=$results[3]; # total peer token count