File size: 9,603 Bytes
91a19aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import dataclasses
import datasets
import json

logger = datasets.logging.get_logger(__name__)

_CITATION = """\
@inproceedings{mueller-etal-2022-coloring,
    title = "Coloring the Blank Slate: Pre-training Imparts a Hierarchical Inductive Bias to Sequence-to-sequence Models",
    author = "Mueller, Aaron  and
      Frank, Robert  and
      Linzen, Tal  and
      Wang, Luheng  and
      Schuster, Sebastian",
    booktitle = "Findings of the Association for Computational Linguistics: ACL 2022",
    month = may,
    year = "2022",
    address = "Dublin, Ireland",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2022.findings-acl.106",
    doi = "10.18653/v1/2022.findings-acl.106",
    pages = "1352--1368",
}
"""

_DESCRIPTION = """\
This is the dataset used for Coloring the Blank Slate: 
Pre-training Imparts a Hierarchical Inductive Bias to
Sequence-to-sequence Models.
"""

class SyntacticTransformationsConfig(datasets.BuilderConfig):
    def __init__(self, description, features, data_url, citation, url, **kwargs):
        super(SyntacticTransformationsConfig, self).__init__(version=datasets.Version("1.18.3"), **kwargs)
        self.description = description
        self.text_features = features
        self.citation = citation
        self.data_url = data_url
        self.url = url
    


