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

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

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

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

 

a = open('text.txt','r')
new = aa.read()
aa.close()
exc={'is','are','the','on','to','can','','of','and','in','that','which','with','all','for','at'}

new = new.lower()

for i in ',.-"':
    new = new.replace(i,' ')
new = new.split(' ')    #分词,单词列表

print(new)

d = {}
keys = set(new)      #单词的集合,存入字典
keys = keys-exc
print(keys)

for i in keys:
    d[i] = new.count(i)     #统计单词,出现次数的字典
print(d)

w = list(d.items())       #将字典键值对转换为列表
w.sort(key = lambda x:x[1],reverse = True)      #排序


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

 

posted @ 2017-09-27 20:13  杜丽晖  阅读(147)  评论(0编辑  收藏  举报