深度学习小项目——WordCloud

简介:        

词云,又称文字云,是文本数据的视觉表示,由词汇组成类似云的彩色图形,用于展示大量文本数据。通常用于描述网站上的关键字元数据(标签),或可视化自由格式文本。 每个词的重要性以字体大小或颜色显示。 词云的作用:

  • 快速感知最突出的文字
  • 快速定位按字母顺序排列的文字中相对突出的部分

词云的本质是点图,是在相应坐标点绘制具有特定样式的文字的结果

依赖:

  • 因为用python画图所以用引用matplotlib库
  • 因为要分词所以要引用分词库 这里用中文分词jieba(自称中文最好的分词工具)
  • 因为要制作词云所以引用开源词云wordcloud库
  • 如果没有意思库的话 自行 pip install 或通过其他方式下载

注意事项

  • 因为wordcloud是国外人开发的词云库,所以默认中文乱码,因此需要引入中文字体

  • WordCloud构造函数可以看到有个字体选项

     

    image.png

  • 网上搜索了一下这个地方ttf格式的中文字体可以用
    wget http://labfile.oss.aliyuncs.com/courses/756/DroidSansFallbackFull.ttf


    代码演示:

    # -*- coding: utf-8 -*-
    # @Time    : 2019/2/15 22:10
    # @Author  : Chaucer_Gxm
    # @Email   : gxm4167235@163.com
    # @File    : Word_1.py
    # @GitHub  : https://github.com/Chaucergit/Code-and-Algorithm
    # @blog    : https://blog.csdn.net/qq_24819773
    # @Software: PyCharm
    from wordcloud import WordCloud
    import matplotlib.pyplot as plt
    
    # 1.打开文本
    file = open('constitution.txt').read()
    
    # 2.生成图像
    wc = WordCloud().generate(file)
    
    # 3.显示词云
    plt.imshow(wc, interpolation='bilinear')
    plt.axis('off')
    plt.show()
    
    # 4.保存到文件
    wc.to_file('wordcloud.png')
    # -*- coding: utf-8 -*-
    # @Time    : 2019/2/15 22:50
    # @Author  : Chaucer_Gxm
    # @Email   : gxm4167235@163.com
    # @File    : Wordcloud_2_chinese3.py
    # @GitHub  : https://github.com/Chaucergit/Code-and-Algorithm
    # @blog    : https://blog.csdn.net/qq_24819773
    # @Software: PyCharm
    
    from wordcloud import WordCloud
    from PIL import Image
    import matplotlib.pyplot as plt
    import numpy as np
    import jieba
    
    # 1.读取文件
    file = open('xyj.txt', 'rb').read()
    
    # 2.中文分词
    text = ' '.join(jieba.cut(file))
    print(text[:200])
    
    # 3.生成对象及 mask
    mask = np.array(Image.open('black_mask.png'))
    wc = WordCloud(mask=mask, font_path='Hiragino.ttf', mode='RGBA', background_color=None).generate(text)
    
    # 4.显示词云
    plt.imshow(wc, interpolation='bilinear')
    plt.axis('off')
    plt.show()
    
    # 5.保存到文件
    wc.to_file('Wordcloud_chinese3.png')
    # -*- coding: utf-8 -*-
    # @Time    : 2019/2/15 23:20
    # @Author  : Chaucer_Gxm
    # @Email   : gxm4167235@163.com
    # @File    : Wordcloud_2_chinese5.py
    # @GitHub  : https://github.com/Chaucergit/Code-and-Algorithm
    # @blog    : https://blog.csdn.net/qq_24819773
    # @Software: PyCharm
    from wordcloud import WordCloud, ImageColorGenerator
    from PIL import Image
    import numpy as np
    import matplotlib.pyplot as plt
    import jieba
    import random
    
    # 1.读取文件
    file = open('xyj.txt', 'rb').read()
    
    # 2.中文分词
    text = ' '.join(jieba.cut(file))
    print(text[:300])
    
    
    # 颜色函数
    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))
        print(s)
        return s
    # 3.生成 mask 及词云对象
    mask = np.array(Image.open('color_mask.png'))
    wc = WordCloud(color_func=random_color, mask=mask, font_path='Hiragino.ttf', width=1000, height=800, mode='RGBA', background_color=None).generate(text)
    
    # 4.显示词云
    plt.imshow(wc, interpolation='bilinear')
    plt.axis('off')
    plt.show()
    # 5.保存图像
    wc.to_file('Wordcloud_chinese5.png')

    运行结果:

  • 词云

  • 全部文件链接(github)

  • https://github.com/Chaucergit/Code-and-Algorithm/tree/master/DL/1.%E8%AF%8D%E4%BA%91

  • 参考书籍及网络资源:
    [1].深度有趣

    [2].https://www.jianshu.com/p/af4b3c2c05d2

     

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值