当我在NLTK中尝试FreqDist()时收到错误消息 - NameError:name'nltk'未定义

use*_*239 7 nltk

我正在学习NLTK,我的mac工作正常,除了我遇到了FreqDist().(我看到另一个关于FreqDist()的问题,但他得到了一个不同的错误消息.TypeError:unhashable type:'list')这是一个例子:

>>> from nltk.corpus import brown
>>> news_text = brown.words(categories='news')
>>> fdist = nltk.FreqDist([w.lower() for w in news_text])

Traceback (most recent call last):

`  File "<stdin>", line 1, in <module>`
`NameError: name 'nltk' is not defined`
Run Code Online (Sandbox Code Playgroud)

此错误消息非常一致.每次尝试FreqDist()时都会收到此消息.其他命令如 - >>> brown.fileids()也没问题.

谢谢你的帮助!

Spa*_*ost 14

在使用FreqDist之前,您需要导入它.

添加一行如下:

import nltk
Run Code Online (Sandbox Code Playgroud)

或者如果你只想使用FreqDist,你应该试试这个:

>>> from nltk.corpus import brown
>>> from nltk import FreqDist
>>> news_text = brown.words(categories='news')
>>> fdist = FreqDist([w.lower() for w in news_text])
Run Code Online (Sandbox Code Playgroud)