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

Wikidata/SPARQL,在结果中获取不同的项目对象 ID

如何解决Wikidata/SPARQL,在结果中获取不同的项目对象 ID

当我运行以下查询时,我得到了多行“Paul Gauguin”,因为关于他的死亡地点/时间有多种信息,同样的情况当然也可能发生在所有其他参数上。

   SELECT disTINCT ?item ?itemLabel ?itemDescription ?birthplaceLabel ?birthdate ?deathplaceLabel ?deathdate ?imageLabel ?article ?articleEn
{
  ?item wdt:P31 wd:Q5.
  ?item wdt:P119 wd:Q5024152.
  
  OPTIONAL {
    ?item wdt:P18 ?image.       
  }
  OPTIONAL {
    ?item wdt:P19 ?birthplace.
  }
  OPTIONAL {
    ?item wdt:P569 ?birthdate.    
  }
  OPTIONAL {
    ?item wdt:P20 ?deathplace.    
  }
  OPTIONAL {
    ?item wdt:P570 ?deathdate.
  }
  OPTIONAL {
     ?article schema:about ?item.
     ?article schema:isPartOf <https://sv.wikipedia.org/>.
  }
   OPTIONAL {
     ?articleEn schema:about ?item.
     ?articleEn schema:isPartOf <https://en.wikipedia.org/>.
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "sv,en,[AUTO_LANGUAGE]". }
}

有没有办法只接收一个相同的对象 id,而不关心是否有其他“版本”的对象。

我尝试了一些嵌套查询,但我无法让它工作。还有其他方法吗?

解决方法

此查询将起作用:

SELECT DISTINCT ?item ?itemLabel ?itemDescription 
(SAMPLE(?birthplaceLabel) AS ?birthplaceLabel)
(SAMPLE(?birthdate) AS ?birthdate)
(SAMPLE(?deathplaceLabel) AS ?deathplaceLabel)
(SAMPLE(?deathdate) AS ?deathdate)
(SAMPLE(STR(?image)) AS ?image)
?article ?articleEn

WHERE {
  ?item wdt:P31 wd:Q5.
  ?item wdt:P119 wd:Q5024152.
  
  OPTIONAL {
    ?item wdt:P18 ?image.       
  }
  OPTIONAL {
    ?item wdt:P19 ?birthplace.
  }
  OPTIONAL {
    ?item wdt:P569 ?birthdate.    
  }
  OPTIONAL {
    ?item wdt:P20 ?deathplace .    
  }
  OPTIONAL {
    ?item wdt:P570 ?deathdate.
  }
  OPTIONAL {
     ?article schema:about ?item.
     ?article schema:isPartOf <https://sv.wikipedia.org/>.
  }
   OPTIONAL {
     ?articleEn schema:about ?item.
     ?articleEn schema:isPartOf <https://en.wikipedia.org/>.
  }
  SERVICE wikibase:label {  ?birthplace rdfs:label ?birthplaceLabel . 
                          ?deathplace rdfs:label ?deathplaceLabel .
                          ?item rdfs:label ?itemLabel ;
                                schema:description ?itemDescription .
                          bd:serviceParam wikibase:language "sv,en,[AUTO_LANGUAGE]". }
}
GROUP BY ?item ?itemLabel ?itemDescription ?article ?articleEn

如您所见,我们使用 SAMPLE 函数并按所有未采样的变量进行分组。 如果您希望查看属性采用的所有可能值,也可以将 SAMPLE 替换为 GROUP_CONCAT。 例如。 ... (GROUP_CONCAT(?deathdate; SEPARATOR="; ") AS ?deathdates) ...

请注意,GROUP_CONCAT 将字符串作为参数。

,

对 Valerio Cocchis 的答案稍加修改,找到了解决方案。

SELECT ?item ?itemLabel ?itemDescription 
(SAMPLE(?birthplaceLabel) AS ?birthplaceLabel)
(SAMPLE(?birthdate) AS ?birthdate)
(SAMPLE(?deathplaceLabel) AS ?deathplaceLabel)
(SAMPLE(?deathdate) AS ?deathdate)
(SAMPLE(?imageLabel) AS ?imageLabel)
?article ?articleEn

WHERE {
  ?item wdt:P31 wd:Q5.
  ?item wdt:P119 wd:Q5024152.
  
  OPTIONAL {
    ?item wdt:P18 ?image.       
  }
  OPTIONAL {
    ?item wdt:P19 ?birthplace.
  }
  OPTIONAL {
    ?item wdt:P569 ?birthdate.    
  }
  OPTIONAL {
    ?item wdt:P20 ?deathplace.    
  }
  OPTIONAL {
    ?item wdt:P570 ?deathdate.
  }
  OPTIONAL {
     ?article schema:about ?item.
     ?article schema:isPartOf <https://sv.wikipedia.org/>.
  }
   OPTIONAL {
     ?articleEn schema:about ?item.
     ?articleEn schema:isPartOf <https://en.wikipedia.org/>.
  }
  SERVICE wikibase:label { 
    bd:serviceParam wikibase:language "sv,[AUTO_LANGUAGE]". 
    ?birthplace rdfs:label ?birthplaceLabel . 
    ?deathplace rdfs:label ?deathplaceLabel .
    ?image rdfs:label ?imageLabel .
    ?item rdfs:label ?itemLabel .
    ?item schema:description ?itemDescription .
  }
}
GROUP BY ?item ?itemLabel ?itemDescription ?article ?articleEn

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 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”。这是什么意思?