SPARQL query for dbpedia to find ski lifts - sparql

I'm currently trying to teach myself how to formulate SPARQL queries to extract tourism-related information from DBpedia (via http://dbpedia.org/sparql/).
So far, I've managed to get all museums for a country.
select ?thing ?type ?category ?long ?lat ?country
where
{
VALUES ?country { <http://dbpedia.org/resource/Canada> }
optional
{
?city dbo:country ?country
}
?thing dbo:location ?city.
optional
{
?thing a ?type .
VALUES ?type { dbo:Museum }
BIND( 'Museum' as ?category )
}
optional
{
?thing a ?type.
VALUES ?type { dbo:skiLift }
BIND( 'Skilift' as ?category )
}
optional
{
?thing geo:long ?long.
?thing geo:lat ?lat
}
{
?thing a dbo:Place
}
filter (BOUND (?type))
}
However, I don't understand what I need to do to also get the same information for things like dbo:skiLift, dbo:touristicSite and the like (found here: http://dbpedia.org/ontology/Place).
What am I doing wrong?

This is because both dbo:skiLift and dbo:touristicSite are properties. These resources show up in the page for Place not as subclasses of Place, but as properties which have the class Place as their domain or range. If you want to find subclasses of Place you can perform the exploratory query (which also uses property path to retrieve the transitive closure of the subClassOf property):
select ?thing
where
{
?thing rdfs:subClassOf+ <http://dbpedia.org/ontology/Place> .
}
Apart from that, I cannot understand why you use two optional clauses for different types in the same query. For example, the following query retrieves museums located at cities of Canada, possibly with their lat and lon, without the use of other optional clauses:
select ?thing ?city ?long ?lat
where
{
?city dbo:country <http://dbpedia.org/resource/Canada> .
?thing dbo:location ?city .
?thing a dbo:Museum .
optional
{
?thing geo:long ?long .
?thing geo:lat ?lat
}
}

Related

Wikidata SPARQL query to return only most recent population

I have this query that returns each value of a POPULATION (property) for each POINT IN TIME (property), but I only want the most recent population for each city. Sorry for the extraneous query items.
SELECT ?cityLabel ?gps ?population ?date (SAMPLE(?areacode) as ?areacode_sample) (SAMPLE(?image) as ?image_sample)
WITH {
SELECT DISTINCT *
WHERE {
?city wdt:P31/wdt:P279* wd:Q515 .
?city wdt:P17 wd:Q30 .
?city wdt:P625 ?gps .
?city p:P1082 ?statement .
?statement ps:P1082 ?population .
?statement pq:P585 ?date .
OPTIONAL {?city wdt:P473 ?areacode .
}
OPTIONAL {?city wdt:P18 ?image .
}
}
ORDER BY DESC(?population)
} AS %i
WHERE {
INCLUDE %i
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . }
}
GROUP BY ?cityLabel ?gps ?population ?date
ORDER BY DESC(?population)
LIMIT 750

SPARQL - Extracting Director and Starring from DBPedia

