Datasets:
Update loading script
Browse files- medical_dialog.py +135 -166
medical_dialog.py
CHANGED
|
@@ -46,15 +46,21 @@ _LICENSE = "Unknown"
|
|
| 46 |
|
| 47 |
# URLS of processed data
|
| 48 |
_URLS = {
|
| 49 |
-
"en":
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
},
|
| 54 |
-
"zh": {
|
| 55 |
-
"train": "
|
| 56 |
-
"validation": "
|
| 57 |
-
"test": "
|
| 58 |
},
|
| 59 |
}
|
| 60 |
|
|
@@ -77,33 +83,6 @@ class MedicalDialog(datasets.GeneratorBasedBuilder):
|
|
| 77 |
),
|
| 78 |
]
|
| 79 |
|
| 80 |
-
@property
|
| 81 |
-
def manual_download_instructions(self):
|
| 82 |
-
*processed, _ = self.config.name.split(".")
|
| 83 |
-
return (
|
| 84 |
-
None
|
| 85 |
-
if processed
|
| 86 |
-
else """\
|
| 87 |
-
\n For English:\nYou need to go to https://drive.google.com/drive/folders/1g29ssimdZ6JzTST6Y8g6h-ogUNReBtJD?usp=sharing,\
|
| 88 |
-
and manually download the dataset from Google Drive. Once it is completed,
|
| 89 |
-
a file named Medical-Dialogue-Dataset-English-<timestamp-info>.zip will appear in your Downloads folder(
|
| 90 |
-
or whichever folder your browser chooses to save files to). Unzip the folder to obtain
|
| 91 |
-
a folder named "Medical-Dialogue-Dataset-English" several text files.
|
| 92 |
-
|
| 93 |
-
Now, you can specify the path to this folder for the data_dir argument in the
|
| 94 |
-
datasets.load_dataset(...) option.
|
| 95 |
-
The <path/to/folder> can e.g. be "/Downloads/Medical-Dialogue-Dataset-English".
|
| 96 |
-
The data can then be loaded using the below command:\
|
| 97 |
-
`datasets.load_dataset("medical_dialog", name="en", data_dir="/Downloads/Medical-Dialogue-Dataset-English")`.
|
| 98 |
-
|
| 99 |
-
\n For Chinese:\nFollow the above process. Change the 'name' to 'zh'.The download link is https://drive.google.com/drive/folders/1r09_i8nJ9c1nliXVGXwSqRYqklcHd9e2
|
| 100 |
-
|
| 101 |
-
**NOTE**
|
| 102 |
-
- A caution while downloading from drive. It is better to download single files since creating a zip might not include files <500 MB. This has been observed mutiple times.
|
| 103 |
-
- After downloading the files and adding them to the appropriate folder, the path of the folder can be given as input tu the data_dir path.
|
| 104 |
-
"""
|
| 105 |
-
)
|
| 106 |
-
|
| 107 |
def _info(self):
|
| 108 |
if self.config.name == "zh":
|
| 109 |
features = datasets.Features(
|
|
@@ -158,23 +137,13 @@ class MedicalDialog(datasets.GeneratorBasedBuilder):
|
|
| 158 |
"""Returns SplitGenerators."""
|
| 159 |
*processed, lang = self.config.name.split(".")
|
| 160 |
if processed:
|
| 161 |
-
data_dir = dl_manager.download(_URLS[lang])
|
|
|
|
| 162 |
splits = [datasets.Split.TRAIN, datasets.Split.VALIDATION, datasets.Split.TEST]
|
| 163 |
-
return [datasets.SplitGenerator(name=split, gen_kwargs={"filepaths": data_dir[split]}) for split in splits]
|
| 164 |
else:
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
raise FileNotFoundError(
|
| 168 |
-
f"{path_to_manual_file} does not exist. Make sure you insert a manual dir via `datasets.load_dataset('medical_dialog', data_dir=...)`. Manual download instructions: {self.manual_download_instructions})"
|
| 169 |
-
)
|
| 170 |
-
|
| 171 |
-
filepaths = [
|
| 172 |
-
os.path.join(path_to_manual_file, txt_file_name)
|
| 173 |
-
for txt_file_name in sorted(os.listdir(path_to_manual_file))
|
| 174 |
-
if txt_file_name.endswith("txt")
|
| 175 |
-
]
|
| 176 |
-
|
| 177 |
-
return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepaths": filepaths})]
|
| 178 |
|
| 179 |
def _generate_examples(self, filepaths):
|
| 180 |
"""Yields examples. Iterates over each file and give the creates the corresponding features.
|
|
@@ -205,130 +174,130 @@ class MedicalDialog(datasets.GeneratorBasedBuilder):
|
|
| 205 |
array = ""
|
| 206 |
else:
|
| 207 |
id_ = -1
|
| 208 |
-
for filepath in filepaths:
|
| 209 |
-
with open(filepath, encoding="utf-8") as f_in:
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
dialogue_url = line.rstrip()
|
| 241 |
|
| 242 |
-
# Extracting the patient info from description.
|
| 243 |
-
if line[:11] == "Description": # Hardcode alert!
|
| 244 |
-
last_part = "description"
|
| 245 |
-
last_dialog = {}
|
| 246 |
-
last_list = []
|
| 247 |
-
last_user = ""
|
| 248 |
-
last_conv = {"speaker": "", "utterance": ""}
|
| 249 |
while True:
|
| 250 |
-
line = f_in.readline()
|
| 251 |
if (not line) or (line in ["\n", "\n\r"]):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
break
|
| 253 |
-
else:
|
| 254 |
-
if data_lang == "zh": # Condition in chinese
|
| 255 |
-
if line[:5] == "病情描述:": # Hardcode alert!
|
| 256 |
-
last_user = "病人"
|
| 257 |
-
sen = f_in.readline().rstrip()
|
| 258 |
-
des_flag = True
|
| 259 |
|
| 260 |
-
if data_lang == "en":
|
| 261 |
-
last_user = "Patient"
|
| 262 |
-
sen = line.rstrip()
|
| 263 |
-
des_flag = True
|
| 264 |
-
|
| 265 |
-
if des_flag:
|
| 266 |
-
if sen == "":
|
| 267 |
-
continue
|
| 268 |
-
if sen in check_list:
|
| 269 |
-
last_conv["speaker"] = ""
|
| 270 |
-
last_conv["utterance"] = ""
|
| 271 |
-
else:
|
| 272 |
-
last_conv["speaker"] = last_user
|
| 273 |
-
last_conv["utterance"] = sen
|
| 274 |
-
check_list.append(sen)
|
| 275 |
-
des_flag = False
|
| 276 |
-
break
|
| 277 |
-
# Extracting the conversation info from dialogue.
|
| 278 |
-
elif line[:8] == "Dialogue": # Hardcode alert!
|
| 279 |
-
if last_part == "description" and len(last_conv["utterance"]) > 0:
|
| 280 |
-
last_part = "dialogue"
|
| 281 |
if data_lang == "zh":
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
| 283 |
|
|
|
|
|
|
|
| 284 |
if data_lang == "en":
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
conv_flag =
|
| 291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
last_list.append(copy.deepcopy(last_conv))
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
last_turn = len(last_list)
|
| 296 |
-
if int(last_turn / 2) > 0:
|
| 297 |
-
temp = int(last_turn / 2)
|
| 298 |
-
id_ += 1
|
| 299 |
-
last_dialog["file_name"] = filepath
|
| 300 |
-
last_dialog["dialogue_id"] = dialogue_id
|
| 301 |
-
last_dialog["dialogue_url"] = dialogue_url
|
| 302 |
-
last_dialog["dialogue_turns"] = last_list[: temp * 2]
|
| 303 |
-
yield id_, last_dialog
|
| 304 |
-
break
|
| 305 |
-
|
| 306 |
-
if data_lang == "zh":
|
| 307 |
-
if line[:3] == "病人:" or line[:3] == "医生:": # Hardcode alert!
|
| 308 |
-
user = line[:2] # Hardcode alert!
|
| 309 |
-
line = f_in.readline()
|
| 310 |
-
conv_flag = True
|
| 311 |
-
|
| 312 |
-
# The elif block is to ensure that multi-line sentences are captured.
|
| 313 |
-
# This has been observed only in english.
|
| 314 |
-
if data_lang == "en":
|
| 315 |
-
if line.strip() == "Patient:" or line.strip() == "Doctor:": # Hardcode alert!
|
| 316 |
-
user = line.replace(":", "").rstrip()
|
| 317 |
-
line = f_in.readline()
|
| 318 |
-
conv_flag = True
|
| 319 |
-
elif line[:2] != "id": # Hardcode alert!
|
| 320 |
-
conv_flag = True
|
| 321 |
-
|
| 322 |
-
# Continues till the next ID is parsed
|
| 323 |
-
if conv_flag:
|
| 324 |
-
sen = line.rstrip()
|
| 325 |
-
if sen == "":
|
| 326 |
-
continue
|
| 327 |
-
|
| 328 |
-
if user == last_user:
|
| 329 |
-
last_conv["utterance"] = last_conv["utterance"] + sen
|
| 330 |
-
else:
|
| 331 |
-
last_user = user
|
| 332 |
-
last_list.append(copy.deepcopy(last_conv))
|
| 333 |
-
last_conv["utterance"] = sen
|
| 334 |
-
last_conv["speaker"] = user
|
|
|
|
| 46 |
|
| 47 |
# URLS of processed data
|
| 48 |
_URLS = {
|
| 49 |
+
"en": "data/Medical-Dialogue-Dataset-English.zip",
|
| 50 |
+
"zh": "data/Medical-Dialogue-Dataset-Chinese.zip",
|
| 51 |
+
"processed.en": "data/processed-english.zip",
|
| 52 |
+
"processed.zh": "data/processed-chinese.zip",
|
| 53 |
+
}
|
| 54 |
+
_FILENAMES = {
|
| 55 |
+
"processed.en": {
|
| 56 |
+
"train": "english-train.json",
|
| 57 |
+
"validation": "english-dev.json",
|
| 58 |
+
"test": "english-test.json",
|
| 59 |
},
|
| 60 |
+
"processed.zh": {
|
| 61 |
+
"train": "train_data.json",
|
| 62 |
+
"validation": "validate_data.json",
|
| 63 |
+
"test": "test_data.json",
|
| 64 |
},
|
| 65 |
}
|
| 66 |
|
|
|
|
| 83 |
),
|
| 84 |
]
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
def _info(self):
|
| 87 |
if self.config.name == "zh":
|
| 88 |
features = datasets.Features(
|
|
|
|
| 137 |
"""Returns SplitGenerators."""
|
| 138 |
*processed, lang = self.config.name.split(".")
|
| 139 |
if processed:
|
| 140 |
+
# data_dir = dl_manager.download(_URLS[lang])
|
| 141 |
+
data_dir = dl_manager.download_and_extract(_URLS[self.config.name])
|
| 142 |
splits = [datasets.Split.TRAIN, datasets.Split.VALIDATION, datasets.Split.TEST]
|
| 143 |
+
return [datasets.SplitGenerator(name=split, gen_kwargs={"filepaths": os.path.join(data_dir, _FILENAMES[self.config.name][split])}) for split in splits]
|
| 144 |
else:
|
| 145 |
+
archive = dl_manager.download(_URLS[self.config.name])
|
| 146 |
+
return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepaths": dl_manager.iter_archive(archive)})]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
def _generate_examples(self, filepaths):
|
| 149 |
"""Yields examples. Iterates over each file and give the creates the corresponding features.
|
|
|
|
| 174 |
array = ""
|
| 175 |
else:
|
| 176 |
id_ = -1
|
| 177 |
+
for filepath, f_in in filepaths:
|
| 178 |
+
# with open(filepath, encoding="utf-8") as f_in:
|
| 179 |
+
# Parameters to just "sectionize" the raw data
|
| 180 |
+
last_part = ""
|
| 181 |
+
last_dialog = {}
|
| 182 |
+
last_list = []
|
| 183 |
+
last_user = ""
|
| 184 |
+
check_list = []
|
| 185 |
+
|
| 186 |
+
# These flags are present to have a single function address both chinese and english data
|
| 187 |
+
# English data is a little hahazard (i.e. the sentences spans multiple different lines),
|
| 188 |
+
# Chinese is compact with one line for doctor and patient.
|
| 189 |
+
conv_flag = False
|
| 190 |
+
des_flag = False
|
| 191 |
+
|
| 192 |
+
while True:
|
| 193 |
+
line = f_in.readline().decode("utf-8")
|
| 194 |
+
if not line:
|
| 195 |
+
break
|
| 196 |
+
|
| 197 |
+
# Extracting the dialog id
|
| 198 |
+
if line[:2] == "id": # Hardcode alert!
|
| 199 |
+
# Handling ID references that may come in the description
|
| 200 |
+
# These were observed in the Chinese dataset and were not
|
| 201 |
+
# followed by numbers
|
| 202 |
+
try:
|
| 203 |
+
dialogue_id = int(re.findall(r"\d+", line)[0])
|
| 204 |
+
except IndexError:
|
| 205 |
+
continue
|
| 206 |
+
|
| 207 |
+
# Extracting the url
|
| 208 |
+
if line[:4] == "http": # Hardcode alert!
|
| 209 |
+
dialogue_url = line.rstrip()
|
| 210 |
+
|
| 211 |
+
# Extracting the patient info from description.
|
| 212 |
+
if line[:11] == "Description": # Hardcode alert!
|
| 213 |
+
last_part = "description"
|
| 214 |
+
last_dialog = {}
|
| 215 |
+
last_list = []
|
| 216 |
+
last_user = ""
|
| 217 |
+
last_conv = {"speaker": "", "utterance": ""}
|
| 218 |
+
while True:
|
| 219 |
+
line = f_in.readline().decode("utf-8")
|
| 220 |
+
if (not line) or (line in ["\n", "\n\r"]):
|
| 221 |
+
break
|
| 222 |
+
else:
|
| 223 |
+
if data_lang == "zh": # Condition in chinese
|
| 224 |
+
if line[:5] == "病情描述:": # Hardcode alert!
|
| 225 |
+
last_user = "病人"
|
| 226 |
+
sen = f_in.readline().decode("utf-8").rstrip()
|
| 227 |
+
des_flag = True
|
| 228 |
|
| 229 |
+
if data_lang == "en":
|
| 230 |
+
last_user = "Patient"
|
| 231 |
+
sen = line.rstrip()
|
| 232 |
+
des_flag = True
|
| 233 |
+
|
| 234 |
+
if des_flag:
|
| 235 |
+
if sen == "":
|
| 236 |
+
continue
|
| 237 |
+
if sen in check_list:
|
| 238 |
+
last_conv["speaker"] = ""
|
| 239 |
+
last_conv["utterance"] = ""
|
| 240 |
+
else:
|
| 241 |
+
last_conv["speaker"] = last_user
|
| 242 |
+
last_conv["utterance"] = sen
|
| 243 |
+
check_list.append(sen)
|
| 244 |
+
des_flag = False
|
| 245 |
+
break
|
| 246 |
+
# Extracting the conversation info from dialogue.
|
| 247 |
+
elif line[:8] == "Dialogue": # Hardcode alert!
|
| 248 |
+
if last_part == "description" and len(last_conv["utterance"]) > 0:
|
| 249 |
+
last_part = "dialogue"
|
| 250 |
+
if data_lang == "zh":
|
| 251 |
+
last_user = "病人"
|
| 252 |
|
| 253 |
+
if data_lang == "en":
|
| 254 |
+
last_user = "Patient"
|
|
|
|
| 255 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
while True:
|
| 257 |
+
line = f_in.readline().decode("utf-8")
|
| 258 |
if (not line) or (line in ["\n", "\n\r"]):
|
| 259 |
+
conv_flag = False
|
| 260 |
+
last_user = ""
|
| 261 |
+
last_list.append(copy.deepcopy(last_conv))
|
| 262 |
+
# To ensure close of conversation, only even number of sentences
|
| 263 |
+
# are extracted
|
| 264 |
+
last_turn = len(last_list)
|
| 265 |
+
if int(last_turn / 2) > 0:
|
| 266 |
+
temp = int(last_turn / 2)
|
| 267 |
+
id_ += 1
|
| 268 |
+
last_dialog["file_name"] = filepath
|
| 269 |
+
last_dialog["dialogue_id"] = dialogue_id
|
| 270 |
+
last_dialog["dialogue_url"] = dialogue_url
|
| 271 |
+
last_dialog["dialogue_turns"] = last_list[: temp * 2]
|
| 272 |
+
yield id_, last_dialog
|
| 273 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
if data_lang == "zh":
|
| 276 |
+
if line[:3] == "病人:" or line[:3] == "医生:": # Hardcode alert!
|
| 277 |
+
user = line[:2] # Hardcode alert!
|
| 278 |
+
line = f_in.readline().decode("utf-8")
|
| 279 |
+
conv_flag = True
|
| 280 |
|
| 281 |
+
# The elif block is to ensure that multi-line sentences are captured.
|
| 282 |
+
# This has been observed only in english.
|
| 283 |
if data_lang == "en":
|
| 284 |
+
if line.strip() == "Patient:" or line.strip() == "Doctor:": # Hardcode alert!
|
| 285 |
+
user = line.replace(":", "").rstrip()
|
| 286 |
+
line = f_in.readline().decode("utf-8")
|
| 287 |
+
conv_flag = True
|
| 288 |
+
elif line[:2] != "id": # Hardcode alert!
|
| 289 |
+
conv_flag = True
|
| 290 |
+
|
| 291 |
+
# Continues till the next ID is parsed
|
| 292 |
+
if conv_flag:
|
| 293 |
+
sen = line.rstrip()
|
| 294 |
+
if sen == "":
|
| 295 |
+
continue
|
| 296 |
+
|
| 297 |
+
if user == last_user:
|
| 298 |
+
last_conv["utterance"] = last_conv["utterance"] + sen
|
| 299 |
+
else:
|
| 300 |
+
last_user = user
|
| 301 |
last_list.append(copy.deepcopy(last_conv))
|
| 302 |
+
last_conv["utterance"] = sen
|
| 303 |
+
last_conv["speaker"] = user
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|