| from datasets import load_dataset_builder, DatasetInfo, DownloadConfig, GeneratorBasedBuilder, datasets | |
| class CustomSQuADFormatDataset(GeneratorBasedBuilder): | |
| """A custom dataset similar to SQuAD but tailored for 'ArabicaQA' hosted on Hugging Face.""" | |
| VERSION = datasets.Version("1.0.0") | |
| BUILDER_CONFIGS = [ | |
| datasets.BuilderConfig(name="ArabicaQA", version=VERSION, description="Custom dataset similar to SQuAD format.") | |
| ] | |
| def _info(self): | |
| return DatasetInfo( | |
| description="This dataset is formatted similarly to the SQuAD dataset.", | |
| features=datasets.Features( | |
| { | |
| "id": datasets.Value("string"), | |
| "title": datasets.Value("string"), | |
| "context": datasets.Value("string"), | |
| "question": datasets.Value("string"), | |
| "answers": datasets.features.Sequence( | |
| { | |
| "text": datasets.Value("string"), | |
| "answer_start": datasets.Value("int32"), | |
| } | |
| ), | |
| } | |
| ), | |
| supervised_keys=None, | |
| homepage="https://huggingface.co/datasets/abdoelsayed/ArabicaQA", | |
| citation="", | |
| ) | |
| def _split_generators(self, dl_manager: DownloadConfig): | |
| urls_to_download = { | |
| "train": "https://huggingface.co/datasets/abdoelsayed/ArabicaQA/raw/main/MRC/train.json", | |
| "dev": "https://huggingface.co/datasets/abdoelsayed/ArabicaQA/raw/main/MRC/val.json", | |
| "test": "https://huggingface.co/datasets/abdoelsayed/ArabicaQA/raw/main/MRC/test.json" | |
| } | |
| downloaded_files = dl_manager.download(urls_to_download) | |
| return [ | |
| datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}), | |
| datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}), | |
| datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["test"]}), | |
| ] | |
| def _generate_examples(self, filepath): | |
| with open(filepath, encoding="utf-8") as f: | |
| squad_data = json.load(f)["data"] | |
| for article in squad_data: | |
| title = article.get("title", "") | |
| for paragraph in article["paragraphs"]: | |
| context = paragraph["context"] | |
| for qa in paragraph["qas"]: | |
| id_ = qa["id"] | |
| question = qa["question"] | |
| answers = [{"text": answer["text"], "answer_start": answer["answer_start"]} for answer in qa.get("answers", [])] | |
| yield id_, { | |
| "title": title, | |
| "context": context, | |
| "question": question, | |
| "answers": answers, | |
| } | |