如何计算不同值的数量、它们的名称以及每个数据出现的次数 属性 SPARQL
How to count the number of distinct values, their names and the number of times each appeared per data property SPARQL
我正在尝试进行 SPARQL 查询,returns Turtle 文件的每个数据 属性 的不同值的数量。我想知道每个值的名称是什么以及每个值重复了多少次。我创建了一个简单的 ontology 来测试:
@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
的新手
非常感谢你的帮助
感谢@AKSW,我能够解决我的问题。这有效:
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 ?property (str(?value) as ?valueLiteral) (str(count(distinct ?ind)) as
?noOfValueOccurrences)
where { ?ind rdf:type uni:Lecturer.
?ind ?property ?value.
?property a owl:DatatypeProperty .}
group by ?property ?value
order by ?property
我正在尝试进行 SPARQL 查询,returns Turtle 文件的每个数据 属性 的不同值的数量。我想知道每个值的名称是什么以及每个值重复了多少次。我创建了一个简单的 ontology 来测试:
@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
的新手非常感谢你的帮助
感谢@AKSW,我能够解决我的问题。这有效:
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 ?property (str(?value) as ?valueLiteral) (str(count(distinct ?ind)) as
?noOfValueOccurrences)
where { ?ind rdf:type uni:Lecturer.
?ind ?property ?value.
?property a owl:DatatypeProperty .}
group by ?property ?value
order by ?property