5roop commited on
Commit
d332207
1 Parent(s): 533dc59

Upload FRENK-hate-sl.py

Browse files
Files changed (1) hide show
  1. FRENK-hate-sl.py +143 -0
FRENK-hate-sl.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 Slovene.
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-sl/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
+ class FRENKHateSpeechSL(datasets.GeneratorBasedBuilder):
61
+ """The FRENK Datasets of Socially Unacceptable Discourse in Slovene"""
62
+
63
+ VERSION = datasets.Version("0.0.0")
64
+
65
+ BUILDER_CONFIGS = [
66
+ datasets.BuilderConfig(name="binary", version=VERSION,
67
+ description="Labels are either 'Offensive' or 'Acceptable'."),
68
+ datasets.BuilderConfig(name="multiclass", version=VERSION,
69
+ description="Labels are 'Acceptable speech', 'Other offensive', 'Background offensive', 'Inappropriate', 'Other violence', 'Background violence'"),
70
+ ]
71
+
72
+ DEFAULT_CONFIG_NAME = "binary"
73
+
74
+ def _info(self):
75
+ feature_dict = {
76
+ "text": datasets.Value("string"),
77
+ "target": datasets.Value("string"),
78
+ "topic": datasets.Value("string"),
79
+ }
80
+ if self.config.name == "binary":
81
+ features = datasets.Features(
82
+ {
83
+ **feature_dict,
84
+ "label": datasets.ClassLabel(names=["Acceptable", "Offensive"]),
85
+ }
86
+ )
87
+ else:
88
+ features = datasets.Features(
89
+ {
90
+ **feature_dict,
91
+ "label": datasets.ClassLabel(names=['Acceptable speech', 'Other offensive', 'Background offensive', 'Inappropriate', 'Other violence', 'Background violence']),
92
+ }
93
+ )
94
+ return datasets.DatasetInfo(
95
+ description=_DESCRIPTION,
96
+ features=features,
97
+ supervised_keys=None,
98
+ homepage=_HOMEPAGE,
99
+ license=_LICENSE,
100
+
101
+ citation=_CITATION,
102
+ )
103
+
104
+
105
+ def _split_generators(self, dl_manager):
106
+ """Returns SplitGenerators."""
107
+
108
+ data_file = dl_manager.download_and_extract(_URL)
109
+ return [
110
+ datasets.SplitGenerator(
111
+ name=datasets.Split.TRAIN, gen_kwargs={
112
+ 'filepath': os.path.join(data_file, "train.tsv"),
113
+ }
114
+ ),
115
+ datasets.SplitGenerator(
116
+ name=datasets.Split.VALIDATION, gen_kwargs={
117
+ 'filepath': os.path.join(data_file, "dev.tsv"),
118
+ }
119
+ ),
120
+ datasets.SplitGenerator(
121
+ name=datasets.Split.TEST, gen_kwargs={
122
+ 'filepath': os.path.join(data_file, "test.tsv"),
123
+ }
124
+ ),
125
+ ]
126
+
127
+ def _generate_examples(self, filepath):
128
+ """Yields examples."""
129
+
130
+ with open(filepath, encoding="utf-8") as f:
131
+ reader = csv.reader(f, delimiter="\t")
132
+ for id_, row in enumerate(reader):
133
+ if id_ == 0:
134
+ continue
135
+ to_return_dict = {
136
+ "text": row[1],
137
+ "target": row[4] ,
138
+ "topic": row[5]
139
+ }
140
+ yield id_, {
141
+ **to_return_dict,
142
+ **{"label": _CLASS_MAP_BINARY[row[3]] if self.config.name == "binary" else _CLASS_MAP_MULTICLASS[row[2]]}
143
+ }