NLTK cookbook - ImportError: No module named chunkers?

686 views
Skip to first unread message

Daniel Wu

unread,
Aug 6, 2014, 1:33:20 PM8/6/14
to nltk-...@googlegroups.com


I've been following the NLTK cookbook on page 195 for Classifier Based Chunking, but I could not import chunkers.py.

I found chunkers.py (I think) on this link:

After copy/pasting it to my notebook, I still get the following message. I'd appreciate your help. I've posted the full code that I used from the book here: 


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-80-201b22386c9f> in <module>()
      1 chunker = ClassifierChunker(train_chunks)
----> 2 score = chunker.evaluate(test_chunks)
      3 score.accuracy()

//anaconda/lib/python2.7/site-packages/nltk/chunk/api.pyc in evaluate(self, gold)
     47         chunkscore = ChunkScore()
     48         for correct in gold:
---> 49             chunkscore.score(correct, self.parse(correct.leaves()))
     50         return chunkscore
     51 

<ipython-input-79-ac1236a407fe> in parse(self, tagged_sent)
     49         if not tagged_sent: return None
     50         chunks = self.tagger.tag(tagged_sent)
---> 51         return nltk.chunk.conlltags2tree([(w,t,c) for ((w,t),c) in
     52         chunks])

AttributeError: 'module' object has no attribute 'conlltags2tree'



Nigel Legg

unread,
Aug 6, 2014, 2:36:23 PM8/6/14
to nltk-...@googlegroups.com
Following the link shows
from nltk.chunk.util import conlltags2tree,
but in the traceback in your mail at line 51 you have "nltk.chunk.conlltags2tree" - so conlltag2tree is not being referenced correctly, giving the error.

Cheers, Nigel
07914 740972



--
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.

Daniel Wu

unread,
Aug 6, 2014, 3:46:18 PM8/6/14
to nltk-...@googlegroups.com
Thanks so much Nigel!

If I could have one follow-up for now.

Can you help me understand how parse() works in the following function from the NLTK cookbook? I don't see it being utilized anywhere...I've tried print statements, but nothing returns.

class ClassifierChunker(nltk.chunk.ChunkParserI):
    
    def __init__(self, train_sents, feature_detector=prev_next_pos_iob,
    **kwargs):
        
        #Self just refers to the class object itself..
        #train_sents = train_chunks (the initial input)
        #Prev_next_pos_iob = a function defined above to find features 
        
        
        if not feature_detector:
            feature_detector = self.feature_detector
        
        #now all train_sents is transformed into tuple form (N, POS), IOB
        train_chunks = chunk_trees2train_chunks(train_sents)
                
        #input feature directly into ClassifierBasedTagger, which is a 
        #sequential classifier taking info of previous POS and word
        self.tagger = ClassifierBasedTagger(train=train_chunks,
                        feature_detector=feature_detector, **kwargs)
        
    def parse(self, tagged_sent):
        """
        As a subclass of ChunkerParserI, 
        it implements the parse() method, which converts the ((w, t), c)
        tuples produced by the internal tagger into a Tree 
        using nltk.chunk.conlltags2tree().
        """

        if not tagged_sent: return None
        chunks = self.tagger.tag(tagged_sent)
        print chunks
        return nltk.chunk.util.conlltags2tree([(w,t,c) for ((w,t),c) in
        chunks])

Nigel Legg

unread,
Aug 6, 2014, 5:00:57 PM8/6/14
to nltk-...@googlegroups.com
It's not something I've used, I'll take a look if I get a chance.

Cheers, Nigel
07914 740972

Jacob Perkins

unread,
Aug 8, 2014, 7:13:09 PM8/8/14
to nltk-...@googlegroups.com
Hi Daniel,

The parse function takes a tagged sentence, as returned from a part-of-speech tagger, and returns a Tree. It has the same API as the default NLTK chunker, which is used by the nltk.chunk.ne_chunk function.

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