Sparql: Query DBPedia and follow redirects - sparql

I'm trying to query DBPedia for some animal pictures.
The species I'm querying for is Pan_troglodytes. When one visits https://en.wikipedia.org/wiki/Pan_troglodytes in a browser, one gets redirected to https://en.wikipedia.org/wiki/Chimpanzee.
I'd like to query for Pan_troglodytes and get the thumbnail that appears in the Chimpanzee page. But my query returns nothing:
prefix dbpedia: <http://dbpedia.org/resource/>
prefix dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT * WHERE {
dbpedia:Pan_troglodytes
dbpedia-owl:abstract ?abstract;
dbpedia-owl:thumbnail ?thumbnail .
}
Does anyone know how I can get the thumbnail for the page to which one is redirected? Any pointers others can provide would be helpful!

Related

Querying Dbpedia by DBO - SPARQL

I am trying to extract the manufacturer section of this dbpedia page http://dbpedia.org/page/Diageo. However my SPARQL query returns nothing. Yet I can return most other values on the page, such as keyPersons which has the exact same layout.
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT ?label
WHERE { <http://dbpedia.org/resource/Diageo>
dbpedia-owl:keyPerson ?label }
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT ?label
WHERE { <http://dbpedia.org/resource/Diageo>
dbpedia-owl:manufacturer ?label }
Any ideas?
In DBpedia, an entity page displays statements in which an entity may be not only a subject, but also an object. In the latter case, respective property appears as "is ... of".
Conversely, the page you have linked to says that dbr:Diageo is dbo:manufacturer of dbr:Johnnie_Walker etc. This means that dbr:Johnnie_Walker dbo:manufacturer dbr:Diageo holds, not that dbr:Diageo dbo:manufacturer dbr:Johnnie_Walker does.
By the way, rdfs:range of dbo:manufacturer is dbo:Organization.
Thus, you should looking for triples that match reversed pattern:
SELECT * WHERE { ?variable dbo:manufacturer ?dbr:Diageo . }
Or, using property paths:
SELECT * WHERE { dbr:Diageo ^dbo:manufacturer ?variable . }
Try it on DBpedia

Sparql: getting all politicians who ruled a city

I'm new to sparql and I'm trying to understand how to get the resources I need for building a query. I started trying to get all the politicians that ruled a city or a country, and at the moment I could do just the following:
I started by following the links in snorql (in the prefixes) and looking for an entity by adding "politician" at the end. I found one :
PREFIX : <http://dbpedia.org/resource/>
So I wrote http://dbpedia.org/resource/Politician and the resource does exist. I tryed to use it in this way:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT ?thing WHERE {
?thing a :Politician .
?thing dbo:birthPlace dbpedia:Italy.
}
LIMIT 50
Run in virtuoso.
Even if I remove the second line of the SELECT, I have no results. But if I change the first line with: ?thing a dbo:Person. or even if I remove it, I get the people born in Italy. But not just the politicians. A second problem is I don't need the politicians that were born but ruled that place. How or where can I find that kind of "relations/descriptors"? Now I am just googling and copy-pasting some existing examples, but I would like to understand how to look for more specific things.
Thanks in advance
Your first query isn't working because Politician is not part of the default (:) namespace, but instead it is present in DBpedia Ontology namespace (dbo).
So, your query should be:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT ?thing WHERE {
?thing a dbo:Politician .
?thing dbo:birthPlace dbpedia:Italy.
}
LIMIT 50
To list all politician who ruled Italy you would need to know which is the predicate for "ruled". Once you have it you can construct a query.
To list all predicates present in the database you can write something like this
SELECT DISTINCT(?b) WHERE {
?a ?b ?c.
}
And it will list all predicates.
I would recommend you to browse through one or two politician and see the predicates they have to check if one works for you.

SPARQL query returns no data

