{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "7Y3fA2ohpe7K"
   },
   "source": [
    "#Section 03. 워드 클라우드 시각화\n",
    "\n",
    "##1. 데이터 가져오기"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 19085,
     "status": "ok",
     "timestamp": 1684661867035,
     "user": {
      "displayName": "꾬은주",
      "userId": "05623544279730996499"
     },
     "user_tz": -540
    },
    "id": "Hpj6IP5ZpOY0",
    "outputId": "5e7842d2-5cea-45bf-a42d-dad794aee6ec"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "# proverb.txt\n",
    "contents=\"\"\" 작은 일에 성실한 사람은 큰일에도 성실하고,\n",
    "작은 일에 정직하지 못한 사람은 큰일에도 정직하지 못하다.\n",
    "누가복음 16:10\n",
    "변함없이 서로 사랑하는 이가 친구이며\n",
    "위급할 때 서로 돕는 이가 형제다.\n",
    "잠언 17:17\"\"\"\n",
    "file=open(\"./proverb.txt\", 'w') #mode w\n",
    "file.write(contents)\n",
    "file.close()\n",
    "# hello.txt\n",
    "file=open(\"./hello.txt\",'a')\n",
    "while 1:\n",
    "  content=input()\n",
    "  if not content: break\n",
    "  file.write(content+ '\\n')\n",
    "  print(content)\n",
    "file.close()\n",
    "\n",
    "# fruits.txt\n",
    "fruits=[\"apple\",\"apple\",\"apple\",\"banana\",\"banana\",\"orange\",\"mango\",\"orange\",\"orange\",\"orange\"]\n",
    "index=len(fruits)\n",
    "file=open(\"./fruits.txt\",'a')\n",
    "for i in range(len(fruits)):\n",
    "  if i < (index-1):\n",
    "    file.write(fruits[i]+'\\t')\n",
    "  else:\n",
    "    file.write(fruits[i]+'\\n')\n",
    "file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 283,
     "status": "ok",
     "timestamp": 1684661914333,
     "user": {
      "displayName": "꾬은주",
      "userId": "05623544279730996499"
     },
     "user_tz": -540
    },
    "id": "hOfS4vGErFsH",
    "outputId": "463faadc-ec09-4a32-cbec-2232f1b6d64b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['apple',\n",
       " 'apple',\n",
       " 'apple',\n",
       " 'banana',\n",
       " 'banana',\n",
       " 'orange',\n",
       " 'mango',\n",
       " 'orange',\n",
       " 'orange',\n",
       " 'orange',\n",
       " 'apple',\n",
       " 'apple',\n",
       " 'apple',\n",
       " 'banana',\n",
       " 'banana',\n",
       " 'orange',\n",
       " 'mango',\n",
       " 'orange',\n",
       " 'orange',\n",
       " 'orange',\n",
       " '안녕하세요',\n",
       " 'hello',\n",
       " '내용을',\n",
       " '추가합니다',\n",
       " '저장하는',\n",
       " '문서',\n",
       " '이름',\n",
       " \"'w'\",\n",
       " '대신',\n",
       " \"'a'를\",\n",
       " '사용합니다',\n",
       " '작은',\n",
       " '일에',\n",
       " '성실한',\n",
       " '사람은',\n",
       " '큰일에도',\n",
       " '성실하고',\n",
       " '작은',\n",
       " '일에',\n",
       " '정직하지',\n",
       " '못한',\n",
       " '사람은',\n",
       " '큰일에도',\n",
       " '정직하지',\n",
       " '못하다',\n",
       " '누가복음',\n",
       " '16:10',\n",
       " '변함없이',\n",
       " '서로',\n",
       " '사랑하는',\n",
       " '이가',\n",
       " '친구이며',\n",
       " '위급할',\n",
       " '때',\n",
       " '서로',\n",
       " '돕는',\n",
       " '이가',\n",
       " '형제다',\n",
       " '잠언',\n",
       " '17:17']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "import glob\n",
    "f1=[]\n",
    "for file in glob.glob('*.txt'):\n",
    "  with open(file, 'r', newline='') as filereader:\n",
    "    for row in filereader:\n",
    "      contents=row.split()\n",
    "      for i in contents:\n",
    "        contents=i.strip('.').strip(',').strip('')\n",
    "        f1.append(contents)\n",
    "f1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 268,
     "status": "ok",
     "timestamp": 1684661961776,
     "user": {
      "displayName": "꾬은주",
      "userId": "05623544279730996499"
     },
     "user_tz": -540
    },
    "id": "2rMO22mjrO7W",
    "outputId": "a1ec938c-53fb-4f9f-9535-b1a2931f0c53"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['apple',\n",
       " 'apple',\n",
       " 'apple',\n",
       " 'banana',\n",
       " 'banana',\n",
       " 'orange',\n",
       " 'mango',\n",
       " 'orange',\n",
       " 'orange',\n",
       " 'orange',\n",
       " 'apple',\n",
       " 'apple',\n",
       " 'apple',\n",
       " 'banana',\n",
       " 'banana',\n",
       " 'orange',\n",
       " 'mango',\n",
       " 'orange',\n",
       " 'orange',\n",
       " 'orange',\n",
       " '안녕',\n",
       " 'hello',\n",
       " '내용',\n",
       " '추가',\n",
       " '저장하는',\n",
       " '문서',\n",
       " '이름',\n",
       " 'w',\n",
       " '대신',\n",
       " 'a',\n",
       " '사용',\n",
       " '작은',\n",
       " '일',\n",
       " '성실한',\n",
       " '사람은',\n",
       " '큰일에도',\n",
       " '성실하고',\n",
       " '작은',\n",
       " '일',\n",
       " '정직하지',\n",
       " '못한',\n",
       " '사람은',\n",
       " '큰일에도',\n",
       " '정직하지',\n",
       " '못',\n",
       " '누가복음',\n",
       " '16:10',\n",
       " '변함없이',\n",
       " '서로',\n",
       " '사랑하는',\n",
       " '이가',\n",
       " '친구이며',\n",
       " '위급할',\n",
       " '때',\n",
       " '서로',\n",
       " '돕는',\n",
       " '이가',\n",
       " '형제',\n",
       " '잠언',\n",
       " '17:17']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "import glob\n",
    "\n",
    "f1=[]\n",
    "for file in glob.glob('*.txt'):\n",
    "  with open(file, 'r', newline='') as filereader:\n",
    "    for row in filereader:\n",
    "      contents=row.split()\n",
    "      for i in contents:\n",
    "        contents=i.strip('.').strip(',').strip('').strip('를').strip('을').strip(\"''\")\n",
    "        contents=contents.strip('합니다').strip('하세요')\n",
    "        contents=contents.strip('에')\n",
    "        f1.append(contents)\n",
    "f1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "iAtEAF6LrZnN"
   },
   "source": [
    "##2. 단어의 빈도수 구하기"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 319,
     "status": "ok",
     "timestamp": 1684661991554,
     "user": {
      "displayName": "꾬은주",
      "userId": "05623544279730996499"
     },
     "user_tz": -540
    },
    "id": "G0GFKEwDrd02",
    "outputId": "35c300ab-36d2-4ea0-ef4b-5ca1bca29b27"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('orange', 8),\n",
       " ('apple', 6),\n",
       " ('banana', 4),\n",
       " ('mango', 2),\n",
       " ('작은', 2),\n",
       " ('일', 2),\n",
       " ('사람은', 2),\n",
       " ('큰일에도', 2),\n",
       " ('정직하지', 2),\n",
       " ('서로', 2),\n",
       " ('이가', 2),\n",
       " ('안녕', 1),\n",
       " ('hello', 1),\n",
       " ('내용', 1),\n",
       " ('추가', 1),\n",
       " ('저장하는', 1),\n",
       " ('문서', 1),\n",
       " ('이름', 1),\n",
       " ('w', 1),\n",
       " ('대신', 1),\n",
       " ('a', 1),\n",
       " ('사용', 1),\n",
       " ('성실한', 1),\n",
       " ('성실하고', 1),\n",
       " ('못한', 1),\n",
       " ('못', 1),\n",
       " ('누가복음', 1),\n",
       " ('16:10', 1),\n",
       " ('변함없이', 1),\n",
       " ('사랑하는', 1),\n",
       " ('친구이며', 1),\n",
       " ('위급할', 1),\n",
       " ('때', 1),\n",
       " ('돕는', 1),\n",
       " ('형제', 1),\n",
       " ('잠언', 1),\n",
       " ('17:17', 1)]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import Counter\n",
    "f1\n",
    "word_counts=Counter(f1)\n",
    "dict(word_counts)\n",
    "# word_counts.items()\n",
    "wc=sorted(word_counts.items(),key=lambda count:count[1], reverse=True)\n",
    "wc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "executionInfo": {
     "elapsed": 1210,
     "status": "ok",
     "timestamp": 1684662004583,
     "user": {
      "displayName": "꾬은주",
      "userId": "05623544279730996499"
     },
     "user_tz": -540
    },
    "id": "1UrxrjQSrgOb"
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "wcdf=pd.DataFrame(wc)\n",
    "wcdf.columns=['word', 'freq']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 238
    },
    "executionInfo": {
     "elapsed": 292,
     "status": "ok",
     "timestamp": 1684662010992,
     "user": {
      "displayName": "꾬은주",
      "userId": "05623544279730996499"
     },
     "user_tz": -540
    },
    "id": "qnjW918priqJ",
    "outputId": "21217399-1c75-4fb4-89b2-67f7dd8e544a"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>freq</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>word</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>orange</th>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>apple</th>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>banana</th>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mango</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>작은</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        freq\n",
       "word        \n",
       "orange     8\n",
       "apple      6\n",
       "banana     4\n",
       "mango      2\n",
       "작은         2"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wcdf1=wcdf.set_index('word')\n",
    "wcdf1.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "s5GCbRdvrlLa"
   },
   "source": [
    "##3. 워드 클라우드 구현하기"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 11933,
     "status": "ok",
     "timestamp": 1684662045448,
     "user": {
      "displayName": "꾬은주",
      "userId": "05623544279730996499"
     },
     "user_tz": -540
    },
    "id": "CLQVtI3sroUQ",
    "outputId": "175c67bf-f92a-47f1-f151-a47f24a81bbe"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: wordcloud in c:\\python\\lib\\site-packages (1.9.2)\n",
      "Requirement already satisfied: pillow in c:\\python\\lib\\site-packages (from wordcloud) (9.3.0)\n",
      "Requirement already satisfied: matplotlib in c:\\python\\lib\\site-packages (from wordcloud) (3.6.2)\n",
      "Requirement already satisfied: numpy>=1.6.1 in c:\\python\\lib\\site-packages (from wordcloud) (1.23.5)\n",
      "Requirement already satisfied: fonttools>=4.22.0 in c:\\python\\lib\\site-packages (from matplotlib->wordcloud) (4.38.0)\n",
      "Requirement already satisfied: packaging>=20.0 in c:\\python\\lib\\site-packages (from matplotlib->wordcloud) (22.0)\n",
      "Requirement already satisfied: python-dateutil>=2.7 in c:\\python\\lib\\site-packages (from matplotlib->wordcloud) (2.8.2)\n",
      "Requirement already satisfied: pyparsing>=2.2.1 in c:\\python\\lib\\site-packages (from matplotlib->wordcloud) (3.0.9)\n",
      "Requirement already satisfied: contourpy>=1.0.1 in c:\\python\\lib\\site-packages (from matplotlib->wordcloud) (1.0.6)\n",
      "Requirement already satisfied: cycler>=0.10 in c:\\python\\lib\\site-packages (from matplotlib->wordcloud) (0.11.0)\n",
      "Requirement already satisfied: kiwisolver>=1.0.1 in c:\\python\\lib\\site-packages (from matplotlib->wordcloud) (1.4.4)\n",
      "Requirement already satisfied: six>=1.5 in c:\\python\\lib\\site-packages (from python-dateutil>=2.7->matplotlib->wordcloud) (1.16.0)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: You are using pip version 22.0.4; however, version 23.1.2 is available.\n",
      "You should consider upgrading via the 'C:\\python\\python.exe -m pip install --upgrade pip' command.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: konlpy in c:\\python\\lib\\site-packages (0.6.0)\n",
      "Requirement already satisfied: numpy>=1.6 in c:\\python\\lib\\site-packages (from konlpy) (1.23.5)\n",
      "Requirement already satisfied: JPype1>=0.7.0 in c:\\python\\lib\\site-packages (from konlpy) (1.4.1)\n",
      "Requirement already satisfied: lxml>=4.1.0 in c:\\python\\lib\\site-packages (from konlpy) (4.9.2)\n",
      "Requirement already satisfied: packaging in c:\\python\\lib\\site-packages (from JPype1>=0.7.0->konlpy) (22.0)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: You are using pip version 22.0.4; however, version 23.1.2 is available.\n",
      "You should consider upgrading via the 'C:\\python\\python.exe -m pip install --upgrade pip' command.\n"
     ]
    }
   ],
   "source": [
    "!pip install wordcloud\n",
    "!pip install konlpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 12252,
     "status": "ok",
     "timestamp": 1684662057694,
     "user": {
      "displayName": "꾬은주",
      "userId": "05623544279730996499"
     },
     "user_tz": -540
    },
    "id": "v1Kyd9k_rqeh",
    "outputId": "39517e55-c529-4282-87b0-aa832093867e"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "지정된 경로를 찾을 수 없습니다.\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import re\n",
    "# 한글 표현을 위해 폰트 설치\n",
    "%config InlineBackend.figure_format ='retina'\n",
    "!apt -qq -y install fonts-nanum > /dev/null\n",
    "import matplotlib.font_manager as fm\n",
    "# 나눔바른고딕 사용\n",
    "fontpath='C:/Windows/Fonts/NanumGothic.ttf'\n",
    "font=fm.FontProperties(fname=fontpath, size=9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 한글이 제대로 표시되지 않는다면 \n",
    "함께 첨부된 NanumGothic.ttf 파일에서 마우스 오른쪽 단추 [설치] 메뉴를 클릭하여 글꼴을 설치한후 실행합니다."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 435
    },
    "executionInfo": {
     "elapsed": 9126,
     "status": "ok",
     "timestamp": 1684662073828,
     "user": {
      "displayName": "꾬은주",
      "userId": "05623544279730996499"
     },
     "user_tz": -540
    },
    "id": "_ZNhn7hNrvF-",
    "outputId": "9b145156-9154-408d-806c-ee915e8c6b62"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 267296 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 418,
       "width": 425
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from wordcloud import WordCloud\n",
    "\n",
    "word_freq=wcdf1['freq'].to_dict()\n",
    "wordcloud=WordCloud(font_path='NanumGothic.ttf', width=1000, height=1000).generate_from_frequencies(word_freq)\n",
    "plt.imshow(wordcloud)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 330
    },
    "executionInfo": {
     "elapsed": 4080,
     "status": "ok",
     "timestamp": 1684662084631,
     "user": {
      "displayName": "꾬은주",
      "userId": "05623544279730996499"
     },
     "user_tz": -540
    },
    "id": "_vIccjlErzJv",
    "outputId": "e35711c0-5cd4-45cf-b2f5-c4609fee765d"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 309488 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 313,
       "width": 552
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "wordcloud=WordCloud(font_path='NanumGothic.ttf', background_color='white', colormap\n",
    "='autumn').generate_from_frequencies(word_freq)\n",
    "plt.imshow(wordcloud)\n",
    "plt.title('Word Frequency', size=13) # 제목 지정\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 465
    },
    "executionInfo": {
     "elapsed": 3519,
     "status": "ok",
     "timestamp": 1684662118694,
     "user": {
      "displayName": "꾬은주",
      "userId": "05623544279730996499"
     },
     "user_tz": -540
    },
    "id": "ewvEh6LYr3CH",
    "outputId": "2d4cc76a-f593-4aea-8227-2ecc78d64106"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 198972 characters]\n",
      "text/plain": [
       "<Figure size 600x600 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 448,
       "width": 484
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from PIL import Image\n",
    "import numpy as np\n",
    "im=Image.open('heart.png') # 이미지 파일 읽어오기\n",
    "mask_arr=np.array(im) # 픽셀 값 배열 형태 변환\n",
    "wordcloud=WordCloud(font_path='NanumGothic.ttf', background_color='white', colormap='autumn',width=700,height=700,random_state=43,mask=mask_arr,prefer_horizontal=True).generate_from_frequencies(word_freq)\n",
    "plt.figure(figsize=(6, 6))\n",
    "plt.imshow(wordcloud)\n",
    "plt.title('Word Frequency', size=13)\n",
    "plt.axis('off')\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "authorship_tag": "ABX9TyP5JzU/x1Z9XRkhhvAwCIxI",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
