views:

31

answers:

1

I'm trying to encode some botanical data in Turtle format, and read this data from Python using RDFLib. However, I'm having trouble, and I'm not sure if it's because my Turtle is malformed or I'm misusing RDFLib.

My test data is:

@PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; .
@PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#&gt; .
@PREFIX p: <http://www.myplantdomain.com/plant/description&gt; .
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&gt;
            ]
        ] ;
        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
        ]
    ] .

And my Python is:

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

Which gives me the errors:

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', []]]

I've tried varying the parse() parameters, but everything gives me an error. I've found little to no examples on how to parse Turtle. What am I doing wrong?

+1  A: 

I think the first problem is w/the uppercase PREFIX-- if you lowercase those it gets past that point. Not sure if it's a bug in rdflib or in the Turtle .ttl, but the Turtle Validator online demo seems to agree it's a problem with the .ttl (says Validation failed: The @PREFIX directive is not supported, line 1 col 0. but that problem goes away if you lowercase them).

Once you're past that hurdle, neither parser likes the part around p:given_by [: "Bad syntax (']' expected) at ^ in:"... per rdflib; Turtle Validator says

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

so it specifically dislikes the p:source/Philip_Miller part.

From these two issues (who knows if there are others...!) I think you can conclude that this N3 source (the .ttl file you post) is broken, and turn your attention to whatever system made this file in the first place, and why it's making it in such a multiply broken way.

Alex Martelli
Thanks. Changing that line to <source/Philip_Miller> seems to fix the problem.
Chris S
@Chris, good to know, thanks!
Alex Martelli