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

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

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

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

fo=open('english.txt','r')
s=fo.read()
s=s.lower()
#大写转换为小写
for i in ',.!':
    s=s.replace(i,' ')
#所有标点符号替换为空格
#单词列表及词频
words=s.split(' ')
#不统计单词的集合
exc={'','you','are','so','up','how', 'in','a','this','the','is','an','more','with','at','all','on','that','have','of','to','it','and','be','will','we','our','can'}
dic={}
key=set(words)
key=key-exc   #键的集合
for i in key:
    dic[i] = words.count(i)#单词计数字典

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

for i in range(20):#输出前15个元组
    print(wc[i])
('human', 5)
('development', 4)
('resources', 4)
('world', 3)
('unprecedented', 3)
('future', 3)
('own', 3)
('beings', 3)
('space', 3)
('one', 3)
('environment', 3)
('generations', 2)
('limited', 2)
('us', 2)
('straight', 2)
('depleted', 2)
('their', 2)
('natural', 2)
('technology', 2)
('intelligent', 2)

对输出结果的简要说明,环境对我们和我们的后代来说是多么重要,我们应该用一种更加关爱环境的方法来生活以便我们的子孙后代也能有空间和资源生存。

posted @ 2017-09-27 16:13  阿植  阅读(204)  评论(0编辑  收藏  举报