Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query dbpedia resource ontology 'wikiPageExternalLink'

Using sparql\sparqlwrapper in python, how will I be able to query for the values of a certain dbpedia resource? For example, how will I be able to get the dbpedia-owl:wikiPageExternalLink values of http://dbpedia.org/page/Asturias? Here's a simple example on how will I be able to query for the rdfs:label of Asturias. But I don't know how to modify the query/query parameters to get values of property/ontology other than those included on rdfs schema. Here's the sample:

from SPARQLWrapper import SPARQLWrapper, JSON, XML, N3, RDF
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    SELECT ?label
    WHERE { <http://dbpedia.org/resource/Asturias> rdfs:label ?label }
""")
print '\n\n*** JSON Example'
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
    print result["label"]["value"]

Hoping to receive feedback. Thanks in advance!

like image 353
jaysonpryde Avatar asked May 21 '12 21:05

jaysonpryde


1 Answers

Not sure where you're stuck—this is really easy:

SELECT ?label
WHERE { <http://dbpedia.org/resource/Asturias>
            dbpedia-owl:wikiPageExternalLink ?label }

Usually you need to declare the namespace prefixes like rdfs: or dbpedia-owl: if you want to use them in the query, but on the DBpedia endpoint this works even without. If you want, you can declare them anyways:

PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT ?label
WHERE { <http://dbpedia.org/resource/Asturias>
            dbpedia-owl:wikiPageExternalLink ?label }

You can find out the full URI corresponding to the prefix by going to http://dbpedia.org/sparql and clicking on “Namespace Prefixes” near the top right corner.

If you want to rename the variable (for example, from ?label to ?link) then do it like this:

PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT ?link
WHERE { <http://dbpedia.org/resource/Asturias>
            dbpedia-owl:wikiPageExternalLink ?link }

and you also have to change "label" to "link" in the Python code that gets the value out of the JSON result.

like image 181
cygri Avatar answered Oct 11 '22 23:10

cygri