#下载 NLTK 图书集
>>> import nltk
>>> nltk.download()
使用 nltk.download()浏览可用的软件包。下载器上的 Collections 选项卡显示软件包如何被打包分组。选择 book 标记所在行,可以获取本书的例子和练习所需的全部数据。这些数据包括约 30 个压缩文件,需要 100MB 硬盘空间。完整的数据集(即下载器中的 all)在本书写作期间大约是这个大小的 5 倍,还在不断扩充。
>>> 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
文本1:《白鲸记》
文本2:《理智与情感》
文本3:《创世纪》
文本4:《就职演讲语料库》
文本5:《NPS 聊天语料库》
文本6:《巨蟒与圣杯》
文本7:《华尔街日报》
文本8:《交友科尔普斯》
文本9:《谁是星期四》
================================================
#搜索文本
##查《白鲸记》中的词monstrous:
text1.concordance("monstrous")
##搜索《理智与情感》中的词affection:
text2.concordance("affection")
##搜索《创世纪》找出某人活了多久:
text3.concordance("lived")
##在《NPS 聊天语料库》搜索一些网络词,如im,ur,lol
text5.concordance("im")
##索引,使我们看到词的上下文,索引monstrous在《白鲸记》出现的上下文
text1.similar("monstrous")
imperial subtly impalpable pitiable curious abundant perilous
trustworthy untoward singular lamentable few determined maddens
horrible tyrannical lazy mystifying christian exasperate
In [15]:
##索引monstrous在《理智与情感》出现的上下文
text2.similar("monstrous")
very exceedingly so heartily a great good amazingly as sweet
remarkably extremely vast
###观察我们从不同的文本中得到的不同结果。Austen(奥斯丁,英国女小说家)使用这些词与 Melville 完全不同;在她那里,monstrous是正面的意思,有时它的功能像词very一样作强调成分。
##研究两个或两个以上的词共同的上下文,如monstrous和very
text2.common_contexts(["monstrous", "very"])
##以判断词在文本中的位置:从文本开头算起在它前面有多少词。这个位置信息可以用离散图表示
##在过去220年中的一些显著的词语用法模式(在一个由就职演说语料首尾相连的人为组合的文本中)
text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"])
###将产生-美国总统就职演说词汇分布图,可以用来研究随时间推移语言使用上的变化
##产生随机文本,由于要搜集词序列的统计信息而执行的比较慢。每次运行它,输出的文本都会不同
text3.generate()
'''
《python自然语言处理时》第28页有这样一个命令--text3.generate()---功能是:产生一些与text3风格类似的随机文本。
用NLTK3.0.4和Python2.7.6来实现时却出现错误:'Text' object has no attribute 'generate' .
探索一下后发现问题所在:
打开nltk文件夹中的text.py发现了,原来新版本的NLTK没有了“text1.generate()”这个功能作者已经把demo里的text.generate()注释掉了,但是我下载了nltk2.0.1版本的安装包,解压后打开nltk文件夹下的text.py,发现老版本中有这个功能(《python自然语言处理时》书中用的是NLTK2.0版本),所以要是想用这个功能的同学请安装nltk2.0.1版本,nltk3.x的版本是没了
'''
================================================
#计数词汇
##获取《创世纪》的长度
len(text3)
44764
##获取词汇表,Python 中我们可以使用命令:set(text3)获得 text3 的词汇表
set(text3)
##排序词汇表
sort(set(text3))
##获取词汇表的长度
len(set(text3))
2789
###尽管小说中有 44,764 个标识符,但只有 2,789 个不同的词汇或“词类型”。一个词类型是指一个词在一
个文本中独一无二的出现形式或拼写。也就是说,这个词在词汇表中是唯一的。我们计数的2,789个项目中包括标点符号,所以我们把这些叫做唯一项目类型而不是词类型。
##测量文本词汇丰富度
len(text3) / len(set(text3))
##计数一个词在文本中出现的次数,计算一个特定的词在文本中占据的百分比
text3.count("smote")
100 * text4.count('a') / len(text4)
##定义计数函数
###词汇差异度,丰富度,越趋近1 越丰富(1 <= lexical_diversity <= len(text))
def lexical_diversity(text):
return len(text) / len(set(text))
###百分比
def percentage(count, total):
return 100 * count / total