So I have a Notation3 file that looks like this:
@prefix wn: <http://www.w3.org/2006/03/wn/wn20/instances/> .
@prefix lemon: <http://www.monnet-project.eu/#> .
@prefix lexinfo: <http://www.lexinfo.net/ontology#> .
:lexicon a lemon:Lexicon ;
lemon:language "it" ;
lemon:entry :fifa.
:fifa a lemon:LexicalEntry ;
lemon:canonicalForm [ lemon:writtenRep "fifa"@it ] ;
lemon:sense [ lemon:reference wn:synset-fear-noun-1 ];
lexinfo:partOfSpeech lexinfo:noun .
And I am trying to enter it into a RDFlib Graph and Serialize it as a RDFxml file using this code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import rdflib
from rdflib import URIRef, Graph, Namespace
from rdflib.plugins.parsers.notation3 import N3Parser
g = Graph()
result = g.parse(file=open("lemon_example_fear.txt", "r"), format="text/n3")
print (g.serialize(format='xml'))
but at the moment I am getting this error:
rdflib.plugins.parsers.notation3.BadSyntax: at line 5 of <>:
Bad syntax (Prefix ":" not bound) at ^ in:
"... lexinfo: <http://www.lexinfo.net/ontology/2.0/lexinfo#> .
^:lexicon a lemon:Lexicon ;
lemon:language "it" ;
lem..."
Is this a problem with the file I am trying to serialize or the code I am using to do it with?
I get the error both with RDFLib 3 and 4 so, as I initially suspected, it is not just a regression bug introduced in RDFLib 4.
Taking a closer look into your data I feel like there is something fishy going on.
Take the first set of statements for instance:
:lexicon a lemon:Lexicon ;
lemon:language "it" ;
lemon:entry :fifa,
:timore.
In terms of RDF, what you are saying here is that you want to define subject referenced by unprefixed property called :lexicon
and assign some more unprefixed properties into it (e.g. :fifa
and :timore
).
Given that you have not defined how the RDF parser should handle unbound prefixes, each parser makes it own decision on how to proceed. For instance W3C RDF Validation assigns unprefixed properties with a generated namespace (for instance #lexicon
-> http://www.w3.org/RDF/Validator/run/1373638767868#lexicon
). The problem here is that this is something that is left for the parser to decide. In your case the parser decides to throw you an error, which IMHO is very acceptable, it not recommended behaviour.
The simplest workaround I can think of is to explicitly define prefix for unprefixed properties in the beginning of your N3 file as follows:
@prefix : <#> .
You can also add prefix programmatically into your graph with RDFLib.
Edit: as noted in the other answer, N3 spec gives clear definition on how undefined, empty prefixes should be handled so this could be in fact regarded as a bug in RDFLib.
Follow-up (2013-07-28): This issue has been reported to RDFLib maintainers, investigated and is likely caused by a bug. See the issue tracker for details: https://github.com/RDFLib/rdflib/issues/312