Search code examples
sparqlontologydbpediasparqlwrapper

Get movie(s) based on book(s) from DBpedia


I am new to SPARQL and trying to fetch a movie adapted from specific book from dbpedia. This is what I have so far:

PREFIX onto: <http://dbpedia.org/ontology/>
SELECT *
WHERE 
{
  <http://dbpedia.org/page/2001:_A_Space_Odyssey> a ?type.
  ?type onto:basedOn ?book .
  ?book   a  onto:Book 
}

I can't get any results. How can I do that?


Solution

  • When using any web resource, and in your case the property :basedOn, you need to make sure that you have declared the right prefix. If you are querying from the DBpedia SPARQL endpoint, then you can directly use dbo:basedOneven without declaring it, as it is among predefined. Alternatively, if you want to use your own, or if you are using another SPARQL client, make sure that whatever short name you choose for this property, you declare the prefix for http://dbpedia.org/ontology/.

    Then, first, to get more result you may not restrict the type of the subject of this triple pattern, as there could be movies that actually not type as such. So, a query like this

    select distinct *
    
    {
    
    ?movie dbo:basedOn ?book .
    
    ?book a dbo:Book .
    
    }
    

    will give you lots of good results but not all. For example, the resource from your example will be missing. You can easily check test the available properties between these two resource with a query like this:

    select  ?p
    
    {
    {<http://dbpedia.org/resource/2001:_A_Space_Odyssey_(film)> ?p <http://dbpedia.org/resource/2001:_A_Space_Odyssey> }
    
    UNION
    
    { <http://dbpedia.org/resource/2001:_A_Space_Odyssey> ?p <http://dbpedia.org/resource/2001:_A_Space_Odyssey_(film)>}
    }
    

    You'll get only one result: http://www.w3.org/2000/01/rdf-schema#seeAlso

    (note that the URI is with 'resource', not with 'page')

    Then you may search for any path between the two resource, using the method described here, or find a combination of other patterns that would increase the number of results.