#!/usr/bin/python3
# -*- coding: utf-8 -*-
# coding=utf-8
# 导入wordcloud模块
from wordcloud import WordCloud
# 中文分词库 pip install jieba
import jieba
# 新建一个词云对象,中文需要加载字体(微软雅黑)
wc_obj = WordCloud(font_path=r'C:\Program Files\Microsoft Office\root\vfs\Fonts\private\MSYH.TTC', width=1366,
height=1000, background_color='white')
# 打开本地文本文件
filepath = r"C:\Users\omaidb\Desktop\小说.txt"
with open(filepath, 'rt', encoding='utf8') as f:
text = f.read()
# jieba中文分词
text_spt = jieba.cut(text, cut_all=False)
# jieba返回的使一个生成器,需要用join拼接
text_cn = '/'.join(text_spt)
# 执行分词
wc_obj.generate(text_cn)
# 输出词云
wc_obj.to_file(r"C:\Users\omaidb\Desktop\ciyun.png")
词云展示