简体   繁体   中英

How to count the number of distinct values, their names and the number of times each appeared per data property SPARQL

I m trying to make a SPARQL query that returns the number of distinct values of each data property of a Turtle file. I would like to know what the name of each value is and how many time each were repeated. I have created a simple ontology to test:

@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
          )
  ] .

And this is the SPARQL query I m using:

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

and here is the results (The counts does not make sense to me) and I m sure there is something wrong with my query:

    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

What I am looking for:

property     value     noOfDistinctValues 
------------------------------------------
staffID      89387         1               
first_name   John          3                
last_name    Doe           2                
staffID      234           1                
last_name    Coles         1                
staffID      7658          1                

I m not even sure what is count that its being returned. I m also new to Ontology and SPARQL

I appreciate your help greatly

Thanks to @AKSW I was able to solve my problem. This worked:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM