0

I want somehow in python in RDF graph with RDFlib, to filter only the predicates that make 1:n relation between subjects and objects.

Could anyone help me with this?

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
Saeid Hedayati
  • 83
  • 1
  • 3
  • 12
  • According to the documentation, rdflib allows you to query with SPARQL. This query might be nontrivial, I'm not sure, what you mean by "predicates that make 1:n relation between subject and object". Please, provide some input data and the output expected. – Stanislav Kralin May 17 '17 at 20:36
  • Unfortunately, I can not understand, what you are looking for… Could you paste into your question 10-12 triples of input data and 3-4 IRIs of output desired? – Stanislav Kralin May 18 '17 at 11:57
  • 1
    If I correctly understood your question, you can do so by performing a SPRQL request. RDFLib is now supporting SPARQL. Do you now what is SPARQL and how to use it ? – raeX May 18 '17 at 18:04
  • @StanislavKralin sorry to bother u again :) , is it possible to write a query in SPARQL to only filter the predicates that have 1:1 relation. i mean a subject with a certain predicate only lead to a single object and vice a versa ? – Saeid Hedayati May 23 '17 at 20:19
  • 1
    i wrote sth like this and i'm not sure whether it's correct or not """SELECT DISTINCT ?predicate WHERE { ?subject1 ?predicate ?object1 . ?subject2 ?predicate ?object2 . FILTER(?subject1 = ?subject2 && ?object1 = ?object2 ) }""") – Saeid Hedayati May 23 '17 at 20:23
  • Your variant is correct. But I think, its performance might be lower on large datasets in RDFlib (I can't test). – Stanislav Kralin May 24 '17 at 04:56

1 Answers1

1

Let's consider the following example:

@prefix ex: <http:/example.com/ontology/>
@prefix owl: <http://www.w3.org/2002/07/owl#>

ex:man   ex:hasWife    ex:woman
ex:man   ex:hasName    "Jean"
ex:man   ex:hasName    "Claude"
ex:man   ex:hasChild   ex:boy
ex:man   ex:hasChild   ex:girl
ex:woman ex:hasHusband ex:man
ex:boy   ex:hasParent  ex:woman
ex:girl  ex:hasParent  ex:woman
ex:man   ex:hasWife    ex:wife
ex:woman owl:sameAs    ex:wife

As well as I understand, the output should be:

ex:hasName
ex:hasWife
ex:hasChild

In order to get this output, one should execute the following SPARQL query:

SELECT DISTINCT ?predicate
WHERE {
   ?subject ?predicate ?object1 .
   ?subject ?predicate ?object2 .
   FILTER (?object1 != ?object2)
}

Using RDFLib, you can do it in this way:

import rdflib g = rdflib.Graph()
g.parse("your_file.xml")
qres = g.query(
    """SELECT DISTINCT ?predicate
       WHERE {
           ?subject ?predicate ?object1 .
           ?subject ?predicate ?object2 .
           FILTER (?object1 != ?object2)
       }""")

for row in qres:
    print("%s" % row)
Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58