SPARQL query to print the name, birthday pairs for a list of people in dbpedia - sparql

Given a list of Wikipedia article titles (people's names), how do I print the name, birthday pair for each person?

Here is one way to do it using the VALUES clause in SPARQL 1.1:
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?givenName ?surname ?birth
WHERE
{
?person dbpedia-owl:birthDate ?birth .
?person foaf:givenName ?givenName .
?person foaf:surname ?surname .
VALUES ?person { dbpedia:Albert_Einstein dbpedia:Max_Planck dbpedia:Marie_Curie }
}

Related

Can't get actor names for a given film title when multiple films have the same name in DBpedia

I am trying to get actor names for a given film title (I also have the release date in hand) with my sparql query, but given the situation that multiple films have the same name, I'm trying to differentiate them with the release date. Some films don't have the release date specified, some films don't have the label specified.
I'm trying to get results when either the release date is specified and is matching, or when it is in the label of the film and is also matching.
If I can't match the date with one of these attributes, I want no results in return
Here is my current query:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?film ?aname
WHERE {
?film a dbo:Film ;
foaf:name "A Nightmare on Elm Street"#en ;
dbo:starring ?a .
?a foaf:name ?aname
OPTIONAL
{ ?film dbo:releaseDate ?rd
BIND(year(xsd:date(?rd)) AS ?rrd)
FILTER ( ( ?rd = "1984-04-30"^^xsd:date ) || ( ?rrd = 1984) )
}
OPTIONAL
{ ?film rdfs:label ?lab
FILTER regex(?lab, "1984", "i")
FILTER ( lang(?lab) = "en" )
}
}
I think you are misusing OPTIONAL here. Instead you should be looking at UNION.
What's the difference?
SELECT ?person ?child
WHERE {
?person a :Person .
OPTIONAL {?person :hasSon ?child}
OPTIONAL {?person :hasDaughter ?child}
}
Will return every person, and optionally their sons/daughters. However this will return also people without any children at all.
Instead, something like:
SELECT ?person ?child
WHERE {
?person a :Person .
{?person :hasSon ?child}
UNION
{?person :hasDaughter ?child}
}
Will only return people who have at least one son or daughter.
Now, in your example, I have a query working like this:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?film ?aname
WHERE {
?film a dbo:Film ;
foaf:name "A Nightmare on Elm Street"#en ;
dbo:starring ?a .
?a foaf:name ?aname
{ ?film dbp:released 1984}
UNION
{ ?film rdfs:label ?lab
FILTER regex(?lab, "1984", "i")
FILTER ( lang(?lab) = "en" )
}
}
Notice that I used the dbp:released property which seems to be working.
It seems that the property used for releases is inconsistent across films, i.e. some use dbp:released , others dbo:releaseDate.
If that's an issue, you can of course add another UNION statement in the query to deal with the different case.
One more thing:
there are many triplestores out there that have reasoning, and reasoning is something that can help deal with a variety of such situations (disambiguation, multiple properties for the same thing, etc)

SPARQL Query for all Books by George Orwell

I created this query to return all books that are notable works by George Orwell but it returns no result.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?title where {
?person foaf:name ?name .
?title dbo:author ?person .
?title dbo:notableWork dbp:George_Orwell .
}
I cannot seem to figure out why there is no result.
I am running the query in http://dbpedia.org/snorql
Don't you have the triples about notable works in the wrong order?
Try rewriting based on this working query
SELECT *
WHERE {
:George_Orwell dbo:notableWork ?title
}
.
title
:Nineteen_Eighty-Four
:Animal_Farm
You can also bind :George_Orwell to a variable and ask more about that:
SELECT *
WHERE {
values ?author { :George_Orwell } .
?author rdfs:label ?l .
?title ?p ?author .
?title rdf:type dbo:Book .
filter (lang(?l) = "en")
}
and DESCRIBE things
describe :Animal_Farm

Federated queries on dbpedia and linkedmdb

I am writing a federated query to get the books based on films in dbpedia and in turn using the film name from dbpedia to retrieve the corresponding imdblink link for the same. I am getting an empty set when I add the service of linkedmdb.
Here is my code
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX movie: <http://data.linkedmdb.org/resource/movie/>
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT distinct ?name ?author ?filmname ?imdbID WHERE {
SERVICE <http://dbpedia.org/sparql> {
?book rdf:type dbo:Book .
?book foaf:name ?name .
?book dbp:author ?author .
?author foaf:name ?authname .
?book ^dbo:basedOn ?movie .
?movie a dbo:Film .
?movie foaf:name ?filmname
FILTER (str(?name) IN ("Royal Flash","White Oleander", "Possession: A Romance", "Misery", "Intensity", "The War of The Roses", "Momo", "The Sicilian", "Derailed", "Ragtime"))
}
SERVICE <http://data.linkedmdb.org/sparql> {
?filmname foaf:page ?imdbID .
?filmname dc:title ?title .
FILTER(regex(str(?imdbID), "www.imdb.com" ) )
}
}
I am using the following endpoint http://www.sparql.org/query.html
That does not work because of the ?filename variable
in the first SERVICE clause you get
?movie foaf:name ?filmname, thus, those are string literals
in the second SERVICE clause ?filmname are URIs
It will become easy to see if you run both SPARQL queries separately.
What you would have to do is to match on the title, i.e. change
?filmname dc:title ?title .
to
?filmname dc:title ?filmname .
Note, this might not work because of different types of strings, e.g. with and without language tag etc.
In that case you would have to match on the lexical form again, i.e. use temporary variables ?filmname1 and ?filmname2 and use
FILTER(str(?filmname1) = str(?filmname2))
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX movie: <http://data.linkedmdb.org/resource/movie/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/terms/>
SELECT DISTINCT ?name ?author ?filmname1 ?imdbID
WHERE
{ SERVICE <http://dbpedia.org/sparql>
{ ?book rdf:type dbo:Book ;
foaf:name ?name ;
dbp:author ?author .
?author foaf:name ?authname .
?book ^dbo:basedOn ?movie .
?movie rdf:type dbo:Film ;
foaf:name ?filmname1
FILTER ( str(?name) IN ("Royal Flash", "White Oleander", "Possession: A Romance", "Misery", "Intensity", "The War of The Roses", "Momo", "The Sicilian", "Derailed", "Ragtime") )
}
SERVICE <http://data.linkedmdb.org/sparql>
{ ?filmname foaf:page ?imdbID ;
dc:title ?filmname2
FILTER regex(str(?imdbID), "www.imdb.com")
}
FILTER ( str(?filmname1) = str(?filmname2) )
}

