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?
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?
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)