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

中文词频统计

时间:2017-09-29 13:24:25      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:style   set   utf-8   span   中文   imp   下载   read   com   

中文分词

  1. 下载一中文长篇小说,并转换成UTF-8编码。
  2. 使用jieba库,进行中文词频统计,输出TOP20的词及出现次数。
  3. 排除一些无意义词、合并同一词。
  4. 对词频统计结果做简单的解读。
import jieba
book=open(F:\\xiyouji.txt,r,encoding=utf-8)

#读入待分析的字符串
str=book.read()
book.close()

for i in ,。!、   \n “ ” ;:
    str=str.replace(i,‘‘)

words=jieba.cut(str)
word=set(words)

#计数字典 
dic={}
for i in word:
    if len(i)>1:
        dic[i]=str.count(i)
str=list(dic.items())

#排序
str.sort(key=lambda x:x[1],reverse=True)
for i in range(20):
    print(str[i])

技术分享

从词频统计结果出来可以看出此小说是西游记,主要人物有唐僧,八戒等,讲述他们取西经的过程。

 

中文词频统计

标签:style   set   utf-8   span   中文   imp   下载   read   com   

原文地址:http://www.cnblogs.com/lkm123/p/7610574.html

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