File size: 6,383 Bytes
43d46be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d2a0b6
43d46be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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.
"""Deal or no deal negotiator"""


import datasets


_CITATION = """\
@article{lewis2017deal,
  title={Deal or no deal? end-to-end learning for negotiation dialogues},
  author={Lewis, Mike and Yarats, Denis and Dauphin, Yann N and Parikh, Devi and Batra, Dhruv},
  journal={arXiv preprint arXiv:1706.05125},
  year={2017}
}
"""

_DESCRIPTION = """\
A large dataset of human-human negotiations on a multi-issue bargaining task, where agents who cannot observe each other’s reward functions must reach anagreement (o a deal) via natural language dialogue.
"""

_HOMEPAGE = "https://github.com/facebookresearch/end-to-end-negotiator"

_LICENSE = "The project is licenced under CC-by-NC"

_URLs = {
    "train": "https://raw.githubusercontent.com/facebookresearch/end-to-end-negotiator/master/src/data/negotiate/train.txt",
    "test": "https://raw.githubusercontent.com/facebookresearch/end-to-end-negotiator/master/src/data/negotiate/test.txt",
    "val": "https://raw.githubusercontent.com/facebookresearch/end-to-end-negotiator/master/src/data/negotiate/val.txt",
    "selfplay": "https://raw.githubusercontent.com/facebookresearch/end-to-end-negotiator/master/src/data/negotiate/selfplay.txt",
}


class DealOrNoDialog(datasets.GeneratorBasedBuilder):
    """Deal or no deal negotiator"""

    VERSION = datasets.Version("1.1.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="dialogues",
            description="Consists of 5808 dialogues, based on 2236 unique scenarios.",
            version=VERSION,
        ),
        datasets.BuilderConfig(
            name="self_play", description="Count and values with no dialogues. Used for self playing.", version=VERSION
        ),
    ]

    DEFAULT_CONFIG_NAME = "dialogues"

    def _info(self):
        if self.config.name == "dialogues":
            features = datasets.Features(
                {
                    "input": datasets.Sequence({"count": datasets.Value("int32"), "value": datasets.Value("int32")}),
                    "dialogue": datasets.Value("string"),
                    "output": datasets.Value("string"),
                    "partner_input": datasets.Sequence(
                        {"count": datasets.Value("int32"), "value": datasets.Value("int32")}
                    ),
                }
            )
        else:  # self_play
            features = datasets.Features(
                {
                    "input": datasets.Sequence({"count": datasets.Value("int32"), "value": datasets.Value("int32")}),
                }
            )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        if self.config.name == "dialogues":
            path_train = dl_manager.download_and_extract(_URLs["train"])
            path_test = dl_manager.download_and_extract(_URLs["test"])
            path_val = dl_manager.download_and_extract(_URLs["val"])

            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={
                        "filepath": path_train,
                        "split": "train",
                    },
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    gen_kwargs={"filepath": path_test, "split": "test"},
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.VALIDATION,
                    gen_kwargs={
                        "filepath": path_val,
                        "split": "val",
                    },
                ),
            ]

        else:
            path = dl_manager.download_and_extract(_URLs["selfplay"])
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={
                        "filepath": path,
                        "split": "train",
                    },
                ),
            ]

    def _generate_examples(self, filepath, split="train"):
        """Yields examples."""
        if self.config.name == "dialogues":
            with open(filepath, encoding="utf-8") as f:
                for idx, line in enumerate(f):
                    tokens = line.split()

                    yield idx, {
                        "input": {
                            "count": get_count_value(get_tag(tokens, "input"))[0],
                            "value": get_count_value(get_tag(tokens, "input"))[1],
                        },
                        "dialogue": get_tag(tokens, "dialogue"),
                        "output": get_tag(tokens, "output"),
                        "partner_input": {
                            "count": get_count_value(get_tag(tokens, "partner_input"))[0],
                            "value": get_count_value(get_tag(tokens, "partner_input"))[1],
                        },
                    }

        else:
            with open(filepath, encoding="utf-8") as f:
                for idx, line in enumerate(f):
                    yield idx, {"input": {"count": get_count_value(line)[0], "value": get_count_value(line)[1]}}


def get_tag(tokens, tag):
    return " ".join(tokens[tokens.index("<" + tag + ">") + 1 : tokens.index("</" + tag + ">")])


def get_count_value(sequence):
    seq_list = [int(el) for el in sequence.split()]
    assert len(seq_list) == 6
    return [seq_list[idx] for idx in [0, 2, 4]], [seq_list[idx] for idx in [1, 3, 5]]