DependencyGraph's tree(), TypeError: 'NoneType' object has no attribute '__getitem__'

171 views
Skip to first unread message

silo

unread,
Oct 28, 2015, 11:08:30 AM10/28/15
to nltk-users
Hi, a beginner here,

import nltk
from nltk.parse import DependencyGraph
from collections import defaultdict

def from_sentence(sent):
    tokens
= nltk.word_tokenize(sent)
    tagged
= nltk.pos_tag(tokens)
   
    dg
= DependencyGraph()
   
for (index, (word, tag)) in enumerate(tagged):
        dg
.nodes[index + 1] = {
           
'word': word,
           
'lemma': '_',
           
'ctag': tag,
           
'tag': tag,
           
'feats': '_',
           
'rel': '_',
           
'deps': defaultdict(),
           
'head': '_',
           
'address': index + 1,
       
}
    dg
.connect_graph()
   
return dg

dg
= from_sentence("Natural Language Processing is really fun.")
tree
= dg.tree()
tree
.pprint()


Error,
Traceback (most recent call last):
...........
 tree
= dg.tree()
 
File "C:\Python27\lib\site-packages\nltk\parse\dependencygraph.py", line 340, in tree
    word
= node['word']
TypeError: 'NoneType' object has no attribute '__getitem__'

 
The same error happens with,
for val in dg.triples():
   
print val

Why the error happened?
"None" is not handled correctly? Or, am I missing something?
 
Thank you very much in advance.
silo

Steven Bird

unread,
Oct 28, 2015, 11:24:13 AM10/28/15
to nltk-users
This looks like a bug, sorry. If you `print(dg)` you'll see that there is an additional "TOP" node that has a word attribute with the value `None`. I've logged it here:



--
You received this message because you are subscribed to the Google Groups "nltk-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nltk-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexis Dimitriadis

unread,
Oct 28, 2015, 12:58:53 PM10/28/15
to nltk-...@googlegroups.com
It's not about the `word` attribute: `node` itself is None at that point. The problem is that `dg.root` is None when dg.tree() is called:

329  -> node = self.root
330  
331          word = node['word']

Alexis


Dr. Alexis Dimitriadis | Assistant Professor and Senior Research Fellow | Utrecht Institute of Linguistics OTS | Utrecht University | Trans 10, 3512 JK Utrecht, room 2.33 | +31 30 253 65 68 | a.dimi...@uu.nl | www.hum.uu.nl/medewerkers/a.dimitriadis

Dmitrijs Milajevs

unread,
Nov 1, 2015, 4:20:07 PM11/1/15
to nltk-users
Hi,

The problem is a bit deeper than it is. Basically, DependencyGraph calls a root node a node that depends on the fake TOP node. In an empty graph there is only TOP node, so there can't be a root.

You can try this code:


>>> import nltk
>>> from nltk.parse import DependencyGraph

>>> def from_sentence(sent):
...     tokens = nltk.word_tokenize(sent)
...     tagged = nltk.pos_tag(tokens)
...
...     records = (
...         ' '.join([word, tag, '0']) for word, tag in tagged
...     )
...     dg = DependencyGraph(records)
...     return dg
...

>>> dg = from_sentence("Natural Language Processing is really fun.")
>>> dg.tree().pprint()
(Natural )

The reason you see only Natural, is that some parts of the code assume that there is only one ROOT node (the node that depends on the TOP), while in this case there are many of them. This is a long known problem and we are working on it, I'll put more information regarding the issue on github.

However, there is information for all the nodes, if you check dg.nodes (reading dg.nodes should be OK, but changing them might break things).

--
Dima
Reply all
Reply to author
Forward
0 new messages