-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfetch.py
255 lines (229 loc) · 7.2 KB
/
fetch.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
import csv
import json
import os
from json import JSONDecodeError
import requests
from analyze import extract_nodes, extract_links, extract_hubs, calc_stats
ENDPOINT = "https://query.wikidata.org/bigdata/namespace/wdq/sparql"
QUERY_TEMPLATE = """
SELECT
?human ?humanLabel ?humanDescription ?gender ?image ?birthdate ?deathdate
?spouse ?mother ?father ?child
?birthplace ?birthplaceLabel
?membership ?membershipLabel
?affiliation ?affiliationLabel
?occupation ?occupationLabel
?education ?educationLabel
?position ?positionLabel
WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "ko,en". }
?human wdt:P31 wd:Q5.
%s
?human wdt:P21 ?gender.
OPTIONAL { ?human wdt:P18 ?image. }
OPTIONAL { ?human wdt:P569 ?birthdate. }
OPTIONAL { ?human wdt:P570 ?deathdate. }
OPTIONAL {
?human p:P26 ?spouseStatement.
?spouseStatement ps:P26 ?spouse.
?spouseStatement wikibase:rank ?spouseRank.
}
OPTIONAL {
?human p:P25 ?motherStatement.
?motherStatement ps:P25 ?mother.
?motherStatement wikibase:rank ?motherRank.
}
OPTIONAL {
?human p:P22 ?fatherStatement.
?fatherStatement ps:P22 ?father.
?fatherStatement wikibase:rank ?fatherRank.
}
OPTIONAL {
?human p:P40 ?childStatement.
?childStatement ps:P40 ?child.
?childStatement wikibase:rank ?childRank.
}
OPTIONAL { ?human wdt:P19 ?birthplace. }
OPTIONAL { ?human wdt:P463/wdt:P1647* ?membership. }
OPTIONAL { ?human wdt:P1416/wdt:P1647* ?affiliation. }
OPTIONAL { ?human wdt:P106/wdt:P1647* ?occupation. }
OPTIONAL { ?human wdt:P69 ?education. }
OPTIONAL { ?human wdt:P39 ?position. }
FILTER(BOUND(?spouse) || BOUND(?mother) || BOUND(?father) || BOUND(?child))
}
"""
QUERY_CONDITIONS = [
(
"Korean",
"{ ?human wdt:P27 wd:Q884. } "
"UNION { ?human wdt:P27 wd:Q423. } "
"UNION { ?human wdt:P27 wd:Q28233. } "
"UNION { ?human wdt:P27 wd:Q28179. } "
"UNION { ?human wdt:P27 wd:Q18097. } "
"UNION { ?human wdt:P27 wd:Q503585. } "
),
(
"who has Korean spouse",
"?human wdt:P26 ?spouse. "
"{ ?spouse wdt:P27 wd:Q884. } "
"UNION { ?spouse wdt:P27 wd:Q423. } "
"UNION { ?spouse wdt:P27 wd:Q28233. } "
"UNION { ?spouse wdt:P27 wd:Q28179. } "
"UNION { ?spouse wdt:P27 wd:Q18097. } "
"UNION { ?spouse wdt:P27 wd:Q503585. } "
),
(
"who has Korean mother",
"?human wdt:P25 ?mother. "
"{ ?mother wdt:P27 wd:Q884. } "
"UNION { ?mother wdt:P27 wd:Q423. } "
"UNION { ?mother wdt:P27 wd:Q28233. } "
"UNION { ?mother wdt:P27 wd:Q28179. } "
"UNION { ?mother wdt:P27 wd:Q18097. } "
"UNION { ?mother wdt:P27 wd:Q503585. } "
),
(
"who has Korean father",
"?human wdt:P22 ?father. "
"{ ?father wdt:P27 wd:Q884. } "
"UNION { ?father wdt:P27 wd:Q423. } "
"UNION { ?father wdt:P27 wd:Q28233. } "
"UNION { ?father wdt:P27 wd:Q28179. } "
"UNION { ?father wdt:P27 wd:Q18097. } "
"UNION { ?father wdt:P27 wd:Q503585. } "
),
(
"who has Korean child",
"?human wdt:P40 ?child. "
"{ ?child wdt:P27 wd:Q884. } "
"UNION { ?child wdt:P27 wd:Q423. } "
"UNION { ?child wdt:P27 wd:Q28233. } "
"UNION { ?child wdt:P27 wd:Q28179. } "
"UNION { ?child wdt:P27 wd:Q18097. } "
"UNION { ?child wdt:P27 wd:Q503585. } "
),
]
NODES = {
"persons": (
("human", "key"),
("humanLabel", "name"),
("humanDescription", "description"),
("gender", "gender"),
("image", "image"),
("birthdate", "birthdate"),
("deathdate", "deathdate"),
),
"birthplaces": (
("birthplace", "key"),
("birthplaceLabel", "name"),
),
"memberships": (
("membership", "key"),
("membershipLabel", "name"),
),
"affiliations": (
("affiliation", "key"),
("affiliationLabel", "name"),
),
"occupations": (
("occupation", "key"),
("occupationLabel", "name"),
),
"educations": (
("education", "key"),
("educationLabel", "name"),
),
"positions": (
("position", "key"),
("positionLabel", "name"),
),
}
LINKS = [
"spouse",
"mother",
"father",
"child",
"birthplace",
"membership",
"affiliation",
"occupation",
"education",
"position",
]
def main():
# Load raw data
force_fetch = os.environ.get('FORCE_FETCH', '0') == '1'
if force_fetch or not os.path.exists('data/raw.json'):
data = fetch_data()
with open('data/raw.json', 'w', encoding="utf-8") as f:
json.dump(data, f)
else:
with open('data/raw.json', 'r', encoding='utf-8') as f:
data = json.load(f)
# Extract nodes
nodes = {}
for key, fields in NODES.items():
rows = extract_nodes(data, fields)
print(f'Unique {key}: {len(rows)}')
header = [field for _, field in fields]
with open(f"data/{key}.csv", "w", encoding="utf-8") as f:
to_csv(rows, header, f)
nodes[key] = {}
for row in rows:
node = dict(zip(header, row))
nodes[key][node["key"]] = node
# Check styles
too_long_descriptions = [
node
for node in nodes['persons'].values()
if len(node['description']) > 20
]
if len(too_long_descriptions) > 0:
print('Following nodes have too long description:')
for node in too_long_descriptions:
print(f'- https://www.wikidata.org/wiki/{node["key"]} : '
f'{node["description"]}')
# Extract links
links = extract_links(data, LINKS)
print(f'Unique links: {len(links)}')
with open(f"data/links.csv", "w", encoding="utf-8") as f:
to_csv(links, ["rel", "source", "target"], f)
# Extract hubs
hubs = extract_hubs(links, 3)[:100]
with open(f"data/hubs.csv", "w", encoding="utf-8") as f:
to_csv(hubs, ["key", "score", "degree"], f)
# Calculate statistics
print(f'Calculate statistics...', end='', flush=True)
stats = calc_stats(links)
subg = stats['subgraphs'][0]
print()
print(f'- n_nodes: {stats["nNodes"]}')
print(f'- n_edges: {stats["nEdges"]}')
print(f'- n_subgraphs: {len(stats["subgraphs"])}')
print(f'- max subgraph n_nodes: {subg["nNodes"]}')
print(f'- max subgraph avg. shortest path: {subg["avgShortestPath"]}')
with open(f"data/stats.json", "w", encoding="utf-8") as f:
json.dump(stats, f, indent=2)
def fetch_data():
data = []
for label, condition in QUERY_CONDITIONS:
print(f'Collecting {label}... ', end='', flush=True)
query = QUERY_TEMPLATE % condition
while True:
try:
new_data = requests.get(
ENDPOINT,
{"query": query, "format": "json"}
).json()["results"]["bindings"]
print(len(new_data))
data += new_data
break
except JSONDecodeError:
pass
return data
def to_csv(rows, fields, f):
w = csv.writer(f)
w.writerow(fields)
w.writerows(rows)
if __name__ == '__main__':
main()