8

我正在尝试以Turtle格式编码一些植物数据,并使用RDFLib从 Python 读取这些数据。但是,我遇到了麻烦,我不确定是因为我的 Turtle 格式不正确还是我在滥用RDFLib。

我的测试数据是:

@PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@PREFIX p: <http://www.myplantdomain.com/plant/description> .
p:description a rdfs:Property .
p:name a rdfs:Property .
p:language a rdfs:Property .
p:value a rdfs:Property .
p:gender a rdfs:Property .
p:inforescence a rdfs:Property .
p:color a rdfs:Property .
p:sense a rdfs:Property .
p:type a rdfs:Property .
p:fruit a rdfs:Property .
p:flower a rdfs:Property .
p:dataSource a rdfs:Property .
p:degree a rdfs:Property .
p:date a rdfs:Property .
p:person a rdfs:Property .
p:c2a7b9a3-c54a-41f5-a3b2-155351b3590f
    p:description [
        p:name [
            p:kingdom "Plantae" ;
            p:division "Pinophyta" ;
            p:class "Pinopsida" ;
            p:order "Pinales" ;
            p:family "Pinaceae" ;
            p:genus "Abies" ;
            p:species "A. alba" ;
            p:language "latin" ;
            p:given_by [
                p:person p:source/Philip_Miller ;
                p:start_date "1923-1-2"^^<http://www.w3.org/2001/XMLSchema#date>
            ]
        ] ;
        p:name [
            p:language "english" ;
            p:value "silver fir"
        ] ;
        p:flower [
            p:gender "male"@en ;
            p:inflorescence "catkin"@en ;
            p:color "brown"@en ;
            p:color "yellow"@en ;
            p:sense "straight"@en
        ] ;
        p:flower [
            p:gender "female"@en ;
            p:inflorescence "catkin"@en ;
            p:color "pink"@en ;
            p:color "yellow"@en ;
            p:sense "straight"@en
        ] ;
        p:fruit [
            p:type "cone"@en ;
            p:color "brown"@en
        ]
    ] .

我的 Python 是:

import rdflib
g = rdflib.Graph()
#result = g.parse('trees.ttl') 
#result = g.parse('trees.ttl', format='ttl')
result = g.parse('trees.ttl', format='n3')
print len(g)
for stmt in g:
    print stmt

这给了我错误:

ValueError: Found @PREFIX when expecting a http://www.w3.org/2000/10/swap/grammar/n3#document . todoStack=[['http://www.w3.org/2000/10/swap/grammar/n3#document', []]]

我试过改变 parse() 参数,但一切都给了我一个错误。我几乎没有找到关于如何解析 Turtle 的示例。我究竟做错了什么?

4

1 回答 1

10

我认为第一个问题是大写字母 PREFIX——如果你把它们小写,它就会超过那个点。不确定这是 rdflib 还是 Turtle 中的错误.ttl,但Turtle Validator在线演示似乎同意这是一个问题.ttl(说Validation failed: The @PREFIX directive is not supported, line 1 col 0.但是如果你将它们小写,这个问题就会消失)。

一旦你越过了这个障碍,两个解析器都不喜欢周围的部分p:given_by [:“Bad syntax (']' expected) at ^ in:”... per rdflib; 海龟验证器说

Validation failed: Expecting a period, semicolon, comma, close-bracket, or close-brace but found '/', line 31 col 33.

所以它特别不喜欢这个p:source/Philip_Miller部分。

从这两个问题(谁知道是否还有其他问题......!)我认为您可以得出结论,这个 N3 源(.ttl您发布的文件)已损坏,并将您的注意力转向首先制作此文件的系统,并且为什么它以如此多的破坏方式制造它。

于 2010-08-24T14:58:12.623 回答