forked from zhouhanhanhan/EIGAT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_dataset_files.py
71 lines (63 loc) · 2.5 KB
/
create_dataset_files.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
import numpy as np
def getID(folder='data/umls/'):
lstEnts = {}
lstRels = {}
with open(folder + 'train.txt') as f, open(folder + 'train_marked.txt', 'w') as f2:
count = 0
for line in f:
line = line.strip().split()
line = [i.strip() for i in line]
# print(line[0], line[1], line[2])
if line[0] not in lstEnts:
lstEnts[line[0]] = len(lstEnts)
if line[1] not in lstRels:
lstRels[line[1]] = len(lstRels)
if line[2] not in lstEnts:
lstEnts[line[2]] = len(lstEnts)
count += 1
f2.write(str(line[0]) + '\t' + str(line[1]) +
'\t' + str(line[2]) + '\n')
print("Size of train_marked set set ", count)
with open(folder + 'valid.txt') as f, open(folder + 'valid_marked.txt', 'w') as f2:
count = 0
for line in f:
line = line.strip().split()
line = [i.strip() for i in line]
# print(line[0], line[1], line[2])
if line[0] not in lstEnts:
lstEnts[line[0]] = len(lstEnts)
if line[1] not in lstRels:
lstRels[line[1]] = len(lstRels)
if line[2] not in lstEnts:
lstEnts[line[2]] = len(lstEnts)
count += 1
f2.write(str(line[0]) + '\t' + str(line[1]) +
'\t' + str(line[2]) + '\n')
print("Size of VALID_marked set set ", count)
with open(folder + 'test.txt') as f, open(folder + 'test_marked.txt', 'w') as f2:
count = 0
for line in f:
line = line.strip().split()
line = [i.strip() for i in line]
# print(line[0], line[1], line[2])
if line[0] not in lstEnts:
lstEnts[line[0]] = len(lstEnts)
if line[1] not in lstRels:
lstRels[line[1]] = len(lstRels)
if line[2] not in lstEnts:
lstEnts[line[2]] = len(lstEnts)
count += 1
f2.write(str(line[0]) + '\t' + str(line[1]) +
'\t' + str(line[2]) + '\n')
print("Size of test_marked set set ", count)
wri = open(folder + 'entity2id.txt', 'w')
for entity in lstEnts:
wri.write(entity + '\t' + str(lstEnts[entity]))
wri.write('\n')
wri.close()
wri = open(folder + 'relation2id.txt', 'w')
for entity in lstRels:
wri.write(entity + '\t' + str(lstRels[entity]))
wri.write('\n')
wri.close()
getID()