Search code examples
sparqldbpediardfslinked-data

Find people linked with Person X using Sparql and DBpedia


I am new to querying DBPedia using Sparql. I would like to find people related to a person X using DBPedia.

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpr: <http://dbpedia.org/resource/>
PREFIX dbpo: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT  ?P 
WHERE {dbpr:Chuck_Norris}

Solution

  • In case you want the people directly related to a person, then you need two triple patterns, one having the person as a subject and another as an object. Here is one way to construct such a query:

    SELECT DISTINCT ?has ?of ?person 
    WHERE {
    
    {?person a foaf:Person; 
    ?of dbr:Chuck_Norris} 
    
    UNION
    
    {?person a foaf:Person. 
    dbr:Chuck_Norris ?has ?person}
    
    }
    

    I'm using dbr: as this is a predefined prefix in DBpedia.

    In some cases, you'll get different results if you query on http://live.dbpedia.org/sparql

    Now, this query has a limitation. It will get you only relations to other DBpedia resources. Quite often there might be related persons asserted as literals. I don't know of an elegant way to get them, but one way would be to ask for a list of properties that are known to be used for person relations. Here is an example with a list with one value for dbp:spouse:

    SELECT DISTINCT ?has ?of ?person 
    WHERE {
    
    {?person a foaf:Person; 
    ?of dbr:Chuck_Norris} 
    
    UNION
    
    {?person a foaf:Person. 
    dbr:Chuck_Norris ?has ?person}
    
    UNION
    
    {values ?has {dbp:spouse}
    dbr:Chuck_Norris ?has ?person .}
    
    }
    

    From http://live.dbpedia.org/sparql this will bring you additionally:

    has                                | person
    ---------------------------------------------------------
    http://dbpedia.org/property/spouse | "Dianne Holechek"@en
    http://dbpedia.org/property/spouse | "Gena O'Kelley"@en
    ---------------------------------------------------------
    

    In general asking for literals like that will bring you a lot of garbage results, which you can reduce using string filters.