samuelcahyawijaya commited on
Commit
1e98637
1 Parent(s): 36af593

Upload nusatranslation_emot.py

Browse files
Files changed (1) hide show
  1. nusatranslation_emot.py +165 -0
nusatranslation_emot.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, DEFAULT_SOURCE_VIEW_NAME, Tasks
10
+
11
+ _LOCAL = False
12
+
13
+ _DATASETNAME = "nusatranslation_emot"
14
+ _SOURCE_VIEW_NAME = DEFAULT_SOURCE_VIEW_NAME
15
+ _UNIFIED_VIEW_NAME = DEFAULT_NUSANTARA_VIEW_NAME
16
+
17
+ _LANGUAGES = ["abs", "btk", "bew", "bug", "jav", "mad", "mak", "min", "mui", "rej", "sun"] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
18
+
19
+ _CITATION = """\
20
+ @unpublished{anonymous2023nusawrites:,
21
+ title={NusaWrites: Constructing High-Quality Corpora for Underrepresented and Extremely Low-Resource Languages},
22
+ author={Anonymous},
23
+ journal={OpenReview Preprint},
24
+ year={2023},
25
+ note={anonymous preprint under review}
26
+ }
27
+ """
28
+
29
+ _DESCRIPTION = """\
30
+ 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.
31
+ 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).
32
+ For the rhetoric mode classification task, we cover 5 rhetoric modes, i.e., narrative, persuasive, argumentative, descriptive, and expository.
33
+ """
34
+
35
+ _HOMEPAGE = "https://github.com/IndoNLP/nusa-writes"
36
+
37
+ _LICENSE = "Creative Commons Attribution Share-Alike 4.0 International"
38
+
39
+ _SUPPORTED_TASKS = [Tasks.EMOTION_CLASSIFICATION]
40
+
41
+ _SOURCE_VERSION = "1.0.0"
42
+
43
+ _NUSANTARA_VERSION = "1.0.0"
44
+
45
+ _URLS = {
46
+ "train": "https://raw.githubusercontent.com/IndoNLP/nusa-writes/main/data/nusa_kalimat-emot-{lang}-train.csv",
47
+ "validation": "https://raw.githubusercontent.com/IndoNLP/nusa-writes/main/data/nusa_kalimat-emot-{lang}-valid.csv",
48
+ "test": "https://raw.githubusercontent.com/IndoNLP/nusa-writes/main/data/nusa_kalimat-emot-{lang}-test.csv",
49
+ }
50
+
51
+
52
+ def nusantara_config_constructor(lang, schema, version):
53
+ """Construct NusantaraConfig with nusatranslation_emot_{lang}_{schema} as the name format"""
54
+ if schema != "source" and schema != "nusantara_text":
55
+ raise ValueError(f"Invalid schema: {schema}")
56
+
57
+ if lang == "":
58
+ return NusantaraConfig(
59
+ name="nusatranslation_emot_{schema}".format(schema=schema),
60
+ version=datasets.Version(version),
61
+ description="nusatranslation_emot with {schema} schema for all 12 languages".format(schema=schema),
62
+ schema=schema,
63
+ subset_id="nusatranslation_emot",
64
+ )
65
+ else:
66
+ return NusantaraConfig(
67
+ name="nusatranslation_emot_{lang}_{schema}".format(lang=lang, schema=schema),
68
+ version=datasets.Version(version),
69
+ description="nusatranslation_emot with {schema} schema for {lang} language".format(lang=lang, schema=schema),
70
+ schema=schema,
71
+ subset_id="nusatranslation_emot",
72
+ )
73
+
74
+
75
+ LANGUAGES_MAP = {
76
+ "abs": "ambon",
77
+ "btk": "batak",
78
+ "bew": "betawi",
79
+ "bhp": "bima",
80
+ "jav": "javanese",
81
+ "mad": "madurese",
82
+ "mak": "makassarese",
83
+ "min": "minangkabau",
84
+ "mui": "musi",
85
+ "rej": "rejang",
86
+ "sun": "sundanese",
87
+ }
88
+
89
+
90
+ class NusaTranslationEmot(datasets.GeneratorBasedBuilder):
91
+ """NusaTranslationEmot is a 5-labels (fear, sadness, happy, anger, love) emotion classification dataset for 11 Indonesian local languages + Indonesian and English."""
92
+
93
+ BUILDER_CONFIGS = (
94
+ [nusantara_config_constructor(lang, "source", _SOURCE_VERSION) for lang in LANGUAGES_MAP]
95
+ + [nusantara_config_constructor(lang, "nusantara_text", _NUSANTARA_VERSION) for lang in LANGUAGES_MAP]
96
+ + [nusantara_config_constructor("", "source", _SOURCE_VERSION), nusantara_config_constructor("", "nusantara_text", _NUSANTARA_VERSION)]
97
+ )
98
+
99
+ DEFAULT_CONFIG_NAME = "nusatranslation_emot_source"
100
+
101
+ def _info(self) -> datasets.DatasetInfo:
102
+ if self.config.schema == "source":
103
+ features = datasets.Features(
104
+ {
105
+ "id": datasets.Value("string"),
106
+ "text": datasets.Value("string"),
107
+ "label": datasets.Value("string"),
108
+ }
109
+ )
110
+ elif self.config.schema == "nusantara_text":
111
+ features = schemas.text_features(["fear", "sadness", "happy", "anger", "love"])
112
+
113
+ return datasets.DatasetInfo(
114
+ description=_DESCRIPTION,
115
+ features=features,
116
+ homepage=_HOMEPAGE,
117
+ license=_LICENSE,
118
+ citation=_CITATION,
119
+ )
120
+
121
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
122
+ """Returns SplitGenerators."""
123
+ if self.config.name == "nusatranslation_emot_source" or self.config.name == "nusatranslation_emot_nusantara_text":
124
+ # Load all 12 languages
125
+ train_csv_path = dl_manager.download_and_extract([_URLS["train"].format(lang=lang) for lang in LANGUAGES_MAP])
126
+ validation_csv_path = dl_manager.download_and_extract([_URLS["validation"].format(lang=lang) for lang in LANGUAGES_MAP])
127
+ test_csv_path = dl_manager.download_and_extract([_URLS["test"].format(lang=lang) for lang in LANGUAGES_MAP])
128
+ else:
129
+ lang = self.config.name.split("_")[2]
130
+ train_csv_path = Path(dl_manager.download_and_extract(_URLS["train"].format(lang=lang)))
131
+ validation_csv_path = Path(dl_manager.download_and_extract(_URLS["validation"].format(lang=lang)))
132
+ test_csv_path = Path(dl_manager.download_and_extract(_URLS["test"].format(lang=lang)))
133
+
134
+ return [
135
+ datasets.SplitGenerator(
136
+ name=datasets.Split.TRAIN,
137
+ gen_kwargs={"filepath": train_csv_path},
138
+ ),
139
+ datasets.SplitGenerator(
140
+ name=datasets.Split.VALIDATION,
141
+ gen_kwargs={"filepath": validation_csv_path},
142
+ ),
143
+ datasets.SplitGenerator(
144
+ name=datasets.Split.TEST,
145
+ gen_kwargs={"filepath": test_csv_path},
146
+ ),
147
+ ]
148
+
149
+ def _generate_examples(self, filepath: Path) -> Tuple[int, Dict]:
150
+ if self.config.schema != "source" and self.config.schema != "nusantara_text":
151
+ raise ValueError(f"Invalid config: {self.config.name}")
152
+
153
+ if self.config.name == "nusatranslation_emot_source" or self.config.name == "nusatranslation_emot_nusantara_text":
154
+ ldf = []
155
+ for fp in filepath:
156
+ ldf.append(pd.read_csv(fp))
157
+ df = pd.concat(ldf, axis=0, ignore_index=True).reset_index()
158
+ # Have to use index instead of id to avoid duplicated key
159
+ df = df.drop(columns=["id"]).rename(columns={"index": "id"})
160
+ else:
161
+ df = pd.read_csv(filepath).reset_index()
162
+
163
+ for row in df.itertuples():
164
+ ex = {"id": str(row.id), "text": row.text, "label": row.label}
165
+ yield row.id, ex