中文词频统计

  1. 下载一中文长篇小说,并转换成UTF-8编码。
  2. 使用jieba库,进行中文词频统计,输出TOP20的词及出现次数。
  3. 排除一些无意义词、合并同一词。
  4. 对词频统计结果做简单的解读。
import jieba #导入结巴

str= open('C:/Uscer/ben/test.txt','r',encoding= 'utf-8').read()#打开文件
words=list(jieba.cut(str))

exp = {','}#删除逗号

keys = set(words)-exp

dic={}
for w in keys:
  if len(w)>1:#去除无意义词
    dic[w]=words.count(w)#计数字典

wc = list(dic.items())#排序
wc.sort(key= lambda x:x[1],reverse=True)


for i in range(20):#输出TOP(20)
    print(wc[i])

  

此文讲述杨叶与人君的故事

原文地址:https://www.cnblogs.com/0055sun/p/7610368.html