Terminal
$ tree
├── const.py
└── word_cloud.py
1 directory, 2 files
・ 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', '']
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)
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
Recommended Posts