A set of script files that do wordcloud in Python3

Resource allocation composition

Terminal


$ tree
├── const.py
└── word_cloud.py

1 directory, 2 files

(Constant definition script)

For the word list of * stop_words *, refer to the following.

I created WordCloud with Python

const.py


fpath = "/Library/Fonts//Hiragino Maru Go ProN W4.ttc"
stop_words = ['Teru', 'Is', 'Become', 'To be','To do', 'is there', 'thing', 'this', 'Mr.', 'do it', 'くTo be', 'do', 'Give me',
              'so', 'Let', 'did', 'think', 'It', 'here', 'Chan', 'Kun', '', 'hand', 'To', 'To', 'Is', 'of', 'But',
              'Ta', 'Shi', 'so', 'Absent', 'Also', 'Nana', 'I', 'Or', 'のso', 'Yo', '']

(Main body script)

The text sentences selected for analysis of wordcloud are some sentences extracted from the "Affogato" item of * Wikipedia *.

The text may be modified so that it is read from a file in the current directory.

const.py


#!python3
import const, MeCab
from wordcloud import WordCloud
import matplotlib.pyplot as plt

#Initial definition of variables
colour = "red"
mode = "summer"

#Method definition
def create_word_cloud(string_text):
	tagger = MeCab.Tagger ("-Owakati")
	words_string = tagger.parse (string_text)
	if type(words_string) == str:
		wordcloud = WordCloud(background_color=colour, colormap=mode,width=900, height=500, font_path=const.fpath, stopwords=set(const.stop_words)).generate(words_string)
		return wordcloud


def render_word_cloud(wordcloud):
    plt.figure(figsize=(5,5))
    plt.imshow(wordcloud)
    plt.axis("off")
    plt.savefig("word_cloud.png ")


if __name__ == '__main__':
    text = """"Affogato" means "drown (ice cream)" in Italian. Espresso is the most popular drink to sprinkle, but there are many variations such as coffee, tea and liquor, and the name changes depending on the drink you sprinkle. In Japan, coffee affogato (Italian for affogato al caffè), which is vanilla ice cream with espresso coffee, is well known. From the above Italian notation and its pronunciation, it is wrong to say "affogato"."""
    wordcloud = create_word_cloud(text)
    render_word_cloud(wordcloud)

( Execution method )

Terminal


$ ls
const.py word_cloud.py

$ tree
├── const.py
└── word_cloud.py
$
$ python3 word_cloud.py
$ls
__pycache__ const.py word_cloud.png word_cloud.py
$

word_cloud.png

スクリーンショット 2020-11-14 21.56.38.png

Recommended Posts

A set of script files that do wordcloud in Python3
What's in that variable (when running a Python script)
Handling of JSON files in Python
A Python script that compares the contents of two directories
A Python script that stores 15 years of MLB game data in MySQL in 10 minutes (Baseball Hack!)
About psd-tools, a library that can process psd files in Python
A function that measures the processing time of a method in python
A python script that deletes ._DS_Store and ._ * files created on Mac
Get a list of files in a folder with python without a path
Output tree structure of files in Python
Display a list of alphabets in Python 3
Run the Python interpreter in a script
I made a script in python to convert .md files to Scrapbox format
Code reading of faker, a library that generates test data in Python
Set up a simple HTTPS server in Python 3
A set of integers that satisfies ax + by = 1.
Draw a graph of a quadratic function in Python
[Python] Get the files in a folder with Python
A memo that I wrote a quicksort in Python
Summary of how to import files in Python 3
Get the caller of a function in Python
A program that removes duplicate statements in Python
Make a copy of the list in Python
Set up a test SMTP server in Python.
Rewriting elements in a loop of lists (Python)
"Python Kit" that calls a Python script from Swift
Make a joyplot-like plot of R in python
Call a Python script from Embedded Python in C ++ / C ++
Output in the form of a python array
Set up a simple SMTP server in Python
A well-prepared record of data analysis in Python
How to get a list of files in the same directory with python
[Python] A program that counts the number of valleys
A script that takes a snapshot of an EBS volume
(Python) Treat integer values as a set of flags
A collection of code often used in personal Python
A memorandum to run a python script in a bat file
Python that merges a lot of excel into one excel
One liner that outputs 1000000 digits of pi in Python
In Python, create a decorator that dynamically accepts arguments Create a decorator
A shell program that becomes aho in multiples of 3
A script that outputs a list of SoftLayer portal users
Group by consecutive elements of a list in Python
Display a histogram of image brightness values in python
A collection of Excel operations often used in Python
A reminder about the implementation of recommendations in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
[Python] A program that compares the positions of kangaroos.
Set a proxy for Python pip (described in pip.ini)
MALSS, a tool that supports machine learning in Python
A python script that generates a sample dataset for checking the operation of a classification tree
A Python script that crawls RSS in Azure Status and posts it to Hipchat
A python script that gets the number of jobs for a specified condition from indeed.com
[Python] A program that finds the shortest number of steps in a game that crosses clouds
A memo that implements the job of loading a GCS file into BigQuery in Python
Consolidate a large number of CSV files in folders with python (data without header)
A shell script that numbers duplicate names when creating files
Take a screenshot in Python
A Python program in "A book that gently teaches difficult programming"
Find out the apparent width of a string in python
A general-purpose program that formats Linux command strings in python