中文词频统计

下载一中文长篇小说,并转换成UTF-8编码。

使用jieba库,进行中文词频统计,输出TOP20的词及出现次数。

排除一些无意义词、合并同一词。

对词频统计结果做简单的解读。

import jieba
txt = open('xiaoshuo.txt','r',encoding='utf-8').read()
words = list(jieba.cut(txt))

for i in ',.?!':
txt=txt.replace(i,' ')
words=txt.split(' ')

dic={}
for w in words:
if len(w)==1:
continue
else:
dic[w]=dic.get(w,0)+1

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

for i in range(20):
print(wc[i])

posted @ 2017-09-29 10:52  007杨碧茜  阅读(95)  评论(0编辑  收藏  举报