Why does this SPARQL query return no data?
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT *
WHERE {
<http://dbpedia.org/resource/Louis,_Prince_of_Condé_(1530–1569)> dbpedia-owl:abstract ?abstract
}
LIMIT 1
If you look at the DBpedia page, it shows the person has an abstract. Is it to do with the brackets in the URL? If so, how can I get round this?
This URI does not lead to the same result as the DBpedia page - for what ever reason. You can see this with
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT *
WHERE {
<http://dbpedia.org/resource/Louis,_Prince_of_Condé_(1530–1569)> ?p ?o
}
LIMIT 100
But it has an owl:sameAs relation to
http://dbpedia.org/resource/Louis,_Prince_of_Cond%C3%A9_(1530%E2%80%931569)
That means if you use this URI in your query, it should work as expected. But you should indeed apply a FILTER on the language, e.g. 'en' for English abstracts.
As AKSW mentions, the resource actually doesn't have many properties, but is connected to the "canonical" version by an owl:sameAs link. You can keep using the IRI that you're using now, follow owl:sameAs in either direction to any of its equal resources (let's call them ?s), and then ask for the abstract of ?s. (And then it's not a bad idea to filter by language, if that's applicable.) You can do this with a query like this (note that the current DBpedia endpoint uses dbo:, now, not the older dbpedia-owl:):
select ?abstract where {
<http://dbpedia.org/resource/Louis,_Prince_of_Condé_(1530–1569)> (owl:sameAs|^owl:sameAs)* ?s .
?s dbo:abstract ?abstract .
filter langMatches(lang(?abstract),'en')
}
It does not have dbpedia-owl:abstract predicate. If you list its predicates you find the following properties:
http://www.w3.org/2002/07/owl#sameAs
http://xmlns.com/foaf/0.1/name
http://purl.org/dc/elements/1.1/description
http://dbpedia.org/ontology/alias
http://dbpedia.org/ontology/birthYear
http://dbpedia.org/ontology/deathYear
http://dbpedia.org/ontology/viafId
http://dbpedia.org/ontology/deathPlace
http://dbpedia.org/ontology/deathDate
http://dbpedia.org/ontology/birthPlace
http://dbpedia.org/ontology/birthDate

How to get abstract and thumbnail of a Wikipedia article from article DBPedia?

I am new to SPARQL. With this query, I can get the birthName of Ernest Hemingway:
select distinct ?birthName
where {
?person a dbpedia-owl:Person .
?person dbpprop:birthName ?birthName .
FILTER (regex(?birthName, "Ernest Miller Hemingway"))
}
LIMIT 1
Is there a way I can get the wikipedia abstract/introduction and thumbnail of Ernest Hemingway with DBPedia?
In general, the best way to start querying DBpedia if you already have an idea what you're looking for is to look at the page for the resource that you're interested in. In this case, you want
http://dbpedia.org/resource/Ernest_Hemingway, which redirects to
http://dbpedia.org/page/Ernest_Hemingway
On that page, you'll see that the property relating a resource to its abstract is dbpedia-owl:abstract, and the thumbnail or image is dbpedia-owl:thumbnail. Thus, you want a query like the following (which you can run on the DBpedia SPARQL endpoint). I've taken the liberty of restricting the results to just the English language abstract.
prefix dbpedia: <http://dbpedia.org/resource/>
prefix dbpedia-owl: <http://dbpedia.org/ontology/>
select ?abstract ?thumbnail where {
dbpedia:Ernest_Hemingway dbpedia-owl:abstract ?abstract ;
dbpedia-owl:thumbnail ?thumbnail .
filter(langMatches(lang(?abstract),"en"))
}
SPARQL results

sparql query returning nothing

Why this SPARQL not returning any value
PREFIX ontology: <http://dbpedia.org/ontology/>
SELECT ?Abstract
WHERE
{
<http://dbpedia.org/resource/Cologne> <http://dbpedia.org/ontology/wikiPageRedirects> ?page .
?page <http://dbpedia.org/ontology/abstract> ?Abstract.
FILTER (lang(?Abstract)='en')
}
Two reasons. Firstly, there are no redirects of Cologne, but there are redirects to it. So:
PREFIX ontology: <http://dbpedia.org/ontology/>
SELECT *
WHERE
{
<http://dbpedia.org/resource/Cologne>
<http://dbpedia.org/ontology/wikiPageRedirects> ?page .
}
returns nothing, whereas:
PREFIX ontology: <http://dbpedia.org/ontology/>
SELECT *
WHERE
{
?page
<http://dbpedia.org/ontology/wikiPageRedirects>
<http://dbpedia.org/resource/Cologne> .
}
does work.
Secondly none of these redirects appears to have an abstract. Cologne itself does, so you could just use that:
PREFIX ontology: <http://dbpedia.org/ontology/>
SELECT ?Abstract
WHERE
{
<http://dbpedia.org/resource/Cologne>
<http://dbpedia.org/ontology/abstract> ?Abstract.
FILTER (lang(?Abstract)='en')
}
It does not return any value because nothing matches your query. If you dereference the URI for Cologne, you can see that there is no wikiPageRedirects property. dbpedia:Cologne does not redirects to anything, but many resources redirect to dbpedia:Cologne. However, the resources that redirect to Cologne do not have an abstract. In fact, this is quite normal: in Wikipedia, you cannot edit a page that redirects to another one, as you are necessarily redirected to the other. So when dbpedia extracts data from Wikipedia, of course there is nothing about the redirected pages.