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

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

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

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

fr=open('D:\english/father.txt','r')
news=fr.read()
fr.close

news=news.lower()

for i in '“”,.?':
    news=news.replace(i,' ')   
words=news.split(' ')
exp={'and','that','had','as','we','for','was','not','','to','is','the','of','in','a','i','he','you','be','it','his',''}

dic={}
#集合
key=set(words)-exp
#计数字典
for wn in key:
    dic[wn]=words.count(wn)
wc=list(dic.items())
#排序
wc.sort(key=lambda x:x[1],reverse=True)

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

\

从结果可知,本篇文章主要围绕父亲和家庭在讲述父爱

posted @ 2017-09-27 16:11  017黄乐仪  阅读(145)  评论(0编辑  收藏  举报