Sparql 查找具有 dbo 类型的实体并按计数对它们进行排序
Sparql find entities with dbo type and sort them by count
我必须对 dbpedia 端点执行 sparql 查询,需要:
- 查找在
label
中包含"vienna"和在abstract
中包含"city"的所有实体
- 过滤它们,只保留至少有一个
dbo
rdf:type
- 按
dbo
类型的计数对结果进行排序(例如,如果一个实体有 5 dbo
rdf:type
它必须显示在具有 4 dbo
[= 的实体之前14=])
我做了几次尝试,最接近结果的是:
select distinct (str(?s) as ?s) count(?t) as ?total where {{ ?s rdfs:label "vienna"@en. ?s rdf:type ?t.}
UNION { ?s rdfs:label ?l. ?s rdf:type ?t . ?l <bif:contains> '("vienna")'
. FILTER EXISTS { ?s dbo:abstract ?cc. ?cc <bif:contains> '("city")'. FILTER(lang(?cc) = "en").}}
FILTER (!strstarts(str(?s), str("http://dbpedia.org/resource/Category:")))
. FILTER (!strstarts(str(?s), str("http://dbpedia.org/property/")))
. FILTER (!strstarts(str(?s), str("http://dbpedia.org/ontology/")))
. FILTER (strstarts(str(?t), str("http://dbpedia.org/ontology/"))).}
LIMIT 50
这将(错误地)在实际过滤之前计算 rdf:type
。我不想计算 rdf:type
不是 dbo
(ontology).
想法是使用一个子查询,您可以在其中搜索实体并在外部查询中进行计数:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?s (count(*) AS ?cnt)
WHERE
{ { SELECT DISTINCT ?s
WHERE
{ ?s rdfs:label ?l .
?l <bif:contains> '"vienna"'
FILTER langMatches(lang(?l), "en")
FILTER EXISTS { ?s dbo:abstract ?cc .
?cc <bif:contains> '"city"'
FILTER langMatches(lang(?cc), "en")
}
?s rdf:type ?t
FILTER ( ! strstarts(str(?s), str("http://dbpedia.org/resource/Category:")) )
FILTER ( ! strstarts(str(?s), str("http://dbpedia.org/property/")) )
FILTER ( ! strstarts(str(?s), str(dbo:)) )
FILTER strstarts(str(?t), str(dbo:))
}
}
?s ?p ?o
FILTER strstarts(str(?p), str(dbo:))
}
GROUP BY ?s
ORDER BY DESC(?cnt)
我必须对 dbpedia 端点执行 sparql 查询,需要:
- 查找在
label
中包含"vienna"和在abstract
中包含"city"的所有实体
- 过滤它们,只保留至少有一个
dbo
rdf:type
- 按
dbo
类型的计数对结果进行排序(例如,如果一个实体有 5dbo
rdf:type
它必须显示在具有 4dbo
[= 的实体之前14=])
我做了几次尝试,最接近结果的是:
select distinct (str(?s) as ?s) count(?t) as ?total where {{ ?s rdfs:label "vienna"@en. ?s rdf:type ?t.}
UNION { ?s rdfs:label ?l. ?s rdf:type ?t . ?l <bif:contains> '("vienna")'
. FILTER EXISTS { ?s dbo:abstract ?cc. ?cc <bif:contains> '("city")'. FILTER(lang(?cc) = "en").}}
FILTER (!strstarts(str(?s), str("http://dbpedia.org/resource/Category:")))
. FILTER (!strstarts(str(?s), str("http://dbpedia.org/property/")))
. FILTER (!strstarts(str(?s), str("http://dbpedia.org/ontology/")))
. FILTER (strstarts(str(?t), str("http://dbpedia.org/ontology/"))).}
LIMIT 50
这将(错误地)在实际过滤之前计算 rdf:type
。我不想计算 rdf:type
不是 dbo
(ontology).
想法是使用一个子查询,您可以在其中搜索实体并在外部查询中进行计数:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?s (count(*) AS ?cnt)
WHERE
{ { SELECT DISTINCT ?s
WHERE
{ ?s rdfs:label ?l .
?l <bif:contains> '"vienna"'
FILTER langMatches(lang(?l), "en")
FILTER EXISTS { ?s dbo:abstract ?cc .
?cc <bif:contains> '"city"'
FILTER langMatches(lang(?cc), "en")
}
?s rdf:type ?t
FILTER ( ! strstarts(str(?s), str("http://dbpedia.org/resource/Category:")) )
FILTER ( ! strstarts(str(?s), str("http://dbpedia.org/property/")) )
FILTER ( ! strstarts(str(?s), str(dbo:)) )
FILTER strstarts(str(?t), str(dbo:))
}
}
?s ?p ?o
FILTER strstarts(str(?p), str(dbo:))
}
GROUP BY ?s
ORDER BY DESC(?cnt)