from utils import parse, write_jsonl_file, read_json_file | |
import os | |
label2emotion = { | |
"fru": "frustrated", | |
"ang": "angry", | |
"neu": "neutral", | |
"exc": "excited", | |
"hap": "happy", | |
"sad": "sad", | |
} | |
def preprocess(args, split): | |
input_dir = os.path.join(args.input_dir, f"{split}_data.json") | |
data = read_json_file(input_dir) | |
dialogues = [] | |
cnt = 0 | |
for dialogue in data: | |
dial = {"turn": "multi", "locale": "en", "dialog": []} | |
for turn in dialogue: | |
_turn = { | |
"roles": [turn["speaker"]], | |
"utterance": turn["text"], | |
} | |
if "label" in turn: | |
_turn["emotions"] = [{"emotion": label2emotion[turn["label"]]}] | |
cnt += 1 | |
dial["dialog"].append(_turn) | |
dialogues.append(dial) | |
write_jsonl_file(dialogues, os.path.join(args.output_dir, f"{split}.jsonl")) | |
if __name__ == "__main__": | |
args = parse() | |
preprocess(args, "train") | |
preprocess(args, "dev") | |
preprocess(args, "test") | |