用Python轻松搞定词云图(高考英语作文+斗罗大陆)

引言

你是否在网络上经常看到各种各样的词云图呢?当你看到它们时,你有没有想一下,这些词云图是怎么生成的呢?其实,词云图的生成非常简单,用Python几行代码就可以轻松搞定词云图。这里分别以英语作文和《斗罗大陆》为例,给大家展示一下如何用Python生成词云图。

具体实现

首先,我们用英语作文来试一下,而英文与中文的区别就是英文语料是不需要进行分词的,因为,英文中,各个单词都是以空格进行分隔的。

Notice
Boys and girls,
May I have your attention, please? Recently, an outstanding movie will be shown in our campus. Welcome to join us end enjoy it! Here are some relevant details about it.
To begin with, the name of the movie is Growing together, which is about the development of our beloved school; as a result, it will be not only meaningful but also interesting. Besides, it will be in the library from 2:00 to 4:00 in the afternoon on June 9th . What’s more, everyone of you will be welcome to take part in it, enjoying the movie, having a heated discussion afterwards and giving your own comments.
Hopefully, you would make it to our activity. I have the confidence that you will have a great time.
The Student Union

这是一篇英语高考作文,我们来生成一下它的词云图。

# -*- coding: utf-8 -*-

from wordcloud import WordCloud
import matplotlib.pyplot as plt

# 打开文本
text = open('./res/English.txt','r').read()

# 生成词云对象
wc = WordCloud().generate(text)

# 显示词云
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
plt.show()


# 保存词云
wc.to_file('./result/wordcloud1.png')

生成的词云图如下:
在这里插入图片描述
接下来我们用中文试一下,这里以小说《斗罗大陆》为例。
在这里插入图片描述

# -*- coding: utf-8 -*-

from wordcloud import WordCloud 
import matplotlib.pyplot as plt 
import jieba

# 打开文本
text = open('./res/douluo.txt','r').read()

# 中文分词
text = ' '.join(jieba.cut(text))
print(text[:100])
# 字体路径
font_path = 'C:\\Windows\\Fonts\\STFANGSO.TTF'
# 生成词云对象
wc = WordCloud(font_path=font_path, width=800, height=600, mode='RGBA', background_color=None).generate(text)

# 显示词云
plt.imshow(wc,interpolation='bilinear')
plt.axis('off')
plt.show()

# 保存词云
wc.to_file('./result/wordcloud2.png')

在这里插入图片描述
当然,我们也可以加入背景图片来控制词云的形状。

# -*- coding: utf-8 -*-

from wordcloud import WordCloud 
from PIL import Image
import numpy as np 
import matplotlib.pyplot as plt 
import jieba

font_path = 'C:\\Windows\\Fonts\\STFANGSO.TTF'

# 打开文本
text = open('./res/douluo.txt','r').read()

# 中文分词
text = ' '.join(jieba.cut(text))

# 生成对象
mask = np.array(Image.open('res/bg.jpg'))
wc = WordCloud(mask=mask, font_path=font_path,mode='RGBA',background_color=None).generate(text)

# 显示词云
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
plt.show()

# 保存词云
wc.to_file('result/wordcloud3.png')

在这里插入图片描述
我们还可以从背景图片中提取颜色,以达到控制文字颜色的目的。
在这里插入图片描述

# -*- coding: utf-8 -*-

from wordcloud import WordCloud, ImageColorGenerator
from PIL import Image
import numpy as np 
import matplotlib.pyplot as plt 
import jieba

font_path = 'C:\\Windows\\Fonts\\STFANGSO.TTF'

# 打开文本
text = open('./res/douluo.txt','r').read()

# 中文分词
text = ' '.join(jieba.cut(text))

# 生成对象
mask = np.array(Image.open('res/bg2.jpg'))
wc = WordCloud(mask=mask, font_path=font_path,mode='RGBA',background_color=None).generate(text)
# 从图片中生成颜色
image_colors = ImageColorGenerator(mask)
wc.recolor(color_func=image_colors)

# 显示词云
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
plt.show()

# 保存词云
wc.to_file('result/wordcloud4.png')

在这里插入图片描述
最后,我提取了小说中的关键词,用这些关键词来生成词云图,让我们看一下效果吧!

# -*- coding: utf-8 -*_
from wordcloud import WordCloud, ImageColorGenerator
from PIL import Image
import numpy as np 
import matplotlib.pyplot as plt 
import jieba.analyse

font_path = 'C:\\Windows\\Fonts\\STFANGSO.TTF'
# 打开文本
text = open('res/douluo.txt').read()

# 提取关键词和权重
freq = jieba.analyse.extract_tags(text, topK=200, withWeight=True)
print(freq[:20])
freq = {i[0]: i[1] for i in freq}

# 生成对象
mask = np.array(Image.open('res/bg.jpg'))
wc = WordCloud(mask=mask, font_path=font_path, mode='RGBA', background_color=None).generate_from_frequencies(freq)

#从图片中提取颜色
image_colors = ImageColorGenerator(mask)
wc.recolor(color_func=image_colors)

# 显示词云
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
plt.show()

# 保存词云
wc.to_file('result/wordcloud5.png')

在这里插入图片描述

总结

从词云图上,我们可以看到,有一些词语是无用的,这些词语是我们不想要的,在生成词云时,我们可以设置停用词来屏蔽这些没有用的词语。
对于字体颜色的控制,我们可以封装一个函数来随机生成颜色,这样生成的词云图会更好看一些。

发布了187 篇原创文章 · 获赞 289 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/Deep___Learning/article/details/104630245