How to get such data as abstraction, location and link for image in Dbpedia?

I am trying to get some data about museum but it is not successfull. This is my code. I know the name of the museum, so I want to get data about this museum
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT ?abstract ?location WHERE {
?architectural_structure rdf:type dbpedia-owl:Museum .
?architectural_structure dbpedia-owl:location dbpedia:Taganrog .
?architectural_structure dbpprop:name dbpedia:Chekhov_Shop .
}
The dbpprop:name that you have selected (dbpedia:Chekhov_Shop) is in fact a string. If you look at the dbpedia page it has been defined as The Chekhov Shop. Therefore, my suggestion is to filter your query based on the name you like to be displayed:
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT * WHERE {
?architectural_structure rdf:type dbpedia-owl:Museum .
?architectural_structure dbpedia-owl:location dbpedia:Taganrog .
?architectural_structure dbpprop:name ?name.
Filter (str(?name)="The Chekhov Shop")
}
And if you need more information about this specific architectural structure, you start exploring. For example,
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT distinct * WHERE {
?architectural_structure rdf:type dbpedia-owl:Museum .
?architectural_structure dbpedia-owl:location dbpedia:Taganrog .
?architectural_structure dbpprop:name ?name.
?architectural_structure dbpprop:location ?location.
?architectural_structure dbpedia-owl:abstract ?abstract.
Filter (str(?name)="The Chekhov Shop")
}
In general, when you are faced with a triple store try to find all ?o ?p ?s and then see where you need to put a specific predicate.

How do get city population by name with SPARQL

I need to get city population by its name. I'm sure SPARQL could provide this, but I'm not sure how to write the query. I have a query which provides COuntry's capital and coordinates by it's name, so I assume it should be something similar. HELP!
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX p: <http://dbpedia.org/property/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?country ?population ?capital ?lat ?long WHERE {
?country a o:Country ; foaf:name "Germany"#en; o:capital [ geo:lat ?lat ; geo:long ?long ; p:name ?capital ]
}
All you need is:
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX p: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?pop WHERE {
?country a o:Country ;
foaf:name ?name ;
p:populationEstimate ?pop .
}
The only tricky part was finding out the name of the property that links Countries and Populations.