中文词频统计及词云制作

中文分词

  1. 下载一中文长篇小说,并转换成UTF-8编码。
  2. 使用jieba库,进行中文词频统计,输出TOP20的词及出现次数。
  3. **排除一些无意义词、合并同一词。
  4. **使用wordcloud库绘制一个词云。
import jieba
txt="E://novels.txt"
book=open(txt,"r",encoding='utf-8').read()
excepts={'前言','第一卷'}
ls=[]
words=jieba.lcut(book)
counts={}
for i in words:
    ls.append(i)
    if len(i)==1:
        continue
    else:
        counts[i]=counts.get(i,0)+1
for i in excepts:
    del(counts[i])
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
print('出现频率最高的词组前十:')
for j in range(10):
    i , count=items[j]
    print("{:<10}{}".format(i,count))

  

 

posted @ 2017-09-25 14:20  学晖学习  阅读(238)  评论(0编辑  收藏  举报