用python进行文本分析(词云)

用python制作词云

话不多说,直接上代码!

import jieba
import wordcloud
from imageio import imread
def Analyze(path0,path1,path2):
    try:
        f = open(path0,'rt',encoding='utf-8')
        t = f.read()
        f.close()
        ls = jieba.lcut(t)
        txt = " ".join(ls)
        mask = imread(path1)
        w = wordcloud.WordCloud(font_path='msyh.ttc',mask=mask,width=1000,height=700,background_color='black',max_words=30)
        w.generate(txt)
        w.to_file(path2)
        print("生成词云成功")
    except:
        print("生成词云失败!")

def main():
    path0 = input("请输入文本路径:")
    path1 = input("请输入图片路径:")
    path2 = input("请输入保存路径:")
    Analyze(path0,path1,path2)

main()

运行结果如下:
用python进行文本分析(词云)_第1张图片
用python进行文本分析(词云)_第2张图片

你可能感兴趣的:(python词云,python,pycharm)