본문 바로가기

Python_Intermediate/WordCloud

kkma 한글 형태소 분석 후 워드클라우드 생성 - 대한민국헌법

반응형

1. import module

from konlpy.tag import Kkma
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from collections import Counter


2. 대한민국 헌법

대한민국헌법.txt


3. 한글 형태소 분석 Code

- TXT 파일 로드

text = ''
with open("data/대한민국헌법.txt", encoding="utf-8") as f:
text = f.read()


- 꼬꼬마 사용 변수 지정

kkma = Kkma()


- 문장 분석

sentences = kkma.sentences(text)
print(sentences)

sentences.txt


- 명사 분석

nouns = kkma.nouns(text)
print(nouns)

nouns.txt

- 형태소 분석

pos = kkma.pos(text)
print(pos)

pos.txt


- 명사 중에 길이가 1이상인것만 추출

words = []
for n in nouns:
if len(n) > 1:
words.append(n)

print(words)

words.txt


- 빈도수 계산

count = Counter(words)

counter.txt


- 상위 100건 추출

most = count.most_common(100)

most.txt


- 딕셔너리 구성

tags = {}
for n, c in most:
tags[n] = c

tag.txt


- 워드클라우드 생성

wc = WordCloud(font_path="NanumGothic", width=1200, height=800,
scale=2.0, max_font_size=250)

gen = wc.generate_from_frequencies(tags)

plt.figure()
plt.imshow(gen, interpolation='bilinear')
wc.to_file("korean.png")
plt.close()


반응형