7

我正在学习 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`

此错误消息非常一致。每次尝试 FreqDist() 时都会收到此消息。其他命令,如 - >>> brown.fileids() 很好。

谢谢你的帮助!

4

4 回答 4

15

在您可以使用 FreqDist 之前,您需要将其导入。

添加一行如下:

import nltk

或者如果你只是想使用 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])
于 2012-11-21T13:06:10.333 回答
1

which means you haven't installed nltk. follow these steps to install nltk:

1:go to this link https://pypi.python.org/pypi/setuptools at the end of page you find setuptools-7.0.zip (md5) download it, then unzip it. you can find easy_install.py python script.

2:use the command sudo easy_install pip. By this time pip will be installed ready to use, (make sure you are in the directory where you can find easy_install script file).

3:use this command sudo pip install -U nltk. successful execution ensure that nltk is now installed.

4:open the IDLE then you type the following:

import nltk

if nltk is installed properly then you will be returned with console.

于 2014-10-22T11:27:00.980 回答
0

nltk 需要您首先下载的数据。然后运行以下代码:

import nltk
nltk.download('stopwords')

from nltk.corpus import stopwords
stopwords.words("english")
于 2019-08-17T06:38:06.017 回答
0

旧版本的 Python 需要 setuptools。如果您运行的是 3.2+,则不需要相同的内容您可以从https://pypi.python.org/pypi/nltk轻松下载相同内容

有关http://www.nltk.org/install.html的更多信息

于 2016-06-14T17:55:03.053 回答