python – 如何查找rdf对象知道主题或其他方式?
作者:互联网
我正在使用RDFLIB在带有ntriples的3个数据集(A,B,C)之间构建图形.
目标:图表包含那些数据集A-> B,B-> C和C-> A之间的链接,我想通过确保从A发出的链接引用回相同的条目来检查这些链接的一致性在一个.
问题:一旦我迭代A-> B中的链接,我想在B-> C中查找相应的条目(可能多于一个),并且对于C-> A,查找相同的条目,是否存在通过了解主题而不迭代所有条目来查找对象的方法?
解决方法:
is there a way to look up objects by knowing the subject without iterating over all entries?
答案是肯定的.您可以使用不同的机制:(a)使用限制进行迭代;或(b)发出SPARQL查询.
(a) constrain the graph and iterate
此解决方案在Graph对象上使用RDFLib三元组函数.见this reference.
#Parse the file
g = rdflib.Graph()
g.parse("yourdata.nquads")
subject = article = rdflib.term.URIRef("http://www.someuri.org/for/your/subject")
# (subject,None,None) represents a constrain to iterate over the graph. By setting
# any of the three elements in the triple you constrain by any combination of subject,
# predicate or object. In this case we only constrain by subject.
for triple in g.triples((subject,None,None)):
print triple
(b) issue a SPARQL query
使用SPARQL standard的更标准的解决方案.
rdflib.plugin.register('sparql', rdflib.query.Processor,
'rdfextras.sparql.processor', 'Processor')
rdflib.plugin.register('sparql', rdflib.query.Result,
'rdfextras.sparql.query', 'SPARQLQueryResult')
#Parse the file
g = rdflib.Graph()
g.parse("yourdata.nquads")
query = """
SELECT ?pred ?obj WHERE {
<http://www.someuri.org/for/your/subject> ?pred ?obj
}
"""
for row in g.query(query):
print "Predicate:%s Object:%s"%(row[0],row[1])
标签:python,rdf,rdflib 来源: https://codeday.me/bug/20190621/1255754.html