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 !

  • 相关阅读:
    Android:使用 DownloadManager 进行版本更新
    Android:UI 沉浸式体验,适合第一屏的引导图片、预览图片。
    Android:相机适配及图片处理的一些问题
    Android: 设置 app 字体大小不跟随系统字体调整而变化
    Android: TextView 及其子类通过代码和 XML 设置字体大小的存在差异的分析
    SQLMap 学习
    我的书单
    macos
    linux BufferedImage.createGraphics()卡住不动
    Linux 中ifconfig和ip addr命令看不到ip
  • 原文地址:https://www.cnblogs.com/chenqionghe/p/12312934.html
Copyright © 2011-2022 走看看