Search code examples
sparqldbpedia

SPARQL query: How to use wikiPageDisambiguates for redirect


I'm trying to retrieve thumbnail links for a given entity name. My current query looks like the following and works for most of the cases.

select ?value { 
     <http://dbpedia.org/resource/Angela_Merkel> dbpedia-owl:thumbnail ?value 
}

However, for some cases e.g "CDU" it fails, because the entity is ambiguous. See this Example in the SPARQL Explorer.

In these cases I would like to return the thumbnail of the first wikiPageDisambiguates entry. So, for "CDU" it would be the thumbnail of this page. Can somebody tell me how to do this in SPARQL?


Solution

  • In these cases I would like to return the thumbnail of the first wikiPageDisambiguates entry. So, for "CDU" it would be the thumbnail of this page. Can somebody tell me how to do this in SPARQL?

    There's no order on these. Any representation necessarily has to put them in some order, but they're not actually ordered in the underlying RDF. You can retrieve an arbitrary one, but not "the first". For instance, look at the results from select * where { dbpedia:CDU ?p ?o }. There are a bunch of disambiguation links. Now, you can follow those links, if they are there, to get thumbnails:

    select ?thumbnail where {
      dbpedia:CDU dbpedia-owl:wikiPageDisambiguates?/dbpedia-owl:thumbnail ?thumbnail
    }
    

    SPARQL results

    The property path dbpedia-owl:wikiPageDisambiguates?/dbpedia-owl:thumbnail uses a question mark after the wiki page disambiguation property. That means that there can be either zero or one occurrences of the the property. Then it has to be followed by a dbpedia-owl:thumbnail link. That means that if dbpedia:CDU has a thumbnail property, you'll get it, or if it has a a disambiguation that has a thumbnail, you'll get that.

    If you do want to impose some ordering, you can do that, but you'll have to determine what it should be. You can use order by to specify the ordering, and limit to specify that you want just the first one. E.g., you could do:

    select ?thumbnail where {
      dbpedia:CDU dbpedia-owl:wikiPageDisambiguates? ?cdu .
      ?cdu dbpedia-owl:thumbnail ?thumbnail ;
           rdfs:label ?label
    }
    order by ?label
    limit 1
    

    SPARQL results