1.读取文件,分词整理
2.设置并输出词云
3.观察结果,优化迭代
安装wordcloud库,用pip install wordcloud命令安装,下载好需要分析的文件并保存为utf-8编码.txt格式
代码如下:
import jieba
import wordcloud
f = open("新时代中国特色社会主义.txt","r",encoding="utf-8")
t = f.read()
f.close()
ls = jieba.lcut(t)
txt = " ".join(ls)
w = wordcloud.WordCloud(font_path = "msyh.ttc",
width = 1000,height = 700,background_color ="white")
w.generate(txt)
w.to_file("grwordcloud.png")
还可以扩展成为不规则的图云,这里用到mask指定词云形状
import jieba
import wordcloud
from scipy.misc import imread
mask = imread("chinamap.jpg") #基于本图片指定的特殊词云形状
f = open("新时代中国特色社会主义.txt","r",encoding="utf-8")
t = f.read()
f.close()
ls = jieba.lcut(t)
txt = " ".join(ls)
w = wordcloud.WordCloud(font_path = "msyh.ttc",
width = 1000,height = 700,background_color ="white",
mask = mask)
w.generate(txt)
w.to_file("grwordcloud.png")