-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path[Code]
287 lines (236 loc) · 10.7 KB
/
[Code]
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
---
title: "Star Trek"
author: "NearAndDistant"
date: "17/08/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
#### Import
```{r}
library(tidyverse)
computer_raw <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-08-17/computer.csv')
```
*Person*
Command: Utterances that directly tell the computer what to do.
Question: Utterances that ask the computer for something.
Statement: Utterances tell the computer or ask it but meaning is inferred.
Password: Utterances that contain a password.
Wake Word: Key phrases used to activate the computer VUI.
Comment: Utterances that have no intended action for the computer
Conversation: Utterances that are more like human conversation, such as phatic expressions, formalities, and colloquial speech
*Domain*
InfoSeek: Querying any information from the computer, somewhat similar to a Google search, but also status updates, etc.
Locate: Querying the physical location of a person, usually on board the ship
Entertainment: Queries related to music, holodeck, other pastimes.
Music: Queries related to music
Holodeck: Queries related to operating the holodeck.
IoT: Queries that activates or uses another piece of hardware, like turning on lights or heating by voice in a smart house.
Replicator: Queries relating to using the replicator
Turbolift: Queries relating to using the turbolift
Analysis: Queries that ask the computer to perform an
Communication: Queries that interact with digital mmunications
Help: Queries that seek help in using the system
Emergency: Queries around emergencies like setting the auto-destruct
*Computer*
Clarification: Utterances asking for more information.
Response: Utterances that respond to a person's query or action
System Alert: Utterances that either respond to a person requested action (often a warning) or are prompted by the system, rather than a person
Information: Utterances that inform users of what is happening without being prompted to do so (which would be a response)
Countdown: Utterances that are part of a countdown.
In Progress: Utterances that indicates that a process is ongoing or updates on that process
Conversation: Utterances that are more like human conversation, such as phatic expressions, formalities, and colloquial speech
#### Clean & Wrangle
```{r}
computer <-
computer_raw %>%
rename(character_type = char_type ,
character = char ,
stage_direction = direction ,
interaction_type = type ,
com_type = pri_type ,
com_domain = domain ,
com_sub_domain = sub_domain,
no_response_comp = nv_resp ,
error_response = error) %>%
mutate(character = if_else(str_detect(character , "Computer"), "Computer", character),
character = if_else(str_detect(character , "Com Panel"), "Computer", character),
character = if_else(str_detect(character , "Picard"), "Picard", character),
character = str_remove(character , " \\(O.S.\\)"),
character = str_remove(character , " \\(V.O.\\)"),
character = str_remove(character , " \\(Cont'D\\)"),
character = str_remove(character , " \\(O.C.\\)"),
character = str_remove(character , "Young ")) %>%
mutate(interaction_type = str_to_title(interaction_type),
com_domain = if_else(str_detect(com_domain , "Iot"), "IoT", com_domain),
com_sub_domain = str_remove(com_sub_domain , "\\?")) %>%
mutate(species = case_when(character == "Worf" ~ "Humanoid",
character == "Troi" ~ "Humanoid",
character == "Beverly" ~ "Humanoid",
character == "Riker" ~ "Humanoid",
character == "Data" ~ "Android",
character == "Picard" ~ "Humanoid",
character == "Geordi" ~ "Humanoid",
character == "Computer" ~ "Computer")) %>%
mutate(species = if_else(is.na(species), "Humanoid" , species)) %>%
select(character_type , character, species , interaction_type , com_type , com_domain , com_sub_domain , line , no_response_comp , error_response)
```
#### EDA
```{r}
theme_set(theme_minimal())
computer %>%
filter(!is.na(species)) %>%
ggplot(aes(interaction_type , fill = interaction_type)) +
geom_histogram(stat = "count")+
coord_flip() +
facet_wrap(~species , nrow = 1) +
theme(legend.position = "none")
```
#### Text Analysis
```{r}
library(tidytext)
library(tidyr)
computer_unnested <-
computer %>%
unnest_tokens("word" , line) %>%
anti_join(stop_words , by = "word") %>%
add_count(word , name = "word_total" , sort = TRUE)
```
#### EDA on Tidy Text
```{r}
computer_unnested %>%
filter(!is.na(species),
word_total >= 35,
word != "computer") %>%
ggplot(aes(word , fill = word)) +
geom_histogram(stat = "count")+
coord_flip() +
facet_wrap(~species) +
theme(legend.position = "none")
```
```{r}
computer_unnested %>%
filter(!is.na(species),
word_total >= 50,
word != "computer") %>%
ggplot(aes(reorder(word , word_total) , fill = species)) +
geom_histogram(stat = "count") +
coord_flip() +
facet_wrap(~interaction_type , scale = "free_y")
```
#### Palette
```{r}
background <- "#C3CBD4"
```
#### Frequency Analysis
```{r}
st_frequencies <-
computer_unnested %>%
filter(word != "computer", word != "program") %>%
mutate(humanoid = if_else(species == "Humanoid" , "Humanoid" , "Non-Humanoid")) %>%
select(humanoid , species , word) %>%
mutate(word_n = n()) %>% # total words = 11,185 (minus "computer")
add_count(word , name = "word_total" , sort = TRUE) %>% # frequency of words across dataset
group_by(word) %>%
mutate(word_pc = word_total / word_n) %>% # test: distinct(word , word_pc) & sum == 1
ungroup() %>%
group_by(species) %>%
mutate(species_n = n()) %>% # test: distinct(species_n) %>% summarise(species_tot = sum(species_n))
add_count(species, word , name = "species_word_total" , sort = TRUE) %>% # frequency of words across species
mutate(species_pc = species_word_total / species_n) %>%
ungroup() %>%
distinct(species , word , .keep_all = TRUE)
```
#### Frequency Graphic
```{r}
library(scales)
library(ggrepel)
showtext::showtext_auto()
sysfonts::font_add_google("Orbitron" , "orbi")
sysfonts::font_add_google("Rationale" , "rationale")
set.seed(111)
plot_freq <-
st_frequencies %>%
ggplot(aes(x = species_pc, y = word_pc)) + # color = abs(word_pc - species_pc
#geom_jitter(size = 0.5, width = 0.75, height = 0.5, alpha = 0.10) +
geom_text_repel(aes(label = word) , color = "grey20" , segment.color = NA , size = 3 , family = "rationale", max.overlaps = 8) +
geom_abline(color = "#2070b6" , alpha = 0.5) +
scale_x_log10(labels = percent_format()) +
scale_y_log10(labels = percent_format()) +
scale_color_viridis_c(option = "cividis" , begin = 0 , end = 0.75) +
facet_wrap(~species , ncol = 3) +
labs(x = "Species Specific Word Weighting", y = "Overall Word Weighting") +
theme(legend.position ="none",
panel.grid = element_blank(),
panel.background = element_rect(fill = background , color = background),
plot.background = element_rect(fill = background, color = background),
text = element_text(family = "rationale"),
axis.title.x = element_text(size = 14, vjust = -3),
axis.title.y = element_text(size = 12),
strip.background = element_blank(), # fill = background , color = background
strip.text = element_text(family = "orbi" , face = "bold" , size = 18),
plot.margin = margin(1.5,1.2,0.5,0.1, unit = "cm")) # 1.5,1.5,0.5,1, # 1.5,1.5,0.5,1,
```
#### Sentiment Analysis
```{r}
st_sentiment_analysis <-
computer_unnested %>%
inner_join(sentiments) %>%
count(species , interaction_type, sentiment , sort = TRUE) %>%
pivot_wider(names_from = sentiment, values_from = n) %>%
mutate(sentiment = positive - negative)
```
#### Sentiment Graphic
```{r}
plot_sentiment <-
st_sentiment_analysis %>%
ggplot(aes(interaction_type, sentiment, fill = species)) +
geom_col(show.legend = FALSE) +
geom_text(aes(label = interaction_type), family = "rationale" , size = 5, color = "grey30") +
scale_y_continuous(breaks = seq(-40, 40, by = 10), limits = c(-40, 40)) +
scale_fill_viridis_d(option = "cividis" , alpha = 0.35, begin = 0.75 , end = 0) +
coord_flip() +
facet_wrap(~species, ncol = 3) +
labs(x = "Interaction Type",
y = "Sentiment") +
theme(panel.background = element_rect(fill = background , color = background),
plot.background = element_rect(fill = background , color = background),
panel.grid = element_blank(),
text = element_text(family = "rationale"),
axis.title.x = element_text(size = 14 , vjust = -3),
axis.ticks = element_blank(),
axis.title.y = element_text(size = 12 , vjust = 7),
axis.text.y = element_blank(),
strip.text = element_blank(),
plot.margin = margin(1,1.5,1,0.8, unit = "cm"))
```
#### Panel
```{r}
library(gridExtra)
plot_arranged <-
grid.arrange(plot_freq , plot_sentiment,
ncol = 1 , heights = c(1 , 0.50))
```
#### Cowplot
```{r}
library(cowplot)
ggdraw(plot_arranged) +
draw_image("https://brandslogos.com/wp-content/uploads/images/large/star-trek-logo.png",
height = 0.5 , width = 0.5,
x = 0.26 , y = 0.24) +
draw_image("https://i.pinimg.com/originals/d6/7d/5c/d67d5c43befed8feae8f60857c51b4a6.png",
height = 0.25 , width = 0.25,
x = -0.065 , y = 0.79) +
draw_text("Using frequency analysis on Star Trek\ninteractions we can analyse the different\nwords Humanoids, Androids and Computers\nuse in Star Trek",
x = 0.80 , y = 0.60 , size = 9 , family = "rationale" , hjust = 0 , color = "#2070b6") +
draw_text("Using sentiment analysis\nwe can see the android Data\nuses far less emotive words\nacross all types of interactions\nwith Computer than Humanoids\nor even Computer does",
x = 0.05 , y = 0.2 , size = 9 , family = "rationale" , hjust = 0 , color = "#2070b6") +
draw_text("Using both Computer plots we can\n see that Computer uses a lot of negative\nsentiment and time words begging the\nquestion if Computer is a glorified clock",
x = 0.52 , y = 0.24 , size = 9 , family = "rationale" , hjust = 0 , color = "#2070b6")
```
#### Saving
```{r}
# save plot
ggsave(here::here("Do Androids Dream of Star Trek?.png"), dpi = 360, height = 10, width = 16)
```