-
Notifications
You must be signed in to change notification settings - Fork 10
/
example_raw_text_cleaning.R
293 lines (220 loc) · 11.5 KB
/
example_raw_text_cleaning.R
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# Setup -------------------------------------------------------------------
library(tidyverse); library(gutenbergr); library(tidytext); library(quanteda); library(stringr)
# the gutenberg organization is lazy (much like their website), and the gutenbergr metadata doesn't even have (approx) year. Practically begs a hack-only approach.
gutenberg_metadata
# thanks for the hack! Conveniently also, the names of the Henry works are
# completely inconsistent, e.g. 3 Henry IV of Henry IV, Part 1, King Henry IV,
# the First Part, King Henry IV, Second Part; Gutenberg also has evidently not
# updated that Two Noble Kinsmen is accepted as Shakespeare (and Fletcher);
# after further processing Hamlet (just Hamlet) and Othello (just Othello) had
# words like neuer for never; so I'm selecting the more usable version
gw0 = gutenberg_works(
author=='Shakespeare, William',
!str_detect(title, "Works|Shakes|Theatre|Folio|Quarto|;|^The Tragedy"),
!str_detect(title, "^King Henry VI|^Henry IV|^The History|^History|^The Life"),
languages='en') %>%
bind_rows(gutenberg_works(gutenberg_id==1542)) %>% # add two noble kinsmen
bind_rows(gutenberg_works(gutenberg_id==1528)) %>% # add back 'the history of troilus...
bind_rows(gutenberg_works(gutenberg_id==1041)) %>% # add back Sonnets
filter(!title %in% c('Hamlet', 'Othello')) # remove these versions
gw0 %>% filter(grepl(x=title, 'Henry'))
gw0 %>% filter(grepl(x=title, 'Troilus'))
gw = gw0 %>%
select(gutenberg_id, title) %>%
mutate(class = 'Comedy',
class = if_else(grepl(title, pattern='Adonis|Lucrece|Complaint|Turtle|Pilgrim|Sonnet'), 'Poem', class),
class = if_else(grepl(title, pattern='Henry|Richard|John'), 'History', class),
class = if_else(grepl(title, pattern='Troilus|Coriolanus|Titus|Romeo|Timon|Julius|Macbeth|Hamlet|Othello|Antony|Cymbeline|Lear'), 'Tragedy', class),
problem = if_else(grepl(title, pattern='Measure|Merchant|^All|Troilus|Timon|Passion'), 'Problem', 'Not'))
gw
#○ Checks ------------------------------------------------------------------
# table(gw$class)
# table(gw$title, gw$class)
# gw$title[gw$problem=='Problem']
# gw %>% filter(class=='Comedy') %>% data.frame() %>% arrange(title)
# gw %>% filter(class=='Tragedy') %>% data.frame() %>% arrange(title)
# Download Works ----------------------------------------------------------
works = lapply(gw$gutenberg_id, gutenberg_download)
names(works) = gw$title
save(works, file='data/texts_raw/shakes/gutenberg_shakespeare.RData')
# works2 = gutenberg_download(100, meta_fields=c('title')) # no titles!
# works2
# Clean up ----------------------------------------------------------------
load('data/texts_raw/shakes/gutenberg_shakespeare.RData')
#○ Trim initial title preface etc. -----------------------------------------
detect_first_act <- function(text_tbl) {
act_lines = grep(text_tbl$text, pattern='^Act|^ACT|^by')
if(length(act_lines) > 0) {
idx = 1:act_lines[1]
text_tbl = text_tbl %>% slice(-idx)
}
text_tbl
}
works_trim = lapply(works, detect_first_act)
# compare
works$`Romeo and Juliet`
works_trim$`Romeo and Juliet`
works$`Shakespeare's Sonnets`
works_trim$`Shakespeare's Sonnets`
#○ Get rid of scene and act lines ----------------------
works_trim = lapply(works_trim, filter,
!text == str_to_upper(text),
!str_detect(text, pattern='^Scene|^Act |^SCENE|^ACT |^\\[|^Enter'))
works_trim
#○ encoding ----------------------------------------------------------------
works_trim = lapply(works_trim, function(x) mutate(x, text= str_conv(text, encoding='UTF8')))
#○ Get rid of metainfo -----------------------------------------------------
# somehow Timon still has metadata since apparently there are no Acts or Scenes in gutenberg's version
meta_idx = sapply(works_trim, function(x) grepl(x[1,'text'], pattern='^Executive'))
timon = works_trim$`Timon of Athens`
timon_begin = 'The Life of Timon of Athens'
works_trim$`Timon of Athens` = slice(timon,
-(1:grep(timon$text, pattern=timon_begin)))
# Create data frame -------------------------------------------------------
works_trim_df = bind_rows(works_trim, .id='work')
# Get and remove characters -----------------------------------------------
library(rvest)
shakes_char_url = 'https://www.opensourceshakespeare.org/views/plays/characters/chardisplay.php'
page0 = read_html(shakes_char_url)
tabs = page0 %>% html_table()
shakes_char = tabs[[2]][-(1:2), c(1,3,5)] # remove header and phantom columns
colnames(shakes_char) = c('Nspeeches', 'Character', 'Play')
shakes_char = shakes_char %>%
distinct(Character,.keep_all=T)
# get dukes, earls, etc.
chars = sapply(shakes_char$Character, function(x) str_split(x, pattern=' '))
chars = sapply(chars, function(x) ifelse(length(x)>1, x[length(x)], NA))
chars = chars[!is.na(chars)]
chars = c(str_replace_all(chars, pattern='\\(|\\)', '')) # otherwise regex will freak
chars = c(chars, 'fford', 'ronicus', 'ssid', 'Palamon', 'Tarquin', 'Baptista',
'Iohn') # some names got screwed up
works_trim_df = works_trim_df %>%
mutate(text = str_replace_all(text,
pattern=paste0(shakes_char$Character, collapse='|'),
replacement=''),
text = str_replace_all(text,
pattern=paste0(str_sub(shakes_char$Character, 1, 3), collapse='|'),
replacement=''),
text = str_replace_all(text,
pattern=paste0(str_sub(shakes_char$Character, 1, 4), collapse='|'),
replacement=''))
works_trim_df = works_trim_df %>%
mutate(text = str_replace_all(text,
pattern=paste0(chars, collapse='|'),
replacement=''),
text = str_replace_all(text,
pattern=paste0(str_sub(chars, 1, 3), collapse='|'),
replacement=''),
text = str_replace_all(text,
pattern=paste0(str_sub(chars, 1, 4), collapse='|'),
replacement=''))
# Get words ---------------------------------------------------------------
# good christ the clusterfuck. Names are sometimes abbreviated, punctuation is erratic at best, etc.
# works_sentences = lapply(works_trim, unnest_tokens, sentence, text, token='sentences')
# works_sentences
works_words = works_trim_df %>%
select(-gutenberg_id) %>%
group_by(work) %>%
unnest_tokens(word, text, to_lower=F) %>%
ungroup
any(works_words$word == 'Romeo') # check previous name removal
any(works_words$word == 'Hamlet')
any(works_words$word == 'Ham')
# Remove stopwords --------------------------------------------------------
# me from python cltk module; em from http://earlymodernconversions.com/wp-content/uploads/2013/12/stopwords.txt
me_stops0 = read_lines('data/middle_english_stop_words')
sort(me_stops0)
me_stops = data_frame(word=str_conv(me_stops0, 'UTF8'),
lexicon = 'cltk')
em_stops0 = read_lines('data/early_modern_english_stop_words.txt')
sort(em_stops0)
em_stops = data_frame(word=str_conv(em_stops0, 'UTF8'),
lexicon = 'emc')
# misc that came up, esp. butchered names from tidytext
other_stops = data_frame(word= c('ile', 'ite', 'nch', 'nce', 'wne', 'lord',
'haue', 'neuer', 'heere', 'nes', 'dinall',
'rence', "i'th", 'ard', 'euer', 'goe', 'mio',
'eance', 'les', 'med', 'gar', 'erset', 'rece',
'frey', 'ntio', 'pyramus', 'ert', 'lingbrooke',
'signior', 'llow', 'dut', 'ducats', 'uolio',
'dost', 'unt', "ha's", 'inia', 'cus', 'hmond',
'tress', 'sull', 'till', 'ries', 'phin', 'vse',
'ena', 'bel', 'ntague', 'scicin', 'pri', 'ghter',
'res', "o'th", 'tector', 'vous', 'oyces', 'yea',
"a'th", 'fidius', 'istian', 'mas', 'ster',
'tista', 'dsor', 'eks', 'nley', 'ards', 'tista',
'ens', 'empts', 'empt', 'emp', 'padua', 'alas',
'anor', 'lish', 'sby', 'morrow', 'cian', "is't",
'shalt', 'rousillon', 'rge', 'refore', 'fulvia',
'inst', 'don', 'ere', 'nckle', 'jan', 'scena',
'alarum', 'alan', 'ces', 'ioles', 'cesario'))
# Fixes based on counts
e_words = c('againe', 'selfe', 'himselfe', 'herselfe', 'heare', 'soule',
'highnesse', 'latine', 'beare', 'lesse', 'liues', 'finde', 'eate',
'dye', 'looke', 'faire', 'speake', 'thinke', 'feare', 'deepe', 'meane',
'poore', 'owne', 'foole', 'downe', 'royall', 'flye', 'empresse')
uv_words = c('loue', 'giue', 'leaue', 'heauen', 'liue', 'euery', 'knaue', 'euen')
# My impression is that if you do a lot of this at once, dplyr will have
# problems, and some things won't be done
works_words = works_words %>%
mutate(word = if_else(word %in% e_words,
str_sub(word, end=nchar(word)-1),
word),
word = if_else(word %in% uv_words,
str_replace(word, 'u', 'v'),
word),
word = if_else(str_detect(word, "'s$|'d$|farre|houres|sonne|soules"),
str_sub(word, end=nchar(word)-2),
word)) %>%
mutate(word = if_else(word=='teares', 'tears', word),
word = if_else(word=='honour', 'honor', word),
word = if_else(word=='maiestie', 'majesty', word),
word = if_else(word=='soueraigne', 'sovereign', word),
word = if_else(word=='souldier', 'soldier', word),
word = if_else(word=='newes', 'news', word),
word = if_else(word=='iustice', 'justice', word),
word = if_else(word=='yeeld', 'yield', word),
word = if_else(word=='emptie', 'empty', word),
word = if_else(word=='empale', 'impale', word),
word = if_else(word=='herine', 'heroine', word),
word = if_else(word=='employd', 'employed', word),
word = if_else(word=='yong', 'young', word),
word = if_else(str_detect(word, "emperour|emperour's|emperours|emperors"), 'emperor', word),
word = if_else(str_detect(word, "emperiall|emperialls"), 'imperial', word),
word = if_else(str_detect(word, "empyrie|emperie"), 'empire', word)
)
works_words = works_words %>%
mutate(word = str_to_lower(str_trim(word))) %>%
anti_join(me_stops) %>%
anti_join(em_stops) %>%
anti_join(stop_words) %>%
anti_join(other_stops) %>%
arrange(work, word)
works_words
any(works_words$word == 'tis') # check
# any(works_words$word == 'thou')
# any(works_words$word == 'ye')
# find issues
works_trim$Coriolanus %>% filter(str_detect(text, 'ioles'))
# Word count tests --------------------------------------------------------
# Here we'll find leftover stuff
term_counts = works_words %>%
group_by(work, word) %>%
count
term_counts %>%
arrange(desc(n)) %>%
filter(str_count(word)>2) %>%
data.frame()
term_counts %>%
arrange(desc(n)) %>%
filter(str_count(word)>2) %>%
data.frame() %>%
filter(n<21)
# it looked like any two letter word was just an abbreviated stopword
test = term_counts %>%
filter(str_count(word)>2)
test %>%
arrange(desc(n)) %>%
data.frame()
# shakes_dtm = %>%
cast_dfm(document=work, term=word, value=n)