holylovenia commited on
Commit
b539158
·
verified ·
1 Parent(s): 82df2cc

Upload netifier.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. netifier.py +130 -0
netifier.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from typing import Dict, List, Tuple
3
+
4
+ import datasets
5
+ import pandas as pd
6
+
7
+ from seacrowd.utils import schemas
8
+ from seacrowd.utils.configs import SEACrowdConfig
9
+ from seacrowd.utils.constants import Tasks
10
+
11
+ _CITATION = """\
12
+ """
13
+
14
+ _LANGUAGES = ["ind"] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
15
+ _LOCAL = False
16
+
17
+ _DATASETNAME = "netifier"
18
+
19
+ _DESCRIPTION = """\
20
+ Netifier dataset is a collection of scraped posts on famous social media sites in Indonesia,
21
+ such as Instagram, Twitter, and Kaskus aimed to do multi-label toxicity classification.
22
+ The dataset consists of 7,773 texts. The author manually labelled ~7k samples into 4 categories:
23
+ pornography, hate speech, racism, and radicalism.
24
+ """
25
+
26
+ _HOMEPAGE = "https://github.com/ahmadizzan/netifier"
27
+ _LICENSE = "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International"
28
+ _URLS = {_DATASETNAME: {"train": "https://raw.githubusercontent.com/ahmadizzan/netifier/master/data/processed/train.csv", "test": "https://raw.githubusercontent.com/ahmadizzan/netifier/master/data/processed/test.csv"}}
29
+ _SUPPORTED_TASKS = [Tasks.ASPECT_BASED_SENTIMENT_ANALYSIS]
30
+ _SOURCE_VERSION = "1.0.0"
31
+ _SEACROWD_VERSION = "2024.06.20"
32
+
33
+
34
+ class Netifier(datasets.GeneratorBasedBuilder):
35
+ """Netifier dataset is a collection of scraped posts on famous social media sites in Indonesia,
36
+ such as Instagram, Twitter, and Kaskus aimed to do multi-label toxicity classification."""
37
+
38
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
39
+ SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
40
+
41
+ BUILDER_CONFIGS = [
42
+ SEACrowdConfig(
43
+ name="netifier_source",
44
+ version=SOURCE_VERSION,
45
+ description="Netifier source schema",
46
+ schema="source",
47
+ subset_id="netifier",
48
+ ),
49
+ SEACrowdConfig(
50
+ name="netifier_seacrowd_text_multi",
51
+ version=SEACROWD_VERSION,
52
+ description="Netifier Nusantara schema",
53
+ schema="seacrowd_text_multi",
54
+ subset_id="netifier",
55
+ ),
56
+ ]
57
+
58
+ DEFAULT_CONFIG_NAME = "netifier_source"
59
+
60
+ def _info(self) -> datasets.DatasetInfo:
61
+ if self.config.schema == "source":
62
+ features = datasets.Features(
63
+ {
64
+ "text": datasets.Value("string"),
65
+ "pornography": datasets.Value("bool"),
66
+ "blasphemy_racism_discrimination": datasets.Value("bool"),
67
+ "radicalism": datasets.Value("bool"),
68
+ "defamation": datasets.Value("bool"),
69
+ }
70
+ )
71
+ elif self.config.schema == "seacrowd_text_multi":
72
+ features = schemas.text_multi_features([0, 1])
73
+
74
+ return datasets.DatasetInfo(
75
+ description=_DESCRIPTION,
76
+ features=features,
77
+ homepage=_HOMEPAGE,
78
+ license=_LICENSE,
79
+ citation=_CITATION,
80
+ )
81
+
82
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
83
+ """Returns SplitGenerators."""
84
+ urls = _URLS[_DATASETNAME]
85
+ train_data = Path(dl_manager.download(urls["train"]))
86
+ test_data = Path(dl_manager.download(urls["test"]))
87
+
88
+ return [
89
+ datasets.SplitGenerator(
90
+ name=datasets.Split.TRAIN,
91
+ gen_kwargs={
92
+ "filepath": train_data,
93
+ "split": "train",
94
+ },
95
+ ),
96
+ datasets.SplitGenerator(
97
+ name=datasets.Split.TEST,
98
+ gen_kwargs={
99
+ "filepath": test_data,
100
+ "split": "test",
101
+ },
102
+ ),
103
+ ]
104
+
105
+ def _generate_examples(self, filepath: Path, split: str) -> Tuple[int, Dict]:
106
+ """Yields examples as (key, example) tuples."""
107
+ # Dataset does not have id, using row index as id
108
+ label_cols = ["pornography", "blasphemy_racism_discrimination", "radicalism", "defamation"]
109
+ df = pd.read_csv(filepath, encoding="ISO-8859-1").reset_index()
110
+ df.columns = ["id", "original_text", "text"] + label_cols
111
+
112
+ if self.config.schema == "source":
113
+ for row in df.itertuples():
114
+ ex = {
115
+ "text": row.text,
116
+ }
117
+ for label in label_cols:
118
+ ex[label] = getattr(row, label)
119
+ yield row.id, ex
120
+
121
+ elif self.config.schema == "seacrowd_text_multi":
122
+ for row in df.itertuples():
123
+ ex = {
124
+ "id": str(row.id),
125
+ "text": row.text,
126
+ "labels": [label for label in row[4:]],
127
+ }
128
+ yield row.id, ex
129
+ else:
130
+ raise ValueError(f"Invalid config: {self.config.name}")