如何计算不同值的数量,它们的名称以及每个数据属性SPARQL各自出现的次数

问题描述 投票:2回答:1

我试图进行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                

我什至不知道它返回了什么。我也是OntologySPARQL

的新手

非常感谢您的帮助

sparql owl ontology protege turtle-rdf
1个回答
0
投票

感谢@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
© www.soinside.com 2019 - 2024. All rights reserved.