본문 바로가기

Python_Intermediate/WordCloud

KoNLpy Korean WordCloud

반응형

Code>

from wordcloud import WordCloud
from matplotlib import pyplot
from collections import Counter
from konlpy.tag import Okt

text = ''
with open("파일경로", encoding="utf-8") as f:
text = f.read()

nlp = Okt()

nouns = nlp.nouns(text)

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

count = Counter(words)

most = count.most_common(100)

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

wc = WordCloud(background_color='white', font_path="출력결과물에 사용할 한글 폰트", width=311, height=512,
scale=2.0, max_font_size=250)

gen = wc.generate_from_frequencies(tags)

pyplot.figure()
pyplot.imshow(gen, interpolation='bilinear')
wc.to_file("출력파일명")
pyplot.close()


정리>

text = ''
with open("파일경로", encoding="utf-8") as f:
text = f.read()

파일경로에 있는 파일을 읽어 들여서 그 안에 내용을 text 함수에 저장한다.


nlp = Okt()
nouns = nlp.nouns(text)

형태소 분석 클래스 객체를 생성한다. 명사들만 추출하여 리스트형식으로 반환 한다.


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

글자 단위가 한개인 단어는 필요 없는 단어로 간주하여 두 글자 이상 단어만 추출한다.


count = Counter(words)
most = count.most_common(100)

단어의 빈도수를 계산하여 상위에서 100건만 추출한다.

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

wc = WordCloud(background_color='white', font_path="출력결과물에 사용할 한글 폰트", width=311, height=512,
scale=2.0, max_font_size=250)

gen = wc.generate_from_frequencies(tags)

pyplot.figure()
pyplot.imshow(gen, interpolation='bilinear')
wc.to_file("출력파일명")
pyplot.close()

빈도수 상위 100건의 데이터를 딕셔너리 형식으로 지정하여 WordCloud에서 로드한다.

각 옵션을 설정하여 출력결과물을 만든다.

background_color : 백그라운드 색상 / font_path : 출력결과물에 사용할 한글 폰트에 영문 이름 / width : 폭 / height : 높이


예제 출력물>




반응형

'Python_Intermediate > WordCloud' 카테고리의 다른 글

190428>Korea News keyword wordcloud  (0) 2019.04.28
Chinese Character WordCloud  (0) 2019.04.20
KoNLpy  (0) 2019.04.20
KoNLpy JAVA Environment Variable Error Dissolvent  (0) 2019.04.20
WordCloud Font HSL Color  (0) 2019.04.20