多筛选Wikidata SPARQL查询异常:未返回目标美国历史县数据
SPARQL查询未返回预期的已撤销美国县实体
问题描述
我尝试生成美国已撤销的县及县等效行政区列表,但编写的SPARQL查询未返回预期结果。测试对象为弗吉尼亚州伊利诺伊县(Wikidata ID: Q14687596),该实体属于「县及县等效行政区(wd:Q47168)」的子类(历史县Q294420),且是已撤销的历史区域,但未被查询返回。
原查询代码
SELECT DISTINCT ?county ?countyLabel ?stateLabel ?page_titleEN ?coordinate_location ?inception ?dissolved ?role WHERE { ?county (wdt:P31/(wdt:P279*)) wd:Q47168; (wdt:P31/(wdt:P279*)) wd:Q1620908. OPTIONAL { ?county wdt:P131 ?state. } OPTIONAL { ?article schema:about ?county; schema:isPartOf <https://en.wikipedia.org/>; schema:name ?page_titleEN. } OPTIONAL { ?county wdt:P625 ?coordinate_location. } OPTIONAL { ?county wdt:P571 ?inception. } OPTIONAL { ?inception wdt:p2868 ?role. } OPTIONAL { ?county wdt:P576 ?dissolved.} SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } ORDER BY (?countyLabel)
问题原因
原查询要求实体同时满足两个类继承条件:
- 是
wd:Q47168(县及县等效行政区)的实例或子类 - 是
wd:Q1620908(历史区域)的实例或子类
但查看目标实体的属性:
- 弗吉尼亚州伊利诺伊县的
P31(实例类型)是wd:Q294420(历史县),而Q294420仅继承自Q47168,并未被归类为Q1620908的子类 - 该实体也没有直接设置
P31为Q1620908,因此第二个条件不满足,导致被过滤掉
解决方法
方案1:用「历史县」类替代历史区域类
直接针对已撤销的县的专属类wd:Q294420(历史县)进行查询,精准匹配需求:
SELECT DISTINCT ?county ?countyLabel ?stateLabel ?page_titleEN ?coordinate_location ?inception ?dissolved ?role WHERE { ?county (wdt:P31/(wdt:P279*)) wd:Q294420. OPTIONAL { ?county wdt:P131 ?state. } OPTIONAL { ?article schema:about ?county; schema:isPartOf <https://en.wikipedia.org/>; schema:name ?page_titleEN. } OPTIONAL { ?county wdt:P625 ?coordinate_location. } OPTIONAL { ?county wdt:P571 ?inception. } OPTIONAL { ?inception wdt:p2868 ?role. } OPTIONAL { ?county wdt:P576 ?dissolved.} SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } ORDER BY (?countyLabel)
方案2:保留县类+过滤撤销日期
如果需要覆盖其他县等效的已撤销行政区,可保留Q47168的条件,同时通过P576(撤销日期)筛选已撤销实体:
SELECT DISTINCT ?county ?countyLabel ?stateLabel ?page_titleEN ?coordinate_location ?inception ?dissolved ?role WHERE { ?county (wdt:P31/(wdt:P279*)) wd:Q47168; wdt:P576 ?dissolved. OPTIONAL { ?county wdt:P131 ?state. } OPTIONAL { ?article schema:about ?county; schema:isPartOf <https://en.wikipedia.org/>; schema:name ?page_titleEN. } OPTIONAL { ?county wdt:P625 ?coordinate_location. } OPTIONAL { ?county wdt:P571 ?inception. } OPTIONAL { ?inception wdt:p2868 ?role. } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } ORDER BY (?countyLabel)
方案3:调整历史区域的匹配逻辑
若一定要保留Q1620908的条件,可调整为检查实体是否属于历史区域的实例(含间接关联):
SELECT DISTINCT ?county ?countyLabel ?stateLabel ?page_titleEN ?coordinate_location ?inception ?dissolved ?role WHERE { ?county (wdt:P31/(wdt:P279*)) wd:Q47168; (wdt:P31|wdt:P279*) wd:Q1620908. OPTIONAL { ?county wdt:P131 ?state. } OPTIONAL { ?article schema:about ?county; schema:isPartOf <https://en.wikipedia.org/>; schema:name ?page_titleEN. } OPTIONAL { ?county wdt:P625 ?coordinate_location. } OPTIONAL { ?county wdt:P571 ?inception. } OPTIONAL { ?inception wdt:p2868 ?role. } OPTIONAL { ?county wdt:P576 ?dissolved.} SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } ORDER BY (?countyLabel)
内容的提问来源于stack exchange,提问作者Jeff Meyer
相关产品推荐
相关产品推荐

