File size: 28,812 Bytes
f653c3f |
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "809c06bc",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import spacy"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e8b0dce4",
"metadata": {},
"outputs": [],
"source": [
"import en_ner_bc5cdr_md\n",
"import en_core_med7_lg"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "84d4467f",
"metadata": {},
"outputs": [],
"source": [
"# To install spaCy's pre-trained model en_ner_bc5cdr_md, en_core_med7_lg use the link below. You can also install the transformer\n",
"\n",
"# !pip install https://s3-us-west-2.amazonaws.com/ai2-s2-scispacy/releases/v0.5.1/en_ner_bc5cdr_md-0.5.1.tar.gz\n",
"# !pip install https://huggingface.co/kormilitzin/en_core_med7_trf/resolve/main/en_core_med7_trf-any-py3-none-any.whl --user\n",
"# !pip install https://huggingface.co/kormilitzin/en_core_med7_lg/resolve/main/en_core_med7_lg-any-py3-none-any.whl --user"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1ee710ca",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>description</th>\n",
" <th>medical_specialty</th>\n",
" <th>sample_name</th>\n",
" <th>transcription</th>\n",
" <th>keywords</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>A 23-year-old white female presents with comp...</td>\n",
" <td>Allergy / Immunology</td>\n",
" <td>Allergic Rhinitis</td>\n",
" <td>SUBJECTIVE:, This 23-year-old white female pr...</td>\n",
" <td>allergy / immunology, allergic rhinitis, aller...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Consult for laparoscopic gastric bypass.</td>\n",
" <td>Bariatrics</td>\n",
" <td>Laparoscopic Gastric Bypass Consult - 2</td>\n",
" <td>PAST MEDICAL HISTORY:, He has difficulty climb...</td>\n",
" <td>bariatrics, laparoscopic gastric bypass, weigh...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Consult for laparoscopic gastric bypass.</td>\n",
" <td>Bariatrics</td>\n",
" <td>Laparoscopic Gastric Bypass Consult - 1</td>\n",
" <td>HISTORY OF PRESENT ILLNESS: , I have seen ABC ...</td>\n",
" <td>bariatrics, laparoscopic gastric bypass, heart...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2-D M-Mode. Doppler.</td>\n",
" <td>Cardiovascular / Pulmonary</td>\n",
" <td>2-D Echocardiogram - 1</td>\n",
" <td>2-D M-MODE: , ,1. Left atrial enlargement wit...</td>\n",
" <td>cardiovascular / pulmonary, 2-d m-mode, dopple...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2-D Echocardiogram</td>\n",
" <td>Cardiovascular / Pulmonary</td>\n",
" <td>2-D Echocardiogram - 2</td>\n",
" <td>1. The left ventricular cavity size and wall ...</td>\n",
" <td>cardiovascular / pulmonary, 2-d, doppler, echo...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" description \\\n",
"0 A 23-year-old white female presents with comp... \n",
"1 Consult for laparoscopic gastric bypass. \n",
"2 Consult for laparoscopic gastric bypass. \n",
"3 2-D M-Mode. Doppler. \n",
"4 2-D Echocardiogram \n",
"\n",
" medical_specialty sample_name \\\n",
"0 Allergy / Immunology Allergic Rhinitis \n",
"1 Bariatrics Laparoscopic Gastric Bypass Consult - 2 \n",
"2 Bariatrics Laparoscopic Gastric Bypass Consult - 1 \n",
"3 Cardiovascular / Pulmonary 2-D Echocardiogram - 1 \n",
"4 Cardiovascular / Pulmonary 2-D Echocardiogram - 2 \n",
"\n",
" transcription \\\n",
"0 SUBJECTIVE:, This 23-year-old white female pr... \n",
"1 PAST MEDICAL HISTORY:, He has difficulty climb... \n",
"2 HISTORY OF PRESENT ILLNESS: , I have seen ABC ... \n",
"3 2-D M-MODE: , ,1. Left atrial enlargement wit... \n",
"4 1. The left ventricular cavity size and wall ... \n",
"\n",
" keywords \n",
"0 allergy / immunology, allergic rhinitis, aller... \n",
"1 bariatrics, laparoscopic gastric bypass, weigh... \n",
"2 bariatrics, laparoscopic gastric bypass, heart... \n",
"3 cardiovascular / pulmonary, 2-d m-mode, dopple... \n",
"4 cardiovascular / pulmonary, 2-d, doppler, echo... "
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"med = pd.read_csv('mtsamples.csv', index_col=0)\n",
"med.head()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "453c00da",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"description 0\n",
"medical_specialty 0\n",
"sample_name 0\n",
"transcription 33\n",
"keywords 1068\n",
"dtype: int64"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"med.isnull().sum()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ac84c247",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(4999, 5)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"med.shape"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "bc3d4bd7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"Int64Index: 4999 entries, 0 to 4998\n",
"Data columns (total 5 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 description 4999 non-null object\n",
" 1 medical_specialty 4999 non-null object\n",
" 2 sample_name 4999 non-null object\n",
" 3 transcription 4966 non-null object\n",
" 4 keywords 3931 non-null object\n",
"dtypes: object(5)\n",
"memory usage: 234.3+ KB\n"
]
}
],
"source": [
"med.info()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "79682feb",
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"\n",
"med['transcription'] = med['transcription'].astype('str')\n",
"med['transcription'] = med['transcription'].apply(lambda x: re.sub('(\\.,)', \". \", x))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "6121d1e4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Document:\n",
"SUBJECTIVE:, This 23-year-old white female presents with complaint of allergies. She used to have allergies when she lived in Seattle but she thinks they are worse here. In the past, she has tried Claritin, and Zyrtec. Both worked for short time but then seemed to lose effectiveness. She has used Allegra also. She used that last summer and she began using it again two weeks ago. It does not appear to be working very well. She has used over-the-counter sprays but no prescription nasal sprays. She does have asthma but doest not require daily medication for this and does not think it is flaring up. MEDICATIONS: , Her only medication currently is Ortho Tri-Cyclen and the Allegra. ALLERGIES: , She has no known medicine allergies. OBJECTIVE:,Vitals: Weight was 130 pounds and blood pressure 124/78. HEENT: Her throat was mildly erythematous without exudate. Nasal mucosa was erythematous and swollen. Only clear drainage was seen. TMs were clear. Neck: Supple without adenopathy. Lungs: Clear. ASSESSMENT:, Allergic rhinitis. PLAN:,1. She will try Zyrtec instead of Allegra again. Another option will be to use loratadine. She does not think she has prescription coverage so that might be cheaper. 2. Samples of Nasonex two sprays in each nostril given for three weeks. A prescription was written as well.\n",
"Annotations:\n",
"{'entities': [(200, 208, 'DRUG'), (214, 220, 'DRUG'), (549, 554, 'FREQUENCY'), (1070, 1076, 'DRUG'), (1134, 1144, 'DRUG'), (1237, 1244, 'DRUG'), (1245, 1248, 'DOSAGE'), (1249, 1255, 'FORM'), (1259, 1263, 'DOSAGE'), (1278, 1293, 'DURATION')]}\n"
]
}
],
"source": [
"nlp = spacy.load(\"en_core_med7_lg\")\n",
"\n",
"# This function generate anotation for each entities and label\n",
"def generate_annotation(texts):\n",
" annotations = []\n",
" for text in texts:\n",
" doc = nlp(text)\n",
" entities = []\n",
" for ent in doc.ents:\n",
" entities.append((ent.start_char, ent.end_char, ent.label_))\n",
" annotations.append((text, {'entities': entities}))\n",
" return annotations\n",
"\n",
"# Extract text entities and labels from the dataset (transcription)\n",
"medical_doc = med['transcription'].tolist()\n",
"\n",
"# Let's generate annotations\n",
"annotations = generate_annotation(medical_doc)\n",
"\n",
"# Let's print documents and annotations\n",
"print(\"Document:\")\n",
"print(annotations[0][0]) # first document text\n",
"print(\"Annotations:\")\n",
"print(annotations[0][1]) # annotation for the first document"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ac6f901",
"metadata": {},
"outputs": [],
"source": [
"#med['annotation'] = med['transcription'].apply(lambda x:generate_annotation(x))"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "846fddb7",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<span class=\"tex2jax_ignore\"><div class=\"entities\" style=\"line-height: 2.5; direction: ltr\">SUBJECTIVE:, This 23-year-old white female presents with complaint of allergies. She used to have allergies when she lived in Seattle but she thinks they are worse here. In the past, she has tried \n",
"<mark class=\"entity\" style=\"background: #3cb44b; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;\">\n",
" Claritin\n",
" <span style=\"font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; vertical-align: middle; margin-left: 0.5rem\">DRUG</span>\n",
"</mark>\n",
", and \n",
"<mark class=\"entity\" style=\"background: #3cb44b; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;\">\n",
" Zyrtec\n",
" <span style=\"font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; vertical-align: middle; margin-left: 0.5rem\">DRUG</span>\n",
"</mark>\n",
". Both worked for short time but then seemed to lose effectiveness. She has used Allegra also. She used that last summer and she began using it again two weeks ago. It does not appear to be working very well. She has used over-the-counter sprays but no prescription nasal sprays. She does have asthma but doest not require \n",
"<mark class=\"entity\" style=\"background: #f58231; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;\">\n",
" daily\n",
" <span style=\"font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; vertical-align: middle; margin-left: 0.5rem\">FREQUENCY</span>\n",
"</mark>\n",
" medication for this and does not think it is flaring up. MEDICATIONS: , Her only medication currently is Ortho Tri-Cyclen and the Allegra. ALLERGIES: , She has no known medicine allergies. OBJECTIVE:,Vitals: Weight was 130 pounds and blood pressure 124/78. HEENT: Her throat was mildly erythematous without exudate. Nasal mucosa was erythematous and swollen. Only clear drainage was seen. TMs were clear. Neck: Supple without adenopathy. Lungs: Clear. ASSESSMENT:, Allergic rhinitis. PLAN:,1. She will try \n",
"<mark class=\"entity\" style=\"background: #3cb44b; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;\">\n",
" Zyrtec\n",
" <span style=\"font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; vertical-align: middle; margin-left: 0.5rem\">DRUG</span>\n",
"</mark>\n",
" instead of Allegra again. Another option will be to use \n",
"<mark class=\"entity\" style=\"background: #3cb44b; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;\">\n",
" loratadine\n",
" <span style=\"font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; vertical-align: middle; margin-left: 0.5rem\">DRUG</span>\n",
"</mark>\n",
". She does not think she has prescription coverage so that might be cheaper. 2. Samples of \n",
"<mark class=\"entity\" style=\"background: #3cb44b; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;\">\n",
" Nasonex\n",
" <span style=\"font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; vertical-align: middle; margin-left: 0.5rem\">DRUG</span>\n",
"</mark>\n",
" \n",
"<mark class=\"entity\" style=\"background: #e6194B; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;\">\n",
" two\n",
" <span style=\"font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; vertical-align: middle; margin-left: 0.5rem\">DOSAGE</span>\n",
"</mark>\n",
" \n",
"<mark class=\"entity\" style=\"background: #ffd8b1; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;\">\n",
" sprays\n",
" <span style=\"font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; vertical-align: middle; margin-left: 0.5rem\">FORM</span>\n",
"</mark>\n",
" in \n",
"<mark class=\"entity\" style=\"background: #e6194B; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;\">\n",
" each\n",
" <span style=\"font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; vertical-align: middle; margin-left: 0.5rem\">DOSAGE</span>\n",
"</mark>\n",
" nostril given \n",
"<mark class=\"entity\" style=\"background: #ffe119; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;\">\n",
" for three weeks\n",
" <span style=\"font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; vertical-align: middle; margin-left: 0.5rem\">DURATION</span>\n",
"</mark>\n",
". A prescription was written as well.</div></span>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[('Claritin', 'DRUG'),\n",
" ('Zyrtec', 'DRUG'),\n",
" ('daily', 'FREQUENCY'),\n",
" ('Zyrtec', 'DRUG'),\n",
" ('loratadine', 'DRUG'),\n",
" ('Nasonex', 'DRUG'),\n",
" ('two', 'DOSAGE'),\n",
" ('sprays', 'FORM'),\n",
" ('each', 'DOSAGE'),\n",
" ('for three weeks', 'DURATION')]"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from spacy import displacy\n",
"nlp = spacy.load(\"en_core_med7_lg\")\n",
"\n",
"# Create distict colours for labels\n",
"\n",
"col_dict = {}\n",
"s_colours = ['#e6194B', '#3cb44b', '#ffe119', '#ffd8b1', '#f58231', '#f032e6', '#42d4f4']\n",
"for label, colour in zip(nlp.pipe_labels['ner'], s_colours):\n",
" col_dict[label] = colour\n",
"\n",
"options = {'ents': nlp.pipe_labels['ner'], 'colors':col_dict}\n",
"\n",
"transcription = med['transcription'][0]\n",
"doc = nlp(transcription)\n",
"\n",
"spacy.displacy.render(doc, style = 'ent', jupyter = True, options = options)\n",
"\n",
"[(ent.text, ent.label_) for ent in doc.ents]"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "44ec9bdb",
"metadata": {},
"outputs": [],
"source": [
"med_adj = med.sample(n=111, replace = False, random_state=42)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "b38de759",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Text: snoring, Entity Type: DISEASE\n",
"Text: pains, Entity Type: DISEASE\n",
"Text: knee pain, Entity Type: DISEASE\n",
"Text: pain, Entity Type: DISEASE\n",
"Text: ankle pain, Entity Type: DISEASE\n",
"Text: gastroesophageal reflux disease, Entity Type: DISEASE\n",
"Text: Heart disease, Entity Type: DISEASE\n",
"Text: stroke, Entity Type: DISEASE\n",
"Text: diabetes, Entity Type: DISEASE\n",
"Text: obesity, Entity Type: DISEASE\n",
"Text: hypertension, Entity Type: DISEASE\n",
"Text: allergic, Entity Type: DISEASE\n",
"Text: Penicillin, Entity Type: CHEMICAL\n",
"Text: chest pain, Entity Type: DISEASE\n",
"Text: coronary artery disease, Entity Type: DISEASE\n",
"Text: congestive heart failure, Entity Type: DISEASE\n",
"Text: arrhythmia, Entity Type: DISEASE\n",
"Text: atrial fibrillation, Entity Type: DISEASE\n",
"Text: cholesterol, Entity Type: CHEMICAL\n",
"Text: pulmonary embolism, Entity Type: DISEASE\n",
"Text: CVA, Entity Type: CHEMICAL\n",
"Text: venous insufficiency, Entity Type: DISEASE\n",
"Text: thrombophlebitis, Entity Type: DISEASE\n",
"Text: asthma, Entity Type: DISEASE\n",
"Text: shortness of breath, Entity Type: DISEASE\n",
"Text: COPD, Entity Type: DISEASE\n",
"Text: emphysema, Entity Type: DISEASE\n",
"Text: sleep apnea, Entity Type: DISEASE\n",
"Text: diabetes, Entity Type: DISEASE\n",
"Text: osteoarthritis, Entity Type: DISEASE\n",
"Text: rheumatoid arthritis, Entity Type: DISEASE\n",
"Text: hiatal hernia, Entity Type: DISEASE\n",
"Text: peptic ulcer disease, Entity Type: DISEASE\n",
"Text: gallstones, Entity Type: DISEASE\n",
"Text: pancreatitis, Entity Type: DISEASE\n",
"Text: fatty liver, Entity Type: DISEASE\n",
"Text: hepatitis, Entity Type: DISEASE\n",
"Text: hemorrhoids, Entity Type: DISEASE\n",
"Text: bleeding, Entity Type: DISEASE\n",
"Text: polyps, Entity Type: DISEASE\n",
"Text: incontinence, Entity Type: DISEASE\n",
"Text: urinary stress incontinence, Entity Type: DISEASE\n",
"Text: cancer, Entity Type: DISEASE\n",
"Text: cellulitis, Entity Type: DISEASE\n",
"Text: meningitis, Entity Type: DISEASE\n",
"Text: encephalitis, Entity Type: DISEASE\n"
]
}
],
"source": [
"nlp = spacy.load(\"en_ner_bc5cdr_md\")\n",
"\n",
"transcription = med['transcription'].iloc[1]\n",
"doc= nlp(transcription)\n",
"\n",
"# Let's extract and print all the entity\n",
"for ent in doc.ents:\n",
" print(f\"Text: {ent.text}, Entity Type: {ent.label_}\")\n",
" #print(f\"Text: {ent.text}, Start: {ent.start_char}, End: {ent.end_char}, Entity Type: {ent.label_}\") you can also use this"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "aad31e23",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"DRUG_DOSE 514 517 Diastat 20 mg\n",
"DRUG_DOSE 518 521 Topamax 25 mg\n",
"DRUG_DOSE 532 535 Tranxene 15 mg\n",
"DRUG_DOSE 538 541 Depakote 125 mg\n",
"DRUG_DOSE 729 732 Depacon 250 mg\n",
"DRUG_DOSE 266 269 Pepcid 40 mg\n",
"DRUG_DOSE 109 112 furosemide 40 mg\n",
"DRUG_DOSE 897 900 diltiazem 120 mg\n",
"DRUG_DOSE 433 436 Aspirin 325 mg\n",
"DRUG_DOSE 443 446 Lisinopril 40 mg\n",
"DRUG_DOSE 453 456 Felodipine 10 mg\n",
"DRUG_DOSE 465 468 Con 20 mEq\n",
"DRUG_DOSE 475 478 Omeprazole 20 mg\n",
"DRUG_DOSE 488 491 MiraLax 17 g\n",
"DRUG_DOSE 498 501 Lasix 20 mg\n",
"DRUG_DOSE 282 285 Omeprazole 40 mg\n",
"DRUG_DOSE 25 28 Prozac 20 mg\n",
"DRUG_DOSE 274 277 Rocephin 250 mg\n",
"DRUG_DOSE 278 281 azithromycin 1000 mg\n",
"DRUG_DOSE 504 507 Coumadin 5 mg\n",
"DRUG_DOSE 524 527 Aspirin 81 mg\n",
"DRUG_DOSE 533 536 Hydrochlorothiazide 25 mg\n",
"DRUG_DOSE 542 545 Plendil 10 mg\n",
"DRUG_DOSE 550 553 Lipitor 40 mg\n",
"DRUG_DOSE 955 958 dexamethasone 4 mg\n",
"DRUG_DOSE 286 289 Plavix 75 mg\n",
"DRUG_DOSE 294 297 metoprolol 25 mg\n",
"DRUG_DOSE 302 305 Flomax 0.4 mg\n",
"DRUG_DOSE 310 313 Zocor 20 mg\n",
"DRUG_DOSE 327 330 lisinopril 10 mg\n",
"DRUG_DOSE 78 81 iCAD Second Look\n",
"DRUG_DOSE 334 337 iCAD Second Look\n",
"DRUG_DOSE 27 30 fentanyl 25 mcg\n",
"DRUG_DOSE 100 103 Xylocaine 1%\n",
"DRUG_DOSE 66 69 Lexiscan 0.4 mg\n",
"DRUG_DOSE 187 190 lidocaine 2%\n",
"DRUG_DOSE 194 197 Marcaine 1.7 mL\n",
"DRUG_DOSE 258 261 Plaquenil 200 mg\n",
"DRUG_DOSE 268 271 Fosamax 170 mg\n",
"DRUG_DOSE 290 293 acid 1 mg\n",
"DRUG_DOSE 299 302 Trilisate 1000 mg\n",
"DRUG_DOSE 320 323 Hydrochlorothiazide 15 mg\n",
"DRUG_DOSE 330 333 Lopressor 50 mg\n",
"DRUG_DOSE 344 347 Trazodone 100 mg\n",
"DRUG_DOSE 353 356 Prempro 0.625 mg\n",
"DRUG_DOSE 362 365 Aspirin 325 mg\n",
"DRUG_DOSE 372 375 Lipitor 10 mg\n",
"DRUG_DOSE 381 384 Pepcid 20 mg\n",
"DRUG_DOSE 391 394 Reglan 10 mg\n",
"DRUG_DOSE 403 406 Celexa 20 mg\n",
"DRUG_DOSE 721 724 Azithromycin 5-\n",
"DRUG_DOSE 746 749 Atarax 25 mg\n",
"DRUG_DOSE 390 393 sotalol 80 mg\n",
"DRUG_DOSE 398 401 metoprolol 50 mg\n",
"DRUG_DOSE 414 417 digoxin 0.125 mg\n",
"DRUG_DOSE 504 507 cephalexin 500 mg\n",
"DRUG_DOSE 56 59 Versed 3,\n",
"DRUG_DOSE 59 62 Demerol 25 and\n",
"DRUG_DOSE 230 233 aspirin one tablet\n",
"DRUG_DOSE 249 252 Warfarin 2.5 mg\n",
"DRUG_DOSE 498 501 Synthroid 0.5 mg\n",
"DRUG_DOSE 507 510 Plavix 75 mg\n",
"DRUG_DOSE 515 518 acid 1 mg\n",
"DRUG_DOSE 522 525 Diovan 80 mg\n",
"DRUG_DOSE 529 532 Renagel 2 tablets\n",
"DRUG_DOSE 540 543 Lasix 40 mg\n",
"DRUG_DOSE 552 555 lovastatin 20 mg\n",
"DRUG_DOSE 559 562 Coreg 3.125 mg\n",
"DRUG_DOSE 577 580 Phenergan 25 mg\n",
"DRUG_DOSE 589 592 Pepcid 20 mg\n",
"DRUG_DOSE 596 599 Vicodin 1 tablet\n",
"DRUG_DOSE 609 612 Levaquin 250 mg\n",
"DRUG_DOSE 89 92 Coreg 6.25 mg\n",
"DRUG_DOSE 132 135 Coreg 6.25 mg\n",
"DRUG_DOSE 140 143 Simvastatin 40 mg\n",
"DRUG_DOSE 148 151 Lisinopril 5 mg\n",
"DRUG_DOSE 156 159 Protonix 40 mg\n",
"DRUG_DOSE 163 166 Aspirin 160 mg\n",
"DRUG_DOSE 172 175 Lasix 20 mg\n",
"DRUG_DOSE 195 198 Advair 500/50 puff\n"
]
}
],
"source": [
"from spacy.matcher import Matcher\n",
"\n",
"# Let's load the model\n",
"nlp = spacy.load(\"en_core_med7_lg\")\n",
"\n",
"patterns = [\n",
" [{\"ENT_TYPE\": \"DRUG\"}, {\"LIKE_NUM\": True}, {\"IS_ASCII\": True}],\n",
" [{\"LOWER\": {\"IN\": [\"mg\", \"g\", \"ml\"]}}, {\"ENT_TYPE\": \"DRUG\"}],\n",
" [{\"ENT_TYPE\": \"DRUG\"}, {\"IS_DIGIT\": True, \"OP\": \"?\"}, {\"LOWER\": {\"IN\": [\"mg\", \"g\", \"ml\"]}}]\n",
"]\n",
"\n",
"matcher = Matcher(nlp.vocab)\n",
"matcher.add(\"DRUG_DOSE\", patterns)\n",
"\n",
"for transcription in med_adj['transcription']:\n",
" doc = nlp(transcription)\n",
" matches = matcher(doc)\n",
" for match_id, start, end in matches:\n",
" string_id = nlp.vocab.strings[match_id]\n",
" span = doc[start:end]\n",
" print(string_id, start, end, span.text)\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "815b1041",
"metadata": {},
"outputs": [],
"source": [
"med_adj = med.sample(n=3, replace = False, random_state=42)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "824246e5",
"metadata": {},
"outputs": [],
"source": [
"# Let's load our pretrained spacy model\n",
"\n",
"nlp = spacy.load(\"en_core_med7_lg\")\n",
"\n",
"# this function will extract relevant entities and labels needed from medical transcription \n",
"\n",
"def extract_keywords(text):\n",
" doc = nlp(text)\n",
" entities = []\n",
" entities = [(ent.text, ent.label_) for ent in doc.ents]\n",
" return entities\n",
"\n",
"# Lets define our categories\n",
"surgery_keywords = [\"surgery\", \"operation\", \"procedure\", \"acute Cholangitis\", \"surgisis\", \"appendicitis\"]\n",
"cardio_pul_keywords = [\"heart\", \"cardiovascular\", \"pulmonary\", \"lungs\"]\n",
"orthopaedic_keywords = [\"orthopaedic\", \"bone\", \"joint\", \"fracture\"]\n",
"neurology_keywords = [\"neurology\", \"nervours system\", \"brain\", \"nerve\"]\n",
"general_med_keywords = [\"patient\", \"complaining\", \"history\", \"medical\"]\n",
" \n",
"# This will process each medical description and check for relevant keywords\n",
"medical_doc = med['transcription']\n",
"for transcription in medical_doc:\n",
" entities = extract_keywords(transcription.lower())\n",
" \n",
" is_surgery = any(keyword in transcription.lower() for keyword in surgery_keywords)\n",
" is_cardio_pul = any(keyword in transcription.lower() for keyword in cardio_pul_keywords)\n",
" is_orthopaedic = any(keyword in transcription.lower() for keyword in orthopaedic_keywords)\n",
" is_neurology = any(keyword in transcription.lower() for keyword in neurology_keywords)\n",
" is_general_med = any(keyword in transcription.lower() for keyword in general_med_keywords)\n",
" \n",
" print(\"Transcription:\", transcription)\n",
" print(\"Entities:\", entities)\n",
" print(\"Is Surgery:\", is_surgery)\n",
" print(\"Is Cardio Pulmonary:\", is_cardio_pul)\n",
" print(\"Orthopaedic:\", is_orthopaedic)\n",
" print(\"Neurology:\", is_neurology)\n",
" print(\"General Medicine:\", is_general_med)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dcdbcd44",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|