File size: 5,938 Bytes
f8b8d03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# coding=utf-8
# Copyright 2020 HuggingFace Datasets Authors.
#
# 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.

# Lint as: python3
import datasets


_CITATION = """\
@InProceedings{WikiAtomicEdits,
  title = {{WikiAtomicEdits: A Multilingual Corpus of Wikipedia Edits for Modeling Language and Discourse}},
  author = {Faruqui, Manaal and Pavlick, Ellie and Tenney, Ian and Das, Dipanjan},
  booktitle = {Proc. of EMNLP},
  year = {2018}
}
"""

_DESCRIPTION = """\
A dataset of atomic wikipedia edits containing insertions and deletions of a contiguous chunk of text in a sentence. This dataset contains ~43 million edits across 8 languages.

An atomic edit is defined as an edit e applied to a natural language expression S as the insertion, deletion, or substitution of a sub-expression P such that both the original expression S and the resulting expression e(S) are well-formed semantic constituents (MacCartney, 2009). In this corpus, we release such atomic insertions and deletions made to sentences in wikipedia.
"""

_HOMEPAGE = "https://github.com/google-research-datasets/wiki-atomic-edits"
_VERSION = "1.0.0"

_URL = {
    "german": {
        "insertions": "https://storage.googleapis.com/wiki-atomic-edits/german/insertions.tsv.gz",
        "deletions": "https://storage.googleapis.com/wiki-atomic-edits/german/deletions.tsv.gz",
    },
    "english": {
        "insertions": "https://storage.googleapis.com/wiki-atomic-edits/english/insertions.tsv.gz",
        "deletions": "https://storage.googleapis.com/wiki-atomic-edits/english/deletions.tsv.gz",
    },
    "spanish": {
        "insertions": "https://storage.googleapis.com/wiki-atomic-edits/spanish/insertions.tsv.gz",
        "deletions": "https://storage.googleapis.com/wiki-atomic-edits/spanish/deletions.tsv.gz",
    },
    "french": {
        "insertions": "https://storage.googleapis.com/wiki-atomic-edits/french/insertions.tsv.gz",
        "deletions": "https://storage.googleapis.com/wiki-atomic-edits/french/deletions.tsv.gz",
    },
    "italian": {
        "insertions": "https://storage.googleapis.com/wiki-atomic-edits/italian/insertions.tsv.gz",
        "deletions": "https://storage.googleapis.com/wiki-atomic-edits/italian/deletions.tsv.gz",
    },
    "japanese": {
        "insertions": "https://storage.googleapis.com/wiki-atomic-edits/japanese/insertions.tsv.gz",
        "deletions": "https://storage.googleapis.com/wiki-atomic-edits/japanese/deletions.tsv.gz",
    },
    "russian": {
        "insertions": "https://storage.googleapis.com/wiki-atomic-edits/russian/insertions.tsv.gz",
        "deletions": "https://storage.googleapis.com/wiki-atomic-edits/russian/deletions.tsv.gz",
    },
    "chinese": {
        "insertions": "https://storage.googleapis.com/wiki-atomic-edits/chinese/insertions.tsv.gz",
        "deletions": "https://storage.googleapis.com/wiki-atomic-edits/chinese/deletions.tsv.gz",
    },
}

_LANG_EDITS = [
    ("german", "insertions"),
    ("german", "deletions"),
    ("english", "insertions"),
    ("english", "deletions"),
    ("spanish", "insertions"),
    ("spanish", "deletions"),
    ("french", "insertions"),
    ("french", "deletions"),
    ("italian", "insertions"),
    ("italian", "deletions"),
    ("japanese", "insertions"),
    ("japanese", "deletions"),
    ("russian", "insertions"),
    ("russian", "deletions"),
    ("chinese", "insertions"),
    ("chinese", "deletions"),
]


class WikiAtomicEditsConfig(datasets.BuilderConfig):
    def __init__(self, *args, language=None, edit_type=None, **kwargs):
        super().__init__(*args, **kwargs)
        self.language = language
        self.edit_type = edit_type


class WikiAtomicEdits(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [
        WikiAtomicEditsConfig(
            name=f"{lang}_{edit_type}",
            language=lang,
            edit_type=edit_type,
            description=f"Language: {lang}, Edit type: {edit_type}",
            version=datasets.Version(_VERSION),
        )
        for lang, edit_type in _LANG_EDITS
    ]
    BUILDER_CONFIG_CLASS = WikiAtomicEditsConfig

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "id": datasets.Value("int32"),
                    "base_sentence": datasets.Value("string"),
                    "phrase": datasets.Value("string"),
                    "edited_sentence": datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        datapath = dl_manager.download_and_extract(_URL[self.config.language][self.config.edit_type])

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"datapath": datapath},
            )
        ]

    def _generate_examples(self, datapath):
        with open(datapath, "r", encoding="utf-8") as data_file:
            next(data_file)
            for sentence_counter, sent in enumerate(data_file):
                sent = sent.split("\t")
                res = {
                    "id": sentence_counter,
                    "base_sentence": sent[0],
                    "phrase": sent[1],
                    "edited_sentence": sent[2],
                }
                yield sentence_counter, res