File size: 5,929 Bytes
f644b6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
495974f
 
 
 
f644b6c
495974f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f644b6c
 
 
 
 
 
495974f
f644b6c
 
 
a9c4360
f644b6c
a9c4360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f644b6c
 
a9c4360
f644b6c
 
 
 
 
 
 
c495158
 
59bb1ae
f644b6c
 
 
 
 
 
 
 
 
a9c4360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff2a474
a9c4360
 
 
 
 
 
 
 
 
 
 
f644b6c
 
 
 
a9c4360
 
 
 
 
 
f644b6c
 
495974f
f644b6c
495974f
 
 
f644b6c
 
 
91c63b4
c495158
 
59bb1ae
f644b6c
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
# 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.
"""BiSECT is a Split and Rephrase corpus created via bilingual pivoting."""

import os
import datasets


_CITATION = """\
@inproceedings{kim-etal-2021-bisect,
    title = "{B}i{SECT}: Learning to Split and Rephrase Sentences with Bitexts",
    author = "Kim, Joongwon  and
      Maddela, Mounica  and
      Kriz, Reno  and
      Xu, Wei  and
      Callison-Burch, Chris",
    booktitle = "Proceedings of the 2021 Conference on Empirical Methods in Natural Language Processing",
    month = nov,
    year = "2021",
    address = "Online and Punta Cana, Dominican Republic",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2021.emnlp-main.500",
    pages = "6193--6209"
}
"""

_DESCRIPTION = """\
BiSECT is a Split and Rephrase corpus created via bilingual pivoting.
"""

_HOMEPAGE = "https://github.com/mounicam/BiSECT"

_URL = "https://raw.githubusercontent.com/mounicam/BiSECT/main/"
_URL_MAIN = _URL + "bisect/"
_URL_CHALLENGE = _URL + "bisect_challenge/"

_URLs = {
    datasets.Split.TRAIN: {
        "src": _URL_MAIN + "train.src.gz",
        "dst": _URL_MAIN + "train.dst.gz",
    },
    datasets.Split.VALIDATION: {
        "src": _URL_MAIN + "valid.src.gz",
        "dst": _URL_MAIN + "valid.dst.gz",
    },
    datasets.Split.TEST: {
        "src": _URL_MAIN + "test.src.gz",
        "dst": _URL_MAIN + "test.dst.gz",
    },
    "challenge_hsplit": {
        "src": "https://raw.githubusercontent.com/cocoxu/simplification/master/data/turkcorpus/test.8turkers.tok.norm",
        "dst": "https://raw.githubusercontent.com/eliorsulem/HSplit-corpus/master/HSplit/HSplit2_full",
    },
    "challenge_bisect": {
        "src": _URL_CHALLENGE + "challenge_test.src",
        "dst": _URL_CHALLENGE + "challenge_test.dst",
    },
}


class BiSECT(datasets.GeneratorBasedBuilder):
    """The BiSECT Split and Rephrase corpus."""

    VERSION = datasets.Version("1.1.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="en",
            version=VERSION,
            description="English data described in the BiSECT paper",
        ),
        datasets.BuilderConfig(
            name="de",
            version=VERSION,
            description="German data described in the BiSECT paper",
        ),
        datasets.BuilderConfig(
            name="es",
            version=VERSION,
            description="Spanish data described in the BiSECT paper",
        ),
        datasets.BuilderConfig(
            name="fr",
            version=VERSION,
            description="French data described in the BiSECT paper",
        ),
    ]

    DEFAULT_CONFIG_NAME = "en"

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "gem_id": datasets.Value("string"),
                    "source": datasets.Value("string"),
                    "target": datasets.Value("string"),
                    "references": [datasets.Value("string")],
                }
            ),
            supervised_keys=None,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        if self.config.name == "en":
            data_dir = dl_manager.download_and_extract(_URLs)
            return [
                datasets.SplitGenerator(
                    name=split_name,
                    gen_kwargs={"filepath": data_dir[split_name], "split": split_name},
                )
                for split_name in data_dir
            ]
        else:
            lang = self.config.name
            url = _URL + "bisect_multilingual/" + f"bisect_{lang}.gz"
            data_dir = dl_manager.download_and_extract(url)

            return [
                datasets.SplitGenerator(
                    name=f"{split}",
                    gen_kwargs={
                        "filepath": os.path.join(data_dir, lang),
                        "split": split,
                    },
                )
                for split in (
                    datasets.Split.TRAIN,
                    datasets.Split.VALIDATION,
                    datasets.Split.TEST,
                )
            ]

    def _generate_examples(self, filepath, split):
        """Yields examples as (key, example) tuples."""

        if self.config.name == "en":
            source_filepath = filepath["src"]
            target_filepath = filepath["dst"]
        else:
            source_filepath = os.path.join(filepath, f"{split}.complex")
            target_filepath = os.path.join(filepath, f"{split}.simple")

        with open(source_filepath, encoding="utf-8") as f:
            source_lines = [line.strip() for line in f if line.strip()]
        with open(target_filepath, encoding="utf-8") as f:
            target_lines = [
                line.strip().replace(" <SEP>", "") for line in f if line.strip()
            ]

        for id_ in range(len(source_lines)):
            yield id_, {
                "gem_id": f"BiSECT_{self.config.name}-{split}-{id_}",
                "source": source_lines[id_],
                "target": target_lines[id_],
                "references": [target_lines[id_]],
            }