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

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

 

fo=open('test.txt','r')
a=fo.read()
fo.close()

exc={'the','a','to','of','and','in','that','on',''}
a=a.lower()
for i in '.,':
a=a.replace(i,' ')
words=a.split(' ')
print(words)

dic={}
keys=set(words)
keys=keys-exc
print(keys)

for i in keys:
dic[i]=words.count(i)
print(dic)

wc=list(dic.items())
wc.sort(key=lambda x:x[1],reverse=True)
print(wc)

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

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