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

综合练习:词频统计

时间:2018-03-27 18:54:22      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:str   rdd   AC   col   sort   统计   log   \n   语法   

 

下载一首英文的歌词或文章

f = open("F:\song.txt","r")
str1=f.read()
f.close()

 

将所有,.?!’:等分隔符全部替换为空格

c="‘,./‘"
for w in c:
  str1.replace(w, )

将所有大写转换为小写

生成单词列表

wordList=str1.lower().split()

生成词频统计

wordDict={}
wordSet=set(wordList)
for w in wordSet:
    wordDict[w]=wordList.count(w)

排序

dictList=list(wordDict.items())
dictList.sort(key=lambda x:x[1],reverse=True)

排除语法型词汇,代词、冠词、连词

pron={for,the,of,to,that,/}
wordSet=set(wordList)-pron

输出词频最大TOP20

将分析对象存为utf-8编码的文件,通过文件读取的方式获得词频分析内容。

f=open("F:\song1.txt",w)
for i in range(20):
    f.write(dictList[i][0]+" "+str(dictList[i][1] )+\n)
f.close()

 

综合练习:词频统计

标签:str   rdd   AC   col   sort   统计   log   \n   语法   

原文地址:https://www.cnblogs.com/cgz123/p/8658592.html

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