Gholamreza commited on
Commit
cfa02b2
1 Parent(s): 2aeffe9

Create pquad.py

Browse files
Files changed (1) hide show
  1. pquad.py +99 -0
pquad.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Taken from https://huggingface.co/datasets/Shayanvsf/pquad_public/raw/main/pquad_public.py
2
+ # Edited for the complete dataset (22MB Train.csv)
3
+ # By Gholamreza Dar
4
+ # Feb 2023
5
+
6
+ import json
7
+ import datasets
8
+ _CITATION = """\
9
+ @article{darvishi2022pquad,
10
+ title={PQuAD: A Persian Question Answering Dataset},
11
+ author={Darvishi, Kasra and Shahbodagh, Newsha and Abbasiantaeb, Zahra and Momtazi, Saeedeh},
12
+ journal={arXiv preprint arXiv:2202.06219},
13
+ year={2022}
14
+ }
15
+ """
16
+ _DESCRIPTION = """\\\\
17
+ PQuAD: PQuAD is a crowd-sourced reading comprehension dataset on Persian Language.
18
+ """
19
+ _URL = "https://raw.githubusercontent.com/AUT-NLP/PQuAD/main/Dataset/"
20
+ _URLS = {
21
+ "train": _URL + "Train.json",
22
+ "validation":_URL + "Validation.json",
23
+ "test": _URL + "Test.json",
24
+ }
25
+ class pquad_public_Config(datasets.BuilderConfig):
26
+ """BuilderConfig for PQuAD."""
27
+ def __init__(self, **kwargs):
28
+ """BuilderConfig for PQuAD.
29
+ Args:
30
+ **kwargs: keyword arguments forwarded to super.
31
+ """
32
+ super(pquad_public_Config, self).__init__(**kwargs)
33
+ class pquad_public(datasets.GeneratorBasedBuilder):
34
+ BUILDER_CONFIGS = [
35
+ pquad_public_Config(name="pquad", version=datasets.Version("1.0.0"), description="PQuAD"),
36
+ ]
37
+ def _info(self):
38
+ return datasets.DatasetInfo(
39
+ # This is the description that will appear on the datasets page.
40
+ description=_DESCRIPTION,
41
+ # datasets.features.FeatureConnectors
42
+ features=datasets.Features(
43
+ {
44
+ "id": datasets.Value("float64"),
45
+ "title": datasets.Value("string"),
46
+ "context": datasets.Value("string"),
47
+ "question": datasets.Value("string"),
48
+ "answers": datasets.features.Sequence(
49
+ {
50
+ "text": datasets.Value("string"),
51
+ "answer_start": datasets.Value("int32"),
52
+ }
53
+ ),
54
+ }
55
+ ),
56
+ supervised_keys=None,
57
+ # Homepage of the dataset for documentation
58
+ homepage="https://github.com/AUT-NLP/PQuAD",
59
+ citation=_CITATION,
60
+ )
61
+ def _split_generators(self, dl_manager):
62
+ """Returns SplitGenerators."""
63
+ # TODO(persian_qa): Downloads the data and defines the splits
64
+ # dl_manager is a datasets.download.DownloadManager that can be used to
65
+ # download and extract URLs
66
+ urls_to_download = _URLS
67
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
68
+ return [
69
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
70
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["validation"]}),
71
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["test"]})
72
+ ]
73
+ def _generate_examples(self, filepath):
74
+ """Yields examples."""
75
+ # TODO(persian_qa): Yields (key, example) tuples from the dataset
76
+ with open(filepath, encoding="utf-8") as f:
77
+ print(filepath)
78
+ squad = json.load(f)
79
+ for example in squad["data"]:
80
+ title = example.get("title", "").strip()
81
+ for paragraph in example["paragraphs"]:
82
+ context = paragraph["context"].strip()
83
+ for qa in paragraph["qas"]:
84
+ question = qa["question"].strip()
85
+ id_ = qa["id"]
86
+ answer_starts = [answer["answer_start"] for answer in qa["answers"]]
87
+ answers = [answer["text"].strip() for answer in qa["answers"]]
88
+ # Features currently used are "context", "question", and "answers".
89
+ # Others are extracted here for the ease of future expansions.
90
+ yield id_, {
91
+ "title": title,
92
+ "context": context,
93
+ "question": question,
94
+ "id": id_,
95
+ "answers": {
96
+ "answer_start": answer_starts,
97
+ "text": answers,
98
+ },
99
+ }