久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

Python第三方庫wordcloud(詞云)快速入門與進(jìn)階

 ZhouAndrew 2018-06-29


下載:
1——使用conda下載(前提是安裝了Anaconda,,推薦這種方法):

conda install -c conda-forge wordcloud
  • 1

2——使用pip命令(筆主開發(fā)環(huán)境為windows,,第一次按這種方法安裝,,會出現(xiàn)錯誤,按照網(wǎng)上的解決辦法一直沒解決):

pip install wordcloud
  • 1

實(shí)例

1–入門案例

#!/usr/bin/env python
"""
Minimal Example
===============

使用默認(rèn)參數(shù)根據(jù)美國憲法生成方形的詞云
"""

from os import path
from wordcloud import WordCloud

d = path.dirname(__file__)

# 讀取整個文本
text = open(path.join(d, 'constitution.txt')).read()

# 生成一個詞云圖像
wordcloud = WordCloud().generate(text)

# matplotlib的方式展示生成的詞云圖像
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")

#max_font_size設(shè)定生成詞云中的文字最大大小
#width,height,margin可以設(shè)置圖片屬性
# generate 可以對全部文本進(jìn)行自動分詞,但是他對中文支持不好
wordcloud = WordCloud(max_font_size=66).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()

# pil方式展示生成的詞云圖像(如果你沒有matplotlib)
# image = wordcloud.to_image()
# image.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

入門案例

2–使用蒙版圖像可以生成任意形狀的wordcloud,。

#!/usr/bin/env python
"""
Masked wordcloud
================

使用蒙版圖像可以生成任意形狀的wordcloud,。
"""

from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

from wordcloud import WordCloud, STOPWORDS

d = path.dirname(__file__)

# 讀取整個文本.
text = open(path.join(d, 'alice.txt')).read()

#讀取圖片(圖片來源:http://www./stencils/movies/alice%20in%20wonderland/255fk.jpg)
alice_mask = np.array(Image.open(path.join(d, "alice_mask.png")))

stopwords = set(STOPWORDS)
stopwords.add("said")
#設(shè)置詞云的一些屬性
wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask,
               stopwords=stopwords)
# 生成詞云
wc.generate(text)

#保存到本地
wc.to_file(path.join(d, "alice.png"))

#展示
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.figure()
plt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear')
plt.axis("off")
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

3–著色

#!/usr/bin/env python
"""
使用自定義顏色
===================

使用重新著色方法和自定義著色功能。
"""

import numpy as np
from PIL import Image
from os import path
import matplotlib.pyplot as plt
import random

from wordcloud import WordCloud, STOPWORDS


def grey_color_func(word, font_size, position, orientation, random_state=None,
                    **kwargs):
    return "hsl(0, 0%%, %d%%)" % random.randint(60, 100)

d = path.dirname(__file__)

# 讀取圖片(圖片來源:http://www./stencils/movies/star%20wars/storm-trooper.gif)
mask = np.array(Image.open(path.join(d, "stormtrooper_mask.png")))

# 文字來源:“新希望”電影劇本(網(wǎng)址:http://www./scripts/Star-Wars-A-New-Hope.html)
text = open(path.join(d, 'a_new_hope.txt')).read()

# 預(yù)處理一點(diǎn)點(diǎn)文本
text = text.replace("HAN", "Han")
text = text.replace("LUKE'S", "Luke")

# 添加電影劇本特定的停用詞
stopwords = set(STOPWORDS)
stopwords.add("int")
stopwords.add("ext")

wc = WordCloud(max_words=1000, mask=mask, stopwords=stopwords, margin=10,
               random_state=1).generate(text)
# 存儲默認(rèn)的彩色圖像
default_colors = wc.to_array()
plt.title("Custom colors")
plt.imshow(wc.recolor(color_func=grey_color_func, random_state=3),
           interpolation="bilinear")
