python生成词云图
python生成词云图
前言
python版本号:3.6.3
在网上看到词云图,一直觉得很有意思,最近没工作很空闲,就想着自己做一做
先放个效果图,这是用杰伦的三首歌《七里香》、《搁浅》、《借口》歌词文本做成的词云图。
python modules
需要使用的python包有:
wordcloud、jieba、numpy、cv2
使用pip安装即可
pip install wordcloud pip install jieba pip install numpy pip install opencv-python
wordcloud参数说明
代码核心是wordcloud部分,下面列一下wordcloud的参数
font_path : string #字体路径,如:font_path = '黑体.ttf' width : int (default=400) #输出的图像宽度,默认为400像素 height : int (default=200) #输出的图像高度,默认为200像素 prefer_horizontal : float (default=0.90) #词语水平方向排版出现的频率,默认 0.9 (所以词语垂直方向排版出现频率为 0.1 ) mask : nd-array or None (default=None) #背景布,除去纯白部分都会用来填充词云 scale : float (default=1) #按照比例进行放大画布,如设置为1.5,则长和宽都是原来画布的1.5倍 min_font_size : int (default=4) #显示的最小的字体大小 max_font_size : int or None (default=None) #显示的最大的字体大小 font_step : int (default=1) #字体步长,如果步长大于1,会加快运算但是可能导致结果出现较大的误差 max_words : number (default=200) #要显示的词的最大个数 stopwords : set of strings or None #设置需要屏蔽的词,如果为空,则使用内置的STOPWORDS。很多文章都没有说清楚要怎么写屏蔽词,其实就是一个list。 background_color : color value (default=”black”) #背景颜色,如background_color='white',背景颜色为白色 mode : string (default=”RGB”) #当参数为“RGBA”并且background_color不为空时,背景为透明 relative_scaling : float (default=.5) #词频和字体大小的关联性 color_func : callable, default=None #生成新颜色的函数,如果为空,则使用 self.color_func regexp : string or None (optional) #使用正则表达式分隔输入的文本 collocations : bool, default=True #是否包括两个词的搭配 colormap : string or matplotlib colormap, default=”viridis” #给每个单词随机分配颜色,若指定color_func,则忽略该方法 random_state : int or None #为每个单词返回一个PIL颜色
wordcloud输出形式
fit_words(frequencies) #根据词频生成词云 generate(text) #根据文本生成词云 generate_from_frequencies(frequencies[, ...]) #根据词频生成词云 generate_from_text(text) #根据文本生成词云 process_text(text) #将长文本分词并去除屏蔽词(此处指英语,中文分词还是需要自己用别的库先行实现,使用上面的 fit_words(frequencies) ) recolor([random_state, color_func, colormap]) #对现有输出重新着色。重新上色会比重新生成整个词云快很多 to_array() #转化为 numpy array to_file(filename) #输出到文件
jieba模块的使用
jieba是中文分词第三方库
一共有三种模式:精确模式、全模式、搜索引擎模式
精确模式下,把文本精确切分,不存在重复词语
全模式,文本中所有可能词语都扫描出来,存在重复
搜索引擎模式,基于精确模式,对长词再次切分
方法 | 含义 |
---|---|
jieba.cut(string) | 精确模式,返回一个生成器generator |
jieba.cut(string,cut_all=True) | 全模式,输出文本中所有词语,返回一个生成器generator |
jieba.cut_for_search(string) | 搜索引擎模式,返回一个生成器generator |
jieba.lcut(string) | 精确模式,返回一个列表模式 |
jieba.lcut(string,cut_all=True) | 全模式,返回一个列表类型 |
jieba.lcut_for_search(string) | 搜索引擎模式,返回列表类型 |
jieba.add_word(word) | 向分词词典中添加新词 |
python中list容量有限,因此引入了生成器generator的概念,generator保存的是算法,list是[],generator是()。
可用for循环打印generator:
for i in generator: print(i)
代码演示
以下代码是一个示例,并不是生成上面示例图片的代码
# -*- coding: utf-8 -*- from wordcloud import WordCloud import jieba import cv2 import numpy as np def read_word(text_path): with open(text_path,'r',encoding='utf-8') as f: cut_word = jieba.cut(f.read()) result = " ".join(cut_word) img = cv2.imread('bg_java.png') img_array = np.array(img) stop_words = ["开发","工作","优先","具有","要求","公司","PHP","参与","熟悉","任职"] wc = WordCloud( font_path='simhei.ttf', #字体 background_color='white', #背景颜色 width=1000, height=600, max_font_size=80, min_font_size=10, mask = img_array, #背景图片 max_words=1000, stopwords=stop_words ) wc.generate_from_text(result) wc.to_file('job_java.png') if __name__ == '__main__': read_word("job.txt")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎 反馈!
该文章没有解决你所遇到的问题? 点击提问,说说你的问题,让更多的人一起探讨吧!