gpt4 book ai didi

sparql - 如何删除sparql查询中的重复项

转载 作者:行者123 更新时间:2023-12-02 07:03:07 25 4
gpt4 key购买 nike

我编写了此查询并返回情侣列表和特定条件。 (在 http://live.dbpedia.org/sparql 中)

SELECT DISTINCT ?actor ?person2 ?cnt
WHERE
{
{
select DISTINCT ?actor ?person2 (count (?film) as ?cnt)
where {
?film dbo:starring ?actor .
?actor dbo:spouse ?person2.
?film dbo:starring ?person2.
}
order by ?actor
}
FILTER (?cnt >9)
}

问题是某些行是重复的。 示例:

http://dbpedia.org/resource/George_Burns http://dbpedia.org/resource/Gracie_Allen 12

http://dbpedia.org/resource/Gracie_Allen http://dbpedia.org/resource/George_Burns 12

如何删除这些重复项?我向 ?actor 添加了性别,但它损害了当前结果。

最佳答案

Natan Cox's answer显示了排除此类伪重复项的典型方法。结果实际上并不是重复的,因为在一个结果中,例如,乔治·伯恩斯是“ Actor ”,而在另一个结果中,他是“人物2”。在许多情况下,您可以添加一个过滤器来要求对这两件事进行排序,这将删除重复的情况。例如,当您有如下数据时:

:a :likes :b .
:a :likes :c .

然后您搜索

select ?x ?y where { 
:a :likes ?x, ?y .
}

您可以添加 filter(?x < ?y) 来强制 ?x 和 ?y 之间的排序,这将删除这些伪重复项。然而,在这种情况下,这有点棘手,因为没有使用相同的条件找到 ?actor 和 ?person2 。如果 DBpedia 包含

:PersonB dbo:spouse :PersonA

但不是

:PersonA dbo:spouse :PersonB

那么简单的过滤器将不起作用,因为您永远找不到主语 PersonA 小于宾语 PersonB 的三元组。因此,在这种情况下,您还需要稍微修改查询以使条件对称:

select distinct ?actor ?spouse (count(?film) as ?count) {
?film dbo:starring ?actor, ?spouse .
?actor dbo:spouse|^dbo:spouse ?spouse .
filter(?actor < ?spouse)
}
group by ?actor ?spouse
having (count(?film) > 9)
order by ?actor

(此查询还表明您在这里不需要子查询,您可以使用 having 来“过滤”聚合值。)但重要的部分是使用属性路径 dbo:spouse|^dbo:spouse 查找 ?spouse 的值,使得或者 ?actor dbo:spouse ?spouse 或者 ?配偶 dbo:配偶? Actor 。这使得关系对称,因此即使仅在一个方向声明关系,您也能保证获得所有对。

关于sparql - 如何删除sparql查询中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36348328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com