首页 > 解决方案 > 如何同时使用 Med7 和 Negspacy?

问题描述

我正在尝试使用 Spacy 的 med7 和 negspacy,但它们都需要单独的 spacy 版本。如何在同一个脚本中同时使用两者?

我正在使用 spacy 的 en_core_med7_lg 从文本中获取疾病、药物和其他实体

import spacy
import scispacy
from spacy import displacy
from spacy.matcher import PhraseMatcher
from spacy.tokens import Span
from negspacy.negation import Negex

med7 = spacy.load("en_core_med7_lg")

# create distinct colours for labels
col_dict = {}
seven_colours = ['#e6194B', '#3cb44b', '#ffe119', '#ffd8b1', '#f58231', '#f032e6', '#42d4f4']
for label, colour in zip(med7.pipe_labels['ner'], seven_colours):
    col_dict[label] = colour

options = {'ents': med7.pipe_labels['ner'], 'colors':col_dict}

negex = Negex(med7, ent_types=["DISEASE", "NEG_ENTITY"], name="negex",
              neg_termset={'pseudo_negations': ['no further', 'not able to be', 'not certain if', 'not certain whether', 'not necessarily', 
                                                'without any further', 'without difficulty', 'without further', 'might not', 'not only', 
                                                'no increase', 'no significant change', 'no change', 'no definite change', 'not extend', 'not cause', 'neither', 'nor'], 
                           'preceding_negations': ['absence of', 'declined', 'denied', 'denies', 'denying', 'no sign of', 'no signs of', 'not', 
                                                   'not demonstrate', 'symptoms atypical', 'doubt', 'negative for', 'no', 'versus', 'without', 
                                                   "doesn't", 'doesnt', "don't", 'dont', "didn't", 'didnt', "wasn't", 'wasnt', "weren't", 'werent', 
                                                   "isn't", 'isnt', "aren't", 'arent', 'cannot', "can't", 'cant', "couldn't", 'couldnt', 'never', 'neither', 'nor'], 
                           'following_negations': ['declined', 'unlikely', 'was not', 'were not', "wasn't", 'wasnt', "weren't", 'werent', 'neither', 'nor'], 
                           'termination': ['although', 'apart from', 'as there are', 'aside from', 'but', 'except', 'however', 'involving', 'nevertheless', 'still', 'though', 'which', 'yet', 'neither', 'nor']},
              extension_name='',
              chunk_prefix=[]
)
med7.add_pipe("negex", config={"ent_types":["DISEASE", "NEG_ENTITY"]})
text = """77 yo M pt here to discuss Heart rate and BP in office is 192/80 HR is 102 pt states at 3:15pm BP was 170/79 and HR was 114   Patient presented with wife C/o HTN, elevated HR, and Right flank pain Reported wife gave Lasix 20 mg BID in fear of water retention Pmhx CKD4 Sees Nephrology, last OV 5/2020, follow up scheduled 8/2020 Reported HR elevated 112 at home   Reported unable to afford clobetasol cream to treat psoriasis on scalp and elbows   PROBLEM  LIST: Patient Active Problem List Diagnosis   Diabetes mellitus     Aortic atherosclerosis     HTN (hypertension)   Diabetic neuropathy     Microalbuminuria   Diabetic peripheral vascular disorder     Diabetic nephropathy associated with type 2 diabetes mellitus     CKD (chronic kidney disease) stage 4, GFR 15-29 ml/min     Homocysteinemia     Refill clinic medication management patient   Anemia, chronic renal failure, stage 4 (severe)     Chronic systolic congestive heart failure     Anxiety   Hyperparathyroidism due to renal insufficiency     Hypertension associated with type 2 diabetes mellitus     Moderate episode of recurrent major depressive disorder     PVD (peripheral vascular disease)     History of prostate cancer   RUQ pain   Psoriasis    PAST MEDICAL HISTORY: Past Medical History: Diagnosis Date   BPPV (benign paroxysmal positional vertigo) 1/9/2019   Bradycardia 6/11/2019   CKD (chronic kidney disease) stage 3, GFR 30-59 ml/min   1/24/2019  GFR on 9/14/2018: 28 GFR on 11/13/2018: 34 GFR on 1/14/2019: 34 GFR on 05/21/2019 :34   CKD (chronic kidney disease) stage 4, GFR 15-29 ml/min   9/24/2018   Hospital discharge follow-up 12/9/2019   Prostate cancer   6/18/2018   Refill clinic medication management patient 5/13/2019   PAST SURGICAL HISTORY: Past Surgical History: Procedure Laterality Date   ANGIOPLASTY  08/02/2018  L leg   ANGIOPLASTY  06/13/2018  R leg   colonoscopy  05/26/2015  Internal hemorrhoids, otherwise normal   egd  05/26/2015  normal, mild chronic gastritis   FAMILY HISTORY: Family History Problem Relation Name Age of Onset   Cancer Son      testiular   Heart Disease Sister   Stroke Brother    SOCIAL HISTORY: Social History  Socioeconomic History   Marital status: Married   Spouse name: Not on file   Number of children: 3   Years of education: Not on file   Highest education level: Not on file Occupational History   Not on file Social Needs   Financial resource strain: Not on file   Food insecurity:   Worry: Not on file   Inability: Not on file   Transportation needs:   Medical: Not on file   Non-medical: Not on file Tobacco Use   Smoking status: Never Smoker   Smokeless tobacco: Never Used Substance and Sexual Activity   Alcohol use: Yes   Comment: Occasionally   Drug use: No   Sexual activity: Not on file Lifestyle   Physical activity:   Days per week: Not on file   Minutes per session: Not on file   Stress: Not on file Relationships   Social connections:   Talks on phone: Not on file   Gets together: Not on file   Attends religious service: Not on file   Active member of club or organization: Not on file   Attends meetings of clubs or organizations: Not on file   Relationship status: Not on file   Intimate partner violence:   Fear of current or ex partner: Not on file   Emotionally abused: Not on file   Physically abused: Not on file   Forced sexual activity: Not on file Other Topics Concern   Military Service Not Asked   Blood Transfusions Not Asked   Caffeine Concern Yes   Occupational Exposure Not Asked   Hobby Hazards Not Asked   Sleep Concern Not Asked   Stress Concern Not Asked   Weight Concern Not Asked   Special Diet Not Asked   Back Care Not Asked   Exercises Regularly No   Bike Helmet Use Not Asked   Seat Belt Use Not Asked   Performs Self-Exams Not Asked Social History Narrative   Not on file  Social History  Tobacco Use Smoking Status Never Smoker Smokeless Tobacco Never Used  Social History  Substance and Sexual Activity Alcohol Use Yes  Comment: Occasionally  Social History  Substance and Sexual Activity Drug Use No   Immunization History Administered Date(s) Administered   Influenza Vaccine (High Dose) >=65 Years 12/04/2018   Influenza Vaccine (Unspecified) 10/12/2012   Influenza Vaccine >=6 Months 10/12/2012   Pneumococcal 13 Vaccine (PREVNAR-13) 12/10/2015   Pneumococcal 23 Vaccine (PNEUMOVAX-23) 11/04/2009, 02/07/2014, 11/12/2016   Td 11/04/2009   Tdap 11/04/2009    CURRENT  MEDICATIONS: Current Outpatient Medications on File Prior to Visit Medication Sig Dispense Refill   cilostazol (PLETAL) 100 MG tablet TAKE 1 TABLET BY MOUTH TWICE A DAY 180 tablet 3   [DISCONTINUED] citalopram (CELEXA) 10 MG tablet Take 2 tablets (20 mg) by mouth daily. 90 tablet 3   [DISCONTINUED] citalopram (CELEXA) 20 MG tablet TAKE 1 TABLET BY MOUTH ONCE DAILY FOR DEPRESSION 90 tablet 3   [DISCONTINUED] clobetasol propionate (TEMOVATE) 0.05 % cream Apply to affected area twice a day 45 g 1   cloNIDine (CATAPRES) 0.1 MG tablet Take 1 tablet (0.1 mg) by mouth 3 times daily as needed (if SBP > 160). 270 tablet 3   clopidogrel (PLAVIX) 75 MG tablet TAKE 1 TABLET BY MOUTH EVERY DAY 90 tablet 3   [DISCONTINUED] epoetin alfa (PROCRIT) 10000 UNIT/ML injection Procrit 10,000 unit/mL injection solution   felodipine (PLENDIL) 10 MG tablet TAKE 1 TABLET BY MOUTH EVERY DAY 90 tablet 4   furosemide (LASIX) 20 MG tablet Take 1 tablet (20 mg) by mouth daily as needed (edema). 90 tablet 2   glipiZIDE (GLUCOTROL) 10 MG tablet TAKE 1 TABLET BY MOUTH TWICE DAILY FOR DIABETES 180 tablet 0   hydrALAZINE (APRESOLINE) 25 MG tablet TAKE 2 TABLETS BY MOUTH TWICE A DAY 360 tablet 1   [DISCONTINUED] hydrALAZINE (APRESOLINE) 25 MG tablet Take 25 mg by mouth 3 times daily.  4   Insulin Degludec (TRESIBA) 100 UNIT/ML SOLN 15 Units. 15 units   [DISCONTINUED] Insulin Degludec 100 UNIT/ML SOPN 15 units daily 9 mL 2   losartan (COZAAR) 50 MG tablet Take 1 tablet (50 mg) by mouth 2 times daily. Hold dose for systolic blood pressure less than 100. 180 tablet 3   Misc. Devices MISC C-pap setting at 9 cm H2O  Dx: Sleep Apnea 1 each 0   [DISCONTINUED] prazosin (MINIPRESS) 1 MG capsule Take 1 capsule (1 mg) by mouth 3 times daily. 90 capsule 11   simvastatin (ZOCOR) 20 MG tablet TAKE 1 TABLET BY MOUTH EVERY DAY 90 tablet 3  No current facility-administered medications on file prior to visit.  Outpatient Medications Marked as Taking for the 7/23/20 encounter (Office Visit) with Diaz, Anarella, NP Medication Sig Dispense Refill   Insulin Degludec (TRESIBA) 100 UNIT/ML SOLN 15 Units. 15 units   ALLERGIES: Allergies Allergen Reactions   Sulfa Drugs Swelling and Cough    REVIEW OF SYSTEMS: Review of Systems All other systems reviewed and are negative. refer to HPI   PHYSICAL EXAM:  07/23/20 1657 07/23/20 1718 BP: 192/80 160/80 Pulse: 102 86 Resp: 18 Temp: 99  F (37.2  C) SpO2: 96%  Body mass index is 27.28 kg/m .  Ht Readings from Last 1 Encounters: 07/23/20 5' 6" (1.676 m)  Wt Readings from Last 1 Encounters: 07/23/20 76.7 kg (169 lb)     Physical Exam Vitals signs and nursing note reviewed. Constitutional:    Appearance: Normal appearance. He is well-developed. HENT:    Head: Normocephalic and atraumatic. Eyes:    Conjunctiva/sclera: Conjunctivae normal. Neck:    Musculoskeletal: Normal range of motion. Cardiovascular:    Rate and Rhythm: Normal rate and regular rhythm. Pulmonary:    Effort: Pulmonary effort is normal.    Breath sounds: Normal breath sounds. Abdominal:    Palpations: Abdomen is soft.    Tenderness: There is no tenderness. There is no right CVA tenderness. Musculoskeletal: Normal range of motion. Skin:    General: Skin is warm. Neurological:    Mental Status: He is alert and oriented to person, place, and time. Psychiatric:    Mood and Affect: Mood normal.    Behavior: Behavior normal.    Thought Content: Thought content normal.    Judgment: Judgment normal.      ASSESSMENT & PLAN:  Luis was seen today for other.  Diagnoses and all orders for this visit:  RUQ pain Assessment & Plan: Active Negative abd exam Labs ordered VSS Stay hydrated Cont to monitor Follow up prn results  Orders: -     CBC w/ Diff Lavender; Future -     Comprehensive Metabolic Panel Green; Future  Psoriasis Assessment & Plan: Active Ordered Clobetasol solution apply ad Avoid allergens Stay hydrated Cont to monitor Follow up prn  Orders: -     clobetasol propionate (TMOVATE) 0.05 % solution; Apply 1 mL topically 2 times daily. Use a small amount as directed     ICD-10-CM ICD-9-CM 1. D71 pain R10.11 789.01 CBC w/ Diff Lavender    Comprehensive Metabolic Panel Green 2. Psoriasis L40.9 696.1 clobetasol propionate (TMOVATE) 0.05 % solution                """
doc = med7(text)
[(ent.text, ent.label_) for ent in doc.ents]


