我必须运行一个循环,该循环使用rdflib中的SPARQL查询检索rdf本体的所有类的所有邻居节点的标签。我知道如何上课:
classes=graph.query("""select ?nodes WHERE {
?nodes rdf:type owl:Class
} limit 100""")
我知道如何获取单个URI的邻居节点:
example = graph.query("""select ?othernodes ?othernodesLabel where {
bind (<http://human.owl#NCI_C12736> as ?mynode )
?mynode ?y ?othernodes.
?othernodes rdfs:label ?othernodesLabel
} limit 100""")
但我需要的是将它们放在一个for循环中,该循环将一个对象中每个类的相邻节点的所有标签分组,这样每个类都有一个包含所有相邻标签的列表节点。
答案 0 :(得分:1)
如果我理解你的问题,最简单的解决方案是将两个查询结合起来并通过Python代码处理标签。查询将是
SELECT ?node ?othernodes ?othernodesLabel WHERE {
?node rdf:type owl:Class .
?node ?y ?othernodes .
?othernodes rdfs:label ?othernodesLabel
}
LIMIT 100
在您的代码中,您可以按每个OWL类处理返回的数据。实际上,您仍然必须遍历结果集,因此,它类似于第一个查询结果的循环,并为每个类执行第二个查询。