gpt4 book ai didi

python - RDFLib 中的命名空间绑定(bind)

转载 作者:行者123 更新时间:2023-12-04 15:07:34 25 4
gpt4 key购买 nike

在以下最小测试用例中:

from rdflib import Graph, Namespace, Literal, RDF

base = "http://test.com/ns"
foobar = Namespace("http://test.com/ns#")
g = Graph(base=base)
g.bind('foobar', foobar)

g.add((foobar.something, RDF.type, Literal('Blah')))
g.add((foobar.something, foobar.contains, Literal('a property')))

g.add((foobar.anotherthing, RDF.type, Literal('Blubb')))
g.add((foobar.anotherthing, foobar.contains, Literal('another property')))

print(g.serialize(format='turtle').decode("utf-8"))

我明白了

@base <http://test.com/ns> .
@prefix foobar: <http://test.com/ns#> .

<#anotherthing> a "Blubb" ;
ns1:contains "another property" .

ns1:something a "Blah" ;
ns1:contains "a property" .

我更期待的是

@base <http://test.com/ns> .
@prefix foobar: <http://test.com/ns#> .

<#anotherthing> a "Blubb" ;
foobar:contains "another property" .

<#something> a "Blah" ;
foobar:contains "a property" .

所以要么是我根本不了解 RDFLib 以及如何使用 namespace ,要么是发生了一些奇怪的事情。
有什么想法吗?

最佳答案

您的 base 声明还需要最后的 has 或斜杠。

所以尝试 g = Graph(base="http://test.com/ns#") 一切都会正常工作

在这种情况下尝试不使用任何基础。

两者都会给你有效的结果。是的,在 RDFlib 的深处有一些奇怪的事情发生在基础上,但只有当它不是以 # 或/结束时。

顺便说一句,你的三元组有点无效:你不能将 rdf:type 与文字的范围值一起使用,它必须是 rdf:Class实例。

关于python - RDFLib 中的命名空间绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65818401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com