An updated version of NegEx for Python with a working example.
This is adapted from Brian Chapman's Negex repo: https://github.com/chapmanbe/negex
readme.txtthe original readme file from the original Python codenegex.pythe file that implements the negex class to do the negationsREADME.mdthis file. an overview of this updated version and repowrapper.pya python script that demonstrates how to use the negex class, modified from the originalnegex_triggers.txta list of negation termsAnnotations-1-120.txta list of sample clinical text annotations to negatesandbox.pya short working example
from negex import *
sample_txt = 'He does not have chest pain. He does have a cough. He denies lightheadedness.'
clinical_terms = ['chest pain', 'cough', 'lightheadedness']
# Set up Negex class
rfile = open(r'negex_triggers.txt')
irules = sortRules(rfile.readlines())
tagger = negTagger(sentence = sample_txt, phrases = clinical_terms, rules = irules, negP=False)
# See the full tagged sentence
tagger.getNegTaggedSentence()
## 'He does [PREN]not have[PREN] [NEGATED]chest pain[NEGATED]. He does have a [NEGATED]cough[NEGATED]. He [PREN]denies[PREN] [NEGATED]lightheadedness[NEGATED].'
# See whether or not something was NEGATED
tagger.getNegationFlag()
### 'negated'
# See the negations scopes
tagger.getScopes()
### ['[NEGATED]chest_pain[NEGATED]. He does have a [NEGATED]cough[NEGATED].',
'[NEGATED]lightheadedness[NEGATED].']