Datasets:
Upload FRENK-hate-en.py
Browse files- FRENK-hate-en.py +144 -0
FRENK-hate-en.py
ADDED
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
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 |
+
"""An annotated dataset for classifying offensive or acceptable speech."""
|
16 |
+
|
17 |
+
import os
|
18 |
+
import csv
|
19 |
+
|
20 |
+
import datasets
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
_CITATION = """\
|
25 |
+
@misc{ljubešić2019frenk,
|
26 |
+
title={The FRENK Datasets of Socially Unacceptable Discourse in Slovene and English},
|
27 |
+
author={Nikola Ljubešić and Darja Fišer and Tomaž Erjavec},
|
28 |
+
year={2019},
|
29 |
+
eprint={1906.02045},
|
30 |
+
archivePrefix={arXiv},
|
31 |
+
primaryClass={cs.CL},
|
32 |
+
url={https://arxiv.org/abs/1906.02045}
|
33 |
+
}
|
34 |
+
"""
|
35 |
+
|
36 |
+
_DESCRIPTION = """\
|
37 |
+
The FRENK Datasets of Socially Unacceptable Discourse in English.
|
38 |
+
"""
|
39 |
+
|
40 |
+
_HOMEPAGE = "https://www.clarin.si/repository/xmlui/handle/11356/1433"
|
41 |
+
|
42 |
+
_LICENSE = "CLARIN.SI Licence ACA ID-BY-NC-INF-NORED 1.0"
|
43 |
+
|
44 |
+
_URL = "https://huggingface.co/datasets/5roop/FRENK-hate-en/resolve/main/data.zip"
|
45 |
+
|
46 |
+
_CLASS_MAP_MULTICLASS = {
|
47 |
+
'Acceptable speech': 0,
|
48 |
+
'Inappropriate': 1,
|
49 |
+
'Background offensive': 2,
|
50 |
+
'Other offensive': 3,
|
51 |
+
'Background violence': 4,
|
52 |
+
'Other violence': 5,
|
53 |
+
}
|
54 |
+
|
55 |
+
_CLASS_MAP_BINARY = {
|
56 |
+
'Acceptable': 0,
|
57 |
+
'Offensive': 1,
|
58 |
+
}
|
59 |
+
|
60 |
+
|
61 |
+
class FRENKHateSpeechEN(datasets.GeneratorBasedBuilder):
|
62 |
+
"""The FRENK Datasets of Socially Unacceptable Discourse in Slovene and English."""
|
63 |
+
|
64 |
+
VERSION = datasets.Version("0.0.0")
|
65 |
+
|
66 |
+
BUILDER_CONFIGS = [
|
67 |
+
datasets.BuilderConfig(name="binary", version=VERSION,
|
68 |
+
description="Labels are either 'Offensive' or 'Acceptable'."),
|
69 |
+
datasets.BuilderConfig(name="multiclass", version=VERSION,
|
70 |
+
description="Labels are 'Acceptable speech', 'Other offensive', 'Background offensive', 'Inappropriate', 'Other violence', 'Background violence'"),
|
71 |
+
]
|
72 |
+
|
73 |
+
DEFAULT_CONFIG_NAME = "binary"
|
74 |
+
|
75 |
+
def _info(self):
|
76 |
+
feature_dict = {
|
77 |
+
"text": datasets.Value("string"),
|
78 |
+
"target": datasets.Value("string"),
|
79 |
+
"topic": datasets.Value("string"),
|
80 |
+
}
|
81 |
+
if self.config.name == "binary":
|
82 |
+
features = datasets.Features(
|
83 |
+
{
|
84 |
+
**feature_dict,
|
85 |
+
"label": datasets.ClassLabel(names=["Acceptable", "Offensive"]),
|
86 |
+
}
|
87 |
+
)
|
88 |
+
else:
|
89 |
+
features = datasets.Features(
|
90 |
+
{
|
91 |
+
**feature_dict,
|
92 |
+
"label": datasets.ClassLabel(names=['Acceptable speech', 'Other offensive', 'Background offensive', 'Inappropriate', 'Other violence', 'Background violence']),
|
93 |
+
}
|
94 |
+
)
|
95 |
+
return datasets.DatasetInfo(
|
96 |
+
description=_DESCRIPTION,
|
97 |
+
features=features,
|
98 |
+
supervised_keys=None,
|
99 |
+
homepage=_HOMEPAGE,
|
100 |
+
license=_LICENSE,
|
101 |
+
|
102 |
+
citation=_CITATION,
|
103 |
+
)
|
104 |
+
|
105 |
+
|
106 |
+
def _split_generators(self, dl_manager):
|
107 |
+
"""Returns SplitGenerators."""
|
108 |
+
|
109 |
+
data_file = dl_manager.download_and_extract(_URL)
|
110 |
+
return [
|
111 |
+
datasets.SplitGenerator(
|
112 |
+
name=datasets.Split.TRAIN, gen_kwargs={
|
113 |
+
'filepath': os.path.join(data_file, "train.tsv"),
|
114 |
+
}
|
115 |
+
),
|
116 |
+
datasets.SplitGenerator(
|
117 |
+
name=datasets.Split.VALIDATION, gen_kwargs={
|
118 |
+
'filepath': os.path.join(data_file, "dev.tsv"),
|
119 |
+
}
|
120 |
+
),
|
121 |
+
datasets.SplitGenerator(
|
122 |
+
name=datasets.Split.TEST, gen_kwargs={
|
123 |
+
'filepath': os.path.join(data_file, "test.tsv"),
|
124 |
+
}
|
125 |
+
),
|
126 |
+
]
|
127 |
+
|
128 |
+
def _generate_examples(self, filepath):
|
129 |
+
"""Yields examples."""
|
130 |
+
|
131 |
+
with open(filepath, encoding="utf-8") as f:
|
132 |
+
reader = csv.reader(f, delimiter="\t")
|
133 |
+
for id_, row in enumerate(reader):
|
134 |
+
if id_ == 0:
|
135 |
+
continue
|
136 |
+
to_return_dict = {
|
137 |
+
"text": row[1],
|
138 |
+
"target": row[4] ,
|
139 |
+
"topic": row[5]
|
140 |
+
}
|
141 |
+
yield id_, {
|
142 |
+
**to_return_dict,
|
143 |
+
**{"label": _CLASS_MAP_BINARY[row[3]] if self.config.name == "binary" else _CLASS_MAP_MULTICLASS[row[2]]}
|
144 |
+
}
|