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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
|
| 16 |
+
"""
|
| 17 |
+
Natural Language Inference (NLI) is the task of determining whether a given hypothesis can be
|
| 18 |
+
inferred from a given premise. Also known as Recognizing Textual Entailment (RTE), this task has
|
| 19 |
+
enjoyed popularity among researchers for some time. However, almost all datasets for this task
|
| 20 |
+
focused on open domain data such as as news texts, blogs, and so on. To address this gap, the MedNLI
|
| 21 |
+
dataset was created for language inference in the medical domain. MedNLI is a derived dataset with
|
| 22 |
+
data sourced from MIMIC-III v1.4. In order to stimulate research for this problem, a shared task on
|
| 23 |
+
Medical Inference and Question Answering (MEDIQA) was organized at the workshop for biomedical
|
| 24 |
+
natural language processing (BioNLP) 2019. The dataset provided herein is a test set of 405 premise
|
| 25 |
+
hypothesis pairs for the NLI challenge in the MEDIQA shared task. Participants of the shared task
|
| 26 |
+
are expected to use the MedNLI data for development of their models and this dataset was used as an
|
| 27 |
+
unseen dataset for scoring each participant submission.
|
| 28 |
+
|
| 29 |
+
The files comprising this dataset must be on the users local machine in a single directory that is
|
| 30 |
+
passed to `datasets.load_datset` via the `data_dir` kwarg. This loader script will read the archive
|
| 31 |
+
files directly (i.e. the user should not uncompress, untar or unzip any of the files). For example,
|
| 32 |
+
if `data_dir` is `"mediqa_nli"` it should contain the following files:
|
| 33 |
+
|
| 34 |
+
mediqa_nli
|
| 35 |
+
├── mednli-for-shared-task-at-acl-bionlp-2019-1.0.1.zip
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
import json
|
| 39 |
+
import os
|
| 40 |
+
from typing import Dict, List, Tuple
|
| 41 |
+
|
| 42 |
+
import datasets
|
| 43 |
+
import pandas as pd
|
| 44 |
+
|
| 45 |
+
from .bigbiohub import entailment_features
|
| 46 |
+
from .bigbiohub import BigBioConfig
|
| 47 |
+
from .bigbiohub import Tasks
|
| 48 |
+
|
| 49 |
+
_LANGUAGES = ['English']
|
| 50 |
+
_PUBMED = False
|
| 51 |
+
_LOCAL = True
|
| 52 |
+
_CITATION = """\
|
| 53 |
+
@misc{https://doi.org/10.13026/gtv4-g455,
|
| 54 |
+
title = {MedNLI for Shared Task at ACL BioNLP 2019},
|
| 55 |
+
author = {Shivade, Chaitanya},
|
| 56 |
+
year = 2019,
|
| 57 |
+
publisher = {physionet.org},
|
| 58 |
+
doi = {10.13026/GTV4-G455},
|
| 59 |
+
url = {https://physionet.org/content/mednli-bionlp19/}
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
_DATASETNAME = "mediqa_nli"
|
| 65 |
+
_DISPLAYNAME = "MEDIQA NLI"
|
| 66 |
+
|
| 67 |
+
_DESCRIPTION = """\
|
| 68 |
+
Natural Language Inference (NLI) is the task of determining whether a given hypothesis can be
|
| 69 |
+
inferred from a given premise. Also known as Recognizing Textual Entailment (RTE), this task has
|
| 70 |
+
enjoyed popularity among researchers for some time. However, almost all datasets for this task
|
| 71 |
+
focused on open domain data such as as news texts, blogs, and so on. To address this gap, the MedNLI
|
| 72 |
+
dataset was created for language inference in the medical domain. MedNLI is a derived dataset with
|
| 73 |
+
data sourced from MIMIC-III v1.4. In order to stimulate research for this problem, a shared task on
|
| 74 |
+
Medical Inference and Question Answering (MEDIQA) was organized at the workshop for biomedical
|
| 75 |
+
natural language processing (BioNLP) 2019. The dataset provided herein is a test set of 405 premise
|
| 76 |
+
hypothesis pairs for the NLI challenge in the MEDIQA shared task. Participants of the shared task
|
| 77 |
+
are expected to use the MedNLI data for development of their models and this dataset was used as an
|
| 78 |
+
unseen dataset for scoring each participant submission.
|
| 79 |
+
"""
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
_HOMEPAGE = "https://physionet.org/content/mednli-bionlp19/1.0.1/"
|
| 83 |
+
|
| 84 |
+
_LICENSE = 'PhysioNet Credentialed Health Data License'
|
| 85 |
+
|
| 86 |
+
_URLS = {}
|
| 87 |
+
|
| 88 |
+
_SUPPORTED_TASKS = [Tasks.TEXTUAL_ENTAILMENT]
|
| 89 |
+
|
| 90 |
+
_SOURCE_VERSION = "1.0.1"
|
| 91 |
+
_BIGBIO_VERSION = "1.0.0"
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
class MEDIQANLIDataset(datasets.GeneratorBasedBuilder):
|
| 95 |
+
"""MEDIQA NLI"""
|
| 96 |
+
|
| 97 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
| 98 |
+
BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
|
| 99 |
+
|
| 100 |
+
BUILDER_CONFIGS = [
|
| 101 |
+
BigBioConfig(
|
| 102 |
+
name="mediqa_nli_source",
|
| 103 |
+
version=SOURCE_VERSION,
|
| 104 |
+
description="MEDIQA NLI source schema",
|
| 105 |
+
schema="source",
|
| 106 |
+
subset_id="mediqa_nli",
|
| 107 |
+
),
|
| 108 |
+
BigBioConfig(
|
| 109 |
+
name="mediqa_nli_bigbio_te",
|
| 110 |
+
version=BIGBIO_VERSION,
|
| 111 |
+
description="MEDIQA NLI BigBio schema",
|
| 112 |
+
schema="bigbio_te",
|
| 113 |
+
subset_id="mediqa_nli",
|
| 114 |
+
),
|
| 115 |
+
]
|
| 116 |
+
|
| 117 |
+
DEFAULT_CONFIG_NAME = "mediqa_nli_source"
|
| 118 |
+
|
| 119 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 120 |
+
|
| 121 |
+
if self.config.schema == "source":
|
| 122 |
+
features = datasets.Features(
|
| 123 |
+
{
|
| 124 |
+
"pairID": datasets.Value("string"),
|
| 125 |
+
"gold_label": datasets.Value("string"),
|
| 126 |
+
"sentence1": datasets.Value("string"),
|
| 127 |
+
"sentence2": datasets.Value("string"),
|
| 128 |
+
"sentence1_parse": datasets.Value("string"),
|
| 129 |
+
"sentence2_parse": datasets.Value("string"),
|
| 130 |
+
"sentence1_binary_parse": datasets.Value("string"),
|
| 131 |
+
"sentence2_binary_parse": datasets.Value("string"),
|
| 132 |
+
}
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
elif self.config.schema == "bigbio_te":
|
| 136 |
+
features = entailment_features
|
| 137 |
+
|
| 138 |
+
return datasets.DatasetInfo(
|
| 139 |
+
description=_DESCRIPTION,
|
| 140 |
+
features=features,
|
| 141 |
+
homepage=_HOMEPAGE,
|
| 142 |
+
license=str(_LICENSE),
|
| 143 |
+
citation=_CITATION,
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
def _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:
|
| 147 |
+
if self.config.data_dir is None:
|
| 148 |
+
raise ValueError(
|
| 149 |
+
"This is a local dataset. Please pass the data_dir kwarg to load_dataset."
|
| 150 |
+
)
|
| 151 |
+
else:
|
| 152 |
+
extract_dir = dl_manager.extract(
|
| 153 |
+
os.path.join(
|
| 154 |
+
self.config.data_dir,
|
| 155 |
+
"mednli-for-shared-task-at-acl-bionlp-2019-1.0.1.zip",
|
| 156 |
+
)
|
| 157 |
+
)
|
| 158 |
+
data_dir = os.path.join(
|
| 159 |
+
extract_dir, "mednli-for-shared-task-at-acl-bionlp-2019-1.0.1"
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
return [
|
| 163 |
+
datasets.SplitGenerator(
|
| 164 |
+
name=datasets.Split.TEST,
|
| 165 |
+
gen_kwargs={
|
| 166 |
+
"examples_filepath": os.path.join(
|
| 167 |
+
data_dir, "mednli_bionlp19_shared_task.jsonl"
|
| 168 |
+
),
|
| 169 |
+
"ground_truth_filepath": os.path.join(
|
| 170 |
+
data_dir, "mednli_bionlp19_shared_task_ground_truth.csv"
|
| 171 |
+
),
|
| 172 |
+
"split": "test",
|
| 173 |
+
},
|
| 174 |
+
),
|
| 175 |
+
]
|
| 176 |
+
|
| 177 |
+
def _generate_examples(
|
| 178 |
+
self, examples_filepath: str, ground_truth_filepath: str, split: str
|
| 179 |
+
) -> Tuple[int, Dict]:
|
| 180 |
+
|
| 181 |
+
ground_truth = pd.read_csv(
|
| 182 |
+
ground_truth_filepath, index_col=0, squeeze=True
|
| 183 |
+
).to_dict()
|
| 184 |
+
with open(examples_filepath, "r") as f:
|
| 185 |
+
if self.config.schema == "source":
|
| 186 |
+
for line in f:
|
| 187 |
+
json_line = json.loads(line)
|
| 188 |
+
json_line["gold_label"] = ground_truth[json_line["pairID"]]
|
| 189 |
+
yield json_line["pairID"], json_line
|
| 190 |
+
|
| 191 |
+
elif self.config.schema == "bigbio_te":
|
| 192 |
+
for line in f:
|
| 193 |
+
json_line = json.loads(line)
|
| 194 |
+
entailment_example = {
|
| 195 |
+
"id": json_line["pairID"],
|
| 196 |
+
"premise": json_line["sentence1"],
|
| 197 |
+
"hypothesis": json_line["sentence2"],
|
| 198 |
+
"label": ground_truth[json_line["pairID"]],
|
| 199 |
+
}
|
| 200 |
+
yield json_line["pairID"], entailment_example
|