/
segment_elan_turns.R
148 lines (131 loc) · 6.48 KB
/
segment_elan_turns.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
# Load libraries
library(tidyverse)
library(slider)
library(glue)
# Set themes
theme_set(theme_classic(base_size=15))
options(ggplot2.discrete.colour = c("dodgerblue", "orange"))
# Make example data
df <- tibble(file=paste0("file_00", c(rep(1, 14), rep(2, 6))),
signer=paste0("S",c(rep(1, 6), 2, 1, 2, 2, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1)),
hand=c("rh", "rh", "rh", "lh", "rh", "rh", "rh", "rh", "rh", "lh", "lh", "rh", "rh", "rh", "rh", "lh", "rh", "rh", "rh", "lh"),
start=c(1000, 1200, 1400, 1600, 1650, 1850, 2000, 2500, 2550, 2600, 2650, 2800, 2900, 3400, 1000, 1100, 2000, 2050, 2500, 2550),
end=c(1150, 1350, 1550, 2550, 1800, 2000, 2100, 2650, 2600, 2650, 2700, 2900, 3100, 3500, 1200, 1900, 2100, 2150, 2600, 2800),
annotation=paste0("GLOSS-", LETTERS[1:20]))
t <- df %>%
mutate(ci = consecutive_id(signer), .by = file) %>%
mutate(rn = row_number(), .by = c(file, signer))
# Define function for strictly sequential turn segments (chronological by start time)
turn_seq <- function(data, file, start, end, participant, annotation, simplify = TRUE, long = FALSE) {
df <- data %>%
arrange({{ file }}, {{ start }}) %>%
mutate(turn = consecutive_id({{ participant }}), .by = {{ file }})
if (simplify) {
df <- df %>%
group_by({{ file }}, {{ participant}}, turn) %>%
mutate(annotations = paste0({{ annotation }}, collapse = " ")) %>%
mutate(start = min({{ start }}),
end = max({{ end }})) %>%
mutate(duration = end-start) %>%
slice(1) %>%
select(-{{ annotation }}) %>%
ungroup()
if (long) {
df <- df %>%
arrange({{ file }}, {{ participant }}, {{ start }}) %>%
mutate(turn = if_else(row_number()!=1 & start<=lag(end), lag(turn), turn), .by = c({{ file }}, {{ participant }})) %>%
group_by({{ file }}, {{ participant}}, turn) %>%
mutate(annotations = paste0(annotations, collapse = " ")) %>%
mutate(start = min({{ start }}),
end = max({{ end }})) %>%
slice(1) %>%
ungroup()
}
}
return(df)
}
# Define function for turn segments within intervals (chronological by start time per participant, but merge segments within interval)
turn_interval <- function(data, file, start, end, participant, annotation, interval = 300, simplify = TRUE) {
df <- data %>%
arrange({{ file }}, {{ participant }}, {{ start }}) %>%
mutate(longest_span = cummax({{ end }}), .by = c({{ file }}, {{ participant }})) %>%
mutate(gap = if_else(is.na(lag({{ end }})), TRUE, {{ start }}-lag(longest_span)>=interval), .by = c({{ file }}, {{ participant }})) %>%
mutate(turn = cumsum(gap), .by = c({{ file }}, {{ participant }}))
if (simplify) {
df <- df %>%
group_by({{ file }}, {{ participant }}, turn) %>%
mutate(annotations = paste0({{ annotation }}, collapse = " ")) %>%
mutate(start = min({{ start }}),
end = max({{ end }})) %>%
slice(1) %>%
select(-c({{ annotation }}, gap, longest_span)) %>%
ungroup()
}
return(df)
}
# Define function for turn segments by most signs per 3-signs moving time window
turn_quant <- function(data, file, start, end, participant, annotation, simplify = TRUE, long = FALSE) {
# Create windows of "whose turn" by max signing (number of signs)
df <- data %>%
arrange({{ file }}, {{ start }}) %>%
mutate(whose_turn = slider::slide_chr(signer, ~ names(which.max(table(.x))), .before = 1, .after = 1)) %>%
mutate(current_turn = if_else({{ participant }}==whose_turn, "same", "other")) %>%
mutate(turn = consecutive_id(whose_turn))
if (simplify) {
df <- df %>%
group_by({{ file }}, turn) %>%
mutate(annotations = paste0({{ annotation }}, collapse = " ")) %>%
mutate(start = min({{ start }}),
end = max({{ end }})) %>%
slice(1) %>%
select(-c({{ annotation }})) %>%
ungroup()
if (long) {
df <- df %>%
arrange({{ file }}, {{ participant }}, {{ start }}) %>%
mutate(turn = if_else(row_number()!=1 & start<=lag(end), lag(turn), turn), .by = c({{ file }}, {{ participant }})) %>%
group_by({{ file }}, {{ participant}}, turn) %>%
mutate(annotations = paste0(annotations, collapse = " ")) %>%
mutate(start = min({{ start }}),
end = max({{ end }})) %>%
slice(1) %>%
ungroup()
}
}
return(df)
}
# Plot sequential (gives overlapping turns within participants in this example)
ggplot() +
geom_segment(data=turn_seq(df, file = file, start = start, end = end, participant = signer, annotation = annotation, simplify = T, long = T),
aes(x=start, xend=end, y=glue::glue("{signer}_(seq)"), yend=glue::glue("{signer}_(seq)"), color=signer),
linewidth=5, show.legend = F, alpha=.5) +
geom_segment(data=df, aes(x=start, xend=end, y=glue::glue("{signer}_{hand}"), yend=glue::glue("{signer}_{hand}"), color=signer),
linewidth=5, show.legend = F, alpha=1) +
labs(y="", x="Time (ms)") +
facet_wrap(~file, ncol=1)
#ggsave(width = 7, height = 4, units = "in", dpi = 600)
# Plot with different intervals
# Set intervals in ms
#ms <- 100
#ms <- 200
ms <- 300
#ms <- 800
ggplot() +
geom_segment(data=turn_interval(df, file = file, start = start, end = end, participant = signer, annotation = annotation, interval = ms, simplify = T),
aes(x=start, xend=end, y=glue::glue("{signer}_(interval: {ms})"), yend=glue::glue("{signer}_(interval: {ms})"), color=signer),
linewidth=5, show.legend = F, alpha=.5) +
geom_segment(data=df, aes(x=start, xend=end, y=glue::glue("{signer}_{hand}"), yend=glue::glue("{signer}_{hand}"), color=signer),
linewidth=5, show.legend = F, alpha=1) +
labs(y="", x="Time (ms)") +
facet_wrap(~file, ncol=1)
#ggsave(width = 7, height = 4, units = "in", dpi = 600)
# Plot quantified
ggplot() +
geom_segment(data=turn_quant(df, file = file, start = start, end = end, participant = signer, annotation = annotation, simplify = T, long = T),
aes(x=start, xend=end, y=glue::glue("{signer}_(quant)"), yend=glue::glue("{signer}_(quant)"), color=signer),
linewidth=5, show.legend = F, alpha=.5) +
geom_segment(data=df, aes(x=start, xend=end, y=glue::glue("{signer}_{hand}"), yend=glue::glue("{signer}_{hand}"), color=signer),
linewidth=5, show.legend = F, alpha=1) +
labs(y="", x="Time (ms)") +
facet_wrap(~file, ncol=1)
#ggsave(width = 7, height = 4, units = "in", dpi = 600)