- A+
简介
这一篇介绍关于词云的生成. 生成的结果如下所示. 会简单介绍一些参考资料和简单的使用方式.

参考链接
- WordCloud 的官方说明: 说明文档-wordcloud.WordCloud
- Github仓库: wordcloud-程序媛的浪漫表白,你的个人标签
- 简单使用方式: Windows环境下Python中wordcloud的使用——自己踩过的坑 2017.08.08
简单使用方式介绍
关于字体说明
使用wordcloud可以指定使用的字体, 在windows中, 字体在以下的文件夹中: C:\Windows\Fonts, 可以将其中的字体文件拷贝到当前的文件夹内.
我们指定字体可以使得其能生成中文的词云. 我下面放一张图, 具体生成过程是和下面的方法是一样的.

使用前准备
- from os import path
- from wordcloud import WordCloud
- from PIL import Image
- import numpy as np
- d = path.dirname(__file__)
- # Read the whole text.
- text = open(path.join(d, 'constitution.txt'), encoding='utf8').read()
- # 导入字体文件
- font_path = path.join(d, 'HYC6GFM.TTF')
生成普通的wordcloud
我们可以指定使用的字体, 图像的大小和颜色等.
- # 生成普通的wordcloud
- wordcloud = WordCloud(font_path=font_path, margin=1, random_state=1, max_words=300, width=1000, height=700, background_color='white').generate(text)
- wordcloud.to_file('wordcloud.jpg')
最终的生成效果如下所示:

使用蒙版来生成图片
我们想要使得生成图片能与蒙版相符, 我们使用下面的蒙版(如果想要使用自己的蒙版,可以使用关键词「剪影」,这样主要的图像是黑色的,我们主要就是突出一个轮廓):

- # 生成带有mask的图片
- mask = np.array(Image.open(path.join(d, "62.jpg")))
- wordcloud = WordCloud(font_path=font_path, mask=mask, margin=1, random_state=1, background_color='white').generate(text)
- wordcloud.to_file('wordcloud_mask.jpg')
最终生成的效果如下所示:

源代码
将上面的测试代码放在了github上, 链接为: wordcloud测试脚本
关于中文文档
因为中文文档与外文不同,字符之间没有空格,所以我们需要首先对中文的文本进行分词。我们使用 jieba 库来对中文进行分词。这一部分参考自,中文文本的词云生成(以《三体》为例)。这一篇文章的代码可能有点问题,我在这里做一下简单的修改。
中文句子分词
首先我们对一句完整的句子进行分词,使用 jieba 来完成。
- # 对句子进行分词
- def seg_sentence(sentence):
- sentence_seged = jieba.cut(sentence.strip()) #strip()用来消除前后的空格
- outstr = ''
- for word in sentence_seged:
- if word != '\t':
- outstr += word
- outstr += " " #去掉制表符并用空格分隔各词
- return outstr.strip()
最终的效果如下所示:

停用词的处理
我们看到,上面句子中包含一些「停用词」,例如标点符号等,我们需要将其去掉。关于停用词表,可以直接在 github 上搜索「停用词」即可。我这里随便选择了其中的一个停用词来作为例子。
下面的函数可以读入停用词表,并将其转换为一个 list。
- # 创建停用词list
- def stopwordslist(filepath):
- stopwords = [line.strip() for line in open(filepath, 'r', encoding='utf-8').readlines()]
- return stopwords
大致的效果如下所示:

中文句子分词与停用词
我们把上面的两个步骤结合起来。对中文句子进行分词,同时去掉其中的「停用词」。
- def seg_sentence(sentence):
- sentence_seged = jieba.cut(sentence.strip())
- stopwords = stopwordslist('./stop.txt') # 这里加载停用词的路径
- outstr = ''
- for word in sentence_seged:
- if word not in stopwords: # 判断如果不是停用词
- if word != '\t':
- outstr += word
- outstr += " "
- return outstr
对中文文档绘制 wordcloud
在有了上面的铺垫之后,我们就可以对一个中文的文档绘制 wordcloud 了。整体的代码如下所示:
- import jieba
- from wordcloud import WordCloud
- import matplotlib.pyplot as plt
- import numpy as np
- from PIL import Image
- inputs = open('./example.txt', 'r', encoding='utf-8') # 原始的中文文档
- outputs = open('./output.txt', 'w', encoding='utf-8') # 分词过后的中文文档
- for line in inputs:
- line_seg = seg_sentence(line) # 对每个句子进行分词
- outputs.write(line_seg + '\n') # 将处理过后的文件进行保存
- outputs.close()
- inputs.close()
- mask = np.array(Image.open("./66.jpg")) # 模板图片
- inputs = open('output.txt', 'r', encoding='utf-8')
- mytext=inputs.read()
- wordcloud=WordCloud(mask=mask, width=3000, height=3000, background_color="white", margin=1,
- max_words=300, min_font_size=10, max_font_size=None, repeat=False,
- font_path="./FZKaTong-M19S.ttf").generate(mytext) #生成云图
- wordcloud.to_file('wordcloud.jpg')
- inputs.close()
最终生成的图像的效果如下所示:

指定颜色风格
在生成词云的时候,有一个参数是 colormap,我们可以指定需要的颜色风格。关于详细的 colormap 的内容,可以查看链接,Matplotlib Colormap reference。我们想要 colormap 起作用,需要设置 mode='RGBA'
。
- wordcloud=WordCloud(mask=mask, width=800, height=800, background_color="white", margin=1, max_words=10000, min_font_size=10, max_font_size=None, repeat=True,collocations=False, mode='RGBA', colormap='Reds',font_path="./FZKaTong-M19S.ttf").generate(mytext)
关键词显示重复
如果我们不想要出现的内容重复,我们可以参数,collocations=False
,即可。
- 微信公众号
- 关注微信公众号
-
- QQ群
- 我们的QQ群号
-
感谢你能看到最后,谢谢对本站的支持!!!
谢谢支持!
记得推荐给大家!
点