词云,又称文字云、标签云,是对文本数据中出现频率较高的“关键词”在视觉上的突出呈现,形成关键词的渲染形成类似云一样的彩色图片,从而一眼就可以领略文本数据的主要表达意思。常见于博客、微博、文章分析等。

除了网上现成的Wordle、Tagxedo、Tagul、Tagcrowd等词云制作工具,在python中也可以用wordcloud包比较轻松地实现(官网、github项目):

from wordcloud importWordCloudimportmatplotlib.pyplot as plt#Read the whole text.

text = open(‘constitution.txt‘).read()#Generate a word cloud image

wordcloud =WordCloud().generate(text)#Display the generated image:#the matplotlib way:

plt.imshow(wordcloud, interpolation=‘bilinear‘)

plt.axis("off")

生成的词云如下:

还可以设置图片作为mask:

alice_mask = np.array(Image.open(path.join(d, "alice_mask.png")))

wc= WordCloud(background_color="white", max_words=2000, mask=alice_mask, stopwords=stopwords, contour_width=3, contour_color=‘steelblue‘)

wc.generate(text)

1. 安装

pip install wordcloud

2. 根据源码分析wordcloud的实现原理

总的来说,wordcloud做的是三件事:

(1) 文本预处理

(2) 词频统计

(3) 将高频词以图片形式进行彩色渲染

从上面的代码可以看到,用 wordcloud.generate(text) 就完成了这三项工作。

源码:

defgenerate(self, text):"""Generate wordcloud from text.

The input "text" is expected to be a natural text. If you pass a sorted

list of words, words will appear in your output twice. To remove this

duplication, set ``collocations=False``.

Alias to generate_from_text.

Calls process_text and generate_from_frequencies.

Returns

-------

self"""

returnself.generate_from_text(text)defgenerate_from_text(self, text):"""Generate wordcloud from text.

The input "text" is expected to be a natural text. If you pass a sorted

list of words, words will appear in your output twice. To remove this

duplication, set ``collocations=False``.

Calls process_text and generate_from_frequencies.

..versionchanged:: 1.2.2

Argument of generate_from_frequencies() is not return of

process_text() any more.

Returns

-------

self"""words=self.process_text(text)

self.generate_from_frequencies(words)return self

generate()和generate_from_text()

它的调用顺序是:

generate(self, text)=>self.generate_from_text(text)=>words=self.process_text(text)

self.generate_from_frequencies(words)

其中 process_text(text) 对应的是文本预处理和词频统计,而 generate_from_frequencies(words) 对应的是根据词频中生成词云。

(1) process_text(text) 主要是进行分词和去噪。

具体地,它做了以下操作:

检测文本编码

分词(根据规则进行tokenize)、保留单词字符(A-Za-z0-9_)和单引号(‘)、去除单字符

去除停用词

去除后缀(‘s) -- 针对英文

去除纯数字

统计一元和二元词频计数(unigrams_and_bigrams) -- 可选

返回的结果是一个字典 dict(string, int) ,表示的是分词后的token以及对应出现的次数。

这里有一些需要注意的地方,文章后面会再提到。

源码如下:

defprocess_text(self, text):"""Splits a long text into words, eliminates the stopwords.

Parameters

----------

text : string

The text to be processed.

Returns

-------

words : dict (string, int)

Word tokens with associated frequency.

..versionchanged:: 1.2.2

Changed return type from list of tuples to dict.

Notes

-----

There are better ways to do word tokenization, but I don‘t want to

include all those things."""stopwords= set([i.lower() for i inself.stopwords])

flags= (re.UNICODE if sys.version < ‘3‘ and type(text) isunicodeelse0)

regexp= self.regexp if self.regexp is not None else r"\w[\w‘]+"words=re.findall(regexp, text, flags)#remove stopwords

words = [word for word in words if word.lower() not instopwords]#remove ‘s

words = [word[:-2] if word.lower().endswith("‘s") elsewordfor word inwords]#remove numbers

words = [word for word in words if notword.isdigit()]ifself.collocations:

word_counts=unigrams_and_bigrams(words, self.normalize_plurals)else:

word_counts, _=process_tokens(words, self.normalize_plurals)return word_counts

