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


def preprocess(args, split):
    path = os.path.join(args.input_dir, f"doqa-cooking-{split}-v2.1.json")
    data = read_json_file(path)

    outfile = os.path.join(args.output_dir, f"{split}.jsonl")

    data = data["data"]

    turns = []
    for i in range(len(data)):
        title = data[i]["title"]
        background = data[i]["background"]

        for example in data[i]["paragraphs"]:
            passage = example["context"]
            if passage.endswith("CANNOTANSWER"):
                passage = passage[: -len("CANNOTANSWER")].strip()
            knowledge = {
                "type": "dict",
                "value": {"title": title, "background": background, "passage": passage},
            }

            t = {"turn": "multi", "locale": "en", "dialog": [], "knowledge": knowledge}

            for qa in example["qas"]:
                t["dialog"].append({"roles": ["USER"], "utterance": qa["question"]})

                assert split != "train" or len(qa["answers"]) == 1

                for answer in qa["answers"]:
                    text = (
                        answer["input_text"]
                        if "input_text" in answer
                        else answer["text"]
                    )
                    if text == "CANNOTANSWER":
                        text = "None"

                    t["dialog"].append({"roles": ["SYSTEM"], "utterance": text})

            turns.append(t)

    write_jsonl_file(turns, outfile)


if __name__ == "__main__":
    args = parse()
    preprocess(args, "train")
    preprocess(args, "dev")
    preprocess(args, "test")