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

hhha = open('hop.txt','r')
news = hhha.read()
hhha.close()    #读取文件
news = news.lower()  #转换大小写
for i in ',.':
    news = news.replace(i,' ')   #将,.转换为空格
words =news.split(' ')#用空格分解单词
exp={'','the','and','to','on','of','s','a','is','u','as','also','','in','with','for','nfl','an'} #介词列表
dic={}
keys = set(words)-exp #排除列表中包含的介词及语法型词汇
for w in keys:
    dic[w]=words.count(w)#单词计数字典
fc=list(dic.items())#单词列表
fc.sort(key=lambda x:x[1],reverse=True)#排序
for i in range(20):  #输出前二十
    print(fc[i])

posted @ 2017-09-27 16:01  blackboardf  阅读(190)  评论(0编辑  收藏  举报