-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
401 lines (324 loc) · 15.2 KB
/
preprocess.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
import os
import json
import argparse
import multiprocessing as mp
from multiprocessing import Pool
from typing import List
parser = argparse.ArgumentParser(description='preprocess')
parser.add_argument('--task', default='wn18rr', type=str, metavar='N',
help='dataset name')
parser.add_argument('--workers', default=2, type=int, metavar='N',
help='number of workers')
parser.add_argument('--train-path', default='', type=str, metavar='N',
help='path to training data')
parser.add_argument('--valid-path', default='', type=str, metavar='N',
help='path to valid data')
parser.add_argument('--test-path', default='', type=str, metavar='N',
help='path to valid data')
args = parser.parse_args()
mp.set_start_method('fork')
def _check_sanity(relation_id_to_str: dict):
# We directly use normalized relation string as a key for training and evaluation,
# make sure no two relations are normalized to the same surface form
relation_str_to_id = {}
for rel_id, rel_str in relation_id_to_str.items():
if rel_str is None:
continue
if rel_str not in relation_str_to_id:
relation_str_to_id[rel_str] = rel_id
elif relation_str_to_id[rel_str] != rel_id:
assert False, 'ERROR: {} and {} are both normalized to {}'\
.format(relation_str_to_id[rel_str], rel_id, rel_str)
return
def _normalize_relations(examples: List[dict], normalize_fn, is_train: bool):
relation_id_to_str = {}
for ex in examples:
rel_str = normalize_fn(ex['relation'])
relation_id_to_str[ex['relation']] = rel_str
ex['relation'] = rel_str
_check_sanity(relation_id_to_str)
if is_train:
out_path = '{}/relations.json'.format(os.path.dirname(args.train_path))
with open(out_path, 'w', encoding='utf-8') as writer:
json.dump(relation_id_to_str, writer, ensure_ascii=False, indent=4)
print('Save {} relations to {}'.format(len(relation_id_to_str), out_path))
wn18rr_id2ent = {}
def _load_wn18rr_texts(path: str):
global wn18rr_id2ent
lines = open(path, 'r', encoding='utf-8').readlines()
for line in lines:
fs = line.strip().split('\t')
assert len(fs) == 3, 'Invalid line: {}'.format(line.strip())
entity_id, word, desc = fs[0], fs[1].replace('__', ''), fs[2]
wn18rr_id2ent[entity_id] = (entity_id, word, desc)
print('Load {} entities from {}'.format(len(wn18rr_id2ent), path))
def _process_line_wn18rr(line: str) -> dict:
fs = line.strip().split('\t')
assert len(fs) == 3, 'Expect 3 fields for {}'.format(line)
head_id, relation, tail_id = fs[0], fs[1], fs[2]
_, head, _ = wn18rr_id2ent[head_id]
_, tail, _ = wn18rr_id2ent[tail_id]
example = {'head_id': head_id,
'head': head,
'relation': relation,
'tail_id': tail_id,
'tail': tail}
return example
def preprocess_wn18rr(path):
if not wn18rr_id2ent:
_load_wn18rr_texts('{}/wordnet-mlj12-definitions.txt'.format(os.path.dirname(path)))
lines = open(path, 'r', encoding='utf-8').readlines()
pool = Pool(processes=args.workers)
examples = pool.map(_process_line_wn18rr, lines)
pool.close()
pool.join()
_normalize_relations(examples, normalize_fn=lambda rel: rel.replace('_', ' ').strip(),
is_train=(path == args.train_path))
out_path = path + '.json'
json.dump(examples, open(out_path, 'w', encoding='utf-8'), ensure_ascii=False, indent=4)
print('Save {} examples to {}'.format(len(examples), out_path))
return examples
fb15k_id2ent = {}
fb15k_id2desc = {}
def _load_fb15k237_wikidata(path: str):
global fb15k_id2ent, fb15k_id2desc
lines = open(path, 'r', encoding='utf-8').readlines()
for line in lines:
fs = line.strip().split('\t')
assert len(fs) == 2, 'Invalid line: {}'.format(line.strip())
entity_id, name = fs[0], fs[1]
name = name.replace('_', ' ').strip()
if entity_id not in fb15k_id2desc:
print('No desc found for {}'.format(entity_id))
fb15k_id2ent[entity_id] = (entity_id, name, fb15k_id2desc.get(entity_id, ''))
print('Load {} entity names from {}'.format(len(fb15k_id2ent), path))
def _load_fb15k237_desc(path: str):
global fb15k_id2desc
lines = open(path, 'r', encoding='utf-8').readlines()
for line in lines:
fs = line.strip().split('\t')
assert len(fs) == 2, 'Invalid line: {}'.format(line.strip())
entity_id, desc = fs[0], fs[1]
fb15k_id2desc[entity_id] = _truncate(desc, 50)
print('Load {} entity descriptions from {}'.format(len(fb15k_id2desc), path))
def _normalize_fb15k237_relation(relation: str) -> str:
tokens = relation.replace('./', '/').replace('_', ' ').strip().split('/')
dedup_tokens = []
for token in tokens:
if token not in dedup_tokens[-3:]:
dedup_tokens.append(token)
# leaf words are more important (maybe)
relation_tokens = dedup_tokens[::-1]
relation = ' '.join([t for idx, t in enumerate(relation_tokens)
if idx == 0 or relation_tokens[idx] != relation_tokens[idx - 1]])
return relation
def _process_line_fb15k237(line: str) -> dict:
fs = line.strip().split('\t')
assert len(fs) == 3, 'Expect 3 fields for {}'.format(line)
head_id, relation, tail_id = fs[0], fs[1], fs[2]
_, head, _ = fb15k_id2ent[head_id]
_, tail, _ = fb15k_id2ent[tail_id]
example = {'head_id': head_id,
'head': head,
'relation': relation,
'tail_id': tail_id,
'tail': tail}
return example
def preprocess_fb15k237(path):
if not fb15k_id2desc:
_load_fb15k237_desc('{}/FB15k_mid2description.txt'.format(os.path.dirname(path)))
if not fb15k_id2ent:
_load_fb15k237_wikidata('{}/FB15k_mid2name.txt'.format(os.path.dirname(path)))
lines = open(path, 'r', encoding='utf-8').readlines()
pool = Pool(processes=args.workers)
examples = pool.map(_process_line_fb15k237, lines)
pool.close()
pool.join()
_normalize_relations(examples, normalize_fn=_normalize_fb15k237_relation, is_train=(path == args.train_path))
out_path = path + '.json'
json.dump(examples, open(out_path, 'w', encoding='utf-8'), ensure_ascii=False, indent=4)
print('Save {} examples to {}'.format(len(examples), out_path))
return examples
wiki5m_id2rel = {}
wiki5m_id2ent = {}
wiki5m_id2text = {}
def _truncate(text: str, max_len: int):
return ' '.join(text.split()[:max_len])
def _load_wiki5m_id2rel(path: str):
global wiki5m_id2rel
for line in open(path, 'r', encoding='utf-8'):
fs = line.strip().split('\t')
assert len(fs) >= 2, 'Invalid line: {}'.format(line.strip())
rel_id, rel_text = fs[0], fs[1]
rel_text = _truncate(rel_text, 10)
wiki5m_id2rel[rel_id] = rel_text
print('Load {} relations from {}'.format(len(wiki5m_id2rel), path))
def _load_wiki5m_id2ent(path: str):
global wiki5m_id2ent
for line in open(path, 'r', encoding='utf-8'):
fs = line.strip().split('\t')
assert len(fs) >= 2, 'Invalid line: {}'.format(line.strip())
ent_id, ent_name = fs[0], fs[1] # the aliases are ignored
wiki5m_id2ent[ent_id] = _truncate(ent_name, 10)
print('Load {} entity names from {}'.format(len(wiki5m_id2ent), path))
def _load_wiki5m_id2text(path: str, max_len: int = 30):
global wiki5m_id2text
for line in open(path, 'r', encoding='utf-8'):
fs = line.strip().split('\t')
assert len(fs) >= 2, 'Invalid line: {}'.format(line.strip())
ent_id, ent_text = fs[0], ' '.join(fs[1:])
wiki5m_id2text[ent_id] = _truncate(ent_text, max_len)
print('Load {} entity texts from {}'.format(len(wiki5m_id2text), path))
def _has_none_value(ex: dict) -> bool:
return any(v is None for v in ex.values())
def _process_line_wiki5m(line: str) -> dict:
fs = line.strip().split('\t')
assert len(fs) == 3, 'Invalid line: {}'.format(line.strip())
head_id, relation_id, tail_id = fs[0], fs[1], fs[2]
example = {'head_id': head_id,
'head': wiki5m_id2ent.get(head_id, None),
'relation': relation_id,
'tail_id': tail_id,
'tail': wiki5m_id2ent.get(tail_id, None)}
return example
def preprocess_wiki5m(path: str, is_train: bool) -> List[dict]:
if not wiki5m_id2rel:
_load_wiki5m_id2rel(path='{}/wikidata5m_relation.txt'.format(os.path.dirname(path)))
if not wiki5m_id2ent:
_load_wiki5m_id2ent(path='{}/wikidata5m_entity.txt'.format(os.path.dirname(path)))
if not wiki5m_id2text:
_load_wiki5m_id2text(path='{}/wikidata5m_text.txt'.format(os.path.dirname(path)))
lines = open(path, 'r', encoding='utf-8').readlines()
pool = Pool(processes=args.workers)
examples = pool.map(_process_line_wiki5m, lines)
pool.close()
pool.join()
_normalize_relations(examples, normalize_fn=lambda rel_id: wiki5m_id2rel.get(rel_id, None), is_train=is_train)
invalid_examples = [ex for ex in examples if _has_none_value(ex)]
print('Find {} invalid examples in {}'.format(len(invalid_examples), path))
if is_train:
# P2439 P1962 P3484 do not exist in wikidata5m_relation.txt
# so after filtering, there are 819 relations instead of 822 relations
examples = [ex for ex in examples if not _has_none_value(ex)]
else:
# Even though it's invalid (contains null values), we should not change validation/test dataset
print('Invalid examples: {}'.format(json.dumps(invalid_examples, ensure_ascii=False, indent=4)))
out_path = path + '.json'
json.dump(examples, open(out_path, 'w', encoding='utf-8'), ensure_ascii=False, indent=4)
print('Save {} examples to {}'.format(len(examples), out_path))
return examples
def dump_all_entities(examples, out_path, id2text: dict):
id2entity = {}
relations = set()
for ex in examples:
head_id = ex['head_id']
relations.add(ex['relation'])
if head_id not in id2entity:
id2entity[head_id] = {'entity_id': head_id,
'entity': ex['head'],
'entity_desc': id2text[head_id]}
tail_id = ex['tail_id']
if tail_id not in id2entity:
id2entity[tail_id] = {'entity_id': tail_id,
'entity': ex['tail'],
'entity_desc': id2text[tail_id]}
print('Get {} entities, {} relations in total'.format(len(id2entity), len(relations)))
json.dump(list(id2entity.values()), open(out_path, 'w', encoding='utf-8'), ensure_ascii=False, indent=4)
def _load_mopenkb_id2rel(path: str):
global mopenkb_id2rel
for line in open(path, 'r', encoding='utf-8'):
fs = line.strip().split('\t')
assert len(fs) >= 2, 'Invalid line: {}'.format(line.strip())
rel_id, rel_text = fs[0], fs[1]
mopenkb_id2rel[rel_id] = rel_text
print('Load {} relations from {}'.format(len(mopenkb_id2rel), path))
# return mopenkb_id2rel
def _load_mopenkb_id2ent(path: str):
global mopenkb_id2ent
for line in open(path, 'r', encoding='utf-8'):
fs = line.strip().split('\t')
assert len(fs) >= 2, 'Invalid line: {}'.format(line.strip())
ent_id, ent_name = fs[0], fs[1] # the aliases are ignored
mopenkb_id2ent[ent_id] = ent_name
print('Load {} entity names from {}'.format(len(mopenkb_id2ent), path))
# return mopenkb_id2ent
def _load_mopenkb_id2text(path: str, max_len: int = 30):
global mopenkb_id2text
for line in open(path, 'r', encoding='utf-8'):
fs = line.strip().split('\t')
assert len(fs) >= 2, 'Invalid line: {}'.format(line.strip())
ent_id, ent_text = fs[0], ' '.join(fs[1:])
mopenkb_id2text[ent_id] = ent_text
print('Load {} entity texts from {}'.format(len(mopenkb_id2text), path))
# return mopenkb_id2text
def _process_line_mopenkb(line):
fs = line.strip().split('\t')
assert len(fs) == 3, 'Invalid line: {}'.format(line.strip())
head_id, relation_id, tail_id = fs[0], fs[1], fs[2]
example = {'head_id': head_id,
'head': mopenkb_id2ent[head_id],
'relation': relation_id,
'tail_id': tail_id,
'tail': mopenkb_id2ent[tail_id]}
return example
mopenkb_id2rel = {}
mopenkb_id2ent = {}
mopenkb_id2text = {}
def preprocess_mopenkb(path, is_train):
mopenkb_path = os.path.dirname(path)
if not mopenkb_id2rel:
_load_mopenkb_id2rel(path='{}/relation.txt'.format(mopenkb_path))
if not mopenkb_id2ent:
_load_mopenkb_id2ent(path='{}/entity.txt'.format(mopenkb_path))
if not mopenkb_id2text:
_load_mopenkb_id2text(path='{}/text.txt'.format(mopenkb_path))
lines = open(path, 'r', encoding='utf-8').readlines()
pool = Pool(processes=args.workers)
examples = pool.map(_process_line_mopenkb, lines)
pool.close()
pool.join()
_normalize_relations(examples, normalize_fn=lambda rel_id: mopenkb_id2rel[rel_id], is_train=is_train)
invalid_examples = [ex for ex in examples if _has_none_value(ex)]
print('Find {} invalid examples in {}'.format(len(invalid_examples), path))
if is_train:
# P2439 P1962 P3484 do not exist in wikidata5m_relation.txt
# so after filtering, there are 819 relations instead of 822 relations
examples = [ex for ex in examples if not _has_none_value(ex)]
else:
# Even though it's invalid (contains null values), we should not change validation/test dataset
print('Invalid examples: {}'.format(json.dumps(invalid_examples, ensure_ascii=False, indent=4)))
out_path = path + '.json'
json.dump(examples, open(out_path, 'w', encoding='utf-8'), ensure_ascii=False, indent=4)
print('Save {} examples to {}'.format(len(examples), out_path))
return examples
def main():
all_examples = []
for path in [args.train_path, args.valid_path, args.test_path]:
assert os.path.exists(path)
print('Process {}...'.format(path))
if args.task.lower() == 'wn18rr':
all_examples += preprocess_wn18rr(path)
elif args.task.lower() == 'fb15k237':
all_examples += preprocess_fb15k237(path)
elif args.task.lower() in ['wiki5m_trans', 'wiki5m_ind']:
all_examples += preprocess_wiki5m(path, is_train=(path == args.train_path))
elif args.task.lower() in ['mopenkb']:
all_examples += preprocess_mopenkb(path, is_train=(path == args.train_path))
else:
assert False, 'Unknown task: {}'.format(args.task)
if args.task.lower() == 'wn18rr':
id2text = {k: v[2] for k, v in wn18rr_id2ent.items()}
elif args.task.lower() == 'fb15k237':
id2text = {k: v[2] for k, v in fb15k_id2ent.items()}
elif args.task.lower() in ['wiki5m_trans', 'wiki5m_ind']:
id2text = wiki5m_id2text
elif args.task.lower() in ['mopenkb']:
id2text = mopenkb_id2text
else:
assert False, 'Unknown task: {}'.format(args.task)
dump_all_entities(all_examples,
out_path='{}/entities.json'.format(os.path.dirname(args.train_path)),
id2text=id2text)
print('Done')
if __name__ == '__main__':
main()