Skip to content

Instantly share code, notes, and snippets.

@Neko288
Created December 12, 2021 03:30
Show Gist options
  • Save Neko288/f6817410e0e37593062554f73219a755 to your computer and use it in GitHub Desktop.
Save Neko288/f6817410e0e37593062554f73219a755 to your computer and use it in GitHub Desktop.
Wordcloudを使う
#Mecabの辞書と、フォントを独自に用意する必要があります。Mecabの辞書の用意は正直かなりめんどうなのです・・・
#このサイトがPythonでのWordcloudの使い方を簡単に紹介しています。https://bit.ly/3GxUFZq
from wordcloud import WordCloud
import MeCab,AutoNaming
from matplotlib import pyplot as plt
from wordcloud import WordCloud
folder_pass = 'C:/any/Python-P/Worldcloud Project/' #このファイルのパス
with open('C:/XXX.txt', mode='rt', encoding='utf-8') as fi:
source_text = fi.read()
tagger = MeCab.Tagger('-d C:/XXX/XXX/mecab-ipadic-neologd')#Mecabの辞書
tagger.parse('')
node = tagger.parseToNode(source_text)
word_list = []
while node:
word_type = node.feature.split(',')[0]
if word_type == '名詞':
word_list.append(node.surface)
node = node.next
word_chain = ' '.join(word_list)
W = WordCloud(width=1920, height=1080, background_color='white', colormap='bone', font_path='C:/Windows/Fonts/yumin.ttf').generate(word_chain)
W.to_file(folder_pass+'images'+AutoNaming.naming(6) + ".png")
plt.imshow(W)
plt.axis('off')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment