File size: 10,079 Bytes
94c28d3
c7edabd
 
 
 
 
8f25adc
500ac49
8f25adc
c7edabd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94c28d3
c7edabd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94c28d3
 
 
c7edabd
 
 
 
 
 
 
 
94c28d3
 
 
 
 
 
 
 
 
c7edabd
 
 
 
 
 
 
 
94c28d3
 
c7edabd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94c28d3
 
 
 
 
 
 
 
 
c7edabd
 
 
 
 
 
 
 
 
94c28d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7edabd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94c28d3
 
071b155
3ce1aad
c7edabd
 
 
3ce1aad
94c28d3
 
c7edabd
 
 
8f25adc
 
 
c7edabd
 
 
 
 
 
 
 
 
 
 
 
 
 
071b155
 
94c28d3
c7edabd
 
7f191b3
 
 
c7edabd
 
 
7f191b3
c7edabd
 
 
 
 
 
 
4909b02
8f25adc
c7edabd
 
4909b02
c7edabd
 
 
 
 
 
 
 
 
66e2146
 
 
c7edabd
 
 
 
 
4909b02
7f191b3
c7edabd
 
 
 
 
 
 
 
071b155
c7edabd
 
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
# basic-sentence-transforms.py: the HF datasets "loading script" for the internal NC_PAT dataset (defines configurations/tasks, columns, etc.)
import os
import json
import datasets
from datasets import Split, SplitGenerator

# relative path was not working here, so switched to absolute path
DATASET_URL = "https://huggingface.co/datasets/rfernand/basic_sentence_transforms/resolve/main"

no_extra = {
    "source": datasets.Value("string"),
    "target": datasets.Value("string"),
}

samp_class = {
    "source": datasets.Value("string"),
    "target": datasets.Value("string"),
    "class": datasets.Value("string"),
}

count_class = {
    "source": datasets.Value("string"),
    "target": datasets.Value("string"),
    "count": datasets.Value("string"),
    "class": datasets.Value("string"),
}

dir_only = {
    "source": datasets.Value("string"),
    "target": datasets.Value("string"),
    "direction": datasets.Value("string"),
}

warmup_configs = [
    {"name": "car_cdr_cons",
    "desc": "small phrase translation tasks that require only: CAR, CDR, or CAR+CDR+CONS operations",
    "features": samp_class},

    {"name": "car_cdr_cons_tuc",
    "desc": "same task as car_cdr_cons, but requires mapping lowercase fillers to their uppercase tokens",
    "features": samp_class},

    {"name": "car_cdr_rcons",
    "desc": "same task as car_cdr_cons, but the CONS samples have their left/right children swapped",
    "features": samp_class},

    {"name": "car_cdr_rcons_tuc",
    "desc": "same task as car_cdr_rcons, but requires mapping lowercase fillers to their uppercase tokens",
    "features": samp_class},

    {"name": "car_cdr_seq",
    "desc": "each samples requires 1-4 combinations of CAR and CDR, as identified by the root filler token",
    "features": count_class},

    {"name": "car_cdr_seq_40k",
    "desc": "same task as car_cdr_seq, but train samples increased from 10K to 40K",
    "features": count_class},

    {"name": "car_cdr_seq_tuc",
    "desc": "same task as car_cdr_seq, but requires mapping lowercase fillers to their uppercase tokens",
    "features": count_class},

    {"name": "car_cdr_seq_40k_tuc",
    "desc": "same task as car_cdr_seq_tuc, but train samples increased from 10K to 40K",
    "features": count_class},

    {"name": "car_cdr_seq_path",
    "desc": "similiar to car_cdr_seq, but each needed operation in represented as a node in the left child of the root",
    "features": count_class},

    {"name": "car_cdr_seq_path_40k",
    "desc": "same task as car_cdr_seq_path, but train samples increased from 10K to 40K",
    "features": count_class},

    {"name": "car_cdr_seq_path_40k_tuc",
    "desc": "same task as car_cdr_seq_path_40k, but requires mapping lowercase fillers to their uppercase tokens",
    "features": count_class},

    {"name": "car_cdr_seq_path_tuc",
    "desc": "same task as car_cdr_seq_path, but requires mapping lowercase fillers to their uppercase tokens",
    "features": count_class},
]

