-
Notifications
You must be signed in to change notification settings - Fork 2
/
fix-labels-frenchcommunes.py
83 lines (69 loc) · 2.56 KB
/
fix-labels-frenchcommunes.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
from SPARQLWrapper import SPARQLWrapper, JSON
import argparse
parser = argparse.ArgumentParser(
description='Fixes labels for French communes.')
parser.add_argument("departement", help="The number of a departement")
args = parser.parse_args()
if args.departement.isdigit():
dept = '"{}"'.format(args.departement)
f = open('temp-lastdept.txt', 'w')
f.write(dept)
f.close()
else:
dept = "?dept"
all_langs = ['af', 'an', 'ast', 'bar', 'bm', 'br', 'ca', 'co', 'cs', 'cy',
'da', 'de', 'de-at', 'de-ch', 'en', 'en-ca', 'en-gb', 'eo', 'es',
'et', 'eu', 'fi', 'fr', 'frc', 'frp', 'fur', 'ga', 'gd', 'gl',
'gsw', 'hr', 'hu', 'ia', 'id', 'ie', 'io', 'it', 'jam', 'kab',
'kg', 'lb', 'li', 'lij', 'lt', 'lv', 'mg', 'min', 'ms', 'nap',
'nb', 'nds', 'nds-nl', 'nl', 'nn', 'nrm', 'oc', 'pap', 'pcd',
'pl', 'pms', 'prg', 'pt', 'pt-br', 'rgn', 'rm', 'ro', 'sc', 'scn',
'sco', 'sk', 'sr-el', 'sv', 'sw', 'tr', 'vec', 'vi', 'vls', 'vmf',
'vo', 'wa', 'wo', 'zu', 'fo', 'is', 'kl']
endpoint = "https://query.wikidata.org/bigdata/namespace/wdq/sparql"
sparql = SPARQLWrapper(endpoint)
sparql.setQuery("""
SELECT DISTINCT ?item ?label WHERE {{
?item wdt:P31/wdt:P279* wd:Q484170 .
?item wdt:P131 ?dept .
?dept wdt:P2586 ?deptnum .
FILTER(STRSTARTS(?deptnum, {})) .
?item rdfs:label ?label .
}}""".format(dept))
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
out = ""
label_counter = 0
item_counter = 0
used_langs = {}
labels = {}
for result in results["results"]["bindings"]:
item = result['item']['value'].rsplit('/', 1)[-1]
if item not in used_langs:
used_langs[item] = []
lang = result["label"]["xml:lang"]
label = result["label"]['value']
if lang == 'fr':
labels[item] = label
used_langs[item].append(lang)
for item, values in used_langs.items():
missing_langs = set(all_langs) - set(values)
missing_langs = sorted(missing_langs)
modified_item_counter = 0
for m in missing_langs:
label = labels[item]
out += "{}\tL{}\t{}\n".format(item, m, label)
label_counter += 1
modified_item_counter += 1
if modified_item_counter:
out += "\n"
item_counter += 1
f = open('temp.txt', 'w')
f.write(out)
f.close()
qs_url = "https://tools.wmflabs.org/wikidata-todo/quick_statements.php"
print("Operation complete! {} labels added on {}/{} items.".format(
label_counter,
item_counter,
len(used_langs)))
print("- Please paste the content of temp.txt to {}".format(qs_url))