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 = ["Faire", "Absent", "Devenir", "Déjà", "Shiyo", "Pouvez", "Devenu", "Ku", "finalement", "y a-t-il", "Peut", "pense", "aujourd'hui", "Il", "cette", "cette", "laquelle", "Lequel", "NULL", "Être", "Nari", "Ah", "Pouvezる", "je"]
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 ")
(Omis)
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:
# [Partie,Partie細分類1,Partie細分類2,Partie細分類3,Type d'utilisation,Type d'utilisation,Prototype,en train de lire,prononciation]
#Busy adjectif,Indépendance,*,*,Adjectif / Idan,Connexion continue,occupé,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)
--Ministère de la santé, du travail et de la protection sociale
Recommended Posts