Text mining with Python ② Visualization with Word Cloud

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)

What is Word Cloud?

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. wordcloud_sample.png

User Local has released Web Service for free, but I will try this with Python. ..

Word Cloud library

There seems to be word_cloud published by Andreas Mueller that can be used in Python, so this Try using.

Install word_cloud

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

Referenced site

-Visualize Twitter account using WordCloud in Python -[Visualize the frequency of word appearance in sentences with Word Cloud. \ Python ]

Recommended Posts

Text mining with Python ② Visualization with Word Cloud
Text mining with Python ① Morphological analysis
Text mining with Python-Scraping-
Logistics visualization with Python
Text extraction with GCP Cloud Vision API (Python3.6)
Try it with Word Cloud Japanese Python JupyterLab.
Text mining with Python ① Morphological analysis (re: Linux version)
[Package cloud] Manage python packages with package cloud
Enable Python raw_input with Sublime Text 3
Speak Japanese text with OpenJTalk + python
Easy data visualization with Python seaborn.
Data analysis starting with python (data visualization 1)
Data analysis starting with python (data visualization 2)
Visualize the frequency of word occurrences in sentences with Word Cloud. [Python]
English speech recognition with python [speech to text]
Try using Python with Google Cloud Functions
3. Natural language processing with Python 1-1. Word N-gram
[GCP] Operate Google Cloud Storage with Python
Create a word frequency counter with Python 3.4
Recommendation of Altair! Data visualization with Python
[Automation] Read a Word document with Python
Create an English word app with python
Try text mining your diary in Python
Read text in images with python OCR
FizzBuzz with Python3
Scraping with Python
Extract text from PowerPoint with Python! (Compatible with tables)
Statistics with python
Scraping with Python
Python with Go
[Various image analysis with plotly] Dynamic visualization with plotly [python, image]
Twilio with Python
Integrate with Python
[Write to map with plotly] Dynamic visualization with plotly [python]
Create a python3 build environment with Sublime Text3
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Wav file generation from numeric text with python
Use Python / Django with Windows Azure Cloud Service!
Excel with Python
[Cloud102] # 1 Get Started with Python (Part 1 Python First Steps)
Microcomputer with Python
Cast with python
Python: Japanese text: Characteristic of utterance from word similarity
Run a machine learning pipeline with Cloud Dataflow (Python)
Easily try Amazon EMR / Cloud Dataproc with Python [mrjob]
[Blender] Complement Blender's Python API with a text editor
Challenge principal component analysis of text data with Python
Visualize keywords in documents with TF-IDF and Word Cloud
Python: Japanese text: Characteristic of utterance from word continuity
Set up a Python development environment with Sublime Text 2
Use python installed with Pyenv with Sublime REPL of Sublime Text 3
Generate Word Cloud from case law data in python3
Text extraction (Read API) with Azure Computer Vision API (Python3.6)
Serial communication with Python
Clustering text in Python