Update common-corpus.py
Browse files4 unmodified lines56788910111213131415161718191 unmodified line2122232324242526272829303132333435363738293940414233343536434445464748495051525338394041424344545556575859606162634 unmodified linesclass CommonCorpus(GeneratorBasedBuilder): VERSION = datasets.Version("1.0.0") BUILDER_CONFIGS = [ datasets.BuilderConfig(name="bytelevel", version=VERSION, description="Bytelevel corpus data"), datasets.BuilderConfig(name="default", version=VERSION, description="Common Corpus dataset"), ]
def _info(self): return DatasetInfo( description="Common Corpus from InfoTokenizers - bytelevel config", description="Common Corpus from InfoTokenizers with multiple subfolders", features=Features({ "text": Value("string"), "source": Value("string"), }), supervised_keys=None, homepage="https://huggingface.co/datasets/InfoTokenizers/common-corpus",1 unmodified line )
def _split_generators(self, dl_manager): # Use the local extracted path to dataset files data_dir = os.path.join(dl_manager.manual_dir, "bytelevel") # Downloaded and extracted folder path data_dir = dl_manager.download_and_extract("https://huggingface.co/datasets/InfoTokenizers/common-corpus/resolve/main/")
# List all subdirectories subdirs = ["bytelevel", "bytelevel-llm-data", "bytelevel-subset", "bytelevel-subset_1", "bytelevel-subset_2"]
# Check existence of each subdir valid_dirs = [os.path.join(data_dir, subdir) for subdir in subdirs if os.path.exists(os.path.join(data_dir, subdir))]
if not valid_dirs: raise FileNotFoundError(f"No valid data directories found in {data_dir}. Expected one of: {subdirs}")
return [ SplitGenerator( name=Split.TRAIN, gen_kwargs={"data_dir": data_dir}, gen_kwargs={"data_dirs": valid_dirs}, ), ]
def _generate_examples(self, data_dir): # Iterate over all .txt files inside bytelevel folder file_paths = [os.path.join(data_dir, f) for f in os.listdir(data_dir) if f.endswith(".txt")]
def _generate_examples(self, data_dirs): example_id = 0
for data_dir in data_dirs: subdir_name = os.path.basename(data_dir)
# Iterate over all .txt files in the directory for root, _, files in os.walk(data_dir): for filename in files: if filename.endswith(".txt"): file_path = os.path.join(root, filename) for file_path in file_paths: with open(file_path, encoding="utf-8") as f: for line in f: text = line.strip() if text: yield example_id, {"text": text} example_id += 1
with open(file_path, encoding="utf-8") as f: for line in f: text = line.strip() if text: yield example_id, { "text": text, "source": subdir_name, } example_id += 1common-corpus.py+35-16