【问题标题】:SPARQL : Find Difference and Common Elements in 2 SetsSPARQL:在 2 个集合中查找差异和共同元素
【发布时间】:2014-11-16 08:38:33
【问题描述】:

我是 SPARQL 的新手,正在努力实现这两个目标,需要您的帮助。我有 2 个类,分别存储了 python 和 CPP 的关键字。现在我打算从这两组中找到常见的关键字以及两者之间的区别(意思是,关键字存在于python中但不存在于CPP中,反之亦然)。我已经尝试过 MINUS 和 NOT EXISTS 版本的查询,但没有任何帮助。 为了找出 2 组的差异,我尝试了以下查询:

SELECT ?subject
    WHERE 
    { ?subject a python:Keywords. 
    { FILTER NOT EXISTS {?subject a cpp:Keywords} }
    }

为了找到 2 个集合中的共同元素,我尝试了以下查询:

select ?subject
where{ ?subject a python:Keywords. FILTER  EXISTS { ?object a cpp:Keywords}
}

它们都没有工作。请帮忙

【问题讨论】:

  • SELECT ?subject WHERE { ?subject a python:Keywords. } 是否真的返回了您期望它返回的内容?同样对于cpp:Keywords?这些是相应的关键字吗?
  • 是的。它分别返回了来自 python 和 cpp 的所有关键字的列表。现在我需要找出这两组之间的共同点和不同点。

标签: sql rdf sparql rdfs


【解决方案1】:
SELECT ?subject
  WHERE 
  { ?subject a python:Keywords .
    ?subject a cpp:Keywords .
  }

应该给你常用的关键字。

SELECT ?subject
  WHERE 
  { ?subject a python:Keywords. 
    FILTER NOT EXISTS {?subject a cpp:Keywords}
  }

应该返回python关键字,不是cpp关键字。

【讨论】:

  • 第一个查询更简洁:?subject a python:Keywords, cpp:Keywords.
【解决方案2】:

代码中的问题

在您的第一个查询中,问题在于您已将过滤器放在另一组大括号内。也就是说,你有:

SELECT ?subject
    WHERE 
    { ?subject a python:Keywords. 
    { FILTER NOT EXISTS {?subject a cpp:Keywords} }
    }

但应该有:

SELECT ?subject
    WHERE 
    { ?subject a python:Keywords. 
      FILTER NOT EXISTS {?subject a cpp:Keywords}
    }

区别很重要,因为第一个是过滤封闭模式的匹配项,这实际上没有任何效果。

解决办法

之后,Abecee's answer 就是您所需要的。交集只是两种类型的东西:

?x a :type1, :type2 

区别在于一种类型的东西,而不是另一种类型:

?x a :type1
filter not exists { ?x a :type2 }

类似的问题和答案

【讨论】:

    猜你喜欢
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多