Get geographical coordinates of a particular place from DbPedia - sparql

I need to get the co-ordinates of some particular places from DbPedia. For the same I am using following snippet:
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT * WHERE {
?x foaf:name 'Mumbai'.
?x dbo:Place 'Mumbai'.
?x geo:lat ?lat .
?x geo:long ?long .
}
On running at http://dbpedia.org/sparql, it returns an empty page. I think I didn't put the name of place in the correct way. Can anyone help me to find the issue

Have a look at the N3/TTL serialization of the data for Mumbai. Note that the foaf:name value has a language tag: foaf:name "Mumbai"#en. You need to use "Mumbai"#en, with the language tag, in the query. Additionally, doing dbo:Place 'Mumbai' doesn't make any sense. You might instead want to ask for things which are dbpedia-owl:Places. You'd end up with a query like:
select * {
?mumbai a dbpedia-owl:Place ;
foaf:name "Mumbai"#en ;
geo:lat ?lat ;
geo:long ?long
}
SPARQL Results

Related

Sparql DBPedia not returning foaf:name query

I am using the Virtuoso SPARQL endpoint for DBpedia. I am having no issue running some test queries, but my issue is when I run on a query based on the foaf:name "literal" of an entity. Here is my code below:
prefix db: <http://dbpedia.org/resource/>
prefix dbo: <http://dbpedia.org/ontology/>
prefix foaf: <http://xlms.com/foaf/0.1/>
SELECT ?capital WHERE{
?state dbo:capital ?capital .
?state foaf:name "State of Alaska"#en .
}
My logic is that by ?state dbo:capital ?capital refers to the capital of a state within DBpedia owl ontology. ?state foaf:name "State of Alaska"#en should match the variable ?state up with the literal entity Alaska. I am having no issue when my queries are controlled by variables that are not foaf:name. I have looked up examples and can't see what the issue is with my syntax. Even when I edit the last line to --
?state a foaf:name "State of Alaska"#en .
-- the code continues to fail. I have looked up other examples and can't find my specific issue. It appears as if this code SHOULD return the capital for Alaska based on the foaf:name Literal.
I found the answer. The key is to not add this prefix at the top
prefix foaf: <http://xlms.com/foaf/0.1/>
That should be replaced with the correct foaf: prefix declaration:
prefix foaf: <http://xmlns.com/foaf/0.1/>
Or probably even skipped, since the foaf: prefix is predefined.
So the query below actually works:
SELECT ?capital WHERE {
?state dbo:capital ?capital .
?state foaf:name "State of Alabama"#en .
}

SPARQL Federated Query Not Returning All Solutions

