【发布时间】:2021-07-22 05:34:13
【问题描述】:
我正在构建一个 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
【问题讨论】: