-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess_data.py
More file actions
150 lines (135 loc) · 5.87 KB
/
preprocess_data.py
File metadata and controls
150 lines (135 loc) · 5.87 KB
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
import os
import random
import argparse
import numpy as np
from tqdm import tqdm
from utils import *
# function for load data
def load_alignments(path):
alignments = []
with open(path, "r", encoding="utf-8") as fr:
for line in tqdm(fr.readlines(), desc="Load Alignments"):
if line:
e1, e2 = [int(e) for e in line.strip().split("\t")]
alignments.append(e1, e2)
return np.array(alignments)
### generate name dict
def load_name_dict(path):
name_dict = {}
with open(path, "r", encoding="utf-8") as fr:
for line in tqdm(fr.readlines(), desc="Load Name"):
if line:
items = line.strip().split("\t")
if items[0].isdigit():
idx, name = int(items[0]), items[1]
else:
idx, name = int(items[1]), items[0]
name_dict[idx] = name.split("/")[-1]
return name_dict
def load_all_name_dict(data_dir):
### load ent name dict
ent_name_dict = None
if os.path.exists(os.path.join(data_dir, "ent_ids_1")):
ent_name_dict_1 = load_name_dict(os.path.join(data_dir, "ent_ids_1"))
ent_name_dict_2 = load_name_dict(os.path.join(data_dir, "ent_ids_2"))
ent_name_dict = merge_dict(ent_name_dict_1, ent_name_dict_2)
ent_ids = [int(idx) for idx in ent_name_dict.keys()]
min_eid, max_eid = min(ent_ids), max(ent_ids)
error_ent_ids = []
if len(ent_ids) != max_eid - min_eid + 1:
for eid in range(min_eid, max_eid+1):
if eid not in ent_ids:
error_ent_ids.append(eid)
### load rel name dict
rel_name_dict = None
if os.path.exists(os.path.join(data_dir, "rel_ids_1")):
rel_name_dict_1 = load_name_dict(os.path.join(data_dir, "rel_ids_1"))
rel_name_dict_2 = load_name_dict(os.path.join(data_dir, "rel_ids_2"))
rel_name_dict = merge_dict(rel_name_dict_1, rel_name_dict_2)
rel_ids = [int(idx) for idx in rel_name_dict.keys()]
min_rid, max_rid = min(rel_ids), max(rel_ids)
error_rel_ids = []
if len(rel_ids) != max_rid - min_rid + 1:
for rid in range(min_rid, max_rid+1):
if rid not in rel_ids:
error_rel_ids.append(rid)
### load time name dict
time_name_dict = None
if os.path.exists(os.path.join(data_dir, "time_id")):
time_name_dict = {}
with open(os.path.join(data_dir, "time_id"), "r", encoding="utf-8") as fr:
for line in tqdm(fr.readlines(), desc="Load Time ID"):
if line:
idx, time = line.strip().split("\t")
idx = int(idx)
if time == '' or time == '-400000':
time = '~'
time_name_dict[idx] = time
else:
time_size = 0
for i in range(2):
with open(os.path.join(data_dir, f"triples_{i+1}"), "r", encoding="utf-8") as fr:
for line in fr.readlines():
if line:
items = line.strip().split("\t")
if len(items) == 4:
tau = items[3]
time_size = max(tau + 1, time_size)
elif len(items) == 5:
ts, te = items[3:]
time_size = max(ts + 1, te + 1, time_size)
else:
break
if time_size > 0:
time_name_dict = {i:i for i in range(time_size)}
return {"ent": ent_name_dict, "rel": rel_name_dict, "time": time_name_dict}, error_ent_ids, error_rel_ids
### generate neighbors
def load_neighbors(path, error_ent_ids, error_rel_ids, neighbor_num=25):
neighbors = {}
with open(path, "r", encoding="utf-8") as fr:
for line in tqdm(fr.readlines(), desc="Load Neighbors"):
if line:
items = [int(item) for item in line.strip().split("\t")]
h, r, t = items[:3]
if h in error_ent_ids or t in error_ent_ids or r in error_rel_ids:
continue
if len(items) == 3:
neighbor = (h, r, t)
elif len(items) == 4:
tau = items[3]
neighbor = (h, r, t, tau, tau)
elif len(items) == 5:
ts, te = items[3:]
neighbor = (h, r, t, ts, te)
else:
raise Exception("Knowledge Tuples's length is not 3, 4, or 5.")
if h not in neighbors:
neighbors[h] = []
if t not in neighbors:
neighbors[t] = []
neighbors[h].append(neighbor)
neighbors[t].append(neighbor)
for eid, neigh in neighbors.items():
if len(neigh) > neighbor_num:
random.shuffle(neigh)
neighbors[eid] = neigh[:neighbor_num]
return neighbors
if __name__ == "__main__":
### arguments
parser = argparse.ArgumentParser()
parser.add_argument("--data", type=str, default="icews_wiki")
parser.add_argument("--neighbor_num", type=int, default=25)
args = parser.parse_args()
data_dir = os.path.join("data", args.data)
### generate name dict
name_dict, error_ent_ids, error_rel_ids = load_all_name_dict(data_dir)
### generate neighbors
neighbors1 = load_neighbors(os.path.join(data_dir, "triples_1"), error_ent_ids, error_rel_ids, neighbor_num=args.neighbor_num)
neighbors2 = load_neighbors(os.path.join(data_dir, "triples_2"), error_ent_ids, error_rel_ids, neighbor_num=args.neighbor_num)
neighbors = merge_dict(neighbors1, neighbors2)
### save dict
cand_dir = os.path.join(data_dir, "candidates")
if not os.path.exists(cand_dir):
os.makedirs(cand_dir)
dump_json(os.path.join(cand_dir, "name_dict"), name_dict)
dump_json(os.path.join(cand_dir, "neighbors"), neighbors)