首页 > 解决方案 > 如何在 Python 中使用 RDFlib 从字典列表(三元组)中制作多个 RDF 图?

问题描述

我所拥有的:需要使用 RDFlib 将其转换为 RDF 的 20k 字典(三元组)列表。

目标:对于每一个三元组,我想要一个新的 rdf Graph()。但是,当我遍历所有三元组时,方法 .add() 会将每个三元组累积到同一个图中。

问题:如何在每个循环中制作一个新图形,以便图形不会在每个循环中累积数据?也许有一种方法可以在每个循环中删除图表?

在实际代码中,我将每个 rdf 转换的三元组发布到一个服务中,这需要一个一个地完成,因为它有很多数据(20k 三元组)。对于每一个三元组,它都是它自己的出版物,所以我不能将它们全部放在同一张图中。

我的示例代码(简化):

import rdflib
from rdflib import Graph, FOAF, Literal, Namespace

list_dicts_triples = [{'subject': 'she', 'predicate': 'went', 'object': 'there'},
              {'subject': 'I', 'predicate': 'like', 'object': 'ice-cream'}]

graph = Graph()
EX = Namespace('http://example.org/')
rdf = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
graph.bind('ex', EX)

subject_rdf = rdf["subject"]
object_rdf = rdf["object"]
predicate_rdf = rdf['predicate']

for triple in list_dicts_triples:
    graph.add((subject_rdf, EX.TYPE, FOAF.Person)) #subject
    graph.add((predicate_rdf, rdflib.URIRef('http://example_predicates.com'), Literal(triple['predicate']))) #predicate
    graph.add((object_rdf, rdflib.URIRef('http://example_concepts.com'), Literal(triple['object']))) #object
    output = graph.serialize(format='ttl').decode('u8')
    print(output) #print rdf graph

当前输出:(如您所见,第一个字典的项目已添加到第二个 rdf 图('there','went'..)。

#graph 1
@prefix ex: <http://example.org/> .
@prefix ns1: <http://> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

rdf:object ns1:example_concepts.com "there" .
rdf:predicate ns1:example_predicates.com "went" .
rdf:subject ex:TYPE <http://xmlns.com/foaf/0.1/Person> .

#graph 2
@prefix ex: <http://example.org/> .
@prefix ns1: <http://> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

rdf:object ns1:example_concepts.com "ice-cream",
        "there" .
rdf:predicate ns1:example_predicates.com "like",
        "went" .
rdf:subject ex:TYPE <http://xmlns.com/foaf/0.1/Person> .


期望的输出:(这里,每个图代表列表的每个唯一字典/三元组。

#graph 1
@prefix ex: <http://example.org/> .
@prefix ns1: <http://> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

rdf:object ns1:example_concepts.com "there" .
rdf:predicate ns1:example_predicates.com "went" .
rdf:subject ex:TYPE <http://xmlns.com/foaf/0.1/Person> .

#graph 2
@prefix ex: <http://example.org/> .
@prefix ns1: <http://> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

rdf:object ns1:example_concepts.com "ice-cream".
rdf:predicate ns1:example_predicates.com "like".
rdf:subject ex:TYPE <http://xmlns.com/foaf/0.1/Person> .




标签: pythonlistfor-looprdfrdflib

解决方案


在上面的代码中,您不会为每个三元组创建一个新图表。请注意,声明在 for 循环之外。将其移入内部,它应该可以按照您的意愿工作。

import rdflib
from rdflib import Graph, FOAF, Literal, Namespace

list_dicts_triples = [{'subject': 'she', 'predicate': 'went', 'object': 'there'},
              {'subject': 'I', 'predicate': 'like', 'object': 'ice-cream'}]


EX = Namespace('http://example.org/')
rdf = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')


subject_rdf = rdf["subject"]
object_rdf = rdf["object"]
predicate_rdf = rdf['predicate']

for triple in list_dicts_triples:
    graph = Graph()
    graph.bind('ex', EX)
    graph.add((subject_rdf, EX.TYPE, FOAF.Person)) #subject
    graph.add((predicate_rdf, rdflib.URIRef('http://example_predicates.com'), Literal(triple['predicate']))) #predicate
    graph.add((object_rdf, rdflib.URIRef('http://example_concepts.com'), Literal(triple['object']))) #object
    output = graph.serialize(format='ttl').decode('u8')
    print(output) #print rdf graph

推荐阅读