wordcloud:
- 安装模块:pip install wordcloud
- 基本使用:
- WordCloud(font_path, background_color, width, height, max_words).generate(xxx)
- font_path:文本的字体
- collocations:是否包含两个词的搭配,默认为true,所以会有重复的数据
- background_color:背景色
- width:幕布的宽度
- height:幕布的高度
- max_words:显示的最大词个数
- generate:读取文本文件
- 案例:
from wordcloud import WordCloud
with open("xxx.txt", encoding="utf-8") as r:
# 读取文本内容
txt = r.read()
# 设置词云图的样式
wordcloud = WordCloud(font_path="xxx.ttf", collocations=False, background_color="black", width=800, height=600, max_words=50).generate(txt)
# 生成图片
img = wordcloud.to_image()
# 展示图片
img.show()
# 保存图片
wordcloud.to_file("xxx.jpg")
jieba:
- 安装模块:pip install jieba
- 基本格式:
- jieba.analyse.extract_tags(xxx, topK, withWeight, allowPOS)
- xxx:需要处理的文本
- topK:返回关键字的数量,重要性从高到低
- withWeight:返回每个关键字的权重
- allowPOS:需要提取的词性,n为名词、v为动词,传的值为元祖
- 案例:
import jieba.analyse
from wordcloud import WordCloud
# 设置文本内容
text = ""
# 使用jieba分词,提取关键词
seg_list = jieba.analyse.extract_tags(text, allowPOS=("n", "v"))
# 拼接关键词成字符串
txt_str = " ".join(seg_list)
# 设置词云图的样式
wordcloud = WordCloud(font_path="xxx.ttf", collocations=False, background_color="black", width=800, height=600, max_words=50).generate(txt_str)
# 生成图片
img = wordcloud.to_image()
# 展示图片
img.show()
# 保存图片
wordcloud.to_file("xxx.jpg")