forked from WDPS-Group2/Entity-Linking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparql.py
45 lines (38 loc) · 1.27 KB
/
sparql.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
import requests, json
QUERY="""
select distinct ?abstract where {
?s <http://www.w3.org/2002/07/owl#sameAs> <http://rdf.freebase.com/ns/%s> .
?s <http://www.w3.org/2002/07/owl#sameAs> ?o .
?o <http://dbpedia.org/ontology/abstract> ?abstract .
}"""
# NOTWORK:filter langMatches( lang(?abstract), "EN" )
def sparql(domain, query):
url = 'http://%s/sparql' % domain
response = requests.post(url, data={'print': True, 'query': query})
if response:
try:
response = response.json()
for binding in response.get('results', {}).get('bindings', []):
abstract = binding.get('abstract',{}).get('value')
# english version
if abstract[-3:-1] == "en":
return abstract
except Exception as e:
print(reponse)
raise e
def query_abstract(domain, freebaseId):
key = freebaseId[1:].replace("/",".")
q = QUERY % key
try:
abstract = sparql(domain,q)
return abstract
except Exception as e:
return None
if __name__ == '__main__':
import sys
try:
_, DOMAIN, QUERY = sys.argv
except Exception as e:
print('Usage: python sparql.py DOMAIN QUERY')
sys.exit(0)
sparql(DOMAIN, QUERY)