def process_text(self, text)

(2) generate_from_frequencies(words) 主要是根据上一步的结果生成词云分布。

具体地,它做了以下操作:

对词计数结果进行排序,并归一化(normalized)到0~1之间,得到词频

创建图像并确定font_size初始值

给self.words_赋值,记录的是出现频率最高的前max_words个词,以及对应的归一化后的词频,即dict(token, normalized_frequency)

画出灰度图:词频越大,font_size越大;根据生成的随机数来决定字的水平/垂直方向

若随机数小于self.prefer_horizontal则为水平方向,否则为垂直方向;

如果空间不足,优先考虑旋转方向,其次考虑将字体变小

给self.layout_赋值,记录的是词和词频、字体大小、位置、方向、以及颜色,即list(zip(frequencies, font_sizes, positions, orientations, colors))

可以看到,这个函数的主要目的在于得到self.layout_的值,记录了要生成词云分布图所需要的信息。

后面wordcloud.to_file(filename)或者plt.imshow(wordcloud)会把结果以图像的形式呈现出来。其中to_file()函数就会先检测是否已经给self.layout_赋值,如果没有的话会报错。

源码如下:

def generate_from_frequencies(self, frequencies, max_font_size=None):"""Create a word_cloud from words and frequencies.

Parameters

----------

frequencies : dict from string to float

A contains words and associated frequency.

max_font_size : int

Use this font-size instead of self.max_font_size

Returns

-------

self"""

#make sure frequencies are sorted and normalized

frequencies = sorted(frequencies.items(), key=itemgetter(1), reverse=True)if len(frequencies) <=0:raise ValueError("We need at least 1 word to plot a word cloud,"

"got %d." %len(frequencies))

frequencies=frequencies[:self.max_words]#largest entry will be 1

max_frequency = float(frequencies[0][1])

frequencies= [(word, freq /max_frequency)for word, freq infrequencies]if self.random_state is notNone:

random_state=self.random_stateelse:

random_state=Random()if self.mask is notNone:

mask=self.mask

width= mask.shape[1]

height=mask.shape[0]if mask.dtype.kind == ‘f‘:

warnings.warn("mask image should be unsigned byte between 0"

"and 255. Got a float array")if mask.ndim == 2:

boolean_mask= mask == 255

elif mask.ndim == 3:#if all channels are white, mask out

boolean_mask = np.all(mask[:, :, :3] == 255, axis=-1)else:raise ValueError("Got mask of invalid shape: %s"

%str(mask.shape))else:

boolean_mask=None

height, width=self.height, self.width

occupancy=IntegralOccupancyMap(height, width, boolean_mask)#create image

img_grey = Image.new("L", (width, height))

draw=ImageDraw.Draw(img_grey)

img_array=np.asarray(img_grey)

font_sizes, positions, orientations, colors=[], [], [], []

last_freq= 1.if max_font_size isNone:#if not provided use default font_size

max_font_size =self.max_font_sizeif max_font_size isNone:#figure out a good font size by trying to draw with

#just the first two words

if len(frequencies) == 1:#we only have one word. We make it big!

font_size =self.heightelse:

self.generate_from_frequencies(dict(frequencies[:2]),

max_font_size=self.height)#find font sizes

sizes = [x[1] for x inself.layout_]try:

font_size= int(2 * sizes[0] * sizes[1]/ (sizes[0] + sizes[1]))#quick fix for if self.layout_ contains less than 2 values

#on very small images it can be empty

exceptIndexError:try:

font_size=sizes[0]exceptIndexError:raise ValueError(‘canvas size is too small‘)else:

font_size=max_font_size#we set self.words_ here because we called generate_from_frequencies

#above... hurray for good design?

self.words_ =dict(frequencies)#start drawing grey image

for word, freq infrequencies:#select the font size

rs =self.relative_scalingif rs !=0:

font_size= int(round((rs * (freq /float(last_freq))+ (1 - rs)) *font_size))if random_state.random()

orientation=Noneelse:

orientation=Image.ROTATE_90

tried_other_orientation=FalsewhileTrue:#try to find a position

font =ImageFont.truetype(self.font_path, font_size)#transpose font optionally

