Upload dcad_2000.py
Browse files- dcad_2000.py +128 -0
dcad_2000.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import datasets
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
_CITATION = ""
|
| 7 |
+
_DESCRIPTION = "DCAD-2000 flexible loader supporting 2000+ languages without defining configs."
|
| 8 |
+
_HOMEPAGE = ""
|
| 9 |
+
_LICENSE = ""
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class DCAD2000Config(datasets.BuilderConfig):
|
| 13 |
+
"""Flexible BuilderConfig for arbitrary language name."""
|
| 14 |
+
def __init__(self, **kwargs):
|
| 15 |
+
super().__init__(version=datasets.Version("1.0.0"), **kwargs)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class DCAD2000(datasets.GeneratorBasedBuilder):
|
| 19 |
+
BUILDER_CONFIG_CLASS = DCAD2000Config
|
| 20 |
+
|
| 21 |
+
def _info(self):
|
| 22 |
+
return datasets.DatasetInfo(
|
| 23 |
+
description=_DESCRIPTION,
|
| 24 |
+
features=datasets.Features({
|
| 25 |
+
"text": datasets.Value("string"),
|
| 26 |
+
"source_file": datasets.Value("string"),
|
| 27 |
+
}),
|
| 28 |
+
citation=_CITATION,
|
| 29 |
+
homepage=_HOMEPAGE,
|
| 30 |
+
license=_LICENSE,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
@staticmethod
|
| 34 |
+
def _get_available_languages(data_dir):
|
| 35 |
+
"""Automatically detect all language folders."""
|
| 36 |
+
if not os.path.isdir(data_dir):
|
| 37 |
+
return []
|
| 38 |
+
return [
|
| 39 |
+
d for d in os.listdir(data_dir)
|
| 40 |
+
if os.path.isdir(os.path.join(data_dir, d))
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
@classmethod
|
| 44 |
+
def _builder_configs(cls):
|
| 45 |
+
"""
|
| 46 |
+
Dynamically create configs for any language folder in repo.
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
data_dir = os.getenv("DATASETS_DATA_DIR") # Set by HF automatically
|
| 50 |
+
if data_dir is None:
|
| 51 |
+
# No detection possible (run-time local load)
|
| 52 |
+
return []
|
| 53 |
+
|
| 54 |
+
langs = cls._get_available_languages(data_dir)
|
| 55 |
+
|
| 56 |
+
return [
|
| 57 |
+
DCAD2000Config(name=lang, description=f"Language folder: {lang}")
|
| 58 |
+
for lang in langs
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
def _split_generators(self, dl_manager):
|
| 62 |
+
"""
|
| 63 |
+
Supports only split='keep' → load files containing 'keep'
|
| 64 |
+
"""
|
| 65 |
+
data_dir = dl_manager.download_and_extract(self.config.data_dir)
|
| 66 |
+
|
| 67 |
+
lang_dir = os.path.join(data_dir, self.config.name)
|
| 68 |
+
|
| 69 |
+
if not os.path.isdir(lang_dir):
|
| 70 |
+
raise FileNotFoundError(f"Language directory not found: {lang_dir}")
|
| 71 |
+
|
| 72 |
+
split = self.config.split
|
| 73 |
+
if split != "keep":
|
| 74 |
+
raise ValueError(f"Only split='keep' is supported. Got {split}")
|
| 75 |
+
|
| 76 |
+
files = [
|
| 77 |
+
os.path.join(lang_dir, f)
|
| 78 |
+
for f in os.listdir(lang_dir)
|
| 79 |
+
if "keep" in f
|
| 80 |
+
]
|
| 81 |
+
|
| 82 |
+
return [
|
| 83 |
+
datasets.SplitGenerator(
|
| 84 |
+
name=split,
|
| 85 |
+
gen_kwargs={"files": files},
|
| 86 |
+
)
|
| 87 |
+
]
|
| 88 |
+
|
| 89 |
+
def _generate_examples(self, files):
|
| 90 |
+
idx = 0
|
| 91 |
+
for path in files:
|
| 92 |
+
filename = os.path.basename(path)
|
| 93 |
+
ext = filename.lower()
|
| 94 |
+
|
| 95 |
+
if ext.endswith(".txt"):
|
| 96 |
+
with open(path, encoding="utf-8") as f:
|
| 97 |
+
for line in f:
|
| 98 |
+
line = line.strip()
|
| 99 |
+
if line:
|
| 100 |
+
yield idx, {"text": line, "source_file": filename}
|
| 101 |
+
idx += 1
|
| 102 |
+
|
| 103 |
+
elif ext.endswith(".jsonl"):
|
| 104 |
+
with open(path, encoding="utf-8") as f:
|
| 105 |
+
for line in f:
|
| 106 |
+
obj = json.loads(line)
|
| 107 |
+
yield idx, {
|
| 108 |
+
"text": obj.get("text", ""),
|
| 109 |
+
"source_file": filename
|
| 110 |
+
}
|
| 111 |
+
idx += 1
|
| 112 |
+
|
| 113 |
+
elif ext.endswith(".json"):
|
| 114 |
+
with open(path, encoding="utf-8") as f:
|
| 115 |
+
obj = json.load(f)
|
| 116 |
+
if isinstance(obj, list):
|
| 117 |
+
for item in obj:
|
| 118 |
+
yield idx, {
|
| 119 |
+
"text": item.get("text", ""),
|
| 120 |
+
"source_file": filename
|
| 121 |
+
}
|
| 122 |
+
idx += 1
|
| 123 |
+
elif isinstance(obj, dict):
|
| 124 |
+
yield idx, {
|
| 125 |
+
"text": obj.get("text", ""),
|
| 126 |
+
"source_file": filename
|
| 127 |
+
}
|
| 128 |
+
idx += 1
|