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

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

fo=open('blank space.txt','r')
song=fo.read()

exc={'the','a','to','of','and','in','that','on',''}

song=song.lower()
for i in ''',."-??\n''':
    song=song.replace(i,'')
words=song.split(' ')

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

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

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

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

fo.close()

运行结果是:

 

posted @ 2017-09-26 09:51  031庄园  阅读(129)  评论(0编辑  收藏  举报