-
Notifications
You must be signed in to change notification settings - Fork 2
/
181108_test.py
67 lines (50 loc) · 1.54 KB
/
181108_test.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
from SPARQLWrapper import SPARQLWrapper, JSON
import pandas as pd
language = 'english'
abbre = 'en'
sparql = SPARQLWrapper("https://query.wikidata.org/sparql")
# From https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples#Cats
# sparql.setQuery("""
# SELECT DISTINCT ?doid ?item ?label ?wpLang
# WHERE {
# ?item wdt:P699 ?doid ;
# rdfs:label ?label ;
# rdfs:label ?%(a)s .
# ?article schema:about ?item ;
# schema:inLanguage ?wpLang .
# FILTER (lang(?label) = ?wpLang)
# FILTER (lang(?%(a)s) = "%(b)s")
# }
# """ % {'a': language, 'b': abbre})
sparql.setQuery("""
SELECT DISTINCT ?doid ?item ?label ?wpLang
WHERE {{
?item wdt:P699 ?doid ;
rdfs:label ?label ;
rdfs:label ?{0} .
?article schema:about ?item ;
schema:inLanguage ?wpLang .
FILTER (lang(?label) = ?wpLang)
FILTER (lang(?{0}) = "{1}")
}}
""".format(language, abbre))
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
results_df = pd.io.json.json_normalize(results['results']['bindings'])
results_df[['doid.value', 'item.value', 'label.value', 'wpLang.value']].head()
import sparql_dataframe
endpoint = "https://query.wikidata.org/sparql"
q = """
SELECT DISTINCT ?doid ?item ?label ?wpLang
WHERE {
?item wdt:P699 ?doid ;
rdfs:label ?label ;
rdfs:label ?english .
?article schema:about ?item ;
schema:inLanguage ?wpLang .
FILTER (lang(?label) = ?wpLang)
FILTER (lang(?english) = "en")
}
"""
df = sparql_dataframe.get(endpoint, q)
df