core_configs = [

    {"name": "active_active_stb",
    "desc": "active sentence translation, from sentence to parenthesized tree form, both directions",
    "features": dir_only},
    
    {"name": "active_active_stb_40k",
    "desc": "same task as active_active_stb, but train samples increased from 10K to 40K",
    "features": dir_only},

    
    {"name": "active_logical_ssb",
    "desc": "active to logical sentence translation, in both directions",
    "features": dir_only},
    
    {"name": "active_logical_ssb_40k",
    "desc": "same task as active_logical_ssb, but train samples increased from 10K to 40K",
    "features": dir_only},
    
    {"name": "active_logical_ttb",
    "desc": "active to logical tree translation, in both directions",
    "features": dir_only},
    
    {"name": "active_logical_ttb_40k",
    "desc": "same task as active_logical_ttb, but train samples increased from 10K to 40K",
    "features": dir_only},


    {"name": "active_passive_ssb",
    "desc": "active to passive sentence translation, in both directions",
    "features": dir_only},
    
    {"name": "active_passive_ssb_40k",
    "desc": "same task as active_passive_ssb, but train samples increased from 10K to 40K",
    "features": dir_only},
    
    {"name": "active_passive_ttb",
    "desc": "active to passive tree translation, in both directions",
    "features": dir_only},
    
    {"name": "active_passive_ttb_40k",
    "desc": "same task as active_passive_ttb, but train samples increased from 10K to 40K",
    "features": dir_only},

    
    {"name": "actpass_logical_ss",
    "desc": "mixture of active to logical and passive to logical sentence translations, single direction",
    "features": no_extra},
    
    {"name": "actpass_logical_ss_40k",
    "desc": "same task as actpass_logical_ss, but train samples increased from 10K to 40K",
    "features": no_extra},
    
    {"name": "actpass_logical_tt",
    "desc": "mixture of active to logical and passive to logical tree translations, single direction",
    "features": no_extra},
    
    {"name": "actpass_logical_tt_40k",
    "desc": "same task as actpass_logical_tt, but train samples increased from 10K to 40K",
    "features": no_extra},
    
    {"name": "logical_logical_stb",
    "desc": "logical form sentence translation, from sentence to parenthesized tree form, both directions",
    "features": dir_only},
    
    {"name": "alogical_logical_stb_40k",
    "desc": "same task as logical_logical_stb, but train samples increased from 10K to 40K",
    "features": dir_only},

    
    {"name": "passive_logical_ssb",
    "desc": "passive to logical sentence translation, in both directions",
    "features": dir_only},
    
    {"name": "passive_logical_ssb_40k",
    "desc": "same task as passive_logical_ssb, but train samples increased from 10K to 40K",
    "features": dir_only},

    {"name": "passive_logical_ttb",
    "desc": "passive to logical tree translation, in both directions",
    "features": dir_only},
    
    {"name": "passive_logical_ttb_40k",
    "desc": "same task as passive_logical_ttb, but train samples increased from 10K to 40K",
    "features": dir_only},
    
    {"name": "passive_passive_stb",
    "desc": "passive sentence translation, from sentence to parenthesized tree form, both directions",
    "features": dir_only},
    
    {"name": "passive_passive_stb_40k",
    "desc": "same task as passive_passive_stb, but train samples increased from 10K to 40K",
    "features": dir_only},
]

configs = warmup_configs + core_configs

class BasicSentenceTransformsConfig(datasets.BuilderConfig):
    """BuilderConfig for basic_sentence_transforms dataset."""

    def __init__(self, features=None, **kwargs):
        # Version history:
        # 0.0.18: Initial version released to HF datasets
        # 0.0.21: release V21 of NC_PAT dataset 
        super().__init__(version=datasets.Version("0.0.21"), **kwargs)

        self.features = features
        self.label_classes = None
        #self.data_url = "./{}.zip".format(kwargs["name"])
        self.data_url ="{}/{}.zip".format(DATASET_URL, kwargs["name"])

        self.citation = None
        self.homepage = None

    def _info(self):
        return datasets.DatasetInfo(
            description=self.description,
            features=self.features,
            # No default supervised_keys (as we have to pass both question
            # and context as input).
            supervised_keys=None,
            homepage=self.homepage,
            citation=self.citation,
        )

class BasicSentenceTransforms(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [BasicSentenceTransformsConfig(name=c["name"], description=c["desc"], features=c["features"]) for c in configs]
    VERSION = datasets.Version("0.0.21")

    def _info(self):
        # features are now required here, so get them from the current CONFIG (following code example from super_glue.py)
        features = {feature: datasets.Value("string") for feature in self.config.features}

        return datasets.DatasetInfo(
            description="The dataset consists of diagnostic/warm-up tasks and core tasks within this dataset." + 
                "The core tasks represent the translation of English sentences between the active, passive, and logical forms.",
            features=datasets.Features(features),
            supervised_keys=None,
            homepage=None,
            citation=None,
        )

    def _split_generators(self, dl_manager: datasets.DownloadManager):
        url = self.config.data_url
        #print("_split_generator: url={}".format(url))

        dl_dir = dl_manager.download_and_extract(url)
        task = self.config_id
        #print("task: {}, dl_dir: {}".format(task, dl_dir))

        splits = [
            SplitGenerator(name=Split.TRAIN, gen_kwargs={"data_file": os.path.join(dl_dir, "train.jsonl")}),
            SplitGenerator(name=Split.VALIDATION, gen_kwargs={"data_file": os.path.join(dl_dir, "dev.jsonl")}),
            SplitGenerator(name=Split.TEST, gen_kwargs={"data_file": os.path.join(dl_dir, "test.jsonl")}),
        ]

        if not task.startswith("car_cdr_cons") and not task.startswith("car_cdr_rcons"):
            splits += [
                SplitGenerator(name="ood_new", gen_kwargs={"data_file": os.path.join(dl_dir, "ood_new.jsonl")}),
                SplitGenerator(name="ood_long", gen_kwargs={"data_file": os.path.join(dl_dir, "ood_long.jsonl")}),
                SplitGenerator(name="ood_all", gen_kwargs={"data_file": os.path.join(dl_dir, "ood_all.jsonl")}),
            ]

        return splits

    def _generate_examples(self, data_file):
        #print("_generate_examples: data_file: {}".format(data_file))

        with open(data_file, encoding="utf-8") as f:
            for i, line in enumerate(f):
                key = str(i)
                row = json.loads(line)    
                yield key, row  

if __name__ == "__main__":
    # short test 
    builder = BasicSentenceTransforms.BUILDER_CONFIGS[0]
    print("name: {}, desc: {}".format(builder.name, builder.description))