-
Notifications
You must be signed in to change notification settings - Fork 145
/
simpsons-guests.Rmd
138 lines (114 loc) · 3.5 KB
/
simpsons-guests.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
---
title: "Simpsons Guests"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(tidyverse)
theme_set(theme_light())
simpsons <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-08-27/simpsons-guests.csv") %>%
mutate(self = str_detect(role, "self|selves"),
season = parse_number(season))
```
```{r}
simpsons %>%
filter(self) %>%
count(guest_star, sort = TRUE) %>%
filter(n > 1) %>%
mutate(guest_star = fct_reorder(guest_star, n)) %>%
ggplot(aes(guest_star, n)) +
geom_col() +
coord_flip() +
labs(title = "Who has played themselves in multiple Simpsons episodes?")
```
```{r}
simpsons %>%
separate_rows(role, sep = ";\\s+") %>%
add_count(role) %>%
filter(n >= 8) %>%
count(season, role) %>%
mutate(role = fct_reorder(role, -n, sum)) %>%
ggplot(aes(season, n)) +
geom_col() +
facet_wrap(~ role)
```
```{r}
simpsons
```
### Bringing in Simpsons dialogue
```{r}
dialogue <- read_csv("~/Downloads/simpsons_dataset.csv") %>%
select(role = raw_character_text, line = spoken_words)
guests_processed <- simpsons %>%
separate_rows(role, sep = ";\\s+") %>%
mutate(role = ifelse(self, guest_star, role),
role = ifelse(role == "Edna Krabappel", "Edna Krabappel-Flanders", role))
guests_summarized <- guests_processed %>%
filter(season <= 27) %>%
group_by(guest_star, role, self) %>%
summarize(nb_episodes = n(),
first_season = min(season),
last_season = max(season)) %>%
arrange(desc(nb_episodes)) %>%
group_by(role) %>%
filter(n() == 1) %>%
ungroup() %>%
filter(!is.na(role))
dialogue_summarized <- dialogue %>%
group_by(role) %>%
summarize(nb_lines = n(),
random_line = sample(line, 1)) %>%
arrange(desc(nb_lines))
guest_roles <- guests_summarized %>%
inner_join(dialogue_summarized, by = "role") %>%
mutate(lines_per_episode = nb_lines / nb_episodes)
guest_roles %>%
mutate(self = ifelse(self, "Playing Themselves", "Playing a Character")) %>%
ggplot(aes(lines_per_episode)) +
geom_histogram(binwidth = 2, center = 1) +
facet_wrap(~ self, ncol = 1) +
labs(x = "Average # of lines per episode",
title = "Most guest stars, especially those playing themselves, have relatively few lines per episode")
guest_roles %>%
arrange(desc(lines_per_episode))
```
```{r}
library(tidytext)
role_words <- dialogue %>%
filter(!is.na(line), !is.na(role)) %>%
mutate(line_number = row_number()) %>%
unnest_tokens(word, line) %>%
anti_join(stop_words, by = "word") %>%
distinct(role, line_number, word) %>%
count(role, word, sort = TRUE)
role_word_tf_idf <- role_words %>%
group_by(role) %>%
mutate(total_words = sum(n)) %>%
ungroup() %>%
bind_tf_idf(word, role, n) %>%
arrange(desc(tf_idf))
role_word_tf_idf %>%
filter(total_words >= 500) %>%
distinct(role, .keep_all = TRUE) %>%
mutate(role_word = paste0(role, ": ", word)) %>%
head(20) %>%
mutate(role_word = fct_reorder(role_word, tf_idf)) %>%
ggplot(aes(role_word, tf_idf)) +
geom_col() +
coord_flip() +
labs(title = "Using TF-IDF as a Simpsons catchphrase detector",
subtitle = "Only the 53 characters that speak at least 500 words in 27 seasons",
x = "",
y = "TF-IDF")
```
```{r}
guests_summarized %>%
filter(nb_episodes > 1) %>%
inner_join(role_word_tf_idf, by = "role") %>%
filter(total_words >= 100) %>%
arrange(desc(tf_idf)) %>%
distinct(role, .keep_all = TRUE) %>%
select(guest_star, role, word, tf_idf)
```