2

我是语义网和 SPARQL 的新手。我正在制作音乐会的 RDFS。它有歌曲条目,如:

con:song04
    rdfs:Class con:Repertoire;
    rdfs:label "Deewani Mastani";
    con:performedBy con:artist02.
    con:performedBy con:artist05.

如果它们由同一位艺术家表演,我正在尝试获取歌曲列表及其数量。我一直在尝试一些来自 stackoverflow 的教程,但并没有达到我的目的。

我使用的查询可能完全错误,如果有人可以在这方面帮助我,这对学习很有帮助。我正在使用Apache Jena Fuseki进行查询。

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX co: <http://rhizomik.net/ontologies/copyrightonto.owl#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
prefix con: <http://www.example.com/paras/concert#>

#Use SPARQL to count and list the songs that two artists share.
SELECT ?songs (COUNT(?artist) AS ?count)  
WHERE
{ 
  ?s rdfs:label ?songs .
  ?s rdf:type con:Repertoire.
  ?s con:performedBy ?artist.
  ?artist rdf:type con:Artist .
  filter( ?count > 1 )
}
4

2 回答 2

2
SELECT ?songs (COUNT(?artist) AS ?count)  
WHERE
{ 
  ?s rdfs:label ?songs .
  ?s rdfs:Class con:Repertoire.
  ?s con:performedBy ?artist.
  ?artist rdfs:Class con:Artist .
  filter( ?count > 1 )
}

如果您将该查询粘贴到sparql.org上的验证器中,您会发现当您使用 COUNT 等聚合函数时,选择未 GROUPed BY 的变量是错误的。您的查询几乎是正确的。您正在尝试按歌曲分组,并计算有多少艺术家创作了这首歌。然后,您只想获取计数大于 1 的结果。您可以这样做:

SELECT ?songs (COUNT(?artist) AS ?count)  
WHERE
{ 
  ?s rdfs:label ?songs .
  ?s rdfs:Class con:Repertoire.
  ?s con:performedBy ?artist.
  ?artist rdfs:Class con:Artist .
}
GROUP BY ?songs
HAVING (?count > 1 )
于 2015-11-23T19:55:48.550 回答
1

根据给出的描述,我完全同意@Joshua Taylor 的观点,并相信你想要做的事情可以通过查询来实现。请检查您的 RDFS 是否正常。

SELECT ?songs (COUNT(?artist) AS ?count)  
WHERE
{ 
  ?s rdfs:label ?songs .
  ?s rdfs:Class con:Repertoire.
  ?s con:performedBy ?artist.
  ?artist rdfs:Class con:Artist .
}
GROUP BY ?songs
HAVING (?count > 1 )
于 2015-11-26T12:38:13.593 回答