Python의 WordCloud에서 놀아 본 [제재 : 吾輩는 고양이이다]
계기
이벤트에 참가했을 때의 옆의 좌석 쪽이 사용하고 있었던 것을 보고 장래적으로 도움이 될 기술을 배울 수 있을 것이라고 생각해, 방법을 조사하면서 WordCloud 를 사용해 보았다. 문서는 →여기
WordCloud란?
문장 중에서 출현 빈도가 높은 단어를 선택하고, 그 빈도에 따른 크기로 도시하는 수법이다. 참조
했던 일
이번은 액배는 고양이인 소설을 바탕으로 WordCloud를 사용하여 간단한 이미지를 작성했다. (이하 참조)
※이미지
소설에 대해서는 푸른 하늘 문고의 것(전처리 실시)을 사용했다. 이하, 코드와 비망록이다.
문장 중에서 출현 빈도가 높은 단어를 선택하고, 그 빈도에 따른 크기로 도시하는 수법이다. 참조
했던 일
이번은 액배는 고양이인 소설을 바탕으로 WordCloud를 사용하여 간단한 이미지를 작성했다. (이하 참조)
※이미지
소설에 대해서는 푸른 하늘 문고의 것(전처리 실시)을 사용했다. 이하, 코드와 비망록이다.
from wordcloud import WordCloud
import MeCab
import re
from PIL import Image
import numpy as np
mask = np.array(Image.open('イメージ画像のパスを入力'))
mask = np.where(mask == 0, 255, 0)
with open("./前処理済/wagahaiwa_nekodearu.txt", mode='r', encoding='utf-8') as f:
text_list = [i.strip() for i in f]
text = "".join(text_list)
stop_words = ['する', 'せる', 'られる', 'あの', 'する', 'ある', 'とこ', 'なる', 'ない', 'ああ', 'れる', 'さん', 'やる', 'この', 'どう', 'そう']
font_path = "/Library/Fonts/BIZ-UDGothicR.ttc"
mecab = MeCab.Tagger("-Ochasen")
mecab_str = mecab.parse(text)
mecab_list = mecab_str.replace('\t', 't').split('\n')
lang_meta_list = [re.split('[t-]' ,lang_meta_str) for lang_meta_str in mecab_list]
for lang_meta in lang_meta_list :
if "助詞" in lang_meta :
continue
if "助動詞" in lang_meta :
continue
if "非自立" in lang_meta :
continue
if "サ変・スル" in lang_meta :
continue
if "サ変接続" in lang_meta :
continue
#if "一般" in lang_meta :
#continue
if "数" in lang_meta :
continue
if "記号" in lang_meta :
continue
else :
try:
lang = lang_meta[2]
print(lang_meta)
text_list.append("{}\t".format(lang))
except:
pass
text = ("").join(text_list)
wordcloud = WordCloud(font_path=font_path, font_step=1, mask=mask, background_color="white", contour_width=1, repeat=1, contour_color='purple', colormap='Praples', stopwords=set(stop_words)).generate(text)
wordcloud.to_file("./吾輩は猫である.png")
고배는 고양이입니다. png를 열면 방금 전
※이미지
출력됨
완료
요약
이번에는 소설의 문서를 사용해 고양이의 이미지를 마스크 이미지로 wordcloud를 실시했지만, 동화의 스토리를 사용해 그 동화에 나오는 캐릭터를 마스크 이미지로서 출현 빈도가 높은 단어로 가득 채우는 것도 흥미롭다고 생각했다.
이상
Reference
이 문제에 관하여(Python의 WordCloud에서 놀아 본 [제재 : 吾輩는 고양이이다]), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/xxx_boy/items/4e9a06499d8e0073ede9텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)