RDFLib+JSONLD+命名图:使用@graph时如何覆盖图名?

2024-06-07 08:50:12 发布

您现在位置:Python中文网/ 问答频道 /正文

性情

完整的说明性代码可在本Gist中找到

假设我们正在JSON-LD文档中描述一篇博客文章。除了文章本身的一些属性(类型和标签)之外,我们还希望为该文章添加一些语义数据。在本例中,我们使用@graph关键字定义了一个Robot类和一个Rover作为它的子类

{
    "@context": {
        "schema": "https://schema.org/",
        "blog": "https://blog.me/",
        "ex": "https://example.org/",
        "rdfs": "http://www.w3.org/2000/01/rdf-schema#"
    },
    "@id": "blog:JSONLD-and-named-graphs",
    "@type": "schema:blogPost",
    "rdfs:label": "JSON-LD and Named Graphs",
    "@graph": [
        {
            "@id": "ex:Robot",
            "@type": "rdfs:Class"
        },
        {
            "@id": "ex:Rover",
            "rdfs:subClassOf": {
                "@id": "ex:Robot"
            }
        }
    ]
}

使用Python rdflib,我们希望将所有这些导入指定为https://myblog.net/rdf/的命名图中,如下所示:

    ...
    graph = ConjunctiveGraph()

    serialized_document = json.dumps(JSONLD_DOCUMENT)

    graph.parse(
        data=serialized_document,
        format='json-ld',
        # All the semantic data about my blog is stored in a particular
        # named graph.
        publicID='https://myblog.net/rdf/',
    )

预期结果

所有数据都应该基于publicID参数在命名图https://myblog.net/rdf/下导入

实际结果

事实上,我们在RDF数据集中得到了两个命名图:

这不是bug,这是JSON-LD语义prescribes的内容{"@id": "badoom", "@graph": {...}}意味着我们已经描述了一个名为@graph并提供了它的名称@id

问题:

我们能否以某种方式重写JSON-LD语义并强制RDFLib将整个输入数据导入到由publicID参数指定的命名图中

软件版本:

Python 3.8.1
rdflib==5.0.0
rdflib-jsonld==0.5.0
PyLD==2.0.3

Tags: 数据httpsidjsonnetschema文章blog

热门问题