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

您的位置:首頁技術文章
文章詳情頁

Python 制作詞云的WordCloud參數用法說明

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

官方API:

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

實現

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) #如果參數為空,則使用二維遮罩繪制詞云。如果 mask 非空,設置的寬高值將被忽略,遮罩形狀被 mask 取代。除全白(#FFFFFF)的部分將不會繪制,其余部分會用于繪制詞云。如:bg_pic = imread(’讀取一張圖片.png’),背景圖片的畫布一定要設置為白色(#FFFFFF),然后顯示的形狀為不是白色的其他顏色??梢杂胮s工具將自己要顯示的形狀復制到一個純白色的畫布上再保存,就ok了。scale : float (default=1) #按照比例進行放大畫布,如設置為1.5,則長和寬都是原來畫布的1.5倍 min_font_size : int (default=4) #顯示的最小的字體大小 font_step : int (default=1) #字體步長,如果步長大于1,會加快運算但是可能導致結果出現較大的誤差 max_words : number (default=200) #要顯示的詞的最大個數 stopwords : set of strings or None #設置需要屏蔽的詞,如果為空,則使用內置的STOPWORDS background_color : color value (default=”black”) #背景顏色,如background_color=’white’,背景顏色為白色 max_font_size : int or None (default=None) #顯示的最大的字體大小 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顏色 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 arrayto_file(filename) #輸出到文件

補充:生成詞云之python中WordCloud包的用法

效果圖:

Python 制作詞云的WordCloud參數用法說明

這是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的所有參數,下面具體介紹一下各個參數:

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) //如果參數為空,則使用二維遮罩繪制詞云。如果 mask 非空,設置的寬高值將被忽略,遮罩形狀被 mask 取代。除全白(#FFFFFF)的部分將不會繪制,其余部分會用于繪制詞云。如:bg_pic = imread(’讀取一張圖片.png’),背景圖片的畫布一定要設置為白色(#FFFFFF),然后顯示的形狀為不是白色的其他顏色。可以用ps工具將自己要顯示的形狀復制到一個純白色的畫布上再保存,就ok了。scale : float (default=1) //按照比例進行放大畫布,如設置為1.5,則長和寬都是原來畫布的1.5倍。min_font_size : int (default=4) //顯示的最小的字體大小font_step : int (default=1) //字體步長,如果步長大于1,會加快運算但是可能導致結果出現較大的誤差。max_words : number (default=200) //要顯示的詞的最大個數stopwords : set of strings or None //設置需要屏蔽的詞,如果為空,則使用內置的STOPWORDSbackground_color : color value (default=”black”) //背景顏色,如background_color=’white’,背景顏色為白色。max_font_size : int or None (default=None) //顯示的最大的字體大小mode : string (default=”RGB”) //當參數為“RGBA”并且background_color不為空時,背景為透明。relative_scaling : float (default=.5) //詞頻和字體大小的關聯性color_func : callable, default=None //生成新顏色的函數,如果為空,則使用 self.color_funcregexp : string or None (optional) //使用正則表達式分隔輸入的文本collocations : bool, default=True //是否包括兩個詞的搭配colormap : string or matplotlib colormap, default=”viridis” //給每個單詞隨機分配顏色,若指定color_func,則忽略該方法。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 arrayto_file(filename) //輸出到文件例子:

想要生成的詞云的形狀:

Python 制作詞云的WordCloud參數用法說明

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

下面是一個文本文檔:

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’)

運行結果:

Python 制作詞云的WordCloud參數用法說明

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

