-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwa_eval_align.pl
226 lines (187 loc) · 6.4 KB
/
wa_eval_align.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
#!/usr/bin/perl -w
# Usage: wa_eval.pl <answer_file> <submission_file>
# where answer_file is the name of the file containing the gold
# standard word alignments
# and submission_file is the name of the file containing the
# automatically produced word alignments
#
# Evaluation is performed using:
# - Standard Precision, Recall, F-measure, separate
# for S (Sure) and P (Probable) cases
# - AER measure, defined as
# AER = 1 - ( |A & S| + |A & P| ) / ( |A| + |S| )
# [where A represents the alignment, S and P represent the
# S (Sure) and P (Probable) gold standard alignments]
#
# March 06, 2003
# Send comments, suggestions to: Rada Mihalcea, rada@cs.unt.edu
# This software is provided free of charge, AS IS. Feel free to use it
# and/or modify it.
scalar(@ARGV==2) || die "Usage: wa_eval.pl answer_file submission_file\n";
$answer = $ARGV[0];
$submission = $ARGV[1];
# open the two files
if ( (! -e $answer) || (! open ANSWERFILE, "<$answer") ) {
die "Can't find/open answer file `$answer': $!\n";
}
if ( (! -e $submission) || (! open SUBMISSIONFILE, "<$submission") ) {
die "Can't find/open submission file `$submission': $!\n";
}
# read all the entries in the answer file
# sure alignments and probable alignments are stored separately
while($line = <ANSWERFILE>) {
chomp $line;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
# get all line components
# format should be
# sentence_no position_L1 position_L2 [S|P] [confidence]
@components = split /\s+/, $line;
if(scalar(@components) < 3) {
print STDERR "Incorrect format in answer file `$answer`\n";
exit;
}
$alignment = $components[0]." ".$components[1]." ".$components[2];
# identify the S[ure] alignments
if( scalar (@components) == 3 ||
(scalar (@components) == 4 &&
($components[3] =~ /^[\d\.]+$/ ||
$components[3] eq 'S')) ||
(scalar (@components) == 5 &&
($components[3] eq 'S' ||
$components[4] eq 'S'))) {
$sureAnswer{$alignment} = 1;
}
# identify the P[robable] alignments
if( (scalar (@components) == 4 &&
$components[3] eq 'P') ||
(scalar (@components) == 5 &&
($components[3] eq 'P' ||
$components[4] eq 'P'))) {
$probableAnswer{$alignment} = 1;
}
}
# read all the entries in the submission file
# sure alignments and probable alignments are stored separately
while($line = <SUBMISSIONFILE>) {
chomp $line;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
# get all line components
# format should be
# sentence_no position_L1 position_L2 [S|P] [confidence]
@components = split /\s+/, $line;
if(scalar(@components) < 3) {
print STDERR "Incorrect format in submission file `$submission`: $!\n";
exit;
}
$alignment = $components[0]." ".$components[1]." ".$components[2];
# identify the S[ure] alignments
if( scalar (@components) == 3 ||
(scalar (@components) == 4 &&
($components[3] =~ /^[\d\.]+$/ ||
$components[3] eq 'S')) ||
(scalar (@components) == 5 &&
($components[3] eq 'S' ||
$components[4] eq 'S'))) {
$sureSubmission{$alignment} = 1;
}
# identify the P[probable] alignments
if( (scalar (@components) == 4 &&
$components[3] eq 'P') ||
(scalar (@components) == 5 &&
($components[3] eq 'P' ||
$components[4] eq 'P'))) {
$probableSubmission{$alignment} = 1;
}
}
# now determine the S[ure] matches
$sureMatch = 0;
foreach $alignment (keys %sureSubmission) {
if(defined($sureAnswer{$alignment})) {
$sureMatch++;
}
}
# and the [P]robable matches
# these are checked against both S[ure] and P[robable] correct alignments
$probableMatch = 0;
foreach $alignment (keys %probableSubmission, keys %sureSubmission) {
if(defined($sureAnswer{$alignment}) ||
defined($probableAnswer{$alignment})) {
$probableMatch++;
}
}
# and also the intersection between all submitted alignments
# and the S [Sure] correct alignments -- as needed by AER
$probableMatchSure = 0;
foreach $alignment (keys %probableSubmission, keys %sureSubmission) {
if(defined($sureAnswer{$alignment})) {
$probableMatchSure++;
}
}
# now determine the precision, recall, and F-measure for [S]ure alignments
if(scalar(keys %sureSubmission) != 0) {
$surePrecision = $sureMatch / scalar(keys %sureSubmission);
}
else {
$surePrecision = 0;
}
if(scalar(keys %sureAnswer) != 0) {
$sureRecall = $sureMatch / scalar(keys %sureAnswer);
}
else {
$sureRecall = 0;
}
if($sureRecall != 0 && $surePrecision != 0) {
$sureFMeasure = 2 * $sureRecall * $surePrecision /
($sureRecall + $surePrecision);
}
else {
$sureFMeasure = 0;
}
# and now determine the precision, recall, and F-measure for [P]robable alignments
if(scalar(keys %sureSubmission) + scalar(keys %probableSubmission) != 0) {
$probablePrecision = $probableMatch / (scalar(keys %sureSubmission) +
scalar(keys %probableSubmission));
}
else {
$probablePrecision = 0;
}
if(scalar(keys %sureAnswer) + scalar(keys %probableAnswer)!= 0) {
$probableRecall = $probableMatch / (scalar(keys %sureAnswer) +
+ scalar(keys %probableAnswer));
}
else {
$probableRecall = 0;
}
if($probableRecall != 0 && $probablePrecision != 0) {
$probableFMeasure = 2 * $probableRecall * $probablePrecision /
($probableRecall + $probablePrecision);
}
else {
$probableFMeasure = 0;
}
# and determine the AER
if(scalar(keys %sureSubmission) + scalar(keys %probableSubmission) != 0) {
$AER = 1 - ($probableMatchSure + $probableMatch) / (scalar(keys %sureSubmission) + scalar(keys %probableSubmission) + scalar(keys %sureAnswer));
}
else {
$AER = 0;
}
# and finally - print out the results
printf("\nThe following results assume correct file formats.\n");
printf("Make sure you have previously checked the file format\n");
printf("using wa_check_submission.pl\n\n");
printf("\n Word Alignment Evaluation \n");
printf("----------------------------------\n");
printf(" Evaluation of SURE alignments \n");
printf(" Precision = %5.4f \n", $surePrecision);
printf(" Recall = %5.4f\n",$sureRecall);
printf(" F-measure = %5.4f\n",$sureFMeasure);
printf("-----------------------------------\n");
printf(" Evaluation of PROBABLE alignments\n");
printf(" Precision = %5.4f\n",$probablePrecision);
printf(" Recall = %5.4f\n",$probableRecall);
printf(" F-measure = %5.4f\n",$probableFMeasure);
printf("-----------------------------------\n");
printf(" AER = %5.4f\n",$AER);