Challenge text mining with Python. (For Python3 series) Follow the steps below.
① Morphological analysis (previous) ② Visualization with Word Cloud (this time) ③ Morphological analysis of Japanese documents and visualization with Word Cloud (next time)
According to the goo dictionary, "Select multiple words that appear frequently in a sentence and use the size according to the frequency. Illustrated method. (Omitted) You can impress the content of a sentence at a glance by changing not only the size of the characters but also the color, font, and orientation. "
In short, ↓ such a guy.
User Local has released Web Service for free, but I will try this with Python. ..
There seems to be word_cloud published by Andreas Mueller that can be used in Python, so this Try using.
It can be installed with pip.
sudo pip3 install wordcloud
I was able to install it smoothly, so I will try it out. It seems that it is necessary to pass a character string separated by a half-width space, so for the time being, in English. The subject is the beginning of President Trump's inauguration speech.
wordcloud_sample.py
# coding: utf-8
from wordcloud import WordCloud
text = "Chief Justice Roberts, President Carter, President Clinton, President \
Bush, President Obama, fellow Americans, and people of the world: \
thank you. We, the citizens of America, are now joined in a great \
national effort to rebuild our country and to restore its promise for \
all of our people. \
Together, we will determine the course of America and the world for \
years to come. \
We will face challenges. We will confront hardships. But we will get \
the job done. \
Every four years, we gather on these steps to carry out the orderly \
and peaceful transfer of power, and we are grateful to President Obama \
and First Lady Michelle Obama for their gracious aid throughout this \
transition. They have been magnificent."
wordcloud = WordCloud(background_color="white",
font_path="/usr/share/fonts/truetype/takao-gothic/TakaoPGothic.ttf",
width=800,height=600).generate(text)
wordcloud.to_file("./wordcloud_sample.png ")
To briefly explain, create a WordCloud object with the settings related to the image to be created as an argument, pass the character string to be drawn to the generate () method, and initialize it. It feels like outputting to an image file with the to_file () method. See Official Reference for constructor arguments.
The image created by moving the above sample is the one at the beginning.
python3 wordcloud_sample.py
-Visualize Twitter account using WordCloud in Python -[Visualize the frequency of word appearance in sentences with Word Cloud. \ Python ]
Recommended Posts