Upload bhinneka_korpus.py with huggingface_hub
Browse files- bhinneka_korpus.py +141 -0
bhinneka_korpus.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
from typing import Dict, List, Tuple
|
| 3 |
+
|
| 4 |
+
import datasets
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
| 8 |
+
from seacrowd.utils.constants import Licenses, Tasks
|
| 9 |
+
from seacrowd.utils import schemas
|
| 10 |
+
|
| 11 |
+
_CITATION = """\
|
| 12 |
+
@misc{lopo2024constructing,
|
| 13 |
+
title={Constructing and Expanding Low-Resource and Underrepresented Parallel Datasets for Indonesian Local Languages},
|
| 14 |
+
author={Joanito Agili Lopo and Radius Tanone},
|
| 15 |
+
year={2024},
|
| 16 |
+
eprint={2404.01009},
|
| 17 |
+
archivePrefix={arXiv},
|
| 18 |
+
primaryClass={cs.CL}
|
| 19 |
+
}
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
_DATASETNAME = "bhinneka_korpus"
|
| 23 |
+
_DESCRIPTION = """The Bhinneka Korpus dataset was parallel dataset for five Indonesian Local Languages conducted
|
| 24 |
+
through a volunteer-driven translation strategy, encompassing sentences in the Indonesian-English pairs and lexical
|
| 25 |
+
terms. The dataset consist of parallel data with 16,000 sentences in total, details with 4,000 sentence pairs for two
|
| 26 |
+
Indonesia local language and approximately 3,000 sentences for other languages, and one lexicon dataset creation for
|
| 27 |
+
Beaye language. In addition, since beaye is a undocumented language, we don't have any information yet about the use
|
| 28 |
+
of language code. Therefore, we used "day" (a code for land dayak language family) to represent the language."""
|
| 29 |
+
|
| 30 |
+
_HOMEPAGE = "https://github.com/joanitolopo/bhinneka-korpus"
|
| 31 |
+
_LICENSE = Licenses.APACHE_2_0.value
|
| 32 |
+
_URLS = "https://raw.githubusercontent.com/joanitolopo/bhinneka-korpus/main/"
|
| 33 |
+
_SUPPORTED_TASKS = [Tasks.MACHINE_TRANSLATION]
|
| 34 |
+
_SOURCE_VERSION = "1.0.0"
|
| 35 |
+
_SEACROWD_VERSION = "2024.06.20"
|
| 36 |
+
|
| 37 |
+
_LANGUAGES = ["abs", "aoz", "day", "mak", "mkn"]
|
| 38 |
+
LANGUAGES_TO_FILENAME_MAP = {
|
| 39 |
+
"abs": "ambonese-malay",
|
| 40 |
+
"aoz": "uab-meto",
|
| 41 |
+
"day": "beaye",
|
| 42 |
+
"mak": "makassarese",
|
| 43 |
+
"mkn": "kupang-malay",
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
_LOCAL = False
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
class BhinnekaKorpusDataset(datasets.GeneratorBasedBuilder):
|
| 50 |
+
"""A Collection of Multilingual Parallel Datasets for 5 Indonesian Local Languages."""
|
| 51 |
+
|
| 52 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
| 53 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
| 54 |
+
SEACROWD_SCHEMA_NAME = "t2t"
|
| 55 |
+
|
| 56 |
+
dataset_names = sorted([f"{_DATASETNAME}_{lang}" for lang in _LANGUAGES])
|
| 57 |
+
BUILDER_CONFIGS = []
|
| 58 |
+
for name in dataset_names:
|
| 59 |
+
source_config = SEACrowdConfig(
|
| 60 |
+
name=f"{name}_source",
|
| 61 |
+
version=SOURCE_VERSION,
|
| 62 |
+
description=f"{_DATASETNAME} source schema",
|
| 63 |
+
schema="source",
|
| 64 |
+
subset_id=name
|
| 65 |
+
)
|
| 66 |
+
BUILDER_CONFIGS.append(source_config)
|
| 67 |
+
seacrowd_config = SEACrowdConfig(
|
| 68 |
+
name=f"{name}_seacrowd_{SEACROWD_SCHEMA_NAME}",
|
| 69 |
+
version=SEACROWD_VERSION,
|
| 70 |
+
description=f"{_DATASETNAME} SEACrowd schema",
|
| 71 |
+
schema=f"seacrowd_{SEACROWD_SCHEMA_NAME}",
|
| 72 |
+
subset_id=name
|
| 73 |
+
)
|
| 74 |
+
BUILDER_CONFIGS.append(seacrowd_config)
|
| 75 |
+
|
| 76 |
+
DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_day_source"
|
| 77 |
+
|
| 78 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 79 |
+
schema = self.config.schema
|
| 80 |
+
features = datasets.Features(
|
| 81 |
+
{
|
| 82 |
+
"source_sentence": datasets.Value("string"),
|
| 83 |
+
"target_sentence": datasets.Value("string"),
|
| 84 |
+
"source_lang": datasets.Value("string"),
|
| 85 |
+
"target_lang": datasets.Value("string")
|
| 86 |
+
} if schema == "source" else schemas.text2text_features
|
| 87 |
+
if schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}" else None
|
| 88 |
+
)
|
| 89 |
+
if features is None:
|
| 90 |
+
raise ValueError("Invalid config schema")
|
| 91 |
+
|
| 92 |
+
return datasets.DatasetInfo(
|
| 93 |
+
description=_DESCRIPTION,
|
| 94 |
+
features=features,
|
| 95 |
+
homepage=_HOMEPAGE,
|
| 96 |
+
license=_LICENSE,
|
| 97 |
+
citation=_CITATION,
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
| 101 |
+
"""Returns SplitGenerators."""
|
| 102 |
+
data_dir = []
|
| 103 |
+
lang = self.config.name.split("_")[2]
|
| 104 |
+
if lang in _LANGUAGES:
|
| 105 |
+
data_dir.append(Path(dl_manager.download(_URLS + f"{LANGUAGES_TO_FILENAME_MAP[lang]}/{lang}.xlsx")))
|
| 106 |
+
else:
|
| 107 |
+
raise ValueError("Invalid language name")
|
| 108 |
+
return [
|
| 109 |
+
datasets.SplitGenerator(
|
| 110 |
+
name=datasets.Split.TRAIN,
|
| 111 |
+
gen_kwargs={
|
| 112 |
+
"filepath": data_dir[0],
|
| 113 |
+
"split": "train",
|
| 114 |
+
"language": lang
|
| 115 |
+
}
|
| 116 |
+
)
|
| 117 |
+
]
|
| 118 |
+
|
| 119 |
+
def _generate_examples(self, filepath: Path, split: str, language: str) -> Tuple[int, Dict]:
|
| 120 |
+
"""Yields examples as (key, example) tuples."""
|
| 121 |
+
dfs = pd.read_excel(filepath, index_col=0, engine="openpyxl")
|
| 122 |
+
source_sents = dfs["ind"]
|
| 123 |
+
target_sents = dfs[language]
|
| 124 |
+
|
| 125 |
+
for idx, (source, target) in enumerate(zip(source_sents.values, target_sents.values)):
|
| 126 |
+
if self.config.schema == "source":
|
| 127 |
+
example = {
|
| 128 |
+
"source_sentence": source,
|
| 129 |
+
"target_sentence": target,
|
| 130 |
+
"source_lang": "ind",
|
| 131 |
+
"target_lang": language
|
| 132 |
+
}
|
| 133 |
+
elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
|
| 134 |
+
example = {
|
| 135 |
+
"id": str(idx),
|
| 136 |
+
"text_1": source,
|
| 137 |
+
"text_2": target,
|
| 138 |
+
"text_1_name": "ind",
|
| 139 |
+
"text_2_name": language,
|
| 140 |
+
}
|
| 141 |
+
yield idx, example
|