OfekGlick commited on
Commit
64af2a3
1 Parent(s): 3022dd2

Upload 2 files

Browse files
Files changed (2) hide show
  1. DiscoEval.py +257 -0
  2. DiscoEvalConstants.py +138 -0
DiscoEval.py ADDED
@@ -0,0 +1,257 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import os
16
+ import io
17
+ import datasets
18
+ import DiscoEvalConstants
19
+ import pickle
20
+ import logging
21
+
22
+ _CITATION = """\
23
+ @InProceedings{mchen-discoeval-19,
24
+ title = {Evaluation Benchmarks and Learning Criteria for Discourse-Aware Sentence Representations},
25
+ author = {Mingda Chen and Zewei Chu and Kevin Gimpel},
26
+ booktitle = {Proc. of {EMNLP}},
27
+ year={2019}
28
+ }
29
+ """
30
+
31
+ _DESCRIPTION = """\
32
+ This dataset contains all tasks of the DiscoEval benchmark for sentence representation learning.
33
+ """
34
+
35
+ _HOMEPAGE = "https://github.com/ZeweiChu/DiscoEval"
36
+
37
+
38
+ class DiscoEvalSentence(datasets.GeneratorBasedBuilder):
39
+ """DiscoEval Benchmark"""
40
+ VERSION = datasets.Version("1.1.0")
41
+ BUILDER_CONFIGS = [
42
+ datasets.BuilderConfig(
43
+ name=DiscoEvalConstants.SPARXIV,
44
+ version=VERSION,
45
+ description="Sentence positioning dataset from arXiv",
46
+ ),
47
+ datasets.BuilderConfig(
48
+ name=DiscoEvalConstants.SPROCSTORY,
49
+ version=VERSION,
50
+ description="Sentence positioning dataset from ROCStory",
51
+ ),
52
+ datasets.BuilderConfig(
53
+ name=DiscoEvalConstants.SPWIKI,
54
+ version=VERSION,
55
+ description="Sentence positioning dataset from Wikipedia",
56
+ ),
57
+ datasets.BuilderConfig(
58
+ name=DiscoEvalConstants.DCCHAT,
59
+ version=VERSION,
60
+ description="Discourse Coherence dataset from chat",
61
+ ),
62
+ datasets.BuilderConfig(
63
+ name=DiscoEvalConstants.DCWIKI,
64
+ version=VERSION,
65
+ description="Discourse Coherence dataset from Wikipedia",
66
+ ),
67
+ datasets.BuilderConfig(
68
+ name=DiscoEvalConstants.RST,
69
+ version=VERSION,
70
+ description="The RST Discourse Treebank dataset ",
71
+ ),
72
+ datasets.BuilderConfig(
73
+ name=DiscoEvalConstants.PDTB_E,
74
+ version=VERSION,
75
+ description="The Penn Discourse Treebank - Explicit dataset.",
76
+ ),
77
+ datasets.BuilderConfig(
78
+ name=DiscoEvalConstants.PDTB_I,
79
+ version=VERSION,
80
+ description="The Penn Discourse Treebank - Implicit dataset.",
81
+ ),
82
+ datasets.BuilderConfig(
83
+ name=DiscoEvalConstants.SSPABS,
84
+ version=VERSION,
85
+ description="The SSP dataset.",
86
+ ),
87
+ datasets.BuilderConfig(
88
+ name=DiscoEvalConstants.BSOARXIV,
89
+ version=VERSION,
90
+ description="The BSO Task with the arxiv dataset.",
91
+ ),
92
+ datasets.BuilderConfig(
93
+ name=DiscoEvalConstants.BSOWIKI,
94
+ version=VERSION,
95
+ description="The BSO Task with the wiki dataset.",
96
+ ),
97
+ datasets.BuilderConfig(
98
+ name=DiscoEvalConstants.BSOROCSTORY,
99
+ version=VERSION,
100
+ description="The BSO Task with the rocstory dataset.",
101
+ ),
102
+ ]
103
+
104
+ def _info(self):
105
+ if self.config.name in [DiscoEvalConstants.SPARXIV, DiscoEvalConstants.SPROCSTORY, DiscoEvalConstants.SPWIKI]:
106
+ features_dict = {
107
+ DiscoEvalConstants.TEXT_COLUMN_NAME[i]: datasets.Value('string')
108
+ for i in range(DiscoEvalConstants.SP_TEXT_COLUMNS)
109
+ }
110
+ features_dict[DiscoEvalConstants.LABEL_NAME] = datasets.ClassLabel(names=DiscoEvalConstants.SP_LABELS)
111
+ features = datasets.Features(features_dict)
112
+
113
+ elif self.config.name in [DiscoEvalConstants.BSOARXIV, DiscoEvalConstants.BSOWIKI, DiscoEvalConstants.BSOROCSTORY]:
114
+ features_dict = {
115
+ DiscoEvalConstants.TEXT_COLUMN_NAME[i]: datasets.Value('string')
116
+ for i in range(DiscoEvalConstants.BSO_TEXT_COLUMNS)
117
+ }
118
+ features_dict[DiscoEvalConstants.LABEL_NAME] = datasets.ClassLabel(names=DiscoEvalConstants.BSO_LABELS)
119
+ features = datasets.Features(features_dict)
120
+
121
+ elif self.config.name in [DiscoEvalConstants.DCCHAT, DiscoEvalConstants.DCWIKI]:
122
+ features_dict = {
123
+ DiscoEvalConstants.TEXT_COLUMN_NAME[i]: datasets.Value('string')
124
+ for i in range(DiscoEvalConstants.DC_TEXT_COLUMNS)
125
+ }
126
+ features_dict[DiscoEvalConstants.LABEL_NAME] = datasets.ClassLabel(names=DiscoEvalConstants.DC_LABELS)
127
+ features = datasets.Features(features_dict)
128
+
129
+ elif self.config.name in [DiscoEvalConstants.RST]:
130
+ features_dict = {
131
+ DiscoEvalConstants.TEXT_COLUMN_NAME[i]: [datasets.Value('string')]
132
+ for i in range(DiscoEvalConstants.RST_TEXT_COLUMNS)
133
+ }
134
+ features_dict[DiscoEvalConstants.LABEL_NAME] = datasets.ClassLabel(names=DiscoEvalConstants.RST_LABELS)
135
+ features = datasets.Features(features_dict)
136
+
137
+ elif self.config.name in [DiscoEvalConstants.PDTB_E]:
138
+ features_dict = {
139
+ DiscoEvalConstants.TEXT_COLUMN_NAME[i]: datasets.Value('string')
140
+ for i in range(DiscoEvalConstants.PDTB_E_TEXT_COLUMNS)
141
+ }
142
+ features_dict[DiscoEvalConstants.LABEL_NAME] = datasets.ClassLabel(names=DiscoEvalConstants.PDTB_E_LABELS)
143
+ features = datasets.Features(features_dict)
144
+
145
+ elif self.config.name in [DiscoEvalConstants.PDTB_I]:
146
+ features_dict = {
147
+ DiscoEvalConstants.TEXT_COLUMN_NAME[i]: datasets.Value('string')
148
+ for i in range(DiscoEvalConstants.PDTB_I_TEXT_COLUMNS)
149
+ }
150
+ features_dict[DiscoEvalConstants.LABEL_NAME] = datasets.ClassLabel(names=DiscoEvalConstants.PDTB_I_LABELS)
151
+ features = datasets.Features(features_dict)
152
+
153
+ elif self.config.name in [DiscoEvalConstants.SSPABS]:
154
+ features_dict = {
155
+ DiscoEvalConstants.TEXT_COLUMN_NAME[i]: datasets.Value('string')
156
+ for i in range(DiscoEvalConstants.SSPABS_TEXT_COLUMNS)
157
+ }
158
+ features_dict[DiscoEvalConstants.LABEL_NAME] = datasets.ClassLabel(names=DiscoEvalConstants.SSPABS_LABELS)
159
+ features = datasets.Features(features_dict)
160
+
161
+ return datasets.DatasetInfo(
162
+ description=_DESCRIPTION,
163
+ features=features,
164
+ homepage=_HOMEPAGE,
165
+ citation=_CITATION,
166
+ )
167
+
168
+ def _split_generators(self, dl_manager):
169
+ if self.config.name in [DiscoEvalConstants.SPARXIV, DiscoEvalConstants.SPROCSTORY, DiscoEvalConstants.SPWIKI]:
170
+ data_dir = DiscoEvalConstants.SP_DATA_DIR + "/" + DiscoEvalConstants.SP_DIRS[self.config.name]
171
+ train_name = DiscoEvalConstants.SP_TRAIN_NAME
172
+ valid_name = DiscoEvalConstants.SP_VALID_NAME
173
+ test_name = DiscoEvalConstants.SP_TEST_NAME
174
+
175
+ elif self.config.name in [DiscoEvalConstants.BSOARXIV, DiscoEvalConstants.BSOWIKI, DiscoEvalConstants.BSOROCSTORY]:
176
+ data_dir = DiscoEvalConstants.BSO_DATA_DIR + "/" + DiscoEvalConstants.BSO_DIRS[self.config.name]
177
+ train_name = DiscoEvalConstants.BSO_TRAIN_NAME
178
+ valid_name = DiscoEvalConstants.BSO_VALID_NAME
179
+ test_name = DiscoEvalConstants.BSO_TEST_NAME
180
+
181
+ elif self.config.name in [DiscoEvalConstants.DCCHAT, DiscoEvalConstants.DCWIKI]:
182
+ data_dir = DiscoEvalConstants.DC_DATA_DIR + "/" + DiscoEvalConstants.DC_DIRS[self.config.name]
183
+ train_name = DiscoEvalConstants.DC_TRAIN_NAME
184
+ valid_name = DiscoEvalConstants.DC_VALID_NAME
185
+ test_name = DiscoEvalConstants.DC_TEST_NAME
186
+
187
+ elif self.config.name in [DiscoEvalConstants.RST]:
188
+ data_dir = DiscoEvalConstants.RST_DATA_DIR
189
+ train_name = DiscoEvalConstants.RST_TRAIN_NAME
190
+ valid_name = DiscoEvalConstants.RST_VALID_NAME
191
+ test_name = DiscoEvalConstants.RST_TEST_NAME
192
+
193
+ elif self.config.name in [DiscoEvalConstants.PDTB_E, DiscoEvalConstants.PDTB_I]:
194
+ data_dir = os.path.join(DiscoEvalConstants.PDTB_DATA_DIR, DiscoEvalConstants.PDTB_DIRS[self.config.name])
195
+ train_name = DiscoEvalConstants.PDTB_TRAIN_NAME
196
+ valid_name = DiscoEvalConstants.PDTB_VALID_NAME
197
+ test_name = DiscoEvalConstants.PDTB_TEST_NAME
198
+
199
+ elif self.config.name in [DiscoEvalConstants.SSPABS]:
200
+ data_dir = DiscoEvalConstants.SSPABS_DATA_DIR
201
+ train_name = DiscoEvalConstants.SSPABS_TRAIN_NAME
202
+ valid_name = DiscoEvalConstants.SSPABS_VALID_NAME
203
+ test_name = DiscoEvalConstants.SSPABS_TEST_NAME
204
+
205
+ urls_to_download = {
206
+ "train": data_dir + "/" + train_name,
207
+ "valid": data_dir + "/" + valid_name,
208
+ "test": data_dir + "/" + test_name,
209
+ }
210
+ logger = logging.getLogger(__name__)
211
+ data_dirs = dl_manager.download_and_extract(urls_to_download)
212
+ logger.info(f"Data directories: {data_dirs}")
213
+ downloaded_files = dl_manager.download_and_extract(data_dirs)
214
+ logger.info(f"Downloading Completed")
215
+
216
+ return [
217
+ datasets.SplitGenerator(
218
+ name=datasets.Split.TRAIN,
219
+ gen_kwargs={
220
+ "filepath": downloaded_files['train'],
221
+ "split": "train",
222
+ },
223
+ ),
224
+ datasets.SplitGenerator(
225
+ name=datasets.Split.VALIDATION,
226
+ gen_kwargs={
227
+ "filepath": downloaded_files['valid'],
228
+ "split": "dev",
229
+ },
230
+ ),
231
+ datasets.SplitGenerator(
232
+ name=datasets.Split.TEST,
233
+ gen_kwargs={
234
+ "filepath": downloaded_files['test'],
235
+ "split": "test"
236
+ },
237
+ ),
238
+ ]
239
+
240
+ def _generate_examples(self, filepath, split):
241
+ logger = logging.getLogger(__name__)
242
+ logger.info(f"Current working dir: {os.getcwd()}")
243
+ logger.info("generating examples from = %s", filepath)
244
+ if self.config.name == DiscoEvalConstants.RST:
245
+ data = pickle.load(open(filepath, "rb"))
246
+ for key, line in enumerate(data):
247
+ example = {DiscoEvalConstants.TEXT_COLUMN_NAME[i]: sent for i, sent in enumerate(line[1:])}
248
+ example[DiscoEvalConstants.LABEL_NAME] = line[0]
249
+ yield key, example
250
+
251
+ else:
252
+ with io.open(filepath, mode='r', encoding='utf-8') as f:
253
+ for key, line in enumerate(f):
254
+ line = line.strip().split("\t")
255
+ example = {DiscoEvalConstants.TEXT_COLUMN_NAME[i]: sent for i, sent in enumerate(line[1:])}
256
+ example[DiscoEvalConstants.LABEL_NAME] = line[0]
257
+ yield key, example
DiscoEvalConstants.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # General Constants:
2
+ LABEL_NAME = 'label'
3
+ TEXT_COLUMN_NAME = [f"sentence_{i}" for i in range(1, 10)]
4
+
5
+ # SSPabs Constants:
6
+ SSPABS = 'SSPabs'
7
+ SSPABS_TRAIN_NAME = 'train.txt'
8
+ SSPABS_VALID_NAME = 'valid.txt'
9
+ SSPABS_TEST_NAME = 'test.txt'
10
+ SSPABS_DATA_DIR = 'data/SSP/abs'
11
+ SSPABS_LABELS = ["0", "1"]
12
+ SSPABS_TEXT_COLUMNS = 1
13
+
14
+ # PDTB Constants:
15
+ PDTB_I = 'PDTB-I'
16
+ PDTB_E = 'PDTB-E'
17
+ PDTB_TRAIN_NAME = 'train.txt'
18
+ PDTB_VALID_NAME = 'valid.txt'
19
+ PDTB_TEST_NAME = 'test.txt'
20
+ PDTB_DATA_DIR = 'data/PDTB'
21
+ PDTB_DIRS = {PDTB_E: 'Explicit', PDTB_I: 'Implicit'}
22
+ PDTB_E_LABELS = [
23
+ 'Comparison.Concession',
24
+ 'Comparison.Contrast',
25
+ 'Contingency.Cause',
26
+ 'Contingency.Condition',
27
+ 'Contingency.Pragmatic condition',
28
+ 'Expansion.Alternative',
29
+ 'Expansion.Conjunction',
30
+ 'Expansion.Instantiation',
31
+ 'Expansion.List',
32
+ 'Expansion.Restatement',
33
+ 'Temporal.Asynchronous',
34
+ 'Temporal.Synchrony',
35
+ ]
36
+ PDTB_I_LABELS = [
37
+ 'Comparison.Concession',
38
+ 'Comparison.Contrast',
39
+ 'Contingency.Cause',
40
+ 'Contingency.Pragmatic cause',
41
+ 'Expansion.Alternative',
42
+ 'Expansion.Conjunction',
43
+ 'Expansion.Instantiation',
44
+ 'Expansion.List',
45
+ 'Expansion.Restatement',
46
+ 'Temporal.Asynchronous',
47
+ 'Temporal.Synchrony',
48
+ ]
49
+ PDTB_E_TEXT_COLUMNS = 2
50
+ PDTB_I_TEXT_COLUMNS = 2
51
+
52
+
53
+ # SP Constants:
54
+ SPARXIV = 'SParxiv'
55
+ SPROCSTORY = 'SProcstory'
56
+ SPWIKI = 'SPwiki'
57
+ SP_TRAIN_NAME = 'train.txt'
58
+ SP_VALID_NAME = 'valid.txt'
59
+ SP_TEST_NAME = 'test.txt'
60
+ SP_DATA_DIR = 'data/SP'
61
+ SP_DIRS = {SPARXIV: 'arxiv', SPROCSTORY: 'rocstory', SPWIKI: 'wiki'}
62
+ SP_LABELS = ["0", "1", "2", "3", "4"]
63
+ SP_TEXT_COLUMNS = 5
64
+
65
+ # BSO Constants:
66
+ BSOARXIV = 'BSOarxiv'
67
+ BSOROCSTORY = 'BSOrocstory'
68
+ BSOWIKI = 'BSOwiki'
69
+ BSO_TRAIN_NAME = 'train.txt'
70
+ BSO_VALID_NAME = 'valid.txt'
71
+ BSO_TEST_NAME = 'test.txt'
72
+ BSO_DATA_DIR = 'data/BSO'
73
+ BSO_DIRS = {BSOARXIV: 'arxiv', BSOROCSTORY: 'rocstory', BSOWIKI: 'wiki'}
74
+ BSO_LABELS = ["0", "1"]
75
+ BSO_TEXT_COLUMNS = 2
76
+
77
+ # DC Constants:
78
+ DCCHAT = 'DCchat'
79
+ DCWIKI = 'DCwiki'
80
+ DC_TRAIN_NAME = 'train.txt'
81
+ DC_VALID_NAME = 'valid.txt'
82
+ DC_TEST_NAME = 'test.txt'
83
+ DC_DATA_DIR = 'data/DC'
84
+ DC_DIRS = {DCCHAT: 'chat', DCWIKI: 'wiki'}
85
+ DC_LABELS = ["0", "1"]
86
+ DC_TEXT_COLUMNS = 6
87
+
88
+
89
+ # RST Constants:
90
+ RST = 'RST'
91
+ RST_TRAIN_NAME = 'RST_TRAIN.pkl'
92
+ RST_VALID_NAME = 'RST_DEV.pkl'
93
+ RST_TEST_NAME = 'RST_TEST.pkl'
94
+ RST_DATA_DIR = 'data/RST'
95
+ RST_LABELS = [
96
+ 'NS-Explanation',
97
+ 'NS-Evaluation',
98
+ 'NN-Condition',
99
+ 'NS-Summary',
100
+ 'SN-Cause',
101
+ 'SN-Background',
102
+ 'NS-Background',
103
+ 'SN-Summary',
104
+ 'NS-Topic-Change',
105
+ 'NN-Explanation',
106
+ 'SN-Topic-Comment',
107
+ 'NS-Elaboration',
108
+ 'SN-Attribution',
109
+ 'SN-Manner-Means',
110
+ 'NN-Evaluation',
111
+ 'NS-Comparison',
112
+ 'NS-Contrast',
113
+ 'SN-Condition',
114
+ 'NS-Temporal',
115
+ 'NS-Enablement',
116
+ 'SN-Evaluation',
117
+ 'NN-Topic-Comment',
118
+ 'NN-Temporal',
119
+ 'NN-Textual-organization',
120
+ 'NN-Same-unit',
121
+ 'NN-Comparison',
122
+ 'NN-Topic-Change',
123
+ 'SN-Temporal',
124
+ 'NN-Joint',
125
+ 'SN-Enablement',
126
+ 'SN-Explanation',
127
+ 'NN-Contrast',
128
+ 'NN-Cause',
129
+ 'SN-Contrast',
130
+ 'NS-Attribution',
131
+ 'NS-Topic-Comment',
132
+ 'SN-Elaboration',
133
+ 'SN-Comparison',
134
+ 'NS-Cause',
135
+ 'NS-Condition',
136
+ 'NS-Manner-Means'
137
+ ]
138
+ RST_TEXT_COLUMNS = 2