首页 > 其他 > 详细

综合练习:词频统计

时间:2018-03-26 11:56:53      阅读:183      评论:0      收藏:0      [点我收藏+]
f=open(/Users/Administrator/Desktop/we.txt,r)
text=f.read()
f.close()
print(text)

rep = ‘‘‘,.!:?‘‘‘
exclude={a,the,for,in,i,my,that,of,have,and,had,it,are}
for c in rep:
     str=text.replace(c,"")
str=str.lower().split()
for i in str:
     print(i)

dict01={}
wordset=set(str)-exclude
for w in wordset:
    dict01[w]=str.count(w)
# set01 = set(str)
# print(set01)
# for item in set01:
#     dict01.update({item:str.count(item)})
# print(dict01)
for c in str:
    dict01[c]=dict01.get(c,0)+1
for w in exclude:
    dict01.pop(w)
dictlist=dict01.items()
dictlist=sorted(dict01.items(),key=lambda d:d[1],reverse=True)
for c in range(10):
    print(dictlist[c])

技术分享图片

 技术分享图片

 

技术分享图片

 

综合练习:词频统计

原文:https://www.cnblogs.com/qq157049540/p/8649757.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!