微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我也清理了代码。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?