电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

Python 制作詞云的WordCloud參數(shù)用法說明

瀏覽:7日期:2022-06-26 08:31:10
場景

官方API:

https://amueller.github.io/word_cloud/generated/wordcloud.WordCloud.html

實(shí)現(xiàn)

font_path : string #字體路徑,需要展現(xiàn)什么字體就把該字體路徑+后綴名寫上,如:font_path = ’黑體.ttf’ width : int (default=400) #輸出的畫布寬度,默認(rèn)為400像素 height : int (default=200) #輸出的畫布高度,默認(rèn)為200像素 prefer_horizontal : float (default=0.90) #詞語水平方向排版出現(xiàn)的頻率,默認(rèn) 0.9 (所以詞語垂直方向排版出現(xiàn)頻率為 0.1 )mask : nd-array or None (default=None) #如果參數(shù)為空,則使用二維遮罩繪制詞云。如果 mask 非空,設(shè)置的寬高值將被忽略,遮罩形狀被 mask 取代。除全白(#FFFFFF)的部分將不會繪制,其余部分會用于繪制詞云。如:bg_pic = imread(’讀取一張圖片.png’),背景圖片的畫布一定要設(shè)置為白色(#FFFFFF),然后顯示的形狀為不是白色的其他顏色??梢杂胮s工具將自己要顯示的形狀復(fù)制到一個(gè)純白色的畫布上再保存,就ok了。scale : float (default=1) #按照比例進(jìn)行放大畫布,如設(shè)置為1.5,則長和寬都是原來畫布的1.5倍 min_font_size : int (default=4) #顯示的最小的字體大小 font_step : int (default=1) #字體步長,如果步長大于1,會加快運(yùn)算但是可能導(dǎo)致結(jié)果出現(xiàn)較大的誤差 max_words : number (default=200) #要顯示的詞的最大個(gè)數(shù) stopwords : set of strings or None #設(shè)置需要屏蔽的詞,如果為空,則使用內(nèi)置的STOPWORDS background_color : color value (default=”black”) #背景顏色,如background_color=’white’,背景顏色為白色 max_font_size : int or None (default=None) #顯示的最大的字體大小 mode : string (default=”RGB”) #當(dāng)參數(shù)為“RGBA”并且background_color不為空時(shí),背景為透明 relative_scaling : float (default=.5) #詞頻和字體大小的關(guān)聯(lián)性 color_func : callable, default=None #生成新顏色的函數(shù),如果為空,則使用 self.color_func regexp : string or None (optional) #使用正則表達(dá)式分隔輸入的文本 collocations : bool, default=True #是否包括兩個(gè)詞的搭配 colormap : string or matplotlib colormap, default=”viridis” #給每個(gè)單詞隨機(jī)分配顏色,若指定color_func,則忽略該方法 random_state : int or None #為每個(gè)單詞返回一個(gè)PIL顏色 fit_words(frequencies) #根據(jù)詞頻生成詞云generate(text) #根據(jù)文本生成詞云generate_from_frequencies(frequencies[, ...]) #根據(jù)詞頻生成詞云generate_from_text(text) #根據(jù)文本生成詞云process_text(text) #將長文本分詞并去除屏蔽詞(此處指英語,中文分詞還是需要自己用別的庫先行實(shí)現(xiàn),使用上面的 fit_words(frequencies) )recolor([random_state, color_func, colormap]) #對現(xiàn)有輸出重新著色。重新上色會比重新生成整個(gè)詞云快很多to_array() #轉(zhuǎn)化為 numpy arrayto_file(filename) #輸出到文件

補(bǔ)充:生成詞云之python中WordCloud包的用法

效果圖:

Python 制作詞云的WordCloud參數(shù)用法說明

這是python中使用wordcloud包生成的詞云圖。

下面來介紹一下wordcloud包的基本用法

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)

這是wordcloud的所有參數(shù),下面具體介紹一下各個(gè)參數(shù):

