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

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

时间:2017-09-27 21:00:06      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:字典   replace   排除   lower   col   div   字符串   reverse   style   

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

 

fo=open(‘test.txt‘,‘r‘)
a=fo.read()
fo.close()

exc={‘the‘,‘a‘,‘to‘,‘of‘,‘and‘,‘in‘,‘that‘,‘on‘,‘‘}
a=a.lower()
for i in ‘.,‘:
a=a.replace(i,‘ ‘)
words=a.split(‘ ‘)
print(words)

dic={}
keys=set(words)
keys=keys-exc
print(keys)

for i in keys:
dic[i]=words.count(i)
print(dic)

wc=list(dic.items())
wc.sort(key=lambda x:x[1],reverse=True)
print(wc)

for i in range(20):
print(wc[i])

 技术分享

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

标签:字典   replace   排除   lower   col   div   字符串   reverse   style   

原文地址:http://www.cnblogs.com/0020l/p/7598575.html

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