티스토리 뷰

카테고리 없음

[자연어처리]WordCloud

코딩계란 2024. 3. 22. 12:27
728x90

개요 

  • TagCloud 라고도 하는데 메타 데이터에서 얻어진 태그들을 분석해서 중요도 나 인기도 등을 고려해서 시각적으로 늘어 놓아 표시한 것
  • 각 태그들은 등장 횟수(중요도) 등에 따라 글자의 크기나 색상을 달리해서 표현
  • 여러가지 패키지가 존재

워드클라우드

워드클라우드 생성 사이트 

 

워드클라우드

워드클라우드 워클생성기 워클 단어구름 한글 워드클라우드 구름단어 글자구름 구름글자 태그클라우드 워드클라우드 태그구름 랜덤이미지 블로그이미지 페이스북이미지

wordcloud.kr

 

TagCloud

1. 사용 준비

  •  패키지 설치: pytagcloud, pygame, simplejson
  •  한글 폰트를 사용하고자 하면 pytagcloud 가 설치된 디렉토리의 fonts 디렉토리에 한글 폰트 파일을 복사하고 Fonts.json 파일에 등록

2. 작업 순서 

  • 단어들의 list를 생성
  • Collections 모듈의 Counter(dict 형태로 각 데이터의 개수를 저장할 수 있는 클래스)를 이용해서 각 단어의 개수를 가지는 Counter 객체를 생성
  • 필요한 경우에는 most_common 메서드를 이용해서 단어 와 단어별 개수를 갖는 list 객체를 생성
  • pytagcloud.make_tags(튜플의 list, maxsize=글자크기의 최대 사이즈)를 호출하여 워드클라우드를 그릴 수 있는 dict 의 list 생성
  • pytagcloud.create_tag_image(dict의 list, 그림 파일 경로, size=(너비, 높이), fontname='폰트이름', rectangular=사각형 출력 여부)

3. 필요한 패키지 설치

pip install pytagcloud
pip install pygame
pip install simplejson

 

한글을 사용하고자 하는 경우 한글 폰트를 pytagcloud 의 fonts 디렉토리에 복사
복사한 폰트를 등록 - Fonts.json

 

4. 실습

import pytagcloud
import collections

# 단어 리스트 생성
nouns = list()
nouns.extend(['apple' for t in range(8)])
nouns.extend(['banana' for t in range(7)])
nouns.extend(['kiwi' for t in range(7)])
nouns.extend(['pineapple' for t in range(6)])
nouns.extend(['watermelon' for t in range(6)])
nouns.extend(['melon' for t in range(6)])
nouns.extend(['strawberry' for t in range(6)])
nouns.extend(['orange' for t in range(5)])
nouns.extend(['mango' for t in range(5)])

#데이터 개수 세기
count = collections.Counter(nouns)
print(count)
#필요한 개수만큼 추출
tag2 = count.most_common(100)

#태그 목록 만들기
taglist = pytagcloud.make_tags(tag2, maxsize=50)
print(taglist)

#태그 클라우드 생성
pytagcloud.create_tag_image(taglist, 'wordcloud.png', size=(900, 600), fontname='Nobile', rectangular=False)
더보기
Counter({'apple': 8, 'banana': 7, 'kiwi': 7, 'pineapple': 6, 'watermelon': 6, 'melon': 6, 'strawberry': 6, 'orange': 5, 'mango': 5})
[{'color': (111, 220, 182), 'size': 106, 'tag': 'apple'}, {'color': (78, 159, 86), 'size': 95, 'tag': 'banana'}, {'color': (36, 90, 23), 'size': 95, 'tag': 'kiwi'}, {'color': (138, 88, 165), 'size': 84, 'tag': 'pineapple'}, {'color': (163, 59, 159), 'size': 84, 'tag': 'watermelon'}, {'color': (51, 52, 79), 'size': 84, 'tag': 'melon'}, {'color': (169, 89, 20), 'size': 84, 'tag': 'strawberry'}, {'color': (82, 132, 218), 'size': 73, 'tag': 'orange'}, {'color': (159, 33, 114), 'size': 73, 'tag': 'mango'}]
import matplotlib.pyplot
import matplotlib.image

img = matplotlib.image.imread('wordcloud.png')
imgplot = matplotlib.pyplot.imshow(img)
matplotlib.pyplot.show()

 

WordCloud

1. 설치 

pip install wordCloud

 

2. 실습

from wordcloud import WordCloud

# 워드클라우드를 만들 텍스트 생성
text =''

text = ''
for t in range(8):
    text = text + 'Python '
for t in range(7):
    text = text + 'Java '
for t in range(7):
    text = text + 'C '
for t in range(8):
    text = text + 'JavaScript '
for t in range(5):
    text = text + 'C# '
for t in range(3):
    text = text + 'Ruby '
for t in range(2):
    text = text + 'scala '
for t in range(6):
    text = text + 'PHP '
for t in range(3):
    text = text + 'Swift '
for t in range(3):
    text = text + 'Kotlin ' 
for t in range(3):
    text = text + '자바'

#워드 클라우드 만들기
#한글을 출려갛고자 하는 경우는 font_path 에 한글폰트 파일 경로를 설정
wordcloud = WordCloud(background_color='white', max_words=2000)
wordcloud = wordcloud.generate(text)
print(wordcloud.words_)

#워드클라우드에 화면에 출력
plt.figure(figsize=(12,12))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()

 

728x90