Search code examples
pythonsparqlwikipediadbpedia

Checking if a person and getting details


I'm new to SPARQL and I want to get details of someone on Dbpedia such as their birthPlace etc, but first I want to do a check that they are type Person. My query so far looks like:

SELECT ?person ?birthPlace
WHERE {
    {?person a dbpedia-owl:Person.}
    UNION 
   {dbpedia:Stephen_Fry dbpprop:birthPlace ?birthPlace .}
}

Solution

  • I want to get details of someone on Dbpedia such as their birthPlace etc, but first I want to do a check that they are type Person.

    If you really want to get Persons first, and then get, e.g., their name, you can take advantage of the fact that SPARQL sub-select queries are evaluated first:

    select ?person ?name {
      { select ?person { ?person a dbpedia-owl:Person } limit 10 }
      ?person foaf:name ?name .
    }
    

    SPARQL results

    That has the advantage that you can select a certain number of persons first, and then get whatever names they might have. E.g., the results to the query above have more than 10 results, since some persons have more than one foaf:name property.

    However, what you're asking for is rather unusual. It you're looking for the names of persons, you'd typically just write a query that says find persons and their names:

    select ?person ?name {
      ?person a dbpedia-owl:Person .
      ?person foaf:name ?name .
    }
    limit 10
    

    SPARQL results

    You don't need to "check" that the person is a person first or anything like that. This query requires that ?person is a Person and has a foaf:name property. You can't get any non-persons from it.

    If you want ?person to be bound to particular values, you can use a values clause:

    select ?person ?name {
      values ?person { dbpedia:Daniel_Webster dbpedia:New_York }
    
      ?person a dbpedia-owl:Person .
      ?person foaf:name ?name .
    }
    

    SPARQL results

    The values clause specifies values that ?person can have. If one of the specified values for ?person doesn't allow a match, e.g., New York in the query above, then you won't see any results for that value in the output.