I'm currently learning SPARQL, and I'm exploring the data from dbpedia. Why does this query work:
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT
?label
WHERE {
dbr:Leipzig rdfs:label ?label.
} LIMIT 20
But this does not (i.e. id does not return anything)
PREFIX geo: <https://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT
?label ?lat
WHERE {
dbr:Leipzig rdfs:label ?label.
dbr:Leipzig geo:lat ?lat.
} LIMIT 20
Because the protocol of the WGS 84 Geo namespace is http and not https, i.e. http://www.w3.org/2003/01/geo/wgs84_pos#
Related
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>
}
}
}
I'm a beginner in SPARQL. And I'm having problems to get the latitude and longitude of all university by city on DBpedia.
I tried multiple things without success.
This page shows the universities of Paris on the dbo:campus property, so I like to get the list of the universities with this property and after that get the geographics coordinates.
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?name, ?univ, ?lat, ?long WHERE {
?p rdf:type dbo:Place.
?p rdfs:label ?name.
?p dbo:campus ?u.
?u geo:lat ?lat.
?u geo:long ?long.
?u rdfs:label ?univ
FILTER(LANG(?name) = "en").
FILTER(?name = "Paris")
}
I check this post DBpedia SPARQL Query US Universities but it doesn't work with another country.
If you read "is SOME_PROPERTY of"on a rendered DBpedia page, this means the inverse direction, i.e., it shows the triple in its inverted form. Thus, you have to invert the triple pattern in the SPARQL query. For your example, it means that universities are the subject and Paris the object:
?u dbo:campus ?p
The labels are language tagged in DBpedia; thus, FILTER(?name = "Paris") is not enough. Adding the English language tag helps:
FILTER(?name = "Paris"#en)
A working query would be
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?name, ?univ WHERE {
?p rdf:type dbo:Place.
?p rdfs:label ?name.
?u dbo:campus ?p.
?u geo:lat ?lat.
?u geo:long ?long.
?u rdfs:label ?univ
FILTER(LANG(?name) = "en").
FILTER(?name = "Paris"#en)
}
Some comments:
Using the label to match a resource can lead to unwanted results. Resources are identified by URIs; thus, use the URI if possible. The VALUES clause is a cool feature of SPARQL 1.1 to support inline data.
If you use the URI, you could omit the rdf:type triple pattern since you wouldn't have to filter for resources of a specific type given the label.
The official SPARQL standard doesn't allow commas in between the projection variables; this is Virtuoso-specific syntax.
SPARQL supports the more compact Turtle syntax.
A FILTER doesn't need a . at the end.
Try to use LANGMATCHES for matching languages in literals.
A "better" query could be:
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?name ?univ ?lat ?long
WHERE
{ VALUES ?p { dbo:Paris }
?p rdfs:label ?name .
?u dbo:campus ?p ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?univ
FILTER langMatches(lang(?name), "en")
}
I'm traying to get the regions of Italy in both Italian and English. I can get then in one laguage with this query...
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT DISTINCT ?RegionIT ?RegionITLabel ?ISO_code ?Geo
{
?RegionIT wdt:P31 wd:Q16110;
wdt:P300 ?ISO_code;
wdt:P625 ?Geo
SERVICE wikibase:label { bd:serviceParam wikibase:language "it" }
}
ORDER BY ?regionITLabel
... but adding another language using the standard SPARQL syntax doesn't work.
... but adding another language using the standard SPARQL syntax doesn't work.
How are you doing that? This works:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT DISTINCT ?RegionIT ?label (lang(?label) as ?label_lang) ?ISO_code ?Geo
{
?RegionIT wdt:P31 wd:Q16110;
wdt:P300 ?ISO_code;
wdt:P625 ?Geo ;
rdfs:label ?label
}
order by ?RegionIT
Link to try query
To limit to just Italian and English filter on the lang:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT DISTINCT ?RegionIT ?label ?ISO_code ?Geo
{
?RegionIT wdt:P31 wd:Q16110;
wdt:P300 ?ISO_code;
wdt:P625 ?Geo ;
rdfs:label ?label
filter(lang(?label) = 'it' || lang(?label) = 'en')
}
order by ?RegionIT
Link to try query
Obviously that multiplies the number of results, one for each language. If that's an issue you can do:
...
rdfs:label ?label_it , ?label_en
filter(lang(?label_it) = 'it' && lang(?label_en) = 'en')
...
which is effectively what the language service does.
Let's list all countries in English and Russian.
#List of countries in English and Russian
SELECT ?country ?label_en ?label_ru
WHERE
{
?country wdt:P31 wd:Q6256.
?country rdfs:label ?label_en filter (lang(?label_en) = "en").
?country rdfs:label ?label_ru filter (lang(?label_ru) = "ru").
}
SPARQL query
This example was taken from the tutorial Research in programming Wikidata, section "Countries".
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 .
}
I have a SPARQL Query, and I want to eliminate all disambigution resources. How can I do this? This is my query:
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/>
select distinct ?Nom ?resource ?url where {
?resource rdfs:label ?Nom.
?resource foaf:isPrimaryTopicOf ?url.
FILTER (langMatches( lang(?Nom), "EN" )).
?Nom <bif:contains> "Apple".
}
You can add the following prefix and filter to your query:
prefix dbo: <http://dbpedia.org/ontology/>
filter not exists {
?resource dbo:wikiPageRedirects*/dbo:wikiPageDisambiguates ?dis
}
This says to exclude resources and resources that redirect to a resources that disambiguate some articles. That gives you a query like this:
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 dbo: <http://dbpedia.org/ontology/>
select distinct ?Nom ?resource ?url where {
?resource rdfs:label ?Nom.
?resource foaf:isPrimaryTopicOf ?url.
FILTER (langMatches( lang(?Nom), "EN" )).
?Nom <bif:contains> "Apple".
filter not exists {
?resource dbo:wikiPageRedirects*/dbo:wikiPageDisambiguates ?dis
}
}
SPARQL results
Now, even though that removes all the disambiguation pages, you may still have results that include "disambiguation" in the title. For instance, one of the results is:
The Little Apple (disambiguation)"#en
http://dbpedia.org/resource/The_Little_Apple_(disambiguation)
Even though that has "disambiguation" in the name, it's not a disambiguation page. It doesn't have any values for dbo:wikiPageDisambiguates. it does redirect to another page, though. You may want to filter out things that redirect to something else, too. You can modify the filter though:
filter not exists {
?resource dbo:wikiPageRedirects|dbo:wikiPageDisambiguates ?dis
}
That says to filter out any resource that either redirects to something, or that disambiguates something. This is actually a simpler filter, really. This makes your query:
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 dbo: <http://dbpedia.org/ontology/>
select distinct ?Nom ?resource ?url where {
?resource rdfs:label ?Nom.
?resource foaf:isPrimaryTopicOf ?url.
FILTER (langMatches( lang(?Nom), "EN" )).
?Nom <bif:contains> "Apple".
filter not exists {
?resource dbo:wikiPageRedirects|dbo:wikiPageDisambiguates ?dis
}
}
SPARQL results