-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval09.pl
2689 lines (2248 loc) · 77.2 KB
/
eval09.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 -w
# $Id: eval09.pl,v 0.4 2009/03/01 stepanek$
# Based on eval08.pl,v 1.8
# Author: Yuval Krymolowski
# Addition of precision and recall
# and of frame confusion list: Sabine Buchholz
# Addition of DEPREL + ATTACHMENT:
# Prokopis Prokopidis (prokopis at ilsp dot gr)
# Acknowledgements:
# to Markus Kuhn for suggesting the use of
# the Unicode category property
# Adaptation to CoNLL-07:
# Deniz Yuret (denizyuret at gmail dot com)
#
# Adaptation to CoNLL-08 (added scores for semantic frames):
# Mihai Surdeanu (mihai at surdeanu dot name)
# Acknowledgements:
# James Henderson for the "Exact match" scores
#
# Adaptation to CoNLL-09 ():
# Mihai Surdeanu (mihai at surdeanu dot name)
# Massi Ciaramita (massi at google dot com)
# Jan Stepanek (stepanek at ufal dot mff dot cuni dot cz)
# Pavel Stranak (stranak at ufal dot mff dot cuni dot cz)
# Marking changes between:
# Adapted to 2009 format (mm) <
# and
# Adapted to 2009 format (mm) >
if ($] < 5.008001)
{
print STDERR <<EOM
This script requires PERL 5.8.1 for running.
This version is needed for proper handling
of Unicode characters.
Please obtain a new version or contact the shared task team
if you are unable to upgrade PERL.
EOM
;
exit(1) ;
}
use strict ;
use warnings;
use Getopt::Std ;
use open qw/:utf8 :std/;
my ($usage) = <<EOT
CoNLL-09 evaluation script:
[perl] eval09.pl [OPTIONS] -g <gold standard> -s <system output>
This script evaluates a system output with respect to a gold standard.
Both files should be in UTF-8 encoded CoNLL-09 tabular format.
The output breaks down the errors according to their type and context.
Optional parameters:
-o FILE : output: print output to FILE (default is standard output)
-q : quiet: only print overall performance, without the details
-b : evalb: produce output in a format similar to evalb
(http://nlp.cs.nyu.edu/evalb/); use together with -q
-p : punctuation: do not score punctuation (default is to score)
-v : version: show the version number
-h : help: print this help text and exit
EOT
;
my ($line_num) ;
my ($sep) = '0x01' ;
my ($START) = '.S' ;
my ($END) = '.E' ;
my ($con_err_num) = 3 ;
my ($freq_err_num) = 10 ;
my ($spec_err_loc_con) = 8 ;
our ($opt_g, $opt_s, $opt_o, $opt_h, $opt_v, $opt_q, $opt_p, $opt_b, $opt_d) ;
my ($word_mismatch_warning);
my $skipped_su_preds_gold = 0;
my $skipped_su_args_gold = 0;
my $skipped_su_preds_sys = 0;
my $skipped_su_args_sys = 0;
################################################################################
### subfunctions ###
################################################################################
# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
# Whether a string consists entirely of characters with the Unicode
# category property "Punctuation" (see "man perlunicode")
sub is_uni_punct
{
my ($word) = @_ ;
return scalar($word =~ /^\p{Punctuation}+$/) ;
}
# The length of a unicode string, excluding non-spacing marks
# (for example vowel marks in Arabic)
sub uni_len
{
my ($word) = @_ ;
my ($ch, $l) ;
$l = 0 ;
foreach $ch (split(//, $word))
{
if ($ch !~ /^\p{NonspacingMark}/)
{
$l++ ;
}
}
return $l ;
}
sub filter_context_counts
{ # filter_context_counts
my ($vec, $num, $max_len) = @_ ;
my ($con, $l, $thresh) ;
$thresh = (sort {$b <=> $a} values %{$vec})[$num-1] ;
foreach $con (keys %{$vec})
{
if (${$vec}{$con} < $thresh)
{
delete ${$vec}{$con} ;
next ;
}
$l = uni_len($con) ;
if ($l > ${$max_len})
{
${$max_len} = $l ;
}
}
} # filter_context_counts
sub print_context
{ # print_context
my ($counts, $counts_pos, $max_con_len, $max_con_pos_len) = @_ ;
my (@v_con, @v_con_pos, $con, $con_pos, $i, $n) ;
printf OUT " %-*s | %-4s | %-4s | %-4s | %-4s", $max_con_pos_len, 'PPOSS', 'any', 'head', 'dep', 'both' ;
print OUT " ||" ;
printf OUT " %-*s | %-4s | %-4s | %-4s | %-4s", $max_con_len, 'word', 'any', 'head', 'dep', 'both' ;
print OUT "\n" ;
printf OUT " %s-+------+------+------+-----", '-' x $max_con_pos_len;
print OUT "--++" ;
printf OUT "--%s-+------+------+------+-----", '-' x $max_con_len;
print OUT "\n" ;
@v_con = sort {${$counts}{tot}{$b} <=> ${$counts}{tot}{$a}} keys %{${$counts}{tot}} ;
@v_con_pos = sort {${$counts_pos}{tot}{$b} <=> ${$counts_pos}{tot}{$a}} keys %{${$counts_pos}{tot}} ;
$n = scalar @v_con ;
if (scalar @v_con_pos > $n)
{
$n = scalar @v_con_pos ;
}
foreach $i (0 .. $n-1)
{
if (defined $v_con_pos[$i])
{
$con_pos = $v_con_pos[$i] ;
printf OUT " %-*s | %4d | %4d | %4d | %4d",
$max_con_pos_len, $con_pos, ${$counts_pos}{tot}{$con_pos},
${$counts_pos}{err_head}{$con_pos}, ${$counts_pos}{err_dep}{$con_pos},
${$counts_pos}{err_dep}{$con_pos}+${$counts_pos}{err_head}{$con_pos}-${$counts_pos}{tot}{$con_pos} ;
}
else
{
printf OUT " %-*s | %4s | %4s | %4s | %4s",
$max_con_pos_len, ' ', ' ', ' ', ' ', ' ' ;
}
print OUT " ||" ;
if (defined $v_con[$i])
{
$con = $v_con[$i] ;
printf OUT " %-*s | %4d | %4d | %4d | %4d",
$max_con_len+length($con)-uni_len($con), $con, ${$counts}{tot}{$con},
${$counts}{err_head}{$con}, ${$counts}{err_dep}{$con},
${$counts}{err_dep}{$con}+${$counts}{err_head}{$con}-${$counts}{tot}{$con} ;
}
else
{
printf OUT " %-*s | %4s | %4s | %4s | %4s",
$max_con_len, ' ', ' ', ' ', ' ', ' ' ;
}
print OUT "\n" ;
}
printf OUT " %s-+------+------+------+-----", '-' x $max_con_pos_len;
print OUT "--++" ;
printf OUT "--%s-+------+------+------+-----", '-' x $max_con_len;
print OUT "\n" ;
print OUT "\n\n" ;
} # print_context
sub num_as_word
{
my ($num) = @_ ;
$num = abs($num) ;
return $num == 1 ? 'one word' :
$num == 2 ? 'two words' :
$num == 3 ? 'three words' :
$num == 4 ? 'four words' :
"$num words";
}
sub describe_err
{ # describe_err
my ($head_err, $head_aft_bef, $dep_err) = @_ ;
my ($dep_g, $dep_s, $desc) ;
my ($head_aft_bef_g, $head_aft_bef_s) = split(//, $head_aft_bef) ;
if ($head_err eq '-')
{
$desc = 'correct head' ;
$desc .= ($head_aft_bef_s eq '0') ? ' (0)' :
($head_aft_bef_s eq 'e') ? ' (the focus word)' :
($head_aft_bef_s eq 'a') ? ' (after focus word)' :
($head_aft_bef_s eq 'b') ? ' (before focus word)' : '' ;
}
elsif ($head_aft_bef_s eq '0')
{
$desc = 'head = 0 instead of ' ;
$desc.= ($head_aft_bef_g eq 'a') ? 'after ' :
($head_aft_bef_g eq 'b') ? 'before ' : '' ;
$desc .= 'the focus word' ;
}
elsif ($head_aft_bef_g eq '0')
{
$desc = 'head is ' ;
$desc .= ($head_aft_bef_g eq 'a') ? 'after ' :
($head_aft_bef_g eq 'b') ? 'before ' : '' ;
$desc .= 'the focus word instead of 0' ;
}
else
{
$desc = num_as_word($head_err) ;
$desc .= ($head_err < 0) ? ' before' : ' after';
$desc = 'head '.$desc.' the correct head ' ;
$desc.= ($head_aft_bef_s eq '0') ? '(0' :
($head_aft_bef_s eq 'e') ? '(the focus word' :
($head_aft_bef_s eq 'a') ? '(after the focus word' :
($head_aft_bef_s eq 'b') ? '(before the focus word' : '' ;
if ($head_aft_bef_g ne $head_aft_bef_s)
{
$desc .= ' instead of' ;
$desc .= ($head_aft_bef_s eq '0') ? '0' :
($head_aft_bef_s eq 'e') ? 'the focus word' :
($head_aft_bef_s eq 'a') ? 'after the focus word' :
($head_aft_bef_s eq 'b') ? 'before the focus word' : '' ;
}
$desc .= ')' ;
}
$desc .= ', ' ;
if ($dep_err eq '-')
{
$desc .= 'correct dependency' ;
}
else
{
($dep_g, $dep_s) = ($dep_err =~ /^(.*)->(.*)$/) ;
$desc .= qq(dependency "$dep_s" instead of "$dep_g") ;
}
return($desc) ;
} # describe_err
sub get_context
{ # get_context
my ($sent, $i_w) = @_ ;
my ($w_2, $w_1, $w1, $w2) ;
my ($p_2, $p_1, $p1, $p2) ;
if ($i_w >= 2)
{
$w_2 = ${${$sent}[$i_w-2]}{word} ;
$p_2 = ${${$sent}[$i_w-2]}{pos} ;
}
else
{
$w_2 = $START ;
$p_2 = $START ;
}
if ($i_w >= 1)
{
$w_1 = ${${$sent}[$i_w-1]}{word} ;
$p_1 = ${${$sent}[$i_w-1]}{pos} ;
}
else
{
$w_1 = $START ;
$p_1 = $START ;
}
if ($i_w <= scalar @{$sent}-2)
{
$w1 = ${${$sent}[$i_w+1]}{word} ;
$p1 = ${${$sent}[$i_w+1]}{pos} ;
}
else
{
$w1 = $END ;
$p1 = $END ;
}
if ($i_w <= scalar @{$sent}-3)
{
$w2 = ${${$sent}[$i_w+2]}{word} ;
$p2 = ${${$sent}[$i_w+2]}{pos} ;
}
else
{
$w2 = $END ;
$p2 = $END ;
}
return ($w_2, $w_1, $w1, $w2, $p_2, $p_1, $p1, $p2) ;
} # get_context
sub uniq {
my %uniq;
@uniq{@_} = ();
return keys %uniq;
} # uniq
#
# Parses the SRL frames from an input sentence
#
sub parse_frames
{
my $is_gold = shift;
my ($frame_lines) = @_;
#print @{$frame_lines};
# this will store all the SRL props in this sentence
my $sent = SRL::sentence->new();
#
# discover predicates first
#
my $pred_count = 0;
my $prev_line_token_count = 0;
for(my $i = 0; $i < $#{$frame_lines}; $i ++){
my $line = ${$frame_lines}[$i];
my @tokens = split(/\s+/, trim($line));
# sanity check: make sure the number of tokens does not change from line to line in a sentence
if($i > 0 and $#tokens != $prev_line_token_count){
die "Invalid number of tokens in line: [$line]\n";
}
$prev_line_token_count = $#tokens;
if($#tokens > 0){ # weak sanity check: make sure there are > 0 tokens
# print "*$line*\n";
#for(my $k = 0; $k <= $#tokens; $k ++){
# print " *$tokens[$k]*";
#}
#print "\n";
# Adapted to 2009 format (mm) <
# predicates are stored at column 13!
my $lemma_sense = $tokens[13];
my $pred_pposs = $tokens[5];
if($lemma_sense ne "_" and $lemma_sense ne "-"){ # 2009 ch data uses dash instead of underscore
# arguments are stored starting at column 14
my $prop = SRL::prop->new($lemma_sense, $pred_pposs, $i, $pred_count + 14);
# Adapted to 2009 format (mm) >
#printf STDERR "%s %d %d\n", $prop->lemma(), $prop->sense(), $prop->column();
# 2009: removed check for SU predicates. They are not present in 2009 data (mihai)
$sent->add_props($prop);
$pred_count ++;
}
} else {
die "Invalid data line: [$line]!\n";
}
}
#
# read the args for each prop
#
foreach my $prop ($sent->props()){
#printf STDERR "%s %d %d\n", $prop->lemma(), $prop->sense(), $prop->column();
for(my $i = 0; $i < $#{$frame_lines}; $i ++){
my $line = ${$frame_lines}[$i];
my @tokens = split(/\s+/, trim($line));
my $tmpc = $prop->column();
#print "COLUMN $tmpc: ";
#for(my $k = 0; $k <= $#tokens; $k ++){
# print " *$tokens[$k]*";
#}
#print "\n";
my $label = $tokens[$prop->column()];
if($label ne "_" and $label ne '-'){ # 2009 ch data uses dash instead of underscore
foreach my $splitlabel (uniq(sort(split /\|/,$label))){
my $arg = SRL::arg->new($splitlabel, $i);
die "$splitlabel at $line\n" unless ref $arg;
#printf STDERR "\tArg %s %d\n", $arg->label(), $arg->position();
$prop->add_args($arg);
}
}
}
#$prop->display();
}
return ($sent);
} # parse_frames
#
# Verifies if two propositions are equal,
# i.e., their full argument sets must be identical
#
sub same_prop
{
my ($gold_prop, $sys_prop) = @_;
my $found = 0;
# must have the same number of args
if($gold_prop->arg_count() != $sys_prop->arg_count()){
return 0;
}
# each gold arg must be matched in sys
foreach my $gold_arg ($gold_prop->args()) {
$found = 0;
foreach my $sys_arg ($sys_prop->args()){
if($gold_arg->label() eq $sys_arg->label() and
$gold_arg->position() == $sys_arg->position()){
$found = 1;
last;
}
}
return 0 unless $found;
}
return 1;
}
# checks if the senses of two semantic propositions are the same
sub same_sense
{
my ($gold_sense, $sys_sense) = @_;
# if senses are numbers => use number comparison
if($gold_sense =~ /^[0-9]+$/ and $sys_sense =~ /^[0-9]+$/){
#print "COMPARING AS NUMBERS $gold_sense vs $sys_sense\n";
$gold_sense =~ s/^0+(.)/$1/ ;
$sys_sense =~ s/^0+(.)/$1/ ;
if($gold_sense == $sys_sense){
return 1;
} else {
return 0;
}
}
# compare as strings
#print "COMPARE AS STRINGS $gold_sense vs $sys_sense\n";
if($gold_sense eq $sys_sense){
return 1;
}
return 0;
}
#
# Updates the SRL score counts for one sentence
#
sub update_srl_scores
{
my ($gold_sent, $sys_sent, $counts) = @_;
my %sent_counts = (
tot_prop => 0,
pred_prop => 0,
corl_prop => 0,
tot_arg => 0,
pred_arg => 0,
corl_arg => 0
);
#printf STDERR "GOLD SRL: ";
#$gold_sent->display();
#printf STDERR "\nSYS SRL: ";
#$sys_sent->display();
#
# compute P/R for predicting predicates and predicate senses
# we model these as dependencies to a virtual ROOT node;
# the labels of these dependencies are the predicate senses
#
$counts->{tot_prop} += $gold_sent->prop_count();
$sent_counts{tot_prop} += $gold_sent->prop_count();
$counts->{pred_prop} += $sys_sent->prop_count();
$sent_counts{pred_prop} += $sys_sent->prop_count();
foreach my $gold_prop ($gold_sent->props()) {
$counts->{tot_prop_per_tag}{$gold_prop->pposs()} ++;
$counts->{prop_per_tag}{$gold_prop->pposs()} = 1;
}
foreach my $sys_prop ($sys_sent->props()){
$counts->{pred_prop_per_tag}{$sys_prop->pposs()} ++;
$counts->{prop_per_tag}{$sys_prop->pposs()} = 1;
}
foreach my $gold_prop ($gold_sent->props()) {
foreach my $sys_prop ($sys_sent->props()){
if($gold_prop->position() == $sys_prop->position()){
$counts->{coru_prop} ++;
$counts->{coru_prop_per_tag}{$gold_prop->pposs()} ++;
if(same_sense($gold_prop->sense(), $sys_prop->sense())){
$counts->{corl_prop} ++;
$sent_counts{corl_prop} ++;
$counts->{corl_prop_per_tag}{$gold_prop->pposs()} ++;
if(same_prop($gold_prop, $sys_prop)){
$counts->{full_corl_prop} ++;
}
}
last if $gold_prop->position() == $sys_prop->position();
}
}
}
#
# compute P/R for semantic dependencies, both labeled and unlabeled
# An unlabeled dependency is considered correct if
# the token positions for predicate and argument are correct
# For a labeled dependency to be correct,
# the argument label must also be correct
# Note: the predicate sense is scored in the block above,
# in the dependencies to the virtual ROOT node
#
$counts->{tot_arg} += $gold_sent->arg_count();
$sent_counts{tot_arg} += $gold_sent->arg_count();
$counts->{pred_arg} += $sys_sent->arg_count();
$sent_counts{pred_arg} += $sys_sent->arg_count();
foreach my $gold_prop ($gold_sent->props()) {
foreach my $gold_arg ($gold_prop->args()){
# Adapted to 2009 format (mm) <
# The substr(0, 2) from PPOSS was English specific. Removed, we now use the complete PPOSS for stats
# my $label = substr($gold_prop->pposs(), 0, 2) . "* + " . $gold_arg->label();
my $label = $gold_prop->pposs() . " + " . $gold_arg->label();
# Adapted to 2009 format (mm) >
$counts->{tot_arg_per_tag}{$label} ++;
$counts->{arg_per_tag}{$label} = 1;
}
}
foreach my $sys_prop ($sys_sent->props()){
foreach my $sys_arg ($sys_prop->args()){
# Adapted to 2009 format (mm) <
# The substr(0, 2) from PPOSS was English specific. Removed, we now use the complete PPOSS for stats
# my $label = substr($sys_prop->pposs(), 0, 2) . "* + " . $sys_arg->label();
my $label = $sys_prop->pposs() . " + " . $sys_arg->label();
# Adapted to 2009 format (mm) >
$counts->{pred_arg_per_tag}{$label} ++;
$counts->{arg_per_tag}{$label} = 1;
}
}
foreach my $gold_prop ($gold_sent->props()) {
foreach my $sys_prop ($sys_sent->props()){
# found two identical predicate positions
if($gold_prop->position() == $sys_prop->position()){
# scan all the arguments for the gold prop
my %coru_arg_checked;
foreach my $gold_arg ($gold_prop->args()){
foreach my $sys_arg ($sys_prop->args()){
# found a correct arg for unlabeled scoring
if($gold_arg->position() == $sys_arg->position()){
# Adapted to 2009 format (mm) <
# The substr(0, 2) from PPOSS was English specific. Removed, we now use the complete PPOSS for stats
# my $label = substr($gold_prop->pposs(), 0, 2) . "* + " . $gold_arg->label();
my $label = $gold_prop->pposs() . " + " . $gold_arg->label();
# Adapted to 2009 format (mm) >
if(not $coru_arg_checked{$gold_arg}){
if(not $coru_arg_checked{$sys_arg}){
$coru_arg_checked{$gold_arg} = 1;
$coru_arg_checked{$sys_arg} = 1;
$counts->{coru_arg} ++ ;
$counts->{coru_arg_per_tag}{$label} ++;
}
}
# found a correct arg for labeled scoring
#my $tmpg = $gold_arg->label();
#my $tmps = $sys_arg->label();
#print "COMPARE ARGS ** WITH **\n";
if($gold_arg->label() eq $sys_arg->label()){
$counts->{corl_arg} ++;
$sent_counts{corl_arg} ++;
$counts->{corl_arg_per_tag}{$label} ++;
}
}
# deleted: for multiple functions, all possible combinations must be checked
#last if $gold_arg->position() == $sys_arg->position();
}
}
}
}
}
$counts->{tot_sent} ++;
if ($sent_counts{tot_prop} == $sent_counts{corl_prop} &&
$sent_counts{pred_prop} == $sent_counts{corl_prop} &&
$sent_counts{tot_arg} == $sent_counts{corl_arg} &&
$sent_counts{pred_arg} == $sent_counts{corl_arg}) {
$counts->{cor_sent} ++;
}
#printf STDERR "%d %d %d %d\n", $counts->{coru_arg}, $counts->{corl_arg}, $counts->{tot_arg}, $counts->{pred_arg};
} # end update_srl_scores
sub read_sent
{ # read_sent
my ($sent_gold, $sent_sys) = @_ ;
my ($line_g, $line_s, $new_sent) ;
my (%fields_g, %fields_s) ;
my ($gold_lines, $sys_lines); # all the lines for the current sentence
my ($gold_srl_sent, $sys_srl_sent); # the SRL::sentence objects for this sentence
$new_sent = 1 ;
@{$sent_gold} = () ;
@{$sent_sys} = () ;
@{$gold_lines} = ();
@{$sys_lines} = ();
while (1)
{ # main reading loop
$line_g = <GOLD> ;
$line_s = <SYS> ;
if(defined $line_g) {
push @{$gold_lines}, $line_g;
}
if(defined $line_s) {
push @{$sys_lines}, $line_s;
}
$line_num++ ;
# system output has fewer lines than gold standard
if ((defined $line_g) && (! defined $line_s))
{
if ($line_g =~ /^\s*$/) {
warn "Warning: ignoring missing blank line at the end of $opt_s.\n";
next;
}
print STDERR "Fatal: line mismatch, line $line_num:\n" ;
print STDERR " gold: $line_g sys : past end of file\n" ;
exit(1) ;
}
# system output has more lines than gold standard
if ((! defined $line_g) && (defined $line_s))
{
if ($line_s =~ /^\s*$/) {
warn "Warning: ignoring extra blank line at the end of $opt_s.\n";
next;
}
print STDERR "Fatal: line mismatch, line $line_num:\n" ;
print STDERR " gold: past end of file\n" ;
print STDERR " sys : $line_s\n" ;
exit(1) ;
}
# end of file reached for both
if ((! defined $line_g) && (! defined $line_s))
{
$gold_srl_sent = parse_frames(1, $gold_lines);
$sys_srl_sent = parse_frames(0, $sys_lines);
return (1, $gold_srl_sent, $sys_srl_sent) ;
}
# one contains end of sentence but other one does not
if (($line_g =~ /^\s+$/) != ($line_s =~ /^\s+$/))
{
print STDERR "Fatal: line mismatch, line $line_num:\n" ;
print STDERR " gold: $line_g" ;
print STDERR " sys : $line_s\n" ;
exit(1) ;
}
# end of sentence reached
if ($line_g =~ /^\s+$/)
{
$gold_srl_sent = parse_frames(1, $gold_lines);
$sys_srl_sent = parse_frames(0, $sys_lines);
return(0, $gold_srl_sent, $sys_srl_sent) ;
}
# now both lines contain information
if ($new_sent)
{
$new_sent = 0 ;
}
# Adapted to 2009 format (mm) <
# 'official' column names (2009)
# options.output = ['id','form','lemma','plemma','pos', 'ppos'
# 'feat', 'pfeat', 'head', 'phead', 'deprel'
# 'pdeprel', 'fillpred', 'pred', 'apred'+]
@fields_g{'word', 'pos', 'head', 'dep'} = (split (/\s+/, trim($line_g)))[1, 5, 8, 10] ;
# Using ppos instead of pos to be consistent with 2008 st where pposs was used
push @{$sent_gold}, { %fields_g } ;
@fields_s{'word', 'pos', 'head', 'dep'} = (split (/\s+/, trim($line_s)))[1, 5, 8, 10] ;
# Adapted to 2009 format (mm) >
# Some teams like to change the word or the pos in the answer file...
# So do not make this fatal and only give one warning.
if ((not defined $word_mismatch_warning) &&
(($fields_g{word} ne $fields_s{word}) ||
($fields_g{pos} ne $fields_s{pos})))
{
$word_mismatch_warning = 1;
print STDERR "Warning: ignoring word/pos mismatch, line $line_num:\n" ;
print STDERR " gold: $line_g sys : $line_s\n" ;
# exit(1) ;
}
push @{$sent_sys}, { %fields_s } ;
} # main reading loop
} # read_sent
################################################################################
### main ###
################################################################################
my ($sent_num, $eof, $word_num, @err_sent) ;
my (@sent_gold, @sent_sys, @starts) ;
my ($gold_srl_sent, $sys_srl_sent);
my ($word, $pos, $wp, $head_g, $dep_g, $head_s, $dep_s) ;
my (%counts, $err_head, $err_dep, $con, $con1, $con_pos, $con_pos1, $thresh) ;
my ($head_err, $dep_err, @cur_err, %err_counts, $err_counter, $err_desc) ;
my ($loc_con, %loc_con_err_counts, %err_desc) ;
my ($head_aft_bef_g, $head_aft_bef_s, $head_aft_bef) ;
my ($con_bef, $con_aft, $con_bef_2, $con_aft_2, @bits, @e_bits, @v_con, @v_con_pos) ;
my ($con_pos_bef, $con_pos_aft, $con_pos_bef_2, $con_pos_aft_2) ;
my ($max_word_len, $max_pos_len, $max_con_len, $max_con_pos_len) ;
my ($max_word_spec_len, $max_con_bef_len, $max_con_aft_len) ;
my (%freq_err, $err) ;
my %srl_counts; # counts for SRL scoring
my ($i, $j, $i_w, $l, $n_args) ;
my ($w_2, $w_1, $w1, $w2) ;
my ($wp_2, $wp_1, $wp1, $wp2) ;
my ($p_2, $p_1, $p1, $p2) ;
my ($short_output) ;
my ($score_on_punct, $score_on_deriv) ;
$counts{punct} = 0; # initialize
$counts{deriv} = 0;
getopts("g:o:s:qvhpbd") ;
if (defined $opt_v)
{
my $id = '$Id: eval09.pl,v 0.4 2009/03/01 01:04:00 stepanek Exp $';
my @parts = split ' ',$id;
print "Version $parts[2]\n";
exit(0);
}
if ((defined $opt_h) || ((! defined $opt_g) && (! defined $opt_s)))
{
die $usage ;
}
if (! defined $opt_g)
{
die "Gold standard file (-g) missing\n" ;
}
if (! defined $opt_s)
{
die "System output file (-s) missing\n" ;
}
if (! defined $opt_o)
{
$opt_o = '-' ;
}
if (defined $opt_q)
{
$short_output = 1 ;
} else {
$short_output = 0 ;
}
if (defined $opt_p)
{
$score_on_punct = 0 ;
} else {
$score_on_punct = 1 ;
}
#
# Removed. The DERIV links only exist in the Turkish dependency treebank
# and are therefore not relevant this year.
#
#if (defined $opt_d)
#{
# $score_on_deriv = 0 ;
#} else {
# $score_on_deriv = 1 ;
#}
$score_on_deriv = 1 ;
$line_num = 0 ;
$sent_num = 0 ;
$eof = 0 ;
@err_sent = () ;
@starts = () ;
%{$err_sent[0]} = () ;
$max_pos_len = length('PPOSS') ;
################################################################################
### reading input ###
################################################################################
# Adapted to 2009 format (mm) <
open (GOLD, "<:utf8", $opt_g) || die "Could not open gold standard file $opt_g\n" ;
open (SYS, "<:utf8", $opt_s) || die "Could not open system output file $opt_s\n" ;
open (OUT, ">$opt_o", ':utf8') || die "Could not open output file $opt_o\n" ;
# Adapted to 2009 format (mm) >
if (defined $opt_b) { # produce output similar to evalb
print OUT " Sent. Attachment Correct Scoring \n";
print OUT " ID Tokens - Unlab. Lab. HEAD HEAD+DEPREL tokens - - - -\n";
print OUT " ============================================================================\n";
}
%srl_counts = (
tot_prop => 0,
pred_prop => 0,
corl_prop => 0,
coru_prop => 0,
full_corl_prop => 0,
tot_sent => 0,
cor_sent => 0
);
while (! $eof)
{ # main reading loop
$starts[$sent_num] = $line_num+1 ;
($eof, $gold_srl_sent, $sys_srl_sent) = read_sent(\@sent_gold, \@sent_sys) ;
my $old_srl_cor_sent = $srl_counts{cor_sent};
update_srl_scores($gold_srl_sent, $sys_srl_sent, \%srl_counts);
my $srl_sent_cor = $srl_counts{cor_sent} - $old_srl_cor_sent;
$sent_num++ ;
%{$err_sent[$sent_num]} = () ;
$word_num = scalar @sent_gold ;
# bug fix by James Henderson:
# if the files has extra blank lines,
# then they were counted as correct sentences in the ExactMatch score.
# solution: skip empty sentences
if ($word_num == 0)
{
next;
}
# for accuracy per sentence
my %sent_counts = ( tot => 0,
err_any => 0,
err_head => 0
);
# printf "$sent_num $word_num\n" ;
my @frames_g = ('** '); # the initial frame for the virtual root
my @frames_s = ('** '); # the initial frame for the virtual root
foreach $i_w (0 .. $word_num-1)
{ # loop on words
push @frames_g, ''; # initialize
push @frames_s, ''; # initialize
}
foreach $i_w (0 .. $word_num-1)
{ # loop on words
($word, $pos, $head_g, $dep_g)
= @{$sent_gold[$i_w]}{'word', 'pos', 'head', 'dep'} ;
$wp = $word.' / '.$pos ;
# printf "%d: %s %s %s %s\n", $i_w, $word, $pos, $head_g, $dep_g ;
if ((! $score_on_punct) && is_uni_punct($word))
{
$counts{punct}++ ;
# ignore punctuations
next ;
}
if ((! $score_on_deriv) && ($dep_g eq 'DERIV'))
{
$counts{deriv}++ ;
# ignore deriv links
next ;
}
if (length($pos) > $max_pos_len)
{
$max_pos_len = length($pos) ;
}
($head_s, $dep_s) = @{$sent_sys[$i_w]}{'head', 'dep'} ;
$counts{tot}++ ;
$counts{word}{$wp}{tot}++ ;
$counts{pos}{$pos}{tot}++ ;
$counts{head}{$head_g-$i_w-1}{tot}++ ;
# for frame confusions
# add child to frame of parent
$frames_g[$head_g] .= "$dep_g ";
$frames_s[$head_s] .= "$dep_s ";
# add to frame of token itself
$frames_g[$i_w+1] .= "*$dep_g* "; # $i_w+1 because $i_w starts counting at zero
$frames_s[$i_w+1] .= "*$dep_g* ";
# for precision and recall of DEPREL
$counts{dep}{$dep_g}{tot}++ ; # counts for gold standard deprels
$counts{dep2}{$dep_g}{$dep_s}++ ; # counts for confusions
$counts{dep_s}{$dep_s}{tot}++ ; # counts for system deprels
$counts{all_dep}{$dep_g} = 1 ; # list of all deprels that occur ...
$counts{all_dep}{$dep_s} = 1 ; # ... in either gold or system output
# for precision and recall of HEAD direction
my $dir_g;
if ($head_g == 0) {
$dir_g = 'to_root';
} elsif ($head_g < $i_w+1) { # $i_w+1 because $i_w starts counting at zero
# also below
$dir_g = 'left';
} elsif ($head_g > $i_w+1) {
$dir_g = 'right';
} else {
# token links to itself; should never happen in correct gold standard
$dir_g = 'self';
}
my $dir_s;
if ($head_s == 0) {
$dir_s = 'to_root';
} elsif ($head_s < $i_w+1) {
$dir_s = 'left';