0

I'm trying to implement a sentence chunker based on a Maxent Classifier, as described in the NLTK book (Example 7.9):

http://nltk.googlecode.com/svn/trunk/doc/book/ch07.html#code-classifier-chunker

When I try to evaluate the chunker with

chunker = ConsecutiveNPChunker(train_sents)
print chunker.evaluate(test_sents)

or to chunk a sentence with

print chunker.parse(test_sents[1])

I receive the following error:

Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    print chunker.parse(test_sents[1])
  File "/usr/local/lib/python2.6/dist-packages/nltk/chunk/api.py", line 34, in parse
    assert 0, "ChunkParserI is an abstract interface"
AssertionError: ChunkParserI is an abstract interface

I didn't find anything on google and I'm stuck on this point. Any help would be really useful!

Vasilis
  • 2,721
  • 7
  • 33
  • 54

1 Answers1

4

You evidently haven't implemented your chunker yet. "ChunkParserI is an abstract interface" means that you need to derive a class from it and define your own parse() method. The NLTK chapter you link to shows how to define an example class, ConsecutiveNPChunker.

The final step will be to create an instance of your new class and call its eval() method (which it inherits from ChunkParserI, so you don't to provide a replacement).

alexis
  • 48,685
  • 16
  • 101
  • 161
  • Thanks Alexis. I actually instantiate my class as follows: chunker = ConsecutiveNPChunker(train_sents) I edited the original question to make it clear. – Vasilis Apr 08 '12 at 21:07
  • 1
    But you don't make clear how `ConsecutiveNPChunker` is defined. Obviously it does not provide a `parse` method. – alexis Apr 08 '12 at 21:13
  • Sorry for not adding the ConsecutiveNPChunker definition, I didn't add it because it's a bit lengthy. But the link I provide has the exact class which includes a parse() method – Vasilis Apr 08 '12 at 22:06
  • 1
    The example in the NLTK book adds a `parse` method, and it works. You don't, so you get the `parse` from `chunk/api.py`, whose only job is to raise the error. Perhaps you have an indentation error in your code? – alexis Apr 08 '12 at 22:08
  • Thanks! Indentation was wrong so - as you said - the parse() from chunk/api.py was called instead the one from the ConsecutiveNPChunker ... – Vasilis Apr 08 '12 at 23:47
  • Good to hear. Just to be clear: Because of your indentation error, `ConsecutiveNPChunker` did not HAVE a `parse` method, which is why the method of the parent class was called. – alexis Apr 09 '12 at 09:49
  • @vasilis would it be possible for you to post the full code you used to get this to work? I'm having a similar error and I don't understand how to fix. – user3314418 Aug 06 '14 at 17:49
  • 1
    @user, fix your indentation or write a `parse()` method, whichever the case may be. – alexis Aug 08 '14 at 19:37