File size: 12,718 Bytes
531820d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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.

import os
import io
import datasets
import constants
import pickle

_CITATION = """\
@InProceedings{mchen-discoeval-19,
                title = {Evaluation Benchmarks and Learning Criteria for Discourse-Aware Sentence Representations},
                author = {Mingda Chen and Zewei Chu and Kevin Gimpel},
                booktitle = {Proc. of {EMNLP}},
                year={2019}
              }
"""

_DESCRIPTION = """\
This dataset contains all tasks of the DiscoEval benchmark for sentence representation learning.
"""

_HOMEPAGE = "https://github.com/ZeweiChu/DiscoEval"


# TODO: Add link to the official dataset URLs here
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
# _URLS = {
#     "first_domain": "https://huggingface.co/great-new-dataset-first_domain.zip",
#     "second_domain": "https://huggingface.co/great-new-dataset-second_domain.zip",
# }


# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
class DiscoEvalSentence(datasets.GeneratorBasedBuilder):
    """DiscoEval Benchmark"""

    VERSION = datasets.Version("1.1.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name=constants.SPARXIV,
            version=VERSION,
            description="Sentence positioning dataset from arXiv",
        ),
        datasets.BuilderConfig(
            name=constants.SPROCSTORY,
            version=VERSION,
            description="Sentence positioning dataset from ROCStory",
        ),
        datasets.BuilderConfig(
            name=constants.SPWIKI,
            version=VERSION,
            description="Sentence positioning dataset from Wikipedia",
        ),
        datasets.BuilderConfig(
            name=constants.DCCHAT,
            version=VERSION,
            description="Discourse Coherence dataset from chat",
        ),
        datasets.BuilderConfig(
            name=constants.DCWIKI,
            version=VERSION,
            description="Discourse Coherence dataset from Wikipedia",
        ),
        datasets.BuilderConfig(
            name=constants.RST,
            version=VERSION,
            description="The RST Discourse Treebank dataset ",
        ),
        datasets.BuilderConfig(
            name=constants.PDTB_E,
            version=VERSION,
            description="The Penn Discourse Treebank - Explicit dataset.",
        ),
        datasets.BuilderConfig(
            name=constants.PDTB_I,
            version=VERSION,
            description="The Penn Discourse Treebank - Implicit dataset.",
        ),
        datasets.BuilderConfig(
            name=constants.SSPABS,
            version=VERSION,
            description="The SSP dataset.",
        ),
    ]

    DEFAULT_CONFIG_NAME = constants.SPARXIV  # It's not mandatory to have a default configuration. Just use one if it make sense.

    def _info(self):
        # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset

        if self.config.name in [constants.SPARXIV, constants.SPROCSTORY, constants.SPWIKI]:
            features_dict = {
                constants.TEXT_COLUMN_NAME[i]: datasets.Value('string')
                for i in range(constants.SP_TEXT_COLUMNS + 1)
            }
            features_dict[constants.LABEL_NAME] = datasets.ClassLabel(names=constants.SP_LABELS)
            features = datasets.Features(features_dict)

        elif self.config.name in [constants.DCCHAT, constants.DCWIKI]:
            features_dict = {
                constants.TEXT_COLUMN_NAME[i]: datasets.Value('string')
                for i in range(constants.DC_TEXT_COLUMNS + 1)
            }
            features_dict[constants.LABEL_NAME] = datasets.ClassLabel(names=constants.DC_LABELS)
            features = datasets.Features(features_dict)

        elif self.config.name in [constants.RST]:
            features_dict = {
                constants.TEXT_COLUMN_NAME[i]: [datasets.Value('string')]
                for i in range(constants.RST_TEXT_COLUMNS + 1)
            }
            features_dict[constants.LABEL_NAME] = datasets.ClassLabel(names=constants.RST_LABELS)
            features = datasets.Features(features_dict)

        elif self.config.name in [constants.PDTB_E]:
            features_dict = {
                constants.TEXT_COLUMN_NAME[i]: datasets.Value('string')
                for i in range(constants.PDTB_E_TEXT_COLUMNS + 1)
            }
            features_dict[constants.LABEL_NAME] = datasets.ClassLabel(names=constants.PDTB_E_LABELS)
            features = datasets.Features(features_dict)

        elif self.config.name in [constants.PDTB_I]:
            features_dict = {
                constants.TEXT_COLUMN_NAME[i]: datasets.Value('string')
                for i in range(constants.PDTB_I_TEXT_COLUMNS + 1)
            }
            features_dict[constants.LABEL_NAME] = datasets.ClassLabel(names=constants.PDTB_I_LABELS)
            features = datasets.Features(features_dict)

        elif self.config.name in [constants.SSPABS]:
            features_dict = {
                constants.TEXT_COLUMN_NAME[i]: datasets.Value('string')
                for i in range(constants.SSPABS_TEXT_COLUMNS + 1)
            }
            features_dict[constants.LABEL_NAME] = datasets.ClassLabel(names=constants.SSPABS_LABELS)
            features = datasets.Features(features_dict)

        else:  # This is an example to show how to have different features for "first_domain" and "second_domain"
            features = datasets.Features(
                {
                    "sentence": datasets.Value("string"),
                    "option2": datasets.Value("string"),
                    "second_domain_answer": datasets.Value("string")
                    # These are the features of your dataset like images, labels ...
                }
            )
        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features=features,  # Here we define them above because they are different between the two configurations
            # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
            # specify them. They'll be used if as_supervised=True in builder.as_dataset.
            # supervised_keys=("sentence", "label"),
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE,
            # Citation for the dataset
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
        # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name

        # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
        # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
        # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive

        # urls = _URLS[self.config.name]
        # data_dir = dl_manager.download_and_extract(urls)
        if self.config.name in [constants.SPARXIV, constants.SPROCSTORY, constants.SPWIKI]:
            data_dir = os.path.join(constants.SP_DATA_DIR, self.config.name)
            train_name = constants.SP_TRAIN_NAME
            valid_name = constants.SP_VALID_NAME
            test_name = constants.SP_TEST_NAME

        elif self.config.name in [constants.DCCHAT, constants.DCWIKI]:
            data_dir = os.path.join(constants.DC_DATA_DIR, self.config.name)
            train_name = constants.DC_TRAIN_NAME
            valid_name = constants.DC_VALID_NAME
            test_name = constants.DC_TEST_NAME

        elif self.config.name in [constants.RST]:
            data_dir = constants.RST_DATA_DIR
            train_name = constants.RST_TRAIN_NAME
            valid_name = constants.RST_VALID_NAME
            test_name = constants.RST_TEST_NAME

        elif self.config.name in [constants.PDTB_E]:
            data_dir = os.path.join(constants.PDTB_DATA_DIR, constants.PDTB_E)
            train_name = constants.PDTB_TRAIN_NAME
            valid_name = constants.PDTB_VALID_NAME
            test_name = constants.PDTB_TEST_NAME

        elif self.config.name in [constants.PDTB_I]:
            data_dir = os.path.join(constants.PDTB_DATA_DIR, constants.PDTB_I)
            train_name = constants.PDTB_TRAIN_NAME
            valid_name = constants.PDTB_VALID_NAME
            test_name = constants.PDTB_TEST_NAME

        elif self.config.name in [constants.SSPABS]:
            data_dir = constants.SSPABS_DATA_DIR
            train_name = constants.SSPABS_TRAIN_NAME
            valid_name = constants.SSPABS_VALID_NAME
            test_name = constants.SSPABS_TEST_NAME

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": os.path.join(data_dir, train_name),
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": os.path.join(data_dir, valid_name),
                    "split": "dev",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": os.path.join(data_dir, test_name),
                    "split": "test"
                },
            ),
        ]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, filepath, split):
        # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
        # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
        if self.config.name in [constants.SPARXIV, constants.SPROCSTORY, constants.SPWIKI,
                                constants.DCWIKI, constants.DCCHAT,
                                constants.PDTB_E, constants.PDTB_I,
                                constants.SSPABS]:
            with io.open(filepath, mode='r', encoding='utf-8') as f:
                for key, line in enumerate(f):
                    line = line.strip().split("\t")
                    example = {constants.TEXT_COLUMN_NAME[i]: sent for i, sent in enumerate(line[1:])}
                    example[constants.LABEL_NAME] = line[0]
                    yield key, example

        elif self.config.name in [constants.RST]:
            data = pickle.load(open(filepath, "rb"))
            for key, line in enumerate(data):
                example = {constants.TEXT_COLUMN_NAME[i]: sent for i, sent in enumerate(line[1:])}
                example[constants.LABEL_NAME] = line[0]
                yield key, example

        # TODO: implement other datasets
        else:
            yield 0, {
                "sentence": 'example sentences',
                "option2": 'another example sentence',
                "second_domain_answer": "" if split == "test" else 'label',
            }


if __name__ == '__main__':
    data = pickle.load(open(r'/data/RST/RST_TRAIN.pkl', "rb"))
    sents = []
    labels = []
    for d in data:
        input1= d[1]
        input2 = d[2]
        label = d[0]
    ofek = 5