kwojtasik commited on
Commit
c0b84ff
1 Parent(s): 2343421

Create poquad.py

Browse files
Files changed (1) hide show
  1. poquad.py +124 -0
poquad.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """FROM SQUAD_V2"""
2
+
3
+
4
+ import json
5
+
6
+ import datasets
7
+ from datasets.tasks import QuestionAnsweringExtractive
8
+
9
+
10
+ # TODO(squad_v2): BibTeX citation
11
+ _CITATION = """\
12
+ Tuora, R., Zawadzka-Paluektau, N., Klamra, C., Zwierzchowska, A., Kobyliński, Ł. (2022).
13
+ Towards a Polish Question Answering Dataset (PoQuAD).
14
+ In: Tseng, YH., Katsurai, M., Nguyen, H.N. (eds) From Born-Physical to Born-Virtual: Augmenting Intelligence in Digital Libraries. ICADL 2022.
15
+ Lecture Notes in Computer Science, vol 13636. Springer, Cham.
16
+ https://doi.org/10.1007/978-3-031-21756-2_16
17
+ """
18
+
19
+ _DESCRIPTION = """\
20
+ PoQuaD description
21
+ """
22
+
23
+
24
+ _URLS = {
25
+ "train": "https://huggingface.co/datasets/clarin-pl/poquad/blob/main/poquad-dev.json",
26
+ "dev": "https://huggingface.co/datasets/clarin-pl/poquad/blob/main/poquad-train.json,
27
+ }
28
+
29
+
30
+ class SquadV2Config(datasets.BuilderConfig):
31
+ """BuilderConfig for SQUAD."""
32
+
33
+ def __init__(self, **kwargs):
34
+ """BuilderConfig for SQUADV2.
35
+ Args:
36
+ **kwargs: keyword arguments forwarded to super.
37
+ """
38
+ super(SquadV2Config, self).__init__(**kwargs)
39
+
40
+
41
+ class SquadV2(datasets.GeneratorBasedBuilder):
42
+ """TODO(squad_v2): Short description of my dataset."""
43
+
44
+ # TODO(squad_v2): Set up version.
45
+ BUILDER_CONFIGS = [
46
+ SquadV2Config(name="poquad", version=datasets.Version("1.0.0"), description="PoQuaD plaint text"),
47
+ ]
48
+
49
+ def _info(self):
50
+ # TODO(squad_v2): Specifies the datasets.DatasetInfo object
51
+ return datasets.DatasetInfo(
52
+ # This is the description that will appear on the datasets page.
53
+ description=_DESCRIPTION,
54
+ # datasets.features.FeatureConnectors
55
+ features=datasets.Features(
56
+ {
57
+ "id": datasets.Value("string"),
58
+ "title": datasets.Value("string"),
59
+ "context": datasets.Value("string"),
60
+ "question": datasets.Value("string"),
61
+ "answers": datasets.features.Sequence(
62
+ {
63
+ "text": datasets.Value("string"),
64
+ "answer_start": datasets.Value("int32"),
65
+ }
66
+ ),
67
+ # These are the features of your dataset like images, labels ...
68
+ }
69
+ ),
70
+ # If there's a common (input, target) tuple from the features,
71
+ # specify them here. They'll be used if as_supervised=True in
72
+ # builder.as_dataset.
73
+ supervised_keys=None,
74
+ # Homepage of the dataset for documentation
75
+ homepage="https://rajpurkar.github.io/SQuAD-explorer/",
76
+ citation=_CITATION,
77
+ task_templates=[
78
+ QuestionAnsweringExtractive(
79
+ question_column="question", context_column="context", answers_column="answers"
80
+ )
81
+ ],
82
+ )
83
+
84
+ def _split_generators(self, dl_manager):
85
+ """Returns SplitGenerators."""
86
+ # TODO(squad_v2): Downloads the data and defines the splits
87
+ # dl_manager is a datasets.download.DownloadManager that can be used to
88
+ # download and extract URLs
89
+ urls_to_download = _URLS
90
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
91
+
92
+ return [
93
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
94
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
95
+ ]
96
+
97
+ def _generate_examples(self, filepath):
98
+ """Yields examples."""
99
+ # TODO(squad_v2): Yields (key, example) tuples from the dataset
100
+ with open(filepath, encoding="utf-8") as f:
101
+ squad = json.load(f)
102
+ for example in squad["data"]:
103
+ title = example.get("title", "")
104
+ for paragraph in example["paragraphs"]:
105
+ context = paragraph["context"] # do not strip leading blank spaces GH-2585
106
+ for qa in paragraph["qas"]:
107
+ question = qa["question"]
108
+ id_ = qa["id"]
109
+
110
+ answer_starts = [answer["answer_start"] for answer in qa["answers"]]
111
+ answers = [answer["text"] for answer in qa["answers"]]
112
+
113
+ # Features currently used are "context", "question", and "answers".
114
+ # Others are extracted here for the ease of future expansions.
115
+ yield id_, {
116
+ "title": title,
117
+ "context": context,
118
+ "question": question,
119
+ "id": id_,
120
+ "answers": {
121
+ "answer_start": answer_starts,
122
+ "text": answers,
123
+ },
124
+ }