-
Notifications
You must be signed in to change notification settings - Fork 2
/
srl-eval.pl
1955 lines (1535 loc) · 42.7 KB
/
srl-eval.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
##################################################################
#
# srl-eval.pl : evaluation program for the CoNLL-2005 Shared Task
#
# Authors : Xavier Carreras and Lluis Marquez
# Contact : carreras@lsi.upc.edu
#
# Created : January 2004
# Modified:
# 2005/04/21 minor update; for perl-5.8 the table in LateX
# did not print correctly
# 2005/02/05 minor updates for CoNLL-2005
#
##################################################################
use strict;
############################################################
# A r g u m e n t s a n d H e l p
use Getopt::Long;
my %options;
GetOptions(\%options,
"latex", # latex output
"C", # confusion matrix
"noW"
);
my $script = "srl-eval.pl";
my $help = << "end_of_help;";
Usage: srl-eval.pl <gold props> <predicted props>
Options:
-latex Produce a results table in LaTeX
-C Produce a confusion matrix of gold vs. predicted argments, wrt. their role
end_of_help;
############################################################
# M A I N P R O G R A M
my $ns = 0; # number of sentence
my $ntargets = 0; # number of target verbs
my %E; # evaluation results
my %C; # confusion matrix
my %excluded = ( V => 1);
##
# open files
if (@ARGV != 2) {
print $help;
exit;
}
my $goldfile = shift @ARGV;
my $predfile = shift @ARGV;
if ($goldfile =~ /\.gz/) {
open GOLD, "gunzip -c $goldfile |" or die "$script: could not open gzipped file of gold props ($goldfile)! $!\n";
}
else {
open GOLD, $goldfile or die "$script: could not open file of gold props ($goldfile)! $!\n";
}
if ($predfile =~ /\.gz/) {
open PRED, "gunzip -c $predfile |" or die "$script: could not open gzipped file of predicted props ($predfile)! $!\n";
}
else {
open PRED, $predfile or die "$script: could not open file of predicted props ($predfile)! $!\n";
}
##
# read and evaluate propositions, sentence by sentence
my $s = SRL::sentence->read_props($ns, GOLD => \*GOLD, PRED => \*PRED);
while ($s) {
my $prop;
my (@G, @P, $i);
map { $G[$_->position] = $_ } $s->gold_props;
map { $P[$_->position] = $_ } $s->pred_props;
for($i=0; $i<@G; $i++) {
my $gprop = $G[$i];
my $pprop = $P[$i];
if ($pprop and !$gprop) {
!$options{noW} and print STDERR "WARNING : sentence $ns : verb ", $pprop->verb,
" at position ", $pprop->position, " : found predicted prop without its gold reference! Skipping prop!\n";
}
elsif ($gprop) {
if (!$pprop) {
!$options{noW} and print STDERR "WARNING : sentence $ns : verb ", $gprop->verb,
" at position ", $gprop->position, " : missing predicted prop! Counting all arguments as missed!\n";
$pprop = SRL::prop->new($gprop->verb, $gprop->position);
}
elsif ($gprop->verb ne $pprop->verb) {
!$options{noW} and print STDERR "WARNING : sentence $ns : props do not match : expecting ",
$gprop->verb, " at position ", $gprop->position,
", found ", $pprop->verb, " at position ", $pprop->position, "! Counting all gold arguments as missed!\n";
$pprop = SRL::prop->new($gprop->verb, $gprop->position);
}
$ntargets++;
my %e = evaluate_proposition($gprop, $pprop);
# Update global evaluation results
$E{ok} += $e{ok};
$E{op} += $e{op};
$E{ms} += $e{ms};
$E{ptv} += $e{ptv};
my $t;
foreach $t ( keys %{$e{T}} ) {
$E{T}{$t}{ok} += $e{T}{$t}{ok};
$E{T}{$t}{op} += $e{T}{$t}{op};
$E{T}{$t}{ms} += $e{T}{$t}{ms};
}
foreach $t ( keys %{$e{E}} ) {
$E{E}{$t}{ok} += $e{E}{$t}{ok};
$E{E}{$t}{op} += $e{E}{$t}{op};
$E{E}{$t}{ms} += $e{E}{$t}{ms};
}
if ($options{C}) {
update_confusion_matrix(\%C, $gprop, $pprop);
}
}
}
$ns++;
$s = SRL::sentence->read_props($ns, GOLD => \*GOLD, PRED => \*PRED);
}
# Print Evaluation results
my $t;
if ($options{latex}) {
print '\begin{table}[t]', "\n";
print '\centering', "\n";
print '\begin{tabular}{|l|r|r|r|}\cline{2-4}', "\n";
print '\multicolumn{1}{l|}{}', "\n";
print ' & Precision & Recall & F$_{\beta=1}$', '\\\\', "\n", '\hline', "\n"; #'
printf("%-10s & %6.2f\\%% & %6.2f\\%% & %6.2f\\\\\n", "Overall", precrecf1($E{ok}, $E{op}, $E{ms}));
print '\hline', "\n";
foreach $t ( sort keys %{$E{T}} ) {
printf("%-10s & %6.2f\\%% & %6.2f\\%% & %6.2f\\\\\n", $t, precrecf1($E{T}{$t}{ok}, $E{T}{$t}{op}, $E{T}{$t}{ms}));
}
print '\hline', "\n";
if (%excluded) {
print '\hline', "\n";
foreach $t ( sort keys %{$E{E}} ) {
printf("%-10s & %6.2f\\%% & %6.2f\\%% & %6.2f\\\\\n", $t, precrecf1($E{E}{$t}{ok}, $E{E}{$t}{op}, $E{E}{$t}{ms}));
}
print '\hline', "\n";
}
print '\end{tabular}', "\n";
print '\end{table}', "\n";
}
else {
printf("Number of Sentences : %6d\n", $ns);
printf("Number of Propositions : %6d\n", $ntargets);
printf("Percentage of perfect props : %6.2f\n",($ntargets>0 ? 100*$E{ptv}/$ntargets : 0));
print "\n";
printf("%10s %6s %6s %6s %6s %6s %6s\n", "", "corr.", "excess", "missed", "prec.", "rec.", "F1");
print "------------------------------------------------------------\n";
printf("%10s %6d %6d %6d %6.2f %6.2f %6.2f\n",
"Overall", $E{ok}, $E{op}, $E{ms}, precrecf1($E{ok}, $E{op}, $E{ms}));
# print "------------------------------------------------------------\n";
print "----------\n";
# printf("%10s %6d %6d %6d %6.2f %6.2f %6.2f\n",
# "all - {V}", $O2{ok}, $O2{op}, $O2{ms}, precrecf1($O2{ok}, $O2{op}, $O2{ms}));
# print "------------------------------------------------------------\n";
foreach $t ( sort keys %{$E{T}} ) {
printf("%10s %6d %6d %6d %6.2f %6.2f %6.2f\n",
$t, $E{T}{$t}{ok}, $E{T}{$t}{op}, $E{T}{$t}{ms}, precrecf1($E{T}{$t}{ok}, $E{T}{$t}{op}, $E{T}{$t}{ms}));
}
print "------------------------------------------------------------\n";
foreach $t ( sort keys %{$E{E}} ) {
printf("%10s %6d %6d %6d %6.2f %6.2f %6.2f\n",
$t, $E{E}{$t}{ok}, $E{E}{$t}{op}, $E{E}{$t}{ms}, precrecf1($E{E}{$t}{ok}, $E{E}{$t}{op}, $E{E}{$t}{ms}));
}
print "------------------------------------------------------------\n";
}
# print confusion matrix
if ($options{C}) {
my $k;
# Evaluation of Unlabelled arguments
my ($uok, $uop, $ums, $uacc) = (0,0,0,0);
foreach $k ( grep { $_ ne "-NONE-" && $_ ne "V" } keys %C ) {
map { $uok += $C{$k}{$_} } grep { $_ ne "-NONE-" && $_ ne "V" } keys %{$C{$k}};
$uacc += $C{$k}{$k};
$ums += $C{$k}{"-NONE-"};
}
map { $uop += $C{"-NONE-"}{$_} } grep { $_ ne "-NONE-" && $_ ne "V" } keys %{$C{"-NONE-"}};
print "--------------------------------------------------------------------\n";
printf("%10s %6s %6s %6s %6s %6s %6s %6s\n", "", "corr.", "excess", "missed", "prec.", "rec.", "F1", "lAcc");
printf("%10s %6d %6d %6d %6.2f %6.2f %6.2f %6.2f\n",
"Unlabeled", $uok, $uop, $ums, precrecf1($uok, $uop, $ums), 100*$uacc/$uok);
print "--------------------------------------------------------------------\n";
print "\n---- Confusion Matrix: (one row for each correct role, with the distribution of predictions)\n";
my %AllKeys;
map { $AllKeys{$_} = 1 } map { $_, keys %{$C{$_}} } keys %C;
my @AllKeys = sort keys %AllKeys;
my $i = -1;
print " ";
map { printf("%4d ", $i); $i++} @AllKeys;
print "\n";
$i = -1;
foreach $k ( @AllKeys ) {
printf("%2d: %-8s ", $i++, $k);
map { printf("%4d ", $C{$k}{$_}) } @AllKeys;
print "\n";
}
my ($t1,$t2);
foreach $t1 ( sort keys %C ) {
foreach $t2 ( sort keys %{$C{$t1}} ) {
# printf(" %-6s vs %-6s : %-5d\n", $t1, $t2, $C{$t1}{$t2});
}
}
}
# end of main program
#####################
############################################################
# S U B R O U T I N E S
# evaluates a predicted proposition wrt the gold correct proposition
# returns a hash with the following keys
# ok : number of correctly predicted args
# ms : number of missed args
# op : number of over-predicted args
# T : a hash indexed by argument types, where
# each value is in turn a hash of {ok,ms,op} numbers
# E : a hash indexed by excluded argument types, where
# each value is in turn a hash of {ok,ms,op} numbers
sub evaluate_proposition {
my ($gprop, $pprop) = @_;
my $o = $gprop->discriminate_args($pprop);
my %e;
my $a;
foreach $a (@{$o->{ok}}) {
if (!$excluded{$a->type}) {
$e{ok}++;
$e{T}{$a->type}{ok}++;
}
else {
$e{E}{$a->type}{ok}++;
}
}
foreach $a (@{$o->{op}}) {
if (!$excluded{$a->type}) {
$e{op}++;
$e{T}{$a->type}{op}++;
}
else {
$e{E}{$a->type}{op}++;
}
}
foreach $a (@{$o->{ms}}) {
if (!$excluded{$a->type}) {
$e{ms}++;
$e{T}{$a->type}{ms}++;
}
else {
$e{E}{$a->type}{ms}++;
}
}
$e{ptv} = (!$e{op} and !$e{ms}) ? 1 : 0;
return %e;
}
# computes precision, recall and F1 measures
sub precrecf1 {
my ($ok, $op, $ms) = @_;
my $p = ($ok + $op > 0) ? 100*$ok/($ok+$op) : 0;
my $r = ($ok + $ms > 0) ? 100*$ok/($ok+$ms) : 0;
my $f1 = ($p+$r>0) ? (2*$p*$r)/($p+$r) : 0;
return ($p,$r,$f1);
}
sub update_confusion_matrix {
my ($C, $gprop, $pprop) = @_;
my $o = $gprop->discriminate_args($pprop, 0);
my $a;
foreach $a ( @{$o->{ok}} ) {
my $g = shift @{$o->{eq}};
$C->{$g->type}{$a->type}++;
}
foreach $a ( @{$o->{ms}} ) {
$C->{$a->type}{"-NONE-"}++;
}
foreach $a ( @{$o->{op}} ) {
$C->{"-NONE-"}{$a->type}++;
}
}
# end of script
###############
################################################################################
#
# Package s e n t e n c e
#
# February 2004
#
# Stores information of a sentence, namely words, chunks, clauses,
# named entities and propositions (gold and predicted).
#
# Provides access methods.
# Provides methods for reading/writing sentences from/to files in
# CoNLL-2004/CoNLL-2005 formats.
#
#
################################################################################
package SRL::sentence;
use strict;
sub new {
my ($pkg, $id) = @_;
my $s = [];
$s->[0] = $id; # sentence number
$s->[1] = undef; # words (the list or the number of words)
$s->[2] = []; # gold props
$s->[3] = []; # predicted props
$s->[4] = undef; # chunks
$s->[5] = undef; # clauses
$s->[6] = undef; # full syntactic tree
$s->[7] = undef; # named entities
return bless $s, $pkg;
}
#-----
sub id {
my $s = shift;
return $s->[0];
}
#-----
sub length {
my $s = shift;
if (ref($s->[1])) {
return scalar(@{$s->[1]});
}
else {
return $s->[1];
}
}
sub set_length {
my $s = shift;
$s->[1] = shift;
}
#-----
# returns the i-th word of the sentence
sub word {
my ($s, $i) = @_;
return $s->[1][$i];
}
# returns the list of words of the sentence
sub words {
my $s = shift;
if (@_) {
return map { $s->[1][$_] } @_;
}
else {
return @{$s->[1]};
}
}
sub ref_words {
my $s = shift;
return $s->[1];
}
sub chunking {
my $s = shift;
return $s->[4];
}
sub clausing {
my $s = shift;
return $s->[5];
}
sub syntree {
my $s = shift;
return $s->[6];
}
sub named_entities {
my $s = shift;
return $s->[7];
}
#-----
sub add_gold_props {
my $s = shift;
push @{$s->[2]}, @_;
}
sub gold_props {
my $s = shift;
return @{$s->[2]};
}
sub add_pred_props {
my $s = shift;
push @{$s->[3]}, @_;
}
sub pred_props {
my $s = shift;
return @{$s->[3]};
}
#------------------------------------------------------------
# I/O F U N C T I O N S
#------------------------------------------------------------
# Reads a complete (words, synt, props) sentence from a stream
# Returns: the reference to the sentence object or
# undef if no sentence found
# The propositions in the file are stored as gold props
# For each gold prop, an empty predicted prop is created
#
# The %C hash contains the column number for each annotation of
# the datafile.
#
sub read_from_stream {
my ($pkg, $id, $fh, %C) = @_;
if (!%C) {
%C = ( words => 0,
pos => 1,
chunks => 2,
clauses => 3,
syntree => 4,
ne => 5,
props => 6
)
}
# my $k;
# foreach $k ( "words", "pos", "props" ) {
# if (!exists($C{$k}) {
# die "sentence->read_from_stream :: undefined column number for $k.\n";
# }
# }
my $cols = read_columns($fh);
if (!@$cols) {
return undef;
}
my $s = $pkg->new($id);
# words and PoS
my $words = $cols->[$C{words}];
my $pos = $cols->[$C{pos}];
# initialize list of words
$s->[1] = [];
my $i;
for ($i=0;$i<@$words;$i++) {
push @{$s->[1]}, SRL::word->new($i, $words->[$i], $pos->[$i]);
}
my $c;
# chunks
if (exists($C{chunks})) {
$c = $cols->[$C{chunks}];
# initialize chunking
$s->[4] = SRL::phrase_set->new();
$s->[4]->load_SE_tagging(@$c);
}
# clauses
if (exists($C{clauses})) {
$c = $cols->[$C{clauses}];
# initialize clauses
$s->[5] = SRL::phrase_set->new();
$s->[5]->load_SE_tagging(@$c);
}
# syntree
if (exists($C{syntree})) {
$c = $cols->[$C{syntree}];
# initialize syntree
$s->[6] = SRL::syntree->new();
$s->[6]->load_SE_tagging($s->[1], @$c);
}
# named entities
if (exists($C{ne})) {
$c = $cols->[$C{ne}];
$s->[7] = SRL::phrase_set->new();
$s->[7]->load_SE_tagging(@$c);
}
my $i = 0;
while ($i<$C{props}) {
shift @$cols;
$i++;
}
# gold props
my $targets = shift @$cols or die "error :: reading sentence $id :: no targets found!\n";
if (@$cols) {
$s->load_props($s->[2], $targets, $cols);
}
# initialize predicted props
foreach $i ( grep { $targets->[$_] ne "-" } ( 0 .. scalar(@$targets)-1 ) ) {
push @{$s->[3]}, SRL::prop->new($targets->[$i], $i);
}
return $s;
}
#------------------------------------------------------------
# reads the propositions of a sentence from files
# allows to store propositions as gold and/or predicted,
# by specifying filehandles as values in the %FILES hash
# indexed by {GOLD,PRED} keys
# expects: each prop file: first column specifying target verbs,
# and remaining columns specifying arguments
# returns a new sentence, containing the list of prop
# objects, one for each column, in gold/pred contexts
# returns undef when EOF
sub read_props {
my ($pkg, $id, %FILES) = @_;
my $s = undef;
my $length = undef;
if (exists($FILES{GOLD})) {
my $cols = read_columns($FILES{GOLD});
# end of file
if (!@$cols) {
return undef;
}
$s = $pkg->new($id);
my $targets = shift @$cols;
$length = scalar(@$targets);
$s->set_length($length);
$s->load_props($s->[2], $targets, $cols);
}
if (exists($FILES{PRED})) {
my $cols = read_columns($FILES{PRED});
if (!defined($s)) {
# end of file
if (!@$cols) {
return undef;
}
$s = $pkg->new($id);
}
my $targets = shift @$cols;
if (defined($length)) {
($length != scalar(@$targets)) and
die "ERROR : sentence $id : gold and pred sentences do not align correctly!\n";
}
else {
$length = scalar(@$targets);
$s->set_length($length);
}
$s->load_props($s->[3], $targets, $cols);
}
return $s;
}
sub load_props {
my ($s, $where, $targets, $cols) = @_;
my $i;
for ($i=0; $i<@$targets; $i++) {
if ($targets->[$i] ne "-") {
my $prop = SRL::prop->new($targets->[$i], $i);
my $col = shift @$cols;
if (defined($col)) {
# print "SE Tagging: ", join(" ", @$col), "\n";
$prop->load_SE_tagging(@$col);
}
else {
print STDERR "WARNING : sentence ", $s->id, " : can't find column of args for prop ", $prop->verb, "!\n";
}
push @$where, $prop;
}
}
}
# writes a sentence to an output stream
# allows to specify which parts of the sentence are written
# by giving true values to the %WHAT hash, indexed by
# {WORDS,SYNT,GOLD,PRED} keys
sub write_to_stream {
my ($s, $fh, %WHAT) = @_;
if (!%WHAT) {
%WHAT = ( WORDS => 1,
PSYNT => 1,
FSYNT => 1,
GOLD => 0,
PRED => 1
);
}
my @columns;
if ($WHAT{WORDS}) {
my @words = map { $_->form } $s->words;
push @columns, \@words;
}
if ($WHAT{PSYNT}) {
my @pos = map { $_->pos } $s->words;
push @columns, \@pos;
my @chunks = $s->chunking->to_SE_tagging($s->length);
push @columns, \@chunks;
my @clauses = $s->clausing->to_SE_tagging($s->length);
push @columns, \@clauses;
}
if ($WHAT{FSYNT}) {
my @pos = map { $_->pos } $s->words;
push @columns, \@pos;
my @sttags = $s->syntree->to_SE_tagging();
push @columns, \@sttags;
}
if ($WHAT{GOLD}) {
push @columns, $s->props_to_columns($s->[2]);
}
if ($WHAT{PRED}) {
push @columns, $s->props_to_columns($s->[3]);
}
if ($WHAT{PROPS}) {
push @columns, $s->props_to_columns($WHAT{PROPS});
}
reformat_columns(\@columns);
# finally, print columns word by word
my $i;
for ($i=0;$i<$s->length;$i++) {
print $fh join(" ", map { $_->[$i] } @columns), "\n";
}
print $fh "\n";
}
# turns a set of propositions (target verbs + args for each one) into a set of
# columns in the CoNLL Start-End format
sub props_to_columns {
my ($s, $Pref) = @_;
my @props = sort { $a->position <=> $b->position } @{$Pref};
my $l = $s->length;
my $verbs = [];
my @cols = ( $verbs );
my $p;
foreach $p ( @props ) {
defined($verbs->[$p->position]) and die "sentence->preds_to_columns: already defined verb at sentence ", $s->id, " position ", $p->position, "!\n";
$verbs->[$p->position] = sprintf("%-15s", $p->verb);
my @tags = $p->to_SE_tagging($l);
push @cols, \@tags;
}
# finally, define empty verb positions
my $i;
for ($i=0;$i<$l;$i++) {
if (!defined($verbs->[$i])) {
$verbs->[$i] = sprintf("%-15s", "-");
}
}
return @cols;
}
# Writes the predicted propositions of the sentence to an output file handler ($fh)
# Specifically, writes a column of target verbs, and a column of arguments
# for each target verb
# OBSOLETE : the same can be done with write_to_stream($s, PRED => 1)
sub write_pred_props {
my ($s, $fh) = @_;
my @props = sort { $a->position <=> $b->position } $s->pred_props;
my $l = $s->length;
my @verbs = ();
my @cols = ();
my $p;
foreach $p ( @props ) {
defined($verbs[$p->position]) and die "prop->write_pred_props: already defined verb at sentence ", $s->id, " position ", $p->position, "!\n";
$verbs[$p->position] = $p->verb;
my @tags = $p->to_SE_tagging($l);
push @cols, \@tags;
}
# finally, print columns word by word
my $i;
for ($i=0;$i<$l;$i++) {
printf $fh "%-15s %s\n", (defined($verbs[$i])? $verbs[$i] : "-"),
join(" ", map { $_->[$i] } @cols);
}
print "\n";
}
# reads columns until blank line or EOF
# returns an array of columns (each column is a reference to an array containing the column)
# each column in the returned array should be the same size
sub read_columns {
my $fh = shift;
# read columns until blank line or eof
my @cols;
my $i;
my @line = split(" ", <$fh>);
while (@line) {
for ($i=0; $i<@line; $i++) {
push @{$cols[$i]}, $line[$i];
}
@line = split(" ", <$fh>);
}
return \@cols;
}
# reformats the tags of a list of columns, so that each
# column has a fixed width along all tags
#
#
sub reformat_columns {
my $cols = shift; # a reference to the list of columns of a sentence
my $i;
for ($i=0;$i<scalar(@$cols);$i++) {
column_pretty_format($cols->[$i]);
}
}
# reformats the tags of a column, so that each
# tag has the same width
#
# tag sequences are left justified
# start-end annotations are centered at the asterisk
#
sub column_pretty_format {
my $col = shift; # a reference to the column (array) of tags
(!@$col) and return undef;
my ($i);
if ($col->[0] =~ /\*/) {
# Start-End
my $ok = 1;
my (@s,@e,$t,$ms,$me);
$ms = 2; $me = 2;
$i = 0;
while ($ok and $i<@$col) {
if ($col->[$i] =~ /^(.*\*)(.*)$/) {
$s[$i] = $1;
$e[$i] = $2;
if (length($s[$i]) > $ms) {
$ms = length($s[$i]);
}
if (length($e[$i]) > $me) {
$me = length($e[$i]);
}
}
else {
# In this case, the current token is not compliant with SE format
# So, we treat format the column as a sequence of tags
$ok = 0;
}
$i++;
}
# print "M $ms $me\n";
if ($ok) {
my $f = "%".($ms+1)."s%-".($me+1)."s";
for ($i=0; $i<@$col; $i++) {
$col->[$i] = sprintf($f, $s[$i], $e[$i]);
}
return;
}
}
# Tokens
my $l=0;
map { (length($_)>$l) and ($l=length($_)) } @$col;
my $f = "%-".($l+1)."s";
for ($i=0; $i<@$col; $i++) {
$col->[$i] = sprintf($f,$col->[$i]);
}
}
1;
##################################################################
#
# Package p r o p : A proposition (verb + args)
#
# January 2004
#
##################################################################
package SRL::prop;
use strict;
# Constructor: creates a new prop, with empty arguments
# Parameters: verb form, position of verb
sub new {
my ($pkg, $v, $position) = @_;
my $p = [];
$p->[0] = $v; # the verb
$p->[1] = $position; # verb position
$p->[2] = undef; # verb sense
$p->[3] = []; # args, empty by default
return bless $p, $pkg;
}
## Accessor/Initialization methods
# returns the verb form of the prop
sub verb {
my $p = shift;
return $p->[0];
}
# returns the verb position of the verb in the prop
sub position {
my $p = shift;
return $p->[1];
}
# returns the verb sense of the verb in the prop
sub sense {
my $p = shift;
return $p->[2];
}
# initializes the verb sense of the verb in the prop
sub set_sense {
my $p = shift;
$p->[2] = shift;
}
# returns the list of arguments of the prop
sub args {
my $p = shift;
return @{$p->[3]};
}
# initializes the list of arguments of the prop
sub set_args {
my $p = shift;
@{$p->[3]} = @_;
}
# adds arguments to the prop
sub add_args {