-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_scrap_transcripts.Rmd
201 lines (130 loc) · 5.28 KB
/
03_scrap_transcripts.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
---
title: "Scrap episode transcripts"
output: html_notebook
editor_options:
chunk_output_type: inline
---
Aim:
* Scrap and clean episode transcripts from genius.com;
* Relate all characters that were scrapped in script '02_scrap_characters_list.Rmd' to those in transcripts.
```{r eval=TRUE, warning=FALSE, message=FALSE}
rm(list = ls())
library(httr)
library(tidytext)
library(knitr)
library(xml2)
library(rebus)
library(stringr)
library(rvest)
library(magrittr)
library(tidyverse)
```
#Scrap list of GoT episodes
I manually limited it at 67
```{r eval=TRUE, warning=FALSE, message=FALSE}
if(!"episode_list_03.RDS" %in% list.files("interim_output/")){
episode.list <- read_html("https://en.wikipedia.org/wiki/List_of_Game_of_Thrones_episodes")
episode.list %<>%
html_nodes( ".summary") %>%
html_text() %>%
str_replace_all(pattern = rebus::or("[[:punct:]]", "[[:cntrl:]]"),
replacement = "")
episode.list %>%
write_rds("interim_output/episode_list_03.RDS")
}else{
episode.list <- read_rds("interim_output/episode_list_03.RDS")[1:67]
}
```
# Scrap GoT transcripts from genius.com
There seem to be two different patterns in the links of genius.com
Ones end with 'script-annotated' and others just with 'annotated'.
e.g. "https://genius.com/Game-of-thrones-episode-annotated" and "https://genius.com/Game-of-thrones-episode-script-annotated"
## Create hypothetical links to transcript websites
```{r eval=TRUE, warning=FALSE, message=FALSE}
links.all.seasons.1 <- episode.list %>%
tolower() %>%
str_replace_all(pattern = " ", replacement = "-") %>%
str_c("https://genius.com/Game-of-thrones", ., "annotated", sep = "-")
links.all.seasons.2 <- episode.list %>%
tolower() %>%
str_replace_all(pattern = " ", replacement = "-") %>%
str_c("https://genius.com/Game-of-thrones", ., "script","annotated", sep = "-")
links.all.seasons.df <- data_frame(link_1 = links.all.seasons.1,
link_2 = links.all.seasons.2)
```
## Check if links are valid
Not all episodes are transcribed in genius.com webpage!
Spot missing transcribed episodes. Careful, this process requieres internet connextion and might take a few minutes.
_To update list of transcripts, delete the output of this chunk of code and run it_
```{r eval=TRUE, warning=FALSE, message=FALSE}
if(!"valid_transcript_links_03.RDS" %in% list.files("interim_output")){
valid.links.1 <- links.all.seasons.df$link_1 %>%
map_lgl(url_success)
valid.links.2 <- vector(mode = "logical", length = length(valid.links.1))
valid.links.2[valid.links.1 == F] <- links.all.seasons.df$link_2[valid.links.1 == F] %>%
map_lgl(url_success)
links.all.seasons.df %<>%
bind_cols(valid_links_1 = valid.links.1,
valid_links_2 = valid.links.2)
valid.links <- if_else(links.all.seasons.df$valid_links_1 == T,
true = links.all.seasons.df$link_1,
false = if_else(links.all.seasons.df$valid_links_2 == T,
true = links.all.seasons.df$link_2,
false = "missing episode"))
valid.links %>%
write_rds(path = "interim_output/valid_transcript_links_03.RDS")
}else{
valid.links <- read_rds(path = "interim_output/valid_transcript_links_03.RDS")
}
```
**04-11-2017 There seem to be 22 episodes missing!**
**Important: 2 episodes (22 and 23) have transcripts without names!**
```{r eval=TRUE, warning=FALSE, message=FALSE}
(valid.links == "missing episode") %>% sum()
valid.links
```
## Scrap transcripts
```{r eval=TRUE, warning=FALSE, message=FALSE}
season.vector <- c(str_c("season_", rep(1:6, 10)), rep("season_7", 7)) %>% sort
episode.number.vector <- c(rep(1:10, 6), rep(1:7))
episode.list.df <- data_frame(season = season.vector,
episode_number = episode.number.vector,
episode_name = episode.list,
link = valid.links)
episode.list.df
rm(valid.links, season.vector, episode.number.vector, links.all.seasons.1, links.all.seasons.2,
links.all.seasons.df, episode.list)
```
## Convert trantscripts into tidy text
```{r eval=TRUE, warning=FALSE, message=FALSE}
if(!"all_transcripts_03.RDS" %in% list.files("interim_output/")){
all.transcripts <- episode.list.df$link[which(episode.list.df$link != "missing episode")] %>%
map(function(x){read_html(x) %>%
html_nodes(".lyrics p") %>%
html_text })
all.transcripts %>%
write_rds(path = "interim_output/all_transcripts_03.RDS")
}else{
all.transcripts <- read_rds("interim_output/all_transcripts_03.RDS")
}
```
```{r eval=TRUE, warning=FALSE, message=FALSE}
all.transcripts %<>%
map_chr(function(x){x %>% unlist %>% str_c(collapse = "_") })
episode.list.df$transcript <- NA
episode.list.df$transcript[episode.list.df$link != "missing episode"] <- all.transcripts
```
```{r eval=TRUE, warning=FALSE, message=FALSE}
rm(all.transcripts)
```
# Manually explore scrapped GoT transcripts
Transcripts can be manually explored here using the function writeLines. Careful with large outputs.
```{r eval=FALSE, warning=FALSE, message=FALSE}
episode.list.df$transcript[3] %>%
writeLines()
```
# Export objects
```{r eval=TRUE, warning=FALSE, message=FALSE}
episode.list.df %>%
write_rds("interim_output/episode_list_transcripts_03.RDS")
```