-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwnuteval.py
556 lines (436 loc) · 14.4 KB
/
wnuteval.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
#!/usr/bin/env python
# Python version of the evaluation script from CoNLL'00-
# Originates from: https://github.com/spyysalo/conlleval.py
# modified to give overlap in surface forms
# Intentional differences:
# - accept any space as delimiter by default
# - optional file argument (default STDIN)
# - option to set boundary (-b argument)
# - LaTeX output (-l argument) not supported
# - raw tags (-r argument) not supported
"""
This is a from scratch rewrite of conlleval.py from Marek Rei, for the WNUT'17 shared task
that is more picky about ill-formed data.
Two metrics are included: entity performance p/r/f and surface form performance p/r/f
Surface forms compares the set of unique surface forms found in the gold data
with those returned by the system, and scores systems based on how well they
found individual surface forms.
Eric Nichols
"""
from __future__ import print_function
from collections import defaultdict, namedtuple
import fileinput
import sys
from functools import reduce
def get_sents(lines):
"""
Args:
lines (Iterable[str]): the lines
Yields:
List[str]: the lines delimited by an empty line
"""
sent = []
stripped_lines = (line.strip() for line in lines)
for line in stripped_lines:
if line == '':
yield sent
sent = []
else:
sent.append(line)
yield sent
Token = namedtuple('Token', 'sent_id word_id word bio tag')
wnut_bio = ('B', 'I', 'O')
wnut_tags = ('corporation', 'creative-work', 'group', 'location', 'person', 'product')
def make_tok(word, bio_tag, sent_id=-1, word_id=-1):
"""
Args:
word (str): the surface form of the word
bio_tag (str): the tag with BIO annotation
sent_id (int): the sentence ID
word_id (int): the word ID
Returns:
Token
Raises:
ValueError
"""
if bio_tag == 'O':
bio, tag = 'O', 'O'
else:
bio, tag = bio_tag.split('-', 1)
if bio not in wnut_bio or tag not in wnut_tags:
raise ValueError('Invalid tag: %s %s %d %d' % (word, bio_tag, sent_id, word_id))
return Token(sent_id, word_id, word, bio, tag)
def token_to_conll(tok):
"""
Args:
tok (Token):
Returns:
str:
"""
return '%s\t%s' % (tok.word, tok.tag if tok.tag == 'O' else '%s-%s' % (tok.bio, tok.tag))
def line_to_toks(line, sent_id=-1, word_id=-1):
"""
Args:
line (str): the input line
sent_id (int): the current sentence ID
word_id (int): the current word ID
Returns:
Dictionary[str,Token]: the gold and guess tokens stored in a dict with keys for gold and guess
Raises:
ValueError
"""
def make_lbl(i):
return 'gold' if i == 0 else 'sys_%d' % i
try:
fields = line.split('\t')
word = fields[0]
return {make_lbl(i): make_tok(word, bio_tag, sent_id, word_id)
for i, bio_tag in enumerate(fields[1:])}
except ValueError:
raise ValueError('Invalid line: %s %d %d' % (line, sent_id, word_id))
def sent_to_toks(sent, sent_id=-1):
"""
Args:
sent (Iterator[str]): the lines that comprise a sentence
sent_id (int): the sentence ID
Returns:
Dictionary[str,List[Token]]: the gold and guess tokens for each word in the sentence,
stored in a dict with keys for gold and guess
"""
toks = defaultdict(list)
for word_id, line in enumerate(sent):
for src, tok in line_to_toks(line, sent_id, word_id).items():
toks[src].append(tok)
return toks
Entity = namedtuple('Entity', 'words sent_id word_id_start word_id_stop tag')
def entity_to_tokens(entity):
"""
Args:
entity (Entity):
Returns:
List[Token]:
"""
def get_bio(_i):
if entity.tag == 'O':
return 'O'
elif _i == 0:
return 'B'
else:
return 'I'
return [Token(entity.sent_id, entity.word_id_start + i, word, get_bio(i), entity.tag)
for i, word in enumerate(entity.words)]
def entity_to_conll(entity):
"""
Args:
entity (Entity):
Returns:
List[str]: a conll-formatted token tag
"""
return [token_to_conll(tok) for tok in entity_to_tokens(entity)]
def get_phrases(entities):
"""
Args:
entities (Iterable[Entity]):
Returns:
Set[Tuple[str]]
"""
return {entity.words for entity in entities}
def get_phrases_and_tags(entities):
"""
Args:
entities (Iterable[Entity]):
Returns:
Set[Tuple[Tuple[str],str]]:
"""
return {(entity.words, entity.tag) for entity in entities}
def toks_to_entities(toks):
"""
Args:
toks (Iterable[Token]): the tokens in a sentence
Returns:
Iterable[Entity]: the corresponding entities in a sentence
Raises:
ValueError
"""
def make_entity(tok):
return Entity((tok.word, ), tok.sent_id, tok.word_id, tok.word_id+1, tok.tag)
def extend_entity(entity, tok):
return Entity(entity.words + (tok.word, ), entity.sent_id, entity.word_id_start, tok.word_id+1, entity.tag)
def reducer(_entities, tok):
last = _entities.pop()
if tok.bio == 'I' and tok.tag == last.tag:
entity = extend_entity(last, tok)
_entities.append(entity)
elif tok.bio == 'B' or (tok.bio == 'O' and tok.tag == 'O'):
entity = make_entity(tok)
_entities.extend([last, entity])
# invalid token sequence tag1 => I-tag2: interpret as tag1 => B-tag2
elif tok.bio == 'I' and tok.tag != last.tag:
# print('Invalid tag sequence: %s => %s' % (last, tok), file=sys.stderr)
entity = make_entity(tok)
_entities.extend([last, entity])
else:
raise ValueError('Invalid tag sequence: %s %s' % (last, tok))
return _entities
return reduce(reducer, toks[1:], [make_entity(toks[0]), ])
def non_other(entity):
# type: (Entity) -> bool
"""
Args:
entity (Entity):
Returns:
bool
"""
return entity.tag != 'O'
def filter_entities(entities, p):
"""
Args:
entities (Iterable[Entity]): the entities in a sentence
p (Call[[Entity],bool): the predicate
Returns:
List(Entity): the entities filtered by predicate p
"""
return [entity for entity in entities if p(entity)]
def drop_other_entities(entities):
"""
Args:
entities (Iterable[Entity]):
Returns:
Iterator[Entity]
"""
return filter_entities(entities, non_other)
def doc_to_tokses(lines):
"""
Args:
lines (Iterable[str]): the lines in a document
Returns:
Dictionary[str,List[List[Tokens]]]: a nested list of list of tokens,
with one list for each sentence, stored in a dict with keys for gold and guess
"""
sents = get_sents(lines)
tokses = defaultdict(list)
for sent_id, sent in enumerate(sents):
for src, toks in sent_to_toks(sent, sent_id).items():
tokses[src].append(toks)
return tokses
def flatten(nested):
"""
Args:
nested (Iterable[Iterable[T]]): a nested iterator
Returns:
List[T]: the iterator flattened into a list
"""
return [x for xs in nested for x in xs]
def doc_to_toks(lines):
"""
Args:
lines (Iterator[str]): the lines in a document
Returns:
Dictionary[str,List[Tokens]]: a lists of all tokens in the document,
stored in a dict with keys for gold and guess
"""
return {src: flatten(nested)
for src, nested in doc_to_tokses(lines).items()}
def doc_to_entitieses(lines):
"""
Args:
lines (Iterator[str]): the lines in a document
Returns:
Dictionary[str,List[List[Entity]]]: a nested list of lists of entities,
stored in a dict with keys for gold and guess
"""
entitieses = defaultdict(list)
for src, tokses in doc_to_tokses(lines).items():
entitieses[src] = [toks_to_entities(toks) for toks in tokses]
return entitieses
def doc_to_entities(lines):
"""
Args:
lines (Iterator[str]): the lines in a document
Returns:
Dictionary[str,List[Entities]]: a lists of all entities in the document,
stored in a dict with keys for gold and guess
"""
return {src: flatten(nested)
for src, nested in doc_to_entitieses(lines).items()}
def get_tags(entities):
"""
Args:
entities (Iterable[Entity]): the entities in a sentence
Returns:
Set[str]: a set of their tags, excluding 'O'
"""
return {entity.tag for entity in entities} - {'O'}
Results = namedtuple('Results', 'gold guess correct p r f')
def get_tagged_entities(entities):
"""
Args:
entities (Dict[str,List[Entity]]):
Returns:
Dict[str,List[Entity]]
"""
return {src: drop_other_entities(entities)
for src, entities in entities.items()}
def get_correct(gold, guess):
"""
Args:
gold (Iterable[T]):
guess (Iterable[T]):
Returns:
Set[T]
"""
return set(gold) & set(guess)
def get_tp(gold, guess):
"""
Args:
gold (Iterable[T]):
guess (Iterable[T]):
Returns:
Set[T]
"""
return get_correct(gold, guess)
def get_fn(gold, guess):
"""
Args:
gold (Iterable[T]):
guess (Iterable[T]):
Returns:
Set[T]
"""
return set(gold) - set(guess)
def get_fp(gold, guess):
"""
Args:
gold (Iterable[T]):
guess (Iterable[T]):
Returns:
Set[T]
"""
return set(guess) - set(gold)
def get_tn(tp, fp, fn, _all):
"""
Args:
tp (Set[T]):
fp (Set[T]):
fn (Set[T]):
_all (Iterable[T]):
Returns:
Set[T]
"""
return set(_all) - tp - fp - fn
def get_tp_fp_fn_tn(gold, guess, _all):
"""
Args:
gold (Iterator[T]):
guess (Iterator[T]):
_all (Iterator[T]):
Returns:
Tuple[Set[str],Set[str],Set[str],Set[str]]:
"""
tp = get_tp(gold, guess)
fp = get_fp(gold, guess)
fn = get_fn(gold, guess)
tn = get_tn(tp, fp, fn, _all)
return tp, fp, fn, tn
def get_tp_fp_fn_tn_phrases(gold, guess, _all):
"""
Args:
gold: List[Entity]
guess: List[Entity]
_all: List[Entity]
Returns:
Tuple[Set[str],Set[str],Set[str],Set[str]]:
"""
all_phrases = get_phrases(_all)
gold_phrases = get_phrases(gold)
guess_phrases = get_phrases(guess)
correct_phrases = get_phrases(get_correct(gold, guess))
tp = correct_phrases
fp = guess_phrases - tp
fn = gold_phrases - tp
tn = get_tn(tp, fp, fn, all_phrases)
return tp, fp, fn, tn
def calc_results(gold_entities, guess_entities, surface_form=False):
"""
Args:
gold_entities (List[Entity]): the gold standard entity annotations
guess_entities (List[Entity]): a system's entity guesses
surface_form (bool): whether or not to calculate f1-scores on the entity surface forms
Returns:
Results: the results stored in a namedtuple
"""
# get the correct system guesses by taking the intersection of gold and guess entities,
# taking into account tags and document locations
correct_entities = get_correct(gold_entities, guess_entities)
if surface_form: # count only unique surface forms when True
correct_entities = get_phrases_and_tags(correct_entities)
gold_entities = get_phrases_and_tags(gold_entities)
guess_entities = get_phrases_and_tags(guess_entities)
gold = len(gold_entities)
guess = len(guess_entities)
correct = len(correct_entities)
try:
p = correct / float(guess)
except ZeroDivisionError:
p = 0.0
try:
r = correct / float(gold)
except ZeroDivisionError:
r = 0.0
try:
f = 2.0 * p * r / (p + r)
except ZeroDivisionError:
f = 0.0
return Results(gold, guess, correct, p, r, f)
# noinspection PyListCreation,PyDictCreation
def fmt_results(tokens, all_entities, surface_form=False):
"""
Args:
tokens (Dict[str,List[Tokens]): a dictionary of gold and guess tokens
all_entities (Dict[str,List[Entity]): a dictionary of gold and guess entities
surface_form (bool): whether or not to calculate f1-scores on the entity surface forms
Yield:
str: (near) W-NUT format evaluation results
"""
_sys = 'sys_1'
# throw out 'O' tags to get overall p/r/f
tagged_entities = get_tagged_entities(all_entities)
results = {'all': calc_results(all_entities['gold'], all_entities[_sys], surface_form=False),
'tagged': calc_results(tagged_entities['gold'], tagged_entities[_sys], surface_form),
'tokens': calc_results(tokens['gold'], tokens[_sys], surface_form=False)}
yield('processed %d tokens with %d phrases; ' %
(results['tokens'].gold, results['tagged'].gold))
yield('found: %d phrases; correct: %d.\n' %
(results['tagged'].guess, results['tagged'].correct))
if results['tokens'].gold > 0:
# only use token counts for accuracy
yield('accuracy: %6.2f%%; ' %
(100. * results['tokens'].correct / results['tokens'].gold))
yield('precision: %6.2f%%; ' % (100. * results['tagged'].p))
yield('recall: %6.2f%%; ' % (100. * results['tagged'].r))
yield('FB1: %6.2f\n' % (100. * results['tagged'].f))
# get results for each entity category
tags = get_tags(all_entities['gold'])
for tag in sorted(tags):
entities = {src: filter_entities(entities, lambda e: e.tag == tag)
for src, entities in all_entities.items()}
results = calc_results(entities['gold'], entities[_sys], surface_form)
yield('%17s: ' % tag)
yield('precision: %6.2f%%; ' % (100. * results.p))
yield('recall: %6.2f%%; ' % (100. * results.r))
yield('FB1: %6.2f %d\n' % (100. * results.f, results.correct))
def main():
# get tokens and entities
lines = [line for line in fileinput.input()]
tokens = doc_to_toks(lines)
entities = doc_to_entities(lines)
# report results
print("### ENTITY F1-SCORES ###")
for line in fmt_results(tokens, entities, surface_form=False):
print(line)
print()
print("### SURFACE FORM F1-SCORES ###")
for line in fmt_results(tokens, entities, surface_form=True):
print(line)
if __name__ == '__main__':
main()