File size: 3,035 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
from utils import parse, read_json_file, write_jsonl_file, write_json_file
from typing import List
import shutil


def reformat(args, domains, split):
    processed_data = []
    ontology = {}
    for domain in domains:
        train_files = [
            filename
            for filename in os.listdir(os.path.join(args.input_dir, domain))
            if split in filename
        ]
        ontology[domain] = {}
        for filename in sorted(train_files):
            filepath = os.path.join(args.input_dir, domain, filename)
            data = read_json_file(filepath)
            for example in data:
                processed_example = {
                    "turn": "single",
                    "locale": "en",
                    "dialog": [
                        {
                            "roles": ["USER"],
                            "utterance": example["userInput"]["text"],
                        }
                    ],
                }

                if "labels" in example:
                    processed_example["dialog"][0]["belief_state"] = [
                        {
                            "informed_slot_value_table": [],
                            "domain": domain,
                        }
                    ]
                    for label in example["labels"]:
                        if "startIndex" in label["valueSpan"]:
                            start = label["valueSpan"]["startIndex"]
                        else:
                            start = 0
                        end = label["valueSpan"]["endIndex"]
                        value = example["userInput"]["text"][start:end]

                        ontology[domain][label["slot"]] = False

                        processed_example["dialog"][0]["belief_state"][0][
                            "informed_slot_value_table"
                        ].append(
                            {
                                "slot": label["slot"],
                                "values": [{"value": value}],
                                "relation": "=",
                            }
                        )
                else:
                    processed_example["dialog"][0]["belief_state"] = []

                processed_data.append(processed_example)

    write_jsonl_file(processed_data, os.path.join(args.output_dir, f"{split}.jsonl"))
    write_json_file(ontology, os.path.join(args.output_dir, f"{split}_ontology.json"))


def preprocess(args):
    domains = ["Buses_1", "Events_1", "Homes_1", "RentalCars_1"]
    reformat(args, domains, "train")
    # reformat(args, domains, "dev")
    reformat(args, domains, "test")

    # copy test split to dev split
    shutil.copy(
        os.path.join(args.output_dir, "test.jsonl"),
        os.path.join(args.output_dir, "dev.jsonl"),
    )
    shutil.copy(
        os.path.join(args.output_dir, "test_ontology.json"),
        os.path.join(args.output_dir, "dev_ontology.json"),
    )


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