维基数据SPARQL查询:如何获取属性"适用于部件"的值(P518)



在维基数据上描述飓风伊恩的页面上,我们可以在维基数据上查询受该事件影响的国家的哪些部分(佛罗里达州,佐治亚州等)

我正试图通过python脚本编写查询以获取这些信息:

SELECT distinct ?label ?item ?superTypeLabel ?date ?start_date ?end_date  ?countryLabel   ?countryPart
WHERE { ?item wdt:P31 wd:Q63100601;
wdt:P31 ?superType;
rdfs:label ?label.
?superType rdfs:label ?superTypeLabel.
# with a point in time or start date
FILTER langMatches(lang(?label),'en').
FILTER langMatches(lang(?superTypeLabel),'en').
# with a point in time or start date
OPTIONAL { ?item wdt:P585 ?date. }
OPTIONAL { ?item wdt:P580 ?date. }
OPTIONAL { ?item wdt:P580 ?start_date. }
OPTIONAL { ?item wdt:P582 ?end_date. }
OPTIONAL { ?item wdt:P17 ?country. 
?country rdfs:label ?countryLabel. 
FILTER langMatches(lang(?countryLabel),'en').}
OPTIONAL { ?item wdt:P518 ?countryPart. }
# but at least one of those
FILTER(BOUND(?date) && DATATYPE(?date) = xsd:dateTime).
# not in the future, and not more than 31 days ago
BIND(NOW() - ?date AS ?distance).
FILTER(0 <= ?distance && ?distance < 365).
FILTER contains(?label,"Hurricane Ian") 
}

前面的查询工作得很好,除了?countryPart值之外,查询不返回任何值,而在HTML页面上,信息得到了很好的表示。请帮忙好吗?

我发现如何通过添加以下行来获取国家部分:

OPTIONAL { ?item  p:P17 [ps:P17 ?country; pq:P518 ?countryPart] }

最终查询:

SELECT distinct ?label ?item ?superTypeLabel ?date ?start_date ?end_date  ?country   ?countryPart
WHERE { ?item wdt:P31 wd:Q63100601;
wdt:P31 ?superType;
rdfs:label ?label.
?superType rdfs:label ?superTypeLabel.
# with a point in time or start date
FILTER langMatches(lang(?label),'en').
FILTER langMatches(lang(?superTypeLabel),'en').
# with a point in time or start date
OPTIONAL { ?item wdt:P585 ?date. }
OPTIONAL { ?item wdt:P580 ?date. }
OPTIONAL { ?item wdt:P580 ?start_date. }
OPTIONAL { ?item wdt:P582 ?end_date. }
OPTIONAL { ?item  p:P17 [ps:P17 ?country; pq:P518 ?countryPart] }
# but at least one of those
FILTER(BOUND(?date) && DATATYPE(?date) = xsd:dateTime).
# not in the future, and not more than 31 days ago
BIND(NOW() - ?date AS ?distance).
FILTER(0 <= ?distance && ?distance < 365).
FILTER contains(?label,"Hurricane Ian") 
}

最新更新