{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>.container { width:100% !important; }</style>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.core.display import display, HTML\n",
    "display(HTML(\"<style>.container { width:100% !important; }</style>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nltk\n",
    "import spacy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from nltk.corpus import gutenberg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from textblob import TextBlob as TB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "nlp = spacy.load('en')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[nltk_data] Downloading package gutenberg to\n",
      "[nltk_data]     C:\\Users\\thoma\\AppData\\Roaming\\nltk_data...\n",
      "[nltk_data]   Package gutenberg is already up-to-date!\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nltk.download('gutenberg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "emma = gutenberg.raw('austen-emma.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "parsed_emma = nlp(emma)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "sample_size = 100\n",
    "my_sample = random.sample(list(parsed_emma.sents), sample_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample = []\n",
    "for sent in parsed_emma.sents:\n",
    "    #sent = sent.replace('--', ' ')\n",
    "    sent = re.sub(\"\\s+\", \" \", sent.text)\n",
    "    sample.append(sent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "persons_per_sentence = []\n",
    "sentiments = []\n",
    "for sent in sample:\n",
    "    person_ents = []\n",
    "    parsed_sentence = nlp(sent)\n",
    "    tb_sentence = TB(sent)\n",
    "    sentiment = tb_sentence.sentiment.polarity\n",
    "    for ent in parsed_sentence.ents:\n",
    "        if ent.label_ == 'PERSON' and ent.text not in person_ents:\n",
    "            person_ents.append(ent.text)\n",
    "    if len(person_ents) > 1: \n",
    "        persons_per_sentence.append(person_ents)\n",
    "        sentiments.append(sentiment)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "import itertools\n",
    "\n",
    "relationships_counts = {}\n",
    "sentiments_vals = {}\n",
    "\n",
    "for (person_set, sentiment) in zip(persons_per_sentence, sentiments):\n",
    "    for combo in itertools.combinations(person_set, 2):\n",
    "        combo = tuple(sorted(combo))\n",
    "        if combo not in relationships_counts:\n",
    "            relationships_counts[combo] = 1\n",
    "            sentiments_vals[combo] = [sentiment]\n",
    "        else:\n",
    "            relationships_counts[combo] +=1\n",
    "            sentiments_vals[combo].append(sentiment)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "avg_sentiment = {k: np.mean(v) for k, v in sentiments_vals.items()}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels = sorted(relationships_counts.items(), key=lambda kv: -kv[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Emma', 'Weston') 0.11\n",
      "('Emma', 'Harriet') 0.19\n",
      "('Emma', 'Knightley') 0.10\n",
      "('Elton', 'Emma') 0.11\n",
      "('Elton', 'Harriet') 0.15\n",
      "('Emma', 'Woodhouse') 0.10\n",
      "('Elton', 'Weston') 0.11\n",
      "('Elton', 'Knightley') 0.20\n",
      "('Weston', 'Woodhouse') 0.04\n",
      "('Knightley', 'Woodhouse') 0.27\n",
      "('Knightley', 'Weston') 0.21\n",
      "('Emma', 'Frank Churchill') 0.16\n",
      "('Elton', 'Jane') 0.08\n",
      "('Bates', 'Emma') 0.07\n",
      "('Harriet', 'Knightley') 0.11\n",
      "('Emma', 'Jane') 0.20\n",
      "('Emma', 'Isabella') 0.21\n",
      "('Frank Churchill', 'Knightley') 0.33\n",
      "('Frank Churchill', 'Weston') 0.25\n",
      "('Emma', 'Jane Fairfax') 0.18\n"
     ]
    }
   ],
   "source": [
    "import networkx as nx\n",
    "\n",
    "G = nx.Graph()\n",
    "edge_labels = []\n",
    "\n",
    "rels_to_plot = 20\n",
    "for rel in rels[:rels_to_plot]:\n",
    "    #G.add_edge(rel[0][0], rel[0][1], weight=rel[1])\n",
    "    #edge_labels.append(rel[1])\n",
    "    G.add_edge(rel[0][0], rel[0][1], weight=avg_sentiment[rel[0]])\n",
    "    edge_labels.append(\"{:.2f}\".format(avg_sentiment[rel[0]]))\n",
    "    print(rel[0], \"{:.2f}\".format(avg_sentiment[rel[0]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x15700251c50>]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 20068 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(pd.Series(sentiments_vals[('Emma', 'Harriet')]).ewm(10).mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "str_labels = {k: \"{:.2f}\".format(v) for k,v in labels.items()}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 144616 characters]\n",
      "text/plain": [
       "<Figure size 1440x1080 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "pos = nx.spring_layout(G)\n",
    "\n",
    "plt.figure(figsize=(20, 15))\n",
    "\n",
    "nx.draw_networkx_nodes(G, pos, node_size=700)\n",
    "nx.draw_networkx_edges(G, pos, width=4, alpha = 0.5)\n",
    "nx.draw_networkx_labels(G, pos, font_size=20, font_family='sans-serif');\n",
    "\n",
    "labels = nx.get_edge_attributes(G,'weight')\n",
    "nx.draw_networkx_edge_labels(G, pos, edge_labels=str_labels)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
