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

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

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

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

fo=open('article.txt','w')
#读入待分析的字符串 
news=fo.read()

fo.close()
#字符串处理
news.lower()          
for i in '.,:;?!-_':
    news.replace(i,' ')
#分解提取单词
news=news.split(' ')      #排除语法型词汇
exp={'the','of','and','to','a','in','at','for','with','an','has','that','will','should','is','its','he','have','on','each','during','as'}
word=set(news)-exp
#计数字典
dic={}                     
for i in word:
    dic[i]=news.count(i)
news=list(dic.items())
#排序
news.sort(key=lambda x:x[1],reverse=True)     
for i in range(20):
   print(news[i])

 

由结果可知,是关于G20峰会的内容,国家之间的开会,更好促进国与国之间交流。

posted on 2017-09-27 16:29  076陈良舒  阅读(138)  评论(0编辑  收藏  举报