Amanpreet Singh commited on
Commit
c8c670b
1 Parent(s): 14b4135

Add dynahate loading file

Browse files
Files changed (1) hide show
  1. dynahate.py +92 -0
dynahate.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 the HuggingFace Datasets Authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ # Lint as: python3
17
+ """Learning from the worst Hate Speech dataset."""
18
+
19
+
20
+ import csv
21
+
22
+ import datasets
23
+ from datasets.tasks import TextClassification
24
+
25
+
26
+ _DESCRIPTION = """\
27
+ We present a human-and-model-in-the-loop process for dynamically generating datasets and training better performing and more robust hate detection models. We provide a new dataset of ~40,000 entries, generated and labelled by trained annotators over four rounds of dynamic data creation. It includes ~15,000 challenging perturbations and each hateful entry has fine-grained labels for the type and target of hate. Hateful entries make up 54% of the dataset, which is substantially higher than comparable datasets. We show that model performance is substantially improved using this approach. Models trained on later rounds of data collection perform better on test sets and are harder for annotators to trick. They also perform better on HATECHECK, a suite of functional tests for online hate detection. See https://arxiv.org/abs/2012.15761 for more details.
28
+ """
29
+
30
+ _CITATION = """\
31
+ @inproceedings{vidgen2021learning,
32
+ title={Learning from the Worst: Dynamically Generated Datasets to Improve Online Hate Detection},
33
+ author={Vidgen, Bertie and Thrush, Tristan and Waseem, Zeerak and Kiela, Douwe},
34
+ booktitle={Proceedings of the 59th Annual Meeting of the Association for Computational Linguistics and the 11th International Joint Conference on Natural Language Processing (Volume 1: Long Papers)},
35
+ pages={1667--1682},
36
+ year={2021}
37
+ }
38
+ """
39
+
40
+ _DOWNLOAD_URL = "https://raw.githubusercontent.com/bvidgen/Dynamically-Generated-Hate-Speech-Dataset/main/Dynamically%20Generated%20Hate%20Dataset%20v{version}.csv"
41
+ _VERSIONS = ("0.2.2", "0.2.3")
42
+
43
+
44
+ class Dynahate(datasets.GeneratorBasedBuilder):
45
+ """Learning from the worst hate speech classification dataset."""
46
+ BUILDER_CONFIGS = [datasets.BuilderConfig(name=version, version=version) for version in _VERSIONS]
47
+ DEFAULT_CONFIG_NAME = "0.2.3"
48
+ def _info(self):
49
+ return datasets.DatasetInfo(
50
+ description=_DESCRIPTION,
51
+ features=datasets.Features(
52
+ {
53
+ "acl.id": datasets.Value("string"),
54
+ "label": datasets.features.ClassLabel(names=["hate", "nothate"]),
55
+ "text": datasets.Value("string"),
56
+ "X1": datasets.Value("int32"),
57
+ "type": datasets.Value("string"),
58
+ "target": datasets.Value("string"),
59
+ "level": datasets.Value("string"),
60
+ "split": datasets.Value("string"),
61
+ "round.base": datasets.Value("int32"),
62
+ "annotator": datasets.Value("string"),
63
+ "round": datasets.Value("string"),
64
+ "acl.id.matched": datasets.Value("string")
65
+ }
66
+ ),
67
+ homepage="https://arxiv.org/abs/2012.15761",
68
+ citation=_CITATION,
69
+ task_templates=[TextClassification(text_column="text", label_column="label")],
70
+ )
71
+
72
+ def _split_generators(self, dl_manager):
73
+ csv_path = dl_manager.download_and_extract(_DOWNLOAD_URL.format(version=self.config.name))
74
+ return [
75
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"csv_path": csv_path, "split": "train"}),
76
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"csv_path": csv_path, "split": "dev"}),
77
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"csv_path": csv_path, "split": "test"}),
78
+ ]
79
+
80
+ def _generate_examples(self, csv_path, split):
81
+ """Generate AG News examples."""
82
+ with open(csv_path, encoding="utf-8") as csv_file:
83
+ csv_reader = csv.reader(csv_file, delimiter=",")
84
+ for id_, row in enumerate(csv_reader):
85
+ if id_ == 0:
86
+ keys = row[:]
87
+ else:
88
+ res = dict([(k, v) for k, v in zip(keys, row) if k != ""])
89
+
90
+ for k in ["X1", "round.base"]:
91
+ res[k] = int(res[k])
92
+ yield id_ - 1, res