File size: 3,053 Bytes
0a6cfbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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,
                        }