import spacy import scispacy from spacy import displacy from negspacy.negation import…
- User
import spacy import scispacy from spacy import displacy from negspacy.negation import Negex from negspacy.termsets import termset nlp = spacy.load("en_ner_bc5cdr_md") nlp.add_pipe("negex", config={"ent_types":["DISEASE"]}) text = "hypertension present and diabetes not present" doc = nlp(text) for e in doc.ents: print(e.text, e._.negex)
output: hypertension False diabetes False
why is diabetes showing false when it should be true
- Assistant
- User
import spacy import scispacy from spacy import displacy from negspacy.negation import Negex from negspacy.termsets import termset nlp = spacy.load("en_ner_bc5cdr_md") ts = termset("en_clinical_sensitive") ts.add_patterns({"following_negations": ["not present"]}) nlp.add_pipe( "negex", config={ "neg_termset":ts.get_patterns() } )
text = "CVA tendernesss Not present"
text = "hypertension present and diabetes not present"
text = "diabetes not present"
doc = nlp(text) for e in doc.ents: print(e.text, e._.negex) output: hypertension True diabetes True
now hypertension is showing true when it should be false
- Assistant
- User
import medspacy from medspacy.ner import TargetRule from medspacy.visualization import visualize_ent
Load medspacy model
nlp = medspacy.load() print(nlp.pipe_names)
text = """ Past Medical History:
- Atrial fibrillation
- Type II Diabetes Mellitus
Assessment and Plan: hypertension present and diabetes not present. """
Add rules for target concept extraction
target_matcher = nlp.get_pipe("medspacy_target_matcher") target_rules = [ TargetRule("atrial fibrillation", "PROBLEM"), TargetRule("atrial fibrillation", "PROBLEM", pattern=[{"LOWER": "afib"}]), TargetRule("pneumonia", "PROBLEM"), TargetRule("Type II Diabetes Mellitus", "PROBLEM", pattern=[ {"LOWER": "type"}, {"LOWER": {"IN": ["2", "ii", "two"]}}, {"LOWER": {"IN": ["dm", "diabetes"]}}, {"LOWER": "mellitus", "OP": "?"} ]), TargetRule("warfarin", "MEDICATION") ] target_matcher.add(target_rules)
doc = nlp(text) visualize_ent(doc)
whats wrong in this code
- Assistant
Your code snippet demonstrates how to use medSpaCy for extracting and visualizing medical information from text. However, there's a small issue with how you've defined one of your s for "Type II Diabetes Mellitus". The problem lies in the pattern specification part of the rule. When defining patterns for spaCy (and by extension, medSpaCy), the values for keys in each dictionary of the pattern should not be enclosed in curly braces () unless you're using them to specify an attribute like . The correct way to define a list of options for a token's lowercased text in the pattern is by using the attribute within the dictionary. Here's the corrected part of your rule definition: