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

 原来

word='''Failure is probably the fortification in your pole.
It is like a peek your wallet as the thief,
when you are thinking how to spend several hard-won lepta,
 when you are wondering whether new money,it has laid background.
Because of you,then at the heart of the most lax,alert,and most low awareness,and left it godsend failed.'''

exc={'the','a','to','of','and','in','that','on','you','are','it','is','your','\n',''}

word=word.lower()
for i in ',.-':
    word=word.replace(i,' ')
words=word.split(' ')
#print(words)
d={ }
s=set(words)
s = s - exc
#print(s)
for i in s:
    d[i]=words.count(i)
#print(d)
ss=list(d.items())
ss.sort(key=lambda x:x[1],reverse=True)
#print('排序结果为:',ss)
for i in range(20):
    print(ss[i])

新的

fo = open('word.txt','r')
word=fo.read()
exc={'the','a','to','of','and','in','that','on','you','are','it','is','your','\n',''}

word=word.lower()
for i in ',.-':
    word=word.replace(i,' ')

words=word.split(' ')
#print(words)
d={ }
s=set(words)
s = s - exc
#print(s)
for i in s:
    d[i]=words.count(i)
#print(d)
ss=list(d.items())
ss.sort(key=lambda x:x[1],reverse=True)
#print('排序结果为:',ss)
for i in range(20):
    print(ss[i])
fo.close()

 

 

posted @ 2017-09-26 09:54  003刘淑千  阅读(188)  评论(0编辑  收藏  举报