rdf - Count resources having a property of a certain type in SPARQL -
i have data in triple store , compute following:
how many resources 'x' have object property 'op' @ least 2 different resources 'r' of similar type 'r' values?
here example of such data in turtle syntax:
prefix ex: <http://example.com> ex:doc1 ex:document ; ex:mentions p1, p2, p3 . ex:doc2 ex:document ; ex:mentions p4, p5 . ex:p1 ex:person ; ex:hasrole ex:r1 . ex:p2 ex:person ; ex:hasrole ex:r1 . ex:p3 ex:person ; ex:hasrole ex:r2 . ex:p4 ex:person ; ex:hasrole ex:r1 . ex:p5 ex:person ; ex:hasrole ex:r2 . ex:r1 ex:role1 . ex:r2 ex:role2 . the objective count resources such ex:doc1 has 2 ex:mentions having similar roles (r1 of type ex:role1). here result 1, leaving aside ex:doc2`.
the strategy be:
identify resources having desired property, i.e. documents (doc) having object properties (mentions) pointing on resources (person), these resources having properties (hasrole) of similar values (the role)).
count them.
i have difficulties step 1. example, query returns docs having p1 role1, if there 1 p (p1) having property.
select distinct ?doc { ?doc ex:document . ?doc ex:mentions ?p1 . ?doc ex:mentions ?p2 . ?p1 ex:hasrole ?r1 . ?p2 ex:hasrole ?r1 . ?r1 ex:role1 . } many help.
your data wasn't quite usable (there no prefixes on p1, p2, etc., resources), after fixing that, able use following query. pretty close; trick need filter(?p1 != ?p2) ensure you're getting different values of ex:mentions property. can check have role common type ?p1 ex:hasrole/a ?roletype . ?p2 ex:hasrole/a ?roletype, or more concisely, ?roletype ^(a/ex:hasrole) ?p1, ?p2. then, in counting, want count distinct values of ?document, need (count(distinct ?document) ?ndocuments):
prefix ex: <http://example.com> select #-- count ?document, count *distinct* values #-- of ?document. (count(distinct ?document) ?ndocuments) { #-- documents have 2 distinct #-- values ex:mentions property ?document ex:document ; ex:mentions ?p1, ?p2 filter(?p1 != ?p2) #-- check have common role type ?roletype ^(a/ex:hasrole) ?p1, ?p2 } -------------- | ndocuments | ============== | 2 | --------------
Comments
Post a Comment