File size: 6,035 Bytes
c3efdac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
from pathlib import Path
from typing import Dict, List, Tuple

import datasets
from pathlib import Path
from seacrowd.utils import schemas
from seacrowd.utils.configs import SEACrowdConfig
from seacrowd.utils.constants import Tasks, Licenses

_CITATION = """\
@misc{united1998universal,
    title={The Universal Declaration of Human Rights, 1948-1998},
    author={United Nations},
    year={1998},
    publisher={United Nations Dept. of Public Information New York}
}
"""
_DATASETNAME = "udhr"
_DESCRIPTION = """\
The Universal Declaration of Human Rights (UDHR) is a milestone document in the history of
human rights. Drafted by representatives with different legal and cultural backgrounds from
all regions of the world, it set out, for the first time, fundamental human rights to be
universally protected. The Declaration was adopted by the UN General Assembly in Paris on
10 December 1948 during its 183rd plenary meeting.
"""
_HOMEPAGE = "https://unicode.org/udhr/index.html"
_LICENSE = Licenses.UNKNOWN.value
_URLS = "https://unicode.org/udhr/assemblies/udhr_txt.zip"

_SUPPORTED_TASKS = [Tasks.SELF_SUPERVISED_PRETRAINING]
_SOURCE_VERSION = "1.0.0"

_SEACROWD_VERSION = "2024.06.20"

_LANGS = {
    "ace": "Aceh",
    "ban": "Bali",
    "bcl": "Bicolano, Central",
    "blt": "Tai Dam", 
    "bug": "Bugis",
    "ceb": "Cebuano",
    "cfm": "Chin, Falam",  # flm
    "cnh": "Chin, Haka",
    "ctd": "Chin, Tedim",
    "duu": "Drung", 
    "hil": "Hiligaynon", 
    "hlt": "Chin, Matu",
    "hni": "Hani",
    "hnj": "Hmong Njua",  # blu
    "ilo": "Ilocano", 
    "ind": "Indonesian",
    "jav": "Javanese",
    "jav_java": "Javanese (Javanese)",
    "khm": "Khmer",
    "kkh": "Khun", 
    "lao": "Lao",
    "lus": "Mizo",
    "mad": "Madura",
    "min": "Minangkabau", 
    "mnw": "Mon", 
    "mya": "Burmese",
    "pam": "Pampangan", 
    "shn": "Shan",
    "sun": "Sunda",
    "tdt": "Tetun Dili", 
    "tet": "Tetun", 
    "tgl": "Tagalog",
    "tha": "Thai",
    "vie": "Vietnamese",
    "war": "Waray-waray", 
    "zlm": "Malay",  # default mly_latn
}

_LOCAL=False
_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"]

def seacrowd_config_constructor(src_lang, schema, version):
    if src_lang == "":
        raise ValueError(f"Invalid src_lang {src_lang}")

    if schema not in ["source", "seacrowd_ssp"]:
        raise ValueError(f"Invalid schema: {schema}")

    return SEACrowdConfig(
        name="udhr_{src}_{schema}".format(src=src_lang, schema=schema),
        version=datasets.Version(version),
        description="udhr {schema} schema for {src} language".format(schema=schema, src=_LANGS[src_lang]),
        schema=schema,
        subset_id="udhr_{src}".format(src=src_lang),
    )


class UDHRDataset(datasets.GeneratorBasedBuilder):
    """
    The Universal Declaration of Human Rights (UDHR) is a milestone document in the history of
    human rights. Drafted by representatives with different legal and cultural backgrounds from
    all regions of the world, it set out, for the first time, fundamental human rights to be
    universally protected. The Declaration was adopted by the UN General Assembly in Paris on
    10 December 1948 during its 183rd plenary meeting.
    """

    SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
    SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)

    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]

    DEFAULT_CONFIG_NAME = "udhr_ind_source"

    def _info(self) -> datasets.DatasetInfo:
        if self.config.schema == "source":
            features = datasets.Features(
                {
                    "id": datasets.Value("string"),
                    "text": datasets.Value("string"),
                }
            )
        elif self.config.schema == "seacrowd_ssp":
            features = schemas.self_supervised_pretraining.features

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
        """Returns SplitGenerators."""
        urls = _URLS
        data_dir = dl_manager.download_and_extract(urls)
        lang = self.config.subset_id.split("_")
        file_key=""
        if lang[1] == "zlm":
            file_key = "mly_latn"
        elif lang[1] == "cfm":
            file_key = "flm"
        elif lang[1] == "hnj":
            file_key = "blu"        
        elif lang[1] == "kkh":
            file_key = "kkh_lana"       
        elif lang[1] == "duu":
            file_key = "020"
        elif lang[1] == "tdt":
            file_key = "010"
        elif len(lang)>2 and f"{lang[1]}_{lang[2]}" == "jav_java":
            file_key = "jav_java"
        else:
            file_key = lang[1]

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": os.path.join(data_dir, f"udhr_{file_key}.txt".format(file_key=file_key)),
                    "split": "train",
                },
            ),
        ]

    def _generate_examples(self, filepath: Path, split: str) -> Tuple[int, Dict]:
        """Yields examples as (key, example) tuples."""
        data = []

        with open(filepath, "r") as f:
            data = [line.rstrip() for line in f.readlines()]

        if self.config.schema == "source":
            yield 0, {"id": Path(filepath).stem, "text": " ".join(data)}

        elif self.config.schema == "seacrowd_ssp":
            yield 0, {"id": Path(filepath).stem, "text": " ".join(data)}