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

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

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

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

#读入待分析的字符
s=open ('head.txt','r')
s1=s.read()
s.close()


#分解提取单词
s=s.lower()
for i in ',.':
    s=s.replace(i,' ')
    words=s.split(" ")        #单词的列表
    print(words)



#计数字典
dic={}
keys=set(words)-exp          #键的集合


#.排除语法型词汇
exp={'to','am','it','you','so','the','will','I','my','that'}

for w in keys:
    dic[w]=words.count(w)    #单词技术字典


#排序
wc=list(dic.items())      #(单词,计数)元组的列表
wc.sort(key=lambda x:x[1],reverse=True)      #列表排序


#输出TOP(20)
for i in range(20):
    print(wc[i])
原文地址:https://www.cnblogs.com/123hyf/p/7603448.html