File size: 956 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
import re
from utils import (
    read_json_file,
    read_jsonl_file,
    write_json_file,
    write_jsonl_file,
    parse,
)


def reformat(args, file):
    path = args.input_dir + "/" + file + ".json"
    data = read_json_file(path)
    turns = []
    for turn_ in data:
        turn = data[turn_]
        speaker = turn["speaker"]
        t = {"turn": "multi", "locale": "en", "dialog": []}
        for j, tt in enumerate(turn["turns"]):
            d = {"roles": [str([speaker, "third-person"][j % 2])], "utterance": tt}
            t["dialog"].append(d)

        t["knowledge"] = {"type": "str", "value": {"context": turn["context"]}}
        turns.append(t)

    if file == "valid":
        file = "dev"
    write_jsonl_file(turns, args.output_dir + "/" + file + ".jsonl")


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


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