{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# **Chapter 2 Visulaizaion**\n", "## **Word Cloud - 말뭉치 응용**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ! apt-get update\n", "# ! apt-get install g++ openjdk-8-jdk \n", "# ! pip3 install nltk konlpy wordcloud matplotlib gensim \n", "\n", "# ! apt-get install fonts-nanum*\n", "# ! apt-get install fontconfig\n", "# ! fc-cache -fv\n", "# ! cp /usr/share/fonts/truetype/nanum/Nanum* /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf/\n", "# ! rm -rf /content/.cache/matplotlib/*\n", "\n", "# speech_text = \"https://raw.githubusercontent.com/YongBeomKim/nltk_rnd/master/data/pyongyang_fin.txt\"\n", "# font_file = \"/usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf/NanumGothicCoding.ttf\"\n", "speech_text = \"../data/pyongyang_fin.txt\"\n", "font_file = \"../data/D2Coding.ttf\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# import nltk\n", "# nltk.download('punkt')\n", "\n", "import pandas as pd\n", "from nltk import FreqDist\n", "from nltk.tokenize import word_tokenize\n", "\n", "# for Colab\n", "# import requests\n", "# texts = requests.get(speech_text).text\n", "# texts[:100]\n", "\n", "# for LocalHost\n", "with open(speech_text, 'r') as f:\n", " texts = f.read()\n", "texts[:100]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "from wordcloud import WordCloud\n", "wcloud = WordCloud(font_file).generate(texts)\n", "plt.figure(figsize=(12,12))\n", "plt.imshow(wcloud)\n", "plt.axis(\"off\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from collections import Counter\n", "dictionary = Counter(texts.split())\n", "dictionary.most_common()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "## **명사만 추출하여 Wordcloud 만들기**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Twitter 모듈을 활용하여 명사만 추출\n", "from konlpy.tag import Okt \n", "twitter = Okt()\n", "tokens = twitter.pos(texts, stem=True)\n", "tokens_noun = [token[0] for token in tokens \n", " if token[1] == \"Noun\"]\n", "\n", "texts_noun = \" \".join(tokens_noun)\n", "texts_noun[:300]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "plt.figure(figsize=(12,12))\n", "wcloud = WordCloud(font_file).generate(texts_noun)\n", "plt.imshow(wcloud)\n", "plt.axis(\"off\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Token 빈도결과값\n", "from collections import Counter\n", "dictionary = Counter(texts_noun.split())\n", "dictionary.most_common()[:10]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.8" } }, "nbformat": 4, "nbformat_minor": 4 }