-
Notifications
You must be signed in to change notification settings - Fork 18
/
data_creator_step2.py
283 lines (210 loc) · 9.14 KB
/
data_creator_step2.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
'''
>write a script to collect everything from a folder
>Use the same script to find all relations
>Create a piclke of relation in that specific form
>Idfy everything with respect to relation
>Store it
The relation file structure would be
[
'http://dbpedia.org/property/ratifiers' : ['ID','SF','SF Tokenized','SF ID']
]
'''
import pickle
import json
import os
import numpy as np
import copy
from utils import dbpedia_interface as dbi
from utils import natural_language_utilities as nlutils
from utils import embeddings_interface as ei
ei.__check_prepared__()
def collect_files(dir_location):
'''
:param dir_location: json file location (No name needed) --> /data/data/raw/lcquad/success
:return: big json combining all the files in the given location
'''
file_list = [os.path.join(dir_location,file) for file in os.listdir(dir_location)]
json_list = [json.load(open(file)) for file in file_list]
final_data = []
for node in json_list:
final_data = final_data + node
return final_data
def update_relation_dict(relation,relation_dict,dbp):
'''
Updates the relation dict if the relation doesn't exists
Also returns the id for the same
:param relation: 'http://dbpedia.org/property/services'
:param relation_dict: {}
:return: id version of the relation and as well as relation_dict
['ID','SF','SF Tokenized','SF ID']
'SF ID' = embeddings_interface.vocabularize(surface_form_tokenized)
'''
if relation in relation_dict.keys():
rel_id = relation_dict[relation][0]
else:
rel_id = len(relation_dict)
surface_form = dbp.get_label(relation)
surface_form_tokenized = nlutils.tokenize(surface_form)
relation_dict[relation] = [len(relation_dict),surface_form,surface_form_tokenized
,ei.vocabularize(surface_form_tokenized)]
return rel_id,relation_dict
def idfy_path(path,relation_dict,dbp):
'''
:param path: ['+', 'http://dbpedia.org/property/services', '-','optinalpath']
:return: checks if the relations in path exists in dict and if not update the relation file
> Also return an id version of the path.
'''
if len(path) == 2:
rel_id,relation_dict = update_relation_dict(relation=path[1],relation_dict=relation_dict, dbp=dbp)
return [path[0],rel_id],relation_dict
else:
rel1_id,relation_dict = update_relation_dict(relation=path[1],relation_dict=relation_dict, dbp=dbp)
rel2_id,relation_dict = update_relation_dict(relation=path[3],relation_dict=relation_dict, dbp=dbp)
return [path[0],rel1_id,path[2],rel2_id],relation_dict
def idfy_const(const,relation_dict,dbp):
'''
:param const: 'http://dbpedia.org/property/services' constraint has no sign
:param relation_dict:
:return: idfy const,updated relation dict
'''
return update_relation_dict(const,relation_dict,dbp)
def idfy_relations_in_node(node,relation_dict,dbp):
'''
Given a node, idfy all the relation and if the relation doesn't exists in the relation
dictionary, update the rel dict.
:param node: data node
:param relation_dict:
:return:
'''
if node['path']:
node['path'],relation_dict = idfy_path(node['path'],relation_dict,dbp)
for index,path in enumerate(node['hop1']):
node['hop1'][index],relation_dict = idfy_path(path,relation_dict,dbp)
for index,path in enumerate(node['hop2']):
node['hop2'][index], relation_dict = idfy_path(path, relation_dict,dbp)
for index,path in enumerate(node['rdf_constraint']['candidates']['uri']):
node['rdf_constraint']['candidates']['uri'][index],relation_dict = idfy_const(path,relation_dict,dbp)
for index,path in enumerate(node['rdf_constraint']['candidates']['x']):
node['rdf_constraint']['candidates']['x'][index],relation_dict = idfy_const(path,relation_dict,dbp)
return node,relation_dict
def sort_list1_wrt_list2(list1,list2):
idlist = {v['_id']: index for index, v in enumerate(list2)}
def getKey(node):
return idlist[node['node']['_id']]
return sorted(list1,key=getKey)
def vectorize_entity(entity,dbp):
'''
:param entity: [e1,e2] where e is of the form 'http://dbpedia.org/resource/Bill_Finger'
:param dbp:
:return:
'''
vector_ent = ei.vectorize(nlutils.tokenize(dbp.get_label(entity[0])))
if len(entity) > 1:
for e in entity:
vector_ent = np.vstack((vector_ent , ei.vectorize(nlutils.tokenize(dbp.get_label(e)))))
return vector_ent
def run(dataset):
dataset = dataset
_save_location_success = 'data/data/raw/%(dataset)s/success'
_save_location_unsuccess = 'data/data/raw/%(dataset)s/unsuccess'
relation_dict_location = 'data/data/common/relations.pickle'
relation_dict_dir = 'data/data/common/'
final_data_location = 'data/data/%(dataset)s/id_big_data.json'
final_data_location_combine = 'data/data/raw/%(dataset)s/combine'
final_data_dir = 'data/data/%(dataset)s/'
dbp = dbi.DBPedia(caching=True)
'''
check if the relation dict exist and
if it does
load it from disk
else
create a new one
'''
if os.path.isfile(relation_dict_location):
relation_dict = pickle.load(open(relation_dict_location, 'rb'))
# To dump -> pickle.dump(relation_dict,open('data/data/common/text.pickle','wb+'))
else:
nlutils.create_dir(relation_dict_dir)
relation_dict = {}
combined_data = collect_files(_save_location_success % {'dataset':dataset})
combined_data_un = collect_files(_save_location_unsuccess % {'dataset':dataset})
nlutils.create_dir(final_data_location_combine % {'dataset':dataset})
json.dump(combined_data,open(os.path.join(final_data_location_combine % {'dataset':dataset},'success.json'),'w+'))
json.dump(combined_data_un,open(os.path.join(final_data_location_combine % {'dataset':dataset},'unsuccess.json'),'w+'))
final_combine_data = combined_data+combined_data_un
if dataset == 'lcquad':
final_combine_data = sort_list1_wrt_list2(final_combine_data,json.load(open('resources/lcquad_data_set.json')))
'''
The final_combine_data needs to be re-ordered so that it could be directly split into training-validation-testing
'''
copy_index = []
for index,node in enumerate(final_combine_data):
if 'path' not in list(node.keys()):
copy_index.append(index)
for i in copy_index:
final_combine_data[i] = copy.deepcopy(final_combine_data[731])
#Now here one can create a vocabulary
for index,node in enumerate(final_combine_data):
final_combine_data[index],relation_dict = idfy_relations_in_node(node,relation_dict=relation_dict,dbp=dbp)
'''
For hiearchial relation detection module one need all the relation (uri)
have a randomly init vectors.
'''
# keys = list(relation_dict.keys())
# ei.update_vocab(keys)
for rel in relation_dict:
relation_dict[rel].append(ei.vocabularize([rel],False))
print("done dumping relation")
pickle.dump(relation_dict,open(relation_dict_location,'wb+'))
'''
Consider dumping here. So that alsong with relationid file and this dump
one can do their own form of pre-processing
'''
#Vocabularize everything and then padding.
'''
location
Things to vocabularize
>question
>path
>hop1
>hop2
'''
id_data = []
x_id = int(ei.vocabularize(['x'])[0])
uri_id = int(ei.vocabularize(['uri'])[0])
for index,node in enumerate(final_combine_data):
temp = {
'uri' : {
'question-id' : [int(id) for id in list(ei.vocabularize(nlutils.tokenize(node['node']['corrected_question'])))],
'hop-2-properties' : node['hop2'],
'hop-1-properties' : node['hop1'],
# 'entity-id':vectorize_entity(node['entity'],dbp)
},
'parsed-data' : {
'node':node['node'],
'parsed_sparql':node['parsed_sparql'],
'path':node['path'],
'entity':node['entity'],
'constraints':node['constraints'],
'updated_sparql':node['updated_sparql'],
'error_flag':node['error_flag']
},
'rdf-type-constraints' : []
}
rdf_candidates = []
for candidate_id in node['rdf_constraint']['candidates']['uri']:
rdf_candidates.append([uri_id,candidate_id])
for candidate_id in node['rdf_constraint']['candidates']['x']:
rdf_candidates.append([x_id, candidate_id])
temp['rdf-type-constraints'] = rdf_candidates
id_data.append(temp)
#embedding interface update vocab here
nlutils.create_dir(final_data_dir %{'dataset':dataset})
json.dump(id_data,open(final_data_location %{'dataset':dataset},'w+'))
if __name__ == '__main__':
run('lcquad')
# run('qald')
#update the vector file and the vocab file
#vocab file is word,index and the vector file is just vectors
#ei.align_id_space()
#ei.__check_prepared__()