transposed_font =ImageFont.TransposedFont(

font, orientation=orientation)#get size of resulting text

box_size = draw.textsize(word, font=transposed_font)#find possible places using integral image:

result = occupancy.sample_position(box_size[1] +self.margin,

box_size[0]+self.margin,

random_state)if result is not None or font_size

break

#if we didn‘t find a place, make font smaller

#but first try to rotate!

if not tried_other_orientation and self.prefer_horizontal < 1:

orientation= (Image.ROTATE_90 if orientation is None elseImage.ROTATE_90)

tried_other_orientation=Trueelse:

font_size-=self.font_step

orientation=Noneif font_size

breakx, y= np.array(result) + self.margin // 2

#actually draw the text

draw.text((y, x), word, fill="white", font=transposed_font)

positions.append((x, y))

orientations.append(orientation)

font_sizes.append(font_size)

colors.append(self.color_func(word, font_size=font_size,

position=(x, y),

orientation=orientation,

random_state=random_state,

font_path=self.font_path))#recompute integral image

if self.mask isNone:

img_array=np.asarray(img_grey)else:

img_array= np.asarray(img_grey) +boolean_mask#recompute bottom right

#the order of the cumsum‘s is important for speed ?!

occupancy.update(img_array, x, y)

last_freq=freq

self.layout_=list(zip(frequencies, font_sizes, positions,

orientations, colors))return self

def generate_from_frequencies(self, frequencies, max_font_size=None)

3. 应用到中文语料应该要注意的点

wordcloud包是由Andreas Mueller在2015-03-20发布1.0.0版本,现在最新的是2018-03-13发布的1.4.1版本。

英文语料可以直接输入到wordcloud中,但是对于中文语料,仅仅用wordcloud不能直接生成中文词云图。

原因:

英文单词以空格分隔,而我们从前面process_text(text)看到源码中是直接用正则表达式(默认为r"\w[\w‘]+")进行处理:

In : re.findall(r"\w[\w‘]+", "It‘s Monday today.")

Out: ["It‘s", ‘Monday‘, ‘today‘]

但是中文里面词与词之间一般不用字符分隔:

In : re.findall(r"\w[\w‘]+", "今天天气不错,蓝天白云,还有温暖的阳光 哈 哈哈")

Out: [‘今天天气不错‘, ‘蓝天白云‘, ‘还有温暖的阳光‘, ‘哈哈‘]

可以看出,原生的wordcloud是为英文服务的,去除标点符号(单符号‘除外)并分割成token;

而应用到中文语料上的时候,注意要先分好词,再用空格分隔连接成字符串,最后输入到wordcloud。

另外要注意的是,无论是对英文还是中文,默认是把单字符剔除掉(因为 regexp = self.regexp if self.regexp is not None else r"\w[\w‘]+" ),如果想要保留单字符,将regexp参数讲表达式设置为 r"\w[\w‘]*" 即可。

from wordcloud importWordCloudfrom scipy.misc importimreaddef generate_wordcloud(text, max_words=200, pic_path=None):"""生成词云

:param text: 一段以空格为间断的字符串

:param max_words: 词数目上限

:param pic_path: 输出图片路径

:return:"""mk= imread("tuoyuan.jpg")

wc= WordCloud(font_path="/usr/share/fonts/myfonts/msyh.ttf", background_color="white", max_words=max_words,

mask=mk, width=1000, height=500, max_font_size=100, prefer_horizontal=0.95, collocations=False)

wc.generate(text=text)ifpic_path:

wc.to_file(pic_path)else:

plt.imshow(wc)

plt.axis("off")

plt.show()returnwc.words_def run_wordcloud(corpus, max_words, pic_path=None):

text= " ".join([" ".join(line) for line in corpus]) #将分词后的结果用空格连接

word2weight = generate_wordcloud(text=text, max_words=max_words, pic_path=pic_path)

word2weight_sorted= sorted(word2weight.items(), key=lambda x: x[1], reverse=True)

logging.info([(k, float("%.5f" % v)) for k, v in word2weight_sorted])

4. 重写代码

用词云是为了直观地看语料的关键信息,在本人的实际工作应用中,主要目的在于获取关键信息,而不太关注界面的呈现方式。

