词云应用

33 篇文章 0 订阅
# -*- coding:utf-8 -*-
from wordcloud import WordCloud,ImageColorGenerator
import matplotlib.pyplot as plt
import jieba
import numpy as np
from PIL import Image
import random
import jieba.analyse



def word_umaru1(path:"file"):#english text
    text = open(path).read()
    wc = WordCloud().generate(text)
    plt.imshow(wc,interpolation = "bilinear")
    plt.axis("off")
    
    wc.to_file("pic1.png")
def word_umaru2(path:"file"):#chinese text
    text = open(path,encoding="utf-8").read()
    wc = WordCloud(font_path='Hiragino.ttf', width=800, height=600, mode='RGBA', background_color=None).generate(text)
    plt.imshow(wc,interpolation = "bilinear")
    plt.axis("off")
    
    wc.to_file("pic2.png")
def word_umaru3(path:"file"):#jieba分词
    text = open(path,encoding="utf-8").read()
    text = ' '.join(jieba.cut(text))
    wc = WordCloud(font_path="msyhbd.ttc",width=800,height=600,mode='RGBA',background_color=None).generate(text)
    plt.imshow(wc,interpolation = "bilinear")
    plt.axis("off")
    
    wc.to_file("pic3.png")

def word_umaru4(path:"file"):#mask遮罩
    text = open(path,encoding="utf-8").read()
    text = ' '.join(jieba.cut(text))
    mask = np.array(Image.open("1.png"))
    wc = WordCloud(mask=mask,font_path="msyhbd.ttc",mode='RGBA',background_color=None).generate(text)
    plt.imshow(wc,interpolation = "bilinear")
    plt.axis("off")
    
    wc.to_file("pic4.png")

def word_umaru5(path:"file"):#使用图片颜色
    text = open(path,encoding="utf-8").read()
    text = ' '.join(jieba.cut(text))
    mask = np.array(Image.open("1.png"))
    wc = WordCloud(mask=mask,font_path="msyhbd.ttc",mode='RGBA',background_color=None).generate(text)

    image_colors  = ImageColorGenerator(mask)
    wc.recolor(color_func = image_colors)

    plt.imshow(wc,interpolation = "bilinear")
    plt.axis("off")
    
    wc.to_file("pic5.png")

def random_color(word,font_size,position,orientation,font_path,random_state):#自定义颜色
    s = 'hsl(0, %d%%,%d%%)'%(random.randint(60,80),random.randint(60,80))
    return s
def word_umaru6(path:"file")::#自定义颜色
    text = open(path,encoding="utf-8").read()
    text = ' '.join(jieba.cut(text))
    mask = np.array(Image.open("1.png"))
    wc = WordCloud(color_func=random_color,mask=mask,font_path="msyhbd.ttc",mode='RGBA',background_color=None).generate(text)

    plt.imshow(wc,interpolation = "bilinear")
    plt.axis("off")
    
    wc.to_file("pic6.png")


def word_umaru7(path:"file"):#分析词汇频率,使用图片颜色
    text = open(path,encoding="utf-8").read()

    freq = jieba.analyse.extract_tags(text,topK=200,withWeight = True)
    freq = {i[0]:i[1] for i in freq}
  

    mask = np.array(Image.open("color_mask.png"))
    wc = WordCloud(mask=mask,font_path='Hiragino.ttf',mode='RGBA',background_color=None).generate_from_frequencies(freq)
    image_colors  = ImageColorGenerator(mask)
    wc.recolor(color_func = image_colors)

    plt.imshow(wc,interpolation = "bilinear")
    plt.axis("off")
    
    wc.to_file("pic7.png")

def main():
    fig = plt.figure(figsize=(10,15))#创建多个子图
    
    for i in range(1,8):
        if i==1:
            path = "ing.txt"
            str_="word_umaru"+str(i)+"(path)"
        else:
            path = "xyj.txt"
            str_="word_umaru"+str(i)+"(path)"
        fig.add_subplot(2,4,i)
        exec(str_)
    fig.savefig("result.png")
    plt.show()
    
main()

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佐倉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值