font_path : string //字體路徑,需要展現(xiàn)什么字體就把該字體路徑+后綴名寫上,如:font_path = ’黑體.ttf’width : int (default=400) //輸出的畫布寬度,默認(rèn)為400像素height : int (default=200) //輸出的畫布高度,默認(rèn)為200像素prefer_horizontal : float (default=0.90) //詞語水平方向排版出現(xiàn)的頻率,默認(rèn) 0.9 (所以詞語垂直方向排版出現(xiàn)頻率為 0.1 )mask : nd-array or None (default=None) //如果參數(shù)為空,則使用二維遮罩繪制詞云。如果 mask 非空,設(shè)置的寬高值將被忽略,遮罩形狀被 mask 取代。除全白(#FFFFFF)的部分將不會繪制,其余部分會用于繪制詞云。如:bg_pic = imread(’讀取一張圖片.png’),背景圖片的畫布一定要設(shè)置為白色(#FFFFFF),然后顯示的形狀為不是白色的其他顏色??梢杂胮s工具將自己要顯示的形狀復(fù)制到一個(gè)純白色的畫布上再保存,就ok了。scale : float (default=1) //按照比例進(jìn)行放大畫布,如設(shè)置為1.5,則長和寬都是原來畫布的1.5倍。min_font_size : int (default=4) //顯示的最小的字體大小font_step : int (default=1) //字體步長,如果步長大于1,會加快運(yùn)算但是可能導(dǎo)致結(jié)果出現(xiàn)較大的誤差。max_words : number (default=200) //要顯示的詞的最大個(gè)數(shù)stopwords : set of strings or None //設(shè)置需要屏蔽的詞,如果為空,則使用內(nèi)置的STOPWORDSbackground_color : color value (default=”black”) //背景顏色,如background_color=’white’,背景顏色為白色。max_font_size : int or None (default=None) //顯示的最大的字體大小mode : string (default=”RGB”) //當(dāng)參數(shù)為“RGBA”并且background_color不為空時(shí),背景為透明。relative_scaling : float (default=.5) //詞頻和字體大小的關(guān)聯(lián)性color_func : callable, default=None //生成新顏色的函數(shù),如果為空,則使用 self.color_funcregexp : string or None (optional) //使用正則表達(dá)式分隔輸入的文本collocations : bool, default=True //是否包括兩個(gè)詞的搭配colormap : string or matplotlib colormap, default=”viridis” //給每個(gè)單詞隨機(jī)分配顏色,若指定color_func,則忽略該方法。fit_words(frequencies) //根據(jù)詞頻生成詞云generate(text) //根據(jù)文本生成詞云generate_from_frequencies(frequencies[, ...]) //根據(jù)詞頻生成詞云generate_from_text(text) //根據(jù)文本生成詞云process_text(text) //將長文本分詞并去除屏蔽詞(此處指英語,中文分詞還是需要自己用別的庫先行實(shí)現(xiàn),使用上面的 fit_words(frequencies) )recolor([random_state, color_func, colormap]) //對現(xiàn)有輸出重新著色。重新上色會比重新生成整個(gè)詞云快很多。to_array() //轉(zhuǎn)化為 numpy arrayto_file(filename) //輸出到文件例子:

想要生成的詞云的形狀:

Python 制作詞云的WordCloud參數(shù)用法說明

圖中黑色部分就是詞云的將要顯示的部分,白色部分不顯示任何詞。

下面是一個(gè)文本文檔:

How the Word Cloud Generator Works

The layout algorithm for positioning words without overlap is available on GitHub under an open source license as d3-cloud. Note that this is the only the layout algorithm and any code for converting text into words and rendering the final output requires additional development.

As word placement can be quite slow for more than a few hundred words, the layout algorithm can be run asynchronously, with a configurable time step size. This makes it possible to animate words as they are placed without stuttering. It is recommended to always use a time step even without animations as it prevents the browser’s event loop from blocking while placing the words.

