RDFLib 中的命名空间绑定

Bal*_*uin 6 python rdf rdflib turtle-rdf

在以下最小测试用例中:

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"))
Run Code Online (Sandbox Code Playgroud)

我明白了

@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" .
Run Code Online (Sandbox Code Playgroud)

我期望的更像是

@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" .
Run Code Online (Sandbox Code Playgroud)

因此,要么我根本不理解 RDFLib 以及如何使用名称空间,要么发生了一些奇怪的事情。
有什么想法吗?

小智 2

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

所以尝试g = Graph(base="http://test.com/ns#")一切都会很好的

或者

在这种情况下尝试没有任何基础。

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

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