-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheval_metrics.pl
378 lines (336 loc) · 17.2 KB
/
eval_metrics.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
#!/usr/bin/perl
##----------------------------------------------------------------------------------------##
## Functionality: ##
## Computes the evaluation metrics: ##
## (i) exact-induction ratio ##
## (ii) under-induction ratio ##
## (iii) over-induction ratio ##
## (iv) precision ##
## (v) recall ##
## The above measurements are computed for each rule in groundtruth, ##
## as well as, for all rules. ##
## For further details regarding the above metrics consult the ##
## "grammar_evaluation.pdf". ##
## ##
## Input: ##
## 1. The name of file that correspond to groundtruth. ##
## This file should be "groundtruth/" directory. ##
## 2. The name of file that correspond to system output. ##
## This file should be "system/" directory. ##
## 3. Value of lambda1 (weight of under-induction) ##
## 4. Value of lambda2 (weight of over-induction) ##
## ##
## Notes: ##
## - Both of the obove files given by 1st and 2nd argument should be in .csv format. ##
## However, do not add the ".csv" extension during the passing of arguments. ##
## - Lambdas given by 3rd and 4th argument should sum to 1, e.g., 0.5+0.5=1. ##
## ##
## Output: ##
## A directory is created named "evaluation_results/", ##
## including a .csv file named according to the name of ##
## file that correspond to system output (2nd input argument) ##
## extended by the values of L1 and L2 and ##
## ending in ".results.csv". ##
## This file includes the scores for the aforementioned evaluation metrics. ##
## ##
## How to run ##
## Example: perl evaluate.pl file_ground file_system 0.5 0.5 ##
## ##
## Contact: ##
## Elias Iosif [email protected] ##
##----------------------------------------------------------------------------------------##
## Input args
##-----------
#------------
$ground_file = $ARGV[0]; chomp($ground_file);
$ground_system = $ARGV[1]; chomp($ground_system);
## Weights used for the computation of precision and recall
##---------------------------------------------------------
$lambda1 = $ARGV[2]; chomp($lambda1); ## Weight assigned to under-induction
$lambda2 = $ARGV[3]; chomp($lambda2); ## Weight assigned to over-induction
## Directories of groundtruth and indcuced rules
##------------------------------------------------
#$d_ground = "evaluation/groundtruth/"; ## Root directory for groundtruth
#$d_system = "evaluation/induced_rules/"; ## Root directory for induced rules
## Create directory for evaluation results
##----------------------------------------
$d_res = "evaluation_results/"; if (!(-d $d_res)) { system ("mkdir $d_res"); }
## Initalize evaluation metrics to be computed all induced rules
##--------------------------------------------------------------
$total_num_exact = 0; ## Total num. of exact matches
$total_num_under = 0; ## Total num. of partial matches: under-induction case
$total_num_over = 0; ## Total num. of partial matches: over-induction case
$total_num_none = 0; ## Total num. of members of induced rules not matched
$total_num_in_groundtruth_not_matched = 0; ## Total num. of members of groundtruth rules not matched
$total_exact_induction_ratio = 0; ## Ratio of exact-induction
$total_under_induction_ratio = 0; ## Ratio of under-induction
$total_over_induction_ratio = 0; ## Ratio of over-induction
$total_rule_precision = 0; ## Precision
$total_rule_recall = 0; ## Recall
if (scalar(@ARGV) == 0) ## No arguments given
{
die ("You must give the name of the file (.csv) corresponding to groundtruth and induced rules.\n");
}
else
{
$groundtruth_path = "";
$induced_path = "";
#$groundtruth_path = $d_ground.$ground_file;
#$induced_path = $d_system.$ground_system;
$groundtruth_path = $d_ground.$ground_file;
$induced_path = $d_system.$ground_system;
if ( (!(-e $groundtruth_path)) || (!(-e $induced_path)) ) ## Check if the file(s) of groundtruth and induced rules exist
{
die ("Can not find file(s) of groundtruth and/or induced rules: $groundtruth_path or $induced_path\n");
}
}
## Create file for reporting evaluation results
##---------------------------------------------
$results_file = $d_res.$ground_system.".L1_".$lambda1."L2_".$lambda1.".results.csv";
open (RES,">$results_file") || die ("Can not write $results_file \n");
print RES ("Rule",",","ExactInductionRatio",",","UnderInductionRatio",",","OverInductionRatio",",","Precision",",","Recall","\n");
## Load groundtruth
##-----------------
%groundtruth_rules2members = ();
%groundtruth_rules2members_num = ();
@members_lst = ();
$members_str = ();
@groundtruth_rules_lst_sort = ();
$rule_label = "";
$num_of_members = 0;
open (G,"$groundtruth_path") || die ("Can not open $groundtruth_path \n");
$rdln = "";
$rdln = <G>;
while ($rdln ne "")
{
chomp($rdln);
$rdln =~ s/\-/ /g; ## New: May 2013
@members_lst = split(/,/,$rdln); ## Rule label + members
$rule_label = shift(@members_lst); ## Extract 1st element that is the rule label
$members_str = join(",",@members_lst);
$num_of_members = $#members_lst + 1;
if ($num_of_members < 1)
{
die ("Rule $rule_label appears to be empty. Check this rule. \n");
#print ("Rule $rule_label appears to be empty. Check this rule. \n");
}
$groundtruth_rules2members{$rule_label} = $members_str;
$groundtruth_rules2members_num{$rule_label} = $num_of_members;
$rdln = <G>;
}
close (G);
@groundtruth_rules_lst_sort = sort(keys(%groundtruth_rules2members));
## Load rules induced by the system
##---------------------------------
%induced_rules2members = ();
%induced_rules2members_num = ();
@members_lst = ();
$members_str = ();
$rule_label = "";
$num_of_members = 0;
open (I,"$induced_path") || die ("Can not open $groundtruth_path \n");
$rdln = "";
$rdln = <I>;
while ($rdln ne "")
{
$rdln =~ s/\-/ /g; ## New: May 2013
chomp($rdln);
@members_lst = split(/,/,$rdln); ## Rule label + members
$rule_label = shift(@members_lst); ## Extract 1st element that is the rule label
$members_str = join(",",@members_lst);
$num_of_members = $#members_lst + 1;
$induced_rules2members{$rule_label} = $members_str;
$induced_rules2members_num{$rule_label} = $num_of_members;
$rdln = <I>;
}
close (I);
## Compute evaluation metrics
##---------------------------
$g_counter = 0;
foreach $grule (@groundtruth_rules_lst_sort) ## For each groundtruth rule: start
{
$g_members_str = "";
@g_members_lst = ();
%g_members_hsh = ();
%g_checked = ();
%g_exact_checked = ();
%g_under_checked = ();
%g_over_checked = ();
$i_members_str = "";
@i_members_lst = ();
%i_members_hsh = ();
%i_checked = ();
%i_exact_checked = ();
%i_under_checked = ();
%i_over_checked = ();
$g_counter ++;
## Current groundtruth rule
##-------------------------
$g_members_str = $groundtruth_rules2members{$grule};
@g_members_lst = split(/,/,$g_members_str);
foreach (@g_members_lst) { $g_members_hsh{$_}=1; }
## Respective induced rule
##-------------------------
$i_members_str = $induced_rules2members{$grule};
@i_members_lst = split(/,/,$i_members_str);
foreach (@i_members_lst) { $i_members_hsh{$_}=1; }
foreach $cur_g_member (@g_members_lst) ## For each member of groundtruth rule: start
{
$length_g_member = 0;
@g_lst = split(/\s+/,$cur_g_member);
$length_g_member = $#g_lst + 1; ## Length of current member counted as number of tokens
## Compare with members of respective induced rule
##------------------------------------------------
if (defined($i_members_hsh{$cur_g_member})) ## Exact match
{
if ( (!(defined($g_exact_checked{$cur_g_member}))) && (!(defined($i_exact_checked{$cur_g_member}))) )
{
$g_checked{$cur_g_member} = 1;
$g_exact_checked{$cur_g_member} = 1;
$i_checked{$cur_g_member} = 1;
$i_exact_checked{$cur_g_member} = 1;
}
}
elsif ( (!(defined($i_members_hsh{$cur_g_member}))) && ($length_g_member > 1)) ## No exact match. So, check for under-induction or over-induction
{
%reference = ();
map { $reference{$_} = 1 } @g_lst;
## Check for each member respective induced rule
##----------------------------------------------
$under_induction_flag = 0;
$over_induction_flag = 0;
foreach $cur_i_member (@i_members_lst) ## For each member of induced rule: start
{
$length_i_member = 0;
@i_lst = split(/\s+/,$cur_i_member);
@overlap_lst = ();
@overlap_lst = grep { $reference{$_} } @i_lst;
$overlap_score = 0;
$overlap_score = $#overlap_lst + 1;
$length_i_member = 0;
$length_i_member = $#i_lst + 1;
if ( ($length_i_member <= $length_g_member) && ($overlap_score > 0) )
{
if ( (!(defined($g_exact_checked{$cur_g_member}))) && (!(defined($i_exact_checked{$cur_g_member}))) )
{
if ( (!(defined($g_under_checked{$cur_g_member}))) && (!(defined($i_under_checked{$cur_g_member}))) )
{
$g_under_checked{$cur_g_member} = 1;
$i_under_checked{$cur_i_member} = 1;
#print (@overlap_lst,"\n");
#print ("Ground: ",$cur_g_member,"\n");
#print ("Induced: ",$cur_i_member,"\n");
}
}
}
if ( ($length_i_member > $length_g_member) && ($overlap_score > 0) )
{
if ( (!(defined($g_exact_checked{$cur_g_member}))) && (!(defined($i_exact_checked{$cur_g_member}))) )
{
if ( (!(defined($g_over_checked{$cur_g_member}))) && (!(defined($i_over_checked{$cur_g_member}))) )
{
$g_over_checked{$cur_g_member} = 1;
$i_over_checked{$cur_i_member} = 1;
}
}
}
} ## For each member of induced rule: start
} ## Not exact match
} ## For each member of groundtruth rule: end
## Evaluation metrics for current rule
##------------------------------------
$num_exact = 0;
$num_under = 0;
$num_over = 0;
$num_none = 0;
$num_in_groundtruth_not_matched = 0;
$exact_induction_ratio = 0;
$under_induction_ratio = 0;
$over_induction_ratio = 0;
$current_rule_precision = 0;
$current_rule_recall = 0;
@num_exact_lst = keys(%i_exact_checked);
$num_exact = $#num_exact_lst + 1;
@num_under_lst = keys(%i_under_checked);
$num_under = $#num_under_lst + 1;
@num_over_lst = keys(%i_over_checked);
$num_over = $#num_over_lst + 1;
foreach $cur_i_mem (keys(%i_members_hsh))
{
if ( (!(defined($i_exact_checked{$cur_i_mem}))) && (!(defined($i_under_checked{$cur_i_mem}))) && (!(defined($i_over_checked{$cur_i_mem}))) )
{
$num_none ++;
}
}
foreach $cur_g_mem (keys(%g_members_hsh))
{
if ( (!(defined($g_exact_checked{$cur_g_mem}))) && (!(defined($g_under_checked{$cur_g_mem}))) && (!(defined($g_over_checked{$cur_g_mem}))) )
{
$num_in_groundtruth_not_matched ++;
}
}
print ("$grule\n");
print ("$num_exact $num_under $num_over $num_none\n");
if (($num_exact==0) && ($num_under==0) && ($num_over==0))
{
$num_exact = $num_under = $num_over = 0;
}
else
{
$exact_induction_ratio = $num_exact / ($num_exact + $num_under + $num_over);
$under_induction_ratio = $num_under / ($num_exact + $num_under + $num_over);
$over_induction_ratio = $num_over / ($num_exact + $num_under + $num_over);;
}
if (($num_exact==0) && ($num_under==0) && ($num_over==0) && ($num_none==0))
{
$current_rule_precision = 0;
}
else
{
$current_rule_precision = ($num_exact+($lambda1*$num_under)+($lambda2*$num_over)) / ($num_exact+$num_under+$num_over+$num_none);
}
$current_rule_recall = ($num_exact+($lambda1*$num_under)+($lambda2*$num_over)) / ($num_exact+$num_under+$num_over+$num_in_groundtruth_not_matched);
## Report metrics using 3-digit precision
##---------------------------------------
$exact_induction_ratio = sprintf("%.3f", $exact_induction_ratio);
$under_induction_ratio = sprintf("%.3f", $under_induction_ratio);
$over_induction_ratio = sprintf("%.3f", $over_induction_ratio);
$current_rule_precision = sprintf("%.3f", $current_rule_precision);
$current_rule_recall = sprintf("%.3f", $current_rule_recall);
print ($grule,",",$exact_induction_ratio,",",$under_induction_ratio,",",$over_induction_ratio,",",$current_rule_precision,",",$current_rule_recall,"\n");
print RES ($grule,",",$exact_induction_ratio,",",$under_induction_ratio,",",$over_induction_ratio,",",$current_rule_precision,",",$current_rule_recall,"\n");
## Update summations for the computation of overall metrics
##---------------------------------------------------------
$total_num_exact += $num_exact;
$total_num_under += $num_under;
$total_num_over += $num_over;
$total_num_none += $num_none;
$total_num_in_groundtruth_not_matched += $num_in_groundtruth_not_matched;
} ## For each groundtruth rule: end
## Compute overall metrics
##------------------------
$total_exact_induction_ratio = $total_num_exact / ($total_num_exact + $total_num_under + $total_num_over);
$total_under_induction_ratio = $total_num_under / ($total_num_exact + $total_num_under + $total_num_over);
$total_over_induction_ratio = $total_num_over/ ($total_num_exact + $total_num_under + $total_num_over);
$pr_enum = 0; ## Enumerator of precision
$pr_denom = 0; ## Denominator of precision
$pr_enum = $total_num_exact + ($lambda1*$total_num_under) + ($lambda2*$total_num_over);
$pr_denom = $total_num_exact + $total_num_under + $total_num_over + $total_num_none;
$total_rule_precision = $pr_enum / $pr_denom;
$rc_enum = 0; ## Enumerator of recall
$rc_denom = 0; ## Denominator of recall
$rc_enum = $total_num_exact + ($lambda1*$total_num_under) + ($lambda2*$total_num_over);
$rc_denom = $total_num_exact + $total_num_under + $total_num_over + $total_num_in_groundtruth_not_matched;
$total_rule_recall = $rc_enum / $rc_denom;
## Report metrics using 3-digit precision
##---------------------------------------
$total_exact_induction_ratio = sprintf("%.3f", $total_exact_induction_ratio);
$total_under_induction_ratio = sprintf("%.3f", $total_under_induction_ratio);
$total_over_induction_ratio = sprintf("%.3f", $total_over_induction_ratio);
$total_rule_precision = sprintf("%.3f", $total_rule_precision);
$total_rule_recall = sprintf("%.3f", $total_rule_recall);
print ("ALL_RULES",",",$total_exact_induction_ratio,",",$total_under_induction_ratio,",",$total_over_induction_ratio,",");
print ($total_rule_precision,",",$total_rule_recall,"\n");
print RES ("ALL_RULES",",",$total_exact_induction_ratio,",",$total_under_induction_ratio,",",$total_over_induction_ratio,",");
print RES ($total_rule_precision,",",$total_rule_recall,"\n");
close (RES);