Python - rdflib - json-ld - 为什么它不解析嵌套的三元组

问题描述 投票:0回答:1

我有以下代码,解析下面的 JSON-ld 文件。但是,它只输出顶级三元组,而不输出其他三元组,例如

name
WebPage

from rdflib import Dataset
from rdflib.namespace import Namespace, NamespaceManager

local_input="""
[
    {
        "@context": "https://schema.org/"
    },
    {
        "@type": "WebPage",
        "@id": "https://example.com/glossary/term/",
        "name": "My Glossary Term",
        "abstract": "Just my glossary Term.",
        "datePublished": "2024-03-08T07:52:13+02:00",
        "dateModified": "2024-03-08T14:54:13+02:00",
        "url": "https://example.com/glossary/term/",
        "author": {
            "@type": "Person",
            "@id": "https://example.com/",
            "name": "John Doe"
        },
        "about": [
            {
                "@type": "DefinedTerm",
                "@id": "https://example.com/glossary/term/#definedTerm"
            }
        ]
    },
    {
        "@type": "DefinedTerm",
        "@id": "https://example.com/glossary/term/#definedTerm",
        "name": "My Term",
        "description": "Just my Term",
        "inDefinedTermSet": {
            "@type": "DefinedTermSet",
            "@id": "https://example.com/glossary/#definedTermSet"
        }
    }
]
"""

SCH = Namespace('https://schema.org/')
namespace_manager = NamespaceManager(Dataset(), bind_namespaces='none')
namespace_manager.bind('', SCH, override=True)
g = Dataset()
g.namespace_manager = namespace_manager
g.parse(data=local_input, format='json-ld', publicID="http://schema.org/")

print(len(g))

import pprint
for stmt in g:
    pprint.pprint(stmt)

输出:

2
(rdflib.term.URIRef('https://example.com/glossary/term/'),
 rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
 rdflib.term.URIRef('http://schema.org/WebPage'),
 rdflib.term.URIRef('urn:x-rdflib:default'))
(rdflib.term.URIRef('https://example.com/glossary/term/#definedTerm'),
 rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
 rdflib.term.URIRef('http://schema.org/DefinedTerm'),
 rdflib.term.URIRef('urn:x-rdflib:default'))
python json-ld rdflib
1个回答
0
投票

你的JSON-LD输入数据不太正确,试试这个:

{
  "@context": {
    "@vocab": "https://schema.org/"
  },
  "@graph": [
    {
      "@id": "https://example.com/glossary/#definedTermSet",
      "@type": "DefinedTermSet"
    },
    {
      "@id": "https://example.com/",
      "@type": "Person",
      "name": "John Doe"
    },
    {
      "@id": "https://example.com/glossary/term/",
      "@type": "WebPage",
      "about": {
        "@id": "https://example.com/glossary/term/#definedTerm"
      },
      "abstract": "Just my glossary Term",
      "author": {
        "@id": "https://example.com/"
      },
      "dateModified": "2024-03-08T14:54:13+02:00",
      "datePublished": "2024-03-08T07:52:13+02:00",
      "name": "My Glossary Term",
      "url": "https://example.com/glossary/term/"
    },
    {
      "@id": "https://example.com/glossary/term/#definedTerm",
      "@type": "DefinedTerm",
      "inDefinedTermSet": {
        "@id": "https://example.com/glossary/#definedTermSet"
      },
      "name": "My Term"
    }
  ]
}

您可以加载它,它将打印出您尝试在数据中创建的所有三元组。

© www.soinside.com 2019 - 2024. All rights reserved.