首页 > 解决方案 > 当过滤属性为空时,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

标签: sparqlwikidata

解决方案


您为什么不将所有与该类型有关的内容放在同一个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

我还清理了代码。


推荐阅读