Could anyone please help me with this code. It doesn't come out with any output but with no error.
q = """SELECT DISTINCT ?label ?abstract ?director ?starring
WHERE {
<http://dbpedia.org/resource/Seven_Beauties> rdfs:label ?label.
?label dbo:abstract ?abstract.
?label dbo:director ?director.
?label dbo:starring ?starring.
FILTER (lang(?label) = "en")
FILTER (lang(?abstract) = "en")
}"""
from textwrap import wrap
for result in query(q):
print(result['label'],
"\n----Director----\n",result['director'],
"\n----Starring----\n",result['starring'],
"\n----Abstract----\n",
"\n".join(wrap(result['abstract'])))
<http://dbpedia.org/resource/Seven_Beauties> is the entity which has a Label, Abstract, Director, Star, etc.
The Label of that entity is a literal, and it has no Director, Star, Abstract, etc.
Try changing your query to --
SELECT DISTINCT ?label ?abstract ?director ?starring
WHERE
{
<http://dbpedia.org/resource/Seven_Beauties>
rdfs:label ?label ;
dbo:abstract ?abstract ;
dbo:director ?director ;
dbo:starring ?starring
FILTER ( lang(?label) = "en" )
FILTER ( lang(?abstract) = "en" )
}
-- and see results from DBpedia.
DBpedia Live delivers nothing for the query above -- because the descriptions of films vary. Making some things optional will get you more results on DBpedia Live --
SELECT DISTINCT ?label ?abstract ?director ?starring
WHERE
{
<http://dbpedia.org/resource/Seven_Beauties> rdfs:label ?label
OPTIONAL { <http://dbpedia.org/resource/Seven_Beauties> dbo:abstract ?abstract }
OPTIONAL { <http://dbpedia.org/resource/Seven_Beauties> dbo:director ?director }
OPTIONAL { <http://dbpedia.org/resource/Seven_Beauties> dbo:starring ?starring }
FILTER ( lang(?label) = "en" )
FILTER ( lang(?abstract) = "en" )
}

Get data about attractions and point of interest from DBpedia using SPARQL

I don't know how to use DBpedia or SPARQL.
Could you please help me in finding data list of attractions and point of interest from DBpedia.
Actually I want Hotels and Tourist Places from all Cities/Country with Latitude, Longitude, attraction Name, Location Name, City, State, continents, etc.
Please Help me to get data using Query of SPARQL.
If you have any other suggestion then please guide me..
This is the endpoint: http://dbpedia.org/sparql/
To get all the hotels in Syria:
select * where {
?hotel a <http://dbpedia.org/ontology/Hotel> .
?hotel dbo:location dbr:Syria
}
This answer is just to give you an idea how to query DBPedia
Update
After you comment Suppose I want to get all Tourist Places or point-of-interest for Mumbai City only
You should define what those "Tourist Places* are? In the below query, I gave you an example, assuming the tourist places are museums, towers, parks, churches, Skyscrapers,
select ?thing ?type ?typeName where {
VALUES ?city {<http://dbpedia.org/resource/London>}
?thing dbo:location ?city.
optional
{
?thing a ?type .
VALUES ?type {<http://dbpedia.org/ontology/Hotel>}
BIND( "Hotel" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {dbo:Museum}
BIND( "Museum" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {dbo:Pyramid}
BIND( "Pyramid" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {yago:Skyscraper104233124}
BIND( "Skyscraper" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {dbo:Park}
BIND( "Park" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {yago:Church103028079}
BIND( "Church" as ?typeName )
}
{
?thing a dbo:Place
}
filter (BOUND (?type))
}
If you get the idea you can extend it
Update 2
You wanted to show long, lat and country. Here you go:
select ?thing ?type ?typeName ?long ?lat ?country where {
VALUES ?city {<http://dbpedia.org/resource/London>}
optional {
?city dbo:country ?country
}
?thing dbo:location ?city.
optional
{
?thing a ?type .
VALUES ?type {<http://dbpedia.org/ontology/Hotel>}
BIND( "Hotel" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {dbo:Museum}
BIND( "Museum" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {dbo:Pyramid}
BIND( "Pyramid" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {yago:Skyscraper104233124}
BIND( "Skyscraper" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {dbo:Park}
BIND( "Park" as ?typeName )
}
optional
{
?thing a ?type.
VALUES ?type {yago:Church103028079}
BIND( "Church" as ?typeName )
}
optional
{
?thing geo:long ?long.
?thing geo:lat ?lat
}
{
?thing a dbo:Place
}
filter (BOUND (?type))
}

Group_concat in SPARQL

I am a beginner with SPARQL, and I am trying to deal with the endpoint of the Spanish National Library.
I have a code that works, here it is:
prefix bne: <http://datos.bne.es/def/> # base URI for ontology documented at http://datos.bne.es/def/
prefix resource: <http://datos.bne.es/resource/>
select distinct
?book
?author
?title
?subtitle
?ISBN
?publisher
?date
?pags
?size
?series
?edition
?subjectLabel
where {
?book a bne:C1003 .
?book bne:P3001 "Errata Naturae" .
?book bne:P1011 ?author .
?book bne:P3002 ?title .
OPTIONAL { ?book bne:P3013 ?ISBN }
OPTIONAL { ?book bne:P3001 ?publisher }
OPTIONAL { ?book bne:P3006 ?date }
OPTIONAL { ?book bne:P3004 ?pags }
OPTIONAL { ?book bne:P3007 ?size }
OPTIONAL { ?book bne:P3016 ?series }
OPTIONAL { ?book bne:P1004 ?date }
OPTIONAL { ?book bne:P3017 ?edition }
OPTIONAL { ?book bne:P3014 ?subtitle }
OPTIONAL { ?book bne:OP3008 ?subject }
?subject rdfs:label ?subjectLabel
}
limit 50
But as some books has two or more subjects, the SPARQL repeats them in the results. I used group_concat, but for some reason it doesn't work:
prefix bne: <http://datos.bne.es/def/> # base URI for ontology documented at http://datos.bne.es/def/
prefix resource: <http://datos.bne.es/resource/>
select distinct
?book
?author
?title
?subtitle
?ISBN
?publisher
?date
?pags
?size
?series
?edition
(GROUP_CONCAT(DISTINCT(?subjectLabel); separator="//") as ?subjects)
where {
?book a bne:C1003 .
?book bne:P3001 "Errata Naturae" .
?book bne:P1011 ?author .
?book bne:P3002 ?title .
OPTIONAL { ?book bne:P3013 ?ISBN }
OPTIONAL { ?book bne:P3001 ?publisher }
OPTIONAL { ?book bne:P3006 ?date }
OPTIONAL { ?book bne:P3004 ?pags }
OPTIONAL { ?book bne:P3007 ?size }
OPTIONAL { ?book bne:P3016 ?series }
OPTIONAL { ?book bne:P1004 ?date }
OPTIONAL { ?book bne:P3017 ?edition }
OPTIONAL { ?book bne:P3014 ?subtitle }
OPTIONAL { ?book bne:OP3008 ?subject }
?subject rdfs:label ?subjectLabel
}
limit 50
group by ?book
order by ?date
Does someone know where I am making a mistake?
Thanks!
Edit:
I was doing one thing wrong: as #AKSW said, I have to group all the variables at the end of the code, or add variables on the select. I have a reduced version of the code for testing this:
PREFIX bne: <http://datos.bne.es/def/>
PREFIX resource: <http://datos.bne.es/resource/>
SELECT ?book ?author ?title (GROUP_CONCAT(DISTINCT ?subject ; separator='//') AS ?subjects)
WHERE
{ ?book a bne:C1003 ;
bne:P3001 "Errata Naturae" ;
bne:P1011 ?author ;
bne:P3002 ?title ;
bne:OP3008 ?subject
}
#group everything here
GROUP BY ?book ?author ?title
#JeenBroekstra, when I run it in a SPARQL Validator, it says it is OK, but when I try to run it in the SPARQL endpoint of the library, it gives me an error:
Virtuoso 37000 Error SP030: SPARQL compiler, line 6: syntax error at 'GROUP_CONCAT' before '('
As noted in comments, the problem is that the target endpoint is running on Virtuoso, Open Source Edition, v06.01.3127 as of 2011-11-16), which did not support GROUP_CONCAT in SPARQL, as SPARQL 1.1 had not yet been finalized.
Upgrading to a current version is strongly recommended!
There is a built-in function in this version of Virtuoso, available as sql:group_concat, as documented, which may serve you for now.

SPARQL DbPedia foaf:name

Trying to execute some queries but when searching for foaf:name resultset is empty. Here's my code:
SELECT DISTINCT ?uri ?string
WHERE {
?uri rdf:type ?x.
?uri foaf:name 'Cavallo domestico'#it .
OPTIONAL { ?uri rdfs:label ?string . FILTER (lang(?string) = 'it') }
}
page exist http://it.dbpedia.org/resource/Equus_caballus/html
Apparently seems it's not related with languages different than english but with foaf:name request. If I execute following, retrieving generic foaf:givenName, it works:
SELECT DISTINCT ?uri ?string
WHERE {
?uri rdf:type ?x.
?uri foaf:givenName 'Jimmy'#en .
OPTIONAL { ?uri rdfs:label ?string . FILTER (lang(?string) = 'en') }
}
I think this wasn't working when I first mentioned that it didn't in a comment, but, as AKSW points out, this seems to be working now. The rdfs:label property has the article titles in various languages, not the foaf:name, so you can do this to get the types of Horse:
select ?x ?type {
?x a ?type ;
rdfs:label "Equus caballus"#it
}
SPARQL results