class SyntacticTransformations(datasets.GeneratorBasedBuilder):
    standard_features = datasets.Features(
        {
            "src": datasets.Value("string"),
            "tgt": datasets.Value("string"),
            "prefix": datasets.Value("string")
        }
    )
    
    BUILDER_CONFIGS = [
        SyntacticTransformationsConfig(
            name="passiv-en-nps",
            description="English passivization transformations.",
            features=standard_features,
            data_url="https://raw.githubusercontent.com/sebschu/multilingual-transformations/main/data/passiv_en_nps/",
            url="https://github.com/sebschu/multilingual-transformations/",
            citation=_CITATION
        ),
        SyntacticTransformationsConfig(
            name="passiv-de-nps",
            description="German passivization transformations.",
            features=standard_features,
            data_url="https://raw.githubusercontent.com/sebschu/multilingual-transformations/main/data/passiv_de_nps/",
            url="https://github.com/sebschu/multilingual-transformations/",
            citation=_CITATION
        ),
        SyntacticTransformationsConfig(
            name="question-en",
            description="English question formation transformations.",
            features=standard_features,
            data_url="https://raw.githubusercontent.com/sebschu/multilingual-transformations/main/data/question_have-havent_en/",
            url="https://github.com/sebschu/multilingual-transformations/",
            citation=_CITATION
        ),
        SyntacticTransformationsConfig(
            name="question-de",
            description="German question formation transformations.",
            features=standard_features,
            data_url="https://raw.githubusercontent.com/sebschu/multilingual-transformations/main/data/question_have-can_withquest_de/",
            url="https://github.com/sebschu/multilingual-transformations/",
            citation=_CITATION
        ),
        SyntacticTransformationsConfig(
            name="passiv-en_de-nps",
            description="Zero-shot English-to-German passivization transformations.",
            features=standard_features,
            data_url="https://raw.githubusercontent.com/sebschu/multilingual-transformations/main/data/passiv_en-de_nps/",
            url="https://github.com/sebschu/multilingual-transformations/",
            citation=_CITATION
        ),
        SyntacticTransformationsConfig(
            name="question-en_de",
            description="Zero-shot English-to-German question formation transformations.",
            features=standard_features,
            data_url="https://raw.githubusercontent.com/sebschu/multilingual-transformations/main/data/question_have-can_de/",
            url="https://github.com/sebschu/multilingual-transformations/",
            citation=_CITATION
        )
    ]
    
    def _split_generators(self, dl_manager):
        if self.config.name == "passiv-en-nps":
            template = "passiv_en_nps.{}.json"
            _URLS = {
                "train": self.config.data_url + template.format("train"),
                "dev": self.config.data_url + template.format("dev"),
                "test": self.config.data_url + template.format("test"),
                "gen": self.config.data_url + template.format("gen"),
            }
        elif self.config.name == "passiv-de-nps":
            template = "passiv_de_nps.{}.json"
            _URLS = {
                "train": self.config.data_url + template.format("train"),
                "dev": self.config.data_url + template.format("dev"),
                "test": self.config.data_url + template.format("test"),
                "gen": self.config.data_url + template.format("gen"),
            }
        elif self.config.name == "question-en":
            template = "question_have.{}.json"
            _URLS = {
                "train": self.config.data_url + template.format("train"),
                "dev": self.config.data_url + template.format("dev"),
                "test": self.config.data_url + template.format("test"),
                "gen": self.config.data_url + template.format("gen"),
            }
        elif self.config.name == "question-de":
            template = "question_have_can.de.{}.json"
            _URLS = {
                "train": self.config.data_url + template.format("train"),
                "dev": self.config.data_url + template.format("dev"),
                "test": self.config.data_url + template.format("test"),
                "gen": self.config.data_url + template.format("gen"),
            }
        elif self.config.name == "question-en_de":
            template = "question_have_can.de.{}.json"
            _URLS = {
                "train": self.config.data_url + "question_have_can.en-de.train.json",
                "dev": self.config.data_url + template.format("dev"),
                "test": self.config.data_url + template.format("test"),
                "gen_rc_s": self.config.data_url + template.format("gen_rc_s"),
                "gen_rc_o": self.config.data_url + template.format("gen_rc_o"),
            }
        elif self.config.name == "passiv-en_de-nps":
            template = "passiv_de_nps.{}.json"
            _URLS = {
                "train": self.config.data_url + "passiv_en-de_nps.train.json",
                "dev": self.config.data_url + template.format("dev"),
                "test": self.config.data_url + template.format("test"),
                "gen_pp_s": self.config.data_url + template.format("gen_pp_s"),
                "gen_pp_o": self.config.data_url + template.format("gen_pp_o"),
            }

        
        data_files = dl_manager.download(_URLS)

        if "en_de" not in self.config.name:
            return [
                datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_files["train"]}),
                datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": data_files["dev"]}),
                datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": data_files["test"]}),
                datasets.SplitGenerator(name=datasets.NamedSplit('generalization'), gen_kwargs={"filepath": data_files["gen"]}),
            ]
        else:
            gen_s = "gen_pp_s" if "passiv" in self.config.name else "gen_rc_s"
            gen_o = "gen_pp_o" if "passiv" in self.config.name else "gen_rc_o"
            return [
                datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_files["train"]}),
                datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": data_files["dev"]}),
                datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": data_files["test"]}),
                datasets.SplitGenerator(name=datasets.NamedSplit('generalization_s'), gen_kwargs={"filepath": data_files[gen_s]}),
                datasets.SplitGenerator(name=datasets.NamedSplit('generalization_o'), gen_kwargs={"filepath": data_files[gen_o]}),
            ]

    def _info(self):
        features = {text_feature: datasets.Value("string") for text_feature in self.config.text_features.keys()}
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(features),
            homepage=self.config.url,
            citation=_CITATION,
        )
    
    def _generate_examples(self, filepath):
        """This function returns the examples in the raw (text) form."""
        logger.info("generating examples from = %s", filepath)
        with open(filepath) as f:
            id_ = 0
            for line in f:
                example = json.loads(line)
                src = example["translation"]["src"]
                tgt = example["translation"]["tgt"]
                prefix = example["translation"]["prefix"]

                # Features currently used are "context", "question", and "answers".
                # Others are extracted here for the ease of future expansions.
                yield id_, {
                    "src": src,
                    "tgt": tgt,
                    "prefix": prefix,
                }
                id_ += 1