02 中文分词库实例
需求:将文本中的内容用词云的形式输出
实现:
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # author:albert time:2019/10/28 4 import matplotlib.pyplot as plt 5 from wordcloud import WordCloud 6 import jieba 7 from imageio import imread 8 9 f = open("决胜全面建成小康社会.txt","r",encoding="utf-8") 10 txt = f.read() 11 result = " ".join(jieba.lcut(txt)) 12 13 color_mask = imread("五角星.jpg") 14 15 wc = WordCloud( 16 font_path=r'C:\Windows\Fonts\simkai.ttf', 17 mask=color_mask, 18 ) 19 wc.generate(result) 20 wc.to_file("xk.png") 21 22 plt.imshow(wc) 23 plt.show()
实现效果: