網(wǎng)上有很多制作詞云的網(wǎng)站,我們使用Python也可以很方便的制作,這里,我們就簡(jiǎn)單學(xué)習(xí)下。
1. word_cloud
GitHub地址:https://github.com/amueller/word_cloud
首先我們需要安裝,正常來(lái)說(shuō),直接就執(zhí)行
pip install wordcloud
即可,但是,我這個(gè)是在Windows平臺(tái),安裝的時(shí)候,提示什么少了,需要去下載個(gè)編譯器的,報(bào)錯(cuò)信息后面有URL,

這里忘記記下來(lái)了,遇到的同學(xué),直接去下載下應(yīng)該就行了
error: Microsoft Visual C++ 14.0 is required
這里,我從網(wǎng)上找了另一個(gè)方法解決
去這個(gè)網(wǎng)站:http://www.lfd.uci.edu/~gohlke/pythonlibs/#wordcloud
找到指定的版本下載就行了

這里,我就下載了這個(gè)cp36的,然后直接安裝就行了
pip install wordcloud-1.3.2-cp36-cp36m-win_amd64.whl
官網(wǎng)上有個(gè)小栗子,我們可以測(cè)試下,代碼和使用到的數(shù)據(jù),github上都有
from os import path
from wordcloud import WordCloud
d = path.dirname(__file__)
# Read the whole text.
text = open(path.join(d, 'constitution.txt')).read()
# Generate a word cloud image
wordcloud = WordCloud().generate(text)
# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
# lower max_font_size
wordcloud = WordCloud(max_font_size=40).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
# The pil way (if you don't have matplotlib)
# image = wordcloud.to_image()
# image.show()

我們通過(guò)wordcloud生成了一個(gè)圖片,然后使用matplotlib將圖片展示出來(lái),我們分析下上面的代碼
matplotlib.pyplot.imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, shape=None, filternorm=1, filterrad=4.0, imlim=None, resample=None, url=None, hold=None, data=None, **kwargs)
Display an image on the axes.
class wordcloud.WordCloud(font_path=None, width=400, height=200, margin=2, ranks_only=None, prefer_horizontal=0.9, mask=None, scale=1, color_func=None, max_words=200, min_font_size=4, stopwords=None, random_state=None, background_color='black', max_font_size=None, font_step=1, mode='RGB', relative_scaling=0.5, regexp=None, collocations=True, colormap=None, normalize_plurals=True)
這里有很多例子:https://amueller.github.io/word_cloud/auto_examples/index.html
2. 中文詞云
上面,我們?cè)囋囍形?,我們換一個(gè)中文的文件試試即可
東方網(wǎng)記者王佳妮8月19日?qǐng)?bào)道:8月18日晚,一則重磅消息在滬發(fā)布:即日起,上海暫停新增投放共享單車(chē)。上海市交通委表示,將采取有效措施,逐步解決共享單車(chē)快速發(fā)展帶來(lái)的無(wú)序和不平衡問(wèn)題,促進(jìn)共享單車(chē)行業(yè)的持續(xù)健康發(fā)展。東方網(wǎng)記者從各平臺(tái)獲悉,將積極配合,同時(shí)考慮將中心城區(qū)部分車(chē)輛向郊區(qū)轉(zhuǎn)移。

執(zhí)行后,發(fā)現(xiàn),中文都是亂碼
我們觀察上面wordcloud類(lèi),他有一個(gè)參數(shù)
font_path : string
Font path to the font that will be used (OTF or TTF). Defaults to DroidSansMono path on a Linux machine. If you are on another OS or don’t have this font, you need to adjust this path.
這個(gè)可以指定為中文字體,
font = r'C:\Windows\Fonts\simsun.ttc'
# Generate a word cloud image
wordcloud = WordCloud(font_path=font).generate(text)
再次執(zhí)行,中文可以正常顯示了

但是,顯示的并不是我們想要的,
正常詞云的話(huà),顯示的是關(guān)鍵詞,和他的出現(xiàn)頻率有關(guān),這里的話(huà),并沒(méi)有對(duì)文本內(nèi)容做很好的分詞
剛剛?cè)ズ?jiǎn)單整理了一個(gè)Python的分詞:Python中文分詞-jieba
這樣,我們繼續(xù)修改下上面的代碼
from os import path
from wordcloud import WordCloud
import jieba
d = path.dirname(__file__)
# Read the whole text.
text = open(path.join(d, 'hello.txt'),encoding='utf-8').read()
text_cut = jieba.cut(text , cut_all=False)
font = r'C:\Windows\Fonts\simsun.ttc'
# Generate a word cloud image
wordcloud = WordCloud(font_path=font).generate(' '.join(text_cut))
# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
再次執(zhí)行下

好嘞,這下差不多了,我們的中文詞云圖就搞好了
3. 自定義形狀的詞云圖
下面,我們看看自定義形狀的詞云圖,這里用官方的例子,就是需要多傳入一個(gè)mask圖片就行了
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 20 14:04:48 2017
@author: yuguiyang
"""
from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS
d = path.dirname(__file__)
# Read the whole text.
text = open(path.join(d, 'alice.txt'),encoding='utf-8').read()
# read the mask image
# taken from
# http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg
alice_mask = np.array(Image.open(path.join(d, "alice_mask.png")))
stopwords = set(STOPWORDS)
stopwords.add("said")
wc = WordCloud(background_color="white", max_words=8000, mask=alice_mask,
stopwords=stopwords)
# generate word cloud
wc.generate(text)
# store to file
wc.to_file(path.join(d, "alice.png"))
# show
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.figure()
plt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear')
plt.axis("off")
plt.show()

剛試了下,就是要找一個(gè)喜歡的圖形圖片,
