问题
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?
回答1:
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:
- there is a
rdfs:label
"Sao Paulo" - the label "Sao Paulo" is language-tagged English
- there is an
rdfs:abstract
- the abstract is language-tagged English
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:
SELECT * WHERE {
?x rdfs:label "São Paulo"@en.
?x rdf:type dbpedia-owl:Settlement.
?x dbpedia-owl:abstract ?abstract.
FILTER (LANGMATCHES(LANG(?abstract), 'en'))
}
Instead of rdf:type
you can use the shorthand a
, which you can read as "?x is a ?y".
Also, LANGMATCHES(LANG(?abstract), 'en')
may be more efficient to evaluate than LANG(?abstract) = 'en')
.
Note that this resource dbpedia:São_Paulo
is not a dbpedia-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.
来源:https://stackoverflow.com/questions/14404805/query-city-state-country-abstracts-in-english