2017-04-15 10:05:47 +00:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import division, print_function, unicode_literals
|
2015-03-11 01:07:03 +00:00
|
|
|
|
2018-03-27 17:23:02 +00:00
|
|
|
from .gold import tags_to_entities, GoldParse
|
2018-04-03 13:50:31 +00:00
|
|
|
from .errors import Errors
|
2015-05-27 01:18:16 +00:00
|
|
|
|
2015-04-05 20:29:30 +00:00
|
|
|
|
2015-05-24 18:07:18 +00:00
|
|
|
class PRFScore(object):
|
2017-04-15 09:59:21 +00:00
|
|
|
"""
|
|
|
|
A precision / recall / F score
|
|
|
|
"""
|
2015-05-24 18:07:18 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.tp = 0
|
|
|
|
self.fp = 0
|
|
|
|
self.fn = 0
|
|
|
|
|
|
|
|
def score_set(self, cand, gold):
|
|
|
|
self.tp += len(cand.intersection(gold))
|
|
|
|
self.fp += len(cand - gold)
|
|
|
|
self.fn += len(gold - cand)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def precision(self):
|
|
|
|
return self.tp / (self.tp + self.fp + 1e-100)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def recall(self):
|
|
|
|
return self.tp / (self.tp + self.fn + 1e-100)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def fscore(self):
|
|
|
|
p = self.precision
|
|
|
|
r = self.recall
|
|
|
|
return 2 * ((p * r) / (p + r + 1e-100))
|
|
|
|
|
|
|
|
|
2015-03-11 01:07:03 +00:00
|
|
|
class Scorer(object):
|
|
|
|
def __init__(self, eval_punct=False):
|
2015-05-24 18:07:18 +00:00
|
|
|
self.tokens = PRFScore()
|
|
|
|
self.sbd = PRFScore()
|
|
|
|
self.unlabelled = PRFScore()
|
|
|
|
self.labelled = PRFScore()
|
|
|
|
self.tags = PRFScore()
|
|
|
|
self.ner = PRFScore()
|
2015-03-11 01:07:03 +00:00
|
|
|
self.eval_punct = eval_punct
|
|
|
|
|
|
|
|
@property
|
|
|
|
def tags_acc(self):
|
2015-05-24 18:07:18 +00:00
|
|
|
return self.tags.fscore * 100
|
2015-05-24 00:49:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def token_acc(self):
|
2015-06-28 04:21:38 +00:00
|
|
|
return self.tokens.precision * 100
|
2015-03-11 01:07:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def uas(self):
|
2015-05-24 18:07:18 +00:00
|
|
|
return self.unlabelled.fscore * 100
|
2015-03-11 01:07:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def las(self):
|
2015-05-24 18:07:18 +00:00
|
|
|
return self.labelled.fscore * 100
|
2015-03-11 01:07:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def ents_p(self):
|
2015-05-27 01:18:16 +00:00
|
|
|
return self.ner.precision * 100
|
2015-03-11 01:07:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def ents_r(self):
|
2015-05-27 01:18:16 +00:00
|
|
|
return self.ner.recall * 100
|
2015-04-19 08:31:31 +00:00
|
|
|
|
2015-03-11 01:07:03 +00:00
|
|
|
@property
|
|
|
|
def ents_f(self):
|
2015-05-27 01:18:16 +00:00
|
|
|
return self.ner.fscore * 100
|
2015-03-11 01:07:03 +00:00
|
|
|
|
2016-10-09 10:24:24 +00:00
|
|
|
@property
|
|
|
|
def scores(self):
|
|
|
|
return {
|
2017-10-27 12:40:14 +00:00
|
|
|
'uas': self.uas,
|
|
|
|
'las': self.las,
|
|
|
|
'ents_p': self.ents_p,
|
|
|
|
'ents_r': self.ents_r,
|
|
|
|
'ents_f': self.ents_f,
|
2016-10-09 10:24:24 +00:00
|
|
|
'tags_acc': self.tags_acc,
|
|
|
|
'token_acc': self.token_acc
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:59:06 +00:00
|
|
|
def score(self, tokens, gold, verbose=False, punct_labels=('p', 'punct')):
|
2018-03-27 17:23:02 +00:00
|
|
|
if len(tokens) != len(gold):
|
2018-04-30 23:33:20 +00:00
|
|
|
gold = GoldParse.from_annot_tuples(tokens, zip(*gold.orig_annot))
|
2015-05-24 18:07:18 +00:00
|
|
|
gold_deps = set()
|
|
|
|
gold_tags = set()
|
2017-10-27 12:40:14 +00:00
|
|
|
gold_ents = set(tags_to_entities([annot[-1]
|
|
|
|
for annot in gold.orig_annot]))
|
2015-05-24 18:07:18 +00:00
|
|
|
for id_, word, tag, head, dep, ner in gold.orig_annot:
|
2015-05-30 16:24:32 +00:00
|
|
|
gold_tags.add((id_, tag))
|
2017-03-16 14:38:28 +00:00
|
|
|
if dep not in (None, "") and dep.lower() not in punct_labels:
|
2015-05-27 01:18:16 +00:00
|
|
|
gold_deps.add((id_, head, dep.lower()))
|
2015-05-24 18:07:18 +00:00
|
|
|
cand_deps = set()
|
|
|
|
cand_tags = set()
|
|
|
|
for token in tokens:
|
2015-06-07 17:10:32 +00:00
|
|
|
if token.orth_.isspace():
|
|
|
|
continue
|
2015-05-30 16:24:32 +00:00
|
|
|
gold_i = gold.cand_to_gold[token.i]
|
|
|
|
if gold_i is None:
|
2018-03-27 17:23:02 +00:00
|
|
|
self.tokens.fp += 1
|
2015-05-30 16:24:32 +00:00
|
|
|
else:
|
2015-06-28 04:21:38 +00:00
|
|
|
self.tokens.tp += 1
|
2015-05-30 16:24:32 +00:00
|
|
|
cand_tags.add((gold_i, token.tag_))
|
2016-02-02 21:59:06 +00:00
|
|
|
if token.dep_.lower() not in punct_labels and token.orth_.strip():
|
2015-05-24 18:07:18 +00:00
|
|
|
gold_head = gold.cand_to_gold[token.head.i]
|
|
|
|
# None is indistinct, so we can't just add it to the set
|
|
|
|
# Multiple (None, None) deps are possible
|
|
|
|
if gold_i is None or gold_head is None:
|
|
|
|
self.unlabelled.fp += 1
|
|
|
|
self.labelled.fp += 1
|
|
|
|
else:
|
2015-05-27 01:18:16 +00:00
|
|
|
cand_deps.add((gold_i, gold_head, token.dep_.lower()))
|
2015-05-28 20:39:08 +00:00
|
|
|
if '-' not in [token[-1] for token in gold.orig_annot]:
|
|
|
|
cand_ents = set()
|
|
|
|
for ent in tokens.ents:
|
|
|
|
first = gold.cand_to_gold[ent.start]
|
|
|
|
last = gold.cand_to_gold[ent.end-1]
|
|
|
|
if first is None or last is None:
|
|
|
|
self.ner.fp += 1
|
|
|
|
else:
|
|
|
|
cand_ents.add((ent.label_, first, last))
|
|
|
|
self.ner.score_set(cand_ents, gold_ents)
|
2015-05-27 01:18:16 +00:00
|
|
|
self.tags.score_set(cand_tags, gold_tags)
|
2015-05-24 18:07:18 +00:00
|
|
|
self.labelled.score_set(cand_deps, gold_deps)
|
|
|
|
self.unlabelled.score_set(
|
|
|
|
set(item[:2] for item in cand_deps),
|
|
|
|
set(item[:2] for item in gold_deps),
|
|
|
|
)
|
2015-06-14 15:45:50 +00:00
|
|
|
if verbose:
|
|
|
|
gold_words = [item[1] for item in gold.orig_annot]
|
|
|
|
for w_id, h_id, dep in (cand_deps - gold_deps):
|
2015-07-28 12:09:30 +00:00
|
|
|
print('F', gold_words[w_id], dep, gold_words[h_id])
|
2015-06-14 15:45:50 +00:00
|
|
|
for w_id, h_id, dep in (gold_deps - cand_deps):
|
2015-07-28 12:09:30 +00:00
|
|
|
print('M', gold_words[w_id], dep, gold_words[h_id])
|