How to extract data from DBpedia using SPARQL - sparql

I'm trying to extract some data from dbpedia using SERVICE function of SPARQL.
In fact I want to extract the names, the lat and lot of all New York theaters. To check if an instance is a theater I can use http://dbpedia.org/class/yago/Theater104417809​. One example of a theater could be http://dbpedia.org/resource/Grand_Theatre_(New_York_City).
How to use service function for getting what I need in SPARQL?
** EDIT **
The query that I'm trying is the following one, but is not returning any value.
PREFIX dbpedia: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX geos: <http://www.opengis.net/ont/geosparql#>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX : <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#>
SELECT *
WHERE {
SERVICE <http://dbpedia.org/sparql/> {
SELECT ?teatreName ?lat ?long
WHERE {
?teatre rdf:type dbpedia:Theatre .
?teatre foaf:name ?teatreName .
?teatre geo:lat ?lat .
?teatre geo:long ?long .
?teatre dbp:city ?ciutat .
?ciutat rdfs:label "New York City"#en
}
}
}

It's not an issue with federated querying, but with your DBpedia query. dbp:city is not an object property but simply of type rdf:Property, thus it's untyped. In your case, it maps to literals which means, you have to use the literal directly. The weird thing here is, that for some reasons you have to use the datatype http://www.w3.org/1999/02/22-rdf-syntax-ns#langString explicitly instead of "New York City"#en - that's clearly not intuitive for any user. Not sure whether this happened due to the DBpedia extraction or is the expected behaviour of Virtuoso.
PREFIX dbpedia: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX geos: <http://www.opengis.net/ont/geosparql#>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX : <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#>
SELECT *
WHERE {
SERVICE <http://dbpedia.org/sparql/> {
SELECT ?teatreName ?lat ?long
WHERE {
?teatre rdf:type dbpedia:Theatre .
?teatre foaf:name ?teatreName .
?teatre geo:lat ?lat .
?teatre geo:long ?long .
?teatre dbp:city "New York City"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>
}
}
}

Related

SPARQL query filtered by minus 100 km from a specific geo:lat and geo:long

I have a database that contains msgs. each msg has geo:long geo:lat stored in the database.
I want to get the msgs that were sent from less than 100 kilometres from different specified geo:long geo:lat which is (50.25 -90.25)
PREFIX off: <http://www.semanticweb.org/balo/ontologies/2020/1/MSGING#>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX spatial: <http://jena.apache.org/spatial#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT ?msg
WHERE {
?msg rdf:type off:Msg.
?msg geo:long ?long.
?msg geo:lat ?lat
}
The code above is missing the filter, but I have put it for reflection.
What should I write as a filter?
Note: I am using StarDog to do the queries.
I came up with this solution and its returning resluts, is it acceptable?
PREFIX off: <http://www.semanticweb.org/balo/ontologies/2020/1/MSGING#>
PREFIX geosf: <http://www.opengis.net/def/function/geosparql/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?msg ?airline
WHERE {
?msg rdf:type off:msg;
off:has_airline ?airline;
geosf:nearby("50.25" "-90.25" 100 <http://qudt.org/vocab/unit#Kilometer> ) .
}
PREFIX off: <http://www.semanticweb.org/balo/ontologies/2020/1/MSGING#>
PREFIX geosf: <http://www.opengis.net/def/function/geosparql/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?msg ?airline
WHERE {
?msg rdf:type off:msg;
off:has_airline ?airline;
geosf:nearby("50.25" "-90.25" 100 <http://qudt.org/vocab/unit#Kilometer> ) .
}

How to filter date to get the film release date in sparql using regex

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX db: <http://dbpedia.org/ontology/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?releaseDate
WHERE {
?film rdf:type db:Film;
foaf:name ?title.
?film dbp:releaseDate ?releaseDate.
FILTER regex(?releaseDate, "2016", "i").
}
Getting the release date but only two i am not sure about querying . since i am new to this.

How to query DBpedia SPARQL by resource uri?

I'm querying DBpedia types in SPARQL (http://dbpedia.org/sparql) by resource's label
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX : <http://dbpedia.org/resource/>
PREFIX ru: <http://ru.dbpedia.org/resource/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?type ?superType WHERE { {
?res rdfs:label "HarryPotter"#en.
} UNION {
?redir dbo:wikiPageRedirects ?res .
?redir rdfs:label "HarryPotter"#en .
}
?res rdf:type ?type .
OPTIONAL {
?type rdfs:subClassOf ?superType .
}
}
It works fine.
But what if I know the exact resource - http://dbpedia.org/page/Harry_Potter? I tried something like:
?res a :Harry_Potter.
But it does not work.
How to query DBpedia types and supertypes if I know the resource URI? I can't figure out which property or operator I should use (e.g., rdfs:Resource, a, etc., which do not work)
When you write
?res a :Harry_Potter.
It doesn't work, because this means "a resource, which is of type :Harry_Potter". It is equivalent to
?res rdf:type :Harry_Potter.
:Harry_Potter identifies a resource and not the type, thus it should be used in place of ?res.
Also I think you mean Harry_Potter_(character), because that is the actual identifier and not redirect.
You query would be as simple as
SELECT ?type ?superType WHERE
{
# give me ?type of the resource
<http://dbpedia.org/resource/Harry_Potter_(character)> rdf:type ?type .
# give me ?superTypes of ?type
OPTIONAL {
?type rdfs:subClassOf ?superType .
}
}
You can just put the URI as the subject in there WHERE conditions.
SELECT ?title, ?releaseDate
WHERE {
<http://dbpedia.org/resource/Super_Mario_Bros._3> dbp:title ?title .
<http://dbpedia.org/resource/Super_Mario_Bros._3> dbo:releaseDate ?releaseDate .
}

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.