I am currently trying to use variables to create a graph of N triples, I am not having any trouble assigning the variables, but i keep getting an error message. Here is the code:

from rdflib import Namespace, URIRef, Graph
from StringIO import StringIO


xmlns = "http://www.example.org/lexicon#"
rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
lemon = "http://www.monnetproject.eu/lemon#"
graph = Graph()
F = open("new_2.txt", "r")
for line in F:

This is the part where I assign the variables. This is pretty long and I know it works so I wont include this. This following code is still part of the for line in F:

line1 = ''+synset_offset+'\rdf.lex_filenum\ '+lex_filenum+''
    line2 = ''+synset_offset+'\lemon.ss_type\ '+ss_type+''
    line3 = ''
    for item in word: 
        line3 +=''+synset_offset+'\lemon.lexical_entry\ '+iw.next()+'/n'
    line4 = ''+synset_offset+'\lemon.gloss\ '+gloss+''
    line5 = ''
    line6 = ''
    line7 = ''
    for item in S:
        pointer = ip.next()
        pos = iss.next()
        source_target = ist.next()
        line5 += ''+synset_offset+'\lemon.has_ptr\ '+pointer+'/n'
        line6 += ''+pointer+'\lemon.pos\ '+pos+'/n'
        line7 += ''+pointer+'\lemon.source_target\ '+source_target+'/n'

    contents = '''\
    '''+line1+'''
    '''+line2+'''
    '''+line3+'''
    '''+line4+'''
    '''+line5+'''
    '''+line6+'''
    '''+line7+''''''  
    tabfile = StringIO(contents)
    for line in tabfile:
        triple = line.split()                # triple is now a list of 3 strings
        triple = (URIRef(t) for t in triple) # we have to wrap them in URIRef
        graph.add(triple)

print graph.serialize(format='nt')  

This is the other code i have that prints everything correctly, showing that it is not the variables that are not working.

print('''<http://example.org/#'''+synset_offset+'''> <http://www.monnetproject.eu/lemon#lex_filenum> "'''+lex_filenum+'''".
<http://example.org/#'''+synset_offset+'''> <http://www.monnetproject.eu/lemon#ss_type> "'''+ss_type+'''".
<http://example.org/#'''+synset_offset+'''> <http://www.monnetproject.eu/lemon#gloss> "'''+gloss+'''".''')
    for item in word:
        print('''<http://example.org/#'''+synset_offset+'''> <http://www.monnetproject.eu/lemon#lex_entry> "'''+iw.next()+'''".''')

    for item in S:
        pointer = ip.next()
        pos = iss.next()
        source_target = ist.next()
        print('''<http://example.org/#'''+synset_offset+'''> <http://www.monnetproject.eu/lemon#has_ptr> "'''+pointer+'''".
<http://example.org/#'''+pointer+'''> <http://www.monnetproject.eu/lemon#pos> "'''+pos+'''".
<http://example.org/#'''+pointer+'''> <http://www.monnetproject.eu/lemon#source_target> "'''+source_target+'''".''')
    print('''\n''')

Any better ideas than the way I have it done here would be very welcome

EDIT: Now i am getting this error:

graph.add(triple)
  File "/usr/lib/python2.7/site-packages/rdflib-4.1_dev-py2.7.egg/rdflib/graph.py", line 352, in add
    def add(self, (s, p, o)):
ValueError: need more than 2 values to unpack
有帮助吗?

解决方案

You try to concatenate a "method" with a str, which means iw.next is the "pointer" to the method and iw.next() would be its return value, which is what you want.

Being explicit:

line3 +=''+synset_offset+'\lemon.lexical_entry\ '+iw.next()+'/n'

Update (regarding the next error):

triple has to be a tuple with 3 elements, just like the function signature says:

add(self, (s, p, o))

Ignore self, since you're calling the instance method.

I'm pretty sure that triple has another type, so check that out (the easiest way is print triple in the for statement).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top