【发布时间】:2020-01-17 21:34:52
【问题描述】:
我正在尝试进行 SPARQL 查询,该查询返回 Turtle 文件的每个数据属性的不同值的数量。我想知道每个值的名称是什么以及每个值重复了多少次。我创建了一个简单的本体来测试:
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix uni: <http://www.example.com/university#> .
@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#> .
@base <http://www.example.com/university> .
<http://www.example.com/university> rdf:type owl:Ontology .
#################################################################
# Classes
#################################################################
### http://www.example.com/university#Lecturer
:Lecturer rdf:type owl:Class ;
rdfs:subClassOf :Person .
### http://www.example.com/university#Person
:Person rdf:type owl:Class .
#################################################################
# Individuals
#################################################################
### http://www.example.com/university#Lecturer1
:Lecturer1 rdf:type owl:NamedIndividual ,
:Lecturer ;
:first_name "John"^^xsd:string ;
:last_name "Coles"^^xsd:string ;
:staffID "234"^^xsd:int .
### http://www.example.com/university#Lecturer2
:Lecturer2 rdf:type owl:NamedIndividual ,
:Lecturer ;
:first_name "John"^^xsd:string ;
:last_name "Doe"^^xsd:string ;
:staffID "89387"^^xsd:int .
### http://www.example.com/university#lecturer3
:lecturer3 rdf:type owl:NamedIndividual ,
:Lecturer ;
:first_name "John"^^xsd:string ;
:last_name "Doe"^^xsd:string ;
:staffID "7658"^^xsd:int .
#################################################################
# General axioms
#################################################################
[ rdf:type owl:AllDisjointClasses ;
owl:members (
:Lecturer
)
] .
这是我正在使用的 SPARQL 查询:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX uni: <http://www.example.com/university#>
select distinct ?ind ?property ?value (count(?value) as ?noOfDistinctValues) where {
?ind rdf:type uni:Lecturer .
?ind ?property ?value .
?property a owl:DatatypeProperty
}
group by ?ind ?property ?value
这是结果(计数对我来说没有意义),我确定我的查询有问题:
ind property value noOfDistinctValues
------------------------------------------------------------
lecturer2 staffID 89387 6
lecturer2 first_name John 8
lecturer2 last_name Doe 8
lecturer1 staffID 234 6
lecturer1 first_name John 8
lecturer1 last_name Coles 8
lecturer3 staffID 7658 6
lecturer3 first_name John 8
lecturer3 last_name Doe 8
我在寻找什么:
property value noOfDistinctValues
------------------------------------------
staffID 89387 1
first_name John 3
last_name Doe 2
staffID 234 1
last_name Coles 1
staffID 7658 1
我什至不确定它被退回的数量是多少。我也是 Ontology 和 SPARQL
的新手非常感谢您的帮助
【问题讨论】:
-
首先,从
group by和select中删除?ind和?value。 -
如果我删除
?value,数字会更加混乱,我需要看看值是什么。我可以删除 ?ind 虽然 -
您的问题有点误导...您想要每个属性的每个值的出现次数?或者每个属性的不同值的总数?两者显然是不同的东西。你需要子查询
-
例如,要获取每个属性每个值的出现次数,您必须计算
?ind当然:select ?property ?value (count(?ind) as ?noOfValueOccurrences) where { ?ind rdf:type uni:Lecturer . ?ind ?property ?value . ?property a owl:DatatypeProperty } group by ?property ?value -
每个属性的不同值的数量:
select ?property (count(distinct ?value) as ?noOfTotalValuesForProperty) where { ?ind rdf:type uni:Lecturer . ?ind ?property ?value . ?property a owl:DatatypeProperty } group by ?property
标签: sparql owl ontology protege turtle-rdf