国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python制作詞云的方法

2020-02-16 11:26:09
字體:
供稿:網(wǎng)友

需求:

看到朋友圈有人發(fā)詞云照片,感覺自己也可以玩一玩,于是乎借助wordcloud實(shí)現(xiàn)功能。

環(huán)境:

MacOS 10.12 +Python 2.7 +Wordcloud
Windows通用

準(zhǔn)備:

安裝wordcloud

$ pip install wordcloud

SIP功能是Apple在OSX上推出的系統(tǒng)完整性保護(hù)功能,新版本的macOS直接用pip安裝報(bào)錯(cuò),在不關(guān)閉SIP功能的前提下,可以使用

$ pip install wordcloud --user -U

某些情況還會(huì)提示錯(cuò)誤,需要安裝VS for Python,直接上官網(wǎng)下載安裝即可。

實(shí)現(xiàn):

源碼

#! /usr/bin/env python# importfrom os import pathfrom PIL import Imageimport numpy as npimport matplotlib.pyplot as pltfrom wordcloud import WordCloud, STOPWORDS# current pathd = path.dirname(__file__)# Read the whole text.text = open(path.join(d, 'test.txt')).read()# read the mask imagetest_mask = np.array(Image.open(path.join(d, "test_mask.png")))stopwords = set(STOPWORDS)stopwords.add("said")# settingwc = WordCloud(background_color="black", max_words=2000, mask=test_mask,        stopwords=stopwords)# generate word cloudwc.generate(text)# plot and showplt.imshow(wc, interpolation='bilinear')plt.axis("off")plt.figure()plt.imshow(test_mask, cmap=plt.cm.gray, interpolation='bilinear')plt.axis("off")plt.show()# store to filewc.to_file(path.join(d, "test.png"))

輔助文件

test_mask.png

test.txt

The list of big's prior run-ins with the far-right fringe is long and varied. He tweeted fake crime statistics spread by racists to paint black cans as violent, then defended them as credible. He sparked a firestorm last year when he declined to renounce

效果圖

這里寫圖片描述

其它說明:

1.文檔可以是任意英文txt文件,以上的是網(wǎng)絡(luò)上英文新聞中的一小段,僅起示例作用。
2.關(guān)于中文支持,有多種方法,主要就是分詞的問題,這里不討論了。
3.我不清楚許多人說的定制是什么意思,因?yàn)槿绻胍约喝我庀胍男螤畹脑?,我覺得用PS做一個(gè)png圖可以達(dá)到同樣的效果,經(jīng)測試也沒有發(fā)現(xiàn)問題。
4.寫的第一篇文章,不足之處歡迎來噴,畢竟我是要學(xué)習(xí)的。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林站長站。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 比如县| 绩溪县| 海南省| 那坡县| 大丰市| 循化| 张家界市| 承德市| 双峰县| 乐陵市| 双流县| 玉溪市| 洪江市| 大埔区| 香格里拉县| 桂阳县| 禹城市| 衡阳市| 武义县| 白银市| 隆回县| 宝兴县| 台安县| 雅安市| 紫金县| 靖边县| 辽阳县| 盐津县| 蕉岭县| 恩施市| 清徐县| 石台县| 宜兴市| 彭水| 阜阳市| 利津县| 盐源县| 盐池县| 汨罗市| 五河县| 鄂州市|