The layout algorithm itself is incredibly simple. For each word, starting with the most “important”:

Attempt to place the word at some starting point: usually near the middle, or somewhere on a central horizontal line. If the word intersects with any previously placed words, move it one step along an increasing spiral. Repeat until no intersections are found. The hard part is making it perform efficiently! According to Jonathan Feinberg, Wordle uses a combination of hierarchical bounding boxes and quadtrees to achieve reasonable speeds.

Glyphs in JavaScript

There isn’t a way to retrieve precise glyph shapes via the DOM, except perhaps for SVG fonts. Instead, we draw each word to a hidden canvas element, and retrieve the pixel data.

Retrieving the pixel data separately for each word is expensive, so we draw as many words as possible and then retrieve their pixels in a batch operation.

Sprites and Masks

My initial implementation performed collision detection using sprite masks. Once a word is placed, it doesn’t move, so we can copy it to the appropriate position in a larger sprite representing the whole placement area.

The advantage of this is that collision detection only involves comparing a candidate sprite with the relevant area of this larger sprite, rather than comparing with each previous word separately.

Somewhat surprisingly, a simple low-level hack made a tremendous difference: when constructing the sprite I compressed blocks of 32 1-bit pixels into 32-bit integers, thus reducing the number of checks (and memory) by 32 times.

In fact, this turned out to beat my hierarchical bounding box with quadtree implementation on everything I tried it on (even very large areas and font sizes). I think this is primarily because the sprite version only needs to perform a single collision test per candidate area, whereas the bounding box version has to compare with every other previously placed word that overlaps slightly with the candidate area.

Another possibility would be to merge a word’s tree with a single large tree once it is placed. I think this operation would be fairly expensive though compared with the analagous sprite mask operation, which is essentially ORing a whole block.

從這個(gè)文本中生成一個(gè)詞云,代碼如下:

#!/usr/bin/python# -*- coding: utf-8 -*-#coding=utf-8#導(dǎo)入wordcloud模塊和matplotlib模塊from wordcloud import WordCloudimport matplotlib.pyplot as pltfrom scipy.misc import imread#讀取一個(gè)txt文件text = open(’test.txt’,’r’).read()#讀入背景圖片bg_pic = imread(’3.png’)#生成詞云wordcloud = WordCloud(mask=bg_pic,background_color=’white’,scale=1.5).generate(text)image_colors = ImageColorGenerator(bg_pic)#顯示詞云圖片plt.imshow(wordcloud)plt.axis(’off’)plt.show()#保存圖片wordcloud.to_file(’test.jpg’)

運(yùn)行結(jié)果:

