Computing with Language:Simple Statistics

Frequency Distributions

//定义变量
fdist1 = FreqDist(text1)
//输出
fdist1
//重复最多的50个
fdist1.most_common(50)
//whale重复次数
fdist1['whale']
//累积频率图
fdist1.plot(50,cumulative=True)
//单频词
fdist1.hapaxes()


//定义V,V是一个链表,而不是一个集合
V = set(text1)
//在V中长度大于15的词
long_words = [w for w in V if len(w) > 15]
//排序
sorted(long_words)

Python这里很类似于数学的表达方式,和正在用的java相比,更偏数学语言。

//词长>7,且词频>7的词(与文本内容相关的高频词)
fdist5 = FreqDist(text5)
sorted(w for w in set(text5) if len(w) > 7 and fdist5[w] > 7)

Collocations and Bigrams

双联词

bigrams(['more','is','said','than','done'])
直接执行上述代码会报错

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    bigrams(['more','is','said','than','done'])
NameError: name 'nltk' is not defined

需要import nltk

from nltk import *
之后执行,并未显示出来,而是以下语句,需要加上list函数执行。

<generator object bigrams at 0x044A6BD0>

list(bigrams(['more','is','said','than','done']))
collocation函数为我们找到一个text中的双联词

text4.collocations()

Counting other things

//词长的频率
fdist = FreqDist([len(w) for w in text1])
fdist.keys()
//freqdist后的结果
fdist.items()
fdist.max()
fdist[3]
fdist.freq(3)

NLTK频率分布类中定义的函数

例子描述
fdist = FreqDist(samples)创建包含给定样本的频率分布
fdist.inc(sample)增加样本
fdist['monstrous']计数给定样本出现的次数
fdist.freq('monstrous')给定样本的频率
fdist.N()样本总数
fdist.keys()以频率递减顺序排序的样本链表
for sample in fdist :以频率递减的顺序遍历样本
fidst.max()数值最大的样本
fdist.tabulate()绘制频率分布表
fdist.plot()绘制频率分布图
fdist.plot(cumulative=True)绘制累积频率分布图
fdist1 < fdist2测试样本在fdist1中出现的频率是否小于fdist2







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值