negated_concepts = []
for e in doc.ents:

    if e._.negex is True:
        negated_concepts.append(e.text.lower())
        print(e.text, e.label_)

print(negated_concepts)

标签: pythondjangospacy

解决方案


不需要negex,只需将您添加neg_termset到管道配置中:

neg_termset={'pseudo_negations': ['no further', 'not able to be', 'not certain if', 'not certain whether', 'not necessarily', 
                                                'without any further', 'without difficulty', 'without further', 'might not', 'not only', 
                                                'no increase', 'no significant change', 'no change', 'no definite change', 'not extend', 'not cause', 'neither', 'nor'], 
                           'preceding_negations': ['absence of', 'declined', 'denied', 'denies', 'denying', 'no sign of', 'no signs of', 'not', 
                                                   'not demonstrate', 'symptoms atypical', 'doubt', 'negative for', 'no', 'versus', 'without', 
                                                   "doesn't", 'doesnt', "don't", 'dont', "didn't", 'didnt', "wasn't", 'wasnt', "weren't", 'werent', 
                                                   "isn't", 'isnt', "aren't", 'arent', 'cannot', "can't", 'cant', "couldn't", 'couldnt', 'never', 'neither', 'nor'], 
                           'following_negations': ['declined', 'unlikely', 'was not', 'were not', "wasn't", 'wasnt', "weren't", 'werent', 'neither', 'nor'], 
                           'termination': ['although', 'apart from', 'as there are', 'aside from', 'but', 'except', 'however', 'involving', 'nevertheless', 'still', 'though', 'which', 'yet', 'neither', 'nor']}

med7.add_pipe("negex", config={"neg_termset": neg_termset, "ent_types":["DISEASE", "NEG_ENTITY"]})

推荐阅读