This is an evolution of this question.
Basically I am having trouble getting all the solutions to a SPARQL query from a remote endpoint. I have read through section 2.4 here because it seems to describe a situation almost identical to mine.
The idea is that I want to filter my results from DBPedia based on information in my local RDF graph. The query is here:
PREFIX ns1:
<http://www.semanticweb.org/caeleanb/ontologies/twittermap#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT *
WHERE {
?p ns1:displayName ?name .
SERVICE <http://dbpedia.org/sparql> {
?s rdfs:label ?name .
?s rdf:type foaf:Person .
}
}
And the only result I get is dbpedia:John_McCain (for ?s). I think this is because John McCain is the only match in the first 'x' results, but I can't figure out how to get the query to return all matches. For example, if I add a filter like:
SERVICE <http://dbpedia.org/sparql> {
?s rdfs:label ?name .
?s rdf:type foaf:Person .
FILTER(?name = "John McCain"#en || ?name = "Jamie Oliver"#en)
}
Then it correctly returns BOTH dbpedia:Jamie_Oliver and dbpedia:John_McCain. There are dozens of other matches like Jamie Oliver that do not come through unless I specifically add it to a Filter like this.
Can someone explain a way to extract the rest of the matches? Thanks.
It looks like the cause of this issue is that the SERVICE block is attempting to pull all foaf:Persons from DBPedia, and then filter them based on my local Stardog db. Since there is a 10,000 result limit when querying DBPedia, only matches which occur in that set of 10,000 arbitrary Persons will be found. To fix this, I wrote a script to put together a FILTER block containing every string name in my Stardog db and attached it to the SERVICE block to filter remotely and thereby avoid hitting the 10,000 result limit. It looks something like this:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX ns1: <http://www.semanticweb.org/caeleanb/ontologies/twittermap#>
CONSTRUCT{
?s rdf:type ns1:Person ;
ns1:Politician .
}
WHERE {
?s rdfs:label ?name .
?s rdf:type dbo:Politician .
FILTER(?name IN ("John McCain"#en, ...)
}

Fetching a list of books adapted into films from dbpedia

I am trying to fetch a list of American movies adapted from books from dbpedia. This is what I have so far:
PREFIX onto: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
SELECT * WHERE {
?movie dcterms:subject <http://dbpedia.org/resource/Category:American_films> .
?movie dcterms:subject <http://dbpedia.org/resource/Category:Films_based_on_novels> .
?movie onto:basedOn ?book .
?book a onto:Book .
}
I get only 4 results back. Is there another property I can use to get more results?
First thing, try removing the second line of your pattern --
?movie dcterms:subject <http://dbpedia.org/resource/Category:Films_based_on_novels> .
You'll get a great many more results from --
PREFIX onto: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
SELECT * WHERE
{
?movie dcterms:subject <http://dbpedia.org/resource/Category:American_films> ;
onto:basedOn ?book .
?book a onto:Book .
}
Have you looked at the descriptions of the results you did get? That will show you the properties you might use...
Remember that you're limited by the data in DBpedia, which may not have what you think it should (for instance, not every film based on a novel is explicitly categorized as such, as you can see by the different results I got), and may also be 6 or more months out of sync with Wikipedia. You might look at DBpedia-live, for constantly updated ingestion.

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!
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 !
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

SPARQL for direct dbpedia resource OR wikiPageRedirects

I'm trying to query data from dbpedia by a country's name. I want it to find it whether there is a resource for that country or via its existence in wikiPageRedirects. Here is a working version:
PREFIX res: <http://dbpedia.org/resource/>
PREFIX ont: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?country ?capital ?label
WHERE {
{ res:Dominion_of_Canada ont:capital ?capital .
?capital rdfs:label ?label }
UNION
{ res:Dominion_of_Canada ont:wikiPageRedirects ?country .
?country ont:capital ?capital .
?capital rdfs:label ?label }
FILTER (lang(?label) = "en")
}
I'd like (if possible), to factor out the ?country. Is it possible to assign a resource to a variable such that the SPARQL query looks like the following?
SELECT ?country ?capital ?label
WHERE {
{ ?country EXISTS res:Dominion_of_Canada } # to get the idea across
UNION
{ res:Dominion_of_Canada ont:wikiPageRedirects ?country }
?country ont:capital ?capital .
?capital rdfs:label ?label .
FILTER (lang(?label) = "en")
}
As ever, speed is important, too. If the resource exists, then it'd be better if it skipped searching on wikiPageRedirects.
Checking whether a resource "exists" or not is a bit vague, since IRIs are just constant data. The question is really whether DBpedia contains any triples about a particular resource. In your case, you're wanting to know whether it it redirects to anything else, or if it has properties of its own. A property path of the form dbpedia:France dbpedia-owl:wikiPageRedirects* ?country is really probably the best way to do that. If there are no redirect links, then ?country is dbpedia:France, and if there are, then ?country is the value of the redirects. The only way to "check" is to look for those triples. I think that means you will end up with something like this (similar to what's shown in my answer to another question involving redirects):
select ?country ?anthem ?author {
#-- The only way to really "check" that the resource
#-- "exists" and is not a redirect, is by checking
#-- whether it has any redirect links. If it doesn't,
#-- then ?country is dbpedia-owl:France, like you want
#-- and if it does, then then you want to follow them.
dbpedia:France dbpedia-owl:wikiPageRedirects* ?country .
#-- I'm using anthem and author here because
#-- it doesn't look like there was reliable information
#-- about the capital.
?country dbpedia-owl:anthem ?anthem .
?anthem dbpprop:author ?author .
}
SPARQL results
What about this?
PREFIX dbr: <http://dbpedia.org/resource/>
select dbr:France ?capital ?label where {
{dbr:France a dbpedia-owl:Country.
dbr:France dbpedia-owl:capital ?capital .
?capital rdfs:label ?label .
}
union {dbr:France dbpedia-owl:wikiPageRedirects ?redirectPage.
?redirectPage dbpedia-owl:capital ?capital.
?capital rdfs:label ?label .
}
}
Result