0

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

Daniel
  • 21
  • 1
  • 8
  • 1
    Execute this and see ,whether this is what you are looking for: SELECT DISTINCT ?link ?person_full_name ?birth_year WHERE { ?link a foaf:Person. ?link ?p ?person_full_name. FILTER(?p IN(dbo:birthName,dbp:birthName ,dbp:fullname,dbp:name)). ?link rdfs:label ?person_name . ?person_name bif:contains "Tim_Cook" . OPTIONAL { ?link dbo:birthYear ?birth_year . } FILTER(langMatches(lang(?person_full_name), "en")) . } – Coder Feb 23 '18 at 18:29
  • @Shubhangi I don't understand your query, but it's for sure not what he's looking for. You select a bunch of name properties, but use the `bif:contains` only on the `rdfs:label` literal. Why not add `rdfs:label` to the `IN` clause and use `bif:contains` on that literal? And why the `OPTIONAL` clause? – UninformedUser Feb 23 '18 at 18:37
  • @Daniel What query did you try so far? You should be able to write your own SPARQL queries. You only need to figure out the corresponding properties, in the simplest case just use `rdfs:label` + the class for person (to make the query more efficient) – UninformedUser Feb 23 '18 at 18:43
  • @AKSW I am not sure what he is looking for so I have added person_name and resource_link as the field he might be interested in .yeah one can add rdfs:label as well you are right but I have seen different property name for certain people for obtaining their full_name varying from person to person on dbpedia page.In-order to obtain full_name of the person I have added those property in filter clause and yes as you said the use of OPTIONAL is not required but I have used it because for few people the property birthyear is not there and instead it is birthDate. – Coder Feb 23 '18 at 18:55
  • @coder thank you so much. Was banging my head to achieve retrieving one or more people from a name or part of it !!!! This is great for me after an AI NLP pipeline does entity recognition as a PERson and then will lookup if that token has matches in DBPedia !!!! – Robert Alexander Jan 06 '23 at 11:26

1 Answers1

0

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).

Robert Alexander
  • 875
  • 9
  • 24