-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreader.py
350 lines (296 loc) · 12.9 KB
/
reader.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
import os
import torch
import json
from tqdm import tqdm
from collections import Counter, defaultdict
class FeatureReader(object):
def __init__(self, data_path) -> None:
self.data = torch.load(data_path)
def read(self, split='train'):
return self.data[split]
class TextReader(object):
'read text feature'
"""read and store DocRED data"""
def __init__(self, data_dir, save_dir, tokenizer) -> None:
self.data_dir = data_dir
self.save_dir = save_dir
if not os.path.exists(self.save_dir):
os.makedirs(self.save_dir)
with open(os.path.join(data_dir, 'rel_info.json')) as fp:
self.rel2info = json.load(fp)
self.id2rel = sorted(list(self.rel2info.keys()))
self.rel2id = {r: i for i, r in enumerate(self.id2rel)}
self.data_paths = {
'train': os.path.join(data_dir, 'train_annotated.json'),
'dist': os.path.join(data_dir, 'train_distant.json'),
'dev': os.path.join(data_dir, 'dev.json'),
'test': os.path.join(data_dir, 'test.json')
}
self.bin_paths = {
'train': os.path.join(save_dir, 'train.pth'),
'dist': os.path.join(save_dir, 'dist.pth'),
'dev': os.path.join(save_dir, 'dev.pth'),
'test': os.path.join(save_dir, 'test.pth')
}
self.tokenizer = tokenizer
def read(self, split='train'):
bin_path = self.bin_paths[split]
if os.path.exists(bin_path):
return torch.load(bin_path)
else:
features = self.read_raw(split)
torch.save(features, bin_path)
return features
def read_raw(self, split='train', max_seq_length=1024):
with open(self.data_paths[split]) as fp:
data = json.load(fp)
features = []
for item in tqdm(data, desc='reading raw data'):
sents = []
sent_map = []
entities = item['vertexSet']
entity_start, entity_end = [], []
for entity in entities:
types = []
for mention in entity:
sent_id = mention["sent_id"]
pos = mention["pos"]
entity_start.append((sent_id, pos[0],))
entity_end.append((sent_id, pos[1] - 1,))
for i_s, sent in enumerate(item['sents']):
new_map = {}
for i_t, token in enumerate(sent):
tokens_wordpiece = self.tokenizer.tokenize(token)
if (i_s, i_t) in entity_start:
tokens_wordpiece = ["*"] + tokens_wordpiece
if (i_s, i_t) in entity_end:
tokens_wordpiece = tokens_wordpiece + ["*"]
new_map[i_t] = len(sents)
sents.extend(tokens_wordpiece)
new_map[i_t + 1] = len(sents)
sent_map.append(new_map)
entity_pos = []
for e in entities:
entity_pos.append([])
for m in e:
start = sent_map[m["sent_id"]][m["pos"][0]]
end = sent_map[m["sent_id"]][m["pos"][1]]
entity_pos[-1].append((start, end,))
labels = torch.zeros(len(entities), len(entities), len(self.rel2id), dtype=torch.bool)
if 'labels' in item:
for fact in item['labels']:
labels[fact['h'], fact['t'], self.rel2id[fact['r']]] = 1
sents = sents[:max_seq_length - 2]
input_ids = self.tokenizer.convert_tokens_to_ids(sents)
input_ids = self.tokenizer.build_inputs_with_special_tokens(input_ids)
features.append({
'input_ids': input_ids,
'entity_pos': entity_pos,
'title': item['title'],
'N': len(entities),
'labels': labels.to_sparse()
})
return features
def get_prior(self, split='train'):
train_data = self.read(split)
total = 0.
pos = torch.zeros([len(self.rel2id)])
for f in tqdm(train_data):
labels = f['labels'].float().to_dense()
pos += labels.sum(dim=(0,1))
total += labels.size(0)**2
return pos / total
class ERuleReader(object):
'read text feature'
"""read and store DocRED data"""
def __init__(self, data_dir, save_dir, max_step=3) -> None:
self.data_dir = data_dir
self.save_dir = save_dir
if not os.path.exists(self.save_dir):
os.makedirs(self.save_dir)
self.rel2id = {k: v-1 for k,v in json.load(open(os.path.join(data_dir, 'meta/rel2id.json'))).items()}
self.id2rel = {k:v for v, k in self.rel2id.items()}
self.R = len(self.rel2id) - 1
self.type2id = json.load(open(os.path.join(data_dir, 'meta/ner2id.json')))
self.id2type = {k:v for v, k in self.type2id.items()}
self.data_paths = {
'rtrain': os.path.join(data_dir, 'rtrain.json'),
'train': os.path.join(data_dir, 'train_annotated.json'),
'dist': os.path.join(data_dir, 'train_distant.json'),
'dev': os.path.join(data_dir, 'dev.json'),
'test': os.path.join(data_dir, 'test.json')
}
self.bin_paths = {
'rtrain': os.path.join(save_dir, 'cooccur-rtrain.pth'),
'train': os.path.join(save_dir, 'cooccur-train.pth'),
'dist': os.path.join(save_dir, 'cooccur-dist.pth'),
'dev': os.path.join(save_dir, 'cooccur-dev.pth'),
'test': os.path.join(save_dir, 'cooccur-test.pth')
}
self.max_step = max_step
def read(self, split='train'):
bin_path = self.bin_paths[split]
if os.path.exists(bin_path):
return torch.load(bin_path)
else:
features = self.read_raw(split)
torch.save(features, bin_path)
return features
def read_raw(self, split='train'):
"""count co-occurence info"""
max_step = self.max_step
r2epair = self.get_r2epair()
rule_counter = {(i, h, t): Counter() for i in range(self.R) for (h, t) in r2epair[i]}
with open(self.data_paths[split]) as fp:
data = json.load(fp)
for item in tqdm(data, desc='reading raw data'):
entities = item['vertexSet']
entity_types = [self.type2id[e[0]['type']] for e in entities]
paths = {}
meta_paths = {1: paths}
for fact in item['labels']:
h, t, r = fact['h'], fact['t'], self.rel2id[fact['r']]
if h not in paths:
paths[h] = {t: [([r], [t])]}
elif t not in paths[h]:
paths[h][t] = [([r], [t])]
else:
paths[h][t].append(([r], [t]))
if t not in paths:
paths[t] = {h: [([r + self.R], [h])]}
elif h not in paths[t]:
paths[t][h] = [([r + self.R], [h])]
else:
paths[t][h].append(([r + self.R], [h]))
for step in range(2, max_step + 1):
prev_paths = meta_paths[step - 1]
paths = {}
for h in prev_paths:
for inode, prev_chain in prev_paths[h].items():
if inode in meta_paths[1]:
for t, rs in meta_paths[1][inode].items():
if h == t:
continue
new_chain = append_chain(prev_chain, rs)
if not new_chain:
continue
if h not in paths:
paths[h] = {t: new_chain}
elif t not in paths[h]:
paths[h][t] = new_chain
else:
paths[h][t].extend(new_chain)
meta_paths[step] = paths
for h in meta_paths[1]:
for t, rs in meta_paths[1][h].items():
c_meta_paths = set()
for step in range(1, max_step + 1):
if h in meta_paths[step] and t in meta_paths[step][h]:
for path in meta_paths[step][h][t]:
c_meta_paths.add(tuple(path[0]))
for r in rs:
if r[0][0] >= self.R:
continue
triple = (r[0][0], entity_types[h], entity_types[t])
rule_counter[triple].update(c_meta_paths)
triples = []
triple2rules = {}
triple2probs = {}
lens = [len(epair) for epair in r2epair]
for ri, epairs in enumerate(r2epair):
for epair in epairs:
triple = (ri, epair[0], epair[1])
total = sum(rule_counter[triple].values())
rules, probs = [], []
for rule in rule_counter[triple]:
rules.append(rule)
probs.append(rule_counter[triple][rule] / total)
triples.append(triple)
triple2rules[triple] = rules
triple2probs[triple] = probs
features = {
'triples': triples,
'sections': lens,
'triple2rules': triple2rules,
'triple2probs': triple2probs,
}
return features
def get_r2epair(self):
r2epair = [[] for _ in range(len(self.rel2id)-1)]
with open(self.data_paths['train']) as fp:
data = json.load(fp)
for item in data:
entities = item['vertexSet']
entity_types = [self.type2id[e[0]['type']] for e in entities]
for fact in item['labels']:
h, t, r = entity_types[fact['h']], entity_types[fact['t']], self.rel2id[fact['r']]
if (h,t) not in r2epair[r]:
r2epair[r].append((h, t))
return r2epair
def get_epair2r(self):
e_pair2r = torch.zeros(len(self.type2id), len(self.type2id), len(self.rel2id)-1).bool()
with open(self.data_paths['train']) as fp:
data = json.load(fp)
for item in data:
entities = item['vertexSet']
entity_types = [self.type2id[e[0]['type']] for e in entities]
for fact in item['labels']:
h, t, r = fact['h'], fact['t'], self.rel2id[fact['r']]
e_pair2r[entity_types[h], entity_types[t], r] = 1
print(e_pair2r.size(), e_pair2r.sum())
return e_pair2r
def get_type_mask(self, triples, sections, split='train'):
ntypes = len(self.type2id)
rpair2id = [{} for _ in sections]
tid = 0
for section in sections:
for sid in range(section):
r, e1, e2 = triples[tid]
rpair2id[r][(e1, e2)] = sid
tid += 1
triple2sid = torch.CharTensor(ntypes, ntypes, self.R).fill_(-1)
for ei in range(ntypes):
for ej in range(ntypes):
for r in range(self.R):
triple2sid[ei, ej, r] = rpair2id[r].get((ei, ej), -1)
with open(self.data_paths[split]) as fp:
data = json.load(fp)
type_masks = []
for item in data:
entities = item['vertexSet']
N = len(entities)
entity_types = torch.tensor([self.type2id[e[0]['type']] for e in entities])
type_indices = (entity_types.unsqueeze(1).repeat(1, N), entity_types.unsqueeze(0).repeat(N, 1))
type_mask = triple2sid[type_indices[0], type_indices[1]]
type_masks.append(type_mask)
return type_masks
def get_dist(self, split='train'):
with open(self.data_paths[split]) as fp:
data = json.load(fp)
dists = []
for item in tqdm(data, desc='reading raw data'):
entities = item['vertexSet']
N = len(entities)
entities_pos = []
for entity in entities:
s = entity[0]['pos'][0]
e = entity[0]['pos'][1]
entities_pos.append([s, e])
dist = torch.zeros(N, N)
for h in range(N):
for t in range(N):
sh, eh = entities_pos[h]
st, et = entities_pos[t]
dist[h,t] = min(abs(sh - et), abs(st - eh))
dists.append(dist)
return dists
def append_chain(chains, rs):
ret = []
for chain, chain_nodes in chains:
for r, rnode in rs:
if rnode[0] not in chain_nodes:
ret.append((chain + r, chain_nodes + rnode))
return ret
if __name__ == "__main__":
reader = ERuleReader('../kbp-benchmarks/DWIE/data/docred-style', 'data/DWIE-erules')
reader.get_dist('train')