<jupyter_start><jupyter_text>0. Setting<jupyter_code>from google.colab import drive
drive.mount('/content/gdrive/')
import pandas as pd
import numpy as np
import os
os.chdir('/content/gdrive/My Drive/ringle')<jupyter_output><empty_output><jupyter_text>1. 텍스트 데이터 불러오기<jupyter_code>data = pd.read_csv('blog review3.csv', encoding='cp949')
data
data = data.drop('출처', axis=1)
data['review'][0]<jupyter_output><empty_output><jupyter_text>2. 텍스트 데이터 정제하기<jupyter_code>data['review'] = data['review'].str.replace("[^ㄱ-ㅎㅏ-ㅣ가-힣 ]","")
# 한글과 공백을 제외하고 모두 제거
data[:5]
reviews = [] #리스트로 저장
for i in range(len(data)):
  review = data['review'][i]
  reviews.append(review)
reviews[0]
text=''
for i in range(len(reviews)):
  text = text + " " + (reviews[i])

text
! pip install konlpy
import sys
from konlpy.tag import Okt
from collections import Counter
from wordcloud import wordcloud

def get_noun(text):
  okt = Okt()
  noun = okt.nouns(text)
  for i,v in enumerate(noun):
    if len(v)<2:
      noun.pop(i)
  count = Counter(noun)
  noun_list = count.most_common(100)

  return noun_list<jupyter_output>Collecting konlpy
  Downloading konlpy-0.5.2-py2.py3-none-any.whl (19.4 MB)
[K     |████████████████████████████████| 19.4 MB 134 kB/s 
[?25hRequirement already satisfied: numpy>=1.6 in /usr/local/lib/python3.7/dist-packages (from konlpy) (1.19.5)
Requirement already satisfied: tweepy>=3.7.0 in /usr/local/lib/python3.7/dist-packages (from konlpy) (3.10.0)
Requirement already satisfied: lxml>=4.1.0 in /usr/local/lib/python3.7/dist-packages (from konlpy) (4.2.6)
Collecting colorama
  Downloading colorama-0.4.4-py2.py3-none-any.whl (16 kB)
Collecting JPype1>=0.7.0
  Downloading JPype1-1.3.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (448 kB)
[K     |████████████████████████████████| 448 kB 68.5 MB/s 
[?25hCollecting beautifulsoup4==4.6.0
  Downloading beautifulsoup4-4.6.0-py3-none-any.whl (86 kB)
[K     |████████████████████████████████| 86 kB 3.7 MB/s 
[?25hRequirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from JPype1>=0.7.0->kon[...]<jupyter_text>3. 워드클라우드 생성<jupyter_code>def visualize(noun_list):
  from wordcloud import WordCloud
  wc= WordCloud(
      font_path = 'a고딕13.ttf',    # 맥에선 한글폰트 설정 잘해야함.
      background_color='white',                             # 배경 색깔 정하기
      colormap = 'Accent_r',                                # 폰트 색깔 정하기
      width = 800,
      height = 800)
  wc.generate_from_frequencies(dict(noun_list))
  wc.to_file('keyword7_blog3.png')
def visualize(noun_list):
  '''워드 클라우드 만들고 현제 파일 경로에 워늗클라우드 이미지 파일 저장해주는 함수'''
  from wordcloud import WordCloud
  wc= WordCloud(
      font_path = 'a고딕13.ttf',    # 맥에선 한글폰트 설정 잘해야함.
      background_color='white',                             # 배경 색깔 정하기
      colormap = 'PuBu',                                # 폰트 색깔 정하기
      width = 800,
      height = 800)
  wc.generate_from_frequencies(dict(noun_list))
  wc.to_file('저장할_이름.png')
noun_list = get_noun(text)
visualize(noun_list)
noun_list
noun_list2 = noun_list[5:] # 불필요하게 빈도수 많은 맨 상위 5개 단어 삭제
visualize(noun_list2)
def visualize_in(noun_list):
  ''' 바로 확인 가능하게 생성된 워드클라우드 이미지를 코드 내에서 반환하는 함수'''
  from wordcloud import WordCloud
  %matplotlib inline 
  import matplotlib.pyplot as plt

  wc= WordCloud(
      font_path = 'a고딕13.ttf',    # 맥에선 한글폰트 설정 잘해야함.
      background_color='white',                             # 배경 색깔 정하기
      colormap = 'PuBu',                                # 폰트 색깔 정하기
      width = 800,
      height = 800)
  image = wc.generate_from_frequencies(dict(noun_list))  
  #wc.to_file('keyword7_blog3_p3.png')
  return image
image = visualize_in(noun_list2)
#plt.show(image)

plt.figure(figsize=(6,6)) #이미지 사이즈 지정
plt.imshow(image, interpolation='lanczos') #이미지의 부드럽기 정도
plt.axis('off') #x y 축 숫자 제거
plt.show()  # 워드클라우드 이미지 확인
#plt.savefig()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1531739912618864, 'fraction_numerical': 0.04034952454381907, 'mean_word_length': 3.422727272727273, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '597220', 'license': 'MIT', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1955, 'n_words': 324, 'path': 'Ringle_Review_Wordcloud.ipynb', 'repo_name': 'seoyunion/Ringle-contest', 'size': '3891'}