SPARQL:select 项并计算其标签的出现次数
SPAQRL: select item and count occurences of its label
我已 this SPARQL query 转到开放研究知识图谱 (ORKG):
PREFIX orkgr: <http://orkg.org/orkg/resource/>
PREFIX orkgc: <http://orkg.org/orkg/class/>
PREFIX orkgp: <http://orkg.org/orkg/predicate/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?o1Label (COUNT(?o1Label) AS ?o1LabelCount)
WHERE {
?o1 a orkgc:Paper.
?o1 rdfs:label ?o1Label.
FILTER (strlen(?o1Label) > 1).
}
GROUP BY ?o1Label
ORDER BY DESC(?o1LabelCount)
这导致标签 (?o1Label
) 和该标签的出现次数 (?o1LabelCount
)。
如何扩展此查询以包含实际项目 (?o1
) 的列?
因为可能有多个候选项(当 o1LabelCount
> 1 时),所以这些项目中的每一项都应该有一行(具有相同的标签和相同的标签计数)。
我看到两个选项:
首先(可能更好)是使用 GROUP_CONCAT
and collect the entities into one field to be parsed again on application side. this could look like this (link):
PREFIX orkgr: <http://orkg.org/orkg/resource/>
PREFIX orkgc: <http://orkg.org/orkg/class/>
PREFIX orkgp: <http://orkg.org/orkg/predicate/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?o1Label (GROUP_CONCAT(?o1, "\t") AS ?o1s) (COUNT(?o1Label) AS ?o1LabelCount)
WHERE {
?o1 a orkgc:Paper.
?o1 rdfs:label ?o1Label.
FILTER (strlen(?o1Label) > 1).
}
GROUP BY ?o1Label
ORDER BY DESC(?o1LabelCount)
另一种方法是使用 nested queries and receive a result as you described (link):
PREFIX orkgr: <http://orkg.org/orkg/resource/>
PREFIX orkgc: <http://orkg.org/orkg/class/>
PREFIX orkgp: <http://orkg.org/orkg/predicate/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?o1Label ?o1 ?o1LabelCount
WHERE {
?o1 rdfs:label ?o1Label .
{
SELECT ?o1Label (COUNT(?o1Label) AS ?o1LabelCount)
WHERE {
[
a orkgc:Paper;
rdfs:label ?o1Label
]
FILTER (strlen(?o1Label) > 1).
}
}
}
GROUP BY ?o1Label
ORDER BY DESC(?o1LabelCount)
我已 this SPARQL query 转到开放研究知识图谱 (ORKG):
PREFIX orkgr: <http://orkg.org/orkg/resource/>
PREFIX orkgc: <http://orkg.org/orkg/class/>
PREFIX orkgp: <http://orkg.org/orkg/predicate/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?o1Label (COUNT(?o1Label) AS ?o1LabelCount)
WHERE {
?o1 a orkgc:Paper.
?o1 rdfs:label ?o1Label.
FILTER (strlen(?o1Label) > 1).
}
GROUP BY ?o1Label
ORDER BY DESC(?o1LabelCount)
这导致标签 (?o1Label
) 和该标签的出现次数 (?o1LabelCount
)。
如何扩展此查询以包含实际项目 (?o1
) 的列?
因为可能有多个候选项(当 o1LabelCount
> 1 时),所以这些项目中的每一项都应该有一行(具有相同的标签和相同的标签计数)。
我看到两个选项:
首先(可能更好)是使用 GROUP_CONCAT
and collect the entities into one field to be parsed again on application side. this could look like this (link):
PREFIX orkgr: <http://orkg.org/orkg/resource/>
PREFIX orkgc: <http://orkg.org/orkg/class/>
PREFIX orkgp: <http://orkg.org/orkg/predicate/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?o1Label (GROUP_CONCAT(?o1, "\t") AS ?o1s) (COUNT(?o1Label) AS ?o1LabelCount)
WHERE {
?o1 a orkgc:Paper.
?o1 rdfs:label ?o1Label.
FILTER (strlen(?o1Label) > 1).
}
GROUP BY ?o1Label
ORDER BY DESC(?o1LabelCount)
另一种方法是使用 nested queries and receive a result as you described (link):
PREFIX orkgr: <http://orkg.org/orkg/resource/>
PREFIX orkgc: <http://orkg.org/orkg/class/>
PREFIX orkgp: <http://orkg.org/orkg/predicate/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?o1Label ?o1 ?o1LabelCount
WHERE {
?o1 rdfs:label ?o1Label .
{
SELECT ?o1Label (COUNT(?o1Label) AS ?o1LabelCount)
WHERE {
[
a orkgc:Paper;
rdfs:label ?o1Label
]
FILTER (strlen(?o1Label) > 1).
}
}
}
GROUP BY ?o1Label
ORDER BY DESC(?o1LabelCount)