import os import csv from datasets import Dataset, DatasetInfo, Features, Value, Sequence, GeneratorBasedBuilder, DownloadManager, Split class FreshWikiDataset(GeneratorBasedBuilder): def _info(self): return DatasetInfo( description="The FreshWiki Dataset is a collection of high-quality Wikipedia articles focusing on the most-edited pages from February 2022 to September 2023.", features=Features({ "json_file": Value("string"), "txt_file": Value("string"), "topic": Value("string"), }), ) def _split_generators(self, dl_manager: DownloadManager): data_dir = dl_manager.download_and_extract("https://huggingface.co/datasets/EchoShao8899/FreshWiki") return [ Split( name=Split.TRAIN, gen_kwargs={ "json_files": os.path.join(data_dir, "json"), "txt_files": os.path.join(data_dir, "txt"), "topic_list_csv": os.path.join(data_dir, "topic_list.csv"), }, ), ] def _generate_examples(self, json_files, txt_files, topic_list_csv): with open(topic_list_csv, "r", encoding="utf-8") as csv_file: reader = csv.DictReader(csv_file) for idx, row in enumerate(reader): topic_name = row['topic'].replace(' ', '_').replace('/', '_') json_path = os.path.join(json_files, f'{topic_name}.json') txt_path = os.path.join(txt_files, f'{topic_name}.txt') if os.path.exists(json_path) and os.path.exists(txt_path): with open(json_path, "r", encoding="utf-8") as json_file, \ open(txt_path, "r", encoding="utf-8") as txt_file: yield idx, { "json_file": json_file.read(), "txt_file": txt_file.read(), "topic": row['topic'], } else: print(f"Warning: Files for topic '{row['topic']}' not found. Skipping.")