File size: 7,304 Bytes
3f27e03
 
 
 
 
 
 
 
 
df5a112
3f27e03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df5a112
3f27e03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df5a112
3f27e03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df5a112
3f27e03
 
 
 
 
5774b28
3f27e03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5774b28
3f27e03
 
 
 
 
 
 
 
 
 
df5a112
 
 
3f27e03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df5a112
 
 
 
 
 
 
3f27e03
 
 
 
 
 
 
 
 
 
 
 
 
 
66eb780
3f27e03
 
 
 
 
 
 
5008aa3
3f27e03
 
66eb780
 
 
 
 
5008aa3
66eb780
 
df5a112
 
 
 
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
"""
 Copyright (c) 2024, Idiap Research Institute.
 All rights reserved.
 SPDX-License-Identifier: MIT License
 For full license text, see the LICENSE file in the repo root
"""

#!/usr/bin/env python3
import os
import csv
import json
import datasets
from datasets import (GeneratorBasedBuilder,
                      BuilderConfig,
                      SplitGenerator, 
                      DatasetInfo, 
                      Features,
                      Value,
                      Version)

logger = datasets.logging.get_logger(__name__)
datasets.logging.disable_progress_bar()

_VERSION = Version("1.0.0")
_CITATION = """
@inproceedings{burdisso-etal-2024-dialog2flow,
    title = "Dialog2Flow: Pre-training Soft-Contrastive Action-Driven Sentence Embeddings for Automatic Dialog Flow Extraction",
    author = "Burdisso, Sergio  and
      Madikeri, Srikanth  and
      Motlicek, Petr",
    booktitle = "Proceedings of the 2024 Conference on Empirical Methods in Natural Language Processing",
    month = nov,
    year = "2024",
    address = "Miami",
    publisher = "Association for Computational Linguistics",
}
"""

DATASETS_PRETRAIN = ["dialog-acts", "slots", "dialog-acts+slots"]
DATASETS_DS = {
    'ABCD': ['test', 'train', 'val'],
    'BiTOD': ['test', 'train', 'val'],
    'DSTC2-Clean': ['test', 'train', 'val'],
    'Disambiguation': ['test', 'train', 'val'],
    'FRAMES': ['test', 'train'],
    'HDSA-Dialog': ['test', 'train', 'val'],
    'GECOR': ['train'],
    'KETOD': ['test', 'train', 'val'],
    'MS-DC': ['train'],
    'MULTIWOZ2_2': ['test', 'train', 'val'],
    'MulDoGO': ['test', 'train', 'val'],
    'MultiWOZ_2.1': ['test', 'train', 'val'],
    'SGD': ['test', 'train', 'val'],
    'SimJointMovie': ['test', 'train', 'val'],
    'SimJointRestaurant': ['test', 'train', 'val'],
    'Taskmaster1': ['test', 'train', 'val'],
    'Taskmaster2': ['train'],
    'Taskmaster3': ['test', 'train', 'val'],
    'WOZ2_0': ['test', 'train', 'val'],
    'SimJointGEN': ['test', 'train', 'val'],
}
DATASETS = list(DATASETS_DS.keys()) + DATASETS_PRETRAIN
SPLIT2NAME = {
    "train": datasets.Split.TRAIN,
    "val": datasets.Split.VALIDATION,
    "test": datasets.Split.TEST,
}


class Dialog2FlowConfig(BuilderConfig):
    """BuilderConfig for Dialog2Flow."""

    def __init__(self, name, citation, url, **kwargs):
        """BuilderConfig for Dialog2Flow.

        Args:
          extra_features: `list[string]`, list of the features that will appear in the
            feature dict. Should not include "label".
          data_url: `string`, url to download the zip file from.
          citation: `string`, citation for the data set.
          url: `string`, url for information about the data set.
          label_classes: `list[string]`, the list of classes for the label if the
            label is present as a string. Non-string labels will be cast to either
            'False' or 'True'.
          **kwargs: keyword arguments forwarded to super.
        """
        super(Dialog2FlowConfig, self).__init__(version=_VERSION, **kwargs)
        self.name = name
        self.citation = citation
        self.url = url


class Dialog2FlowBuilder(GeneratorBasedBuilder):
    BUILDER_CONFIG_CLASS = Dialog2FlowConfig
    BUILDER_CONFIGS = []
    for dataset in DATASETS:
        BUILDER_CONFIGS.append(
            Dialog2FlowConfig(
                name=dataset,
                description="",
                citation=_CITATION,
                url="https://github.com/idiap/dialog2flow",
        ))

    DEFAULT_CONFIG_NAME = "dialog-acts+slots"

    def _info(self):
        if self.config.name in DATASETS_PRETRAIN:
            features = {"utterance": Value("string"), "label": Value("string")}
        else:
            features = {"dialog": [
                {
                    "speaker": Value("string"),
                    "text": Value("string"),
                    "domains": [
                        Value("string")
                    ],
                    "labels": {
                        "dialog_acts": {
                            "acts" : [Value("string")],
                            "main_acts" : [Value("string")],
                            "original_acts" : [Value("string")],
                        },
                        "slots": [Value("string")],
                        "intents": [Value("string")]
                    }
                }
            ]}
        return DatasetInfo(
            description="",
            features=Features(features),
            homepage=self.config.url,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        if self.config.name in DATASETS_PRETRAIN:
            file_path = dl_manager.download({
                "train": f"{self.config.name}.csv",  # full
                "val": "val.csv",  # few shot subset
                # "test": "test.csv",  # TODO: use SpokenWOZ
            })
            splits = [
                SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={
                        "file_path": file_path["train"],
                        "split": datasets.Split.TRAIN,
                    },
                ),
                SplitGenerator(
                    name=datasets.Split.VALIDATION,
                    gen_kwargs={
                        "file_path": file_path["val"],
                        "split": datasets.Split.VALIDATION,
                    },
                ),
                # SplitGenerator(  # TODO: use SpokenWOZ
                #     name=datasets.Split.TEST,
                #     gen_kwargs={
                #         "file_path": file_path["test"],
                #         "split": datasets.Split.TEST,
                #     },
                # )
            ]
        else:
            splits = []
            file_path = dl_manager.download({
                "train": os.path.join(self.config.name, "data.json")
            })
            split_names = DATASETS_DS[self.config.name]
            for split_name in split_names:
                splits.append(
                    SplitGenerator(
                        name=SPLIT2NAME[split_name],
                        gen_kwargs={
                            "file_path": file_path["train"],
                            "split": SPLIT2NAME[split_name],
                            "split_name": split_name
                        },
                    )
                )
        return splits

    def _load_json(self, file_path):
        with open(file_path, encoding="utf-8") as f:
            data = json.load(f)
        return data

    def _generate_examples(self, file_path, split, split_name=None):
        if split_name is not None:
            data = self._load_json(file_path)
            data = [(dial_id, dial) for dial_id, dial in data["dialogs"].items() if split_name in dial_id]
            logger.info(f"generating {len(data)} examples from = {split}")
            for dial_id, dial in data:
                yield dial_id, {"dialog": dial}
        else:
            with open(file_path, newline='') as csvfile:
                csvreader = csv.reader(csvfile)
                for ix, row in enumerate(csvreader):
                    yield ix, {"utterance": row[0], "label": row[1]}