{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "tags": [
     "remove_input"
    ]
   },
   "outputs": [],
   "source": [
    "path_data = '../../data/'\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "plt.style.use('fivethirtyeight')\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\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": "markdown",
   "metadata": {},
   "source": [
    "# Plotting the classics\n",
    "In this example, we will explore statistics for two classic novels: *The Adventures of Huckleberry Finn* by Mark Twain, and *Little Women* by Louisa May Alcott. The text of any book can be read by a computer at great speed. Books published before 1923 are currently in the *public domain*, meaning that everyone has the right to copy or use the text in any way. [Project Gutenberg](http://www.gutenberg.org/) is a website that publishes public domain books online. Using Python, we can load the text of these books directly from the web.\n",
    "\n",
    "This example is meant to illustrate some of the broad themes of this text. Don't worry if the details of the program don't yet make sense. Instead, focus on interpreting the images generated below. Later sections of the text will describe most of the features of the Python programming language used below.\n",
    "\n",
    "First, we read the text of both books into lists of chapters, called `huck_finn_chapters` and `little_women_chapters`. In Python, a name cannot contain any spaces, and so we will often use an underscore `_` to stand in for a space. The `=` in the lines below give a name on the left to the result of some computation described on the right. A *uniform resource locator* or *URL* is an address on the Internet for some content; in this case, the text of a book. The `#` symbol starts a comment, which is ignored by the computer but helpful for people reading the code."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read two books, fast!\n",
    "\n",
    "huck_finn_url = 'https://www.inferentialthinking.com/data/huck_finn.txt'\n",
    "huck_finn_text = read_url(huck_finn_url)\n",
    "huck_finn_chapters = huck_finn_text.split('CHAPTER ')[44:]\n",
    "\n",
    "little_women_url = 'https://www.inferentialthinking.com/data/little_women.txt'\n",
    "little_women_text = read_url(little_women_url)\n",
    "little_women_chapters = little_women_text.split('CHAPTER ')[1:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "While a computer cannot understand the text of a book, it can provide us with some insight into the structure of the text. The name `huck_finn_chapters` is currently bound to a list of all the chapters in the book. We can place them into a table to see how each chapter begins."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "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>Chapters</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>I. YOU don't know about me without you have re...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>II. WE went tiptoeing along a path amongst the...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>III. WELL, I got a good going-over in the morn...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>IV. WELL, three or four months run along, and ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>V. I had shut the door to. Then I turned aroun...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>VI. WELL, pretty soon the old man was up and a...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>VII. \"GIT up! What you 'bout?\" I opened my eye...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>VIII. THE sun was up so high when I waked that...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>IX. I wanted to go and look at a place right a...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>X. AFTER breakfast I wanted to talk about the ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>XI. \"COME in,\" says the woman, and I did. She ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>XII. IT must a been close on to one o'clock wh...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>XIII. WELL, I catched my breath and most faint...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>XIV. BY and by, when we got up, we turned over...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>XV. WE judged that three nights more would fet...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>XVI. WE slept most all day, and started out at...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>XVII. IN about a minute somebody spoke out of ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>XVIII. COL. Grangerford was a gentleman, you s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>XIX. TWO or three days and nights went by; I r...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>XX. THEY asked us considerable many questions;...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>XXI. IT was after sun-up now, but we went righ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>XXII. THEY swarmed up towards Sherburn's house...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>XXIII. WELL, all day him and the king was hard...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>XXIV. NEXT day, towards night, we laid up unde...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>XXV. THE news was all over town in two minutes...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>XXVI. WELL, when they was all gone the king he...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>XXVII. I crept to their doors and listened; th...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>XXVIII. BY and by it was getting-up time. So I...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>XXIX. THEY was fetching a very nice-looking ol...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>XXX. WHEN they got aboard the king went for me...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>XXXI. WE dasn't stop again at any town for day...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>XXXII. WHEN I got there it was all still and S...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>XXXIII. SO I started for town in the wagon, an...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>XXXIV. WE stopped talking, and got to thinking...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>XXXV. IT would be most an hour yet till breakf...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>XXXVI. AS soon as we reckoned everybody was as...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>XXXVII. THAT was all fixed. So then we went aw...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>XXXVIII. MAKING them pens was a distressid tou...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>XXXIX. IN the morning we went up to the villag...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>XL. WE was feeling pretty good after breakfast...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>XLI. THE doctor was an old man; a very nice, k...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>XLII. THE old man was uptown again before brea...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>THE LAST THE first time I catched Tom private ...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                             Chapters\n",
       "0   I. YOU don't know about me without you have re...\n",
       "1   II. WE went tiptoeing along a path amongst the...\n",
       "2   III. WELL, I got a good going-over in the morn...\n",
       "3   IV. WELL, three or four months run along, and ...\n",
       "4   V. I had shut the door to. Then I turned aroun...\n",
       "5   VI. WELL, pretty soon the old man was up and a...\n",
       "6   VII. \"GIT up! What you 'bout?\" I opened my eye...\n",
       "7   VIII. THE sun was up so high when I waked that...\n",
       "8   IX. I wanted to go and look at a place right a...\n",
       "9   X. AFTER breakfast I wanted to talk about the ...\n",
       "10  XI. \"COME in,\" says the woman, and I did. She ...\n",
       "11  XII. IT must a been close on to one o'clock wh...\n",
       "12  XIII. WELL, I catched my breath and most faint...\n",
       "13  XIV. BY and by, when we got up, we turned over...\n",
       "14  XV. WE judged that three nights more would fet...\n",
       "15  XVI. WE slept most all day, and started out at...\n",
       "16  XVII. IN about a minute somebody spoke out of ...\n",
       "17  XVIII. COL. Grangerford was a gentleman, you s...\n",
       "18  XIX. TWO or three days and nights went by; I r...\n",
       "19  XX. THEY asked us considerable many questions;...\n",
       "20  XXI. IT was after sun-up now, but we went righ...\n",
       "21  XXII. THEY swarmed up towards Sherburn's house...\n",
       "22  XXIII. WELL, all day him and the king was hard...\n",
       "23  XXIV. NEXT day, towards night, we laid up unde...\n",
       "24  XXV. THE news was all over town in two minutes...\n",
       "25  XXVI. WELL, when they was all gone the king he...\n",
       "26  XXVII. I crept to their doors and listened; th...\n",
       "27  XXVIII. BY and by it was getting-up time. So I...\n",
       "28  XXIX. THEY was fetching a very nice-looking ol...\n",
       "29  XXX. WHEN they got aboard the king went for me...\n",
       "30  XXXI. WE dasn't stop again at any town for day...\n",
       "31  XXXII. WHEN I got there it was all still and S...\n",
       "32  XXXIII. SO I started for town in the wagon, an...\n",
       "33  XXXIV. WE stopped talking, and got to thinking...\n",
       "34  XXXV. IT would be most an hour yet till breakf...\n",
       "35  XXXVI. AS soon as we reckoned everybody was as...\n",
       "36  XXXVII. THAT was all fixed. So then we went aw...\n",
       "37  XXXVIII. MAKING them pens was a distressid tou...\n",
       "38  XXXIX. IN the morning we went up to the villag...\n",
       "39  XL. WE was feeling pretty good after breakfast...\n",
       "40  XLI. THE doctor was an old man; a very nice, k...\n",
       "41  XLII. THE old man was uptown again before brea...\n",
       "42  THE LAST THE first time I catched Tom private ..."
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Display the chapters of Huckleberry Finn in a dataframe.\n",
    "\n",
    "pd.DataFrame({'Chapters':huck_finn_chapters})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each chapter begins with a chapter number in Roman numerals, followed by the first sentence of the chapter. Project Gutenberg has printed the first word of each chapter in upper case. "
   ]
  }
 ],
 "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
