File size: 1,099 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 read_json_file, parse, write_jsonl_file
import os


def reformat(args, file):
    path = os.path.join(args.input_dir, f"{file}.json")
    data = read_json_file(path)

    turns = []
    for turn in data:
        t = {"turn": "multi", "locale": "en", "dialog": []}
        for i, utt in enumerate(turn["History"][2:]):
            d = {"roles": [("USER", "SYSTEM")[i % 2]], "utterance": utt}
            t["dialog"].append(d)
        d = {
            "roles": ["USER"],
            "utterance": turn["Question"],
            "rewritten": turn["Rewrite"],
        }
        t["dialog"].append(d)

        t["knowledge"] = {
            "type": "dict",
            "value": {
                "article title": turn["History"][0],
                "section title": turn["History"][1],
            },
        }

        turns.append(t)

    write_jsonl_file(turns, os.path.join(args.output_dir, f"{file}.jsonl"))


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


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