所以在了解wordcloud源码实现原理之后,决定自己用代码实现。

一方面,使得代码的实现更公开透明,在效率相当的情况下尽量避免使用第三方库,效果可控,甚至还可以提升效率;

另一方面,能结合实际情况更灵活地处理问题。

针对中文的预处理,可以和分词结合一起完成。这里主要进行:分词和词性标注、小写化、去停用词、去数字、去单字符、以及保留指定词性。

importjiebaimportjieba.posseg as psegclassUtils(object):def __init__(self, utils_data=None):

self.stopwords=self.init_utils(utils_data)

self.pos_save={"n", "an", "Ng", "nr", "ns", "nt", "nz", "vn", "un", #名

"v", "vg", "vd", #动

"a", "ag", "ad", #形

"j", "l", "i", "z", "b", "g", "s", "h", #j简称略语、l习用语、i成语、z状态词、b区别词、g语素、s处所词、h前接成分

"zg", "eng","x"} #未知(自定义词)

def_init_utils(self, utils_data):for wd in utils_data["user_dict"]:

jieba.add_word(wd)return set(utils_data["stopwords"])def _token_filter(self, token): #去停用词; 去数字; 去单字

return token not in self.stopwords and not token.isdigit() and len(token) >= 2

def _token_filter_with_flag(self, pair_word_flag): #保留指定词性

return self.token_filter(pair_word_flag.word) and pair_word_flag.flag inself.pos_savedefcut(self, text):return list(filter(self._token_filter, list(jieba.cut(text.lower())))) #分词; 小写化;

defcut_with_flag(self, text):

pairs= list(filter(self._token_filter_with_flag, list(pseg.cut(text.lower())))) #分词和词性标注; 小写化;

return [p.word for p in pairs]

做完文本分词和其它预处理之后,直接统计词及对应的出现次数即可。为了更直观,这里输出的是词计数,而不是归一化后的词频。排序结果与wordcloud等同。

def word_count(corpus, n_gram=1, n=None):

counter=Counter()if n_gram == 1:for line incorpus:

counter.update(line)elif n_gram == 2:for line incorpus:

size=len(line)

counter.update(["%s_%s" % (line[idx], line[idx + 1]) for idx in range(size) if idx + 1 < size]) #有序

else:

logging.info("[Error] Invalid value of param n_gram: %s (only 1 or 2 accepted)" %n_gram)return counter.most_common(n=n)

另外还可以统计高频词的共现情况、把高频词/词共现反向映射到对应的句子等等,便于从高频词层面到高频句子类型层面的归纳。

参考:

https://pypi.org/project/wordcloud/

https://github.com/amueller/word_cloud

http://python.jobbole.com/87496/

https://www.jianshu.com/p/ead991a08563

https://blog.csdn.net/qq_34739497/article/details/78285972

https://www.cnblogs.com/sunnyeveryday/p/7043399.html

https://www.cnblogs.com/naraka/p/8992058.html

https://www.cnblogs.com/franklv/p/6995150.html

https://blog.csdn.net/Tang_Chuanlin/article/details/79862505

