{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Match: 100% in URL: artikel/artikel3.txt\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 189844 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Match: 100% in URL: artikel/artikel2.txt\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 199756 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Match: 100% in URL: artikel/artikel1.txt\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 221924 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Read input and preprocess data\n",
    "import csv\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from os import path\n",
    "from PIL import Image\n",
    "from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator\n",
    "from fuzzywuzzy import fuzz\n",
    "from ipywidgets import widgets\n",
    "from IPython.display import display, clear_output\n",
    "\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "% matplotlib inline\n",
    "\n",
    "with open('articles.csv', 'r') as csvfile:\n",
    "  data = [ f for f in csv.reader(csvfile) ]\n",
    "\n",
    "mapping = { f[0] : f[1] for f in data }\n",
    "\n",
    "search_box = widgets.Text(description=\"Search words:\", value=\"South Kensington, London\")\n",
    "display(search_box)\n",
    "\n",
    "def submit(sender):\n",
    "    clear_output()\n",
    "    \n",
    "    # Search inbox.\n",
    "    search = search_box.value\n",
    "\n",
    "    # Search all articles that have the word\n",
    "    ratio = [ (fuzz.token_set_ratio(f[1], search), f[0]) for f in data ]\n",
    "\n",
    "    ratio.sort(reverse=True)\n",
    "    stopwords = set(STOPWORDS)\n",
    "\n",
    "    for x in zip(ratio, range(1, 11)):\n",
    "        if x[0][0] < 30:\n",
    "            continue\n",
    "        print(\"Match: %d%% in URL: %s\" %(x[0]))\n",
    "        plt.figure(figsize = (10, 10))\n",
    "        wordcloud = WordCloud(stopwords=stopwords, background_color=\"white\").generate(mapping[x[0][1]])\n",
    "        plt.imshow(wordcloud, interpolation='bilinear', )\n",
    "        plt.axis(\"off\")\n",
    "        plt.show()\n",
    "\n",
    "search_box.on_submit(submit)"
   ]
  }
 ],
 "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.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
