1.NLP是什么?
自然语言处理 Natural Language Processing,简称NLP。
书籍推荐:《数学之美》1,2版都可以,通俗易懂。
2.NLTK是什么?
自然语言处理工具集 Natural Language Toolkit,简称NLTK。
NLTK 网站:http://www.nltk.org/
3.安装NLTK
同python安装库的方法一样,这里就不详细写了。
NLTK 安装说明:http://www.nltk.org/install.html
4.下载数据
①安装单个数据包(先上代码)
import nltk #导入nltk
nltk.download("gutenberg") #下载古登堡语料
"""下载语料库就完成了,是不是很简单"""
解释:
nltk.download()安装nltk中语料库的方法。
古登堡语料库(Gutenberg Corpus),古登堡计划(Gutenberg Project) 中选择了 14 个文本,整个语料库包含了一百七十万字。
语料库
NLTK 数据:http://www.nltk.org/nltk_data/
②安装整个语料库
nltk.download('all')
③安装书中所需案例,或者练习训练用。
nltk.download('book')
这里的书中所需案例指的是《Python自然语言处理》
《Python自然语言处理》:http://ishare.iask.sina.com.cn/f/23996193.html
④需要没有训练过的数据。
nltk.download('all-corpora')
NLTK 数据的安装说明:http://www.nltk.org/data.html
5.案例
from nltk.corpus import gutenberg
from nltk.probability import FreqDist
print(gutenberg.fileids())
fd = FreqDist()
contents = gutenberg.words(fileids=["austen-persuasion.txt"])
fd = FreqDist(contents)
print(fd.B()) #包含 98171 字
print(fd.N()) #6141 个唯一单词
print(fd.pformat(maxlen=10)) #出现频率最高的10个词。
print(fd.max()) #出现频率最大的一个词