Reputation: 21
I need some help in a query. I have a list of names and I want to write a program in python that will send a query for each person on the list and it will look for his information in dbpedia and in wikidata and will return some of it.
Can someone help me with this one?
Thanks
Upvotes: 0
Views: 1155
Reputation: 1161
The following working python code takes the (slightly modified) query from @coder and uses it in a small demo program.
It will search for people with "Andreotti" as part of their name, and return one or more URLs each pointing to the page for one person found.
from SPARQLWrapper import SPARQLWrapper, JSON
NAME_FRAGMENT = "Andreotti"
QUERY = f"""
SELECT DISTINCT ?uri
WHERE {{
?uri a foaf:Person.
?uri ?p ?person_full_name.
FILTER(?p IN(dbo:birthName,dbp:birthName ,dbp:fullname,dbp:name)).
?uri rdfs:label ?person_name .
?person_name bif:contains "{NAME_FRAGMENT}" .
FILTER(langMatches(lang(?person_full_name), "en")) .
}}
LIMIT 100
"""
# Specify the DBPedia endpoint
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery(QUERY)
sparql.setReturnFormat(JSON)
# Run the query
result = sparql.query().convert()
# The return data contains "bindings" (a list of dictionaries)
for link in result["results"]["bindings"]:
# We want the "value" attribute of the "comment" field
print(link["uri"]["value"])
Please note that as far as I understand the names are always scraped from the English wikipedia pages and therefore you will only find the English rendition (I stand to be corrected if this is not true).
Upvotes: 0