当过滤 属性 为空时,SPARQL 正则表达式过滤器导致 'no results'

SPARQL regex filter causing 'no results' when filtered property is empty

我正在构建一个维基数据 SPARQL,以根据电影的 IMDb ID 获取有关电影的信息。我想使用正则表达式过滤器过滤流派结果,我几乎在每个实例中都使用它,除非电影没有列出流派。

这是我的代码(包含的 IMDb 代码 'tt8332922' 适用于 'A Quiet Place: Part II',它没有列出类型。如果您将它换成具有列出类型的电影的另一个 IMDb 代码,查询会return个结果)

我尝试将其包装在 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

我也清理了代码。