You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thank you to Markus Hartenfeller for providing this gist solution to importing named entities from outside spacy.
importspacyfromnegspacy.negationimportNegexfromnegspacy.termsetsimporttermsetinput_text="There is no evidence of pneumonia."nlp=spacy.blank("en") # that's sufficient and much more lightweightnlp.add_pipe('sentencizer') # add a single pipeline component (sentence boundaries are needed by ConText)print('pipeline components: ', nlp.pipe_names)
doc=nlp(input_text) # apply pipeline to textprint("Entities before entity infusion: ", doc.ents) # --> no entities detected at this point because the pipeline contains no NER component# define entities based on char offset coordinates as returned by BERN2, as well as the returned entity typescust_ent=doc.char_span(24,33 ,label="DISEASE") # fits to example "There is no evidence of pneumonia."doc.set_ents([cust_ent], default="unmodified")
ents= [(e.text, e.start, e.end, e.label_) foreindoc.ents]
print('After adding entities', ents)
# now apply NegSpacy to custom documentdefault_ts=termset("en_clinical").get_patterns()
negex=Negex(nlp, name='testnegspacy', extension_name="is_negex", ent_types= ["DISEASE"], chunk_prefix=list(), neg_termset=default_ts) # here we could specify our own ruleset by giving a path to a json fle containing our custom rules, default is under /Users/Markus/anaconda3/envs/nlp/lib/python3.10/site-packages/resources/context_rules.jsonnegex(doc) #apply ConText ruleset to our doc, defined by BERN2's entitiesents_2= [(e.text, e.start, e.end, e.label_, e._.is_negex) foreindoc.ents] # has to match the extension flag ._.is_x as defines above when we created the negex objectprint('After Negex semantic modifier detection:', ents_2)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Thank you to Markus Hartenfeller for providing this gist solution to importing named entities from outside spacy.
Beta Was this translation helpful? Give feedback.
All reactions