Upload glotstorybook.py with huggingface_hub
Browse files- glotstorybook.py +143 -0
glotstorybook.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 Tasks, Licenses, TASK_TO_SCHEMA, SCHEMA_TO_FEATURES
|
| 9 |
+
|
| 10 |
+
_CITATION = """\
|
| 11 |
+
@inproceedings{kargaran2023glotlid,
|
| 12 |
+
title = {{GlotLID: Language Identification for Low-Resource Languages}},
|
| 13 |
+
author = {Kargaran, Amir Hossein and Imani, Ayyoob and Yvon, Fran{\c{c}}ois and Sch{\"u}tze, Hinrich},
|
| 14 |
+
year = 2023,
|
| 15 |
+
booktitle = {The 2023 Conference on Empirical Methods in Natural Language Processing},
|
| 16 |
+
url = {https://openreview.net/forum?id=dl4e3EBz5j}
|
| 17 |
+
}
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
_DATASETNAME = "glotstorybook"
|
| 21 |
+
_DESCRIPTION = """\
|
| 22 |
+
The GlotStoryBook dataset is a compilation of children's storybooks from the Global
|
| 23 |
+
Storybooks project, encompassing 174 languages organized for machine translation tasks. It
|
| 24 |
+
features rows containing the text segment (text number), the language code, and the file
|
| 25 |
+
name, which corresponds to the specific book and story segment. This structure allows for
|
| 26 |
+
the comparison of texts across different languages by matching file names and text numbers
|
| 27 |
+
between rows.
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
_HOMEPAGE = "https://huggingface.co/datasets/cis-lmu/GlotStoryBook"
|
| 31 |
+
_LICENSE = f"""{Licenses.OTHERS.value} | \
|
| 32 |
+
We do not own any of the text from which these data has been extracted. All the files are
|
| 33 |
+
collected from the repository located at https://github.com/global-asp/. The source
|
| 34 |
+
repository for each text and file is stored in the dataset. Each file in the dataset is
|
| 35 |
+
associated with one license from the CC family. The licenses include 'CC BY', 'CC BY-NC',
|
| 36 |
+
'CC BY-NC-SA', 'CC-BY', 'CC-BY-NC', and 'Public Domain'. We also license the code, actual
|
| 37 |
+
packaging and the metadata of these data under the cc0-1.0.
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
_LOCAL=False
|
| 41 |
+
_LANGUAGES = ["khg", "khm", "mya", "tet", "tha", "vie"]
|
| 42 |
+
|
| 43 |
+
_URLS = "https://huggingface.co/datasets/cis-lmu/GlotStoryBook/resolve/main/GlotStoryBook.csv"
|
| 44 |
+
|
| 45 |
+
_SUPPORTED_TASKS = [Tasks.SELF_SUPERVISED_PRETRAINING]
|
| 46 |
+
_SOURCE_VERSION = "1.0.0"
|
| 47 |
+
_SEACROWD_VERSION = "2024.06.20"
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class GlotStoryBookDataset(datasets.GeneratorBasedBuilder):
|
| 51 |
+
"""Compilation of storybooks from the Global Storybooks project"""
|
| 52 |
+
|
| 53 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
| 54 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
| 55 |
+
|
| 56 |
+
SEACROWD_SCHEMA_NAME = TASK_TO_SCHEMA[_SUPPORTED_TASKS[0]].lower()
|
| 57 |
+
|
| 58 |
+
BUILDER_CONFIGS = [
|
| 59 |
+
SEACrowdConfig(
|
| 60 |
+
name=f"{_DATASETNAME}_source",
|
| 61 |
+
version=SOURCE_VERSION,
|
| 62 |
+
description=f"{_DATASETNAME} source schema",
|
| 63 |
+
schema="source",
|
| 64 |
+
subset_id=f"{_DATASETNAME}",
|
| 65 |
+
),
|
| 66 |
+
SEACrowdConfig(
|
| 67 |
+
name=f"{_DATASETNAME}_seacrowd_{SEACROWD_SCHEMA_NAME}",
|
| 68 |
+
version=SEACROWD_VERSION,
|
| 69 |
+
description=f"{_DATASETNAME} SEACrowd schema",
|
| 70 |
+
schema=f"seacrowd_{SEACROWD_SCHEMA_NAME}",
|
| 71 |
+
subset_id=f"{_DATASETNAME}",
|
| 72 |
+
),
|
| 73 |
+
]
|
| 74 |
+
|
| 75 |
+
DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"
|
| 76 |
+
|
| 77 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 78 |
+
if self.config.schema == "source":
|
| 79 |
+
features = datasets.Features(
|
| 80 |
+
{
|
| 81 |
+
"text": datasets.Value("string"),
|
| 82 |
+
"text_number": datasets.Value("int64"),
|
| 83 |
+
"license": datasets.Value("string"),
|
| 84 |
+
"text_by": datasets.Value("string"),
|
| 85 |
+
"translation_by": datasets.Value("string"),
|
| 86 |
+
"language": datasets.Value("string"),
|
| 87 |
+
"file_name": datasets.Value("string"),
|
| 88 |
+
"source": datasets.Value("string"),
|
| 89 |
+
"iso639-3": datasets.Value("string"),
|
| 90 |
+
"script": datasets.Value("string"),
|
| 91 |
+
}
|
| 92 |
+
)
|
| 93 |
+
elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
|
| 94 |
+
features = SCHEMA_TO_FEATURES[self.SEACROWD_SCHEMA_NAME.upper()]
|
| 95 |
+
|
| 96 |
+
return datasets.DatasetInfo(
|
| 97 |
+
description=_DESCRIPTION,
|
| 98 |
+
features=features,
|
| 99 |
+
homepage=_HOMEPAGE,
|
| 100 |
+
license=_LICENSE,
|
| 101 |
+
citation=_CITATION,
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
| 105 |
+
data_path = Path(dl_manager.download_and_extract(_URLS))
|
| 106 |
+
|
| 107 |
+
return [
|
| 108 |
+
datasets.SplitGenerator(
|
| 109 |
+
name=datasets.Split.TRAIN,
|
| 110 |
+
gen_kwargs={
|
| 111 |
+
"filepath": data_path,
|
| 112 |
+
"split": "train",
|
| 113 |
+
},
|
| 114 |
+
),
|
| 115 |
+
]
|
| 116 |
+
|
| 117 |
+
def _generate_examples(self, filepath: Path, split: str) -> Tuple[int, Dict]:
|
| 118 |
+
"""Yields examples as (key, example) tuples."""
|
| 119 |
+
df = pd.read_csv(filepath)
|
| 120 |
+
df = df[df["ISO639-3"].isin(_LANGUAGES)]
|
| 121 |
+
|
| 122 |
+
if self.config.schema == "source":
|
| 123 |
+
for i, row in df.iterrows():
|
| 124 |
+
yield i, {
|
| 125 |
+
"text": row["Text"],
|
| 126 |
+
"text_number": row["Text Number"],
|
| 127 |
+
"license": row["License"],
|
| 128 |
+
"text_by": row["Text By"],
|
| 129 |
+
"translation_by": row["Translation By"],
|
| 130 |
+
"language": row["Language"],
|
| 131 |
+
"file_name": row["File Name"],
|
| 132 |
+
"source": row["Source"],
|
| 133 |
+
"iso639-3": row["ISO639-3"],
|
| 134 |
+
"script": row["Script"],
|
| 135 |
+
}
|
| 136 |
+
elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
|
| 137 |
+
df = df.sort_values(by=["ISO639-3", "Source", "File Name", "Text Number"])
|
| 138 |
+
df = df.groupby(["ISO639-3", "Source", "File Name"]).agg({"Text": " ".join}).reset_index()
|
| 139 |
+
for i, row in df.iterrows():
|
| 140 |
+
yield i, {
|
| 141 |
+
"id": str(i),
|
| 142 |
+
"text": row["Text"],
|
| 143 |
+
}
|