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

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

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

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

fo =open('C:\million.txt','r')
s=fo.read()
fo.close()
s=s.lower()
for i in ',.':
    news=s.replace(i,' ')
news=news.replace('\n',' ')
word=news.split(' ')
exp = {' ','of','it','in','that','""','be','for','not','had','the','to','and','a','an','as','also','but','is','are','','i','and','was',''}
dic={}
keys=set(word)-exp
for w in keys:
    dic[w]=word.count(w)

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:10  067杜嘉成  阅读(144)  评论(0编辑  收藏  举报