File size: 1,770 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
61
62
from utils import read_json_file, write_jsonl_file, parse, choices
import os
import copy


def reformat(args, split):
    infile = os.path.join(args.input_dir, f"{split}.json")
    outfile = os.path.join(args.output_dir, f"{split}.jsonl")

    data = read_json_file(infile)
    processed_data = []
    for example in data:
        knowledge_dial = []
        for turn in example[0]:
            role = turn[0]
            role = "woman" if role == "W" else "man"
            text = turn[2:].strip()
            knowledge_dial.append({"roles": [role], "utterance": text})
        _dial = {
            "turn": "single",
            "locale": "en",
            "dialog": [],
            "knowledge": {"type": "list", "value": {}},
        }

        for turn in example[1]:
            dialogue = copy.deepcopy(_dial)
            dialogue["dialog"].append(
                {"roles": ["USER"], "utterance": turn["question"]}
            )

            label = -1
            for idx, choice in enumerate(turn["choice"]):
                # dialogue["dialog"].append(
                #     {"roles": [f"{choices[idx]} choice"], "utterance": choice}
                # )

                dialogue["knowledge"]["value"][chr(ord('A') + idx)] = choice

                if choice == turn["answer"]:
                    label = idx

            assert label >= 0

            dialogue["dialog"][-1]["roles_to_select"] = [chr(ord('A') + label)]
            dialogue["knowledge"]["value"]["dialogue"] = knowledge_dial

            processed_data.append(dialogue)

    write_jsonl_file(processed_data, outfile)


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


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