-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_cleaning.R
183 lines (168 loc) · 4.77 KB
/
data_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
# ---- DATA CLEANING ----
library(tidyverse)
library(quanteda)
library(quanteda.textstats)
transcripts <- read_rds("data/peaky_blinders_transcripts.rds")
peakyCorpus <- corpus(transcripts$Transcripts)
# Tokenise the transcripts
transcripts_tkns <- transcripts$Transcripts |>
tokens(
remove_numbers = T,
remove_punct = T,
remove_symbols = T
) |>
tokens_remove(stopwords("en")) |>
tokens_tolower()
# List most common character name phrases
collocations_names <- c(
"mr winston", "laura mckee", "haydn stagg",
"mrs connors", "luca changretta", "grace shelby",
"mr levitt", "oswald mosley", "billy kimber",
"winston churchill", "polly gray", "aunt polly",
"michael gray", "mr solomons", "mr campbell",
"mr gold", "jessie eden", "jimmy mccavern",
"uncle jack", "freddie thorne", "mr thomas",
"jack nelson", "johnny dogs", "aberama gold",
"mr churchill", "arthur shelby", "mr nelson",
"thomas shelby", "tommy shelby", "mr shelby",
"mrs changretta", "billy grade", "evadne barwell",
"elizabeth gray", "greta jurossi", "mrs thorne",
"mr stagg"
)
# Create the collocations object
collocations <- transcripts_tkns |>
textstat_collocations(min_count = 2) |>
filter(collocation %in% collocations_names)
# Add underscore between name phrases
# e.g "mr shelby" > "mr_shelby"
transcripts_tkns2 <- transcripts_tkns |>
tokens_compound(pattern = phrase(collocations$collocation))
# Now substitute different character names & nicknames into one
# Names & nicknames designating the same character
original_names <- c(
"thomas", "tommy", "tom", "mr_thomas","thomas_shelby", "tommy_shelby", "mr_shelby",
"ada", "mrs_thorne",
"campbell", "mr_campbell",
"polly","polly_gray", "aunt_polly", "elizabeth_gray",
"arthur", "arthur_shelby",
"grace", "grace_shelby",
"freddie", "freddie_thorne",
"churchill", "winston_churchill", "mr_winston", "mr_churchill",
"billy", "billy_kimber",
"alfie", "mr_solomons",
"michael", "michael_gray",
"ruben", "oliver",
"leon", "romanov",
"jessie","jessie_eden",
"luca", "luca_changretta",
"aberama","mr_gold", "aberama_gold",
"ben", "younger",
"oswald", "mosley", "oswald_mosley",
"jimmy", "jimmy_mccavern", "mccavern",
"jack","uncle_jack", "mr_nelson", "nelson", "jack_nelson",
"laura", "laura_mckee",
"hydyn_stagg", "mr_stagg")
new_names <- c(
rep("thomas", 7),
rep("ada", 2),
rep("campbell", 2),
rep("polly", 4),
rep("arthur", 2),
rep("grace", 2),
rep("freddie", 2),
rep("churchill", 4),
rep("billy_kimber", 2),
rep("alfie", 2),
rep("michael", 2),
rep("ruben", 2),
rep("romanov", 2),
rep("jessie_eden", 2),
rep("luca_changretta", 2),
rep("aberama", 3),
rep("younger", 2),
rep("oswald_mosley", 3),
rep("jimmy_mccavern", 3),
rep("jack_nelson", 5),
rep("laura_mckee", 2),
rep("hayden_stagg", 2)
)
transcripts_tkns_final <- transcripts_tkns2 |>
tokens_replace(original_names, new_names)
# Based on this new text, build a co-occurrence matrix
cooccurrence_matrix <- fcm(transcripts_tkns_final,
context = "window",
count = "frequency",
window = 25,
tri = FALSE
)
# Peaky Blinders character names and nicknames (including phrases)
characters <- c(
"thomas", "tommy", "tom", "mr_thomas","thomas_shelby", "tommy_shelby", "mr_shelby",
"campbell", "mr_campbell",
"moss",
"polly","polly_gray", "aunt_polly",
"arthur", "arthur_shelby",
"grace", "grace_shelby",
"freddie", "freddie_thorne",
"ada", "mrs_thorne",
"john",
"johnny_dogs",
"charlie",
"jeremiah",
"isiah",
"danny",
"churchill", "winston_churchill", "mr_winston", "mr_churchill",
"billy", "billy_kimber",
"darby",
"sabini",
"alfie", "mr_solomons",
"may",
"michael", "michael_gray",
"lizzie",
"esme",
"tatiana",
"izabella",
"ruben", "oliver",
"leon", "romanov",
"linda",
"jessie","jessie_eden",
"luca", "luca_changretta",
"angel_changretta",
"curly",
"bonnie",
"aberama","mr_gold", "aberama_gold",
"gina",
"ben", "younger",
"oswald", "mosley", "oswald_mosley",
"jimmy", "jimmy_mccavern", "mccavern",
"chang",
"barney",
"finn",
"frances",
"mitford",
"jack","uncle_jack", "mr_nelson", "nelson", "jack_nelson",
"laura", "laura_mckee",
"erasmus",
"hayden_stagg",
"mrs_connors",
"mr_levitt",
"mrs_changretta",
"anna",
"barney",
"billy_grade",
"cyril",
"evadne_barwell",
"hughes",
"goliath",
"karl",
"gretta_jurossi",
"vicente"
)
# Filter co-occurrence matrix to only keep character interactions
cooccurrence_df <- convert(cooccurrence_matrix, to = "data.frame") |>
filter(doc_id %in% characters)
new_order <- cooccurrence_df$doc_id
cooccurrence_df <- cooccurrence_df |>
select(doc_id, any_of(new_order)) |>
column_to_rownames(var = "doc_id")
saveRDS(cooccurrence_df, "data/cooccurrence_df.rds")