码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Python使用词云图展示


    网上看到一个txt文本信息,共2351条饭否记录,据说是微信之父每天发的饭否记录,其实我不知道什么是饭否。我读取这个文本内容,展示到词语图上。之前也使用过,但是好久没有玩Python了,称假期空闲,练习练习。开始发行“通过网页”变成了高频词汇,一看源文本文件,发行每条记录的后面都包含“ 2010-11-26 13:59 通过网页”,这样通过网页肯定是高频词了,所有重新处理了源文本信息,使用正则表达式式,提前时间节点前的任意字符。使用的正则表达式:.+(?=(\d{4}\-\d{2}\-\d{2}))

    提取信息如下:

    找一个背景,网上找奋斗图片,去掉背景色,裁剪人物。

    完整代码:

    from os import path
    from PIL import Image
    import numpy as np
    import matplotlib.pyplot as plt
    import os
    import chardet
    
    from wordcloud import WordCloud, STOPWORDS
    
    # get data directory (using getcwd() is needed to support running example in generated IPython notebook)
    d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()
    print(d)
    
    # Read the whole text.
    # text = open(path.join(d, 'ZXL.txt'), encoding='utf-8', errors='ignore').read()
    with open(path.join(d, 'ZXL.txt'), 'rb') as f:
        raw_data = f.read()
        result = chardet.detect(raw_data)
        use_encoding = result['encoding']
    
    # 查看文本使用的编码
    print(use_encoding) # utf-8
    text = open(path.join(d, 'ZXL.txt'), 'r', encoding=use_encoding).read()
    
    # read the mask image
    fight_mask = np.array(Image.open(path.join(d, "fight.png")))
    
    # 指定字体文件路径
    font_path = r'C:\Windows\Fonts\方正粗黑宋简体.ttf'
    
    # 创建词云图对象并设置字体
    stopwords = set(STOPWORDS)
    
    wc = WordCloud(background_color="white",
                   max_words=6000, mask=fight_mask,
                   font_path=font_path,
                   stopwords=stopwords,
                   contour_width=3,
                   contour_color='steelblue')
    
    # generate word cloud
    wc.generate(text)
    
    # store to file
    wc.to_file(path.join(d, "ZXL_example.png"))
    
    # show
    plt.imshow(wc, interpolation='bilinear')
    plt.axis("off")
    plt.show()
    
    
    运行效果如下:
    

    生产图片文件:

     

  • 相关阅读:
    【SSM -MyBatis篇03】MyBatis Generator(MBG)配置属性详解(基于MyBatis3) - 逆向生成 - 配置MBG模板
    Java VS kotlin 泛型
    NO.396 旋转数组
    二叉树的按层遍历
    如何开发自己的npm包并上传到npm官网可以下载
    万宾科技智能井盖,实现对井盖的监测
    【论文笔记】Dynamic Convolution: Attention over Convolution Kernels
    【算法集训 | 暑期刷题营】7.20题---贪心
    C++设计模式-创建型设计模式:构建者(Builder)
    斯坦福抄袭清华、面壁智能大模型,当事人已道歉、删项目
  • 原文地址:https://blog.csdn.net/flysh05/article/details/133523301
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号