我有一个skos概念的层次结构和一些由这些概念索引的个体(例如ex:Regina dct:subject ex:Pizza
)。
我想找到每个被一些概念(或更具体的概念)索引的人。下面的查询检索每顿比萨饼或自制餐。 (我正在使用concat
和group_concat
轻松获取一行中的膳食概念)
select ?ind ?name (group_concat(concat(str(?concept),':',?conceptName)) as ?concepts){
?ind a ex:Individual.
?ind skos:prefLabel ?name.
?ind dct:subject ?concept.
?concept skos:prefLabel ?conceptName.
optional{?ind dct:subject [skos:broaderTransitive* ex:Pizza].}
optional{?ind dct:subject [skos:broaderTransitive* ex:Homemade].}
}
group by ?ind ?name
这很好,但现在我想根据他们与我想要的概念数量对这些结果进行排名。所以在我的例子中,我首先想要自制的比萨饼,然后只有比萨饼或自制的饭菜 我也想摆脱没有我感兴趣的概念的结果 理想情况下,我还希望直接用所需概念编制索引的膳食得分高于更具体概念索引的膳食。
我当然可以对结果进行后处理,但我很确定在sparql中有一种聪明有效的方法。有什么想法吗?
编辑:根据请求提供样本数据
@prefix : <http://www.semanticweb.org/owl/owlapi/turtle#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix ex: <http://example.com#> .
ex:FoodScheme a skos:ConceptScheme ;
skos:prefLabel "The food schema"@en ;
skos:hasTopConcept ex:Food .
ex:Food a skos:Concept ;
skos:prefLabel "Food"@en ;
skos:inScheme ex:FoodScheme .
ex:Italian a skos:Concept ;
skos:prefLabel "Italian food"@en ;
skos:broaderTransitive ex:Food ;
skos:inScheme ex:FoodScheme .
ex:Pizza a skos:Concept ;
skos:prefLabel "Pizza"@en ;
skos:broaderTransitive ex:Italian ;
skos:inScheme ex:FoodScheme .
ex:Pasta a skos:Concept ;
skos:prefLabel "Pasta"@en ;
skos:broaderTransitive ex:italian ;
skos:inScheme ex:FoodScheme .
ex:Homemade a skos:Concept ;
skos:prefLabel "Homemade food"@en ;
skos:broaderTransitive ex:Food ;
skos:inScheme ex:FoodScheme .
ex:Regina a ex:Meal ;
skos:prefLabel "Regina"@en ;
dct:subject ex:Homemade ;
dct:subject ex:Pizza.
ex:PeppyPaneer a ex:Meal ;
skos:prefLabel "From Domino's"@en ;
dct:subject ex:Pizza.
ex:Carbonara a ex:Meal ;
skos:prefLabel "Pasta a la carbonara"@en ;
dct:subject ex:Homemade ;
dct:subject ex:Pasta.
ex:Lasagna a ex:Meal ;
skos:prefLabel "Lasagna"@en ;
dct:subject ex:Italian.
自制披萨的预期结果(里贾纳首先,因为它是自制的和披萨,不包括carbonara因为它既不是自制也不是披萨.PeppyPaneer和Carbonara的顺序无关紧要,它们应该具有相同的“得分”):
ex:Regina
ex:PeppyPaneer
ex:Carbonara
意大利人的预期结果(首先是Lasagna,因为它直接被索引为意大利语,而其他人则没有):
ex:Lasagna
ex:Carbonara
ex:PeppyPaneer
ex:Regina
答案 0 :(得分:3)
鉴于一组相关主题{ ex:Pizza ex:Homemade }
,我们可以这样做:
PREFIX ex: <http://example.com#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?ind ?name (GROUP_CONCAT(DISTINCT ?conceptStr) AS ?concepts) (COUNT(DISTINCT ?cls) AS ?score)
WHERE
{ ?ind skos:prefLabel ?name ;
dct:subject ?concept .
?concept skos:prefLabel ?conceptName
BIND(concat(str(?concept), ":", ?conceptName) AS ?conceptStr)
OPTIONAL
{ VALUES ?cls { ex:Pizza ex:Homemade }
?ind dct:subject/(skos:broaderTransitive)* ?cls
}
}
GROUP BY ?ind ?name
HAVING ( ?score > 0 )
ORDER BY DESC(?score)
+-----------------+-------------------------+--------------------------------------------------------------------------+-------+
| ind | name | concepts | score |
+-----------------+-------------------------+--------------------------------------------------------------------------+-------+
| ex:Regina | Regina en | http://example.com#Pizza:Pizza http://example.com#Homemade:Homemade food | 2 |
| ex:Carbonara | Pasta a la carbonara en | http://example.com#Pasta:Pasta http://example.com#Homemade:Homemade food | 1 |
| ex:PeppyPaneer | From Domino's en | http://example.com#Pizza:Pizza | 1 |
+-----------------+-------------------------+--------------------------------------------------------------------------+-------+