WordCloud:(词云)
主要是统计词的出现个数,一个和Jieba库一起使用,且会生成图片;
import jieba,wordcloud
f = open("threekingdoms.txt", "r", encoding="utf-8")
t = f.read()
f.close()
ls = jieba.lcut(t)
# join方法就是将join前面的str作为分隔符,返回str
txt = " ".join(ls)
# font_path="msyh.ttc" 字体为微软雅黑
w = wordcloud.WordCloud(font_path="msyh.ttc", width=100,\
height=700,background_color="white",max_words=15)
# 生成词云
w.generate(txt)
# 设置路径文件格式名称
w.to_file("FirstFileV1.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",mask=mask,\
width = 1000,height = 700, background_color = "white")
w.generate(txt)
w.to_file("TestMaskV1.png")