-
Notifications
You must be signed in to change notification settings - Fork 15
/
data_utils.py
343 lines (262 loc) · 9.87 KB
/
data_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
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
from __future__ import absolute_import
import os
import re
import numpy as np
from collections import Counter
#process Path-QA or Conj-QA data&KB
# kb: h \t r \t t
# form: question \t ans \t e1#r1#e2#r2#e3#<end>#e3 \t ans1/ans2/ \t e1#r1#e2///e2#r2#e3#///s#r#t///s#r#t
# form: question \t ans \t e1#r1#ans#<end>#ans*e2#r2#ans#<end>#ans \t ans1/ \t e1#r1#e2///e2#r2#e3#///s#r#t///s#r#t \t e1/e2
# form: question \t ans \t e1#r1#e2#rc2#ec2#r2#e3#rc3#ec3#<end>#e3#<end>#e3 \t ans1/ \t e1#r1#e2///e2#r2#e3#///s#r#t///s#r#t
def process_data_c(KB_file, data_file, word2id, rel2id, ent2id, words, relations, entities):#relations is set, other is list(), *2id is dict()
read_KB(KB_file, entities, relations)
data,sentence_size,memory_size = read_data(data_file, words)
#set ids
if len(word2id)==0:
word2id['<unk>'] = 0
if len(rel2id)==0:
rel2id['<end>'] = 0
if len(ent2id)==0:
ent2id['<unk>'] = 0
for r in relations:
# same r_id in rel2id and word2id
if not rel2id.has_key(r):
rel2id[r] = len(rel2id)
if not word2id.has_key(r):
word2id[r] = len(word2id)
for e in entities:
if not ent2id.has_key(e):
ent2id[e] = len(ent2id)
for word in words:
if not word2id.has_key(word):
word2id[word] = len(word2id)
print ('here are %d words in word2id(vocab)' %len(word2id)) #75080
print ('here are %d relations in rel2id(rel_vocab)' %len(rel2id)) #13+1
print ('here are %d entities in ent2id(ent_vocab)' %len(ent2id)) #13+1
Triples, KBs, tails_size = get_KB(KB_file,ent2id,rel2id)
print "#records or Triples", len(np.nonzero(KBs)[0])
Q = []
QQ = []
A = []
AA = []
P = []
PP = []
S = []
SS = []
D = []
DD = []
for query, answer, path, answerset, subgraph, subject in data:
query = query.strip().split()
ls = max(0, sentence_size-len(query))
q = [word2id[w] for w in query] + [0] * ls
Q.append(q)
QQ.append(query)
a = np.zeros(len(ent2id)) # if use new ans-vocab, add 0 for 'end'
a[ent2id[answer]] = 1
A.append(a)
AA.append(ent2id[answer])
#p = [[ent2id[],rel2id[],ent2id[],rel2id[],ent2id[]], [], []]
# POSITION+'#'+"plays_position_inverse"+'#'+PLAYER+'*'+CLUB+'#'+"plays_in_club_inverse"+'#'+PLAYER
path = path.strip().split('*') #path = [POSITION+'#'+"plays_position_inverse"+'#'+PLAYER, CLUB+'#'+"plays_in_club_inverse"+'#'+PLAYER]
p=[]
for subpath in path:
subpath = subpath.split("#")
p.append([ent2id[subpath[0]], rel2id[subpath[1]], ent2id[subpath[2]],rel2id[subpath[3]],ent2id[subpath[4]]])
P.append(p) #N*2*3
PP.append(path)
sg = []
subgraph = subgraph.split('///') #subgraph is a list including many triple-str for memn2n-t
#isubgraph=list(set(subgraph.replace('///','#').split('#')))
ls = max(0, memory_size-len(subgraph))
b = 0
for t in subgraph:
t = t.split('#')
if not len(t)==3:
print "subgraph not a triple form!"
print t
tt = [ent2id[t[0]],rel2id[t[1]],ent2id[t[2]]]
if not tt in Triples.tolist():
b += 1
continue
sg.append(tt)
'''
for t in isubgraph:
sg.append([ent2id[t]])
'''
for i in range(ls):
#sg.append( [0,0,0] )
sg.append([0])
D.append(sg)
DD.append(subgraph)
anset = answerset.split('/')
anset = anset[:-1]
ass=[]
for a in anset:
ass.append(ent2id[a])
S.append(ass)
SS.append(anset)
return np.array(Q),np.array(A),np.array(P),np.array(D),np.array(S),QQ,AA,PP,DD,SS,Triples,KBs,sentence_size,memory_size, tails_size
def process_data(KB_file, data_file, word2id, rel2id, ent2id, words, relations, entities): #relations is set, other is list(), *2id is dict()
read_KB(KB_file, entities, relations)
data,sentence_size,memory_size = read_data(data_file, words)
#set ids
if len(word2id)==0:
word2id['<unk>'] = 0
if len(rel2id)==0:
rel2id['<end>'] = 0
if len(ent2id)==0:
ent2id['<unk>'] = 0
for r in relations:
# same r_id in rel2id and word2id
if not rel2id.has_key(r):
rel2id[r] = len(rel2id)
if not word2id.has_key(r):
word2id[r] = len(word2id)
for e in entities:
if not ent2id.has_key(e):
ent2id[e] = len(ent2id)
for word in words:
if not word2id.has_key(word):
word2id[word] = len(word2id)
print ('here are %d words in word2id(vocab)' %len(word2id)) #75080
print ('here are %d relations in rel2id(rel_vocab)' %len(rel2id)) #13+1
print ('here are %d entities in ent2id(ent_vocab)' %len(ent2id)) #13+1
Triples, KBs,tails_size = get_KB(KB_file,ent2id,rel2id)
print "#records or Triples", len(np.nonzero(KBs)[0])
Q = []
QQ = []
A = []
AA = []
P = []
PP = []
S = []
SS = []
D = []
DD = []
for query, answer, path, answerset, subgraph in data:
path = path.strip().split('#') #path = [s,r1,m,r2,t]
#answer = path[-1]
query = query.strip().split()
ls = max(0, sentence_size-len(query))
q = [word2id[w] for w in query] + [0] * ls
Q.append(q)
QQ.append(query)
a = np.zeros(len(ent2id)) # if use new ans-vocab, add 0 for 'end'
a[ent2id[answer]] = 1
A.append(a)
AA.append(ent2id[answer])
#p = [ ent2id[path[0]], rel2id[path[1]], ent2id[path[2]], rel2id[path[3]], ent2id[path[4]] ]
p=[]
for i in range(len(path)):
if i % 2 == 0:
e = ent2id[path[i]]
# e = np.zeros(len(relations))
# e[0] = ent2id[path[i]]
p.append(e)
else:
r = rel2id[path[i]]
# r = np.zeros(len(relations))
# r[rel2id[path[i]]] =1
p.append(r)
#p.append(rel2id[path[3]])
#p.append(ent2id[path[4]])
P.append(p)
PP.append(path)
sg = []
subgraph = subgraph.split('///') #subgraph is a list including many triple-str for memn2n-t |||| 3 in this function & 1 in read-doc
#isubgraph=list(set(subgraph.replace('///','#').split('#')))
ls = max(0, memory_size-len(subgraph))
for t in subgraph:
t = t.split('#')
if not len(t)==3:
print "subgraph not a triple form!"
print t
tt = [ent2id[t[0]],rel2id[t[1]],ent2id[t[2]]]
if not tt in Triples.tolist():
ls += 1 #add padding
continue
sg.append(tt)
'''
for t in isubgraph:
sg.append([ent2id[t],0,0])
'''
for i in range(ls):
sg.append( [0,0,0] )
D.append(sg)
DD.append(subgraph)
anset = answerset.split('/')
anset = anset[:-1]
ass=[]
for a in anset:
ass.append(ent2id[a])
S.append(ass)
SS.append(anset)
# return Q,A,P,D,QQ,AA,PP,DD,KBs,sentence_size,memory_size,tails_size
return np.array(Q),np.array(A),np.array(P),np.array(D),np.array(S),QQ,AA,PP,DD,SS,Triples,KBs,sentence_size,memory_size, tails_size
def read_KB(KB_file, entities, relations):
#example in KB_file: KBs.txt h \t r \t t
if os.path.isfile(KB_file):
with open(KB_file) as f:
lines = f.readlines()
else:
raise Exception("!! %s is not found!!" % KB_file)
for line in lines:
line = line.strip().split('\t')
entities.add(line[0])
entities.add(line[2])
relations.add(line[1])
def get_KB(KB_file,ent2id,rel2id):
nwords = len(ent2id)
nrels = len(rel2id)
tails = np.zeros([nwords*nrels,1], 'int32')
#KBmatrix = np.zeros([nwords, nrels,nwords], 'int32')
KBmatrix = np.zeros([nwords * nrels,nwords], 'int32')
Triples = []
f = open(KB_file)
control = 1
b = 0
for line in f.readlines():
line = line.strip().split('\t')
''' delete half triples
control += 1
if control % 2 == 0:
b += 1
continue
'''
h = ent2id[line[0]]
r = rel2id[line[1]]
t = ent2id[line[2]]
Triples.append([h,r,t])
#[h,r]->[h*nrels+r]
lenlist = tails[h*nrels+r]
KBmatrix[h*nrels+r,lenlist] = t
tails[h*nrels+r]+=1
print "delete triples:", b
return np.array(Triples), KBmatrix[:,:np.max(tails)], np.max(tails)
def read_data(data_file, words):
#example in data_file: WC-C: q+'\t'+ans+'\t'+p+'\t'+ansset+'\t'+c+'\t'+sub+'\n'
if os.path.isfile(data_file):
with open(data_file) as f:
lines = f.readlines()
else:
raise Exception("!! %s is not found!!" % data_file)
data = []
questions = []
doc = []
for line in lines:
line = line.strip().split('\t')
data.append(line)
for w in line[0].strip().split():
words.add(w)
questions.append(line[0].strip().split())
doc.append(line[4].strip().split('///')) #for memn2n-triple
#doc.append(list(set(line[4].strip().replace('///','#').split('#')))) #for memn2n-entity
sentence_size = max(len(i) for i in questions)
memory_size = max(len(i) for i in doc)
return data, sentence_size , memory_size
def tokenize(sent):
'''Return the tokens of a sentence including punctuation.
>>> tokenize('Bob dropped the apple. Where is the apple?')
['Bob', 'dropped', 'the', 'apple', '.', 'Where', 'is', 'the', 'apple', '?']
'''
return [x.strip() for x in re.split('(\W+)?', sent) if x.strip()]