Pythonだけで作れる簡単な詞雲


一、準備
詞雲の作成に必要な三つのカバン:

  :pip install matplotlib
  :pip install jieba
  :pip install wordcloud
(他のカバンが必要かもしれません。pip install numpyなどは自分で追加できます。)
二、英語の雲
test.txtテキストの内容:

python python3 is good well bestbast shell cool
Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life became the appearance of the 
same day; May be backto oneself the paranoid weird belief disillusionment, these days, my mind has been very messy, in my mind constantly. Always 
feel oneself should go to do something, or write something. Twenty years of life trajectory deeply shallow, suddenly feel something, do it.The end 
of our life, and can meet many things really do?During myhood, think lucky money and new clothes are necessary for New Year, but as the advance of 
the age, will be more and more found that those things are optional; Junior high school, thought to have a crush on just means that the real growth,
but over the past three years later, his writing of alumni in peace, suddenly found that isn't really grow up, it seems is not so important; Then 
in high school, think don't want to give vent to out your inner voice can be in the high school children of the feelings in a period, but was event
ually infarction when graduation party in the throat, later again stood on the pitch he has sweat profusely, looked at his thrown a basketball hoops
, suddenly found himself has already can't remember his appearance.
ソースコード:

import matplotlib.pyplot as plt  #        ,      figure  ,                   
import jieba  # jieba           (                 )
from wordcloud import WordCloud  #   wordcloud 

text = open(r'test.txt', "r").read()  #   txt    ,          r R         ,                ,   r  “  ”
cut_text = jieba.cut(text)  #       ,     ,      ,       ,             
result = " ".join(cut_text)  #                    ,        
# join     :'sep'.join(seq)    :sep:   。    ;seq:        、   、  、  ; : sep     , seq               

#      ,        WordCloud       ,              
#        :              ,         ,      :mode='RGBA' colormap='pink'
wc = WordCloud(
    #     ,         
    background_color='white',  #      ,     
    width=500,  #      
    height=350,  #      
    max_font_size=50,  #     
    min_font_size=10,  #     
    mode='RGBA'  #     “RGBA”  background_color    ,     
)
wc.generate(result)  #             
wc.to_file(r"wordcloud.png")  #          
plt.imshow(wc)  #           
plt.axis("off")  #        ,       
plt.show()  # plt.imshow()           ,      ,      。     plt.show()    
効果の展示:
在这里插入图片描述
三、中国語の雲
pkq.jpgテンプレートの内容:
在这里插入图片描述
poem.txtテキストの内容:

          ,       。
          ,       。
       ,       。
       ,       。
       ,       。
   ,   ,   ,   。
     ,       。
       ,       。
       ,       。
       ,       。
       ,       。
   ,   ,
       ,       。
     ,     。
     ,     。
     ,       。
     ,       。
       ,       。
       ,       。
       ,       。
     ,     。
       ,       。
     ,     。
     ,     。
       ,       。
       ,       。
      ,      。
    ,    。    ,    。
       ,       。
       ,         。
       ,       。
      ,      。
      ,      。
       ,       。
       ,       ,
       。         ,
       
ソースコード:

import wordcloud
import numpy as np
from PIL import Image # Image    Python PIL         
import jieba

pic = Image.open("pkq.jpg")  #       ,    
shape = np.array(pic)  #          
wc = wordcloud.WordCloud(mask=shape, font_path="simkai.ttf", background_color="white",
                         max_font_size=100)  # mask     ,font_path   ,        

text = open(r'poem.txt', "r", encoding='UTF-8').read()  #             utf―8
cut_text = jieba.cut(text)
result = " ".join(cut_text)
wc.generate(result)
wc.to_file("cloud.jpg")
効果図:
在这里插入图片描述
ここでPythonだけで作成できる簡単な詞雲についての文章を紹介します。Pythonの詞雲の内容については、以前の文章を検索したり、次の関連記事を見たりしてください。これからもよろしくお願いします。