hails commited on
Commit
8d86a76
1 Parent(s): bb3f608

Create headqa.py

Browse files
Files changed (1) hide show
  1. headqa.py +162 -0
headqa.py ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ # NOTE: This is an exact copy of
16
+ # https://github.com/huggingface/datasets/blob/3804442bb7cfcb9d52044d92688115cfdc69c2da/datasets/head_qa/head_qa.py
17
+ # with the exception of the `image` feature. This is to avoid adding `Pillow`
18
+ # as a dependency.
19
+ """HEAD-QA: A Healthcare Dataset for Complex Reasoning."""
20
+
21
+
22
+ import json
23
+ import os
24
+
25
+ import datasets
26
+
27
+
28
+ _CITATION = """\
29
+ @inproceedings{vilares-gomez-rodriguez-2019-head,
30
+ title = "{HEAD}-{QA}: A Healthcare Dataset for Complex Reasoning",
31
+ author = "Vilares, David and
32
+ G{\'o}mez-Rodr{\'i}guez, Carlos",
33
+ booktitle = "Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics",
34
+ month = jul,
35
+ year = "2019",
36
+ address = "Florence, Italy",
37
+ publisher = "Association for Computational Linguistics",
38
+ url = "https://www.aclweb.org/anthology/P19-1092",
39
+ doi = "10.18653/v1/P19-1092",
40
+ pages = "960--966",
41
+ abstract = "We present HEAD-QA, a multi-choice question answering testbed to encourage research on complex reasoning. The questions come from exams to access a specialized position in the Spanish healthcare system, and are challenging even for highly specialized humans. We then consider monolingual (Spanish) and cross-lingual (to English) experiments with information retrieval and neural techniques. We show that: (i) HEAD-QA challenges current methods, and (ii) the results lag well behind human performance, demonstrating its usefulness as a benchmark for future work.",
42
+ }
43
+ """
44
+
45
+ _DESCRIPTION = """\
46
+ HEAD-QA is a multi-choice HEAlthcare Dataset. The questions come from exams to access a specialized position in the
47
+ Spanish healthcare system, and are challenging even for highly specialized humans. They are designed by the Ministerio
48
+ de Sanidad, Consumo y Bienestar Social.
49
+ The dataset contains questions about the following topics: medicine, nursing, psychology, chemistry, pharmacology and biology.
50
+ """
51
+
52
+ _HOMEPAGE = "https://aghie.github.io/head-qa/"
53
+
54
+ _LICENSE = "MIT License"
55
+
56
+ _URL = "https://drive.google.com/uc?export=download&confirm=t&id=1a_95N5zQQoUCq8IBNVZgziHbeM-QxG2t"
57
+
58
+ _DIRS = {"es": "HEAD", "en": "HEAD_EN"}
59
+
60
+
61
+ class HeadQA(datasets.GeneratorBasedBuilder):
62
+ """HEAD-QA: A Healthcare Dataset for Complex Reasoning"""
63
+
64
+ VERSION = datasets.Version("1.1.0")
65
+
66
+ BUILDER_CONFIGS = [
67
+ datasets.BuilderConfig(
68
+ name="es", version=VERSION, description="Spanish HEAD dataset"
69
+ ),
70
+ datasets.BuilderConfig(
71
+ name="en", version=VERSION, description="English HEAD dataset"
72
+ ),
73
+ ]
74
+
75
+ DEFAULT_CONFIG_NAME = "es"
76
+
77
+ def _info(self):
78
+ return datasets.DatasetInfo(
79
+ description=_DESCRIPTION,
80
+ features=datasets.Features(
81
+ {
82
+ "name": datasets.Value("string"),
83
+ "year": datasets.Value("string"),
84
+ "category": datasets.Value("string"),
85
+ "qid": datasets.Value("int32"),
86
+ "qtext": datasets.Value("string"),
87
+ "ra": datasets.Value("int32"),
88
+ "answers": [
89
+ {
90
+ "aid": datasets.Value("int32"),
91
+ "atext": datasets.Value("string"),
92
+ }
93
+ ],
94
+ }
95
+ ),
96
+ supervised_keys=None,
97
+ homepage=_HOMEPAGE,
98
+ license=_LICENSE,
99
+ citation=_CITATION,
100
+ )
101
+
102
+ def _split_generators(self, dl_manager):
103
+ """Returns SplitGenerators."""
104
+ data_dir = dl_manager.download_and_extract(_URL)
105
+
106
+ dir = _DIRS[self.config.name]
107
+ data_lang_dir = os.path.join(data_dir, dir)
108
+
109
+ return [
110
+ datasets.SplitGenerator(
111
+ name=datasets.Split.TRAIN,
112
+ gen_kwargs={
113
+ "data_dir": data_dir,
114
+ "filepath": os.path.join(data_lang_dir, f"train_{dir}.json"),
115
+ },
116
+ ),
117
+ datasets.SplitGenerator(
118
+ name=datasets.Split.TEST,
119
+ gen_kwargs={
120
+ "data_dir": data_dir,
121
+ "filepath": os.path.join(data_lang_dir, f"test_{dir}.json"),
122
+ },
123
+ ),
124
+ datasets.SplitGenerator(
125
+ name=datasets.Split.VALIDATION,
126
+ gen_kwargs={
127
+ "data_dir": data_dir,
128
+ "filepath": os.path.join(data_lang_dir, f"dev_{dir}.json"),
129
+ },
130
+ ),
131
+ ]
132
+
133
+ def _generate_examples(self, data_dir, filepath):
134
+ """Yields examples."""
135
+ with open(filepath, encoding="utf-8") as f:
136
+ head_qa = json.load(f)
137
+ for exam_id, exam in enumerate(head_qa["exams"]):
138
+ content = head_qa["exams"][exam]
139
+ name = content["name"].strip()
140
+ year = content["year"].strip()
141
+ category = content["category"].strip()
142
+ for question in content["data"]:
143
+ qid = int(question["qid"].strip())
144
+ qtext = question["qtext"].strip()
145
+ ra = int(question["ra"].strip())
146
+
147
+ aids = [answer["aid"] for answer in question["answers"]]
148
+ atexts = [answer["atext"].strip() for answer in question["answers"]]
149
+ answers = [
150
+ {"aid": aid, "atext": atext} for aid, atext in zip(aids, atexts)
151
+ ]
152
+
153
+ id_ = f"{exam_id}_{qid}"
154
+ yield id_, {
155
+ "name": name,
156
+ "year": year,
157
+ "category": category,
158
+ "qid": qid,
159
+ "qtext": qtext,
160
+ "ra": ra,
161
+ "answers": answers,
162
+ }