SPARQL:精确的rdfs:标签查询会产生不一致的结果

时间:2017-09-18 08:37:24

标签: python sparql wikipedia dbpedia

我正在尝试通过使用SPARQL进行显式搜索来从特定城市获取人口,纬度,经度等数据。以下代码适用于像巴塞罗那这样的城市,但对毕尔巴鄂这样的城市没有任何结果。所以,以下内容:

SELECT Distinct ?city, ?country, ?lat, ?lon, ?population, ?area, ?elevation WHERE {
  ?city rdf:type <http://dbpedia.org/ontology/City> .
  ?city  <http://dbpedia.org/ontology/country> ?country .
  ?city  <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
  ?city  <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?lon .
  ?city  <http://dbpedia.org/ontology/populationTotal> ?population .
  ?city  <http://dbpedia.org/ontology/PopulatedPlace/areaTotal> ?area .
  ?city  <http://dbpedia.org/ontology/elevation> ?elevation;
rdfs:label "Barcelona"@en .
}

返回

:Barcelona  :Spain  41.3833 2.18333 1604555 "101.4"^^dbpedia:datatype/squareKilometre   12.0

但与行相同的块:

rdfs:label "Bilbao"@en .

回来了。瓦伦西亚,波哥大,比纳斯科等城市的失败......如果可能的话,我想在没有过滤器的情况下进行搜索。我使用以下过滤后的查询获得了混合结果:

SELECT ?city, ?country, ?lat, ?lon, ?population, ?area, ?elevation, ?label  WHERE {
  ?city rdf:type <http://dbpedia.org/ontology/City> .
  ?city  <http://dbpedia.org/ontology/country> ?country .
  ?city  <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
  ?city  <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?lon .
  ?city  <http://dbpedia.org/ontology/populationTotal> ?population .
  ?city  <http://dbpedia.org/ontology/PopulatedPlace/areaTotal> ?area .
  ?city  <http://dbpedia.org/ontology/elevation> ?elevation;
rdfs:label ?label .
FILTER contains ( ?label, "Bilbao")
FILTER langMatches(lang(?label),'en')
}
LIMIT 100

任何想法都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

Dbpedia数据远非同质,因此,您必须确保您的查询真正适合数据。例如,Bilbao的DBpedia资源不属于类dbo:City

SELECT * { dbr:Bilbao a ?cls }

其中,它属于类

+------------------------------------------------------------+
|                            cls                             |
+------------------------------------------------------------+
| http://www.wikidata.org/entity/Q486972                     |
| http://www.w3.org/2003/01/geo/wgs84_pos#SpatialThing       |
| http://www.w3.org/2002/07/owl#Thing                        |
| http://umbel.org/umbel/rc/Village                          |
| http://umbel.org/umbel/rc/PopulatedPlace                   |
| http://umbel.org/umbel/rc/Location_Underspecified          |
| http://schema.org/Place                                    |
| http://dbpedia.org/ontology/Settlement                     |
| http://dbpedia.org/ontology/PopulatedPlace                 |
| http://dbpedia.org/ontology/Place                          |
| http://dbpedia.org/ontology/Location                       |
| http://dbpedia.org/class/yago/YagoPermanentlyLocatedEntity |
| http://dbpedia.org/class/yago/YagoLegalActorGeo            |
| http://dbpedia.org/class/yago/YagoGeoEntity                |
| ...                                                        |
+------------------------------------------------------------+

对于属性也是如此,您必须确保资源的所有属性都存在。如果您不能这样做,请将三元模式包装到OPTIONAL子句中。请注意,由于左连接执行,此查询可能更昂贵。