栄達の神は待たない

とある大学生が自らの技術力の向上と知識を得るモチベーションUPのための自己満ブログ。

WordCloudで遊んでみる

簡単な動機

プログラムでなんかエモい感じのものを作りたくなったから。

暇つぶし。

WordCloudとは

ワードクラウド(WordCloud)

文章中で出現頻度が高い単語を複数選び出し、その頻度に応じた大きさで図示する手法。

コトバンクより

こういうことができるらしい

私が見たものは、赤ずきんちゃんのワードクラウドだったかな

一目惚れして作り方調べたら、Pythonのパッケージがあったから " やってみよう "ってなった

てことで準備する!

環境

windows 10

Python 3.7

wordcloud 1.6.0

今回はPythonでWordCloudをする

WordCloud 公式 https://amueller.github.io/word_cloud/

WordCLoudのインストール

コマンドプロンプトで入力

pip install wordcloud

準備完了

はやっwww

サンプルを動かす

サンプルコード (公式にあるやつ)

import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud

text = "square"

x, y = np.ogrid[:300, :300]

mask = (x - 150) ** 2 + (y - 150) ** 2 > 130 ** 2
mask = 255 * mask.astype(int)


wc = WordCloud(background_color="white", repeat=True, mask=mask)
wc.generate(text)

plt.axis("off")
plt.imshow(wc, interpolation="bilinear")
plt.show()
実行結果

f:id:utageer:20191223092455p:plain
wordcloudサンプル実行結果

DEKITA !!!

次回はオリジナルのをつくってみるよ