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

.读入待分析的字符串

分解提取单词 

计数字典

排除语法型词汇

排序

输出TOP(20)

fo = open('新建文本文档.txt','r')

new=fo.read()
exc={'the','a','to','of','and','in','that','on',''}
new=new.lower()
for i in ',."-':
    new=new.replace(i,' ')
words=new.split(' ')
print(words)


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


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

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

for i in  range(20):
    print(wc[i])
fo.close()

 

posted on 2017-09-26 15:56  201506050009曹艺健  阅读(91)  评论(0编辑  收藏  举报