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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from typing import Dict, List, Tuple
|
| 4 |
+
|
| 5 |
+
import datasets
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from seacrowd.utils import schemas
|
| 8 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
| 9 |
+
from seacrowd.utils.constants import Tasks, Licenses
|
| 10 |
+
|
| 11 |
+
_CITATION = """\
|
| 12 |
+
@misc{united1998universal,
|
| 13 |
+
title={The Universal Declaration of Human Rights, 1948-1998},
|
| 14 |
+
author={United Nations},
|
| 15 |
+
year={1998},
|
| 16 |
+
publisher={United Nations Dept. of Public Information New York}
|
| 17 |
+
}
|
| 18 |
+
"""
|
| 19 |
+
_DATASETNAME = "udhr"
|
| 20 |
+
_DESCRIPTION = """\
|
| 21 |
+
The Universal Declaration of Human Rights (UDHR) is a milestone document in the history of
|
| 22 |
+
human rights. Drafted by representatives with different legal and cultural backgrounds from
|
| 23 |
+
all regions of the world, it set out, for the first time, fundamental human rights to be
|
| 24 |
+
universally protected. The Declaration was adopted by the UN General Assembly in Paris on
|
| 25 |
+
10 December 1948 during its 183rd plenary meeting.
|
| 26 |
+
"""
|
| 27 |
+
_HOMEPAGE = "https://unicode.org/udhr/index.html"
|
| 28 |
+
_LICENSE = Licenses.UNKNOWN.value
|
| 29 |
+
_URLS = "https://unicode.org/udhr/assemblies/udhr_txt.zip"
|
| 30 |
+
|
| 31 |
+
_SUPPORTED_TASKS = [Tasks.SELF_SUPERVISED_PRETRAINING]
|
| 32 |
+
_SOURCE_VERSION = "1.0.0"
|
| 33 |
+
|
| 34 |
+
_SEACROWD_VERSION = "2024.06.20"
|
| 35 |
+
|
| 36 |
+
_LANGS = {
|
| 37 |
+
"ace": "Aceh",
|
| 38 |
+
"ban": "Bali",
|
| 39 |
+
"bcl": "Bicolano, Central",
|
| 40 |
+
"blt": "Tai Dam",
|
| 41 |
+
"bug": "Bugis",
|
| 42 |
+
"ceb": "Cebuano",
|
| 43 |
+
"cfm": "Chin, Falam", # flm
|
| 44 |
+
"cnh": "Chin, Haka",
|
| 45 |
+
"ctd": "Chin, Tedim",
|
| 46 |
+
"duu": "Drung",
|
| 47 |
+
"hil": "Hiligaynon",
|
| 48 |
+
"hlt": "Chin, Matu",
|
| 49 |
+
"hni": "Hani",
|
| 50 |
+
"hnj": "Hmong Njua", # blu
|
| 51 |
+
"ilo": "Ilocano",
|
| 52 |
+
"ind": "Indonesian",
|
| 53 |
+
"jav": "Javanese",
|
| 54 |
+
"jav_java": "Javanese (Javanese)",
|
| 55 |
+
"khm": "Khmer",
|
| 56 |
+
"kkh": "Khun",
|
| 57 |
+
"lao": "Lao",
|
| 58 |
+
"lus": "Mizo",
|
| 59 |
+
"mad": "Madura",
|
| 60 |
+
"min": "Minangkabau",
|
| 61 |
+
"mnw": "Mon",
|
| 62 |
+
"mya": "Burmese",
|
| 63 |
+
"pam": "Pampangan",
|
| 64 |
+
"shn": "Shan",
|
| 65 |
+
"sun": "Sunda",
|
| 66 |
+
"tdt": "Tetun Dili",
|
| 67 |
+
"tet": "Tetun",
|
| 68 |
+
"tgl": "Tagalog",
|
| 69 |
+
"tha": "Thai",
|
| 70 |
+
"vie": "Vietnamese",
|
| 71 |
+
"war": "Waray-waray",
|
| 72 |
+
"zlm": "Malay", # default mly_latn
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
_LOCAL=False
|
| 76 |
+
_LANGUAGES=["ace", "ban", "bcl", "blt", "bug", "ceb", "cfm", "cnh", "ctd", "duu", "hil", "hlt", "hni", "hnj", "ilo", "ind", "jav", "khm", "kkh", "lao", "lus", "mad", "min", "mnw", "mya", "pam", "shn", "sun", "tdt", "tet", "tgl", "tha", "vie", "war", "zlm"]
|
| 77 |
+
|
| 78 |
+
def seacrowd_config_constructor(src_lang, schema, version):
|
| 79 |
+
if src_lang == "":
|
| 80 |
+
raise ValueError(f"Invalid src_lang {src_lang}")
|
| 81 |
+
|
| 82 |
+
if schema not in ["source", "seacrowd_ssp"]:
|
| 83 |
+
raise ValueError(f"Invalid schema: {schema}")
|
| 84 |
+
|
| 85 |
+
return SEACrowdConfig(
|
| 86 |
+
name="udhr_{src}_{schema}".format(src=src_lang, schema=schema),
|
| 87 |
+
version=datasets.Version(version),
|
| 88 |
+
description="udhr {schema} schema for {src} language".format(schema=schema, src=_LANGS[src_lang]),
|
| 89 |
+
schema=schema,
|
| 90 |
+
subset_id="udhr_{src}".format(src=src_lang),
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
class UDHRDataset(datasets.GeneratorBasedBuilder):
|
| 95 |
+
"""
|
| 96 |
+
The Universal Declaration of Human Rights (UDHR) is a milestone document in the history of
|
| 97 |
+
human rights. Drafted by representatives with different legal and cultural backgrounds from
|
| 98 |
+
all regions of the world, it set out, for the first time, fundamental human rights to be
|
| 99 |
+
universally protected. The Declaration was adopted by the UN General Assembly in Paris on
|
| 100 |
+
10 December 1948 during its 183rd plenary meeting.
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
| 104 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
| 105 |
+
|
| 106 |
+
BUILDER_CONFIGS = [seacrowd_config_constructor(lang, "source", _SOURCE_VERSION) for lang in _LANGS] + [seacrowd_config_constructor(lang, "seacrowd_ssp", _SEACROWD_VERSION) for lang in _LANGS]
|
| 107 |
+
|
| 108 |
+
DEFAULT_CONFIG_NAME = "udhr_ind_source"
|
| 109 |
+
|
| 110 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 111 |
+
if self.config.schema == "source":
|
| 112 |
+
features = datasets.Features(
|
| 113 |
+
{
|
| 114 |
+
"id": datasets.Value("string"),
|
| 115 |
+
"text": datasets.Value("string"),
|
| 116 |
+
}
|
| 117 |
+
)
|
| 118 |
+
elif self.config.schema == "seacrowd_ssp":
|
| 119 |
+
features = schemas.self_supervised_pretraining.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 |
+
urls = _URLS
|
| 132 |
+
data_dir = dl_manager.download_and_extract(urls)
|
| 133 |
+
lang = self.config.subset_id.split("_")
|
| 134 |
+
file_key=""
|
| 135 |
+
if lang[1] == "zlm":
|
| 136 |
+
file_key = "mly_latn"
|
| 137 |
+
elif lang[1] == "cfm":
|
| 138 |
+
file_key = "flm"
|
| 139 |
+
elif lang[1] == "hnj":
|
| 140 |
+
file_key = "blu"
|
| 141 |
+
elif lang[1] == "kkh":
|
| 142 |
+
file_key = "kkh_lana"
|
| 143 |
+
elif lang[1] == "duu":
|
| 144 |
+
file_key = "020"
|
| 145 |
+
elif lang[1] == "tdt":
|
| 146 |
+
file_key = "010"
|
| 147 |
+
elif len(lang)>2 and f"{lang[1]}_{lang[2]}" == "jav_java":
|
| 148 |
+
file_key = "jav_java"
|
| 149 |
+
else:
|
| 150 |
+
file_key = lang[1]
|
| 151 |
+
|
| 152 |
+
return [
|
| 153 |
+
datasets.SplitGenerator(
|
| 154 |
+
name=datasets.Split.TRAIN,
|
| 155 |
+
gen_kwargs={
|
| 156 |
+
"filepath": os.path.join(data_dir, f"udhr_{file_key}.txt".format(file_key=file_key)),
|
| 157 |
+
"split": "train",
|
| 158 |
+
},
|
| 159 |
+
),
|
| 160 |
+
]
|
| 161 |
+
|
| 162 |
+
def _generate_examples(self, filepath: Path, split: str) -> Tuple[int, Dict]:
|
| 163 |
+
"""Yields examples as (key, example) tuples."""
|
| 164 |
+
data = []
|
| 165 |
+
|
| 166 |
+
with open(filepath, "r") as f:
|
| 167 |
+
data = [line.rstrip() for line in f.readlines()]
|
| 168 |
+
|
| 169 |
+
if self.config.schema == "source":
|
| 170 |
+
yield 0, {"id": Path(filepath).stem, "text": " ".join(data)}
|
| 171 |
+
|
| 172 |
+
elif self.config.schema == "seacrowd_ssp":
|
| 173 |
+
yield 0, {"id": Path(filepath).stem, "text": " ".join(data)}
|