-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikidata.py
324 lines (256 loc) · 9.27 KB
/
wikidata.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
import re
import os
import json
import requests
from time import sleep
from math import log
from collections import defaultdict
from secrets import EMAIL
DATA_DIR = 'intermediate_data_files'
LOC_INFO_FILENAME = os.path.join(DATA_DIR, 'wikidata_locations_info.json')
RANKING_FILENAME = os.path.join(DATA_DIR, 'wikidata_ranking.json')
NORM_LOC_FILENAME = os.path.join(DATA_DIR, 'locations_normalized.json')
COEFS_FILENAME = 'coefficients.json'
SPARQL_URL = 'https://query.wikidata.org/sparql' # for SPARQL queries
ENTITY_URL = 'https://www.wikidata.org/wiki/Special:EntityData/%s.json' # for retrieving entities by ID
USER_AGENT = {'User-Agent': f'Location extractor ({EMAIL})'} # to avoid ban
# limit entities to subclasses of
# wd:Q82794 - `geographic region` and
# wd:Q2221906 - `geographic location.
# Heavy query:
# use fulltext search (for speed) to search by label and aliases;
# limit search and output to relevant language;
# output id, primary label, and all aliases.
GEO_REGION = 'Q82794'
GEO_LOC = 'Q2221906'
HEAVY_CANDIDATE_QUERY = """
SELECT DISTINCT ?locId ?label ?altLabel WHERE {
?loc wdt:P31/wdt:P279* wd:%s .
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:endpoint "www.wikidata.org";
wikibase:api "Generator";
mwapi:generator "search";
mwapi:gsrsearch 'inlabel:"%s@%s"';
mwapi:gsrlimit "max".
?loc wikibase:apiOutputItem mwapi:title.
}
SERVICE wikibase:label {
bd:serviceParam wikibase:language "%s".
?loc rdfs:label ?label;
skos:altLabel ?altLabel.
}
BIND(STRAFTER(STR(?loc), STR(wd:) ) as ?locId)
}
"""
# light query:
# exact match with primary label;
# output id and primary label.
LIGHT_CANDIDATE_QUERY = """
SELECT DISTINCT ?locId ?label WHERE {
?loc wdt:P31/wdt:P279* wd:%s;
rdfs:label "%s"@%s.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "%s".
?loc rdfs:label ?label.
}
BIND(STRAFTER(STR(?loc), STR(wd:) ) as ?locId)
}
"""
# query to get links TO the page
LINKS_QUERY = """
SELECT DISTINCT (COUNT(?entity) AS ?entityCount) WHERE {
?entity ?property wd:%s .
?prop wikibase:directClaim ?property .
}
"""
def create_loc_regexes(loc):
"""
Create regexes for general and bracketed locations.
"""
loc_regex = re.compile(rf'{loc}', flags=re.I)
loc_regex_brackets = re.compile(rf'\([^)]*?{loc}[^(]*?\)', flags=re.I)
return loc_regex, loc_regex_brackets
def get_candidates(ancestor_id, loc_name, lang, loc_regex, loc_regex_brackets):
"""
Query Wikidata by location name and language.
Filter and reformat results.
"""
heavy_query = HEAVY_CANDIDATE_QUERY % (ancestor_id, loc_name, lang, lang)
light_query = LIGHT_CANDIDATE_QUERY % (ancestor_id, loc_name, lang, lang)
try:
r = requests.get(
SPARQL_URL,
params={'format': 'json', 'query': heavy_query},
headers=USER_AGENT,
timeout=60
)
# If heavy query timeouts (too many objects to retrieve),
# the entity is large and famous (e.g. capital or country),
# so run lighter exact match query.
except requests.exceptions.Timeout:
print(f'Heavy query timeout: {loc_name}, {lang}. Trying exact query')
r = requests.get(
SPARQL_URL,
params={'format': 'json', 'query': light_query},
headers=USER_AGENT
)
candidates = r.json()['results']['bindings']
candidates = [
c['locId']['value'] for c in candidates
if not is_bracketed(c, loc_regex, loc_regex_brackets)
]
return candidates
def is_bracketed(data, loc_regex, loc_regex_brackets):
"""
Check if the location is always mentioned in brackets
in all locations.
"""
all_labels = f"{data['label']['value']} {data.get('altLabel', {}).get('value', '')}"
n_locs = len(loc_regex.findall(all_labels))
n_locs_brackets = len(loc_regex_brackets.findall(all_labels))
return n_locs == n_locs_brackets
def get_entity(entity_id):
"""
Retrieve Wikidata entity by id.
"""
data = requests.get(
ENTITY_URL % entity_id,
headers=USER_AGENT
).json()['entities'][entity_id]
return data
def get_n_links(entity_id):
"""
Get number of links TO the entity.
"""
l_query = LINKS_QUERY % entity_id
l_r = requests.get(
SPARQL_URL,
params={'format': 'json', 'query': l_query},
headers=USER_AGENT
).json()
n_links = int(l_r['results']['bindings'][0]['entityCount']['value'])
return n_links
def get_entity_info(lang, data, entity_id):
"""
Get all numbers characterizing entity "importance" and
and other details for later processing from full entity data.
"""
info = {
'n_labels': len(data['labels']),
'n_descriptions': len(data['descriptions']),
'n_sitelinks': len(data['sitelinks']),
'n_aliases': sum(len(a) for a in data['aliases'].values()),
'n_statements': sum(len(s) for s in data['claims'].values()),
'n_qualifiers': sum(
len(claim.get('qualifiers', []))
for claims in data['claims'].values()
for claim in claims
),
'n_references': sum(
len(claim.get('references', []))
for claims in data['claims'].values()
for claim in claims
),
'n_links': get_n_links(entity_id),
'label': data['labels'].get(lang, {}).get('value'),
'aliases': [x['value'] for x in data['aliases'].get(lang, [])],
'wikipedia_url': data['sitelinks'].get(f'{lang}wiki', {}).get('url')
}
return info
def collect_candidates(unique_locations):
location_candidates = defaultdict(dict)
unique_candidates = defaultdict(set)
for lang, locs in unique_locations.items():
print(f'Getting candidates for {lang}: {len(locs)} unique locations')
for i, loc in enumerate(locs):
print(loc)
if not i % 50:
sleep(2)
loc_regex, loc_regex_brackets = create_loc_regexes(loc)
candidates = get_candidates(GEO_REGION, loc, lang, loc_regex, loc_regex_brackets)
if not candidates:
candidates = get_candidates(GEO_LOC, loc, lang, loc_regex, loc_regex_brackets)
location_candidates[lang][loc] = candidates
unique_candidates[lang] |= set(candidates)
return location_candidates, unique_candidates
def get_candidates_stats(unique_candidates):
entities_data = defaultdict(dict)
for lang, entity_set in unique_candidates.items():
print(f'Getting stats for {lang}: {len(entity_set)} unique entities')
for i, entity_id in enumerate(entity_set):
if not i % 50:
sleep(2)
entity = get_entity(entity_id)
if entity is None:
continue
info = get_entity_info(lang, entity, entity_id)
if info['wikipedia_url'] is None:
continue
entities_data[lang][entity_id] = info
return entities_data
def calculate_score(query, entity_info, coefs):
"""
Calculate score based on entity stats
and optimal coefficients.
"""
if query == entity_info['label']:
coef = coefs['label']
elif query in entity_info['aliases']:
coef = coefs['aliases']
else:
coef = 1
stat_sum = 0
for key, value in coefs.items():
if key in ['label', 'aliases']:
continue
stat = entity_info[key]
stat = log(stat + 1) if value == 'log' else stat * value
stat_sum += stat
score = coef * stat_sum
return round(score, 3)
def score_candidates(location_candidates, candidates_stats, coefs):
scores = defaultdict(dict)
for lang, locs in location_candidates.items():
for loc, candidates in locs.items():
loc_scores = []
for candidate_id in candidates:
candidate_info = candidates_stats[lang].get(candidate_id)
if candidate_info is None:
continue
score = calculate_score(loc, candidate_info, coefs)
loc_scores.append([
candidate_id,
score,
candidate_info['wikipedia_url']
])
scores[lang][loc] = sorted(loc_scores, key=lambda x: x[1], reverse=True)
return scores
def get_stats():
with open(NORM_LOC_FILENAME) as f:
unique_locations = {
lang: set(locs.values())
for lang, locs in json.load(f).items()
}
location_candidates, unique_candidates = collect_candidates(unique_locations)
candidates_stats = get_candidates_stats(unique_candidates)
with open(LOC_INFO_FILENAME, 'w') as f:
json.dump(
{
'candidates': location_candidates,
'stats': candidates_stats
},
f,
ensure_ascii=False
)
def get_ranking():
with open(LOC_INFO_FILENAME) as f:
info = json.load(f)
with open(COEFS_FILENAME) as f:
coefs = json.load(f)
scores = score_candidates(info['candidates'], info['stats'], coefs)
with open(RANKING_FILENAME, 'w') as f:
json.dump(scores, f, indent=2, ensure_ascii=False)
return scores
if __name__ == '__main__':
get_stats()
get_ranking()