Datasets:

Modalities:
Text
ArXiv:
Libraries:
Datasets
License:
File size: 4,505 Bytes
f65642d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5602cf0
 
f65642d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# coding=utf-8

# Lint as: python3
"""XWinograd"""


import json

import pandas as pd

import datasets


logger = datasets.logging.get_logger(__name__)


_CITATION = """\
@misc{tikhonov2021heads,
    title={It's All in the Heads: Using Attention Heads as a Baseline for Cross-Lingual Transfer in Commonsense Reasoning},
    author={Alexey Tikhonov and Max Ryabinin},
    year={2021},
    eprint={2106.12066},
    archivePrefix={arXiv},
    primaryClass={cs.CL}
}
"""

_DESCRIPTION = """\
A multilingual collection of Winograd Schemas in six languages \
that can be used for evaluation of cross-lingual commonsense reasoning capabilities.
"""


#_URL = "https://github.com/yandex-research/crosslingual_winograd/blob/main/dataset.tsv"
#_URL = "https://huggingface.co/datasets/muennighoff/xwinograd/resolve/main/data/xwinograd.tsv"
_URL = "https://huggingface.co/datasets/Muennighoff/xwinograd/blob/main/data/xwinograd.tsv"

import json
import random

def winogrande_format(row):
    array = row["pronoun"]
    position_idx = json.loads(array)[1][0]
    # Turn unicode into proper chinese characters
    sent = str(u"{}".format(row["sent"]))
    start_idx = 0
    for i, tok in enumerate(json.loads(row["toks"])):
        tok = str(u"{}".format(tok))
        cur_start_idx = sent.find(tok)
        if i == position_idx:
            break
        sent = sent[cur_start_idx + len(tok):]
        start_idx += cur_start_idx + len(tok)
    # +1 to give room for an optional space
    row["sentence"] = row["sent"][:start_idx] + row["sent"][start_idx:start_idx+len(tok)+1].replace(tok, "_") + row["sent"][start_idx+len(tok)+1:]

    sol = json.loads(row["solution"])

    cor_answer_idx = random.choice([1, 2])
    incor_answer_idx = 2 if cor_answer_idx == 1 else 1

    cor_answer = str(u"{}".format(sol[0][0])) if sol[0][-1] == True else str(u"{}".format(sol[1][0]))
    incor_answer = str(u"{}".format(sol[0][0])) if sol[0][-1] == False else str(u"{}".format(sol[1][0]))

    row[f"option{cor_answer_idx}"] = cor_answer
    row[f"option{incor_answer_idx}"] = incor_answer
    row["answer"] = cor_answer_idx
    return row


class XWinograd(datasets.GeneratorBasedBuilder):
    """XWinograd"""

    VERSION = datasets.Version("1.0.0")
    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="en",
            version=VERSION,
            description="X",
        ),
        datasets.BuilderConfig(
            name="fr",
            version=VERSION,
            description="X",
        ),
        datasets.BuilderConfig(
            name="jp",
            version=VERSION,
            description="X",
        ),
        datasets.BuilderConfig(
            name="pt",
            version=VERSION,
            description="X",
        ),
        datasets.BuilderConfig(
            name="ru",
            version=VERSION,
            description="X",
        ),
        datasets.BuilderConfig(
            name="zh",
            version=VERSION,
            description="X",
        ),
    ]
    DEFAULT_CONFIG_NAME = "en"

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "sentence": datasets.Value("string"),
                    "option1": datasets.Value("string"),
                    "option2": datasets.Value("string"),
                    "answer": datasets.Value("string")
                }
            ),
            supervised_keys=None,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        downloaded_files = dl_manager.download_and_extract(_URL)
        return [
            datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files}),
        ]

    def _generate_examples(self, filepath):
        """This function returns the examples in the raw (text) form."""
        logger.info("generating examples from = %s", filepath)
        ds = pd.read_csv(
            filepath, sep='\t', header=None, 
            names=["lang", "type", "original", "sent", "toks", "pronoun", "solution"]
        )
        if self.config.name:
            ds = ds[ds["lang"] == self.config.name]
        ds = ds.apply(winogrande_format, axis=1)

        for idx, row in ds.iterrows():
            yield idx, {
                "sentence": row["sentence"],
                "option1": row["option1"],
                "option2": row["option2"],
                "answer": row["answer"],
            }