File size: 1,405 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from utils import (
    read_jsonl_file,
    write_jsonl_file,
    parse,
)

label2relation = {
    "1": "Child and Parent",
    "2": "Child and Other Family Elder",
    "3": "Siblings",
    "4": "Spouse",
    "5": "Lovers",
    "6": "Courtship",
    "7": "Friends",
    "8": "Neighbors",
    "9": "Roommates",
    "10": "Workplace Superior and Subordinate",
    "11": "Colleague or Partners",
    "12": "Opponents",
    "13": "Professional Contact",
}


def reformat(args, file):
    path = args.input_dir + "/" + file + ".txt"
    data = read_jsonl_file(path)

    turns = []
    for i in range(len(data)):
        t = {"turn": "multi", "locale": "en", "dialog": []}
        AB2name = {"A": data[i]["nameA"], "B": data[i]["nameB"]}

        for c in data[i]["context"]:
            role = c[0]
            utterance = c[2:].strip()
            # one bad case
            if role not in AB2name:
                role = "B"
                utterance = c

            d = {"roles": [AB2name[role]], "utterance": utterance}
            t["dialog"].append(d)

        t["role_relations"] = [{"relation": label2relation[data[i]["label"]]}]

        turns.append(t)

    write_jsonl_file(turns, args.output_dir + "/" + file + ".jsonl")


def preprocess(args):
    reformat(args, "train")
    reformat(args, "dev")
    reformat(args, "test")


if __name__ == "__main__":
    args = parse()
    preprocess(args)