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


def load_intent2domain(args):
    data = read_json_file(os.path.join(args.input_dir, "domains.json"))

    intent2domain = {"oos": "oos"}

    for domain in data:
        for intent in data[domain]:
            assert intent not in intent2domain
            intent2domain[intent] = domain

    return intent2domain


def load_sent2domain(filename, intent2domain):
    data = read_json_file(filename)

    sent2domain = dict()
    for split in data:
        for example in data[split]:
            sent2domain[example[0]] = intent2domain[example[1]]

    return sent2domain


def reformat(data_file, data_dir, sent2domain):
    origin_data = read_json_file(data_file)

    data = dict()
    for split in origin_data:
        if "oos" in split:
            continue
        data[split] = []

        for example in origin_data[split]:
            dial = {
                "turn": "single",
                "locale": "en",
                "domain": [sent2domain[example[0]]]
                if example[0] in sent2domain
                else ["oos"],
                "dialog": [
                    {
                        "roles": ["USER"],
                        "utterance": example[0],
                        "active_intents": [example[1]],
                    }
                ],
            }
            data[split].append(dial)

    for split in data:
        if "val" in split:
            part = split.replace("val", "dev")
        else:
            part = split
        outfile = os.path.join(data_dir, f"{part}.jsonl")
        write_jsonl_file(data[split], outfile)


def preprocess(args):
    intent2domain = load_intent2domain(args)
    sent2domain = load_sent2domain(
        os.path.join(args.input_dir, "data_full.json"), intent2domain
    )

    data_file = os.path.join(args.input_dir, "data_full.json")
    # data_dir = os.path.join(args.output_dir, filename[:-5])
    data_dir = args.output_dir

    reformat(data_file, data_dir, sent2domain)


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