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

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

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

 

fo = open('C:\\Users\\CANDY\\Desktop\\a.txt','r')
news = fo.read()
fo.close()
news=news.lower()#将所有大写转换为小写
for i in ',.?!\n""'' ':#将所有将所有其他做分隔符(,.?!)替换为空格
news=news.replace(i,' ')
words = news.split(' ')
exp={'','in','has','and','a','to','s','the','it','that','was','of','but','as','with','on'}

dic={}
keys = set(words)-exp
for i in keys:
dic[i]=words.count(i)#单词计数字典
its=list(dic.items())#单词计数元组的列表
its.sort(key=lambda x:x[1],reverse=True)#排序

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

  

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

对《老人与海》做的分析,old man fish,出现的频率很高。老人去钓鱼回来的过程。

posted on 2017-09-27 18:01  黑木-007  阅读(174)  评论(0编辑  收藏  举报