wc.to_file("a_new_hope.png")
plt.axis("off")
plt.figure()
plt.title("Default colors")
plt.imshow(default_colors, interpolation="bilinear")
plt.axis("off")
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

#!/usr/bin/env python
"""
Image-colored wordcloud
=======================
您可以在ImageColorGenerator中實(shí)現(xiàn)使用基于圖像的著色策略對文字云進(jìn)行著色,,它使用由源圖像中的單詞占用的區(qū)域的平均顏色,。

"""

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

# 讀取整個文本
text = open(path.join(d, 'alice.txt')).read()

# 讀取蒙板/彩色圖像(圖片是從http://jirkavinse./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")

wc = WordCloud(background_color="white", max_words=2000, mask=alice_coloring,
               stopwords=stopwords, max_font_size=40, random_state=42)
# 生成詞云
wc.generate(text)

# 從圖像創(chuàng)建著色
image_colors = ImageColorGenerator(alice_coloring)

# 顯示
plt.imshow(wc, interpolation="bilinear")
plt.axis("off") #不顯示坐標(biāo)尺寸
plt.figure()
# 重新著色詞云并顯示
# 我們也可以直接在構(gòu)造函數(shù)中給使用:color_func=image_colors 
plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")
plt.axis("off") #不顯示坐標(biāo)尺寸
plt.figure()
plt.imshow(alice_coloring, cmap=plt.cm.gray, interpolation="bilinear")
plt.axis("off") #不顯示坐標(biāo)尺寸
plt.show()#一次繪制三張圖
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46


4–表情

 #!/usr/bin/env python
"""
表情實(shí)例
===============
一個簡單的例子,顯示如何包含表情符號,。 請注意,,這個例子似乎不適用于OS X(蘋果系統(tǒng)),但是確實(shí)如此
在Ubuntu中正常工作
包含表情符號有3個重要步驟:
1) 使用io.open而不是內(nèi)置的open來讀取文本輸入,。 這確保它被加載為UTF-8
2) 重寫詞云使用的正則表達(dá)式以將文本解析為單詞,。 默認(rèn)表達(dá)式只會匹配ascii的單詞
3) 將默認(rèn)字體覆蓋為支持表情符號的東西。 包含的Symbola字體包括黑色和白色大多數(shù)表情符號的白色輪廓,。 目前PIL / Pillow庫存在的問題似乎可以預(yù)防
它在OS X上運(yùn)行正常(https://github.com/python-pillow/Pillow/issues/1774),。
如果你有問題,試試在Ubuntu上運(yùn)行
"""
import io
import string
from os import path
from wordcloud import WordCloud

d = path.dirname(__file__)

#使用io.open將文件正確加載為UTF-8非常重要
text = io.open(path.join(d, 'happy-emoji.txt')).read()

# the regex used to detect words is a combination of normal words, ascii art, and emojis
# 2+ consecutive letters (also include apostrophes), e.x It's
normal_word = r"(?:\w[\w']+)"
# 2+ consecutive punctuations, e.x. :)
ascii_art = r"(?:[{punctuation}][{punctuation}]+)".format(punctuation=string.punctuation)
# a single character that is not alpha_numeric or other ascii printable
emoji = r"(?:[^\s])(?<![\w{ascii_printable}])".format(ascii_printable=string.printable)
regexp = r"{normal_word}|{ascii_art}|{emoji}".format(normal_word=normal_word, ascii_art=ascii_art,
                                                     emoji=emoji)

# 生成一個詞云圖片
# Symbola字體包含大多數(shù)表情符號
font_path = path.join(d, 'fonts', 'Symbola', 'Symbola.ttf')
wordcloud = WordCloud(font_path=font_path, regexp=regexp).generate(text)

# 采用matplotlib方式:展示生成的圖片
import matplotlib.pyplot as plt
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

表情實(shí)例

以上所有例子可以到我的github上下載(持續(xù)更新Python第三方庫使用demo以及常見python爬蟲,,python玩微信等內(nèi)容):

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn),。請注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購買等信息,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多