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3951475
b1ea4e2
3951475
 
 
 
 
 
 
 
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# 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.
"""Multi-Dimensional Gender Bias classification"""


import json

import datasets


# TODO: Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """\
@inproceedings{md_gender_bias,
  author    = {Emily Dinan and
               Angela Fan and
               Ledell Wu and
               Jason Weston and
               Douwe Kiela and
               Adina Williams},
  editor    = {Bonnie Webber and
               Trevor Cohn and
               Yulan He and
               Yang Liu},
  title     = {Multi-Dimensional Gender Bias Classification},
  booktitle = {Proceedings of the 2020 Conference on Empirical Methods in Natural
               Language Processing, {EMNLP} 2020, Online, November 16-20, 2020},
  pages     = {314--331},
  publisher = {Association for Computational Linguistics},
  year      = {2020},
  url       = {https://www.aclweb.org/anthology/2020.emnlp-main.23/}
}
"""

# TODO: Add description of the dataset here
# You can copy an official description
_DESCRIPTION = """\
Machine learning models are trained to find patterns in data.
NLP models can inadvertently learn socially undesirable patterns when training on gender biased text.
In this work, we propose a general framework that decomposes gender bias in text along several pragmatic and semantic dimensions:
bias from the gender of the person being spoken about, bias from the gender of the person being spoken to, and bias from the gender of the speaker.
Using this fine-grained framework, we automatically annotate eight large scale datasets with gender information.
In addition, we collect a novel, crowdsourced evaluation benchmark of utterance-level gender rewrites.
Distinguishing between gender bias along multiple dimensions is important, as it enables us to train finer-grained gender bias classifiers.
We show our classifiers prove valuable for a variety of important applications, such as controlling for gender bias in generative models,
detecting gender bias in arbitrary text, and shed light on offensive language in terms of genderedness.
"""

_HOMEPAGE = "https://parl.ai/projects/md_gender/"

_LICENSE = "MIT License"

_URL = "http://parl.ai/downloads/md_gender/gend_multiclass_10072020.tgz"

_CONF_FILES = {
    "funpedia": {
        "train": "funpedia/train.jsonl",
        "validation": "funpedia/valid.jsonl",
        "test": "funpedia/test.jsonl",
    },
    "image_chat": {
        "train": "image_chat/engaging_imagechat_gender_captions_hashed.test.jsonl",
        "validation": "image_chat/engaging_imagechat_gender_captions_hashed.train.jsonl",
        "test": "image_chat/engaging_imagechat_gender_captions_hashed.valid.jsonl",
    },
    "wizard": {
        "train": "wizard/train.jsonl",
        "validation": "wizard/valid.jsonl",
        "test": "wizard/test.jsonl",
    },
    "convai2_inferred": {
        "train": (
            "inferred_about/convai2_train_binary.txt",
            "inferred_about/convai2_train.txt",
        ),
        "validation": (
            "inferred_about/convai2_valid_binary.txt",
            "inferred_about/convai2_valid.txt",
        ),
        "test": (
            "inferred_about/convai2_test_binary.txt",
            "inferred_about/convai2_test.txt",
        ),
    },
    "light_inferred": {
        "train": (
            "inferred_about/light_train_binary.txt",
            "inferred_about/light_train.txt",
        ),
        "validation": (
            "inferred_about/light_valid_binary.txt",
            "inferred_about/light_valid.txt",
        ),
        "test": (
            "inferred_about/light_test_binary.txt",
            "inferred_about/light_test.txt",
        ),
    },
    "opensubtitles_inferred": {
        "train": (
            "inferred_about/opensubtitles_train_binary.txt",
            "inferred_about/opensubtitles_train.txt",
        ),
        "validation": (
            "inferred_about/opensubtitles_valid_binary.txt",
            "inferred_about/opensubtitles_valid.txt",
        ),
        "test": (
            "inferred_about/opensubtitles_test_binary.txt",
            "inferred_about/opensubtitles_test.txt",
        ),
    },
    "yelp_inferred": {
        "train": (
            "inferred_about/yelp_train_binary.txt",
            "",
        ),
        "validation": (
            "inferred_about/yelp_valid_binary.txt",
            "",
        ),
        "test": (
            "inferred_about/yelp_test_binary.txt",
            "",
        ),
    },
}


class MdGenderBias(datasets.GeneratorBasedBuilder):
    """Multi-Dimensional Gender Bias classification"""

    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="gendered_words",
            version=VERSION,
            description="A list of common nouns with a masculine and feminine variant.",
        ),
        datasets.BuilderConfig(
            name="name_genders",
            version=VERSION,
            description="A list of first names with their gender attribution by year in the US.",
        ),
        datasets.BuilderConfig(
            name="new_data", version=VERSION, description="Some data reformulated and annotated along all three axes."
        ),
        datasets.BuilderConfig(
            name="funpedia",
            version=VERSION,
            description="Data from Funpedia with ABOUT annotations based on Funpedia information on an entity's gender.",
        ),
        datasets.BuilderConfig(
            name="image_chat",
            version=VERSION,
            description="Data from ImageChat with ABOUT annotations based on image recognition.",
        ),
        datasets.BuilderConfig(
            name="wizard",
            version=VERSION,
            description="Data from WizardsOfWikipedia with ABOUT annotations based on Wikipedia information on an entity's gender.",
        ),
        datasets.BuilderConfig(
            name="convai2_inferred",
            version=VERSION,
            description="Data from the ConvAI2 challenge with ABOUT annotations inferred by a trined classifier.",
        ),
        datasets.BuilderConfig(
            name="light_inferred",
            version=VERSION,
            description="Data from LIGHT with ABOUT annotations inferred by a trined classifier.",
        ),
        datasets.BuilderConfig(
            name="opensubtitles_inferred",
            version=VERSION,
            description="Data from OpenSubtitles with ABOUT annotations inferred by a trined classifier.",
        ),
        datasets.BuilderConfig(
            name="yelp_inferred",
            version=VERSION,
            description="Data from Yelp reviews with ABOUT annotations inferred by a trined classifier.",
        ),
    ]

    DEFAULT_CONFIG_NAME = (
        "new_data"  # It's not mandatory to have a default configuration. Just use one if it make sense.
    )

    def _info(self):
        # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
        if (
            self.config.name == "gendered_words"
        ):  # This is the name of the configuration selected in BUILDER_CONFIGS above
            features = datasets.Features(
                {
                    "word_masculine": datasets.Value("string"),
                    "word_feminine": datasets.Value("string"),
                }
            )
        elif self.config.name == "name_genders":
            features = datasets.Features(
                {
                    "name": datasets.Value("string"),
                    "assigned_gender": datasets.ClassLabel(names=["M", "F"]),
                    "count": datasets.Value("int32"),
                }
            )
        elif self.config.name == "new_data":
            features = datasets.Features(
                {
                    "text": datasets.Value("string"),
                    "original": datasets.Value("string"),
                    "labels": [
                        datasets.ClassLabel(
                            names=[
                                "ABOUT:female",
                                "ABOUT:male",
                                "PARTNER:female",
                                "PARTNER:male",
                                "SELF:female",
                                "SELF:male",
                            ]
                        )
                    ],
                    "class_type": datasets.ClassLabel(names=["about", "partner", "self"]),
                    "turker_gender": datasets.ClassLabel(
                        names=["man", "woman", "nonbinary", "prefer not to say", "no answer"]
                    ),
                    "episode_done": datasets.Value("bool_"),
                    "confidence": datasets.Value("string"),
                }
            )
        elif self.config.name == "funpedia":
            features = datasets.Features(
                {
                    "text": datasets.Value("string"),
                    "title": datasets.Value("string"),
                    "persona": datasets.Value("string"),
                    "gender": datasets.ClassLabel(names=["gender-neutral", "female", "male"]),
                }
            )
        elif self.config.name == "image_chat":
            features = datasets.Features(
                {
                    "caption": datasets.Value("string"),
                    "id": datasets.Value("string"),
                    "male": datasets.Value("bool_"),
                    "female": datasets.Value("bool_"),
                }
            )
        elif self.config.name == "wizard":
            features = datasets.Features(
                {
                    "text": datasets.Value("string"),
                    "chosen_topic": datasets.Value("string"),
                    "gender": datasets.ClassLabel(names=["gender-neutral", "female", "male"]),
                }
            )
        elif self.config.name == "yelp_inferred":
            features = datasets.Features(
                {
                    "text": datasets.Value("string"),
                    "binary_label": datasets.ClassLabel(names=["ABOUT:female", "ABOUT:male"]),
                    "binary_score": datasets.Value("float"),
                }
            )
        else:  # data with inferred labels
            features = datasets.Features(
                {
                    "text": datasets.Value("string"),
                    "binary_label": datasets.ClassLabel(names=["ABOUT:female", "ABOUT:male"]),
                    "binary_score": datasets.Value("float"),
                    "ternary_label": datasets.ClassLabel(names=["ABOUT:female", "ABOUT:male", "ABOUT:gender-neutral"]),
                    "ternary_score": datasets.Value("float"),
                }
            )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,  # Here we define them above because they are different between the two configurations
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        archive = dl_manager.download(_URL)
        data_dir = "data_to_release"
        if self.config.name == "gendered_words":
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={
                        "filepath": None,
                        "filepath_pair": (
                            data_dir + "/" + "word_list/male_word_file.txt",
                            data_dir + "/" + "word_list/female_word_file.txt",
                        ),
                        "files": dl_manager.iter_archive(archive),
                    },
                )
            ]
        elif self.config.name == "name_genders":
            return [
                datasets.SplitGenerator(
                    name=f"yob{yob}",
                    gen_kwargs={
                        "filepath": data_dir + "/" + f"names/yob{yob}.txt",
                        "filepath_pair": None,
                        "files": dl_manager.iter_archive(archive),
                    },
                )
                for yob in range(1880, 2019)
            ]
        elif self.config.name == "new_data":
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={
                        "filepath": data_dir + "/" + "new_data/data.jsonl",
                        "filepath_pair": None,
                        "files": dl_manager.iter_archive(archive),
                    },
                )
            ]
        elif self.config.name in ["funpedia", "image_chat", "wizard"]:
            return [
                datasets.SplitGenerator(
                    name=spl,
                    gen_kwargs={
                        "filepath": data_dir + "/" + fname,
                        "filepath_pair": None,
                        "files": dl_manager.iter_archive(archive),
                    },
                )
                for spl, fname in _CONF_FILES[self.config.name].items()
            ]
        else:
            return [
                datasets.SplitGenerator(
                    name=spl,
                    gen_kwargs={
                        "filepath": None,
                        "filepath_pair": (
                            data_dir + "/" + fname_1,
                            data_dir + "/" + fname_2,
                        ),
                        "files": dl_manager.iter_archive(archive),
                    },
                )
                for spl, (fname_1, fname_2) in _CONF_FILES[self.config.name].items()
            ]

    def _generate_examples(self, filepath, filepath_pair, files):
        if self.config.name == "gendered_words":
            male_data, female_data = None, None
            for path, f in files:
                if path == filepath_pair[0]:
                    male_data = f.read().decode("utf-8").splitlines()
                elif path == filepath_pair[1]:
                    female_data = f.read().decode("utf-8").splitlines()
                if male_data is not None and female_data is not None:
                    for id_, (l_m, l_f) in enumerate(zip(male_data, female_data)):
                        yield id_, {
                            "word_masculine": l_m.strip(),
                            "word_feminine": l_f.strip(),
                        }
                    break
        elif self.config.name == "name_genders":
            for path, f in files:
                if path == filepath:
                    for id_, line in enumerate(f):
                        name, g, ct = line.decode("utf-8").strip().split(",")
                        yield id_, {
                            "name": name,
                            "assigned_gender": g,
                            "count": int(ct),
                        }
                    break
        elif "_inferred" in self.config.name:
            if "yelp" in self.config.name:
                for path, f in files:
                    if path == filepath_pair[0]:
                        for id_, line_b in enumerate(f):
                            text_b, label_b, score_b = line_b.decode("utf-8").split("\t")
                            yield id_, {
                                "text": text_b,
                                "binary_label": label_b,
                                "binary_score": float(score_b.strip()),
                            }
                        break
            else:
                binary_data, ternary_data = None, None
                for path, f in files:
                    if path == filepath_pair[0]:
                        binary_data = f.read().decode("utf-8").splitlines()
                    elif path == filepath_pair[1]:
                        ternary_data = f.read().decode("utf-8").splitlines()
                    if binary_data is not None and ternary_data is not None:
                        for id_, (line_b, line_t) in enumerate(zip(binary_data, ternary_data)):
                            text_b, label_b, score_b = line_b.split("\t")
                            text_t, label_t, score_t = line_t.split("\t")
                            yield id_, {
                                "text": text_b,
                                "binary_label": label_b,
                                "binary_score": float(score_b.strip()),
                                "ternary_label": label_t,
                                "ternary_score": float(score_t.strip()),
                            }
                        break
        else:
            for path, f in files:
                if path == filepath:
                    for id_, line in enumerate(f):
                        example = json.loads(line.decode("utf-8").strip())
                        if "turker_gender" in example and example["turker_gender"] is None:
                            example["turker_gender"] = "no answer"
                        yield id_, example
                    break