在尝试用官方的安装方式几次失败后,发现官方的这种方式真的很坑。于是上网按着大家的教程,终于摸索出了一个可行的方法。
安装
首先下载nltk安装包:
解压
下载完成后,将文件解压放在D盘根目录。
测试
然后打开IDE进行测试:
from nltk.book import *
输出结果:
*** Introductory Examples for the NLTK Book ***
Loading text1, ..., text9 and sent1, ..., sent9
Type the name of the text or sentence to view it.
Type: 'texts()' or 'sents()' to list the materials.
text1: Moby Dick by Herman Melville 1851
text2: Sense and Sensibility by Jane Austen 1811
text3: The Book of Genesis
text4: Inaugural Address Corpus
text5: Chat Corpus
text6: Monty Python and the Holy Grail
text7: Wall Street Journal
text8: Personals Corpus
text9: The Man Who Was Thursday by G . K . Chesterton 1908
到这里,说明nltk库已经安装好了,但是在编程测试的时候,仍然报错:
from nltk import word_tokenize
from nltk import Text
tokens = word_tokenize("Here is some not very interesting text")
text = Text(tokens)
print(text)
错误1:punkt没找到
解决方案:
1、打开路径:D:\nltk_data\tokenizers,发现有一个名为<punkt.zip>的压缩包;
2、选中该压缩包,解压到当前目录下
再次运行上述程序,又报错,错误为:D:\nltk_data\tokenizers\punkt\PY3\english.pickle没找到
解决方案:
1、打开路径D:\nltk_data\tokenizers\punkt,发现其实存在english.pickle
2、路径检索的时候多了个\PY3\,所以,在D:\nltk_data\tokenizers\punkt新建一下文件夹,命名为PY3,然后将文件english.pickle拷贝进去PY3文件夹下
再次运行程序,结果正确:
<Text: Here is some not very interesting text...>
总结
如果在运行程序时,提示说没有找到某个文件,那么可以去该路径下查看,是否没有解压文件,或者路径不对。
按照提示路径创建文件即可。