holylovenia
commited on
Commit
·
fdfbdae
1
Parent(s):
1408622
Upload nusax_senti.py with huggingface_hub
Browse files- nusax_senti.py +172 -0
nusax_senti.py
ADDED
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 (DEFAULT_NUSANTARA_VIEW_NAME,
|
10 |
+
DEFAULT_SOURCE_VIEW_NAME, Tasks)
|
11 |
+
|
12 |
+
_LOCAL = False
|
13 |
+
|
14 |
+
_DATASETNAME = "nusax_senti"
|
15 |
+
_SOURCE_VIEW_NAME = DEFAULT_SOURCE_VIEW_NAME
|
16 |
+
_UNIFIED_VIEW_NAME = DEFAULT_NUSANTARA_VIEW_NAME
|
17 |
+
|
18 |
+
_LANGUAGES = ["ind", "ace", "ban", "bjn", "bbc", "bug", "jav", "mad", "min", "nij", "sun", "eng"] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
|
19 |
+
|
20 |
+
_CITATION = """\
|
21 |
+
@misc{winata2022nusax,
|
22 |
+
title={NusaX: Multilingual Parallel Sentiment Dataset for 10 Indonesian Local Languages},
|
23 |
+
author={Winata, Genta Indra and Aji, Alham Fikri and Cahyawijaya,
|
24 |
+
Samuel and Mahendra, Rahmad and Koto, Fajri and Romadhony,
|
25 |
+
Ade and Kurniawan, Kemal and Moeljadi, David and Prasojo,
|
26 |
+
Radityo Eko and Fung, Pascale and Baldwin, Timothy and Lau,
|
27 |
+
Jey Han and Sennrich, Rico and Ruder, Sebastian},
|
28 |
+
year={2022},
|
29 |
+
eprint={2205.15960},
|
30 |
+
archivePrefix={arXiv},
|
31 |
+
primaryClass={cs.CL}
|
32 |
+
}
|
33 |
+
"""
|
34 |
+
|
35 |
+
_DESCRIPTION = """\
|
36 |
+
NusaX is a high-quality multilingual parallel corpus that covers 12 languages, Indonesian, English, and 10 Indonesian local languages, namely Acehnese, Balinese, Banjarese, Buginese, Madurese, Minangkabau, Javanese, Ngaju, Sundanese, and Toba Batak.
|
37 |
+
|
38 |
+
NusaX-Senti is a 3-labels (positive, neutral, negative) sentiment analysis dataset for 10 Indonesian local languages + Indonesian and English.
|
39 |
+
"""
|
40 |
+
|
41 |
+
_HOMEPAGE = "https://github.com/IndoNLP/nusax/tree/main/datasets/sentiment"
|
42 |
+
|
43 |
+
_LICENSE = "Creative Commons Attribution Share-Alike 4.0 International"
|
44 |
+
|
45 |
+
_SUPPORTED_TASKS = [Tasks.SENTIMENT_ANALYSIS]
|
46 |
+
|
47 |
+
_SOURCE_VERSION = "1.0.0"
|
48 |
+
|
49 |
+
_NUSANTARA_VERSION = "1.0.0"
|
50 |
+
|
51 |
+
_URLS = {
|
52 |
+
"train": "https://raw.githubusercontent.com/IndoNLP/nusax/main/datasets/sentiment/{lang}/train.csv",
|
53 |
+
"validation": "https://raw.githubusercontent.com/IndoNLP/nusax/main/datasets/sentiment/{lang}/valid.csv",
|
54 |
+
"test": "https://raw.githubusercontent.com/IndoNLP/nusax/main/datasets/sentiment/{lang}/test.csv",
|
55 |
+
}
|
56 |
+
|
57 |
+
|
58 |
+
def nusantara_config_constructor(lang, schema, version):
|
59 |
+
"""Construct NusantaraConfig with nusax_senti_{lang}_{schema} as the name format"""
|
60 |
+
if schema != "source" and schema != "nusantara_text":
|
61 |
+
raise ValueError(f"Invalid schema: {schema}")
|
62 |
+
|
63 |
+
if lang == "":
|
64 |
+
return NusantaraConfig(
|
65 |
+
name="nusax_senti_{schema}".format(schema=schema),
|
66 |
+
version=datasets.Version(version),
|
67 |
+
description="nusax_senti with {schema} schema for all 12 languages".format(schema=schema),
|
68 |
+
schema=schema,
|
69 |
+
subset_id="nusax_senti",
|
70 |
+
)
|
71 |
+
else:
|
72 |
+
return NusantaraConfig(
|
73 |
+
name="nusax_senti_{lang}_{schema}".format(lang=lang, schema=schema),
|
74 |
+
version=datasets.Version(version),
|
75 |
+
description="nusax_senti with {schema} schema for {lang} language".format(lang=lang, schema=schema),
|
76 |
+
schema=schema,
|
77 |
+
subset_id="nusax_senti",
|
78 |
+
)
|
79 |
+
|
80 |
+
|
81 |
+
LANGUAGES_MAP = {
|
82 |
+
"ace": "acehnese",
|
83 |
+
"ban": "balinese",
|
84 |
+
"bjn": "banjarese",
|
85 |
+
"bug": "buginese",
|
86 |
+
"eng": "english",
|
87 |
+
"ind": "indonesian",
|
88 |
+
"jav": "javanese",
|
89 |
+
"mad": "madurese",
|
90 |
+
"min": "minangkabau",
|
91 |
+
"nij": "ngaju",
|
92 |
+
"sun": "sundanese",
|
93 |
+
"bbc": "toba_batak",
|
94 |
+
}
|
95 |
+
|
96 |
+
|
97 |
+
class NusaXSenti(datasets.GeneratorBasedBuilder):
|
98 |
+
"""NusaX-Senti is a 3-labels (positive, neutral, negative) sentiment analysis dataset for 10 Indonesian local languages + Indonesian and English."""
|
99 |
+
|
100 |
+
BUILDER_CONFIGS = (
|
101 |
+
[nusantara_config_constructor(lang, "source", _SOURCE_VERSION) for lang in LANGUAGES_MAP]
|
102 |
+
+ [nusantara_config_constructor(lang, "nusantara_text", _NUSANTARA_VERSION) for lang in LANGUAGES_MAP]
|
103 |
+
+ [nusantara_config_constructor("", "source", _SOURCE_VERSION), nusantara_config_constructor("", "nusantara_text", _NUSANTARA_VERSION)]
|
104 |
+
)
|
105 |
+
|
106 |
+
DEFAULT_CONFIG_NAME = "nusax_senti_ind_source"
|
107 |
+
|
108 |
+
def _info(self) -> datasets.DatasetInfo:
|
109 |
+
if self.config.schema == "source":
|
110 |
+
features = datasets.Features(
|
111 |
+
{
|
112 |
+
"id": datasets.Value("string"),
|
113 |
+
"text": datasets.Value("string"),
|
114 |
+
"label": datasets.Value("string"),
|
115 |
+
}
|
116 |
+
)
|
117 |
+
elif self.config.schema == "nusantara_text":
|
118 |
+
features = schemas.text_features(["negative", "neutral", "positive"])
|
119 |
+
|
120 |
+
return datasets.DatasetInfo(
|
121 |
+
description=_DESCRIPTION,
|
122 |
+
features=features,
|
123 |
+
homepage=_HOMEPAGE,
|
124 |
+
license=_LICENSE,
|
125 |
+
citation=_CITATION,
|
126 |
+
)
|
127 |
+
|
128 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
129 |
+
"""Returns SplitGenerators."""
|
130 |
+
if self.config.name == "nusax_senti_source" or self.config.name == "nusax_senti_nusantara_text":
|
131 |
+
# Load all 12 languages
|
132 |
+
train_csv_path = dl_manager.download_and_extract([_URLS["train"].format(lang=LANGUAGES_MAP[lang]) for lang in LANGUAGES_MAP])
|
133 |
+
validation_csv_path = dl_manager.download_and_extract([_URLS["validation"].format(lang=LANGUAGES_MAP[lang]) for lang in LANGUAGES_MAP])
|
134 |
+
test_csv_path = dl_manager.download_and_extract([_URLS["test"].format(lang=LANGUAGES_MAP[lang]) for lang in LANGUAGES_MAP])
|
135 |
+
else:
|
136 |
+
lang = self.config.name[12:15]
|
137 |
+
train_csv_path = Path(dl_manager.download_and_extract(_URLS["train"].format(lang=LANGUAGES_MAP[lang])))
|
138 |
+
validation_csv_path = Path(dl_manager.download_and_extract(_URLS["validation"].format(lang=LANGUAGES_MAP[lang])))
|
139 |
+
test_csv_path = Path(dl_manager.download_and_extract(_URLS["test"].format(lang=LANGUAGES_MAP[lang])))
|
140 |
+
|
141 |
+
return [
|
142 |
+
datasets.SplitGenerator(
|
143 |
+
name=datasets.Split.TRAIN,
|
144 |
+
gen_kwargs={"filepath": train_csv_path},
|
145 |
+
),
|
146 |
+
datasets.SplitGenerator(
|
147 |
+
name=datasets.Split.VALIDATION,
|
148 |
+
gen_kwargs={"filepath": validation_csv_path},
|
149 |
+
),
|
150 |
+
datasets.SplitGenerator(
|
151 |
+
name=datasets.Split.TEST,
|
152 |
+
gen_kwargs={"filepath": test_csv_path},
|
153 |
+
),
|
154 |
+
]
|
155 |
+
|
156 |
+
def _generate_examples(self, filepath: Path) -> Tuple[int, Dict]:
|
157 |
+
if self.config.schema != "source" and self.config.schema != "nusantara_text":
|
158 |
+
raise ValueError(f"Invalid config: {self.config.name}")
|
159 |
+
|
160 |
+
if self.config.name == "nusax_senti_source" or self.config.name == "nusax_senti_nusantara_text":
|
161 |
+
ldf = []
|
162 |
+
for fp in filepath:
|
163 |
+
ldf.append(pd.read_csv(fp))
|
164 |
+
df = pd.concat(ldf, axis=0, ignore_index=True).reset_index()
|
165 |
+
# Have to use index instead of id to avoid duplicated key
|
166 |
+
df = df.drop(columns=["id"]).rename(columns={"index": "id"})
|
167 |
+
else:
|
168 |
+
df = pd.read_csv(filepath).reset_index()
|
169 |
+
|
170 |
+
for row in df.itertuples():
|
171 |
+
ex = {"id": str(row.id), "text": row.text, "label": row.label}
|
172 |
+
yield row.id, ex
|