holylovenia
commited on
Commit
•
ed2b8e8
1
Parent(s):
e032ad8
Upload nusatranslation_mt.py with huggingface_hub
Browse files- nusatranslation_mt.py +205 -0
nusatranslation_mt.py
ADDED
@@ -0,0 +1,205 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
from typing import Dict, List, Tuple
|
3 |
+
import re
|
4 |
+
|
5 |
+
|
6 |
+
import datasets
|
7 |
+
import pandas as pd
|
8 |
+
|
9 |
+
from nusacrowd.utils import schemas
|
10 |
+
from nusacrowd.utils.configs import NusantaraConfig
|
11 |
+
from nusacrowd.utils.constants import DEFAULT_NUSANTARA_VIEW_NAME, DEFAULT_SOURCE_VIEW_NAME, Tasks
|
12 |
+
|
13 |
+
_DATASETNAME = "nusatranslation_mt"
|
14 |
+
_SOURCE_VIEW_NAME = DEFAULT_SOURCE_VIEW_NAME
|
15 |
+
_UNIFIED_VIEW_NAME = DEFAULT_NUSANTARA_VIEW_NAME
|
16 |
+
|
17 |
+
_LANGUAGES = ["ind", "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 |
+
_LOCAL = False
|
19 |
+
|
20 |
+
_CITATION = """\
|
21 |
+
@unpublished{anonymous2023nusawrites:,
|
22 |
+
title={NusaWrites: Constructing High-Quality Corpora for Underrepresented and Extremely Low-Resource Languages},
|
23 |
+
author={Anonymous},
|
24 |
+
journal={OpenReview Preprint},
|
25 |
+
year={2023},
|
26 |
+
note={anonymous preprint under review}
|
27 |
+
}
|
28 |
+
"""
|
29 |
+
|
30 |
+
_DESCRIPTION = """\
|
31 |
+
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.
|
32 |
+
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).
|
33 |
+
For the rhetoric mode classification task, we cover 5 rhetoric modes, i.e., narrative, persuasive, argumentative, descriptive, and expository.
|
34 |
+
"""
|
35 |
+
|
36 |
+
_HOMEPAGE = "https://github.com/IndoNLP/nusatranslation/tree/main/datasets/mt"
|
37 |
+
|
38 |
+
_LICENSE = "Creative Commons Attribution Share-Alike 4.0 International"
|
39 |
+
|
40 |
+
_SUPPORTED_TASKS = [Tasks.MACHINE_TRANSLATION]
|
41 |
+
|
42 |
+
_SOURCE_VERSION = "1.0.0"
|
43 |
+
|
44 |
+
_NUSANTARA_VERSION = "1.0.0"
|
45 |
+
|
46 |
+
_URLS = {
|
47 |
+
"train": "https://raw.githubusercontent.com/IndoNLP/nusa-writes/main/data/nusa_kalimat-mt-{lang}-train.csv",
|
48 |
+
"validation": "https://raw.githubusercontent.com/IndoNLP/nusa-writes/main/data/nusa_kalimat-mt-{lang}-valid.csv",
|
49 |
+
"test": "https://raw.githubusercontent.com/IndoNLP/nusa-writes/main/data/nusa_kalimat-mt-{lang}-test.csv",
|
50 |
+
}
|
51 |
+
|
52 |
+
LANGUAGES_MAP = {
|
53 |
+
"abs": "ambon",
|
54 |
+
"btk": "batak",
|
55 |
+
"bew": "betawi",
|
56 |
+
"bhp": "bima",
|
57 |
+
"jav": "javanese",
|
58 |
+
"mad": "madurese",
|
59 |
+
"mak": "makassarese",
|
60 |
+
"min": "minangkabau",
|
61 |
+
"mui": "musi",
|
62 |
+
"rej": "rejang",
|
63 |
+
"sun": "sundanese",
|
64 |
+
}
|
65 |
+
|
66 |
+
|
67 |
+
class NusaTranslationMT(datasets.GeneratorBasedBuilder):
|
68 |
+
"""NusaTranslation-MT is a parallel corpus for training and benchmarking machine translation models from 11 Indonesian local language to Bahasa Indonesia. The data is presented in csv format with 2 columns, where one column contain sentence in Bahasa and another in the local language."""
|
69 |
+
|
70 |
+
BUILDER_CONFIGS = (
|
71 |
+
[
|
72 |
+
NusantaraConfig(
|
73 |
+
name=f"nusatranslation_mt_ind_{subset}_source",
|
74 |
+
version=datasets.Version(_SOURCE_VERSION),
|
75 |
+
description=f"nusatranslation_mt ind2{subset} source schema",
|
76 |
+
schema="source",
|
77 |
+
subset_id=f"nusatranslation_mt",
|
78 |
+
)
|
79 |
+
for subset in _LANGUAGES[1:]
|
80 |
+
]
|
81 |
+
+ [
|
82 |
+
NusantaraConfig(
|
83 |
+
name=f"nusatranslation_mt_ind_{subset}_nusantara_t2t",
|
84 |
+
version=datasets.Version(_NUSANTARA_VERSION),
|
85 |
+
description=f"nusatranslation_mt ind2{subset} Nusantara schema",
|
86 |
+
schema="nusantara_t2t",
|
87 |
+
subset_id=f"nusatranslation_mt",
|
88 |
+
)
|
89 |
+
for subset in _LANGUAGES[1:]
|
90 |
+
]
|
91 |
+
+ [
|
92 |
+
NusantaraConfig(
|
93 |
+
name=f"nusatranslation_mt_{subset}_ind_source",
|
94 |
+
version=datasets.Version(_SOURCE_VERSION),
|
95 |
+
description=f"nusatranslation_mt {subset}2ind source schema",
|
96 |
+
schema="source",
|
97 |
+
subset_id=f"nusatranslation_mt",
|
98 |
+
)
|
99 |
+
for subset in _LANGUAGES[1:]
|
100 |
+
]
|
101 |
+
+ [
|
102 |
+
NusantaraConfig(
|
103 |
+
name=f"nusatranslation_mt_{subset}_ind_nusantara_t2t",
|
104 |
+
version=datasets.Version(_NUSANTARA_VERSION),
|
105 |
+
description=f"nusatranslation_mt {subset}2ind Nusantara schema",
|
106 |
+
schema="nusantara_t2t",
|
107 |
+
subset_id=f"nusatranslation_mt",
|
108 |
+
)
|
109 |
+
for subset in _LANGUAGES[1:]
|
110 |
+
]
|
111 |
+
)
|
112 |
+
|
113 |
+
DEFAULT_CONFIG_NAME = "nusatranslation_mt_jav_ind_source"
|
114 |
+
|
115 |
+
def _info(self):
|
116 |
+
if self.config.schema == "source":
|
117 |
+
features = datasets.Features({"id": datasets.Value("string"), "text": datasets.Value("string"), "label": datasets.Value("string")})
|
118 |
+
elif self.config.schema == "nusantara_t2t":
|
119 |
+
features = schemas.text2text_features
|
120 |
+
|
121 |
+
return datasets.DatasetInfo(
|
122 |
+
description=_DESCRIPTION,
|
123 |
+
features=features,
|
124 |
+
homepage=_HOMEPAGE,
|
125 |
+
license=_LICENSE,
|
126 |
+
citation=_CITATION,
|
127 |
+
)
|
128 |
+
|
129 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
130 |
+
"""Returns SplitGenerators."""
|
131 |
+
lang = self.config.name.split("_")[2] if self.config.name.split("_")[2] != "ind" else self.config.name.split("_")[3]
|
132 |
+
train_csv_path = Path(dl_manager.download_and_extract(_URLS["train"].format(lang=lang)))
|
133 |
+
validation_csv_path = Path(dl_manager.download_and_extract(_URLS["validation"].format(lang=lang)))
|
134 |
+
test_csv_path = Path(dl_manager.download_and_extract(_URLS["test"].format(lang=lang)))
|
135 |
+
|
136 |
+
return [
|
137 |
+
datasets.SplitGenerator(
|
138 |
+
name=datasets.Split.TRAIN,
|
139 |
+
gen_kwargs={"filepath": train_csv_path},
|
140 |
+
),
|
141 |
+
datasets.SplitGenerator(
|
142 |
+
name=datasets.Split.VALIDATION,
|
143 |
+
gen_kwargs={"filepath": validation_csv_path},
|
144 |
+
),
|
145 |
+
datasets.SplitGenerator(
|
146 |
+
name=datasets.Split.TEST,
|
147 |
+
gen_kwargs={"filepath": test_csv_path},
|
148 |
+
),
|
149 |
+
]
|
150 |
+
|
151 |
+
def _merge_subsets(self, df, subsets, revert=False):
|
152 |
+
if not subsets:
|
153 |
+
return None
|
154 |
+
# df = None
|
155 |
+
# print(dfs)
|
156 |
+
# print(subsets)
|
157 |
+
orig_columns = df.columns.tolist()
|
158 |
+
print(df.columns)
|
159 |
+
|
160 |
+
df.columns = orig_columns[:1] + ["label", "text"] if revert else orig_columns[:1] + ["text", "label"]
|
161 |
+
return df
|
162 |
+
|
163 |
+
def get_domain_data(self, dfs):
|
164 |
+
domain = self.config.name
|
165 |
+
matched_domain = re.findall(r"nusatranslation_mt_.*?_.*?_", domain)
|
166 |
+
|
167 |
+
assert len(matched_domain) == 1
|
168 |
+
domain = matched_domain[0][:-1].replace("nusatranslation_mt_", "").split("_")
|
169 |
+
src_lang, tgt_lang = domain[0], domain[1]
|
170 |
+
|
171 |
+
subsets = LANGUAGES_MAP.get(src_lang if src_lang != "ind" else tgt_lang, None)
|
172 |
+
return src_lang, tgt_lang, self._merge_subsets(dfs, subsets, revert=(src_lang != "ind"))
|
173 |
+
|
174 |
+
def _generate_examples(self, filepath: Path) -> Tuple[int, Dict]:
|
175 |
+
if self.config.schema != "source" and self.config.schema != "nusantara_t2t":
|
176 |
+
raise ValueError(f"Invalid config schema: {self.config.schema}")
|
177 |
+
|
178 |
+
# print(filepath)
|
179 |
+
df = pd.read_csv(filepath)
|
180 |
+
# ldf = []
|
181 |
+
# for fp in filepath:
|
182 |
+
# ldf.append(pd.read_csv(fp))
|
183 |
+
src_lang, tgt_lang, df = self.get_domain_data((df))
|
184 |
+
|
185 |
+
if self.config.schema == "source":
|
186 |
+
for idx, row in enumerate(df.itertuples()):
|
187 |
+
ex = {
|
188 |
+
"id": str(idx),
|
189 |
+
"text": row.text,
|
190 |
+
"label": row.label,
|
191 |
+
}
|
192 |
+
yield idx, ex
|
193 |
+
|
194 |
+
elif self.config.schema == "nusantara_t2t":
|
195 |
+
for idx, row in enumerate(df.itertuples()):
|
196 |
+
ex = {
|
197 |
+
"id": str(idx),
|
198 |
+
"text_1": row.text,
|
199 |
+
"text_2": row.label,
|
200 |
+
"text_1_name": src_lang,
|
201 |
+
"text_2_name": tgt_lang,
|
202 |
+
}
|
203 |
+
yield idx, ex
|
204 |
+
else:
|
205 |
+
raise ValueError(f"Invalid config: {self.config.name}")
|