{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This version has been modified to read Little Women text from millerjw.com\n",
    "#  instead of from inferential thinking.\n",
    "from datascience import *\n",
    "import numpy as np\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plots\n",
    "plots.style.use('fivethirtyeight')\n",
    "import warnings\n",
    "warnings.simplefilter(action=\"ignore\", category=FutureWarning)\n",
    "\n",
    "from urllib.request import urlopen \n",
    "import re\n",
    "def read_url(url): \n",
    "    return re.sub('\\\\s+', ' ', urlopen(url).read().decode())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# little_women_url = 'https://www.inferentialthinking.com/data/little_women.txt'\n",
    "little_women_url = 'http://www.millerjw.com/dom/mgmt462/pfiles/little_women.txt'\n",
    "little_women_text = read_url(little_women_url)\n",
    "chapters = little_women_text.split('CHAPTER ')[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "chapters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Table().with_column('Text', chapters)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.char.count(chapters, 'e')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.char.count(chapters, 'Jo')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Table().with_columns(\n",
    "    'Jo', np.char.count(chapters, 'Jo'),\n",
    "    'Meg', np.char.count(chapters, 'Meg'),\n",
    "    'Amy', np.char.count(chapters, 'Amy'),\n",
    "    'Beth', np.char.count(chapters, 'Beth'),\n",
    "    'Laurie', np.char.count(chapters, 'Laurie')\n",
    " )   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Table().with_columns(\n",
    "    'Jo', np.char.count(chapters, 'Jo'),\n",
    "    'Meg', np.char.count(chapters, 'Meg'),\n",
    "    'Amy', np.char.count(chapters, 'Amy'),\n",
    "    'Beth', np.char.count(chapters, 'Beth'),\n",
    "    'Laurie', np.char.count(chapters, 'Laurie')\n",
    ").cumsum().plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualizing chapter length vs number of periods"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Table().with_columns([\n",
    "        'Chapter Length', [len(c) for c in chapters],\n",
    "        'Number of Periods', np.char.count(chapters, '.'),\n",
    "    ]).scatter('Number of Periods')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "woman_by_chapter = Table().with_columns(\n",
    "    'Jo', np.char.count(chapters, 'Jo'),\n",
    "    'Meg', np.char.count(chapters, 'Meg'),\n",
    "    'Amy', np.char.count(chapters, 'Amy'),\n",
    "    'Beth', np.char.count(chapters, 'Beth'),\n",
    "    'Laurie', np.char.count(chapters, 'Laurie')\n",
    " ) \n",
    "woman_by_chapter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "woman_by_chapter.cumsum().plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
