SPARQL计算多个图中的出现次数

时间:2013-01-21 16:30:41

标签: rdf sparql

我正在尝试编写一个SPARQL查询来计算多个图中对象的出现次数。以下示例数据和预期输出:

命名图g1:

@prefix g1: <http://example.com/g1#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
g1:ex1 rdfs:label "a" .
g1:ex2 rdfs:label "a" .
g1:ex3 rdfs:label "b" .
g1:ex3 rdfs:label "d" .

命名图g2:

@prefix g2: <http://example.com/g2#> .    
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
g2:ex1 rdfs:label "a" .
g2:ex2 rdfs:label "b" .
g2:ex3 rdfs:label "c" .

SPARQL查询的预期输出:

?label ?g1count ?g2count
a      2        1
b      1        1
c      0        1
d      1        0

我可以通过rdfs:标签和计算出现的联合来获得两个图的总计数:

prefix g1: <http://example.com/g1#>
prefix g2: <http://example.com/g2#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?label count(?label) as ?count
{
  {
    GRAPH g1: {
      ?s rdfs:label ?label
    }
  } UNION {
    GRAPH g2:
    {
      ?s rdfs:label ?label
    }
  }
}

我认为从这里我可以使用每个UNION块中的子查询来获取个别计数,但除了这种查询可能效率低下之外,我还没有获得预期结果的运气。

2 个答案:

答案 0 :(得分:4)

来自RobV的回答(对于评论来说太大了):

prefix g1: <http://example.com/g1#>
prefix g2: <http://example.com/g2#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?label (count(?s1) as ?g1count) (count(?s2) AS ?g2count)
{
  {
    GRAPH g1: {
      ?s1 rdfs:label ?label
    }
  } UNION {
    GRAPH g2: {
      ?s2 rdfs:label ?label
    }
  }
} group by ?label order by ?label

结果:

---------------------
| label | g1c | g2c |
=====================
| "a"   | 2   | 1   |
| "b"   | 1   | 1   |
| "c"   | 0   | 1   |
| "d"   | 1   | 0   |
---------------------

答案 1 :(得分:2)

您可以利用COUNT函数忽略未绑定值并仅为您的变量赋予不同名称的事实,即

prefix g1: <http://example.com/g1#>
prefix g2: <http://example.com/g2#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT (COALESCE(?label, ?label2) AS ?label) (count(?label1) as ?g1count) (count(?label2) AS ?g2count)
{
  {
    GRAPH g1: {
      ?s rdfs:label ?label1
    }
  } UNION {
    GRAPH g2:
    {
      ?s rdfs:label ?label2
    }
  }
}

COALESCE函数用于将实际值与标签组合成单个变量,因为COALESCE从参数返回第一个非空值