Reputation: 1262
I am trying to add triples to a Graph using python's rdflib package. The relations are provided as a list (a particular column in a dataframe)
sampleRelations = ['similarTo', 'brotherOf', 'capitalOf']
g = Graph()
# general relations
gen = Namespace('http://abcd.com/general#')
g.bind('gen',gen)
# Adding predefined relationships
g.add( (gen.relatedTo, RDFS.subClassOf, OWL.ObjectProperty) )
This works as usual. But, when iterating through a list:
for rel in sampleRelations:
g.add( ('gen.'+rel, RDFS.subClassOf, OWL.ObjectProperty) )
It throws an error : "Subject %s must be an rdflib term" % (s,).
relWithNamespace = gen+rel
print(relWithNamespace)
g.add( (relWithNamespace, RDFS.subClassOf, OWL.ObjectProperty) )
Error
AssertionError: Subject http://abcd.com/general#similarTo must be an rdflib term
I understand the problem. I'm looking for pointers which can circumvent this.
Upvotes: 0
Views: 3714
Reputation: 1262
RDF terms can be of BNode, URI Reference or a Literal
sampleRelations = ['similarTo', 'brotherOf', 'capitalOf'`]
g = Graph()
# general relations
gen = Namespace('http://abcd.com/general#')
g.bind('gen',gen)
# Adding predefined relationships
g.add( (gen.relatedTo, RDFS.subClassOf, OWL.ObjectProperty) )
for rel in sampleRelations :
rel = URIRef('http://abcd.com/general#' + rel)
g.add((rel, RDFS.subClassOf, OWL.ObjectProperty))
URI Reference can be also done using:
for rel in sampleRelations :
g.add((gen.term(rel), RDFS.subClassOf, OWL.ObjectProperty))
or
for rel in sampleRelations :
g.add((gen[rel], RDFS.subClassOf, OWL.ObjectProperty))
Upvotes: 2