文件方式实现完整的英文词频统计实例
可以下载一长篇的英文小说,进行词频的分析。
1.读入待分析的字符串
2.分解提取单词
3.计数字典
4.排除语法型词汇
5.排序
6.输出TOP(20)
7.对输出结果的简要说明。
fo=open('D:\\numb.txt','r') news=fo.read() fo.close() news=news.lower()# 字符串处理 for i in ',.': news=news.replace(i,'') words=news.split(' ')#单词的列表 print(words) exp={'i','so','the','to','be','in','of'}#不统计单词的集合 dic={} keys=set(words)-exp#键的集合 for j in keys: dic[j]=words.count(j)#单词计数字典 s=list(dic.items()) s.sort(key=lambda x:x[1],reverse=True)#列表排序 for i in range(20):#输出TOP20元组 print(s[i])