Python 制作詞云的WordCloud參數(shù)用法說明

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: python
相關(guān)文章:
主站蜘蛛池模板: 透平油真空滤油机-变压器油板框滤油机-滤油车-华之源过滤设备 | 钢托盘,铁托盘,钢制托盘,镀锌托盘,饲料托盘,钢托盘制造商-南京飞天金属13260753852 | 正压送风机-多叶送风口-板式排烟口-德州志诺通风设备 | 拉力机-万能试验机-材料拉伸试验机-电子拉力机-拉力试验机厂家-冲击试验机-苏州皖仪实验仪器有限公司 | 广州冷却塔维修厂家_冷却塔修理_凉水塔风机电机填料抢修-广东康明节能空调有限公司 | 蓝牙音频分析仪-多功能-四通道-八通道音频分析仪-东莞市奥普新音频技术有限公司 | 济南侦探调查-济南调查取证-山东私家侦探-山东白豹调查咨询公司 密集架|电动密集架|移动密集架|黑龙江档案密集架-大量现货厂家销售 | 企典软件一站式企业管理平台,可私有、本地化部署!在线CRM客户关系管理系统|移动办公OA管理系统|HR人事管理系统|人力 | 密度电子天平-内校-外校电子天平-沈阳龙腾电子有限公司 | 医学动画公司-制作3d医学动画视频-医疗医学演示动画制作-医学三维动画制作公司 | 焦作网 WWW.JZRB.COM| 热回收盐水机组-反应釜冷水机组-高低温冷水机组-北京蓝海神骏科技有限公司 | 净气型药品柜-试剂柜-无管道净气型通风柜-苏州毕恩思 | 高柔性拖链电缆_卷筒电缆_耐磨耐折聚氨酯电缆-玖泰特种电缆 | 绿叶|绿叶投资|健康产业_绿叶投资集团有限公司 | 工业风机_环保空调_冷风机_工厂车间厂房通风降温设备旺成服务平台 | 聚合氯化铝厂家-聚合氯化铝铁价格-河南洁康环保科技 | 振动筛-交叉筛-螺旋筛-滚轴筛-正弦筛-方形摇摆筛「新乡振动筛厂家」 | 除甲醛公司-甲醛检测治理-杭州创绿家环保科技有限公司-室内空气净化十大品牌 | 河南卓美创业科技有限公司-河南卓美防雷公司-防雷接地-防雷工程-重庆避雷针-避雷器-防雷检测-避雷带-避雷针-避雷塔、机房防雷、古建筑防雷等-山西防雷公司 | 储气罐,真空罐,缓冲罐,隔膜气压罐厂家批发价格,空压机储气罐规格型号-上海申容压力容器集团有限公司 | 超声波气象站_防爆气象站_空气质量监测站_负氧离子检测仪-风途物联网 | 防爆电机-高压防爆电机-ybx4电动机厂家-河南省南洋防爆电机有限公司 | 气动隔膜泵-电动隔膜泵-循环热水泵-液下排污/螺杆/管道/化工泵「厂家」浙江绿邦 | 手持式浮游菌采样器-全排二级生物安全柜-浙江孚夏医疗科技有限公司 | 水篦子|雨篦子|镀锌格栅雨水篦子|不锈钢排水篦子|地下车库水箅子—安平县云航丝网制品厂 | China plate rolling machine manufacturer,cone rolling machine-Saint Fighter | 不锈钢法兰-碳钢法兰-法兰盘生产加工厂家-[鼎捷峰]-不锈钢法兰-碳钢法兰-法兰盘生产加工厂家-[鼎捷峰] | 工控机,嵌入式主板,工业主板,arm主板,图像采集卡,poe网卡,朗锐智科 | 高硼硅玻璃|水位计玻璃板|光学三棱镜-邯郸奥维玻璃科技有限公司 高温高压釜(氢化反应釜)百科 | 立式矫直机_卧式矫直机-无锡金矫机械制造有限公司 | 分类168信息网 - 分类信息网 免费发布与查询 | 影像测量仪_三坐标测量机_一键式二次元_全自动影像测量仪-广东妙机精密科技股份有限公司 | 佛山市钱丰金属不锈钢蜂窝板定制厂家|不锈钢装饰线条|不锈钢屏风| 电梯装饰板|不锈钢蜂窝板不锈钢工艺板材厂家佛山市钱丰金属制品有限公司 | Safety light curtain|Belt Sway Switches|Pull Rope Switch|ultrasonic flaw detector-Shandong Zhuoxin Machinery Co., Ltd | 合肥注册公司|合肥代办营业执照、2024注册公司流程 | 解放卡车|出口|济南重汽|报价大全|山东三维商贸有限公司 | 哈尔滨发电机,黑龙江柴油发电机组-北方星光 | 低温柔性试验仪-土工布淤堵-沥青车辙试验仪-莱博特(天津)试验机有限公司 | 宁夏活性炭_防护活性炭_催化剂载体炭-宁夏恒辉活性炭有限公司 | 丙烷/液氧/液氮气化器,丙烷/液氧/液氮汽化器-无锡舍勒能源科技有限公司 |