首页 > 其他 > 详细

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

时间:2017-09-27 18:44:51      阅读:225      评论:0      收藏:0      [点我收藏+]

1.读入待分析的字符串

fo=open(‘test.txt‘,‘r‘) #读入待分析的字符串
str=fo.read()
fo.close()

2.分解提取单词 

for i in ‘,.?!"\n--‘: #分解提取单词
    str=str.replace(i,‘ ‘)
words=str.split(‘ ‘)

3.计数字典

for i in keys:    #创建计数字典
    dict[i]=words.count(i)

4.排除语法型词汇

dict={}         #排除语法型词汇
ecp=set([‘‘,‘a‘,‘an‘,‘the‘,‘and‘,‘to‘,‘in‘,‘on‘,‘of‘,‘for‘,‘i‘,‘our‘,‘us‘,‘into‘,‘her‘,‘we‘,
         ‘when‘,‘their‘,‘my‘,‘from‘,‘them‘,‘with‘,‘after‘,‘would‘,‘was‘,‘had‘,‘that‘,‘while‘,
         ‘his‘,‘she‘,‘up‘,‘it‘,‘they‘,‘so‘,‘by‘])

5.排序

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

6.输出TOP(20)

print(‘输出TOP20:‘)
for i in range(20):
   print(items[i])

技术分享肖申克的救赎

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

原文:http://www.cnblogs.com/1031353319qq/p/7603082.html

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