综合练习
词频统计预处理
下载一首英文的歌词或文章
将所有,.?!’:等分隔符全部替换为空格
将所有大写转换为小写
生成单词列表
生成词频统计
排序
排除语法型词汇,代词、冠词、连词
输出词频最大TOP20
将分析对象存为utf-8编码的文件,通过文件读取的方式获得词频分析内容。
f=open("file.txt","r") news=f.read() f.close() sep=‘‘‘,().;--‘‘‘ exclude={‘the‘,‘to‘,‘and‘,‘of‘,‘in‘,‘for‘,‘on‘,‘a‘,‘when‘,‘as‘,‘not‘,‘with‘,‘that‘} for c in sep: news = news.replace(c,‘‘) wordList=news.lower().split() wordDict={} wordSet=set(wordList)-exclude for w in wordSet: wordDict[w]=wordList.count(w) dictList = list(wordDict.items()) dictList.sort(key=lambda x:x[1],reverse=True) for i in range(20): print(dictList[i])