首页 > 其他 > 详细

文件方式实现完整的英文词频统计实例

时间:2017-09-27 19:42:17      阅读:214      评论:0      收藏:0      [点我收藏+]

可以下载一长篇的英文小说,进行词频的分析。

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

7.对输出结果的简要说明。

fo=open("novel.txt",r)
new=fo.read()
fo.close()

new=new.lower()#字符串处理
for i in ,!."":
    new=new.replace(i, )
new=new.split( )#单词的列表

exp={‘‘,are,of,be,in,to,the,a,was,and,for,at,is}#不统计单次的集合
dict={}#创建一个空的字典
keys=set(new)-exp#运用集合中的差运算,键的集合
for i in keys:    
    dict[i]=new.count(i)#统计字典中每个单词出现的次数

novel=list(dict.items())#(单词,计数)元组的列表
novel.sort(key=lambda x:x[1],reverse=True) #列表排序

for i in range(20):#输出前TOP20元组
    print(novel[i])

截图:

技术分享

分析:这篇短文主要讲述,在圣诞节的时候,一对穷夫妇相互送对方礼物。

文件方式实现完整的英文词频统计实例

原文:http://www.cnblogs.com/renninwang/p/7603440.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!