File size: 14,997 Bytes
a33738f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# coding=utf-8
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""SGD: The Schema Guided Dialogue dataet"""


import json

import datasets


_CITATION = """\
@inproceedings{aaai/RastogiZSGK20,
  author    = {Abhinav Rastogi and
               Xiaoxue Zang and
               Srinivas Sunkara and
               Raghav Gupta and
               Pranav Khaitan},
  title     = {Towards Scalable Multi-Domain Conversational Agents: The Schema-Guided
               Dialogue Dataset},
  booktitle = {The Thirty-Fourth {AAAI} Conference on Artificial Intelligence, {AAAI}
               2020, The Thirty-Second Innovative Applications of Artificial Intelligence
               Conference, {IAAI} 2020, The Tenth {AAAI} Symposium on Educational
               Advances in Artificial Intelligence, {EAAI} 2020, New York, NY, USA,
               February 7-12, 2020},
  pages     = {8689--8696},
  publisher = {{AAAI} Press},
  year      = {2020},
  url       = {https://aaai.org/ojs/index.php/AAAI/article/view/6394}
}
"""

_DESCRIPTION = """\
The Schema-Guided Dialogue dataset (SGD) was developed for the Dialogue State Tracking task of the Eights Dialogue Systems Technology Challenge (dstc8).
The SGD dataset consists of over 18k annotated multi-domain, task-oriented conversations between a human and a virtual assistant.
These conversations involve interactions with services and APIs spanning 17 domains, ranging from banks and events to media, calendar, travel, and weather.
For most of these domains, the SGD dataset contains multiple different APIs, many of which have overlapping functionalities but different interfaces,
which reflects common real-world scenarios.
"""

_HOMEPAGE = "https://github.com/google-research-datasets/dstc8-schema-guided-dialogue"

_LICENSE = "CC BY-SA 4.0"

_URL_LIST = [
    (
        "train_schema.json",
        "https://github.com/google-research-datasets/dstc8-schema-guided-dialogue/raw/master/train/schema.json",
    ),
    (
        "dev_schema.json",
        "https://github.com/google-research-datasets/dstc8-schema-guided-dialogue/raw/master/dev/schema.json",
    ),
    (
        "test_schema.json",
        "https://github.com/google-research-datasets/dstc8-schema-guided-dialogue/raw/master/test/schema.json",
    ),
]
_URL_LIST += [
    (
        f"train_dialogues_{i:03d}.json",
        f"https://github.com/google-research-datasets/dstc8-schema-guided-dialogue/raw/master/train/dialogues_{i:03d}.json",
    )
    for i in range(1, 128)
]
_URL_LIST += [
    (
        f"dev_dialogues_{i:03d}.json",
        f"https://github.com/google-research-datasets/dstc8-schema-guided-dialogue/raw/master/dev/dialogues_{i:03d}.json",
    )
    for i in range(1, 21)
]
_URL_LIST += [
    (
        f"test_dialogues_{i:03d}.json",
        f"https://github.com/google-research-datasets/dstc8-schema-guided-dialogue/raw/master/test/dialogues_{i:03d}.json",
    )
    for i in range(1, 35)
]

_URLs = dict(_URL_LIST)

_USER_ACTS = [
    "INFORM_INTENT",
    "NEGATE_INTENT",
    "AFFIRM_INTENT",
    "INFORM",
    "REQUEST",
    "AFFIRM",
    "NEGATE",
    "SELECT",
    "REQUEST_ALTS",
    "THANK_YOU",
    "GOODBYE",
]

_SYSTEM_ACTS = [
    "INFORM",
    "REQUEST",
    "CONFIRM",
    "OFFER",
    "NOTIFY_SUCCESS",
    "NOTIFY_FAILURE",
    "INFORM_COUNT",
    "OFFER_INTENT",
    "REQ_MORE",
    "GOODBYE",
]

_ALL_ACTS = sorted(list(set(_USER_ACTS + _SYSTEM_ACTS)))


class SchemaGuidedDstc8(datasets.GeneratorBasedBuilder):

    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="dialogues", description="The dataset of annotated dialogues."),
        datasets.BuilderConfig(name="schema", description="The schemas corresponding to the API calls."),
    ]

    DEFAULT_CONFIG_NAME = "dialogues"

    def _info(self):
        if self.config.name == "schema":
            features = datasets.Features(
                {
                    "service_name": datasets.Value("string"),
                    "description": datasets.Value("string"),
                    "slots": datasets.Sequence(
                        {
                            "name": datasets.Value("string"),
                            "description": datasets.Value("string"),
                            "is_categorical": datasets.Value("bool"),
                            "possible_values": datasets.Sequence(datasets.Value("string")),
                        }
                    ),
                    "intents": datasets.Sequence(
                        {
                            "name": datasets.Value("string"),
                            "description": datasets.Value("string"),
                            "is_transactional": datasets.Value("bool"),
                            "required_slots": datasets.Sequence(datasets.Value("string")),
                            # optional_slots was originally a dictionary
                            "optional_slots": datasets.Sequence(
                                {
                                    "slot_name": datasets.Value("string"),
                                    "slot_value": datasets.Value("string"),
                                }
                            ),
                            "result_slots": datasets.Sequence(datasets.Value("string")),
                        },
                    ),
                }
            )
        else:
            features = datasets.Features(
                {
                    "dialogue_id": datasets.Value("string"),
                    "services": datasets.Sequence(datasets.Value("string")),
                    "turns": datasets.Sequence(
                        {
                            "speaker": datasets.ClassLabel(names=["USER", "SYSTEM"]),
                            "utterance": datasets.Value("string"),
                            "frames": datasets.Sequence(
                                {
                                    "service": datasets.Value("string"),
                                    "slots": datasets.Sequence(
                                        {
                                            "slot": datasets.Value("string"),
                                            "start": datasets.Value("int32"),
                                            "exclusive_end": datasets.Value("int32"),
                                        }
                                    ),
                                    # optional
                                    "state": {
                                        "active_intent": datasets.Value("string"),
                                        "requested_slots": datasets.Sequence(datasets.Value("string")),
                                        # slot_values was originally a dictionary
                                        "slot_values": datasets.Sequence(
                                            {
                                                "slot_name": datasets.Value("string"),
                                                "slot_value_list": datasets.Sequence(datasets.Value("string")),
                                            }
                                        ),
                                    },
                                    "actions": datasets.Sequence(
                                        {
                                            "act": datasets.ClassLabel(names=_ALL_ACTS),
                                            # optional
                                            "slot": datasets.Value("string"),
                                            # optional
                                            "canonical_values": datasets.Sequence(datasets.Value("string")),
                                            # optional
                                            "values": datasets.Sequence(datasets.Value("string")),
                                        }
                                    ),
                                    # optional
                                    "service_results": datasets.Sequence(
                                        # Arrow doesn't like Sequences of Sequences for default values so we need a Sequence of Features of Sequences
                                        {
                                            "service_results_list": datasets.Sequence(
                                                # originally each list item was a dictionary (optional)
                                                {
                                                    "service_slot_name": datasets.Value("string"),
                                                    "service_canonical_value": datasets.Value("string"),
                                                }
                                            )
                                        }
                                    ),
                                    # optional
                                    "service_call": {
                                        "method": datasets.Value("string"),
                                        # parameters was originally a dictionary
                                        "parameters": datasets.Sequence(
                                            {
                                                "parameter_slot_name": datasets.Value("string"),
                                                "parameter_canonical_value": datasets.Value("string"),
                                            }
                                        ),
                                    },
                                }
                            ),
                        }
                    ),
                }
            )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,  # Here we define them above because they are different between the two configurations
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        data_files = dl_manager.download_and_extract(_URLs)
        return [
            datasets.SplitGenerator(
                name=spl_enum,
                gen_kwargs={
                    "filepaths": data_files,
                    "split": spl,
                },
            )
            for spl, spl_enum in [
                ("train", datasets.Split.TRAIN),
                ("dev", datasets.Split.VALIDATION),
                ("test", datasets.Split.TEST),
            ]
        ]

    def _generate_examples(self, filepaths, split):
        id_ = -1
        file_list = [fpath for fname, fpath in filepaths.items() if fname.startswith(f"{split}_{self.config.name}")]
        for filepath in file_list:
            examples = json.load(open(filepath, encoding="utf-8"))
            for example in examples:
                id_ += 1
                if self.config.name == "schema":
                    example["intents"] = example.get("intents", [])
                    for intent in example["intents"]:
                        optional_slots = intent.get("optional_slots", {})
                        intent["optional_slots"] = {
                            "slot_name": list(optional_slots.keys()),
                            "slot_value": list(optional_slots.values()),
                        }
                else:
                    for turn in example["turns"]:
                        for frame in turn["frames"]:
                            # add empty state if the key is missing from the dict
                            frame["state"] = frame.get(
                                "state",
                                {
                                    "active_intent": "",
                                    "requested_slots": [],
                                    "slot_values": {},
                                },
                            )
                            # linearize the optional slot_values dictionary
                            slot_values_dict = frame["state"].get("slot_values", {})
                            frame["state"]["slot_values"] = {
                                "slot_name": list(slot_values_dict.keys()),
                                "slot_value_list": list(slot_values_dict.values()),
                            }
                            # add default values for optional fields in actions
                            for action in frame["actions"]:
                                action["slot"] = action.get("slot", "")
                                action["canonical_values"] = action.get("canonical_values", [])
                                action["values"] = action.get("values", [])
                            # add "service_results" field when necessary and linearize the dictionaries in the list otherwise
                            service_results = []
                            for result in frame.get("service_results", []):
                                service_results += [
                                    {
                                        "service_slot_name": list(result.keys()),
                                        "service_canonical_value": list(result.values()),
                                    }
                                ]
                            frame["service_results"] = {
                                "service_results_list": service_results,
                            }
                            # add "service_call" field when necessary and linearize the parameters dictionary otherwise
                            frame["service_call"] = frame.get(
                                "service_call",
                                {
                                    "method": "",
                                    "parameters": {},
                                },
                            )
                            parameters_dict = frame["service_call"].get("parameters", {})
                            frame["service_call"]["parameters"] = {
                                "parameter_slot_name": list(parameters_dict.keys()),
                                "parameter_canonical_value": list(parameters_dict.values()),
                            }
                yield id_, example