国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

Python+wordcloud十分鐘學會生成英文詞云

來源:懂視網 責編:小采 時間:2020-11-27 14:09:29
文檔

Python+wordcloud十分鐘學會生成英文詞云

Python+wordcloud十分鐘學會生成英文詞云:基于python生成的wordcloud詞云在這兩年一直都熱門話題,如果你耐下性子花個10分鐘看看這篇文章,或許你就再也不用羨慕那些會詞云的人了。這不是一項高深莫測的技術,你也可以學會。快來試試吧!本篇我們講解的是如何制作英文詞云,下一期我們將給大家帶來如
推薦度:
導讀Python+wordcloud十分鐘學會生成英文詞云:基于python生成的wordcloud詞云在這兩年一直都熱門話題,如果你耐下性子花個10分鐘看看這篇文章,或許你就再也不用羨慕那些會詞云的人了。這不是一項高深莫測的技術,你也可以學會。快來試試吧!本篇我們講解的是如何制作英文詞云,下一期我們將給大家帶來如
基于python生成的wordcloud

詞云在這兩年一直都熱門話題,如果你耐下性子花個10分鐘看看這篇文章,或許你就再也不用羨慕那些會詞云的人了。這不是一項高深莫測的技術,你也可以學會。快來試試吧!

u=3986286550,4041352992&fm=26&gp=0.jpg

本篇我們講解的是如何制作英文詞云,下一期我們將給大家帶來如何制作中文詞云,敬請期待!

快速生成詞云

from wordcloud import WordCloud
f = open(u'txt/AliceEN.txt','r').read()
wordcloud = WordCloud(background_color="white",width=1000, height=860, margin=2).generate(f)
# width,height,margin可以設置圖片屬性
# generate 可以對全部文本進行自動分詞,但是他對中文支持不好,對中文的分詞處理請看我的下一篇文章
#wordcloud = WordCloud(font_path = r'D:Fontssimkai.ttf').generate(f)
# 你可以通過font_path參數來設置字體集
#background_color參數為設置背景顏色,默認顏色為黑色
import matplotlib.pyplot as plt
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
wordcloud.to_file('test.png')

# 保存圖片,但是在第三模塊的例子中 圖片大小將會按照 mask 保存

a.png

自定義字體顏色

這段代碼主要來自wordcloud的github,你可以在github下載該例子

#!/usr/bin/env python
"""
Colored by Group Example
========================
Generating a word cloud that assigns colors to words based on
a predefined mapping from colors to words
"""
from wordcloud import (WordCloud, get_single_color_func)
import matplotlib.pyplot as plt
class SimpleGroupedColorFunc(object):
 """Create a color function object which assigns EXACT colors
 to certain words based on the color to words mapping
 Parameters
 ----------
 color_to_words : dict(str -> list(str))
 A dictionary that maps a color to the list of words.
 default_color : str
 Color that will be assigned to a word that's not a member
 of any value from color_to_words.
 """
 def __init__(self, color_to_words, default_color):
 self.word_to_color = {word: color
 for (color, words) in color_to_words.items()
 for word in words}
 self.default_color = default_color
 def __call__(self, word, **kwargs):
 return self.word_to_color.get(word, self.default_color)
class GroupedColorFunc(object):
 """Create a color function object which assigns DIFFERENT SHADES of
 specified colors to certain words based on the color to words mapping.
 Uses wordcloud.get_single_color_func
 Parameters
 ----------
 color_to_words : dict(str -> list(str))
 A dictionary that maps a color to the list of words.
 default_color : str
 Color that will be assigned to a word that's not a member
 of any value from color_to_words.
 """
 def __init__(self, color_to_words, default_color):
 self.color_func_to_words = [
 (get_single_color_func(color), set(words))
 for (color, words) in color_to_words.items()]
 self.default_color_func = get_single_color_func(default_color)
 def get_color_func(self, word):
 """Returns a single_color_func associated with the word"""
 try:
 color_func = next(
 color_func for (color_func, words) in self.color_func_to_words
 if word in words)
 except StopIteration:
 color_func = self.default_color_func
 return color_func
 def __call__(self, word, **kwargs):
 return self.get_color_func(word)(word, **kwargs)
