-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_dataset_files.py
71 lines (65 loc) · 2.69 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
import numpy as np
from config import Config
args = Config()
args.load_config()
device = "cuda" if args.cuda else "cpu"
def getID():
folder = "{path}/{dataset}/".format(path=args.data_folder, dataset=args.dataset)
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)
with open(folder + 'entity2id.txt', 'w') as writeE2ID:
for entity in lstEnts:
writeE2ID.write(entity + ' ' + str(lstEnts[entity]))
writeE2ID.write('\n')
with open(folder + 'relation2id.txt', 'w') as writeR2ID:
for entity in lstRels:
writeR2ID.write(entity + ' ' + str(lstRels[entity]))
writeR2ID.write('\n')