Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple sparql query from dbpedia

I have a question about a SPARQL query that I'm trying to build from a tutorial. I want to generate triples that return a list of band members and the bands that they are in using a DBPedia endpoint.

my query

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?bandname where {
?person foaf:name ?name .
?band dbo:bandMember ?person .
?band dbo:genre dbpedia:Punk_rock .
?band dbp:name ?bandname .
}

I'm also using a [SPARQL query validator][2] to try to figure out my problem and it seems I'm using an incorrect prefix for "dbp:name ?bandname"I just want the triples returned in JSON if possible.

Once I can get this to run, I'd like to add another prefix, from GeoNames, to see the places associated with the bands, if possible (but that part is in the future). Any insights would be greatly appreciated!

like image 294
Sara Lafia Avatar asked Feb 24 '16 07:02

Sara Lafia


People also ask

How do I query DBpedia with SPARQL?

To actually query DBpedia, you can go to dbpedia.org/sparql and submit your query to return the results in a number of formats such as HTML, JSON or CSV. You can also run SPARQL on DBpedia from within a Python application but that will be covered in my next blog.

How do I run a SPARQL query in Python?

To use as a command line script, you will need to install SPARQLWrapper and then a command line script called rqw (spaRQl Wrapper) will be available within the Python environment into which it is installed. run $ rql -h to see all the script's options.


2 Answers

There are some issues with your query.

  • The "name" property for a band has the following URI : http://dbpedia.org/property/name. It is a property, and not a resource, but you defined it as such in the prefixes. You shoud define it as follows : PREFIX dbp: <http://dbpedia.org/property/>
  • I just had a quick check at what a band page has as properties, and saw that, apart from the dbo:bandMember one you are using, there is another property, currentMembers, that seems to retrieve more information. This seems logical though, in so far as dbo:bandMember only retrieves entities (members URIs), whereas dbp:currentMembers also retrieves literals. It depends on your use case here...
  • You use the dbpedia:prefix in your query, that does not seem to have been defined beforehand.

Here is the query I used to retrieve a list of bands associated with their members :

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?members ?bandName where {
 ?band dbo:genre dbr:Punk_rock .
 ?band dbp:currentMembers ?members.
 ?band foaf:name ?bandName
 FILTER(langMatches(lang(?bandName), "en"))
}

The filter part allows us to avoid duplicates in case the literals are also defined in other languages.

But if you still want to use the dbo:bandMember property, this query also does the job :

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?personName ?bandName where {
 ?band dbo:bandMember ?person .
 ?person foaf:name ?personName .
 ?band dbo:genre dbr:Punk_rock .
 ?band foaf:name ?bandName
 FILTER(langMatches(lang(?bandName), "en"))
 FILTER(langMatches(lang(?personName), "en"))
}

Hope that helps !

like image 64
KevinD Avatar answered Nov 04 '22 13:11

KevinD


The problem is with undefined prefix dbpedia. Also I think you have to replace dbp:name with foaf:name:

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?bandname where {
   ?person foaf:name ?name .
   ?band dbo:bandMember ?person .
   ?band dbo:genre dbp:Punk_rock .
   ?band foaf:name ?bandname .
}

Alas, with SELECT you won't get triples, but just the names (or other stuff) in tabular results. Much like SQL. If you want triples, you need CONSTRUCT

like image 5
Tomasz Pluskiewicz Avatar answered Nov 04 '22 12:11

Tomasz Pluskiewicz