-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsar_eval.py
730 lines (433 loc) · 24.7 KB
/
tsar_eval.py
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
# evaluation script including encoding utf-8.
# Official evaluation script for TSAR-2022 Shared Task on Lexical Simplification for English, Portuguese and Spanish.
# site: https://taln.upf.edu/pages/tsar2022-st/
import sys
from optparse import OptionParser
import math
class TSAR2022_SharedTask_Evaluator(object):
def __init__(self,output_file,verbose):
self.verbose=verbose
self.output_file=output_file
self.combined = None
self.goldinfo = None
self.flagUseComplexWordsInGoldAnnotations=False
def match(self,label, gold_annotation):
if label == gold_annotation:
return True
return False
def match_group(self,label, gold_annotations):
for g in gold_annotations:
if self.match(label, g):
return True
return False
def match_group_to_group(self,list_labels, gold_annotations):
for label in list_labels:
if self.match_group(label, gold_annotations):
return True
return False
def read_files(self,gold_file, labels_file):
self.gold_file=gold_file
self.labels_file=labels_file
if self.verbose:
print("______________________________________________________")
print("gold_file:",gold_file)
print("labels_file:",labels_file)
print("______________________________________________________")
# Read files
gold_lines = open(gold_file,'r', encoding='utf-8').readlines()
labels_lines = open(labels_file,'r', encoding='utf-8').readlines()
self.goldinfo={}
if self.verbose:
print("______________________________________________________")
print("_____________ GOLD ANNOTATIONS FILE __________________")
print("______________________________________________________")
for line in gold_lines:
split = line.strip().split("\t")
keygold = split[0] + "__" + split[1]
complex_word=split[1]
if self.verbose:
print("keygold:", keygold)
items = [item.strip() for item in split[2:]]
if self.verbose:
print("items:", items)
dict_items={}
for item in items:
if self.flagUseComplexWordsInGoldAnnotations==False:
if item!=complex_word:
if item in dict_items.keys():
dict_items[item]+=1
else:
dict_items[item]=1
else:
if item in dict_items.keys():
dict_items[item]+=1
else:
dict_items[item]=1
if self.verbose:
print("goldinfo ITEMS:", dict_items)
dict_values_items={}
for value in dict_items.values():
dict_values_items[value]=[]
for k in dict_items.keys():
if k not in dict_values_items[dict_items[k]]:
dict_values_items[dict_items[k]].append(k)
list_num_counts_items=sorted(dict_values_items.keys())
list_num_counts_items.reverse()
if self.verbose:
print("list_num_counts_items:", list_num_counts_items)
list_values_items_lists=[]
for key in list_num_counts_items:
list_values_items_lists.append(dict_values_items[key])
if self.verbose:
print("list_values_items_lists:", list_values_items_lists)
self.goldinfo[keygold] = {'gold': dict_items.keys(), 'list_values': list_num_counts_items,'list_keys_lists':list_values_items_lists}
if self.verbose:
print("goldinfo:", self.goldinfo[keygold])
print("______________________________________________________")
self.combined={}
if self.verbose:
print("______________________________________________________")
print("_____________ PREDICTIONS FILE _______________________")
print("______________________________________________________")
for line in labels_lines:
split = line.strip().split("\t")
key = split[0] + "__" + split[1]
items = [item.strip() for item in split[2:]]
if self.verbose:
print("______________________________________________________")
print("original_items:", items)
#filtering out complex words in the predicted labels and repeated predictions
complex_word=split[1]
filtered_items=[]
for item in items:
if item not in filtered_items and not item==complex_word:
filtered_items.append(item)
if self.verbose:
print("filtered_items:", filtered_items)
print("______________________________________________________")
self.combined[key] = { 'labels': filtered_items }
def computeAccuracy_at_1(self):
# Accuracy Metric
if self.verbose:
print()
print()
print("############################")
print(" Accuracy Metric ACC@1")
print("############################")
print()
print("______________________________________________________")
tp = 0
total = 0
for key in self.combined.keys():
if self.combined[key]['labels']!=None and len(self.combined[key]['labels'])>0:
if self.verbose:
print("______________________________________________________")
print("Context = " + str(self.combined[key]))
print("Gold = ",self.goldinfo[key]['gold'])
print("Label = ",self.combined[key]['labels'])
print("Label topk=1 =",self.combined[key]['labels'][0])
sentence,complexWord=key.split('__')
if self.match_group(self.combined[key]['labels'][0],self.goldinfo[key]['gold']):
if self.verbose:
print("MATCH @acc1_set_gold=",self.combined[key]['labels'][0])
print("______________________________________________________")
tp += 1
elif self.verbose:
print("NO MATCH\n")
print("______________________________________________________")
total += 1
if self.verbose:
print("TOTALMATCHES acc@1: ",str(tp))
print("______________________________________________________")
accuracy = tp/total
return accuracy
def computeAccuracy_at_N_at_top_gold_1(self,N):
# Accuracy Metric
if self.verbose:
print()
print()
print("#############################################")
print(" Accuracy Metric @",N,"@ GOLD FIRST MATCH")
print("#############################################")
print()
print("______________________________________________________")
tp = 0
total = 0
for key in self.combined.keys():
if self.combined[key]['labels']!=None and len(self.combined[key]['labels'])>0:
if self.verbose:
print("______________________________________________________")
print("Context = " + str(self.combined[key]))
print("Gold = ",self.goldinfo[key]['gold'])
print("Label = ",self.combined[key]['labels'])
print("Label topN=",N," ",self.combined[key]['labels'][0:N])
print("Gold topk=1 list =", self.goldinfo[key]['list_keys_lists'][0])
sentence,complexWord=key.split('__')
if self.match_group_to_group(self.combined[key]['labels'][0:N],self.goldinfo[key]['list_keys_lists'][0]):
if self.verbose:
print("MATCH acc@N_gold_first=",self.combined[key]['labels'][0:N])
print("______________________________________________________")
tp += 1
elif self.verbose:
print("NO MATCH\n")
print("______________________________________________________")
total += 1
if self.verbose:
print("TOTALMATCHES accuracy@",N,"_at_top_gold_1:",str(tp))
print("______________________________________________________")
accuracy = tp/total
return accuracy
def computePrecisionMetrics_at_K(self,K):
# Precision
if self.verbose:
print()
print()
print("##############################################################################################")
print(" Potential, MicroAverage and MacroAverage Precision, Recall and F1 Metrics at ",K)
print("##############################################################################################")
print()
MacroAverage_Precision = 0
MicroAverage_Precision = 0
MacroAverage_Recall = 0
MicroAverage_Recall = 0
MacroAverage_F1 = 0
MicroAverage_F1 = 0
running_precision = 0
total_counts_precision=0
running_recall = 0
total_counts_recall=0
total_counts_labels=0
potential_counts=0
Potential=0
total = 0
for key in self.combined.keys():
if self.combined[key]['labels']!=None and len(self.combined[key]['labels'])>0:
if self.verbose:
print("______________________________________________________")
print("Context = " + str(self.combined[key]))
print("Gold = " + str(self.goldinfo[key]['gold']))
print("Label = " + str(self.combined[key]['labels']))
gold_annotations= self.goldinfo[key]['gold']
labels = self.combined[key]['labels'][0:K]
total_counts_labels+=len(labels)
sentence,complexWord=key.split('__')
acc_labels = [l for l in labels if self.match_group( l, gold_annotations)]
acc_gold = [l for l in gold_annotations if self.match_group(l, labels)]
total_counts_precision+=len(acc_labels)
if len(acc_labels)>0:
potential_counts+=1
total_counts_recall+=len(acc_gold)
precision = len(acc_labels) / len(labels)
recall = len(acc_gold) / len(gold_annotations)
if self.verbose:
print("Matched Labels with respect Gold = " +str(acc_labels))
print("Matched Gold with respect Labels = " +str(acc_gold))
print("Precision = " + str(precision))
print("Recall = " + str(recall)+"\n")
print("______________________________________________________")
running_precision += precision
running_recall += recall
total += 1
MacroAverage_Precision = running_precision / total
MacroAverage_Recall = running_recall / total
if total_counts_labels>0:
MicroAverage_Precision= total_counts_precision / total_counts_labels
MicroAverage_Recall = total_counts_recall / total_counts_labels
MacroAverage_F1=0
MicroAverage_F1=0
if (MacroAverage_Precision+MacroAverage_Recall)>0:
MacroAverage_F1 = 2*MacroAverage_Precision*MacroAverage_Recall/(MacroAverage_Precision+MacroAverage_Recall)
MicroAverage_F1 = 2*MicroAverage_Precision*MicroAverage_Recall/(MicroAverage_Precision+MicroAverage_Recall)
if (potential_counts>0):
Potential=potential_counts/total
return MacroAverage_Precision,MacroAverage_Recall,MacroAverage_F1,MicroAverage_Precision,MicroAverage_Recall,MicroAverage_F1, Potential
#Mean Average Precision
# Parameters :
# 1. List of Binary Relevance Judgments e.g. [False, True, True, False, False]
# 2. K
def MAP_at_K(self,list_gold_items_match,K):
MAP = 0
AP = 0
TruePositivesSeen=0
index=0
list_precision_calculations=[]
for item in list_gold_items_match:
index+=1
if index>K:
break
if item==True:
TruePositivesSeen+=1
precision=TruePositivesSeen/index
list_precision_calculations.append(precision)
AP+=precision
else:
list_precision_calculations.append(0)
MAP=AP/K
if self.verbose:
print("list_precision_calculations:", list_precision_calculations)
print("AP:", AP)
print("K:", K)
print("MAP:", MAP)
print("______________________________________________________")
return MAP
def computeMAP_at_K(self,K):
# MAP metric
if self.verbose:
print()
print()
print("###########################################################################")
print(" MeanAveragePrecision (MAP) metric @",K)
print("###########################################################################")
print()
total_instances=0
MAP_global_accumulator=0
MAP=0
for key in self.combined.keys():
if self.verbose:
print("______________________________________________________")
print("Context = " + str(self.combined[key]))
print("Gold = " + str(self.goldinfo[key]['gold']))
print("Label = " + str(self.combined[key]['labels']))
gold = set(self.goldinfo[key]['gold'])
labels = set(self.combined[key]['labels'])
list_labels=self.combined[key]['labels']
gold_annotations=self.goldinfo[key]['gold']
#########################################
# MAP
#########################################
if self.verbose:
print("MAP")
print("labels:",list_labels)
print("gold:", gold_annotations)
sentence,complexWord=key.split('__')
labels_relevance_judgements=[]
for label in list_labels:
labels_relevance_judgements.append(self.match_group(label, gold_annotations))
if self.verbose:
print("labels_relevance_judgements:")
print(labels_relevance_judgements)
MAP_local=self.MAP_at_K(labels_relevance_judgements,K)
MAP_global_accumulator+=MAP_local
total_instances+=1
if (MAP_global_accumulator>0):
MAP=MAP_global_accumulator/total_instances
return MAP
def print_output_results(self, Potential_at_1,
Potential_at_3,
Potential_at_5,
Potential_at_10,
MAP_at_3,
MAP_at_5,
MAP_at_10,
Accuracy_at_1_at_top_gold_1,
Accuracy_at_2_at_top_gold_1,
Accuracy_at_3_at_top_gold_1):
print("========= EVALUATION config.=========")
print('GOLD file = ' + str(self.gold_file))
print('PREDICTION LABELS file = ' + str(self.labels_file))
print('OUTPUT file = ' + str(self.output_file))
print("=============== RESULTS =============\n")
print('MAP@1/Potential@1/Precision@1 = ' + str(Potential_at_1)+"\n")
print('MAP@3 = ' + str(MAP_at_3))
print('MAP@5 = ' + str(MAP_at_5))
print('MAP@10 = ' + str(MAP_at_10)+"\n")
print('Potential@3 = ' + str(Potential_at_3))
print('Potential@5 = ' + str(Potential_at_5))
print('Potential@10 = ' + str(Potential_at_10)+"\n")
print('Accuracy@1@top_gold_1 = ' + str(Accuracy_at_1_at_top_gold_1))
print('Accuracy@2@top_gold_1 = ' + str(Accuracy_at_2_at_top_gold_1))
print('Accuracy@3@top_gold_1 = ' + str(Accuracy_at_3_at_top_gold_1))
print('\n')
def write_output_results(self, Potential_at_1,
Potential_at_3,
Potential_at_5,
Potential_at_10,
MAP_at_3,
MAP_at_5,
MAP_at_10,
Accuracy_at_1_at_top_gold_1,
Accuracy_at_2_at_top_gold_1,
Accuracy_at_3_at_top_gold_1):
out_file = open(self.output_file,mode='w',encoding='utf-8')
out_file.write("========= EVALUATION config.=========\n")
out_file.write('GOLD file = ' + str(self.gold_file)+'\n')
out_file.write('PREDICTION LABELS file = ' + str(self.labels_file)+'\n')
out_file.write('OUTPUT file = ' + str(self.output_file)+'\n')
out_file.write("=============== RESULTS =============\n")
out_file.write('MAP@1/Potential@1/Precision@1 = ' + str(Potential_at_1)+'\n\n')
out_file.write('MAP@3 = ' + str(MAP_at_3)+'\n')
out_file.write('MAP@5 = ' + str(MAP_at_5)+'\n')
out_file.write('MAP@10 = ' + str(MAP_at_10)+'\n\n')
out_file.write('Potential@3 = ' + str(Potential_at_3)+'\n')
out_file.write('Potential@5 = ' + str(Potential_at_5)+'\n')
out_file.write('Potential@10 = ' + str(Potential_at_10)+'\n\n')
out_file.write('Accuracy@1@top_gold_1 = ' + str(Accuracy_at_1_at_top_gold_1)+'\n')
out_file.write('Accuracy@2@top_gold_1 = ' + str(Accuracy_at_2_at_top_gold_1)+'\n')
out_file.write('Accuracy@3@top_gold_1 = ' + str(Accuracy_at_3_at_top_gold_1)+'\n\n')
out_file.write('________________________________\n')
out_file.close()
def main():
# arg parsing
parser = OptionParser(usage='Evaluation Script for the TSAR-2022 Lexical Simplification Shared Task\n\nUsage: %prog <options>')
parser.add_option('--gold_file', metavar='<PATH>', action='store', type='string', dest='gold_file', default='', help='The path to the file with the gold annotated instances')
parser.add_option('--predictions_file', metavar='<PATH>', action='store', type='string', dest='predictions_file', default='', help='The path to file with the predictions')
parser.add_option('--output_file', metavar='<PATH>', action='store', type='string',dest='output_file', default='', help='path to the output file')
parser.add_option('--verbose', help='Verbose output mode', action='store_true')
(options, args) = parser.parse_args(sys.argv)
if (options.gold_file==''):
print("Error: input path to the file with gold annotations is missing!")
print(parser.print_help())
sys.exit(1)
if (options.predictions_file==''):
print("Error: input path to the predictions file is missing!")
print(parser.print_help())
sys.exit(1)
if (options.output_file==''):
print("Error: path to the output file is missing!")
print(parser.print_help())
sys.exit(1)
evaluator=TSAR2022_SharedTask_Evaluator(options.output_file,options.verbose)
evaluator.read_files(options.gold_file,options.predictions_file)
Accuracy_at_1_at_top_gold_1=evaluator.computeAccuracy_at_N_at_top_gold_1(1)
Accuracy_at_2_at_top_gold_1=evaluator.computeAccuracy_at_N_at_top_gold_1(2)
Accuracy_at_3_at_top_gold_1=evaluator.computeAccuracy_at_N_at_top_gold_1(3)
MacroAverage_Precision_at_1, MacroAverage_Recall_at_1, MacroAverage_F1_at_1, MicroAverage_Precision_at_1, MicroAverage_Recall_at_1, MicroAverage_F1_at_1, Potential_at_1=evaluator.computePrecisionMetrics_at_K(1)
MacroAverage_Precision_at_3, MacroAverage_Recall_at_3, MacroAverage_F1_at_3, MicroAverage_Precision_at_3, MicroAverage_Recall_at_3, MicroAverage_F1_at_3, Potential_at_3=evaluator.computePrecisionMetrics_at_K(3)
MacroAverage_Precision_at_5, MacroAverage_Recall_at_5, MacroAverage_F1_at_5, MicroAverage_Precision_at_5, MicroAverage_Recall_at_5, MicroAverage_F1_at_5, Potential_at_5=evaluator.computePrecisionMetrics_at_K(5)
MacroAverage_Precision_at_10, MacroAverage_Recall_at_10, MacroAverage_F1_at_10, MicroAverage_Precision_at_10, MicroAverage_Recall_at_10, MicroAverage_F1_at_10, Potential_at_10=evaluator.computePrecisionMetrics_at_K(10)
MAP_at_3=evaluator.computeMAP_at_K(3)
MAP_at_5=evaluator.computeMAP_at_K(5)
MAP_at_10=evaluator.computeMAP_at_K(10)
factor=10000 #floor 4 decimal numbers
Accuracy_at_1_at_top_gold_1_floored=math.floor(Accuracy_at_1_at_top_gold_1 * factor) / factor
Accuracy_at_2_at_top_gold_1_floored=math.floor(Accuracy_at_2_at_top_gold_1 * factor) / factor
Accuracy_at_3_at_top_gold_1_floored=math.floor(Accuracy_at_3_at_top_gold_1 * factor) / factor
Potential_at_1_floored=math.floor(Potential_at_1 * factor) / factor
Potential_at_3_floored=math.floor(Potential_at_3 * factor) / factor
Potential_at_5_floored=math.floor(Potential_at_5 * factor) / factor
Potential_at_10_floored=math.floor(Potential_at_10 * factor) / factor
MAP_at_3_floored=math.floor(MAP_at_3 * factor) / factor
MAP_at_5_floored=math.floor(MAP_at_5 * factor) / factor
MAP_at_10_floored=math.floor(MAP_at_10 * factor) / factor
evaluator.print_output_results(Potential_at_1_floored,
Potential_at_3_floored,
Potential_at_5_floored,
Potential_at_10_floored,
MAP_at_3_floored,
MAP_at_5_floored,
MAP_at_10_floored,
Accuracy_at_1_at_top_gold_1_floored,
Accuracy_at_2_at_top_gold_1_floored,
Accuracy_at_3_at_top_gold_1_floored)
evaluator.write_output_results(Potential_at_1_floored,
Potential_at_3_floored,
Potential_at_5_floored,
Potential_at_10_floored,
MAP_at_3_floored,
MAP_at_5_floored,
MAP_at_10_floored,
Accuracy_at_1_at_top_gold_1_floored,
Accuracy_at_2_at_top_gold_1_floored,
Accuracy_at_3_at_top_gold_1_floored)
if __name__ == '__main__':
main()