-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
80 lines (73 loc) · 2.87 KB
/
api.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
import json, urllib # Needed libs
from pprint import pprint # Not necessary
def entity2id(q):
# Get wikidata id from wikidata api
ans = []
url = "https://www.wikidata.org/w/api.php?action=wbsearchentities&format=json&search="+"+".join(q.split(" "))+"&language=en"
response = json.loads(urllib.urlopen(url).read())
ans += response["search"]
if (ans == [] and " " in q):
# Reverse Trick : Pan Changjiang
url = "https://www.wikidata.org/w/api.php?action=wbsearchentities&format=json&search="+"+".join(q.split(" ")[::-1])+"&language=en"
response = json.loads(urllib.urlopen(url).read())
ans += response["search"]
if (ans == [] and len(q.split(" ")) > 2):
# Abbreviation Trick
url = "https://www.wikidata.org/w/api.php?action=wbsearchentities&format=json&search="+"+".join([q.split(" ")[0], q.split(" ")[-1]])+"&language=en"
response = json.loads(urllib.urlopen(url).read())
ans += response["search"]
if len(ans) > 0:
# Returns the first one, most likely one
return ans[0]["id"]
else:
# Some outliers : Salvador Domingo Felipe Jacinto Dali i Domenech - Q5577
return "Not Applicable"
def getp(p):
# Get property name given property id
# Initialization required
return property_dict[p]
def getc(c):
# Get entity name given entity id
url = "https://www.wikidata.org/w/api.php?action=wbgetentities&props=labels&ids="+c+"&languages=en&format=json"
response = json.loads(urllib.urlopen(url).read())
return response["entities"][c]["labels"]["en"]["value"]
def Related(name):
# Get related property-entity (property id, property name, entity id, entity name) given entity name
# Return a list of dicts, each dict contains (pid, property, eid, entity)
# Fail to fetch eid would result in empty list
query = entity2id(name)
if query == "Not Applicable": return []
ans = []
url = "https://www.wikidata.org/w/api.php?action=wbgetentities&ids="+query+"&format=json&languages=en"
response = json.loads(urllib.urlopen(url).read())
for p in response["entities"][query]["claims"]:
for c in response["entities"][query]["claims"][p]:
# Enumerate property & entity (multi-property, multi-entity)
try:
# Some properties are not related to entities, thus try & except
cid = c["mainsnak"]["datavalue"]["value"]["id"]
ans.append({
"pid": p,
"property": getp(p),
"eid": cid,
"entity": getc(cid)
})
#ans.append("\\property\\"+p+"\t"+getp(p)+"\t\\entity\\"+cid+"\t"+getc(cid))
# Print in a pid-pname-eid-ename fashion
except:
continue
return ans
def init():
# WARNING: RUN BEFORE USE GETP
# Needed for property name fetching
global property_dict
property_dict = {}
url = "https://quarry.wmflabs.org/run/45013/output/1/json"
# Fetch json from given lib
res = json.loads(urllib.urlopen(url).read())
for w in res["rows"]:
property_dict[w[0]] = w[1]
init()
# For test only
entity = raw_input("Please input the entity name\n")
pprint(Related(entity))