参考来源:北京理工大学嵩天老师的Python培训课程。
基本思路
-
读取文件、分词整理;
-
设置并输入词云;
-
观察结果,优化迭代。
举一反三:
- 了解词云更多参数,扩展词云能力;
- 特色词云:设计一款属于自己特色的词云风格;
- 更多文件:用更多的文件练习词云的生成。
方法实例一
import jieba
import wordcloud
file = open("三国演义.txt", "r", encoding="utf-8")
t = file.read()
file.close()
ls = jieba.lcut(t)
txt = " ".join(ls)
w = wordcloud.WordCloud(\
width=1000, height=700,\
background_color="white",
font_path="msyh.ttc"
)
w.generate(txt)
w.to_file("2.png")
方法实例二