當前位置:成語大全網 - 新華字典 - python如何生成詞雲圖

python如何生成詞雲圖

from wordcloud import WordCloud

import jieba

import numpy

import PIL.Image as Image

#1.將字符串切分

def chinese_jieba(text):

wordlist_jieba=jieba.cut(text)

space_wordlist=" ".join(wordlist_jieba)

return space_wordlist

with open("test.txt" ,encoding="utf-8")as file:

text=file.read()

text=chinese_jieba(text)

#2.圖片遮罩層

mask_pic=numpy.array(Image.open("china.jpg"))

#3.將參數mask設值為:mask_pic

wordcloud = WordCloud(font_path="C:/Windows/Fonts/simfang.ttf",mask=mask_pic).generate(text)

image=wordcloud.to_image()

image.show()

如圖所示