![]() |
|---|
| © Karobben |
由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址
from os import pathfrom PIL import Imageimport numpy as npimport matplotlib.pyplot as pltfrom wordcloud import WordCloud, STOPWORDS## Read the whole text.text = open('alice.txt').read()## read the mask image## taken from## http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpgalice_mask = np.array(Image.open("alice_mask.png"))stopwords = set(STOPWORDS)stopwords.add("said")wc = WordCloud(background_color="white",max_words=200, mask=alice_mask,max_font_size=200, # 根据你的图片大小定义stopwords=stopwords)## generate word cloudwc.generate(text)## store to filewc.to_file("alice.png")## showplt.imshow(wc, interpolation='bilinear')plt.axis("off")plt.figure()plt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear')plt.axis("off")plt.show()## remove punctuationSpecial = "”"TT = text.translate(str.maketrans(' ', ' ', string.punctuation))TT = TT.translate(str.maketrans('', '', string.whitespace[1:]))TT = TT.translate(str.maketrans('', '', string.digits))TT = TT.lower()TT = TT.split(" ")TT = list(set(TT))for i in string.ascii_lowercase:try:TT.remove(i)except:print(i)f = open("list",'a')for i in TT:f.write(i+'\n')f.close()
https://amueller.github.io/word_cloud/auto_examples/colored.html#colored-py
Enjoy~
由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址
GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗

