首页 > 解决方案 > Pyechant 和 Webtext 库问题

问题描述

我是 python 新手,我正在关注一本书 Python 3 with NLTK (CookBook) 我已经通过在 Cmd 中编写pip install nltk成功地安装了 nltk 包。现在我正在尝试像这样安装 webtext 和 pyechant 库,但我遇到了这个问题: 在此处输入图像描述

这也是代码,这些是我得到代码的错误:

from nltk.collocations import TrigramCollocationFinder
from nltk.metrics import TrigramAssocMeasures
words = [w.lower() for w in webtext.words('singles.txt')]
tcf = TrigramCollocationFinder.from_words(words)
tcf.apply_word_filter(filter_stops)
tcf.apply_freq_filter(3)
print(tcf.nbest(TrigramAssocMeasures.likelihood_ratio, 4))

错误:

    File "C:/Users/lenovo/Desktop/cook book/ch1(15).py", line 3, in <module>
        words = [w.lower() for w in webtext.words('singles.txt')]
  NameError: name 'webtext' is not defined

在此处输入图像描述

标签: pythonnltklibraries

解决方案


添加

从 nltk.corpus 导入 webtext

然后还要确保下载它:

nltk.download('webtext')


推荐阅读