You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Wikidata SPARQL获取艺人出生地/国籍及出生日期遇问题

解决SPARQL查询音乐艺人/乐队信息的消歧与批量查询问题

问题背景

我需要编写SPARQL查询,获取音乐艺人、乐队或其成员的出生地/国籍(任一可用即可)及出生日期。现有查询虽能正常运行,但存在两个问题:

  • 带消歧义后缀(如"(singer)"或"(musician)")的条目无法匹配到,手动枚举所有可能的后缀容易遗漏
  • 查询通过JavaScript构建时,目标名称(如示例中的"Queen")需要重复多次,不够简洁

最初的查询代码:

SELECT ?page ?birth ?locLabel WHERE{
  {VALUES ?page {"Queen"@en "Queen (band)"@en "Queen (singer)"@en "Queen (rapper)"@en}.
  ?sitelink schema:name ?page;
            schema:isPartOf <https://en.wikipedia.org/>;
            schema:about ?item.
  OPTIONAL{?item wdt:P27 ?loc. hint:Prior hint:rangeSafe true.}
  OPTIONAL{?item wdt:P495 ?loc. hint:Prior hint:rangeSafe true.}
  OPTIONAL{?item wdt:P569 ?birth. hint:Prior hint:rangeSafe true.}
  OPTIONAL{?item wdt:P527 [wdt:P569 ?birth]. hint:Prior hint:rangeSafe true.}}
  SERVICE wikibase:label{bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".}
}

尝试后的问题

根据建议使用wikibase:mwapi的EntitySearch服务后,新查询又出现两个问题:

  • 返回错误结果(例如搜索"Alan Walker"时返回"Alan Tyson")
  • 无法同时查询多个艺人/乐队,会产生大量API调用,不符合使用场景

尝试的查询代码:

SELECT ?item ?itemLabel ?birth ?locLabel WHERE {
  SERVICE wikibase:mwapi {
    bd:serviceParam wikibase:endpoint "www.wikidata.org";
                    wikibase:api "EntitySearch";
                    mwapi:search "Alan Walker";
                    mwapi:language "en".
    ?item wikibase:apiOutputItem mwapi:item. }
  FILTER(EXISTS {?item wdt:P31/wdt:P279? wd:Q215380 .} || EXISTS {?item wdt:P106/wdt:P279? wd:Q639669 .})
  OPTIONAL{?item wdt:P27 ?loc. hint:Prior hint:rangeSafe true.}
  OPTIONAL{?item wdt:P495 ?loc. hint:Prior hint:rangeSafe true.}
  OPTIONAL{?item wdt:P569 ?birth. hint:Prior hint:rangeSafe true.}
  OPTIONAL{?item wdt:P527 [wdt:P569 ?birth]. hint:Prior hint:rangeSafe true.}
  SERVICE wikibase:label{bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".}
}

优化后的解决方案

下面是更高效可靠的SPARQL查询,解决了消歧、批量查询和结果准确性问题:

SELECT ?item ?itemLabel ?birth ?locLabel WHERE {
  # 批量指定需要查询的艺人/乐队名称,JS构建时只需动态生成这部分
  VALUES ?searchName {"Queen" "Alan Walker"}
  
  # 匹配英文维基百科的站点链接
  ?sitelink schema:isPartOf <https://en.wikipedia.org/>;
            schema:about ?item;
            schema:name ?pageName.
  
  # 正则匹配:自动适配带括号消歧后缀的条目,无需手动枚举
  FILTER(REGEX(?pageName, CONCAT("^", ?searchName, "( \\(.*\\))?$"), "i"))
  
  # 严格过滤音乐相关实体:乐队(含子类)或音乐人职业(含子类)
  FILTER(EXISTS {?item wdt:P31/wdt:P279? wd:Q215380} || EXISTS {?item wdt:P106/wdt:P279? wd:Q639669})
  
  # 获取国籍(优先)或出生地
  OPTIONAL {?item wdt:P27 ?loc. hint:Prior hint:rangeSafe true.}
  OPTIONAL {?item wdt:P495 ?loc. hint:Prior hint:rangeSafe true.}
  
  # 获取艺人自身或乐队成员的出生日期
  OPTIONAL {?item wdt:P569 ?birth. hint:Prior hint:rangeSafe true.}
  OPTIONAL {?item wdt:P527 [wdt:P569 ?birth]. hint:Prior hint:rangeSafe true.}
  
  SERVICE wikibase:label {bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".}
}

方案优势

  1. 自动消歧匹配:通过正则表达式自动匹配目标名称开头、后面带任意括号消歧内容的条目,无需手动枚举所有可能的后缀
  2. 支持批量查询:通过VALUES ?searchName可以一次性指定多个艺人/乐队名称,一次查询完成所有请求,避免多次API调用
  3. 结果精准:保留实体类型过滤逻辑,确保只返回乐队或从事音乐人职业的实体,减少错误结果
  4. JS构建更简洁:目标名称只需要在VALUES中写一次,JS生成查询时只需动态替换这部分内容即可,无需重复编写

内容的提问来源于stack exchange,提问作者Hubbit200

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 03:50:48