---
title: "Lab 11.1 - Life and Death in King Lear"
author: "Author Goes Here"
date: "Semester Goes Here"
output:
  html_document:
    code_download: true
---

```{r include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r warning = FALSE, message = FALSE}
library(tidyverse)
library(rvest)
```

The goal of this exercise is to learn how to use regular expressions to filter text. It will do an analysis similar to the analysis of *MacBeth* described in the text (see [Section 19.1](https://mdsr-book.github.io/mdsr2e/ch-text.html)) but this time on *King Lear*, another Shakespeare tragedy not lacking in death.

## Loading the King Lear Text

We can retrieve this text from Project Gutenberg using rvest, Tidyverse's Web scraping library. For now, you can just use this code; we'll study it in more detail when we get to the Web scraping unit.

```{r}
lear_url <- "http://www.gutenberg.org/cache/epub/1532/pg1532.txt"
lear_text <- read_html(lear_url) %>%
  html_nodes("body") %>%
  html_nodes("p") %>%
  html_text()
substring(lear_text, 1, 200)
```

Now we split the raw text into lines:

```{r}
lear <- lear_text %>% 
  str_split("\r\n") %>%
  pluck(1)
length(lear)
```

## Computing Interesting Statistics

🚧
How many lines were spoken by Edgar? You'll need to take a look at the raw text of the play to see how the formatting for speeches in *King Lear* differs from their formatting in *MacBeth* as assumed by the text example. N.b., the character name is always on a separate line ended with a period (`.`).

```{r}
pattern <- "^EDGAR\\.$"
lear %>% str_subset(pattern) %>% length()
```

🚧
How many unique speakers have lines? You'll need to search for character names, which are of varying lengths and are formatted in all capital letters. Use the `unique()` function to filter out repeated names and use `sort()` can help with readability.


🚧
Modify the regex `^la{2,3}` (given below) to match `lala` and 'lalala` but not `lalalala`.

```{r}
pattern <- "^la{2,3}"
c("", "la", "laa", "lala", "lalala", "lalalala", "blalalala") %>% str_subset(pattern)
```

🚧
Give an example of a string that matches this regex: `\\d{3}-\\d{3}-\\d{4}`. Hint: you probably know several of these from memory and share them with others regularly.


🚧
In a programming course, you might have written a function that checks whether a string was a valid US social security number (e.g., `123-45-6789`). Write a regular expression that does the same thing and test it on few well-chosen test cases.

🚧
Reproduce the life &amp; death plot demonstrated in the text. Choose any characters you'd like to trace, but include King Lear and his oldest daughter Regan.
