File size: 6,418 Bytes
9ea5b68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
# Dataset license is specified in the folder data/LICENSE.txt

# 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.

import os
from unicodedata import name

import datasets

_CITATION = """\
@article{bordes2016learning,
  title={Learning end-to-end goal-oriented dialog},
  author={Bordes, Antoine and Boureau, Y-Lan and Weston, Jason},
  journal={arXiv preprint arXiv:1605.07683},
  year={2016}
}
"""

_DESCRIPTION = """\
This section presents the set of 6 tasks for testing end-to-end dialog systems in the restaurant domain described in the paper:

Antoine Bordes, Y-Lan Boureau, Jason Weston, Learning End-to-End Goal-Oriented Dialog, arxiv:1605.07683.

Each task tests a unique aspect of dialog. Tasks are designed to complement the set of 20 bAbI tasks for story understanding of the previous section.

For each task, there are 1000 dialogs for training, 1000 for development and 1000 for testing. For tasks 1-5, we also include a second test set (with suffix -OOV.txt) that contains dialogs including entities not present in training and development sets.

"""

_HOMEPAGE = "https://research.facebook.com/downloads/babi/"

_LICENSE = "data/LICENSE.txt"

DIALOG_BABI_ARCHIVE_URL = "https://www.dropbox.com/s/20rgyj8rryvos9l/dialog-bAbI-tasks-1_6.zip?dl=1"


class DialogBabiDataset(datasets.GeneratorBasedBuilder):
    """Facebook's dataset 'dialog-babi' for end-to-end dialogue learning."""

    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="task1-API-calls", version=VERSION, description="Issuing API calls"),
        datasets.BuilderConfig(name="task2-API-refine", version=VERSION, description="Updating API calls"),
        datasets.BuilderConfig(name="task3-options", version=VERSION, description="Displaying options"),
        datasets.BuilderConfig(name="task4-phone-address", version=VERSION, description="Providing extra information"),
        datasets.BuilderConfig(name="task5-full-dialogs", version=VERSION, description="Conducting full dialogs"),
        datasets.BuilderConfig(name="task6-dstc2", version=VERSION, description="2nd Dialog State Tracking Challenge (Henderson et al., 2014a)"),
    ]

    DEFAULT_CONFIG_NAME = "task1-API-calls"

    def _info(self):
        if self.config.name == "task1-API-calls":  
            features = datasets.Features(
                {
                    "user_turns": datasets.Sequence(datasets.Value("string")),
                    "system_turns": datasets.Sequence(datasets.Value("string"))
                }
            )
        else:  
            features = datasets.Features(
                {
                    "user_turns": datasets.Sequence(datasets.Value("string")),
                    "system_turns": datasets.Sequence(datasets.Value("string"))
                }
            )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        data_dir = dl_manager.download_and_extract(DIALOG_BABI_ARCHIVE_URL)
        task_data_dir = os.path.join(data_dir, "dialog-bAbI-tasks-1_6", self.config.name)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": os.path.join(task_data_dir, f"dialog-babi-{self.config.name}-trn.txt"),
                    "split": f"{self.config.name}-train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": os.path.join(task_data_dir, f"dialog-babi-{self.config.name}-dev.txt"),
                    "split": f"{self.config.name}-dev",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": os.path.join(task_data_dir, f"dialog-babi-{self.config.name}-tst.txt"),
                    "split": f"{self.config.name}-test"
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split("TESTOOV"),
                gen_kwargs={
                    "filepath": os.path.join(task_data_dir, f"dialog-babi-{self.config.name}-tst-OOV.txt"),
                    "split": f"{self.config.name}-test-OOV"
                },
            )
        ]

    def _generate_examples(self, filepath, split):
        with open(filepath, encoding="utf-8") as f:
            dialogue_rows = []
            dialogue_id = 1

            for row in f:
                turn = row.strip()
                if not turn:
                    yield self._format_dialogue(dialogue_id,split, dialogue_rows)
                    dialogue_rows.clear()
                    dialogue_id += 1
                else:
                    dialogue_rows.append(turn)
            
            if dialogue_rows:
                yield self._format_dialogue(dialogue_id, split, dialogue_rows)
    
    def _format_dialogue(self, dialogue_id, split, dialogue_rows):
        
        user_turns = []
        system_turns = []

        for turn in dialogue_rows:
            rest_turn, sys_turn = turn.split("\t")
            _, user_turn = rest_turn.split(" ", 1)
            user_turns.append(user_turn)
            system_turns.append(sys_turn)


        example_key = f"{split}-{dialogue_id}"
        return example_key, {
            "user_turns": user_turns,
            "system_turns": system_turns
        }