text = """The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!"""
# Since the text is small collocations are turned off and text is lower-cased
wc = WordCloud(collocations=False).generate(text.lower())
# 自定義所有單詞的顏色
color_to_words = {
 # words below will be colored with a green single color function
 '#00ff00': ['beautiful', 'explicit', 'simple', 'sparse',
 'readability', 'rules', 'practicality',
 'explicitly', 'one', 'now', 'easy', 'obvious', 'better'],
 # will be colored with a red single color function
 'red': ['ugly', 'implicit', 'complex', 'complicated', 'nested',
 'dense', 'special', 'errors', 'silently', 'ambiguity',
 'guess', 'hard']
}
# Words that are not in any of the color_to_words values
# will be colored with a grey single color function
default_color = 'grey'
# Create a color function with single tone
# grouped_color_func = SimpleGroupedColorFunc(color_to_words, default_color)
# Create a color function with multiple tones
grouped_color_func = GroupedColorFunc(color_to_words, default_color)
# Apply our color function
# 如果你也可以將color_func的參數設置為圖片,詳細的說明請看 下一部分
wc.recolor(color_func=grouped_color_func)
# Plot
plt.figure()
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.show()

b.png

利用背景圖片生成詞云,設置停用詞詞集

該段代碼主要來自于wordcloud的github,你同樣可以在github下載該例子以及原圖片與效果圖

#!/usr/bin/env python
"""
Image-colored wordcloud
=======================
You can color a word-cloud by using an image-based coloring strategy
implemented in ImageColorGenerator. It uses the average color of the region
occupied by the word in a source image. You can combine this with masking -
pure-white will be interpreted as 'don't occupy' by the WordCloud object when
passed as mask.
If you want white as a legal color, you can just pass a different image to
"mask", but make sure the image shapes line up.
"""
from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
d = path.dirname(__file__)
# Read the whole text.
text = open(path.join(d, 'alice.txt')).read()
# read the mask / color image taken from
# http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010
alice_coloring = np.array(Image.open(path.join(d, "alice_color.png")))
# 設置停用詞
stopwords = set(STOPWORDS)
stopwords.add("said")
# 你可以通過 mask 參數 來設置詞云形狀
wc = WordCloud(background_color="white", max_words=2000, mask=alice_coloring,
 stopwords=stopwords, max_font_size=40, random_state=42)
# generate word cloud
wc.generate(text)
# create coloring from image
image_colors = ImageColorGenerator(alice_coloring)
# show
# 在只設置mask的情況下,你將會得到一個擁有圖片形狀的詞云
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.figure()
# recolor wordcloud and show
# we could also give color_func=image_colors directly in the constructor
# 我們還可以直接在構造函數中直接給顏色
# 通過這種方式詞云將會按照給定的圖片顏色布局生成字體顏色策略
plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")
plt.axis("off")
plt.figure()
plt.imshow(alice_coloring, cmap=plt.cm.gray, interpolation="bilinear")
plt.axis("off")
plt.show()

展示效果如下:

c.png

d.png

e.png

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

Python+wordcloud十分鐘學會生成英文詞云

Python+wordcloud十分鐘學會生成英文詞云:基于python生成的wordcloud詞云在這兩年一直都熱門話題,如果你耐下性子花個10分鐘看看這篇文章,或許你就再也不用羨慕那些會詞云的人了。這不是一項高深莫測的技術,你也可以學會。快來試試吧!本篇我們講解的是如何制作英文詞云,下一期我們將給大家帶來如
推薦度:
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 欧美一区二区在线观看 | 国产一区二区网站 | 免费大黄网站 | 国产成人h福利小视频在线观看 | 欧美一区二区在线 | 国产一级特黄高清免费大片dvd | 成人免费一级毛片在线播放视频 | 国产毛片高清 | 韩国精品一区二区久久 | 色综合91久久精品中文字幕 | 最新国产精品视频免费看 | 久久国产一区二区 | 亚洲欧美在线视频 | 美女一区 | 精品一区二区三区五区六区七区 | 久久综合影院 | 免费一级 一片一毛片 | 日本黄一级日本黄二级 | 精品欧美一区二区三区 | 日韩123 | 欧美阿v| 欧美日韩国产在线人 | 久久国产毛片 | 欧美日韩亚洲区久久综合 | 日韩亚洲欧美视频 | 亚洲国产成人久久综合碰碰动漫3d | 免费国产一区 | 国产va精品免费观看 | 亚洲欧美日韩中文v在线 | 青青草国产在线视频 | 91精品国产免费久久久久久 | 91视频一区 | 欧美综合第一页 | 日韩综合网站 | 精品国产91久久久久 | 欧美激情综合网 | 一道精品一区二区三区 | 国产精品亚洲αv天堂2021 | 日韩精品成人 | 国产精品久久久久久久免费 | 日韩综合网站 |