-
Notifications
You must be signed in to change notification settings - Fork 0
/
utterance_collapse.R
151 lines (89 loc) · 5.03 KB
/
utterance_collapse.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
utterance_collapse <-
function(my_df, utterance_vector_processed, utterance_vector_unprocessed, overlap_threshold) {
my_df_new_proc = list()
my_df_new_unproc = list()
i = 1
k = 1
while(i <= nrow(my_df)){
success = FALSE
if (length(utterance_vector_unprocessed[[i]])>0){
utterance_proc = utterance_vector_processed[[i]]
utterance_unproc = utterance_vector_unprocessed[[i]]
role = as.character(my_df[i,"role"])
my_start = my_df[i,"start"]
my_end = my_df[i,"end"]
if ((i+1)<=nrow(my_df)){
for (j in (i+1):nrow(my_df)){
#if (as.character(my_df[j,"role"]) == role) {
if (length(utterance_vector_unprocessed[[j]])>0){
utterance_candidate_proc = utterance_vector_processed[[j]]
utterance_candidate_unproc = utterance_vector_unprocessed[[j]]
# # we compute overlap in terms of processed text (without stopwords and stemmed)
# overlap = intersect(wordStem(utterance_proc),wordStem(utterance_candidate_proc))
# attempt: overlap in terms of last four and first four unprocessed words
if (success==TRUE){
utterance_proc = unlist(strsplit(utterance_proc,split=" "))
utterance_unproc = unlist(strsplit(utterance_unproc,split=" "))
}
if (length(utterance_unproc)>=4){
text_before = utterance_unproc[(length(utterance_unproc)-3):length(utterance_unproc)]
} else {
text_before = utterance_unproc
}
if (length(utterance_candidate_unproc)>=4){
text_after = utterance_candidate_unproc[1:4]
} else {
text_after = utterance_candidate_unproc
}
overlap = intersect(text_before, text_after)
#if (length(overlap)/max(length(utterance_candidate_proc), length(utterance_proc)) >= overlap_threshold){
if (length(overlap)/min(4, length(utterance_unproc),length(utterance_candidate_unproc))>=overlap_threshold){
# if success
if (length(utterance_proc)==0){
utterance_proc = " "
}
if (length(utterance_candidate_proc)==0){
utterance_candidate_proc = " "
}
utterance_proc = str_trim(string_join(paste(utterance_proc,collapse=" "), paste(utterance_candidate_proc,collapse=" "))$output)
utterance_unproc = string_join(paste(utterance_unproc, collapse=" "), paste(utterance_candidate_unproc, collapse=" "))$output
i = i + 1
my_end = my_df[j,"end"]
success = TRUE
} else if ((as.character(my_df[j,"role"]) == role)&(length(utterance_proc)>0)&(length(utterance_candidate_proc)>0)) {
# if same speaker, and last word == first word (excluding stopwords), success
if (utterance_proc[length(utterance_proc)] == utterance_candidate_proc[1]){
utterance_proc = str_trim(string_join(paste(utterance_proc,collapse=" "), paste(utterance_candidate_proc,collapse=" "))$output)
utterance_unproc = string_join(paste(utterance_unproc, collapse=" "), paste(utterance_candidate_unproc, collapse=" "))$output
i = i + 1
my_end = my_df[j,"end"]
success = TRUE
} else {
# as soon as we don't get a success we exit the j loop
break
}
} else {
# as soon as we don't get a success we exit the j loop
break
}
} else {
break
}
# } else {
# break
# }
}
}
my_df_new_proc[[k]] = c(my_start, my_end, role, paste(utterance_proc,collapse=" "))
my_df_new_unproc[[k]] = c(my_start, my_end, role, paste(utterance_unproc,collapse=" "))
i = i + 1
k = k+1
} else {
i = i + 1
}
}
# convert lists into data frames
reduced_my_df_proc = data.frame(matrix(unlist(my_df_new_proc), nrow=length(my_df_new_proc), byrow=T))
reduced_my_df_unproc = data.frame(matrix(unlist(my_df_new_unproc), nrow=length(my_df_new_unproc), byrow=T))
output = list(reduced_my_df_proc = reduced_my_df_proc, reduced_my_df_unproc = reduced_my_df_unproc)
}