-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathpreprocess.py
More file actions
317 lines (261 loc) · 11.4 KB
/
Copy pathpreprocess.py
File metadata and controls
317 lines (261 loc) · 11.4 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import spacy
import argparse
from tqdm import tqdm
import pandas as pd
import pickle
import json
from functools import partial
from collections import Counter
from utils.tools import counter_to_distribution
spacy_en = spacy.load("en")
def create_examples_EC(split):
"""
split: train, val or test
return: a list of examples, each example is a list of utterances and an emotion label for the last utterance
"""
with open("./data/EC/{0}.txt".format(split), "r") as f:
f.readline()
conversations = f.readlines()
print("{0} split has {1} conversations".format(split, len(conversations)))
print("max_conv_length: ", 3)
examples = []
dummy_emotion = conversations[0].strip().split("\t")[-1]
for conv in conversations:
utterances_emotion = [e.strip() for e in conv.split("\t")][1:]
ex = []
for idx, utter in enumerate(utterances_emotion[:-1]):
if idx%2 == 0:
speaker = "Speaker A"
else:
speaker = "Speaker B"
if idx <= 1:
ex.append((utter, speaker, dummy_emotion, 0))
elif idx == 2:
ex.append((utter, speaker, utterances_emotion[-1], 1))
examples.append(ex)
return examples
def create_examples_DD(split):
"""
split: train, val or test
return: a list of examples, each example is a list of utterances and an emotion label for the last utterance
"""
with open("./data/DD/dialogues_{0}.txt".format(split), "r") as f:
conversations = f.readlines()
with open("./data/DD/dialogues_emotion_{0}.txt".format(split), "r") as f:
emotions = f.readlines()
print("{0} split has {1} conversations".format(split, len(conversations)))
examples = []
max_conv_length = max([len(conv.split("__eou__")[:-1]) for conv in conversations])
print("max_conv_length: ", max_conv_length)
dummy_utterance = "this is a dummy sentence"
dummy_emotion = emotions[0].strip().split(" ")[0]
dummy_speaker = "Speaker A"
for conv, emo in zip(conversations, emotions):
utterances = [utter.strip() for utter in conv.split("__eou__")][:-1]
# add speaker info
speakers = []
for idx, utter in enumerate(utterances):
if idx%2 == 0:
speaker = "Speaker A"
else:
speaker = "Speaker B"
speakers.append(speaker)
# add emotion
utter_emotions = [emotion.strip() for emotion in emo.split(" ")[:-1]]
assert len(utterances) == len(speakers) == len(utter_emotions)
# pad dummy utterances
conv_length = len(utterances)
masks = conv_length*[1] + (max_conv_length-conv_length)*[0]
utterances += (max_conv_length-conv_length)*[dummy_utterance]
utter_emotions += (max_conv_length-conv_length)*[dummy_emotion]
speakers += (max_conv_length-conv_length)*[dummy_speaker]
# create examples
examples.append(list(zip(utterances, speakers, utter_emotions, masks)))
return examples
def create_examples_MELD(split):
"""
split: train, val or test
return: a list of examples, each example is a list of utterances and an emotion label for the last utterance
"""
data = pd.read_csv("./data/MELD/{0}.csv".format(split))
print("{0} split has {1} conversations".format(split, data["Dialogue_ID"].unique().shape[0]))
examples = []
conv_lengths = []
for idx, conv in data.groupby("Dialogue_ID"):
conv_lengths.append(len(conv))
max_conv_length = max(conv_lengths)
print("max_conv_length: ", max_conv_length)
dummy_utterance = "this is a dummy sentence"
dummy_emotion = data["Emotion"][0]
dummy_speaker = data["Speaker"][0]
for idx, conv in data.groupby("Dialogue_ID"):
utterances = conv["Utterance"].tolist()
speakers = conv["Speaker"].tolist()
emotions = conv["Emotion"].tolist()
assert len(utterances) == len(speakers) == len(emotions)
# pad dummy utterances
conv_length = len(utterances)
masks = conv_length*[1] + (max_conv_length-conv_length)*[0]
utterances += (max_conv_length-conv_length)*[dummy_utterance]
emotions += (max_conv_length-conv_length)*[dummy_emotion]
speakers += (max_conv_length-conv_length)*[dummy_speaker]
# associate utterances with speakers
utterances = list(zip(utterances, speakers, emotions, masks))
examples.append(utterances)
return examples
def create_examples_EmoryNLP(split):
"""
split: train, val or test
return: a list of examples, each example is a list of utterances and an emotion label for the last utterance
"""
data = pd.read_csv("./data/EmoryNLP/{0}.csv".format(split))
print("{0} split has {1} conversations".format(split, data[["Season", "Episode", "Scene_ID"]].drop_duplicates().shape[0]))
examples = []
conv_lengths = []
for idx, conv in data.groupby(["Season", "Episode", "Scene_ID"]):
conv_lengths.append(len(conv))
max_conv_length = max(conv_lengths)
print("max_conv_length: ", max_conv_length)
dummy_utterance = "this is a dummy sentence"
dummy_emotion = data["Emotion"][0]
dummy_speaker = data["Speaker"][0][2:-2]
for idx, conv in data.groupby(["Season", "Episode", "Scene_ID"]):
utterances = conv["Utterance"].tolist()
speakers = [speaker[2:-2] for speaker in conv["Speaker"].tolist()]
emotions = conv["Emotion"].tolist()
assert len(utterances) == len(speakers) == len(emotions)
# pad dummy utterances
conv_length = len(utterances)
masks = conv_length*[1] + (max_conv_length-conv_length)*[0]
utterances += (max_conv_length-conv_length)*[dummy_utterance]
emotions += (max_conv_length-conv_length)*[dummy_emotion]
speakers += (max_conv_length-conv_length)*[dummy_speaker]
# associate utterances with speakers
utterances = list(zip(utterances, speakers, emotions, masks))
examples.append(utterances)
return examples
"""
# Regarding producing data.pkl for IEMOCAP:
# The exact code for preprocessing IEMOCAP raw to data.pkl is left somewhere in my older server
# The following code is not gurrenteed to work and just intended to give some clues.
dataset = pickle.load(open("./data/IEMOCAP/IEMOCAP_features_raw.pkl", 'rb'), encoding='latin1')
# randomly select validation sessions
train_ids = list(dataset[7])
val_ids = []
num_vals = 20
while len(set(val_ids)) < num_vals:
random_id = random.choice(train_ids)
val_ids.append(random_id)
val_ids = list(set(val_ids))
train_ids = list(set(train_ids) - set(val_ids))
test_ids = list(dataset[8])
new_dataset = []
new_dataset.append(dataset[1]) # speaker
new_dataset.append(dataset[6]) # utterances
new_dataset.append(dataset[2]) # label
new_dataset.append(train_ids)
new_dataset.append(val_ids)
new_dataset.append(test_ids)
# save new dataset
with open("./data/IEMOCAP/data.pkl", "wb") as f:
pickle.dump(new_dataset, f)
"""
def create_examples_IEMOCAP(split):
dataset = pickle.load(open("./data/IEMOCAP/data.pkl", 'rb'))
if split == "train":
session_ids = dataset[3]
elif split == "val":
session_ids = dataset[4]
else:
session_ids = dataset[5]
print("{0} split has {1} conversations".format(split, len(session_ids)))
examples = []
conv_lengths = []
for i in session_ids:
conv_lengths.append(len(dataset[1][i]))
dummy_emotion = dataset[2][i][0]
dummy_speaker = dataset[0][i][0]
max_conv_length = max(conv_lengths)
print("max_conv_length: ", max_conv_length)
dummy_utterance = "this is a dummy sentence"
for i in session_ids:
speakers = dataset[0][i]
utterances = dataset[1][i]
emotions = dataset[2][i]
assert len(speakers) == len(utterances) == len(emotions)
# pad dummy utterances
conv_length = len(utterances)
masks = conv_length*[1] + (max_conv_length-conv_length)*[0]
utterances += (max_conv_length-conv_length)*[dummy_utterance]
emotions += (max_conv_length-conv_length)*[dummy_emotion]
speakers += (max_conv_length-conv_length)*[dummy_speaker]
# associate utterances with speakers
utterances = list(zip(utterances, speakers, emotions, masks))
examples.append(utterances)
return examples
def clip_conversation_length(examples, max_conversation_length):
"""
examples: a list of examples
max_conversation_length: the max number of utterances in one example
return: a list of clipped examples where each example is limited to the most recent k utterances
"""
clipped_examples = []
num_clips = 0
for ex in examples:
if len(ex) > max_conversation_length+1:
num_clips += 1
ex = ex[-(max_conversation_length+1):]
clipped_examples.append(ex)
print("Number of clipped examples: {0}".format(num_clips))
return clipped_examples
def clean(text, max_sequence_length):
"""
text: a piece of text in str
max_sequence_length: the max sequence length for each utterance
return: a list tokenized cleaned words
"""
# lower case
text = text.lower()
# other cleaning processes
# tokenization
return [token.text for token in spacy_en.tokenizer(text)][:max_sequence_length]
def clean_examples(examples, max_sequence_length):
"""
examples: a list of examples, each example is a list of (utterance, speaker, emotion, mask)
max_sequence_length: the max sequence length for each utterance
return: a list tokenized cleaned examples
"""
cleaned_examples = []
for ex in tqdm(examples):
cleaned_examples.append([(clean(utterance, max_sequence_length), speaker, emotion, mask)
for utterance, speaker, emotion, mask in ex])
return cleaned_examples
create_examples_dict = {
"EC": create_examples_EC,
"DD": create_examples_DD,
"MELD": create_examples_MELD,
"EmoryNLP": create_examples_EmoryNLP,
"IEMOCAP": create_examples_IEMOCAP
}
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Data preprocessing script")
parser.add_argument('--dataset', help='Dataset to preprocess', choices=['EC','DD','MELD', "EmoryNLP", "IEMOCAP"], required=True)
parser.add_argument('--max_conversation_length', type=int, default=10)
parser.add_argument('--max_sequence_length', type=int, default=30)
# parse args
args = parser.parse_args()
dataset = args.dataset
max_conversation_length = args.max_conversation_length
max_sequence_length = args.max_sequence_length
# create examples
print("Preprocessing {0}...".format(dataset))
create_examples = create_examples_dict[dataset]
for split in ["train", "val", "test"]:
examples = create_examples(split)
# examples = clip_conversation_length(examples, max_conversation_length)
examples = clean_examples(examples, max_sequence_length)
# save data
path_to_save = "./data/{0}/{1}.pkl".format(dataset, split)
print("Saving data to {0}".format(path_to_save))
with open(path_to_save, "wb") as f:
pickle.dump(examples, f)