-
Notifications
You must be signed in to change notification settings - Fork 0
/
score-answers.pl
485 lines (415 loc) · 12.3 KB
/
score-answers.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
#!/usr/bin/perl
# USAGE: perl score-answers.pl <response_file> <answerkey_file>
#
# **********************************************************************
# DATA STRUCTURES
# **********************************************************************
use Class::Struct;
struct keyinfo => {
qid => '$',
question => '$',
answer => '$',
difficulty => '$',
};
struct respinfo => {
qid => '$',
answer => '$',
};
# **********************************************************************
# MAIN FUNCTIONS & GLOBAL VARIABLES
# **********************************************************************
$recall_sum = 0;
$precision_sum = 0;
$fmeasure_sum = 0;
$num_questions = 0;
$num_questions_answered = 0;
$correctwords_sum = 0;
$respwords_sum = 0;
$keywords_sum = 0;
$num_args = @ARGV;
if ($num_args < 2) {
print "ERROR: missing required input file.";
print "Usage is: score-answer.pl <response_file> <answerkey_file>\n";
}
else {
$response_file = $ARGV[0];
$answerkey_file = $ARGV[1];
my($keyinfo_list, $respinfo_list);
$keyinfo_list = read_anskey($answerkey_file);
$respinfo_list = read_responses($response_file);
$success = score($keyinfo_list, $respinfo_list);
if ($success) {
print_final_stats();
}
else {
print "\n**************************************************\n\n";
print " ABORTING DUE TO MISALIGNMENT!";
print "\n\n**************************************************\n";
}
}
# ***********************************************************************
# FUNCTIONS
# ***********************************************************************
# Score all answers
sub score {
my($keyinfo_list, $respinfo_list) = @_;
while (($keyinfo = shift(@$keyinfo_list)) &&
($respinfo = shift(@$respinfo_list))) {
$qid = $keyinfo->qid;
# Make sure QIDs are the same
if ($respinfo->qid !~ /^$qid$/i) {
print "ERROR: Key and Response not aligned properly!!!\n";
print "Key QID = $qid Respond QID = ";
print $respinfo->qid; print "\n";
return(0); # return failure
}
else {
score_info($keyinfo, $respinfo);
}
}
return(1); # return success
}
# Score one particular answer
sub score_info {
my($keyinfo, $respinfo) = @_;
my($qid, $keyans, $respans, @keyoptions, $option);
my($recall,$precision,$fmeasure,$correct,$numkeywords,$numrespwords);
my($best_recall, $best_precision, $best_correct, $best_keywords);
my($best_numrespwords, $best_keymatch, $num_options);
my($best_fmeasure) = -1;
$qid = $keyinfo->qid;
print "-----------------------------------------------------------------------------------------\n";
print "\nSCORING $qid\n";
$keyans = $keyinfo->answer;
$respans = $respinfo->answer;
@keyoptions = split /\s*\|\s*/, $keyans;
$num_options = @keyoptions;
foreach $option (@keyoptions) {
($recall,$precision,$fmeasure,$correct,$numkeywords,$numrespwords)
= score_strings($option, $respans);
if ((($fmeasure =~ /^-$/) && ($best_fmeasure == -1)) ||
($fmeasure > $best_fmeasure)) {
$best_keymatch = $option;
$best_recall = $recall;
$best_precision = $precision;
$best_fmeasure = $fmeasure;
$best_correct = $correct;
$best_keywords = $numkeywords;
$best_numrespwords = $numrespwords;
}
}
$recall_sum += $best_recall;
$precision_sum += $best_precision;
$fmeasure_sum += $best_fmeasure;
$num_questions++;
if ($best_numrespwords > 0) { # don't increment if no response words!
$num_questions_answered++;
}
$correctwords_sum += $best_correct;
$respwords_sum += $best_numrespwords;
$keywords_sum += $best_keywords;
if ($num_options > 1) {
print "\n=> Best match in answer key is:\n `$best_keymatch'\n";
}
print_stats($best_recall, $best_precision, $best_fmeasure,
$best_correct, $best_keywords, $best_numrespwords);
}
# String matching procedure
sub score_strings {
my($keystr, $respstr) = @_;
my(@keywords) = (); my(@respwords) = ();
my($correct) = 0; my($spurious) = 0; my($missed) = 0;
my($numkeywords, $numrespwords, $fmeasure, $recall, $precision);
print "\nComparing Key `$keystr'\n and Resp `$respstr'\n";
@keywords = split /\s+/, $keystr;
@respwords = split /\s+/, $respstr;
strip_punctuation(\@keywords);
strip_punctuation(\@respwords);
$numkeywords = @keywords;
$numrespwords = @respwords;
foreach $respword (@respwords) {
if (member($respword, @keywords)) {
# print "Found `$respword' in `@keywords'\n";
$correct++;
@keywords = remove($respword, @keywords);
}
else {
$spurious++;
}
}
$missed = @keywords; # number of unmatched words left in @keywords
($recall, $precision, $fmeasure) =
compute_stats($correct, $numkeywords, $numrespwords);
return($recall, $precision, $fmeasure, $correct, $numkeywords,
$numrespwords);
}
sub strip_punctuation {
my($words) = @_;
my($num_words, $word);
$num_words = @$words;
# strip off leading and trailing punctuation
# (punctuation in the middle of a word is fine! Consider things like
# decimal points in numbers, apostrophes, etc.)
#
for ($i=0; $i < $num_words; $i++) {
@$words[$i] =~ s/^[,:;\.\!\?\'\"\(\{\)\}]//;
@$words[$i] =~ s/[,:;\.\!\?\'\"\(\{\)\}]$//;
}
# check for words that are the empty string (meaning that they
# consisted of ONLY punctutation symbols before the strippage above)
# and remove them from the list of words
for ($i=0; $i < $num_words; $i++) {
$word = shift(@$words);
if ($word !~ /^$/) { # not empty string so push back on
push(@$words, $word);
}
}
}
# computes recall, precision, and fmeasure
sub compute_stats {
my($correct, $numkeywords, $numrespwords) = @_;
my($recall, $precision, $fmeasure);
if ($numkeywords > 0) {
$recall = $correct / $numkeywords;
}
else {
$recall = "-";
}
if ($numrespwords > 0) {
$precision = $correct / $numrespwords;
}
else {
$precision = "-";
}
$fmeasure = compute_fmeasure($recall, $precision);
return($recall, $precision, $fmeasure);
}
sub compute_fmeasure {
my($recall, $precision) = @_;
my($f, $denom);
if (($recall =~ /^-$/) || ($precision =~ /^-$/)) {
return("-");
}
else {
$denom = $recall + $precision;
if ($denom > 0) {
$f = (2 * $recall * $precision) / $denom;
}
else {
$f = 0;
}
return($f);
}
}
sub member {
my($target, @lst) = @_;
my($found) = 0;
my($item);
while (!$found && (@lst)) {
$item = shift @lst;
if ($item =~ /^\Q$target\E$/i) {
$found = 1;
}
}
return ($found);
}
sub remove {
my($target, @lst) = @_;
my($found) = 0;
my($item);
my(@newlst) = ();
# print "Removing `$target'\n";
# print "Initial list = @lst\n";
while ($item = shift @lst) {
if ($found || ($item !~ /^$target$/i)) { # retain everything except the
push(@newlst, $item); # *first* string that matches. All other
} # instances should be retained.
else {
$found = 1;
}
}
# print "Modified list = @newlst\n";
return(@newlst);
}
# Reads in an answer key file
sub read_anskey {
my($file) = @_;
my($qid, $question, $answer, $difficulty, $keyinfo);
my(@keyinfo_list) = ();
open(INPUT_STREAM, "$file") || die "ERROR: Couldn't open file $file\n";
while ($line = <INPUT_STREAM>) {
if ($line =~ /^\s+$/) { # skip blank lines between questions
next;
}
if ($line =~ /^QuestionID:\s*(.*?)\s*$/i) {
$qid = $1;
}
else {
print "ERROR: Expected Question ID but found: $line\n";
}
$line = <INPUT_STREAM>;
if ($line =~ /^Question:\s*(.*?)\s*$/i) {
$question = $1;
}
else {
print "ERROR: Expected Question but found: $line\n";
}
$line = <INPUT_STREAM>;
if ($line =~ /^Answer:\s*(.*?)\s*$/i) {
$answer = $1;
}
else {
print "ERROR: Expected Answer but found: $line\n";
}
$line = <INPUT_STREAM>;
if ($line =~ /^Difficulty:\s*(.*?)\s*$/i) {
$difficulty = $1;
}
else {
print "ERROR: Expected Difficulty but found: $line\n";
}
$keyinfo = create_keyinfo($qid, $question, $answer, $difficulty);
push(@keyinfo_list, $keyinfo);
}
return(\@keyinfo_list);
}
# Reads in a system-generated response file
sub read_responses {
my($file) = @_;
my($qid, $answer, $respinfo);
my(@respinfo_list) = ();
open(INPUT_STREAM, "$file") || die "ERROR: Couldn't open file $file\n";
while ($line = <INPUT_STREAM>) {
if ($line =~ /^\s+$/) { # skip blank lines between questions
next;
}
if ($line =~ /^QuestionID:\s*(.*?)\s*$/i) {
$qid = $1;
}
else {
print "ERROR: Expected QuestionID but found: $line\n";
}
$line = <INPUT_STREAM>;
if ($line =~ /^Answer:\s*(.*?)\s*$/i) {
$answer = $1;
}
else {
print "ERROR: Expected Answer but found: $line\n";
}
$respinfo = create_respinfo($qid, $answer);
push(@respinfo_list, $respinfo);
}
return(\@respinfo_list);
}
sub create_keyinfo {
my($qid, $question, $answer, $difficulty) = @_;
my($keyinfo);
$keyinfo = keyinfo->new();
$keyinfo->qid($qid);
$keyinfo->question($question);
$keyinfo->answer($answer);
$keyinfo->difficulty($difficulty);
return($keyinfo);
}
sub print_keyinfo {
my($keyinfo) = @_;
my($qid, $question, $answer, $difficulty);
$qid = $keyinfo->qid;
$question = $keyinfo->question;
$answer = $keyinfo->answer;
$difficulty = $keyinfo->difficulty;
print "QUESTION ID: `$qid'\n";
print "QUESTION : `$question'\n";
print "ANSWER : `$answer'\n";
print "DIFFICULTY : `$difficulty'\n\n";
}
sub create_respinfo {
my($qid, $answer) = @_;
my($respinfo);
$respinfo = respinfo->new();
$respinfo->qid($qid);
$respinfo->answer($answer);
return($respinfo);
}
sub print_respinfo {
my($respinfo) = @_;
my($qid, $answer);
$qid = $respinfo->qid;
$answer = $respinfo->answer;
print "QUESTION ID: `$qid'\n";
print "ANSWER : `$answer'\n\n";
}
# Given a string and number N, this function pads spaces onto the right
# side of the string until the string has length N.
#
sub pad_w_spaces {
my($str,$N) = @_;
my($len) = length $str;
while ($len < $N) {
$str = $str . " ";
$len = length $str;
}
return($str);
}
sub print_stats {
my($recall, $precision, $fmeasure, $correct, $numkeywords, $numrespwords) = @_;
print "\nRecall = ";
if ($recall !~ /^-$/) {
printf "%.2f", $recall;
}
else {
print "N/A";
}
print " ($correct/$numkeywords)\n";
print "Precision = ";
if ($precision !~ /^-$/) {
printf "%.2f", $precision;
}
else {
print "N/A";
}
print " ($correct/$numrespwords)\n";
print "F-measure = ";
if ($fmeasure !~ /^-$/) {
printf "%.2f", $fmeasure;
}
else {
print "N/A";
}
print "\n\n";
}
sub print_final_stats {
my($avg_recall, $avg_precision, $avg_fmeasure);
my($overall_recall, $overall_precision, $overall_fmeasure);
print "\n\nFinished processing $num_questions questions, ";
print "$num_questions_answered of which had responses.\n";
print "*************************************************************************\n\n";
print "FINAL RESULTS\n\n";
# These are word-based version of R, P, and F
# ($overall_recall, $overall_precision, $overall_fmeasure) =
# compute_stats($correctwords_sum, $keywords_sum, $respwords_sum);
# print_stats($overall_recall, $overall_precision,
# $overall_fmeasure, $correctwords_sum, $keywords_sum,$respwords_sum);
if ($num_questions > 0) {
$avg_recall = $recall_sum / $num_questions;
if ($num_questions_answered > 0) {
$avg_precision = $precision_sum / $num_questions_answered;
}
else { # System didn't produce ANY answers if we get here!
$avg_precision = 0;
}
$avg_fmeasure = compute_fmeasure($avg_recall, $avg_precision);
print "AVERAGE RECALL = "; printf "%.4f", $avg_recall;
print " ("; printf "%.2f", $recall_sum;
print " / $num_questions)";
print "\nAVERAGE PRECISION = "; printf "%.4f", $avg_precision;
print " ("; printf "%.2f", $precision_sum;
print " / $num_questions_answered)";
print "\nAVERAGE F-MEASURE = "; printf "%.4f", $avg_fmeasure;
print "\n\n";
print "*************************************************************************\n";
}
else {
print "ERROR: No questions/answers found!\n";
}
}