samuelcahyawijaya
commited on
Commit
•
c84d97b
1
Parent(s):
ee2e175
Upload nusaparagraph_emot.py
Browse files- nusaparagraph_emot.py +170 -0
nusaparagraph_emot.py
ADDED
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
from typing import Dict, List, Tuple
|
3 |
+
import datasets
|
4 |
+
import pandas as pd
|
5 |
+
from nusacrowd.utils import schemas
|
6 |
+
from nusacrowd.utils.configs import NusantaraConfig
|
7 |
+
from nusacrowd.utils.constants import (DEFAULT_NUSANTARA_VIEW_NAME,
|
8 |
+
DEFAULT_SOURCE_VIEW_NAME, Tasks)
|
9 |
+
_LOCAL = False
|
10 |
+
_DATASETNAME = "nusaparagraph_emot"
|
11 |
+
_SOURCE_VIEW_NAME = DEFAULT_SOURCE_VIEW_NAME
|
12 |
+
_UNIFIED_VIEW_NAME = DEFAULT_NUSANTARA_VIEW_NAME
|
13 |
+
_LANGUAGES = [
|
14 |
+
"btk", "bew", "bug", "jav", "mad", "mak", "min", "mui", "rej", "sun"
|
15 |
+
] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
|
16 |
+
_CITATION = """\
|
17 |
+
@unpublished{anonymous2023nusawrites:,
|
18 |
+
title={NusaWrites: Constructing High-Quality Corpora for Underrepresented and Extremely Low-Resource Languages},
|
19 |
+
author={Anonymous},
|
20 |
+
journal={OpenReview Preprint},
|
21 |
+
year={2023},
|
22 |
+
note={anonymous preprint under review}
|
23 |
+
}
|
24 |
+
"""
|
25 |
+
_DESCRIPTION = """\
|
26 |
+
Democratizing access to natural language processing (NLP) technology is crucial, especially for underrepresented and extremely low-resource languages. Previous research has focused on developing labeled and unlabeled corpora for these languages through online scraping and document translation. While these methods have proven effective and cost-efficient, we have identified limitations in the resulting corpora, including a lack of lexical diversity and cultural relevance to local communities. To address this gap, we conduct a case study on Indonesian local languages. We compare the effectiveness of online scraping, human translation, and paragraph writing by native speakers in constructing datasets. Our findings demonstrate that datasets generated through paragraph writing by native speakers exhibit superior quality in terms of lexical diversity and cultural content. In addition, we present the NusaWrites benchmark, encompassing 12 underrepresented and extremely low-resource languages spoken by millions of individuals in Indonesia. Our empirical experiment results using existing multilingual large language models conclude the need to extend these models to more underrepresented languages.
|
27 |
+
We introduce a novel high quality human curated corpora, i.e., NusaMenulis, which covers 12 languages spoken in Indonesia. The resource extend the coverage of languages to 5 new languages, i.e., Ambon (abs), Bima (bhp), Makassarese (mak), Palembang / Musi (mui), and Rejang (rej).
|
28 |
+
For the emotion recognition task, we cover the 6 basic emotions (Ekman, 1992): fear, disgusted, sad, happy, angry, and surprise, and an additional emotion label: shame (Poulson and of Tasmania. School of Management, 2000.
|
29 |
+
"""
|
30 |
+
_HOMEPAGE = "https://github.com/IndoNLP/nusa-writes"
|
31 |
+
_LICENSE = "Creative Commons Attribution Share-Alike 4.0 International"
|
32 |
+
_SUPPORTED_TASKS = [Tasks.EMOTION_CLASSIFICATION]
|
33 |
+
_SOURCE_VERSION = "1.0.0"
|
34 |
+
_NUSANTARA_VERSION = "1.0.0"
|
35 |
+
_URLS = {
|
36 |
+
"train":
|
37 |
+
"https://raw.githubusercontent.com/IndoNLP/nusa-writes/main/data/nusa_alinea-emot-{lang}-train.csv",
|
38 |
+
"validation":
|
39 |
+
"https://raw.githubusercontent.com/IndoNLP/nusa-writes/main/data/nusa_alinea-emot-{lang}-valid.csv",
|
40 |
+
"test":
|
41 |
+
"https://raw.githubusercontent.com/IndoNLP/nusa-writes/main/data/nusa_alinea-emot-{lang}-test.csv",
|
42 |
+
}
|
43 |
+
def nusantara_config_constructor(lang, schema, version):
|
44 |
+
"""Construct NusantaraConfig with nusaparagraph_emot_{lang}_{schema} as the name format"""
|
45 |
+
if schema != "source" and schema != "nusantara_text":
|
46 |
+
raise ValueError(f"Invalid schema: {schema}")
|
47 |
+
if lang == "":
|
48 |
+
return NusantaraConfig(
|
49 |
+
name="nusaparagraph_emot_{schema}".format(schema=schema),
|
50 |
+
version=datasets.Version(version),
|
51 |
+
description=
|
52 |
+
"nusaparagraph_emot with {schema} schema for all 10 languages".
|
53 |
+
format(schema=schema),
|
54 |
+
schema=schema,
|
55 |
+
subset_id="nusaparagraph_emot",
|
56 |
+
)
|
57 |
+
else:
|
58 |
+
return NusantaraConfig(
|
59 |
+
name="nusaparagraph_emot_{lang}_{schema}".format(lang=lang,
|
60 |
+
schema=schema),
|
61 |
+
version=datasets.Version(version),
|
62 |
+
description=
|
63 |
+
"nusaparagraph_emot with {schema} schema for {lang} language".
|
64 |
+
format(lang=lang, schema=schema),
|
65 |
+
schema=schema,
|
66 |
+
subset_id="nusaparagraph_emot",
|
67 |
+
)
|
68 |
+
LANGUAGES_MAP = {
|
69 |
+
"btk": "batak",
|
70 |
+
"bew": "betawi",
|
71 |
+
"bug": "buginese",
|
72 |
+
"jav": "javanese",
|
73 |
+
"mad": "madurese",
|
74 |
+
"mak": "makassarese",
|
75 |
+
"min": "minangkabau",
|
76 |
+
"mui": "musi",
|
77 |
+
"rej": "rejang",
|
78 |
+
"sun": "sundanese"
|
79 |
+
}
|
80 |
+
class NusaParagraphEmot(datasets.GeneratorBasedBuilder):
|
81 |
+
"""NusaParagraph-Emot is a 7-labels (fear, disgusted, sad, happy, angry, surprise, and shame) emotion classification dataset for 10 Indonesian local languages."""
|
82 |
+
BUILDER_CONFIGS = ([
|
83 |
+
nusantara_config_constructor(lang, "source", _SOURCE_VERSION)
|
84 |
+
for lang in LANGUAGES_MAP
|
85 |
+
] + [
|
86 |
+
nusantara_config_constructor(lang, "nusantara_text",
|
87 |
+
_NUSANTARA_VERSION)
|
88 |
+
for lang in LANGUAGES_MAP
|
89 |
+
] + [
|
90 |
+
nusantara_config_constructor("", "source", _SOURCE_VERSION),
|
91 |
+
nusantara_config_constructor("", "nusantara_text", _NUSANTARA_VERSION)
|
92 |
+
])
|
93 |
+
DEFAULT_CONFIG_NAME = "nusaparagraph_emot_ind_source"
|
94 |
+
def _info(self) -> datasets.DatasetInfo:
|
95 |
+
if self.config.schema == "source":
|
96 |
+
features = datasets.Features({
|
97 |
+
"id": datasets.Value("string"),
|
98 |
+
"text": datasets.Value("string"),
|
99 |
+
"label": datasets.Value("string"),
|
100 |
+
})
|
101 |
+
elif self.config.schema == "nusantara_text":
|
102 |
+
features = schemas.text_features([
|
103 |
+
"fear", "disgusted", "sad", "happy", "angry", "surprise",
|
104 |
+
"shame"
|
105 |
+
])
|
106 |
+
return datasets.DatasetInfo(
|
107 |
+
description=_DESCRIPTION,
|
108 |
+
features=features,
|
109 |
+
homepage=_HOMEPAGE,
|
110 |
+
license=_LICENSE,
|
111 |
+
citation=_CITATION,
|
112 |
+
)
|
113 |
+
def _split_generators(
|
114 |
+
self, dl_manager: datasets.DownloadManager
|
115 |
+
) -> List[datasets.SplitGenerator]:
|
116 |
+
"""Returns SplitGenerators."""
|
117 |
+
if self.config.name == "nusaparagraph_emot_source" or self.config.name == "nusaparagraph_emot_nusantara_text":
|
118 |
+
# Load all 12 languages
|
119 |
+
train_csv_path = dl_manager.download_and_extract([
|
120 |
+
_URLS["train"].format(lang=lang)
|
121 |
+
for lang in LANGUAGES_MAP
|
122 |
+
])
|
123 |
+
validation_csv_path = dl_manager.download_and_extract([
|
124 |
+
_URLS["validation"].format(lang=lang)
|
125 |
+
for lang in LANGUAGES_MAP
|
126 |
+
])
|
127 |
+
test_csv_path = dl_manager.download_and_extract([
|
128 |
+
_URLS["test"].format(lang=lang)
|
129 |
+
for lang in LANGUAGES_MAP
|
130 |
+
])
|
131 |
+
else:
|
132 |
+
lang = self.config.name.split('_')[2]
|
133 |
+
train_csv_path = Path(
|
134 |
+
dl_manager.download_and_extract(
|
135 |
+
_URLS["train"].format(lang=lang)))
|
136 |
+
validation_csv_path = Path(
|
137 |
+
dl_manager.download_and_extract(
|
138 |
+
_URLS["validation"].format(lang=lang)))
|
139 |
+
test_csv_path = Path(
|
140 |
+
dl_manager.download_and_extract(
|
141 |
+
_URLS["test"].format(lang=lang)))
|
142 |
+
return [
|
143 |
+
datasets.SplitGenerator(
|
144 |
+
name=datasets.Split.TRAIN,
|
145 |
+
gen_kwargs={"filepath": train_csv_path},
|
146 |
+
),
|
147 |
+
datasets.SplitGenerator(
|
148 |
+
name=datasets.Split.VALIDATION,
|
149 |
+
gen_kwargs={"filepath": validation_csv_path},
|
150 |
+
),
|
151 |
+
datasets.SplitGenerator(
|
152 |
+
name=datasets.Split.TEST,
|
153 |
+
gen_kwargs={"filepath": test_csv_path},
|
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 |
+
if self.config.name == "nusaparagraph_emot_source" or self.config.name == "nusaparagraph_emot_nusantara_text":
|
160 |
+
ldf = []
|
161 |
+
for fp in filepath:
|
162 |
+
ldf.append(pd.read_csv(fp))
|
163 |
+
df = pd.concat(ldf, axis=0, ignore_index=True).reset_index()
|
164 |
+
# Have to use index instead of id to avoid duplicated key
|
165 |
+
df = df.drop(columns=["id"]).rename(columns={"index": "id"})
|
166 |
+
else:
|
167 |
+
df = pd.read_csv(filepath).reset_index()
|
168 |
+
for row in df.itertuples():
|
169 |
+
ex = {"id": str(row.id), "text": row.text, "label": row.label}
|
170 |
+
yield row.id, ex
|