-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtokeneval.py
97 lines (86 loc) · 3.52 KB
/
tokeneval.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
import pickle
import nltk
import re
from ucca import layer0, layer1, classify
def get_terminals_labels(passages):
terminals = []
labels = []
for passage in passages:
l0 = passage.layer(layer0.LAYER_ID)
l1 = passage.layer(layer1.LAYER_ID)
heads = [x for x in l1.all
if x.extra.get('hidden_scene') or x.extra.get('noun_scene')]
positive_nouns = [head.get_terminals()[0] for head in heads]
negative_nouns = [x for x in l0.all
if re.match(r'NN.+', x.extra.get('postag', ''))
and x not in positive_nouns]
labels.extend([1] * len(positive_nouns) + [0] * len(negative_nouns))
terminals.extend(positive_nouns + negative_nouns)
return terminals, labels
def get_context(terminals, context=2):
tokens = []
for main_terminal in terminals:
l0 = main_terminal.root.layer(layer0.LAYER_ID)
main_position = main_terminal.position
pre_context = [l0.by_position(i).text
for i in range(main_position - 1,
main_position - context - 1, -1)
if i >= 1]
post_context = [l0.by_position(i).text
for i in range(main_position + 1,
main_position + context + 1, 1)
if i <= len(l0.all)]
tokens.append((main_terminal.text, tuple(pre_context),
tuple(post_context)))
return tokens
def lemmatize(token, targets):
wn = nltk.stem.wordnet.WordNetLemmatizer()
lemma = token
if lemma in targets:
return lemma
if lemma.istitle():
lemma = token.lower()
if lemma in targets:
return lemma
lemma = wn.lemmatize(lemma)
return lemma
def evaluate_with_type(tokens, token_labels, targets, target_labels):
tp, tn, fp, fn = [], [], [], [] # True/Flase positive/negative labels
found, not_found = [], []
for token, token_label in zip(tokens, token_labels):
lemma = lemmatize(token, targets)
# finding in targets with lemma, but recording results with the orig
if lemma in targets:
found.append((token, token_label))
target_label = target_labels[targets.index(lemma)]
if target_label == token_label == 0:
tn.append(token)
elif target_label == token_label == 1:
tp.append(token)
elif token_label == 0:
fp.append(token)
else:
fn.append(token)
else:
not_found.append((token, token_label))
return found, not_found, tp, tn, fp, fn
def evaluate_with_classifier(tokens, token_labels, targets,
token_features, classifier):
tp, tn, fp, fn = [], [], [], [] # True/Flase positive/negative labels
found, not_found = [], []
pred = classify.predict_labels(classifier, token_features).tolist()
for token, token_label, guessed_label in zip(tokens, token_labels, pred):
lemma = lemmatize(token, targets)
if lemma in targets:
found.append((token, token_label))
if guessed_label == token_label == 0:
tn.append(token)
elif guessed_label == token_label == 1:
tp.append(token)
elif token_label == 0:
fp.append(token)
else:
fn.append(token)
else:
not_found.append((token, token_label))
return found, not_found, tp, tn, fp, fn