jieba和词云

from wordcloud import WordCloud
import matplotlib.pyplot as plt
import jieba
import numpy as np
from PIL import Image
 
 
# 生成词云
def create_word_cloud(filename):
    text = open(r"C:\Users\lenovo\Desktop\{}.txt".format(filename),encoding = "utf-8").read()

    wordlist = jieba.cut(text, cut_all=True)
    wl = " ".join(wordlist)
 
    cloud_mask = np.array(Image.open(r"C:\Users\lenovo\Desktop\壁纸呗\hand drawn of mengyuan04.png"))
 

    wc = WordCloud(

        background_color="black",

        mask=cloud_mask,

        max_words=2000,
        
        font_path=r'C:\Windows\Fonts\simhei.ttf',
        height=1200,
        width=1600,
        
        max_font_size=100,
        
        random_state=100,
    )
 
    myword = wc.generate(wl) 
    
    plt.imshow(myword)
    plt.axis("off")
    plt.show()
    wc.to_file(r'C:\Users\lenovo\Desktop\py_book.png') 
 
 
if __name__ == '__main__':
    create_word_cloud('dicuo')
View Code

 

posted @ 2020-06-29 19:19  aoimo  阅读(88)  评论(0编辑  收藏  举报