I am trying to query city, state and country abstracts in English from DBpedia with mixed results. It seems to work well with City and Country but not with State.
SELECT * WHERE {
?x rdfs:label "France"@en.
?x dbpedia-owl:abstract ?abstract.
FILTER (LANG(?abstract) = 'en')
}
Or
SELECT * WHERE {
?x rdfs:label "Boston"@en.
?x dbpedia-owl:abstract ?abstract.
FILTER (LANG(?abstract) = 'en')
}
However, these queries don't find any results:
SELECT * WHERE {
?x rdfs:label "Sao Paulo"@en.
?x dbpedia-owl:abstract ?abstract.
FILTER (LANG(?abstract) = 'en')
}
SELECT * WHERE {
?x rdfs:label "Massachusetts"@en.
?x dbpedia-owl:abstract ?abstract.
FILTER (LANG(?abstract) = 'en')
}
First, how can I filter the search to only Cities, States or Countries? Second, how can I find states such as Massachusetts or Rhone in France?
SPARQL queries (without
OPTIONAL
) only return results for which all triple patterns can be matched. For your queries that do not return results, this means one or more of the following statements about a resource ?x are not available:rdfs:label
"Sao Paulo"rdfs:abstract
In this example, you probably wanted to find
<http://dbpedia.org/resource/Sao_Paulo>
. The label for this resource is not "Sao Paulo", but "São Paulo".The second query worked (Massachusetts) for me, but took some time to complete.
To restrict your results to resources of a certain type (like City, State or Country), you need to specify the type. In RDF, a type (or rather 'class') of a resource is specified using
rdf:type
. You can query that in the same way:Instead of
rdf:type
you can use the shorthanda
, which you can read as "?x is a ?y".Also,
LANGMATCHES(LANG(?abstract), 'en')
may be more efficient to evaluate thanLANG(?abstract) = 'en')
.Note that this resource
dbpedia:São_Paulo
is not adbpedia-owl:City
in DBpedia, because it is a municipality on Wikipedia: http://en.wikipedia.org/wiki/S%C3%A3o_Paulo. Similarly,dbpedia:Massachusetts
is not defined as a state. Both are dbpedia-owl:PopulatedPlaces, though.