File size: 8,673 Bytes
2e8d854
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79a0451
2e8d854
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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.
""" Wino-X is a parallel dataset of German, French, and Russian Winograd schemas, aligned with their English
counterparts, used to examine whether neural machine translation models can perform coreference resolution that
requires commonsense knowledge, and whether multilingual language models are capable of commonsense reasoning across
multiple languages. """

import csv
import json
import os

import datasets

_CITATION = """\
@inproceedings{Emelin2021WinoXMW,
  title={Wino-X: Multilingual Winograd Schemas for Commonsense Reasoning and Coreference Resolution},
  author={Denis Emelin and Rico Sennrich},
  booktitle={EMNLP},
  year={2021}
}
"""

# You can copy an official description
_DESCRIPTION = """\
Wino-X is a parallel dataset of German, French, and Russian Winograd schemas, aligned with their English 
counterparts, used to examine whether neural machine translation models can perform coreference resolution that 
requires commonsense knowledge and whether multilingual language models are capable of commonsense reasoning across 
multiple languages.
"""

_HOMEPAGE = "https://github.com/demelin/Wino-X"

_LICENSE = "MIT"

# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
_URLS = {
    "mt_en_de": "https://huggingface.co/datasets/demelin/wino_x/resolve/main/data/mt/en_de_test.jsonl",
    "mt_en_fr": "https://huggingface.co/datasets/demelin/wino_x/resolve/main/data/mt/en_fr_test.jsonl",
    "mt_en_ru": "https://huggingface.co/datasets/demelin/wino_x/resolve/main/data/mt/en_ru_test.jsonl",
    "lm_en_de": "https://huggingface.co/datasets/demelin/wino_x/resolve/main/data/lm/en_de_test.jsonl",
    "lm_en_fr": "https://huggingface.co/datasets/demelin/wino_x/resolve/main/data/lm/en_fr_test.jsonl",
    "lm_en_ru": "https://huggingface.co/datasets/demelin/wino_x/resolve/main/data/lm/en_ru_test.jsonl"
}


class WinoX(datasets.GeneratorBasedBuilder):
    """ Wino-X is a dataset of German, French, and Russian Winograd schemas, aligned with their English counterparts """

    VERSION = datasets.Version("1.1.0")
    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="mt_en_de", version=VERSION,
                               description="This is the EN-DE part of the Wino-X translation data."),
        datasets.BuilderConfig(name="mt_en_fr", version=VERSION,
                               description="This is the EN-FR part of the Wino-X translation data."),
        datasets.BuilderConfig(name="mt_en_ru", version=VERSION,
                               description="This is the EN-RU part of the Wino-X translation data."),
        datasets.BuilderConfig(name="lm_en_de", version=VERSION,
                               description="This is the EN-DE part of the Wino-X language modeling data."),
        datasets.BuilderConfig(name="lm_en_fr", version=VERSION,
                               description="This is the EN-FR part of the Wino-X language modeling data."),
        datasets.BuilderConfig(name="lm_en_ru", version=VERSION,
                               description="This is the EN-RU part of the Wino-X language modeling data."),
    ]

    def _info(self):

        # MT example:
        # {"qID": "3UDTAB6HH8D37OQL3O6F3GXEEOF09Z-1",
        # "sentence": "The woman looked for a different vase for the bouquet because it was too small.",
        # "translation1": "Die Frau suchte nach einer anderen Vase für den Blumenstrauß, weil sie zu klein war.",
        # "translation2": "Die Frau suchte nach einer anderen Vase für den Blumenstrauß, weil er zu klein war.",
        # "answer": 1,
        # "pronoun1": "sie",
        # "pronoun2": "er",
        # "referent1_en": "vase",
        # "referent2_en": "bouquet",
        # "true_translation_referent_of_pronoun1_de": "Vase",
        # "true_translation_referent_of_pronoun2_de": "Blumenstrauß",
        # "false_translation_referent_of_pronoun1_de": "Vase",
        # "false_translation_referent_of_pronoun2_de": "Blumenstrauß"}

        tgt_lang = self.config.name.split('_')[-1]
        if self.config.name.startswith('mt_'):
            features = datasets.Features(
                {
                    "qID": datasets.Value("string"),
                    "sentence": datasets.Value("string"),
                    "translation1": datasets.Value("string"),
                    "translation2": datasets.Value("string"),
                    "answer": datasets.Value("int64"),
                    "pronoun1": datasets.Value("string"),
                    "pronoun2": datasets.Value("string"),
                    "referent1_en": datasets.Value("string"),
                    "referent2_en": datasets.Value("string"),
                    "true_translation_referent_of_pronoun1_{}".format(tgt_lang): datasets.Value("string"),
                    "true_translation_referent_of_pronoun2_{}".format(tgt_lang): datasets.Value("string"),
                    "false_translation_referent_of_pronoun1_{}".format(tgt_lang): datasets.Value("string"),
                    "false_translation_referent_of_pronoun2_{}".format(tgt_lang): datasets.Value("string")
                }
            )

        # LM example:
        # {"qID": "3UDTAB6HH8D37OQL3O6F3GXEEOF09Z-1",
        # "sentence": "The woman looked for a different vase for the bouquet because it was too small.",
        # "context_en": "The woman looked for a different vase for the bouquet because _ was too small.",
        # "context_de": "Die Frau suchte nach einer anderen Vase für den Blumenstrauß, weil _ zu klein war.",
        # "option1_en": "the vase",
        # "option2_en": "the bouquet",
        # "option1_de": "die Vase",
        # "option2_de": "der Blumenstrauß",
        # "answer": 1,
        # "context_referent_of_option1_de": "Vase",
        # "context_referent_of_option2_de": "Blumenstrauß"}

        else:
            features = datasets.Features(
                {
                    "qID": datasets.Value("string"),
                    "sentence": datasets.Value("string"),
                    "context_en": datasets.Value("string"),
                    "context_{}".format(tgt_lang): datasets.Value("string"),
                    "answer": datasets.Value("int64"),
                    "option1_en": datasets.Value("string"),
                    "option2_en": datasets.Value("string"),
                    "option1_{}".format(tgt_lang): datasets.Value("string"),
                    "option2_{}".format(tgt_lang): datasets.Value("string"),
                    "context_referent_of_option1_{}".format(tgt_lang): datasets.Value("string"),
                    "context_referent_of_option2_{}".format(tgt_lang): datasets.Value("string")
                }
            )

        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features=features,
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE,
            # License for the dataset if available
            license=_LICENSE,
            # Citation for the dataset
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        downloaded_files = dl_manager.download_and_extract(_URLS[self.config.name])
        return [datasets.SplitGenerator(name=datasets.Split.TEST,
                                        gen_kwargs={'filepath': downloaded_files, 'split': 'test'})]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, filepath, split):
        # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
        with open(filepath, encoding="utf-8") as f:
            for key, row in enumerate(f):
                data = json.loads(row)
                yield key, data