holylovenia commited on
Commit
d62e0af
1 Parent(s): 374bc8e

Upload id_multilabel_hs.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. id_multilabel_hs.py +154 -0
id_multilabel_hs.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from typing import Dict, List, Tuple
3
+
4
+ import datasets
5
+ import pandas as pd
6
+
7
+ from nusacrowd.utils import schemas
8
+ from nusacrowd.utils.configs import NusantaraConfig
9
+ from nusacrowd.utils.constants import Tasks
10
+
11
+ _CITATION = """\
12
+ @inproceedings{ibrohim-budi-2019-multi,
13
+ title = "Multi-label Hate Speech and Abusive Language Detection in {I}ndonesian {T}witter",
14
+ author = "Ibrohim, Muhammad Okky and
15
+ Budi, Indra",
16
+ booktitle = "Proceedings of the Third Workshop on Abusive Language Online",
17
+ month = aug,
18
+ year = "2019",
19
+ address = "Florence, Italy",
20
+ publisher = "Association for Computational Linguistics",
21
+ url = "https://aclanthology.org/W19-3506",
22
+ doi = "10.18653/v1/W19-3506",
23
+ pages = "46--57",
24
+ }
25
+ """
26
+
27
+ _LOCAL = False
28
+ _LANGUAGES = ["ind"] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
29
+ _DATASETNAME = "id_multilabel_hs"
30
+
31
+ _DESCRIPTION = """\
32
+ The ID_MULTILABEL_HS dataset is collection of 13,169 tweets in Indonesian language,
33
+ designed for hate speech detection NLP task. This dataset is combination from previous research and newly crawled data from Twitter.
34
+ This is a multilabel dataset with label details as follows:
35
+ -HS : hate speech label;
36
+ -Abusive : abusive language label;
37
+ -HS_Individual : hate speech targeted to an individual;
38
+ -HS_Group : hate speech targeted to a group;
39
+ -HS_Religion : hate speech related to religion/creed;
40
+ -HS_Race : hate speech related to race/ethnicity;
41
+ -HS_Physical : hate speech related to physical/disability;
42
+ -HS_Gender : hate speech related to gender/sexual orientation;
43
+ -HS_Gender : hate related to other invective/slander;
44
+ -HS_Weak : weak hate speech;
45
+ -HS_Moderate : moderate hate speech;
46
+ -HS_Strong : strong hate speech.
47
+ """
48
+
49
+ _HOMEPAGE = "https://aclanthology.org/W19-3506/"
50
+ _LICENSE = "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International"
51
+ _URLS = {
52
+ _DATASETNAME: "https://raw.githubusercontent.com/okkyibrohim/id-multi-label-hate-speech-and-abusive-language-detection/master/re_dataset.csv",
53
+ }
54
+ _SUPPORTED_TASKS = [Tasks.ASPECT_BASED_SENTIMENT_ANALYSIS]
55
+ _SOURCE_VERSION = "1.0.0"
56
+ _NUSANTARA_VERSION = "1.0.0"
57
+
58
+
59
+ class IdAbusive(datasets.GeneratorBasedBuilder):
60
+ """The ID_MULTILABEL_HS dataset is multi-label hate speech and abusive language detection in Indonesian tweets"""
61
+
62
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
63
+ NUSANTARA_VERSION = datasets.Version(_NUSANTARA_VERSION)
64
+
65
+ BUILDER_CONFIGS = [
66
+ NusantaraConfig(
67
+ name="id_multilabel_hs_source",
68
+ version=SOURCE_VERSION,
69
+ description="ID Multilabel HS source schema",
70
+ schema="source",
71
+ subset_id="id_multilabel_hs",
72
+ ),
73
+ NusantaraConfig(
74
+ name="id_multilabel_hs_nusantara_text_multi",
75
+ version=NUSANTARA_VERSION,
76
+ description="ID Multilabel HS Nusantara schema",
77
+ schema="nusantara_text_multi",
78
+ subset_id="id_multilabel_hs",
79
+ ),
80
+ ]
81
+
82
+ DEFAULT_CONFIG_NAME = "id_multilabel_hs_source"
83
+
84
+ def _info(self) -> datasets.DatasetInfo:
85
+ if self.config.schema == "source":
86
+ features = datasets.Features({
87
+ "tweet": datasets.Value("string"),
88
+ "HS": datasets.Value("bool"),
89
+ "Abusive": datasets.Value("bool"),
90
+ "HS_Individual": datasets.Value("bool"),
91
+ "HS_Group": datasets.Value("bool"),
92
+ "HS_Religion": datasets.Value("bool"),
93
+ "HS_Race": datasets.Value("bool"),
94
+ "HS_Physical": datasets.Value("bool"),
95
+ "HS_Gender": datasets.Value("bool"),
96
+ "HS_Other": datasets.Value("bool"),
97
+ "HS_Weak": datasets.Value("bool"),
98
+ "HS_Moderate": datasets.Value("bool"),
99
+ "HS_Strong": datasets.Value("bool"),
100
+ })
101
+ elif self.config.schema == "nusantara_text_multi":
102
+ features = schemas.text_multi_features([0, 1])
103
+
104
+ return datasets.DatasetInfo(
105
+ description=_DESCRIPTION,
106
+ features=features,
107
+ homepage=_HOMEPAGE,
108
+ license=_LICENSE,
109
+ citation=_CITATION,
110
+ )
111
+
112
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
113
+ """Returns SplitGenerators."""
114
+ # Dataset does not have predetermined split, putting all as TRAIN
115
+ urls = _URLS[_DATASETNAME]
116
+ base_dir = Path(dl_manager.download_and_extract(urls))
117
+ data_files = {"train": base_dir}
118
+
119
+ return [
120
+ datasets.SplitGenerator(
121
+ name=datasets.Split.TRAIN,
122
+ gen_kwargs={
123
+ "filepath": data_files["train"],
124
+ "split": "train",
125
+ },
126
+ ),
127
+ ]
128
+
129
+ def _generate_examples(self, filepath: Path, split: str) -> Tuple[int, Dict]:
130
+ """Yields examples as (key, example) tuples."""
131
+ # Dataset does not have id, using row index as id
132
+ label_cols = ["HS", "Abusive", "HS_Individual", "HS_Group", "HS_Religion", "HS_Race", "HS_Physical", "HS_Gender", "HS_Other", "HS_Weak", "HS_Moderate", "HS_Strong"]
133
+ df = pd.read_csv(filepath, encoding="ISO-8859-1").reset_index()
134
+ df.columns = ["id", "tweet"] + label_cols
135
+
136
+ if self.config.schema == "source":
137
+ for row in df.itertuples():
138
+ ex = {
139
+ "tweet": row.tweet,
140
+ }
141
+ for label in label_cols:
142
+ ex[label] = getattr(row, label)
143
+ yield row.id, ex
144
+
145
+ elif self.config.schema == "nusantara_text_multi":
146
+ for row in df.itertuples():
147
+ ex = {
148
+ "id": str(row.id),
149
+ "text": row.tweet,
150
+ "labels": [label for label in row[3:]],
151
+ }
152
+ yield row.id, ex
153
+ else:
154
+ raise ValueError(f"Invalid config: {self.config.name}")