ArabicaQA / MRC /ArabicaQA.py
abdoelsayed's picture
all data
a9299b3
raw
history blame
No virus
4.11 kB
import csv
import json
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, including CSV data."
)
]
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"),
}
),
# Additional fields from the CSV can be added here if needed
}
),
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",
"csv": "https://huggingface.co/datasets/abdoelsayed/ArabicaQA/raw/main/MRC/all_data_meta.csv"
}
downloaded_files = dl_manager.download(urls_to_download)
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"json_filepath": downloaded_files["train"], "csv_filepath": downloaded_files["csv"]}),
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"json_filepath": downloaded_files["dev"], "csv_filepath": downloaded_files["csv"]}),
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"json_filepath": downloaded_files["test"], "csv_filepath": downloaded_files["csv"]}),
]
def _generate_examples(self, json_filepath, csv_filepath):
# Read the CSV file and store it in memory
csv_data = {}
with open(csv_filepath, encoding="utf-8") as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
csv_data[row['question_id']] = row
# Read the JSON file and yield examples
with open(json_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", [])]
# Combine data from the CSV if present
additional_csv_data = csv_data.get(id_, {})
# Include additional fields from the CSV in the example if necessary
example = {
"title": title,
"context": context,
"question": question,
"answers": answers,
# Add additional fields from the CSV here
}
yield id_, example