完整的说明性代码可在本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数据集中得到了两个命名图:
@graph
节中的所有内容李>这不是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
似乎适合此作业的工具是
@included
关键字,而不是@graph
。见the JSON-LD 1.1 spec。这对我很管用(p.S.在使用RDFLib之前,我必须将文档展平;请参见the GitHub issue。)
相关问题 更多 >
编程相关推荐