中文词频统计及词云制作


2.中文分词


  1. 下载一中文长篇小说,并转换成UTF-8编码。
  2. 使用jieba库,进行中文词频统计,输出TOP20的词及出现次数。
  3. **排除一些无意义词、合并同一词。
  4. **使用wordcloud库绘制一个词云。

import
jieba txt = open('汉魏文魁.txt',"r",encoding='utf-8').read() ex = {'自己','可是','不是','他们','还是','什么','没有','一个','知道','时候', '公子','突然','那么','虽然','就是','以后','然后','要是','已经','于是' ,'这个','还有','怎么','不会','可能','只是','因为','出来','起来','竟然' ,'赶紧','可以'} ls = [] words = jieba.lcut(txt) counts = {} for word in words: ls.append(word) if len(word) == 1: continue else: counts[word] = counts.get(word,0)+1 for word in ex: del(counts[word]) items = list(counts.items()) items.sort(key = lambda x:x[1], reverse = True) for i in range(20): word , count = items[i] print ("{:<10}{:>5}".format(word,count))

posted on 2017-09-25 17:17  33陈思远  阅读(176)  评论(0编辑  收藏  举报