码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-26 21:03:45      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:other   leave   .so   技术分享   字符   lambda   替换   body   and   

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

>>> fo=open(‘123.txt‘,‘w‘)
>>> fo.write(‘‘‘I messed up tonight, I lost another fight
I still mess up but I‘ll just start again
I keep falling down, I keep on hitting the ground
I always get up now to see what‘s next
Birds don‘t just fly, they fall down and get up
Nobody learns without getting it won
I won‘t give up, no I won‘t give in
Til I reach the end, then I‘ll start again
No I won‘t leave, I wanna try everything
I wanna try even though I could fail
I won‘t give up, no I won‘t give in
Til I reach the end and then I‘ll start again
No I won‘t leave, I wanna try everything
I wanna try even though I could fail‘‘‘)
574
>>> fo.close()
fo=open(‘123.txt‘,‘r‘)
song=fo.read()
 
song=song.lower() #小写
for i in ‘,.‘:
    song=song.replace(i,‘ ‘)  #替换标点符号
words=song.split()  #分单词
#print(words)
 
dic={} #字典
keys=set(words) #集合
#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])
fo.close()

技术分享

技术分享

技术分享

技术分享

技术分享

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

标签:other   leave   .so   技术分享   字符   lambda   替换   body   and   

原文地址:http://www.cnblogs.com/xfy1317/p/7595113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!