码迷,mamicode.com
首页 > 其他好文 > 详细

jieba和词云

时间:2020-06-29 19:58:44      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:from   back   opened   http   mat   plot   rand   txt   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=rC:\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(rC:\Users\lenovo\Desktop\py_book.png) 
 
 
if __name__ == __main__:
    create_word_cloud(dicuo)
View Code

技术图片

 

jieba和词云

标签:from   back   opened   http   mat   plot   rand   txt   jieba   

原文地址:https://www.cnblogs.com/modiqiang/p/13209830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!