码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-27 21:03:25      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:print   txt   ever   div   range   lower   数字   style   元组   

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

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

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

#读入待分析的字符
s=open (head.txt,r)
s1=s.read()
s.close()


#分解提取单词
s=s.lower()
for i in ,.:
    s=s.replace(i, )
    words=s.split(" ")        #单词的列表
    print(words)



#计数字典
dic={}
keys=set(words)-exp          #键的集合


#.排除语法型词汇
exp={to,am,it,you,so,the,will,I,my,that}

for w in keys:
    dic[w]=words.count(w)    #单词技术字典


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


#输出TOP(20)
for i in range(20):
    print(wc[i])

 

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

标签:print   txt   ever   div   range   lower   数字   style   元组   

原文地址:http://www.cnblogs.com/123hyf/p/7603448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!