-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_to_triples.py
221 lines (191 loc) · 7.73 KB
/
json_to_triples.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
import os, json
from tqdm import tqdm
from rdflib import Literal
directory = '../wikidata_proc_json_2'
out_dir = 'ttl_files'
#WD="<http://www.wikidata.org/entity/"
WD="wd:"
WDT="wdt:"
num_p31_skipped = 0
def get_triple(s, p, o, sl=WD, pl=WDT, ol=WD):
triple = "{wds}{s} {wdp}{p} {wdo}{o} .".format(s=s, p=p, o=o, wds=sl, wdp=pl, wdo=ol)
return triple
def write_prefix(outf):
prefixlist = open('prefix.ttl', 'r').readlines()
for p in prefixlist:
outf.write(p)
def read_wikidata_short_1(directory, out_dir):
'''
The outermost index indexes the ‘subject entities’. The value of each wikidata_short_1[key] is a dict. with keys as pid’s (id’s corresponding to the relations/predicates). The value of each wikidata_short_1[key][pid] is a list of object entities with the outer key as the subject entity.
The (key, pid, object entity) is a usual KB triple.
'''
global num_p31_skipped
filename = os.path.join(directory, 'wikidata_short_1.json')
outfilename = os.path.join(out_dir, 'wikidata_short_1.ttl')
print('Loading file ', filename)
jobj = json.load(open(filename, 'r'))
print('Loaded file ', filename)
outf = open(outfilename, 'w')
write_prefix(outf)
for key in jobj.keys():
for pid in jobj[key].keys():
if pid == 'P31':
num_p31_skipped += 1
continue
if len(jobj[key][pid]) == 0:
t = get_triple(s=key, p = pid, o = 'Q0')
outf.write(t + '\n')
else:
for obj in jobj[key][pid]:
triple = get_triple(s=key, p=pid, o=obj)
outf.write(triple + '\n')
print(num_p31_skipped)
return None
def read_wikidata_short_2(directory, out_dir):
'''
The outermost index indexes the ‘subject entities’. The value of each wikidata_short_1[key] is a dict. with keys as pid’s (id’s corresponding to the relations/predicates). The value of each wikidata_short_1[key][pid] is a list of object entities with the outer key as the subject entity.
The (key, pid, object entity) is a usual KB triple.
'''
global num_p31_skipped
filename = os.path.join(directory, 'wikidata_short_2.json')
outfilename = os.path.join(out_dir, 'wikidata_short_2.ttl')
print('Loading file ', filename)
jobj = json.load(open(filename, 'r'))
print('Loaded file ', filename)
outf = open(outfilename, 'w')
write_prefix(outf)
for key in jobj.keys():
for pid in jobj[key].keys():
if pid == 'P31':
num_p31_skipped += 1
continue
if len(jobj[key][pid]) == 0:
t = get_triple(s=key, p = pid, o = 'Q0')
outf.write(t + '\n')
else:
for obj in jobj[key][pid]:
triple = get_triple(s=key, p=pid, o=obj)
outf.write(triple + '\n')
print('num_p31_skipped ', num_p31_skipped)
return None
def read_comp_wikidata_rev(directory, out_dir):
'''
The outermost index indexes the ‘object entities’. The value of each comp_wikidata_rev[key] is a dict. with keys as pid’s (id’s corresponding to the relations/predicates). The value of each comp_wikidata_rev[key][pid] is a list of subject entities with outer key as the object entity.
The (key, inverse(pid), subject entity) is a usual KB triple.
'''
global num_p31_skipped
filename = os.path.join(directory, 'comp_wikidata_rev.json')
outfilename = os.path.join(out_dir, 'comp_wikidata_rev.ttl')
print('Loading file ', filename)
jobj = json.load(open(filename, 'r'))
print('Loaded file ', filename)
outf = open(outfilename, 'w')
write_prefix(outf)
for key in jobj.keys():
for pid in jobj[key].keys():
if pid == 'P31':
num_p31_skipped += 1
continue
if len(jobj[key][pid]) == 0:
triple = get_triple(s='Q0', p=pid, o=key)
outf.write(triple + '\n')
else:
for obj in jobj[key][pid]:
# reversed
triple = get_triple(s=obj, p=pid, o=key)
outf.write(triple + '\n')
print('num_p31_skipped ', num_p31_skipped)
return None
def read_par_child_file(directory, out_dir):
count_so = {}
count_r = {}
filename = os.path.join(directory, 'par_child_dict.json')
outfilename = os.path.join(out_dir, 'par_child_dict.ttl')
#outfilename_p0 = os.path.join(out_dir, 'par_child_dict_p0.ttl')
print('Loading file ', filename)
jobj = json.load(open(filename, 'r'))
print('Loaded file ', filename)
outf = open(outfilename, 'w')
#outf_p0 = open(outfilename_p0, 'w')
write_prefix(outf)
#write_prefix(outf_p0)
keys = jobj.keys()
for parent in tqdm(keys):
childs = jobj[parent]
for child in childs:
#triple = get_triple(s=child, p='P31', o=parent)
triple = get_triple(s=child, p='P31', o=parent)
outf.write(triple + '\n')
#outf_p0.write(triple_p0 + '\n')
return count_so, count_r
def read_items_wikidata_n(directory, out_dir):
filename = os.path.join(directory, 'items_wikidata_n.json')
outfilename = os.path.join(out_dir, 'items_wikidata_n.ttl')
print('Loading file ', filename)
jobj = json.load(open(filename, 'r'))
print('Loaded file ', filename)
outf = open(outfilename, 'w')
all_triples = []
write_prefix(outf)
for k, v in tqdm(jobj.items()):
v = Literal(v).n3()
triple = get_triple(s = k, p = 'rdfs:label', o = v, sl=WD, pl='', ol='')
outf.write(triple + '\n')
#########################
def read_child_par_dict_immed(directory, out_dir):
name = 'child_par_dict_immed'
filename = os.path.join(directory, name + '.json')
outfilename = os.path.join(out_dir, name + '.ttl')
print('Loading file ', filename)
jobj = json.load(open(filename, 'r'))
print('Loaded file ', filename)
outf = open(outfilename, 'w')
write_prefix(outf)
for key, obj in jobj.items():
triple = get_triple(s=key, p='P31', o=obj)
outf.write(triple + '\n')
outf.close()
def read_child_par_dict_save(directory, out_dir):
'''
Example: "Q26521211 - Broad Leaze Farmhouse": "Q3947 - house",
'''
name = 'child_par_dict_save'
filename = os.path.join(directory, name + '.json')
outfilename = os.path.join(out_dir, name + '.ttl')
print('Loading file ', filename)
jobj = json.load(open(filename, 'r'))
print('Loaded file ', filename)
outf = open(outfilename, 'w')
write_prefix(outf)
for key, obj in jobj.items():
triple = get_triple(s=key, p='P31', o=obj)
outf.write(triple + '\n')
outf.close()
def read_child_all_parents_till_5_levels(directory, out_dir):
'''
Example
"Q15140120 - Lexus RC": [
"Q3231690 - automobile model"
],
'''
name = 'child_all_parents_till_5_levels'
filename = os.path.join(directory, name + '.json')
outfilename = os.path.join(out_dir, name + '.ttl')
print('Loading file ', filename)
jobj = json.load(open(filename, 'r'))
print('Loaded file ', filename)
outf = open(outfilename, 'w')
write_prefix(outf)
for key in jobj.keys():
for obj in jobj[key]:
triple = get_triple(s=key, p='P31', o=obj)
outf.write(triple + '\n')
outf.close()
read_wikidata_short_1(directory, out_dir)
read_wikidata_short_2(directory, out_dir)
read_comp_wikidata_rev(directory, out_dir)
read_par_child_file(directory, out_dir)
read_items_wikidata_n(directory, out_dir)
#read_child_par_dict_immed(directory, out_dir)
#read_child_par_dict_save(directory, out_dir)
#read_child_all_parents_till_5_levels(directory, out_dir)