python词云需要导入什么包_[python] 词云:wordcloud包的安装、使用、原理(源码分析)、中文词云生成、代码重写...相关推荐

  1. eureka 之前的服务如何关闭_干货分享 | 服务注册中心Spring Cloud Eureka部分源码分析...

    友情提示:全文13000多文字,预计阅读时间10-15分钟 Spring Cloud Eureka作为常用的服务注册中心,我们有必要去了解其内在实现机制,这样出现问题的时候我们可以快速去定位问题.当我 ...

  2. ZooKeeper面试题(2020最新版,springmvc源码分析pdf百度云

    10. ACL 权限控制机制 UGO(User/Group/Others) 目前在 Linux/Unix 文件系统中使用,也是使用最广泛的权限控制方式.是一种粗粒度的文件系统权限控制模式. ACL(A ...

  3. java观察者模式类图_设计模式(十八)——观察者模式(JDK Observable源码分析)...

    1 天气预报项目需求,具体要求以下: 1) 气象站能够将天天测量到的温度,湿度,气压等等以公告的形式发布出去(好比发布到本身的网站或第三方).java 2) 须要设计开放型 API,便于其余第三方也能 ...

  4. dll 源码_【技术分享】 | 一个JAVA内存马的源码分析

    前言 偶然接触到了这样一个JAVA内存马,其作者也是冰蝎的作者,项目地址: https://github.com/rebeyond/memShell 正好最近在接触JAVA,借此机会学习下大佬的代码, ...

  5. java web开源项目源码_超赞!推荐一个专注于Java后端源码分析的Github项目!

    大家好,最近有小伙伴们建议我把源码分析文章及源码分析项目(带注释版)放到github上,这样小伙伴们就可以把带中文注释的源码项目下载到自己本地电脑,结合源码分析文章自己本地调试,总之对于学习开源项目源 ...

  6. java类加载机制为什么双亲委派_[五]类加载机制双亲委派机制 底层代码实现原理 源码分析 java类加载双亲委派机制是如何实现的...

    Launcher启动类 本文是双亲委派机制的源码分析部分,类加载机制中的双亲委派模型对于jvm的稳定运行是非常重要的不过源码其实比较简单,接下来简单介绍一下我们先从启动类说起有一个Launcher类 ...

  7. 阿里云image-syncer源码分析

    阿里云image-syncer源码分析 欢迎关注"云原生手记"微信公众号 背景 大家在公司中都会使用到容器镜像私有仓库,一般都用harbor,也有会用registry搭建一个简陋的 ...

  8. python生成中文词云的代码_[python] 基于词云的关键词提取:wordcloud的使用、源码分析、中文词云生成和代码重写...

    1. 词云简介 词云,又称文字云.标签云,是对文本数据中出现频率较高的"关键词"在视觉上的突出呈现,形成关键词的渲染形成类似云一样的彩色图片,从而一眼就可以领略文本数据的主要表达意 ...

  9. 【Android】条形码/二维码扫描——ZXing源码分析及相关jar包导入

    转载自:http://blog.csdn.net/u010574567/article/details/51916604 *********************1.源码分析************ ...

最新文章

  1. 开发商微信选房后不退认筹金_认筹金贸然转定金退不回 购房人认栽?
  2. github上fork原项目,如何将本地仓库代码更新到最新版本?
  3. mysql signal函数_使用signal函数
  4. Java设计模式6:策略模式
  5. filter java oauth_java – 带有自定义安全过滤器的Spring Boot OAuth2
  6. PolarDB-X 云原生分布式数据库 > API参考 > API参考(2017版本) > 数据库管理类 API > 删除 DRDS 数据库
  7. MFC工作笔记0005---::在vc++中是什么意思
  8. SAP License:市场需要双重SAP顾问
  9. 算法:剑指 Offer 06. 从尾到头打印链表
  10. Word论文排版教程
  11. 马尔可夫不等式 Markov's inequality
  12. 把mov格式的视频转换mp4步骤
  13. 猫和计算机连接网络,猫、路由器和网卡的连接教程[图文]
  14. Pygame实战之外星人入侵NO.7——大批外星人来袭
  15. sb 讲解 (!(~+[])+{})[--[~+][+[]]*[~+[]] + ~~!+[]]+({}+[])[[~!+[]]*~+[]]
  16. mybatis 查询出的日期没有时分秒
  17. OSChina 周四乱弹 ——00后让别人给自己网购女朋友
  18. ASP + Serv-u 實現FTP的代碼
  19. anaconda-navigaotr出现adding featured channels并无法正常打开_CodingPark编程公园
  20. 【Dive into Deep Learning / 动手学深度学习】第二章 - 第一节:数据操作

热门文章

  1. 马上步入社会了,去工作
  2. 数据科学家必须符合道德吗
  3. Unity UGUI——开源
  4. 心律失常患者要如何饮食?小编给出了这5个饮食小建议!
  5. CCleaner软件系统要求说明
  6. 操作系统第十讲——调度的概念、层次
  7. 社会消防安全培训之火场疏散逃生互动演练系统
  8. python threading setdaemon_Python线程为什么搞个setDaemon
  9. LeetCode 862 题解
  10. mybatis面试题及回答