您的位置:首页 > 编程语言 > Python开发

Windows 64位下python3安装nltk模块

2018-09-19 09:53 633 查看

在网上找了各种安装教程,都没有在python3下安装nltk,于是我自己尝试着安装,算是成功了

1、首先,假设你的python3已经安装好,并且安装了numpy,matplotlib,pandas一些常用的库,当你安装nltk时,我相信你一定具备了一些python的常用技能,所以默认你已经安装了以上常用模块

2、然后,用cmd打开电脑终端,然后输入D:\Python362\Scripts\pip install pyyaml nltk,这样既安装了PyYAML模块,又安装了nltk模块

3、再次,在python3编辑器里输入import nltk按回车,如下图:

如这样你以为nltk就安装成功了那么你就大错特错了,因为还有一步,如果最后一步忽略的话,那么你在用nltk的时候会出现很多错误,具体出现什么样的错误,忘记截图了,后来运行成功后就没有错误了。

4、最后,在python3 shell里面输入一下命令:

按回车,然后就会出现如下窗口:

我是选择默认的下载路径,点击Download,静静的等待它下载完,关闭窗口。

下面进行nltk测试:

sent1 = 'The cat is walking in the bedroom'
sent2 = 'A dog was running across the kitchen'
from sklearn.feature_extraction.text import CountVectorizer
count_vec = CountVectorizer()
sentences = [sent1, sent2]
print(count_vec.fit_transform(sentences).toarray())
print(count_vec.get_feature_names())
import nltk
tokens_l = nltk.word_tokenize(sent1)
print(tokens_l)

运行结果如下:

[[0 1 1 0 1 1 0 0 2 1 0]
[1 0 0 1 0 0 1 1 1 0 1]]
['across', 'bedroom', 'cat', 'dog', 'in', 'is', 'kitchen', 'running', 'the', 'walking', 'was']
['The', 'cat', 'is', 'walking', 'in', 'the', 'bedroom']

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python python3 nltk