標簽: python
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
蜜桃精品视频| 国产一区三区在线播放| 日韩精品免费观看视频| 国产高清一区二区| 久久精品日韩欧美| 日本在线不卡视频| 久久国产高清| 国产综合婷婷| 国模 一区 二区 三区| 日韩av在线中文字幕| 欧美日韩激情在线一区二区三区| 日韩精品午夜视频| 免费在线看一区| 欧美韩日一区| 久久a爱视频| 日韩和欧美的一区| 欧美欧美黄在线二区| 日韩av中文在线观看| 国户精品久久久久久久久久久不卡| 国产一区二区三区四区大秀| 97精品久久| 日韩国产欧美一区二区三区| 亚洲精品福利| 亚洲激情二区| 日韩动漫一区| 美女久久一区| 男女男精品视频网| 综合激情网...| 亚洲影视一区| 石原莉奈在线亚洲二区| 噜噜噜躁狠狠躁狠狠精品视频| 一区二区精品| 日韩av中文字幕一区二区| 欧美精品国产| 欧美xxxx性| 日本一区二区免费高清| 国产一区二区久久久久| 在线天堂资源www在线污| 三级精品视频| 国产精品婷婷| 久久国产日本精品| 亚洲伊人精品酒店| 国产精品嫩模av在线| 美女视频网站久久| 亚洲综合电影| 午夜久久影院| 日韩高清成人在线| 久久三级中文| 久久先锋影音| 国产精品成人3p一区二区三区| 精品视频在线观看网站| 久久精品123| 欧美专区一区二区三区| 欧美啪啪一区| 免费看av不卡| 日韩精品欧美| 日本免费新一区视频| 麻豆国产欧美一区二区三区| 欧美成人a交片免费看| 中文亚洲欧美| 国产精品一区亚洲| 日韩不卡在线| 日韩中出av| 国产一区久久| 国产精品多人| 欧美成人日韩| 青青国产精品| 午夜精品久久久久久久久久蜜桃| 久久高清免费观看| 久久一区精品| 国产毛片一区| 国产精品一区二区三区四区在线观看 | 国产亚洲毛片在线| 欧美日一区二区三区在线观看国产免 | 成人片免费看| 91欧美日韩在线| 99久久夜色精品国产亚洲1000部| 最新国产精品| 久久婷婷av| 精品国产乱码| 国产欧美日韩一级| 在线综合亚洲| 伊人久久国产| 7m精品国产导航在线| 欧美成a人国产精品高清乱码在线观看片在线观看久 | 日韩中文字幕在线一区| 欧美日韩在线播放视频| www.九色在线| 亚洲天堂av影院| 超碰成人av| 欧美极品中文字幕| 欧美一级一区| 欧美日一区二区在线观看| 亚洲精品成a人ⅴ香蕉片| 久久午夜影视| 亚洲一区观看| 蜜臀久久久99精品久久久久久| 在线亚洲激情| 日韩一区精品视频| 日韩中文字幕麻豆| 亚洲欧美一级| 婷婷亚洲成人| 欧美精品影院| 日韩不卡在线观看日韩不卡视频| 亚洲精品九九| 狠狠干综合网| 日韩精品一二三| 亚洲婷婷在线| 久久99国产精品视频| 亚洲女人av| 亚洲一区观看| 免费在线成人| 精品国产乱码久久久| 午夜在线精品| 午夜亚洲福利| 美女国产精品久久久| 国产69精品久久| 蜜桃一区二区三区在线| 天堂中文av在线资源库| 99精品在线观看| 国产精品igao视频网网址不卡日韩| 日韩av网站在线免费观看| 亚洲一区资源| 欧美日一区二区| 国产欧美一区二区三区米奇| 亚洲女同中文字幕| 精品视频一区二区三区四区五区 | 午夜亚洲福利| 欧美精品影院| 亚洲免费毛片| 久久精品中文| 国产激情综合| 日韩精品久久久久久久软件91| 久久精品亚洲| 日韩在线观看一区| 麻豆精品视频在线观看| 视频一区视频二区中文字幕| 欧美日韩调教| 爽好久久久欧美精品| 国产精品中文字幕制服诱惑| 亚洲视频播放| 电影91久久久| 国产精品调教| 日韩精品欧美成人高清一区二区| 久久爱www.| 欧美日韩在线精品一区二区三区激情综合 | 一区二区三区四区在线看| 久久久久97| 欧美中文字幕一区二区| 精品久久91| 亚洲精品成人| 国产资源在线观看入口av| 日本色综合中文字幕| 日韩.com| 国产专区精品| 午夜一区在线| 国产欧美在线| 亚洲激情久久| 午夜精品免费| 国产精品97| 国产激情综合| 国产免费久久| 久久激情一区| 天堂日韩电影| 日韩av午夜在线观看| 日韩精品欧美大片| 日韩亚洲精品在线观看| 日韩av首页| 欧美日韩在线二区| 欧美国产先锋| 精品一区91| 激情91久久| 亚洲激情二区| 日韩网站在线| 久久精品免费看| av亚洲免费| 97欧美在线视频| 中文字幕高清在线播放| 精品久久不卡| 日本成人手机在线| 欧美日韩亚洲在线观看| 欧美成人综合| 在线一区电影| a国产在线视频| 欧美午夜精品一区二区三区电影| 国产日韩欧美一区| 蜜臀av性久久久久蜜臀aⅴ流畅| 久久久久免费| 999久久久精品国产| 久久av中文| 欧美成人基地 | 亚洲天堂黄色| 国产精品久久免费视频| 国产一区二区三区黄网站| 日韩中文一区二区| 国产精品亚洲综合色区韩国| 麻豆国产精品| 国产伦精品一区二区三区千人斩| 美女视频黄 久久| 国产精品蜜月aⅴ在线|