BramVanroy
commited on
Commit
•
6430350
1
Parent(s):
64f799e
add loader file
Browse files- hplt_monolingual_v1_2.py +197 -0
hplt_monolingual_v1_2.py
ADDED
@@ -0,0 +1,197 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Data loading script for HPLT Monolingual Release v1.2."""
|
2 |
+
|
3 |
+
import json
|
4 |
+
from pathlib import Path
|
5 |
+
from typing import List
|
6 |
+
|
7 |
+
import datasets
|
8 |
+
|
9 |
+
_DESCRIPTION = """\
|
10 |
+
Data release 1.2 of the monolingual portion of HPLT (December 2023)
|
11 |
+
|
12 |
+
There are 75 languages in this release (22 TB of raw files, 11 TB of deduped files and 8.4 TB of clean files) provided as JSONL files compressed with zstd. For convenience, data is split into multiple shards, a few GB each. The number of shards per language depends on the size of the specific corpus.
|
13 |
+
"""
|
14 |
+
|
15 |
+
_HOMEPAGE = "https://hplt-project.org/"
|
16 |
+
|
17 |
+
_LICENSE = "CC0"
|
18 |
+
|
19 |
+
_PROCTYPE2URL = {
|
20 |
+
"": "https://data.hplt-project.org/one/monotext/{code}_map.txt",
|
21 |
+
"deduplicated": "https://data.hplt-project.org/one/monotext/deduplicated/{code}_map.txt",
|
22 |
+
"cleaned": "https://data.hplt-project.org/one/monotext/cleaned/{code}_map.txt",
|
23 |
+
}
|
24 |
+
|
25 |
+
_CODE2LANG = {
|
26 |
+
"af": "Afrikaans",
|
27 |
+
"ar": "Arabic",
|
28 |
+
"az": "Azerbaijani",
|
29 |
+
"be": "Belarusian",
|
30 |
+
"bg": "Bulgarian",
|
31 |
+
"bn": "Bangla",
|
32 |
+
"ca": "Catalan",
|
33 |
+
"cs": "Czech",
|
34 |
+
"cy": "Welsh",
|
35 |
+
"da": "Danish",
|
36 |
+
"de": "German",
|
37 |
+
"el": "Greek",
|
38 |
+
"en": "English",
|
39 |
+
"eo": "Esperanto",
|
40 |
+
"es": "Spanish",
|
41 |
+
"et": "Estonian",
|
42 |
+
"eu": "Basque",
|
43 |
+
"fa": "Persian",
|
44 |
+
"fi": "Finnish",
|
45 |
+
"fr": "French",
|
46 |
+
"ga": "Irish",
|
47 |
+
"gl": "Galician",
|
48 |
+
"gu": "Gujarati",
|
49 |
+
"hbs": "Serbo-Croatian",
|
50 |
+
"he": "Hebrew",
|
51 |
+
"hi": "Hindi",
|
52 |
+
"hu": "Hungarian",
|
53 |
+
"hy": "Armenian",
|
54 |
+
"id": "Indonesian",
|
55 |
+
"is": "Icelandic",
|
56 |
+
"it": "Italian",
|
57 |
+
"ja": "Japanese",
|
58 |
+
"ka": "Georgian",
|
59 |
+
"kk": "Kazakh",
|
60 |
+
"kn": "Kannada",
|
61 |
+
"ko": "Korean",
|
62 |
+
"ky": "Kyrgyz",
|
63 |
+
"la": "Latin",
|
64 |
+
"lt": "Lithuanian",
|
65 |
+
"lv": "Latvian",
|
66 |
+
"mk": "Macedonian",
|
67 |
+
"ml": "Malayalam",
|
68 |
+
"mn": "Mongolian",
|
69 |
+
"mr": "Marathi",
|
70 |
+
"ms": "Malay",
|
71 |
+
"mt": "Maltese",
|
72 |
+
"my": "Burmese",
|
73 |
+
"nb": "Norwegian Bokmål",
|
74 |
+
"ne": "Nepali",
|
75 |
+
"nl": "Dutch",
|
76 |
+
"nn": "Norwegian Nynorsk",
|
77 |
+
"pa": "Punjabi",
|
78 |
+
"pl": "Polish",
|
79 |
+
"ps": "Pashto",
|
80 |
+
"pt": "Portuguese",
|
81 |
+
"ro": "Romanian",
|
82 |
+
"ru": "Russian",
|
83 |
+
"si": "Sinhala",
|
84 |
+
"sk": "Slovak",
|
85 |
+
"sl": "Slovenian",
|
86 |
+
"so": "Somali",
|
87 |
+
"sq": "Albanian",
|
88 |
+
"sv": "Swedish",
|
89 |
+
"sw": "Swahili",
|
90 |
+
"ta": "Tamil",
|
91 |
+
"te": "Telugu",
|
92 |
+
"th": "Thai",
|
93 |
+
"tl": "Filipino",
|
94 |
+
"tr": "Turkish",
|
95 |
+
"tt": "Tatar",
|
96 |
+
"uk": "Ukrainian",
|
97 |
+
"ur": "Urdu",
|
98 |
+
"uz": "Uzbek",
|
99 |
+
"vi": "Vietnamese",
|
100 |
+
"zh": "Chinese",
|
101 |
+
}
|
102 |
+
|
103 |
+
_SUPPORTED_LANG_STR = "* " + "\n* ".join([f"{code}: {lang}" for code, lang in _CODE2LANG.items()])
|
104 |
+
|
105 |
+
_VERSION = "1.2.0"
|
106 |
+
|
107 |
+
|
108 |
+
class Hplt_monolingual_v1_2(datasets.GeneratorBasedBuilder):
|
109 |
+
"""Release v1.2 of the HPLT monolingual datasets."""
|
110 |
+
|
111 |
+
VERSION = datasets.Version(_VERSION)
|
112 |
+
|
113 |
+
BUILDER_CONFIGS = [
|
114 |
+
datasets.BuilderConfig(
|
115 |
+
name=f"{code}_{proc_type}" if proc_type else code,
|
116 |
+
version=datasets.Version(_VERSION),
|
117 |
+
description=f"{lang}{' ('+proc_type+')' if proc_type else ''} - HPLT {_VERSION}",
|
118 |
+
)
|
119 |
+
for code, lang in _CODE2LANG.items()
|
120 |
+
for proc_type in _PROCTYPE2URL
|
121 |
+
]
|
122 |
+
|
123 |
+
def _info(self):
|
124 |
+
features = datasets.Features(
|
125 |
+
{
|
126 |
+
"id": datasets.Value("int64"),
|
127 |
+
"document_lang": datasets.Value("string"),
|
128 |
+
"scores": datasets.Sequence(datasets.Value("float64")),
|
129 |
+
"langs": datasets.Sequence(datasets.Value("string")),
|
130 |
+
"text": datasets.Value("string"),
|
131 |
+
"url": datasets.Value("string"),
|
132 |
+
"collection": datasets.Value("string"),
|
133 |
+
}
|
134 |
+
)
|
135 |
+
|
136 |
+
return datasets.DatasetInfo(
|
137 |
+
description=_DESCRIPTION,
|
138 |
+
features=features,
|
139 |
+
homepage=_HOMEPAGE,
|
140 |
+
license=_LICENSE,
|
141 |
+
)
|
142 |
+
|
143 |
+
def _split_generators(self, dl_manager):
|
144 |
+
error_msg = (
|
145 |
+
"You can choose any of the supported language codes as-is, or the `deduplicated` or `cleaned` versions,"
|
146 |
+
f" e.g. nl, nl_deduplicated, nl_cleaned.\nSupported languages are:\n{_SUPPORTED_LANG_STR}"
|
147 |
+
)
|
148 |
+
# Get the language code and type (default emptry string ``, `deduplicated`, or `cleaned`)
|
149 |
+
try:
|
150 |
+
if "_" in self.config.name:
|
151 |
+
langcode, proctype = self.config.name.split("_", 1)
|
152 |
+
url = _PROCTYPE2URL[proctype].format(code=langcode)
|
153 |
+
else:
|
154 |
+
url = _PROCTYPE2URL[""].format(code=self.config.name)
|
155 |
+
except Exception as exc:
|
156 |
+
raise ValueError(f"Error reading the entered config '{self.config.name}'. {error_msg}") from exc
|
157 |
+
|
158 |
+
try:
|
159 |
+
pf_json_list = Path(dl_manager.download(url)).resolve()
|
160 |
+
except Exception as exc:
|
161 |
+
raise KeyError(
|
162 |
+
f"Error downloading JSONL overview. This may indicate a problem with the server OR you entered an"
|
163 |
+
f" unsupported language.\n{error_msg}"
|
164 |
+
) from exc
|
165 |
+
|
166 |
+
data_urls = [
|
167 |
+
lstrip for line in pf_json_list.read_text(encoding="utf-8").splitlines() if (lstrip := line.strip())
|
168 |
+
]
|
169 |
+
pfs_data = [Path(url) for url in dl_manager.download_and_extract(data_urls)]
|
170 |
+
return [
|
171 |
+
datasets.SplitGenerator(
|
172 |
+
name=datasets.Split.TRAIN,
|
173 |
+
gen_kwargs={
|
174 |
+
"pfs_data": pfs_data,
|
175 |
+
},
|
176 |
+
)
|
177 |
+
]
|
178 |
+
|
179 |
+
def _generate_examples(self, pfs_data: List[Path]):
|
180 |
+
# Apparently a unique key for each sample is recommended for legacy tfds reasons
|
181 |
+
# https://github.com/huggingface/datasets/blob/e52f4d0cf1cb89a9719b42fff13a891f92d51e04/templates/new_dataset_script.py#L156
|
182 |
+
document_idx = 1
|
183 |
+
|
184 |
+
for pfin in pfs_data:
|
185 |
+
with pfin.open(encoding="utf-8") as fhin:
|
186 |
+
for line in fhin:
|
187 |
+
data = json.loads(line)
|
188 |
+
yield document_idx, {
|
189 |
+
"id": document_idx,
|
190 |
+
"document_lang": data["document_lang"],
|
191 |
+
"scores": data["scores"],
|
192 |
+
"langs": data["langs"],
|
193 |
+
"text": data["text"],
|
194 |
+
"url": data["url"],
|
195 |
+
"collection": data["collection"],
|
196 |
+
}
|
197 |
+
document_idx += 1
|