config
import re
import os
### MeCab
POS_LIST = [10, 11, 31, 32, 34]
POS_LIST.extend(list(range(36,50)))
POS_LIST.extend([59, 60, 62, 67])
STOP_WORDS = ["Machen", "Abwesend", "Werden", "Bereits", "Shiyo", "Können", "Wurde", "Ku", "Schließlich", "Gibt es", "Kann", "Überlegen", "heute", "Es", "Dies", "Das", "welcher", "Welche", "NULL", "Sein", "Nari", "Ah", "Könnenる", "ich"]
RE_ALPHABET = re.compile("^[0-9a-zA-Z0-9 .,*<>]+$") # alphabet, number, space, comma or dot
current_dir = os.getcwd()
OUTPUT_PNG_FILE = os.path.join(current_dir, "wordcloud.png ")
(Weggelassen)
import MeCab
from os import path
from wordcloud import WordCloud
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import re
def create_mecab_list(text_list):
mecab_list = []
mecab = MeCab.Tagger("-Ochasen -d /usr/local/lib/mecab/dic/mecab-ipadic-neologd") # MacOS
mecab.parse("")
# encoding = text.encode('utf-8')
for text in text_list:
node = mecab.parseToNode(text)
while node:
# [Teil,Teil細分類1,Teil細分類2,Teil細分類3,Nutzungsart,Nutzungsart,Prototyp,lesen,Aussprache]
#Beschäftigtes Adjektiv,Unabhängigkeit,*,*,Adjektiv / Idan,Kontinuierliche Verbindung,beschäftigt,Isogasiku,Isogasiku
morpheme = node.feature.split(",")[6]
if RE_ALPHABET.match(morpheme):
node = node.next
continue
if morpheme in STOP_WORDS:
node = node.next
continue
if len(morpheme) > 1:
if node.posid in POS_LIST:
mecab_list.append(morpheme)
node = node.next
return mecab_list
wordcloud
import MeCab
from os import path
from wordcloud import WordCloud
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import re
def create_wordcloud(morphemes):
# fpath = "/usr/share/fonts/truetype/takao-gothic/TakaoPGothic.ttf" # Ubuntu
fpath = "/System/Library/Fonts/Hiragino Marugo ProN W4.ttc" # Mac OS X
wordcloud = WordCloud(
background_color="whitesmoke",
collocations=False,
stopwords=set(STOP_WORDS),
max_font_size=80,
relative_scaling=.5,
width=800,
height=500,
font_path=fpath
).generate(morphemes)
plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
wordcloud.to_file(OUTPUT_PNG_FILE)
Recommended Posts