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 !

  • 相关阅读:
    Java设计模式—模板方法模式
    STM32 常用GPIO操作函数记录
    GPIO 配置之ODR, BSRR, BRR 详解
    STM32F4先设置寄存器还是先使能时钟
    LDR指令的格式:
    printf函数重定向
    stm32F4各个库文件的作用分析
    STM32F4时钟设置分析
    STM32F407存储器和总线架构
    SPI移位寄存器
  • 原文地址:https://www.cnblogs.com/chenqionghe/p/12312934.html
Copyright © 2011-2022 走看看