zoukankan      html  css  js  c++  java
  • python绘制WordCloud词云图

    前言

    当我们想快速了解书籍、小说、电影剧本中的内容时,可以绘制 WordCloud 词云图,显示主要的关键词(高频词),可以非常直观地看到结果

    核心代码

    from wordcloud import WordCloud
    import matplotlib.pyplot as plt
    import jieba
    from PIL import Image
    import numpy as np
    
    # 生成词云函数
    def create_word_cloud(words):
         # 使用结巴分词
         text = " ".join(jieba.cut(words,cut_all=False, HMM=True))
         wc = WordCloud(
               font_path="./wc.ttf",
               max_words=100,
               width=2000,
               height=1200,
        )
         wordcloud = wc.generate(text)
         # 写词云图片
         wordcloud.to_file("wordcloud.jpg")
         # 显示词云文件
         plt.imshow(wordcloud)
         plt.axis("off")
         plt.show()
    

    测试

    ok,现在我们来传入一段文字,生成词云图片分析一下

    s= """
    1.life lies in movement. 生命在于运动
    2.sport is the source of all life. 运动是生命的源泉.
    3.to keep on, day after day practice go down, and only activities to keep the enthusiasm of adequate training and improve motor skills.
    日复一日地坚持练下去吧,只有活动适量才能保持训练的热情和提高运动的技能.——塞涅卡
    4.activity is the basis of life! 活动是生活的基础!——歌德
    5.people's sound, not only by foods, especially to rely on motion.人的健全,不但靠饮食,尤靠运动.
    6.the olympic motto is "higher, faster, stronger." 奥林匹克的格言是“更高,更快,更强”.
    7.the health of the body for motionless and destruction, for sports practice and keep for a long time. --socrates 身体的健康因静止不动而破坏,因运动练习而长期保持.——苏格拉底
    chenqionghe
    chenqionghe
    chenqionghe
    chenqionghe
    chenqionghe
    muscle
    muscle
    muscle
    muscle
    muscle
    yeah buddy! light weight baby
    yeah buddy! light weight baby
    yeah buddy! light weight baby
    chenqionghe, go to the gym,yeah buddy! light weight baby
    I was in the gym lifting weights.
    """
    create_word_cloud(s)
    

    运行结果如下

    分析这张图片,不难看出:chenqionghe喜欢运动,有肌肉,light weight baby !

  • 相关阅读:
    信息产品是信息化理念的凝缩的精华
    自然科学技术表面上是反应人与自然的关系,更深层还是人与人之间的关系
    思与在,为何没有行
    haproxysocket 参数记录
    zabbix 监控 haproxy 记录
    Centos6.5安装OpenLDAP
    ansible mysql模块的使用今年
    haproxy 官方文档查看
    centos 7 部署 mysql 报错记录
    ansible playbook学习
  • 原文地址:https://www.cnblogs.com/chenqionghe/p/12312934.html
Copyright © 2011-2022 走看看