-
Notifications
You must be signed in to change notification settings - Fork 3
/
preprocess_yago.py
343 lines (313 loc) · 13.5 KB
/
preprocess_yago.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
343
# -*- coding: utf-8 -*-
#YAGO preprocessing file.
#The label file, type file, subclass file and the fact file are converted into python dictionaries.
import pickle
import re
from collections import defaultdict
def getParentAndChild(line):
triple = line.replace(",","temp_placeholder")
triple = triple.lower().split('\t',maxsplit = 4)
child = triple[0].replace("temp_placeholder",",")
child = child.strip()
tempList = child.rsplit("/",1)
child = tempList[-1][:-1]
parent = triple[2].replace("temp_placeholder",",")
parent = parent.strip()
tempList = parent.rsplit("/",1)
parent = tempList[-1][:-1]
return (parent, child)
def findSubClasses(path):
i = 1
j = 1
dictionary = {}
with open(path,'r', encoding = "UTF-8") as infile:
for line in infile:
line = line.lower()
#sample line: <http://yago-knowledge.org/resource/fishing_tackle> <http://www.w3.org/2000/01/rdf-schema#subclassof> <http://schema.org/thing> .
j+=1
if "rdf-schema#subclassof" in line and "http://schema.org/thing" in line:
parent, child = getParentAndChild(line)
dictionary[child] =[]
i+=1
if i % 1000 == 0:
print("Written lines:",i)
print("Processed lines:",j)
print("Class file written in class dictionary.")
return dictionary
def findAllEdges(path):
i = 1
j = 1
edges = []
with open(path,'r', encoding = "UTF-8") as infile:
for line in infile:
line = line.lower()
#sample line: <http://yago-knowledge.org/resource/fishing_tackle> <http://www.w3.org/2000/01/rdf-schema#subclassof> <http://schema.org/thing> .
j+=1
if "rdf-schema#subclassof" in line and "http://schema.org/thing" not in line:
parent, child = getParentAndChild(line)
edges.append((parent,child))
i+=1
if i % 1000 == 0:
print("Written lines:",i)
print("Processed lines:",j)
print("Class file written in class dictionary.")
return edges
#using bfs to create the inheritance dictionary:
class Hierarchy:
# class Constructor
def __init__(self):
#define dictionary of list. This will store the hierarchy
self.hierarchy = defaultdict(list)
def addEdge(self,u,v):
self.hierarchy[u].append(v)
# Function to compute BFS
def BreadthFirstSearch(self, top_type):
visited = {}
for key in self.hierarchy:
visited[key] = False
# BFS waiting buffer
buffer = []
buffer.append(top_type)
visited[top_type] = True
while buffer:
top_type = buffer.pop(0)
for i in self.hierarchy[top_type]:
if visited.get(i, "None") == False:
buffer.append(i)
visited[i] = True
else:
visited[i] = True
return visited
SCHEMA_FILE_PATH = r"../yago/yago_original/yago-wd-schema.nt" #contains labels and some types too
#ENTITY DICTIONARY
LABEL_INPUT_FILE_PATH = r"../yago/yago_original/yago-wd-labels.nt"
LABEL_OUTPUT_FILE_PATH = r"../yago/yago_pickle/yago-wd-labels_dict.pickle"
#TYPE DICTIONARY
FULL_TYPES_INPUT_FILE_PATH = r"../yago/yago_original/yago-wd-full-types.nt"
SIMPLE_TYPES_INPUT_FILE_PATH = r"../yago/yago_original/yago-wd-simple-types.nt"
TYPES_OUTPUT_FILE_PATH = r"../yago/yago_pickle/yago-wd-full-types_dict.pickle"
#INHERITANCE DICTIONARY
CLASS_INPUT_FILE_PATH = r"../yago/yago_original/yago-wd-class.nt"
CLASS_OUTPUT_FILE_PATH = r"../yago/yago_pickle/yago-wd-class_dict.pickle"
#RELATIONSHIP DICTIONARY
FACTS_INPUT_FILE_PATH = r"../yago/yago_original/yago-wd-facts.nt"
FACTS_OUTPUT_FILE_PATH = r"../yago/yago_pickle/yago-wd-facts_dict.pickle"
#entity dictionary
#1. Convert label file into pickle file. The dictionary stores label as key and entity URI as value
i = 1
j = 1
dictionary = {}
earlierEntity = []
with open(SCHEMA_FILE_PATH,'r', encoding = "UTF-8") as infile:
for line in infile:
#sample line:
#<http://schema.org/performinggroup> <http://www.w3.org/2000/01/rdf-schema#label> "performing group" .
#expected output:
#{performing group: [performinggroup]} i.e. label : [entity]
line = line.lower()
if "rdf-schema#label" in line:
triple = line.replace(",","temp_placeholder")
triple = triple.lower().split('\t',maxsplit = 4)
label = triple[2].replace("temp_placeholder", "")
label = label.replace("@en", "")
label = label.replace('"', '')
label = re.sub(r'[^\w]', ' ', label)
label = " ".join(label.split())
entity = triple[0].replace("temp_placeholder",",")
entity = entity.strip()
entityList = entity.rsplit("/",1)
entity = entityList[-1][:-1]
if label in dictionary:
earlierEntity = dictionary[label]
newEntity = earlierEntity + [entity]
dictionary[label] = list(set(newEntity))
else:
dictionary[label] = [entity]
print("Schema label file written in entity dictionary.")
#go through each lines of input file and write them to output file after conversion
with open(LABEL_INPUT_FILE_PATH,'r', encoding = "UTF-8") as infile:
for line in infile:
#Sample line: <http://yago-knowledge.org/resource/ecclesiastes> <http://schema.org/alternatename> "book of ecclesiastes"@en .
#expected output: {book of ecclesiastes: [ecclesiastes]} i.e. label : [entity]
line = line.lower()
j+=1
if "@en" in line and "rdf-schema#comment" not in line:
triple = line.replace(",","temp_placeholder")
triple = triple.lower().split('\t',maxsplit = 4)
label = triple[2].replace("temp_placeholder", "")
label = label.replace("@en", "")
label = label.replace('"', '')
label = re.sub(r'[^\w]', ' ', label)
label = " ".join(label.split())
entity = triple[0].replace("temp_placeholder",",")
entity = entity.strip()
entityList = entity.rsplit("/",1)
entity = entityList[-1][:-1]
if label in dictionary:
earlierEntity = dictionary[label]
newEntity = earlierEntity + [entity]
dictionary[label] = list(set(newEntity))
else:
dictionary[label] = [entity]
i+=1
if i % 1000000 == 0:
print("Written lines:",i)
print("Processed lines:",j)
print("Label file written in entity dictionary.")
file_pointer=open(LABEL_OUTPUT_FILE_PATH, 'wb')
pickle.dump(dictionary,file_pointer, protocol=pickle.HIGHEST_PROTOCOL)
#type dictionary
#2. convert type file into dictionary. This stores the entity URIs as key and types as values.
dictionary = {}
earlierType = []
with open(SCHEMA_FILE_PATH,'r', encoding = "UTF-8") as infile:
for line in infile:
line = line.lower()
#sample line:
# <http://schema.org/performinggroup> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#class> .
#expected output:
# {performinggroup: [performinggroup]} i.e. entity: type
if "22-rdf-syntax-ns#type" in line and "2002/07/owl#class" in line:
triple = line.replace(",","temp_placeholder")
triple = triple.lower().split('\t',maxsplit = 4)
entity = triple[0].replace("temp_placeholder",",")
entity = entity.strip()
entityList = entity.rsplit("/",1)
entity = entityList[-1][:-1]
if entity[0] != "_":
if entity in dictionary:
earlierType = dictionary[entity]
newEntity = earlierType + [entity]
dictionary[entity] = list(set(newEntity))
else:
dictionary[entity] = [entity]
print("Schema type file written in type dictionary.")
i = 1
j = 1
with open(FULL_TYPES_INPUT_FILE_PATH,'r', encoding = "UTF-8") as infile:
for line in infile:
line = line.lower()
#sample line: <http://yago-knowledge.org/resource/harald_ringstorff> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://yago-knowledge.org/resource/human> .
#expected output: {harald_ringstorff: [human, person]} i.e. entity: [type]
j+=1
if "type" in line:
triple = line.replace(",","temp_placeholder")
triple = triple.lower().split('\t',maxsplit = 4)
entity = triple[0].replace("temp_placeholder",",")
typeName = triple[2].replace("temp_placeholder",",")
typeName = typeName.strip()
#instead of full type url, extract its name only.
typeList = typeName.rsplit("/",1)
typeName = typeList[-1][:-1]
entity = entity.strip()
entityList = entity.rsplit("/",1)
entity = entityList[-1][:-1]
if entity in dictionary:
earlierType = dictionary[entity]
newEntity = earlierType + [typeName]
dictionary[entity] = list(set(newEntity))
else:
dictionary[entity] = [typeName]
i+=1
if i % 1000000 == 0:
print("Written lines:",i)
print("Processed lines:",j)
print("Full types file written in type dictionary.")
i = 1
j = 1
with open(SIMPLE_TYPES_INPUT_FILE_PATH,'r', encoding = "UTF-8") as infile:
for line in infile:
line = line.lower()
#sample line: <http://yago-knowledge.org/resource/pulicat_lake_bird_sanctuary> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/animalshelter> .
#expected output: {pulicat_lake_bird_sanctuary: [animalshelter]} i.e. entity: [type]
j+=1
if "type" in line:
triple = line.replace(",","temp_placeholder")
triple = triple.lower().split('\t',maxsplit = 4)
entity = triple[0].replace("temp_placeholder",",")
typeName = triple[2].replace("temp_placeholder",",")
typeName = typeName.strip()
#instead of full type url, extract its name only.
typeList = typeName.rsplit("/",1)
typeName = typeList[-1][:-1]
entity = entity.strip()
entityList = entity.rsplit("/",1)
entity = entityList[-1][:-1]
if entity in dictionary:
earlierType = dictionary[entity]
newType = []
newType = earlierType + [typeName]
dictionary[entity] = list(set(newType))
else:
dictionary[entity] = [typeName]
i+=1
if i % 1000000 == 0:
print("Written lines:",i)
print("Processed lines:",j)
print("Simple types file written in type dictionary.")
file_pointer=open(TYPES_OUTPUT_FILE_PATH, 'wb')
pickle.dump(dictionary,file_pointer, protocol=pickle.HIGHEST_PROTOCOL)
#inheritance dictionary
#3. convert class file into dictionary.
#This stores the types having thing as top level type as key and a list
# of its children as value.
dictionaryClass = findSubClasses(CLASS_INPUT_FILE_PATH)
topTypes = set()
for key in dictionaryClass:
topTypes.add(key)
allSchemaEdges = findAllEdges(SCHEMA_FILE_PATH)
allClassEdges = findAllEdges(CLASS_INPUT_FILE_PATH)
allEdges = list(set(allSchemaEdges + allClassEdges))
hrchy = Hierarchy()
for edge in allEdges:
hrchy.addEdge(edge[0],edge[1])
dictionary = {}
for key in dictionaryClass:
dictionaryClass[key] = hrchy.BreadthFirstSearch(key)
for edge in dictionaryClass:
visited_stats =hrchy.BreadthFirstSearch(edge)
for result in visited_stats:
if visited_stats[result]:
if edge in dictionary:
dictionary[edge].append(result)
else:
dictionary[edge] = [result]
file_pointer=open(CLASS_OUTPUT_FILE_PATH, 'wb')
pickle.dump(dictionary, file_pointer, protocol=pickle.HIGHEST_PROTOCOL)
#relationship dictionary
#4. convert fact file into fact dictionary. This stores the relation semantics.
i = 1
dictionary = {}
earlierRelation = []
with open(FACTS_INPUT_FILE_PATH,'r', encoding = "UTF-8") as infile:
for line in infile:
line = line.lower()
triple = line.replace(",","temp_placeholder")
triple = triple.lower().split('\t',maxsplit = 4)
subject = triple[0].replace("temp_placeholder",",")
subject = subject.strip()
subjectList = subject.rsplit("/",1)
subject = subjectList[-1][:-1]
obj = triple[2].replace("temp_placeholder",",")
obj = obj.strip()
objList = obj.rsplit("/",1)
obj = objList[-1][:-1]
key = subject + "__"+obj
predicate = triple[1].replace("temp_placeholder",",")
predicate = predicate.strip()
predList = predicate.rsplit("/",1)
value = predList[-1][:-1]
if key in dictionary:
earlierRelation = dictionary[key]
newRelation = []
newRelation = earlierRelation + [value]
dictionary[key] = list(set(newRelation))
else:
dictionary[key] = [value]
i+=1
if i % 1000000 == 0:
print("Written lines:",i)
print("Fact file written in relation dictionary.")
file_pointer=open(FACTS_OUTPUT_FILE_PATH, 'wb')
pickle.dump(dictionary,file_pointer, protocol=pickle.HIGHEST_PROTOCOL)
file_pointer.close()