Datasets:

ArXiv:
License:
gabrielaltay commited on
Commit
adfcb09
1 Parent(s): e33db38

upload hubscripts/meddialog_hub.py to hub from bigbio repo

Browse files
Files changed (1) hide show
  1. meddialog.py +222 -0
meddialog.py ADDED
@@ -0,0 +1,222 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 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
+ import json
17
+ import re
18
+
19
+ import datasets
20
+
21
+ from .bigbiohub import text_features
22
+ from .bigbiohub import BigBioConfig
23
+ from .bigbiohub import Tasks
24
+
25
+ _DATASETNAME = "meddialog"
26
+ _DISPLAYNAME = "MedDialog"
27
+
28
+ _LANGUAGES = ['English', 'Chinese']
29
+ _PUBMED = False
30
+ _LOCAL = False
31
+ _CITATION = """
32
+ @article{DBLP:journals/corr/abs-2004-03329,
33
+ author = {Shu Chen and
34
+ Zeqian Ju and
35
+ Xiangyu Dong and
36
+ Hongchao Fang and
37
+ Sicheng Wang and
38
+ Yue Yang and
39
+ Jiaqi Zeng and
40
+ Ruisi Zhang and
41
+ Ruoyu Zhang and
42
+ Meng Zhou and
43
+ Penghui Zhu and
44
+ Pengtao Xie},
45
+ title = {MedDialog: {A} Large-scale Medical Dialogue Dataset},
46
+ journal = {CoRR},
47
+ volume = {abs/2004.03329},
48
+ year = {2020},
49
+ url = {https://arxiv.org/abs/2004.03329},
50
+ eprinttype = {arXiv},
51
+ eprint = {2004.03329},
52
+ biburl = {https://dblp.org/rec/journals/corr/abs-2004-03329.bib},
53
+ bibsource = {dblp computer science bibliography, https://dblp.org}
54
+ }
55
+ """
56
+
57
+ _DESCRIPTION = """
58
+ The MedDialog dataset (English) contains conversations (in English) between doctors and patients.\
59
+ It has 0.26 million dialogues. The data is continuously growing and more dialogues will be added. \
60
+ The raw dialogues are from healthcaremagic.com and icliniq.com.\
61
+
62
+ All copyrights of the data belong to healthcaremagic.com and icliniq.com.
63
+ """
64
+
65
+ _HOMEPAGE = "https://github.com/UCSD-AI4H/Medical-Dialogue-System"
66
+
67
+ _LICENSE = 'License information unavailable'
68
+
69
+ _URLs = {
70
+ "en": {
71
+ "train": "https://drive.google.com/file/d/1ria4E6IdTIPsikL4Glm3uy1tFKJKw0W8/view?usp=sharing",
72
+ "validation": "https://drive.google.com/file/d/1KAZneuwdfEVQQM6euCX4pMDP-9DQpiB5/view?usp=sharing",
73
+ "test": "https://drive.google.com/file/d/10izqL71kcgnteYsf87Vh6j_mZ8sZM2Rc/view?usp=sharing",
74
+ },
75
+ "zh": {
76
+ "train": "https://drive.google.com/file/d/1AaDJoHaiHAwEZwtskRH8oL1UP4FRgmgx/view?usp=sharing",
77
+ "validation": "https://drive.google.com/file/d/1TvfZCmQqP1kURIfEinOcj5VOPelTuGwI/view?usp=sharing",
78
+ "test": "https://drive.google.com/file/d/1pmmG95Yl6mMXRXDDSRb9-bYTxOE7ank5/view?usp=sharing",
79
+ },
80
+ }
81
+
82
+ _SUPPORTED_TASKS = [Tasks.TEXT_CLASSIFICATION]
83
+ _SOURCE_VERSION = "1.0.0"
84
+ _BIGBIO_VERSION = "1.0.0"
85
+
86
+
87
+ class MedDialog(datasets.GeneratorBasedBuilder):
88
+ """MedDialog: Large-scale Medical Dialogue Datasets in English and Chinese."""
89
+
90
+ DEFAULT_CONFIG_NAME = "meddialog_en_source"
91
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
92
+ BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
93
+
94
+ BUILDER_CONFIGS = [
95
+ # Source schemas
96
+ BigBioConfig(
97
+ name="meddialog_en_source",
98
+ version=SOURCE_VERSION,
99
+ description="MedDialog source schema",
100
+ schema="source",
101
+ subset_id="meddialog_en",
102
+ ),
103
+ BigBioConfig(
104
+ name="meddialog_zh_source",
105
+ version=SOURCE_VERSION,
106
+ description="MedDialog source schema",
107
+ schema="source",
108
+ subset_id="meddialog_zh",
109
+ ),
110
+ # BigBio schema: text classification
111
+ BigBioConfig(
112
+ name="meddialog_en_bigbio_text",
113
+ version=BIGBIO_VERSION,
114
+ description="MedDialog simplified BigBio schema",
115
+ schema="bigbio_text",
116
+ subset_id="meddialog_en",
117
+ ),
118
+ BigBioConfig(
119
+ name="meddialog_zh_bigbio_text",
120
+ version=BIGBIO_VERSION,
121
+ description="MedDialog simplified BigBio schema",
122
+ schema="bigbio_text",
123
+ subset_id="meddialog_zh",
124
+ ),
125
+ ]
126
+
127
+ def _get_gdrive_url(self, url):
128
+ """Converts URL from google drive shareable link to format used by dl_manager."""
129
+ fileid = re.match("https://drive\.google\.com/file/d/(.+)/view\?", url).group(1)
130
+ return f"https://drive.google.com/uc?id={fileid}"
131
+
132
+ def _info(self):
133
+ lang = self.config.name.split("_")[1]
134
+ if self.config.schema == "source":
135
+ if lang == "en":
136
+ features = datasets.Features(
137
+ {
138
+ "description": datasets.Value("string"),
139
+ "utterances": datasets.Sequence(
140
+ {
141
+ "speaker": datasets.ClassLabel(
142
+ names=["patient", "doctor"]
143
+ ),
144
+ "utterance": datasets.Value("string"),
145
+ }
146
+ ),
147
+ }
148
+ )
149
+ elif lang == "zh":
150
+ features = datasets.Features(
151
+ {
152
+ "utterances": datasets.Sequence(
153
+ {
154
+ "speaker": datasets.ClassLabel(names=["病人", "医生"]),
155
+ "utterance": datasets.Value("string"),
156
+ }
157
+ ),
158
+ }
159
+ )
160
+ elif self.config.schema == "bigbio_text":
161
+ features = text_features
162
+
163
+ return datasets.DatasetInfo(
164
+ description=_DESCRIPTION,
165
+ features=features,
166
+ supervised_keys=None,
167
+ homepage=_HOMEPAGE,
168
+ license=str(_LICENSE),
169
+ citation=_CITATION,
170
+ )
171
+
172
+ def _split_generators(self, dl_manager):
173
+ lang = self.config.name.split("_")[1]
174
+ my_urls = {
175
+ split: self._get_gdrive_url(url) for split, url in _URLs[lang].items()
176
+ }
177
+ dl_dir = dl_manager.download_and_extract(my_urls)
178
+ return [
179
+ datasets.SplitGenerator(
180
+ name=split,
181
+ gen_kwargs={"filepath": dl_dir[split], "split": split, "lang": lang},
182
+ )
183
+ for split in _URLs[lang]
184
+ ]
185
+
186
+ def _generate_examples(self, filepath, split, lang):
187
+ with open(filepath, "r") as f:
188
+ data = json.load(f)
189
+
190
+ # delimiter symbol differs by language
191
+ delimiter = ":" if lang == "zh" else ":"
192
+ document_id = f"{lang}_{split}"
193
+
194
+ for i, d in enumerate(data):
195
+ out_utterances = []
196
+ utterances = d["utterances"] if lang == "en" else d
197
+ for j, utt in enumerate(utterances):
198
+ elements = utt.strip().split(delimiter)
199
+ speaker = elements[0]
200
+ text = delimiter.join(elements[1:]).strip()
201
+ if self.config.schema == "bigbio_text":
202
+ # TODO - this ignores description
203
+ id = f"{document_id}_{i}_{j}"
204
+ yield id, {
205
+ "id": id,
206
+ "document_id": document_id,
207
+ "text": text,
208
+ "labels": [speaker],
209
+ }
210
+ else:
211
+ out_utterances.append({"speaker": speaker, "utterance": text})
212
+ if self.config.schema == "source":
213
+ id = f"{document_id}_{i}"
214
+ if lang == "en":
215
+ yield id, {
216
+ "description": d["description"],
217
+ "utterances": out_utterances,
218
+ }
219
+ else:
220
+ yield id, {
221
+ "utterances": out_utterances,
222
+ }