forked from PlanTL-GOB-ES/SPACCC_MEDDOCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.py
executable file
·558 lines (435 loc) · 18 KB
/
classes.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
import os
from tags import PHITag
from xml.etree import ElementTree
class Annotation(object):
def __init__(self, file_name=None, root="root"):
self.doc_id = ''
self.sys_id = ''
self.text = None
self.num_sentences = None
self.root = root
self.sensitive_spans = []
self.sensitive_spans_merged = []
self.verbose = False
if file_name:
self.sys_id = os.path.basename(os.path.dirname(file_name))
self.doc_id = os.path.splitext(os.path.basename(file_name))[0]
else:
self.doc_id = None
@property
def id(self):
return self.doc_id
def get_phi(self):
return self.phi
def get_phi_spans(self):
return self.sensitive_spans
def get_phi_spans_merged(self):
return self.sensitive_spans_merged
def get_phi_spans_joins(self):
return self.sensitive_spans_joins
def get_number_sentences(self):
import os
try:
self.num_sentences = \
sum(1 for line in open(os.path.join(os.path.dirname(os.path.abspath(__file__)),'annotated_corpora/sentence_splitted/') +
self.doc_id +
".ann"))
except IOError:
print("File '" +
'freeling/sentence_splitted/' +
self.doc_id +
".ann' not found.")
return self.num_sentences
def add_spans(self, phi_tags):
for tag in sorted(phi_tags):
self.sensitive_spans.append(tag)
for y in sorted(phi_tags):
if not self.sensitive_spans_merged:
self.sensitive_spans_merged.append(y)
else:
x = self.sensitive_spans_merged.pop()
if self.is_all_non_alphanumeric(self.text[x[1]:y[0]]):
self.sensitive_spans_merged.append((x[0], y[1]))
else:
self.sensitive_spans_merged.append(x)
self.sensitive_spans_merged.append(y)
@staticmethod
def is_all_non_alphanumeric(string):
for i in string:
if i.isalnum():
return False
return True
class i2b2Annotation(Annotation):
""" This class models the i2b2 annotation format."""
def __init__(self, file_name=None, root="root"):
self.doc_id = ''
self.sys_id = ''
self.text = None
self.num_sentences = None
self.root = root
self.phi = []
self.sensitive_spans = []
self.sensitive_spans_merged = []
self.verbose = False
if file_name:
self.sys_id = os.path.basename(os.path.dirname(file_name))
self.doc_id = os.path.splitext(os.path.basename(file_name))[0]
self.parse_text_and_tags(file_name)
self.parse_text_and_spans(file_name)
self.file_name = file_name
else:
self.doc_id = None
def normalize_tags(self,root):
root.tag = root.tag.upper()
for child in root:
self.normalize_tags(child)
def normalize_attr(self,root):
for attr, value in root.attrib.items():
norm_attr = attr.upper()
if norm_attr != attr:
root.set(norm_attr, value)
root.attrib.pop(attr)
for child in root:
self.normalize_attr(child)
def parse_text_and_tags(self, file_name=None):
if file_name is not None:
text = open(file_name, 'r').read()
self.text = text
tree = ElementTree.parse(file_name)
root = tree.getroot()
self.normalize_tags(root)
self.root = root.tag
try:
self.text = root.find("TEXT").text
except AttributeError:
self.text = None
# Handles files where PHI, and AnnotatorTags are all just
# stuffed into tag element.
for t, cls in PHITag.tag_types.items():
if len(root.find("TAGS").findall(t)):
for element in root.find("TAGS").findall(t):
self.phi.append(cls(element))
def parse_text_and_spans(self, file_name=None):
if file_name is not None:
text = open(file_name, 'r').read()
self.text = text
tree = ElementTree.parse(file_name)
root = tree.getroot()
self.normalize_tags(root)
self.root = root.tag
try:
self.text = root.find("TEXT").text
except AttributeError:
self.text = None
# Fill list with tuples (start, end) for each annotation
phi_tags = []
for t, cls in PHITag.tag_types.items():
if len(root.find("TAGS").findall(t)):
for element in root.find("TAGS").findall(t):
phi_tags.append((cls(element).get_start(), cls(element).get_end()))
# Store spans
self.add_spans(phi_tags)
class BratAnnotation(Annotation):
""" This class models the BRAT annotation format."""
def __init__(self, file_name=None, root="root"):
self.doc_id = ''
self.sys_id = ''
self.text = None
self.num_sentences = None
self.root = root
self.phi = []
self.sensitive_spans = []
self.sensitive_spans_merged = []
self.verbose = False
if file_name:
self.sys_id = os.path.basename(os.path.dirname(file_name))
self.doc_id = os.path.splitext(os.path.basename(file_name))[0]
self.parse_text_and_tags(file_name)
self.parse_text_and_spans(file_name)
self.file_name = file_name
else:
self.doc_id = None
def parse_text_and_tags(self, file_name=None):
if file_name is not None:
text = open(os.path.splitext(file_name)[0] + '.txt', 'r').read()
self.text = text
for row in open(file_name, 'r'):
line = row.strip()
if line.startswith("T"): # Lines is a Brat TAG
try:
label = line.split("\t")[1].split()
tag = label[0]
start = int(label[1])
end = int(label[2])
self.phi.append((tag, start, end))
except IndexError:
print("ERROR! Index error while splitting sentence '" +
line + "' in document '" + file_name + "'!")
else: # Line is a Brat comment
if self.verbose:
print("\tSkipping line (comment):\t" + line)
def parse_text_and_spans(self, file_name=None):
if file_name is not None:
text = open(os.path.splitext(file_name)[0] + '.txt', 'r').read()
self.text = text
phi_tags = []
for row in open(file_name, 'r'):
line = row.strip()
if line.startswith("T"): # Lines is a Brat TAG
try:
label = line.split("\t")[1].split()
start = int(label[1])
end = int(label[2])
phi_tags.append((start, end))
except IndexError:
print("ERROR! Index error while splitting sentence '" +
line + "' in document '" + file_name + "'!")
else: # Line is a Brat comment
if self.verbose:
print("\tSkipping line (comment):\t" + line)
# Store spans
self.add_spans(phi_tags)
class Evaluate(object):
"""Base class with all methods to evaluate the different subtracks."""
def __init__(self, sys_ann, gs_ann):
self.tp = []
self.fp = []
self.fn = []
self.doc_ids = []
self.verbose = False
self.sys_id = sys_ann[list(sys_ann.keys())[0]].sys_id
@staticmethod
def get_tagset_ner(annotation):
return annotation.get_phi()
@staticmethod
def get_tagset_span(annotation):
return annotation.get_phi_spans()
@staticmethod
def get_tagset_span_merged(annotation):
return annotation.get_phi_spans_merged()
@staticmethod
def is_contained(content, container):
for element in sorted(container):
if content[0] >= element[0] and content[1] <= element[1]:
return True
return False
@staticmethod
def recall(tp, fn):
try:
return len(tp) / float(len(fn) + len(tp))
except ZeroDivisionError:
return 0.0
@staticmethod
def precision(tp, fp):
try:
return len(tp) / float(len(fp) + len(tp))
except ZeroDivisionError:
return 0.0
@staticmethod
def F_beta(p, r, beta=1):
try:
return (1 + beta**2) * ((p * r) / (p + r))
except ZeroDivisionError:
return 0.0
def micro_recall(self):
try:
return sum([len(t) for t in self.tp]) / \
float(sum([len(t) for t in self.tp]) +
sum([len(t) for t in self.fn]))
except ZeroDivisionError:
return 0.0
def micro_precision(self):
try:
return sum([len(t) for t in self.tp]) / \
float(sum([len(t) for t in self.tp]) +
sum([len(t) for t in self.fp]))
except ZeroDivisionError:
return 0.0
def _print_docs(self):
for i, doc_id in enumerate(self.doc_ids):
mp = Evaluate.precision(self.tp[i], self.fp[i])
mr = Evaluate.recall(self.tp[i], self.fn[i])
str_fmt = "{:<35}{:<15}{:<20}"
print(str_fmt.format(doc_id,
"Precision",
"{:.4}".format(mp)))
print(str_fmt.format("",
"Recall",
"{:.4}".format(mr)))
print(str_fmt.format("",
"F1",
"{:.4}".format(Evaluate.F_beta(mp, mr))))
print("{:-<60}".format(""))
def _print_summary(self, file_W):
mp = self.micro_precision()
mr = self.micro_recall()
if self.label == "SubTrack 2 [strict]":
file_W.write("Subtask2Strict_Precision : {}\n".format(mp))
file_W.write("Subtask2Strict_Recall : {}\n".format(mr))
file_W.write("Subtask2Strict_F1 : {}\n".format(Evaluate.F_beta(mr, mp)))
print("Subtask2Strict_Precision : {}\n".format(mp))
print("Subtask2Strict_Recall : {}\n".format(mr))
print("Subtask2Strict_F1 : {}\n".format(Evaluate.F_beta(mr, mp)))
elif self.label == "SubTrack 2 [merged]":
file_W.write("Subtask2Merged_Precision : {}\n".format(mp))
file_W.write("Subtask2Merged_Recall : {}\n".format(mr))
file_W.write("Subtask2Merged_F1 : {}\n".format(Evaluate.F_beta(mr, mp)))
print("Subtask2Merged_Precision : {}\n".format(mp))
print("Subtask2Merged_Recall : {}\n".format(mr))
print("Subtask2Merged_F1 : {}\n".format(Evaluate.F_beta(mr, mp)))
def print_docs(self):
print("\n")
print("Report ({}):".format(self.sys_id))
print("{:-<60}".format(""))
print("{:<35}{:<15}{:<20}".format("Document ID", "Measure", "Micro"))
print("{:-<60}".format(""))
self._print_docs()
def print_report(self, file_W):
self._print_summary(file_W)
class EvaluateSubtrack1(Evaluate):
"""Class for running the NER evaluation."""
def __init__(self, sys_sas, gs_sas):
self.tp = []
self.fp = []
self.fn = []
self.num_sentences = []
self.doc_ids = []
self.verbose = False
self.sys_id = sys_sas[list(sys_sas.keys())[0]].sys_id
self.label = "Subtrack 1 [NER]"
for doc_id in sorted(list(set(sys_sas.keys()) & set(gs_sas.keys()))):
gold = set(self.get_tagset_ner(gs_sas[doc_id]))
sys = set(self.get_tagset_ner(sys_sas[doc_id]))
num_sentences = self.get_num_sentences(sys_sas[doc_id])
self.tp.append(gold.intersection(sys))
self.fp.append(sys - gold)
self.fn.append(gold - sys)
self.num_sentences.append(num_sentences)
self.doc_ids.append(doc_id)
@staticmethod
def get_num_sentences(annotation):
return annotation.get_number_sentences()
@staticmethod
def leak_score(fn, num_sentences):
try:
return float(len(fn) / num_sentences)
except ZeroDivisionError:
return 0.0
except TypeError:
return "NA"
def micro_leak(self):
try:
return float(sum([len(t) for t in self.fn]) / sum(t for t in self.num_sentences))
except ZeroDivisionError:
return 0.0
except TypeError as error:
print(error)
return 'NA'
def _print_docs(self):
for i, doc_id in enumerate(self.doc_ids):
mp = EvaluateSubtrack1.precision(self.tp[i], self.fp[i])
mr = EvaluateSubtrack1.recall(self.tp[i], self.fn[i])
leak = EvaluateSubtrack1.leak_score(self.fn[i], self.num_sentences[i])
str_fmt = "{:<35}{:<15}{:<20}"
print(str_fmt.format(doc_id,
"Leak",
"{:.4}".format(leak)))
print(str_fmt.format("",
"Precision",
"{:.4}".format(mp)))
print(str_fmt.format("",
"Recall",
"{:.4}".format(mr)))
print(str_fmt.format("",
"F1",
"{:.4}".format(Evaluate.F_beta(mp, mr))))
print("{:-<60}".format(""))
def _print_summary(self, file_W):
mp = self.micro_precision()
mr = self.micro_recall()
ml = self.micro_leak()
file_W.write("Subtask1_Leak : {} \n".format(ml))
file_W.write("Subtask1_Precision : {}\n".format(mp))
file_W.write("Subtask1_Recall : {}\n".format(mr))
file_W.write("Subtask1_F1 : {}\n".format(Evaluate.F_beta(mr, mp)))
print("Subtask1_Leak : {}\n".format(ml))
print("Subtask1_Precision : {}\n".format(mp))
print("Subtask1_Recall : {}\n".format(mr))
print("Subtask1_F1 : {}\n".format(Evaluate.F_beta(mr, mp)))
class EvaluateSubtrack2(Evaluate):
"""Class for running the SPAN evaluation with strict span mode."""
def __init__(self, sys_sas, gs_sas):
self.tp = []
self.fp = []
self.fn = []
self.doc_ids = []
self.verbose = False
self.sys_id = sys_sas[list(sys_sas.keys())[0]].sys_id
self.label = "Subtrack 2 [strict]"
for doc_id in sorted(list(set(sys_sas.keys()) & set(gs_sas.keys()))):
gold = set(self.get_tagset_span(gs_sas[doc_id]))
sys = set(self.get_tagset_span(sys_sas[doc_id]))
self.tp.append(gold.intersection(sys))
self.fp.append(sys - gold)
self.fn.append(gold - sys)
self.doc_ids.append(doc_id)
class EvaluateSubtrack2merged(Evaluate):
"""Class for running the SPAN evaluation with merged spans mode."""
def __init__(self, sys_sas, gs_sas):
self.tp = []
self.fp = []
self.fn = []
self.doc_ids = []
self.verbose = False
self.sys_id = sys_sas[list(sys_sas.keys())[0]].sys_id
self.label = "Subtrack 2 [merged]"
for doc_id in sorted(list(set(sys_sas.keys()) & set(gs_sas.keys()))):
gold_strict = set(self.get_tagset_span(gs_sas[doc_id]))
sys_strict = set(self.get_tagset_span(sys_sas[doc_id]))
gold_merged = set(self.get_tagset_span_merged(gs_sas[doc_id]))
sys_merged = set(self.get_tagset_span_merged(sys_sas[doc_id]))
intersection = gold_strict.intersection(sys_strict).union(gold_merged.intersection(sys_merged))
fp = sys_strict - gold_strict
for tag in sys_strict:
if self.is_contained(tag, intersection):
if tag in fp:
fp.remove(tag)
fn = gold_strict - sys_strict
for tag in gold_strict:
if self.is_contained(tag, intersection):
if tag in fn:
fn.remove(tag)
self.tp.append(intersection)
self.fp.append(fp)
self.fn.append(fn)
self.doc_ids.append(doc_id)
class MeddocanEvaluation(object):
"""Base class for running the evaluations."""
def __init__(self):
self.evaluations = []
def add_eval(self, e, label=""):
e.sys_id = "SYSTEM: " + e.sys_id
e.label = label
self.evaluations.append(e)
def print_docs(self):
for e in self.evaluations:
e.print_docs()
def print_report(self, file_W=None):
for e in self.evaluations:
e.print_report(file_W)
class NER_Evaluation(MeddocanEvaluation):
"""Class for running the NER evaluation (Subtrack 1)."""
def __init__(self, annotator_cas, gold_cas, **kwargs):
self.evaluations = []
# Basic Evaluation
self.add_eval(EvaluateSubtrack1(annotator_cas, gold_cas, **kwargs),
label="SubTrack 1 [NER]")
class Span_Evaluation(MeddocanEvaluation):
"""Class for running the SPAN evaluation (Subtrack 2). Calls to 'strict'
and 'merged' evaluations. """
def __init__(self, annotator_cas, gold_cas, **kwargs):
self.evaluations = []
self.add_eval(EvaluateSubtrack2(annotator_cas, gold_cas, **kwargs),
label="SubTrack 2 [strict]")
self.add_eval(EvaluateSubtrack2merged(annotator_cas, gold_cas, **kwargs),
label="SubTrack 2 [merged]")