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

词频统计

时间:2018-03-26 12:40:13      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:替换   pen   eve   post   技术分享   log   rds   int   lower   

f = open(‘news.txt‘,‘r‘)
news = f.read()
f.close()
print(news)

sep = ‘‘‘.,?‘!:"‘‘‘
exclude = {‘the‘,‘and‘,‘of‘,‘to‘,‘and‘,‘he‘,‘for‘}
# 将sep里面的字符替换为空格
for c in sep:
    news = news.replace(c," ")

wordList = news.lower().split()
print(wordList)

wordDict = {}
wordSet = set(wordList)
for w in wordSet:
    wordDict[w] = wordList.count(w)
    # print(wordDict[w],wordList.count(w))
dictList = list(wordDict.items())
dictList.sort(key=lambda  x:x[1],reverse=True)
a = dictList
# for w in dictList:
#     print(w)
for i in range(10):
    print(dictList[i])
exit()
for w in wordList:
    wordDict[w] = wordDict.get(w,0)+1
for w in exclude:
    wordDict.pop(w)
dictList = list(wordDict.items())
print(dictList)

技术分享图片

 


  

词频统计

标签:替换   pen   eve   post   技术分享   log   rds   int   lower   

原文地址:https://www.cnblogs.com/a13798508446/p/8649761.html

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