File size: 1,054 Bytes
a6326c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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")