python WordCloudワードクラウド図を描く

1826 ワード

目次
  • 前言
  • コアコード
  • テスト
  • 前言
    本や小説、映画のシナリオの内容を素早く理解したい場合は、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、今私たちは1段の文字を伝えて、語雲のピクチャーを生成して分析します
    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)

    実行結果は次のとおりです.
    この画像を分析すると、chenqiongheeは運動が好きで、筋肉があって、light weight baby!