File size: 4,283 Bytes
5e19492
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15ea971
 
 
 
 
 
 
5e19492
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# coding=utf-8
# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Lint as: python3


import csv
import os
import textwrap
import numpy as np
import datasets
import pandas as pd


_CITATION = """
@article{sileo2023generating,
  title={Generating multiple-choice questions for medical question answering with distractors and cue-masking},
  author={Sileo, Damien and Uma, Kanimozhi and Moens, Marie-Francine},
  journal={arXiv preprint arXiv:2303.07069},
  year={2023}
}
"""

_DESCRIPTION = """\
Anonymous submission
"""

URL = 'https://sileod.s3.eu-west-3.amazonaws.com/wikimedqa/'


class WikiMedQAConfig(datasets.BuilderConfig):
    """BuilderConfig for WikiMedQA."""

    def __init__(
        self,
        data_dir,
        label_classes=None,
        process_label=lambda x: x,
        **kwargs,
    ):

        super(WikiMedQAConfig, self).__init__(version=datasets.Version("1.0.5", ""), **kwargs)
        self.text_features = {k:k for k in ['text']+[f'option_{i}' for i in range(8)]}
        self.label_column = 'label'
        self.label_classes = list('01234567')
        self.data_url = URL
        self.url=URL
        self.data_dir=data_dir
        self.citation = _CITATION
        self.process_label = process_label


class WikiMedQA(datasets.GeneratorBasedBuilder):
    """Evaluation of word estimative of probability understanding"""

    BUILDER_CONFIGS = [
        WikiMedQAConfig(
            name="medwiki",
            data_dir="medwiki"),
         WikiMedQAConfig(
            name="wikem",
            data_dir="wikem"),
        WikiMedQAConfig(
            name="wikidoc",
            data_dir="wikidoc"),
        ]

    def _info(self):
        features = {text_feature: datasets.Value("string") for text_feature in self.config.text_features.keys()}
        features["label"] = datasets.features.ClassLabel(names=self.config.label_classes)
        features["idx"] = datasets.Value("int32")

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(features),
            homepage=self.config.url,
            citation=self.config.citation + "\n" + _CITATION,
        )
    def _split_generators(self, dl_manager):
        
        data_dirs=[]
        for split in ['train','validation','test']:
            url=f'{URL}{self.config.data_dir}.csv'
            print(url)
            data_dirs+=[dl_manager.download(url)]
        print(data_dirs)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "data_file": data_dirs[0],
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "data_file": data_dirs[1],
                    "split": "dev",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "data_file": data_dirs[2],
                    "split": "test",
                },
            ),
        ]

    def _generate_examples(self, data_file, split):
        df = pd.read_csv(data_file)
        df=df[['text','options','label']]
        train, dev, test = np.split(df.sample(frac=1, random_state=42), 
                       [int(.9*len(df)), int(.95*len(df))])
        df=eval(split)
        df['options']=df['options'].map(eval)
        for i in range(8):
            df[f'option_{i}']=df.options.map(lambda x:x[i])
        del df['options']
        df['idx']=df.index
        for idx, example in df.iterrows():
            yield idx, dict(example)