【问题标题】:SPARQL regex filter causing 'no results' when filtered property is empty当过滤属性为空时,SPARQL 正则表达式过滤器导致“无结果”
【发布时间】: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

【问题讨论】:

    标签: sparql wikidata


    【解决方案1】:

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

    我还清理了代码。

    【讨论】:

    • 谢谢!我试图将它们全部放在同一个可选块中,但我是新手,不知道我可以换掉SERVICE wikibase:label { bd:serviceParam wikibase:language "en" 并将&& lang(?genreLabel) = "en") 放入过滤器中。这很好用。太感谢了。以及清理它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多