日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

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

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

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

官方API:

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

實現(xiàn)

font_path : string #字體路徑,需要展現(xiàn)什么字體就把該字體路徑+后綴名寫上,如:font_path = ’黑體.ttf’ width : int (default=400) #輸出的畫布寬度,默認為400像素 height : int (default=200) #輸出的畫布高度,默認為200像素 prefer_horizontal : float (default=0.90) #詞語水平方向排版出現(xià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),然后顯示的形狀為不是白色的其他顏色。可以用ps工具將自己要顯示的形狀復制到一個純白色的畫布上再保存,就ok了。scale : float (default=1) #按照比例進行放大畫布,如設(shè)置為1.5,則長和寬都是原來畫布的1.5倍 min_font_size : int (default=4) #顯示的最小的字體大小 font_step : int (default=1) #字體步長,如果步長大于1,會加快運算但是可能導致結(jié)果出現(xiàn)較大的誤差 max_words : number (default=200) #要顯示的詞的最大個數(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”) #當參數(shù)為“RGBA”并且background_color不為空時,背景為透明 relative_scaling : float (default=.5) #詞頻和字體大小的關(guān)聯(lián)性 color_func : callable, default=None #生成新顏色的函數(shù),如果為空,則使用 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顏色 fit_words(frequencies) #根據(jù)詞頻生成詞云generate(text) #根據(jù)文本生成詞云generate_from_frequencies(frequencies[, ...]) #根據(jù)詞頻生成詞云generate_from_text(text) #根據(jù)文本生成詞云process_text(text) #將長文本分詞并去除屏蔽詞(此處指英語,中文分詞還是需要自己用別的庫先行實現(xiàn),使用上面的 fit_words(frequencies) )recolor([random_state, color_func, colormap]) #對現(xiàn)有輸出重新著色。重新上色會比重新生成整個詞云快很多to_array() #轉(zhuǎn)化為 numpy arrayto_file(filename) #輸出到文件

補充:生成詞云之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ù),下面具體介紹一下各個參數(shù):

font_path : string //字體路徑,需要展現(xiàn)什么字體就把該字體路徑+后綴名寫上,如:font_path = ’黑體.ttf’width : int (default=400) //輸出的畫布寬度,默認為400像素height : int (default=200) //輸出的畫布高度,默認為200像素prefer_horizontal : float (default=0.90) //詞語水平方向排版出現(xià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工具將自己要顯示的形狀復制到一個純白色的畫布上再保存,就ok了。scale : float (default=1) //按照比例進行放大畫布,如設(shè)置為1.5,則長和寬都是原來畫布的1.5倍。min_font_size : int (default=4) //顯示的最小的字體大小font_step : int (default=1) //字體步長,如果步長大于1,會加快運算但是可能導致結(jié)果出現(xiàn)較大的誤差。max_words : number (default=200) //要顯示的詞的最大個數(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”) //當參數(shù)為“RGBA”并且background_color不為空時,背景為透明。relative_scaling : float (default=.5) //詞頻和字體大小的關(guān)聯(lián)性color_func : callable, default=None //生成新顏色的函數(shù),如果為空,則使用 self.color_funcregexp : string or None (optional) //使用正則表達式分隔輸入的文本collocations : bool, default=True //是否包括兩個詞的搭配colormap : string or matplotlib colormap, default=”viridis” //給每個單詞隨機分配顏色,若指定color_func,則忽略該方法。fit_words(frequencies) //根據(jù)詞頻生成詞云generate(text) //根據(jù)文本生成詞云generate_from_frequencies(frequencies[, ...]) //根據(jù)詞頻生成詞云generate_from_text(text) //根據(jù)文本生成詞云process_text(text) //將長文本分詞并去除屏蔽詞(此處指英語,中文分詞還是需要自己用別的庫先行實現(xiàn),使用上面的 fit_words(frequencies) )recolor([random_state, color_func, colormap]) //對現(xiàn)有輸出重新著色。重新上色會比重新生成整個詞云快很多。to_array() //轉(zhuǎn)化為 numpy arrayto_file(filename) //輸出到文件例子:

想要生成的詞云的形狀:

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

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

下面是一個文本文檔:

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.

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

#!/usr/bin/python# -*- coding: utf-8 -*-#coding=utf-8#導入wordcloud模塊和matplotlib模塊from wordcloud import WordCloudimport matplotlib.pyplot as pltfrom scipy.misc import imread#讀取一個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’)

運行結(jié)果:

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

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

