-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.py
140 lines (115 loc) · 4.63 KB
/
utils.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
import os
import numpy as np
class Triple(object):
def __init__(self, head, tail, relation):
self.s = head
self.o = tail
self.r = relation
# self.t = tim
class Quadruple(object):
def __init__(self, head, tail, relation, tim):
self.s = head
self.o = tail
self.r = relation
self.t = tim
def get_total_number(inPath, fileName):
with open(os.path.join(inPath, fileName), 'r') as fr:
for line in fr:
line_split = line.split()
return int(line_split[0]), int(line_split[1]), int(line_split[2])
def load_quadruples(inPath, fileName, temFileName, fileName2 = None, temFileName2 = None, fileName3 = None, temFileName3 = None):
tem = np.load(os.path.join(inPath, temFileName)).tolist()
with open(os.path.join(inPath, fileName), 'r') as fr:
quadrupleList = []
quadrupleTotal = 0
times = set()
for line in fr:
quadrupleTotal += 1
line_split = line.split()
head = int(line_split[0])
tail = int(line_split[2])
rel = int(line_split[1])
time = tem[quadrupleTotal-1]
# times.add(time)
quadrupleList.append(Quadruple(head, tail, rel, time))
trainTotal = quadrupleTotal
if temFileName2 is not None:
tem2 = np.load(os.path.join(inPath, temFileName2)).tolist()
if fileName2 is not None:
assert quadrupleTotal != 0
with open(os.path.join(inPath, fileName2), 'r') as fr:
for line in fr:
quadrupleTotal += 1
line_split = line.split()
head = int(line_split[0])
tail = int(line_split[2])
rel = int(line_split[1])
time = tem2[quadrupleTotal-trainTotal-1]
quadrupleList.append(Quadruple(head, tail, rel, time))
trainTestTotal = quadrupleTotal
if temFileName3 is not None:
tem3 = np.load(os.path.join(inPath, temFileName3)).tolist()
if fileName3 is not None:
assert quadrupleTotal != 0
with open(os.path.join(inPath, fileName3), 'r') as fr:
for line in fr:
quadrupleTotal += 1
line_split = line.split()
head = int(line_split[0])
tail = int(line_split[2])
rel = int(line_split[1])
time = tem3[quadrupleTotal-trainTestTotal-1]
quadrupleList.append(Quadruple(head, tail, rel, time))
times = list(times)
times.sort()
tripleDict = {}
for quadruple in quadrupleList:
tripleDict[(quadruple.s, quadruple.o, quadruple.r)] = True
return quadrupleTotal, quadrupleList, tripleDict, times
def load_quadruples_TTransE(inPath, fileName, fileName2 = None, fileName3 = None):
quadrupleList = []
quadrupleTotal = 0
with open(os.path.join(inPath, fileName), 'r') as fr:
for line in fr:
quadrupleTotal += 1
line_split = line.split()
head = int(line_split[0])
tail = int(line_split[2])
rel = int(line_split[1])
time = int(line_split[3])
quadrupleList.append(Quadruple(head, tail, rel, time))
if fileName2 is not None:
assert quadrupleTotal != 0
with open(os.path.join(inPath, fileName2), 'r') as fr:
for line in fr:
quadrupleTotal += 1
line_split = line.split()
head = int(line_split[0])
tail = int(line_split[2])
rel = int(line_split[1])
time = int(line_split[3])
quadrupleList.append(Quadruple(head, tail, rel, time))
if fileName3 is not None:
assert quadrupleTotal != 0
with open(os.path.join(inPath, fileName3), 'r') as fr:
for line in fr:
quadrupleTotal += 1
line_split = line.split()
head = int(line_split[0])
tail = int(line_split[2])
rel = int(line_split[1])
time = int(line_split[3])
quadrupleList.append(Quadruple(head, tail, rel, time))
quadrupleDict = {}
for quadruple in quadrupleList:
quadrupleDict[(quadruple.s, quadruple.o, quadruple.r, quadruple.t)] = True
return quadrupleTotal, quadrupleList, quadrupleDict
def get_quadruple_t(quads, time):
return [quad for quad in quads if quad.t == time]
def getTimedict(inPath, fileName):
timedict = {}
with open(os.path.join(inPath, fileName), 'r') as fr:
for line in fr:
line_split = line.split()
timedict[line_split[0]] = line_split[1]
return timedict