Word Cloud Visualization

STEP

  • Text를 youtube_comment_crawling.csv(유튜브 댓글 크롤링)에서 댓글텍스트column만 추출한다.
  • Text를 스페이스 기준으로 끊는다.
    • collections 라이브러리 Counter 사용
    • "단어":카운트 형식
  • Word Cloud 라이브러리를 통해 시각화

.csv파일 불러오기

import pandas as pd
 
# 되도록 변수 이름은 df등 데이터프레임을 나타내는 변수 이름으로 정하자
comment_file = pd.read_csv("C:/Users/pps/Desktop/TIL/youtube_comments_crawling.csv", encoding= 'utf-8')

’댓글텍스트’ column만 텍스트로 불러오기

# iterrows 함수를 사용해 댓글텍스트만 텍스트로 불러오기
comment_slicing_list = []
for index, row in comment_file.iterrows():
    comment_slicing_list.extend(str(row['댓글텍스트']).split(' '))

텍스트 단어별 개수 세기

from collections import Counter
 
counts=Counter(comment_slicing_list)
cnt = len(comment_slicing_list)
tags = counts.most_common(cnt)
 

텍스트 데이터 시각화

  • pip install wordcloud
from wordcloud import WordCloud
import matplotlib.pyplot as plt
 
# word cloud 디자인만들기
# 한글이 깨지는 경우가 있으니 font를 지정해주는 것이 좋다.
wc = WordCloud(font_path='C:/Users/pps/AppData/Local/Microsoft/Windows/Fonts/NanumBarunGothic.ttf', background_color='white', width=800, height=600)
 
cloud = wc.generate_from_frequencies(dict(tags))
 
plt.figure(figsize=(10, 8))
plt.axis('off')
plt.imshow(cloud)
plt.show()

결과

  • “김하성”, “김하성” 등 조사로 인해 겹치는 부분이 생긴다.
  • 위의 겹치는 결과에 대해서 겹치는 것은 하나로 보이게 만들도록 하겠다.