sparql - 当过滤属性为空时,SPARQL 正则表达式过滤器导致“无结果”
问题描述
我正在构建一个 Wikidata SPARQL,以根据其 IMDb ID 获取有关电影的信息。我想使用正则表达式过滤器过滤类型结果,并且我让它几乎适用于所有实例,除非电影没有列出类型。
这是我的代码(包含的 IMDb 代码“tt8332922”用于“安静的地方:第二部分”,没有列出流派。如果将其换成具有列出流派的电影的另一个 IMDb 代码,查询将返回结果)
我尝试将其包装在 中OPTIONAL
,但过滤器根本不起作用。我还尝试将过滤器嵌套在可选{}
的初始?genre
定义中,但我无法获取用于抓取英语语言标签的语法。
SELECT ?filmLabel (group_concat(DISTINCT ?basedongroup;separator=", ") as ?basedon) (group_concat(DISTINCT ?genrefixed;separator=", ") as ?genres) (group_concat(DISTINCT ?countryLabel;separator=", ") as ?countries) (group_concat(DISTINCT ?narrativelocaleLabel;separator=", ") as ?Locales) (group_concat(DISTINCT ?setinperiodLabel;separator=", ") as ?Period) ?ratingLabel ?wppage ?metacritic ?aspectratioLabel ?wikidataid
WHERE
{
?film wdt:P345 'tt8332922' .
OPTIONAL { ?wppage schema:about ?film .
FILTER(contains(str(?wppage),'//en.wikipedia')) .}
{bind (REPLACE(STR(?film),"http://www.wikidata.org/entity/","") AS ?wikidataid)}
OPTIONAL { ?film wdt:P144 ?based.
?basedwp schema:about ?based .
FILTER(contains(str(?basedwp),'//en.wikipedia')) . }
OPTIONAL { ?film wdt:P136 ?genre. }
OPTIONAL { ?film wdt:P495 ?country. }
OPTIONAL { ?film wdt:P840 ?narrativelocale. }
OPTIONAL { ?film wdt:P2061 ?aspectratio.}
OPTIONAL { ?film wdt:P1657 ?rating. }
OPTIONAL { ?film wdt:P1712 ?metacritic.}
OPTIONAL { ?film wdt:P2408 ?setinperiod.}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]".
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en".
?based rdfs:label ?basedLabel .
?country rdfs:label ?countryLabel .
?genre rdfs:label ?genreLabel .
?narrativelocale rdfs:label ?narrativelocaleLabel .
?setinperiod rdfs:label ?setinperiodLabel .
?based schema:description ?basedDescription .
}
filter(!regex(str(?genreLabel),'(based on|flashback)')) .
{bind (REPLACE(STR(?genreLabel),"film","") AS ?genrefixed)}
{bind (concat(?basedLabel, " - ", ?basedDescription, " || ('", str(?basedwp), "')") as ?basedongroup)}
}
GROUP BY ?filmLabel ?wppage ?ratingLabel ?metacritic ?aspectratioLabel ?wikidataid
解决方案
您为什么不将所有与该类型有关的内容放在同一个OPTIONAL
块中?
SELECT
?filmLabel
(group_concat(DISTINCT ?basedongroup;separator=", ") as ?basedon)
(group_concat(DISTINCT ?genrefixed;separator=", ") as ?genres)
(group_concat(DISTINCT ?countryLabel;separator=", ") as ?countries)
(group_concat(DISTINCT ?narrativelocaleLabel;separator=", ") as ?Locales)
(group_concat(DISTINCT ?setinperiodLabel;separator=", ") as ?Period)
?ratingLabel ?wppage ?metacritic ?aspectratioLabel ?wikidataid
WHERE
{
?film wdt:P345 'tt8332922' .
OPTIONAL {
?wppage schema:about ?film .
FILTER(contains(str(?wppage),'//en.wikipedia')) . }
BIND(REPLACE(STR(?film),"http://www.wikidata.org/entity/","") AS ?wikidataid)
OPTIONAL {
?film wdt:P144 ?based.
?basedwp schema:about ?based .
FILTER(contains(str(?basedwp),'//en.wikipedia')) . }
OPTIONAL {
?film wdt:P136 ?genre .
?genre rdfs:label ?genreLabel .
FILTER(!REGEX(str(?genreLabel),'(based on|flashback)') && lang(?genreLabel) = "en") .
BIND(REPLACE(STR(?genreLabel),"film","") AS ?genrefixed) }
OPTIONAL { ?film wdt:P495 ?country . }
OPTIONAL { ?film wdt:P840 ?narrativelocale . }
OPTIONAL { ?film wdt:P2061 ?aspectratio . }
OPTIONAL { ?film wdt:P1657 ?rating . }
OPTIONAL { ?film wdt:P1712 ?metacritic . }
OPTIONAL { ?film wdt:P2408 ?setinperiod . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]" . }
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en".
?based rdfs:label ?basedLabel .
?country rdfs:label ?countryLabel .
?narrativelocale rdfs:label ?narrativelocaleLabel .
?setinperiod rdfs:label ?setinperiodLabel .
?based schema:description ?basedDescription . }
BIND(CONCAT(?basedLabel, " - ", ?basedDescription, " || ('", str(?basedwp), "')") as ?basedongroup)
}
GROUP BY ?filmLabel ?wppage ?ratingLabel ?metacritic ?aspectratioLabel ?wikidataid
我还清理了代码。
推荐阅读
- matlab - 使用 dsolve 绘制解曲线时遇到问题
- javascript - 如何在Javascript中获取第n个表列值
- pandas - 熊猫数据框中的匹配和重新排列值
- web - 网站如何切实可行地允许用户个人资料拥有自己的 URL?
- java - 使用带有 Spring boot Transaction 的 RxJava 1.1.5 不会回滚
- php - 使用 codeigniter 中的取消链接删除图像的权限被拒绝
- java - 如何将此 java lambda 代码重构为只有 1 个 lambda
- python - Pandas - 如果字段为 NaT (Null),则填写其他列
- sockets - 错误“已建立的连接被主机中的软件中止”
- r - 因子错误(newCases[, a], levels = 1:nlevels(data[, a]), labels = levels(data[, : invalid 'labels'; 长度 0 应该是 1 或 2