Anupam
Anupam

Reputation: 717

error in inaugural corpus in nltk

I have an error while running a program given in NLP with python..as follows

 import nltk
 from nltk.corpus import inaugural
>>> cfd = nltk.ConditionalFreqDist(
...             (target,file[:4])
...             for fileid in inaugural.fileids()
...             for w in inaugural.words(fileid)
...             for target in ['america','citizen']
...             if w.lower().startswith(target))
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "/usr/local/lib/python2.7/dist-packages/nltk/probability.py", line 1729, in __init__
    for (cond, sample) in cond_samples:
  File "<stdin>", line 6, in <genexpr>
TypeError: 'type' object has no attribute '__getitem__'

I am new to python,,what does this error exactly means..

Upvotes: 0

Views: 695

Answers (2)

Frank
Frank

Reputation: 36

This code is from Natural Language Processing with Python by Bird, Klein and Loper, page 46. Indeed there's a typo in the book. It has been fixed in the online version (http://www.nltk.org/book/ch02.html).

Change file[:4] to fileid[:4], as moose suggested.

Upvotes: 2

Martin Thoma
Martin Thoma

Reputation: 136389

The problem is file[:4]. file is an object that does not support slicing ([:4])

nltk.ConditionalFreqDist seems to take a list of tuples. Eventually you wanted to write fileid instead of file?

Upvotes: 0

Related Questions