Python nltk中的初始语料库错误
我在用python运行NLP中给出的程序时出错。如下所示Python nltk中的初始语料库错误,python,nltk,Python,Nltk,我在用python运行NLP中给出的程序时出错。如下所示 import nltk from nltk.corpus import inaugural >>> cfd = nltk.ConditionalFreqDist( ... (target,file[:4]) ... for fileid in inaugural.fileids() ... for w in inaugural.words(fil
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__'
导入nltk
从nltk.corpus导入就职典礼
>>>cfd=nltk.ConditionalFreqDist(
…(目标,文件[:4])
…用于inituary.fileids()中的fileid
…在就职演说中代表w。文字(文件ID)
…目标在[‘美国’,‘公民’
…如果w.lower().startswith(目标))
回溯(最近一次呼叫最后一次):
文件“”,第3行,在
文件“/usr/local/lib/python2.7/dist packages/nltk/probability.py”,第1729行,在__
对于cond_样品中的(cond,样品):
文件“”,第6行,在
TypeError:“type”对象没有属性“\uuuu getitem\uuuu”
我是python新手,这个错误到底意味着什么?问题是
文件[:4]
。是不支持切片的对象([:4]
)
似乎有一个元组列表。最终,您希望编写fileid
而不是file
?此代码来自Bird、Klein和Loper编写的Python自然语言处理,第46页。这本书确实有一个打字错误。它已在在线版本()中修复
按照moose的建议,将
文件[:4]
更改为文件ID[:4]
。您到底想做什么/期望发生什么?你从哪里复制的代码?