-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtools.py
262 lines (187 loc) · 5.26 KB
/
tools.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
# coding: utf8
import os
import sys
import pprint
import collections
from os.path import abspath, join, exists
def read_input(path):
with open(path, encoding='utf8') as fp:
return fp.read()
def read_phrases(path):
phrases = {}
with open(path) as fp:
for line in fp:
idx, start, end = line.split()
phrases[int(idx)] = dict(
start=int(start),
end=int(end),
)
return phrases
def read_labels(path):
labels = {}
with open(path) as fp:
for line in fp:
idx, label = line.split()
labels[int(idx)] = label
return labels
def read_links(path):
links = []
with open(path) as fp:
for line in fp:
line = line.split()
rel = line[0]
try:
links.append(dict(
rel=rel,
arg1=int(line[1]),
arg2=int(line[2]),
arg3=None,
))
except:
pass
return links
def find_obj(objs, x):
if isinstance(objs, dict):
source = objs.items()
else:
source = zip(objs, objs)
for idx, l in source:
for field in l:
if l[field] != x[field]:
break
else:
return idx
return None
def between(x, a, b):
return x >= a and x <= b
def intersect(x1, y1, x2, y2):
if x1 >= y1:
return False
if x2 >= y2:
return False
return between(x1, x2, y2) or between(y1, x2, y2) or between(x2, x1, y1) or between(y2, x1, y1)
def find_partial(objs, x):
fidx = find_obj(objs, x)
if fidx:
return fidx, True
start = x['start']
end = x['end']
for idx, l in objs.items():
sstart = l['start']
send = l['end']
if intersect(start, end, sstart, send):
return idx, False
return None, False
def sort(items):
return sorted(items, key=lambda x: (x['start'], x['end']))
def compare_phrases(gold_phrases, dev_phrases):
correct = []
missing = []
spurious = []
partial = []
mapping = {}
for idx, l in gold_phrases.items():
fidx, exact = find_partial(dev_phrases, l)
if fidx and not "eval:%i" % fidx in mapping:
if exact:
correct.append(l)
else:
partial.append((l, dev_phrases[fidx]))
mapping["ref:%i" % idx] = fidx
mapping["eval:%i" % fidx] = idx
else:
missing.append(l)
for fidx, l in dev_phrases.items():
if not "eval:%i" % fidx in mapping:
spurious.append(l)
return dict(
correct=sort(correct),
missing=sort(missing),
spurious=sort(spurious),
partial=partial,
mapping=mapping,
)
def compare_labels(gold, dev, mapping):
confussion_matrix = collections.defaultdict(lambda: 0)
correct = []
incorrect = []
spurious = []
missing = []
for idx, l in gold.items():
fidx = mapping.get('ref:%i' % idx)
if not fidx:
missing.append(dict(id=idx, label=l))
continue
if not fidx in dev:
missing.append(dict(id=idx, label=l))
confussion_matrix[(l, 'None')] += 1
continue
l2 = dev[fidx]
confussion_matrix[(l, l2)] += 1
if l == l2:
correct.append(dict(fidx=fidx, label=l2))
else:
incorrect.append(dict(fidx=fidx, label=l2, correct=l))
for fidx, l in dev.items():
if "eval:%i" % fidx in mapping:
continue
spurious.append(dict(fidx=fidx, label=l))
return dict(
confussion_matrix=confussion_matrix,
correct=correct,
incorrect=incorrect,
missing=missing,
spurious=spurious,
)
def map_entities(x, mapping, map_key):
result = dict(
rel=x['rel'],
arg1=None,
arg2=None,
arg3=None,
)
for key in ["arg1", "arg2", "arg3"]:
value = x[key]
if value is None:
result[key] = None
continue
mapped = map_key + ":%i" % value
if not mapped in mapping:
return False
result[key] = mapping[mapped]
return result
def find_relation(rel, relations):
for r in relations:
for k in ["rel", "arg1", "arg2", "arg3"]:
if r[k] != rel[k]:
break
else:
return True
return False
def compare_links(gold_links, dev_links, mapping):
correct = []
missing = []
spurious = []
for rel in gold_links:
mapped = map_entities(rel, mapping, "ref")
if not mapped:
missing.append(rel)
continue
if not find_relation(mapped, dev_links):
missing.append(rel)
continue
correct.append(rel)
for rel in dev_links:
mapped = map_entities(rel, mapping, "eval")
if not mapped:
spurious.append(rel)
continue
if not find_relation(mapped, gold_links):
spurious.append(rel)
return dict(
correct=correct,
missing=missing,
spurious=spurious,
)
def get_span(sentences, obj):
return sentences[obj["start"]:obj["end"]]