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

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

时间:2017-09-27 19:12:30      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:排序   单词   pre   range   blog   下载   with   into   提取   

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

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

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

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

str=str.lower()

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

#排除语法型词汇
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‘])
keys=set(words)-ecp 

#创建计数字典
for i in keys:
    dict[i]=words.count(i)
#将字典转换为列表,以便后序排序
items=list(dict.items()) 

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

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

技术分享

这篇英语文章阐述了家庭内孩子与母亲之前出去游玩所发生的小趣事。

 

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

标签:排序   单词   pre   range   blog   下载   with   into   提取   

原文地址:http://www.cnblogs.com/wuwenx/p/7602279.html

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