Commit
·
ce8feca
1
Parent(s):
f14bf77
Upload nusax_mt.py with huggingface_hub
Browse files- nusax_mt.py +179 -0
nusax_mt.py
ADDED
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
_DATASETNAME = "nusax_mt"
|
13 |
+
_SOURCE_VIEW_NAME = DEFAULT_SOURCE_VIEW_NAME
|
14 |
+
_UNIFIED_VIEW_NAME = DEFAULT_NUSANTARA_VIEW_NAME
|
15 |
+
|
16 |
+
_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)
|
17 |
+
_LOCAL = False
|
18 |
+
|
19 |
+
_CITATION = """\
|
20 |
+
@misc{winata2022nusax,
|
21 |
+
title={NusaX: Multilingual Parallel Sentiment Dataset for 10 Indonesian Local Languages},
|
22 |
+
author={Winata, Genta Indra and Aji, Alham Fikri and Cahyawijaya,
|
23 |
+
Samuel and Mahendra, Rahmad and Koto, Fajri and Romadhony,
|
24 |
+
Ade and Kurniawan, Kemal and Moeljadi, David and Prasojo,
|
25 |
+
Radityo Eko and Fung, Pascale and Baldwin, Timothy and Lau,
|
26 |
+
Jey Han and Sennrich, Rico and Ruder, Sebastian},
|
27 |
+
year={2022},
|
28 |
+
eprint={2205.15960},
|
29 |
+
archivePrefix={arXiv},
|
30 |
+
primaryClass={cs.CL}
|
31 |
+
}
|
32 |
+
"""
|
33 |
+
|
34 |
+
_DESCRIPTION = """\
|
35 |
+
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.
|
36 |
+
|
37 |
+
NusaX-MT is a parallel corpus for training and benchmarking machine translation models across 10 Indonesian local languages + Indonesian and English. The data is presented in csv format with 12 columns, one column for each language.
|
38 |
+
"""
|
39 |
+
|
40 |
+
_HOMEPAGE = "https://github.com/IndoNLP/nusax/tree/main/datasets/mt"
|
41 |
+
|
42 |
+
_LICENSE = "Creative Commons Attribution Share-Alike 4.0 International"
|
43 |
+
|
44 |
+
_SUPPORTED_TASKS = [Tasks.MACHINE_TRANSLATION]
|
45 |
+
|
46 |
+
_SOURCE_VERSION = "1.0.0"
|
47 |
+
|
48 |
+
_NUSANTARA_VERSION = "1.0.0"
|
49 |
+
|
50 |
+
_URLS = {
|
51 |
+
"train": "https://raw.githubusercontent.com/IndoNLP/nusax/main/datasets/mt/train.csv",
|
52 |
+
"validation": "https://raw.githubusercontent.com/IndoNLP/nusax/main/datasets/mt/valid.csv",
|
53 |
+
"test": "https://raw.githubusercontent.com/IndoNLP/nusax/main/datasets/mt/test.csv",
|
54 |
+
}
|
55 |
+
|
56 |
+
|
57 |
+
def nusantara_config_constructor(lang_source, lang_target, schema, version):
|
58 |
+
"""Construct NusantaraConfig with nusax_mt_{lang_source}_{lang_target}_{schema} as the name format"""
|
59 |
+
if schema != "source" and schema != "nusantara_t2t":
|
60 |
+
raise ValueError(f"Invalid schema: {schema}")
|
61 |
+
|
62 |
+
if lang_source == "" and lang_target == "":
|
63 |
+
return NusantaraConfig(
|
64 |
+
name="nusax_mt_{schema}".format(schema=schema),
|
65 |
+
version=datasets.Version(version),
|
66 |
+
description="nusax_mt with {schema} schema for all 132 language pairs".format(schema=schema),
|
67 |
+
schema=schema,
|
68 |
+
subset_id="nusax_mt",
|
69 |
+
)
|
70 |
+
else:
|
71 |
+
return NusantaraConfig(
|
72 |
+
name="nusax_mt_{lang_source}_{lang_target}_{schema}".format(lang_source=lang_source, lang_target=lang_target, schema=schema),
|
73 |
+
version=datasets.Version(version),
|
74 |
+
description="nusax_mt with {schema} schema for {lang_source} source language and {lang_target} target language".format(lang_source=lang_source, lang_target=lang_target, schema=schema),
|
75 |
+
schema=schema,
|
76 |
+
subset_id="nusax_mt",
|
77 |
+
)
|
78 |
+
|
79 |
+
|
80 |
+
LANGUAGES_MAP = {
|
81 |
+
"ace": "acehnese",
|
82 |
+
"ban": "balinese",
|
83 |
+
"bjn": "banjarese",
|
84 |
+
"bug": "buginese",
|
85 |
+
"eng": "english",
|
86 |
+
"ind": "indonesian",
|
87 |
+
"jav": "javanese",
|
88 |
+
"mad": "madurese",
|
89 |
+
"min": "minangkabau",
|
90 |
+
"nij": "ngaju",
|
91 |
+
"sun": "sundanese",
|
92 |
+
"bbc": "toba_batak",
|
93 |
+
}
|
94 |
+
|
95 |
+
|
96 |
+
class NusaXMT(datasets.GeneratorBasedBuilder):
|
97 |
+
"""NusaX-MT is a parallel corpus for training and benchmarking machine translation models across 10 Indonesian local languages + Indonesian and English. The data is presented in csv format with 12 columns, one column for each language."""
|
98 |
+
|
99 |
+
BUILDER_CONFIGS = (
|
100 |
+
[nusantara_config_constructor(lang1, lang2, "source", _SOURCE_VERSION) for lang1 in LANGUAGES_MAP for lang2 in LANGUAGES_MAP if lang1 != lang2]
|
101 |
+
+ [nusantara_config_constructor(lang1, lang2, "nusantara_t2t", _NUSANTARA_VERSION) for lang1 in LANGUAGES_MAP for lang2 in LANGUAGES_MAP if lang1 != lang2]
|
102 |
+
+ [nusantara_config_constructor("", "", "source", _SOURCE_VERSION), nusantara_config_constructor("", "", "nusantara_t2t", _NUSANTARA_VERSION)]
|
103 |
+
)
|
104 |
+
|
105 |
+
DEFAULT_CONFIG_NAME = "nusax_senti_ind_eng_source"
|
106 |
+
|
107 |
+
def _info(self) -> datasets.DatasetInfo:
|
108 |
+
if self.config.schema == "source" or self.config.schema == "nusantara_t2t":
|
109 |
+
features = schemas.text2text_features
|
110 |
+
else:
|
111 |
+
raise ValueError(f"Invalid config schema: {self.config.schema}")
|
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 |
+
train_csv_path = Path(dl_manager.download_and_extract(_URLS["train"]))
|
124 |
+
validation_csv_path = Path(dl_manager.download_and_extract(_URLS["validation"]))
|
125 |
+
test_csv_path = Path(dl_manager.download_and_extract(_URLS["test"]))
|
126 |
+
|
127 |
+
return [
|
128 |
+
datasets.SplitGenerator(
|
129 |
+
name=datasets.Split.TRAIN,
|
130 |
+
gen_kwargs={"filepath": train_csv_path},
|
131 |
+
),
|
132 |
+
datasets.SplitGenerator(
|
133 |
+
name=datasets.Split.VALIDATION,
|
134 |
+
gen_kwargs={"filepath": validation_csv_path},
|
135 |
+
),
|
136 |
+
datasets.SplitGenerator(
|
137 |
+
name=datasets.Split.TEST,
|
138 |
+
gen_kwargs={"filepath": test_csv_path},
|
139 |
+
),
|
140 |
+
]
|
141 |
+
|
142 |
+
def _generate_examples(self, filepath: Path) -> Tuple[int, Dict]:
|
143 |
+
if self.config.schema != "source" and self.config.schema != "nusantara_t2t":
|
144 |
+
raise ValueError(f"Invalid config schema: {self.config.schema}")
|
145 |
+
|
146 |
+
df = pd.read_csv(filepath).reset_index()
|
147 |
+
if self.config.name == "nusax_mt_source" or self.config.name == "nusax_mt_nusantara_t2t":
|
148 |
+
# load all 132 language pairs
|
149 |
+
id_count = -1
|
150 |
+
for lang_source in LANGUAGES_MAP:
|
151 |
+
for lang_target in LANGUAGES_MAP:
|
152 |
+
if lang_source == lang_target:
|
153 |
+
continue
|
154 |
+
|
155 |
+
for _, row in df.iterrows():
|
156 |
+
id_count += 1
|
157 |
+
ex = {
|
158 |
+
"id": str(id_count),
|
159 |
+
"text_1": row[LANGUAGES_MAP[lang_source]],
|
160 |
+
"text_2": row[LANGUAGES_MAP[lang_target]],
|
161 |
+
"text_1_name": lang_source,
|
162 |
+
"text_2_name": lang_target,
|
163 |
+
}
|
164 |
+
yield id_count, ex
|
165 |
+
|
166 |
+
else:
|
167 |
+
df = pd.read_csv(filepath).reset_index()
|
168 |
+
lang_source = self.config.name[9:12]
|
169 |
+
lang_target = self.config.name[13:16]
|
170 |
+
|
171 |
+
for index, row in df.iterrows():
|
172 |
+
ex = {
|
173 |
+
"id": str(index),
|
174 |
+
"text_1": row[LANGUAGES_MAP[lang_source]],
|
175 |
+
"text_2": row[LANGUAGES_MAP[lang_target]],
|
176 |
+
"text_1_name": lang_source,
|
177 |
+
"text_2_name": lang_target,
|
178 |
+
}
|
179 |
+
yield str(index), ex
|