[PYTHON] If you want to create a Word Cloud.

Thing you want to do

――Create Word Cloud with as few steps as possible. (Please when it is enough to make it once)

point

--Perform morphological analysis with janome --If it is MeCab, it is necessary to set it so that it can be seen from python. --Font settings for Word Cloud

environment

Library setup If you have Anaconda, is it okay in such a place?

pip install janome
pip install wordcloud

Environmental setting

import pandas as pd

from janome.tokenizer import Tokenizer

import matplotlib.pyplot as plt
from wordcloud import WordCloud

Preparation from data reading

--Prepare such data (sample.csv). Although I say csv, I don't use commas, but ...

sample.csv


Lion may be a panda
No, that's not the case
 :
##Data read
df = pd.read_csv('sample.csv', header=None)
##Give a title
df.colums = ['sentences']

##Function group definition
def get_nouns(sentence, noun_list):
    for token in t.tokenize(sentence):
        split_token = token.part_of_speech.split(',')
        ##Extract general nouns
        if split_token[0] == 'noun' and split_token[1] == 'General':
            noun_list.append(token.surface)

def depict_word_cloud(noun_list):
    ##Space-separate noun list elements(word_cloud specifications)
    noun_space = ' '.join(map(str, noun_list))
    ##word cloud settings(Font settings)
    wc = WordCloud(background_color="white", font_path=r"C:/WINDOWS/Fonts/msgothic.ttc", width=300,height=300)
    wc.generate(noun_space)
    ##Specifying the size of the output image
    plt.figure(figsize=(5,5))
    ##Delete scale
    plt.tick_params(labelbottom=False,
                    labelleft=False,
                    labelright=False,
                    labeltop=False,
                   length=0)
    ##Display of word cloud
    plt.imshow(wc)
    plt.show()

Run when you're ready

##Preparation for morphological analysis
t = Tokenizer()

noun_list = []
for sentence in list(df['sentences']):
    get_nouns(sentence, noun_list)

depict_word_cloud(noun_list)

result

Success if this happens: image.png

Recommended Posts

If you want to create a Word Cloud.
Scraping your Qiita articles to create a word cloud
[Python] If you suddenly want to create an inquiry form
I made a tool to create a word cloud from wikipedia
If you want to assign csv export to a variable in python
I want to create a window in Python
[Django] What to do if the model you want to create has a large number of fields
[Python] If you want to draw a scatter plot of multiple clusters
How to remember when you forget a word
I want to create a plug-in type implementation
If you were to create a TODO application (distributed) using only Python-extension 1
Create a Word Cloud from an academic program
If you want to make a TODO application (distributed) now using only Python
If you want to display values using choices in a template in a Django model
If you want to make a discord bot with python, let's use a framework
If you want to use Cython, also include python-dev
When you want to play a game via Proxy
I want to manually create a legend with matplotlib
When you want to plt.save in a for statement
[Django] A memorandum when you want to communicate asynchronously [Python3]
If you want your colleagues to use the same language
When you want to hit a UNIX command on Python
I want to create a Dockerfile for the time being.
Steps to create a Django project
How to create a Conda package
How to create a virtual bridge
Key operations you want to know
How to create a Dockerfile (basic)
5 Ways to Create a Python Chatbot
How to create a config file
Don't write Python if you want to speed it up with Python
What to do if you get a minus zero in Python
Don't you want to say that you made a face recognition program?
Create a word cloud with only positive / negative words on Twitter
Qiskit: I want to create a circuit that creates arbitrary states! !!
I want to know if you install Python on Mac ・ Iroha
I want to create a system to prevent forgetting to tighten the key 1
What to do if you get a UnicodeDecodeError with pip install
I want to create a pipfile and reflect it in docker
[TensorFlow] If you want to run TensorBoard, install it with pip
I want to create a machine learning service without programming! WebAPI
Check if you can connect to a TCP port in Python
When you want to sort a multidimensional list by multiple lines
If you try to make a word cloud with comments from WEB manga, it is interesting to visually understand what kind of manga it is.
I tried to create a linebot (implementation)
How to make Word Cloud characters monochromatic
Workaround if you cannot add to LD_LIBRARY_PATH
Want to solve a simple classification problem?
How to create a clone from Github
Create a bot to retweet coronavirus information
If you want a singleton in python, think of the module as a singleton
How to create a git clone folder
I want to print in a comprehension
I tried to create a linebot (preparation)
Gist repository to use when you want to try a little with ansible
If you want to include awsebcli with CircleCI, specify the python version
If you want to switch the execution user in the middle of a Fabric task, settings context manager
I want to build a Python environment
If you want to get multiple statistics with groupby in pandas v1
What to do if you get a TypeError with numpy min, max
Create a word frequency counter with Python 3.4