0

I'm using the following query to get the link / URI to the correct titled resource given an incorrect title:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?redirectsTo WHERE {
  ?x rdfs:label "Environmental Protection Agency"@en .
  ?x dbo:wikiPageRedirects ?redirectsTo
}

Using the dbpedia endpoint this is not a problem and returns the correct link to the article, United States Environmental Protection Agency

I'm using the python SPARQLWrapper library to send the sparql query but when I do this exact same query using python:

sparql = SPARQLWrapper("http://dbpedia.org/sparql")
        # First check if this entity redirects to another entity
        sparql.setQuery("""
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX dbo: <http://dbpedia.org/ontology/>
            PREFIX db: <http://dbpedia.org/resource/>
            SELECT ?redirectsTo WHERE {
              ?x rdfs:label "%s"@en .
              ?x dbo:wikiPageRedirects ?redirectsTo
            }
        """ % entity)
        sparql.setReturnFormat(JSON)
        uri_result = sparql.query().convert()['results']['bindings']
        print(uri_result)

I get an empty list of results with no redirects. However, this query does work in python for some things - Obama will give the link for Barack Obama for example

I've heard that the sparql web interface uses a different dataset? If so how can I use this within my python script

Thanks

imattacus
  • 354
  • 1
  • 3
  • 12
  • 1
    I don't know where you heard that, but the web interface doesn't use a different dataset - it is just the web interface to the underlying SPARQL service available via HTTP. Nothing more. Please print your query - I'm pretty sure that something goes wrong with the Python variable replacement. By the way, the web interface uses the default graph `http://dbpedia.org` – UninformedUser Nov 27 '16 at 20:24
  • Ah brilliant thanks! I was replacing whitespace for underscores earlier in the code that was stopping this from working. Thanks! – imattacus Nov 27 '16 at 20:44

0 Answers0