標簽: python
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
日本久久综合| 不卡在线一区二区| 蜜桃视频一区二区| 视频一区视频二区中文字幕| 欧美日韩黑人| 伊人久久大香伊蕉在人线观看热v| 一本色道精品久久一区二区三区| 99国产精品99久久久久久粉嫩| 91精品一区二区三区综合| 欧美精品资源| 久久av在线| 欧美一区91| 美女视频黄免费的久久| 久草免费在线视频| 在线亚洲精品| 日韩高清二区| 日韩国产在线| 日韩三区四区| 中文在线а√天堂| 日韩精品一区第一页| 日韩极品在线观看| 日韩精品1区| 亚洲欧美网站在线观看| 久久影视三级福利片| 国产在线观看www| 免费人成网站在线观看欧美高清| 久久99久久人婷婷精品综合| 成人羞羞在线观看网站| 日韩手机在线| 亚洲午夜视频| 国产伦精品一区二区三区视频| 日韩在线观看| 欧美一级二级视频| 亚洲色诱最新| 日韩国产在线| 蜜桃精品视频| 免费高清在线一区| 999精品色在线播放| 国产精品一页| 一区在线观看| 欧美三级网址| 美女精品视频在线| 欧美一区二区三区免费看| 午夜精品一区二区三区国产| 麻豆精品少妇| 欧美日韩一区二区三区四区在线观看| 亚洲黑丝一区二区| 黑森林国产精品av| 国产一区2区在线观看| 婷婷亚洲成人| 六月天综合网| 日韩视频二区| 国产主播一区| 欧美~级网站不卡| 国产一区二区三区久久| 国产精品视频首页| 欧美偷窥清纯综合图区| 亚洲区第一页| 亚洲欧洲国产精品一区| 在线一区二区三区视频| 视频一区二区不卡| 免费美女久久99| 午夜久久av | 欧美一区在线观看视频| 日韩欧美中文在线观看| 亚洲欧洲免费| 国产精品亚洲欧美一级在线| 日韩一区二区三免费高清在线观看| 美女精品在线| 亚洲v天堂v手机在线| 国产午夜久久av| 丁香婷婷久久| 午夜久久福利| 免费观看久久久4p| 另类欧美日韩国产在线| 天堂8中文在线最新版在线| 欧美日韩精品一区二区视频| 丝袜脚交一区二区| 麻豆一区二区三区| 亚洲精品国产嫩草在线观看| 999在线观看精品免费不卡网站| 中文字幕日韩亚洲| 你懂的国产精品| 欧美日韩视频网站| 亚洲精品韩国| 欧美天堂视频| 日韩一区精品| 激情久久久久久| 日韩黄色av| 特黄特色欧美大片| 亚洲乱码久久| 久久久久91| 欧美日韩xxxx| 亚洲在线国产日韩欧美| 精品五月天堂| 欧美一级二级三级视频| 婷婷中文字幕一区| 久久精品国产福利| 国产一区导航| 久久久一本精品| 国产精品高潮呻吟久久久久| 日韩天堂av| 色婷婷色综合| 国产精品乱战久久久| 黑丝一区二区三区| 九色porny丨国产首页在线| 国产欧美高清视频在线| 老司机精品久久| 欧美 日韩 国产一区二区在线视频 | 国产精品成人a在线观看| 一区二区国产在线| 亚洲天堂免费电影| 精品免费在线| 日本h片久久| 国产亚洲精品自拍| 激情欧美丁香| 欧美亚洲精品在线| 国产精品久久久久久久久妇女| 日本一区福利在线| 六月天综合网| 鲁大师成人一区二区三区| 美女少妇全过程你懂的久久| 日韩免费小视频| 中文字幕色婷婷在线视频| 福利一区二区三区视频在线观看| 久久av资源| 久久久精品区| 色一区二区三区| 欧美日韩免费看片| 久久久久国产| 亚洲制服少妇| 日本成人手机在线| 国产免费久久| 久久久免费人体| 精品视频国内| 国产精品字幕| 亚洲精品在线二区| 国产午夜一区| 国产黄大片在线观看| 日韩精品午夜| 亚洲人成高清| 麻豆极品一区二区三区| 日韩免费小视频| 亚洲欧美成人综合| 国产欧美一区二区三区精品观看| 久久永久免费| 国产女优一区| 国产精品高清一区二区| 中文字幕在线官网| 亚洲少妇自拍| 日产欧产美韩系列久久99| 你懂的国产精品| 99国产精品私拍| 国产精品白丝久久av网站| 久久精品官网| 久久99精品久久久野外观看| 久久精品亚洲人成影院| 91成人精品观看| 久久精品av| 国产乱码精品一区二区三区亚洲人| 日韩欧美字幕| 国产日韩高清一区二区三区在线 | 久久国产精品毛片| 日韩综合在线| 清纯唯美亚洲综合一区| 午夜视频精品| av最新在线| 青青草国产精品亚洲专区无| 国产99久久久国产精品成人免费| 91精品福利观看| 欧美一级专区| 四虎884aa成人精品最新| 日韩高清一区在线| 国产视频亚洲| 亚洲一本视频| 日韩欧美一区二区三区在线视频| 日韩激情啪啪| 蜜桃视频免费观看一区| 久久九九精品| 久久美女性网| 另类专区亚洲| 精品国产a一区二区三区v免费| 日韩高清电影免费| 中文字幕av亚洲精品一部二部| 色老板在线视频一区二区| 国产精品亚洲成在人线| 日韩黄色在线观看| 好看不卡的中文字幕| 一区二区小说| 国产精品av久久久久久麻豆网| 欧美香蕉视频| аⅴ资源天堂资源库在线| 国产一区日韩| 欧美日韩在线观看首页| 精品视频一二| 91免费精品| 日韩高清中文字幕一区二区| 超碰在线99| 日韩精品2区| 黑丝一区二区三区|