Skip to main content

ConText algorithm using spaCy for clinical NLP

Project description

cycontext

A Python implementation of the ConText algorithm for clinical text concept assertion using the spaCy framework

Overview

This package implements the ConText algorithm within the spaCy framework. ConText detects semantic modifiers such as negation, family history, and certainty in clinical text by relating target concepts (such as "pneumonia") with semantic modifiers (such as "no evidence of").

This builds on pyConText, which extracts both targets and modifiers using regular expressions and relates them using a NetworkX graph.

See notebooks/ for detailed explanations and examples.

Key Features

  • cycontext is used as a modular spaCy component
  • Modifiers are defined in a knowledge base which are used to modify target spans
  • Results are stored in a custom attribute Doc._.context_graph
  • Modifier -> target relationships are stored in Doc._.context_graph.edges

Basic Usage

Installation

Install cycontext using the setup.py script:

python setup.py install

In the future cycontext should be added to PyPi and be made available through conda.

Example

import spacy

from cycontext import ConTextComponent, ConTextItem

nlp = spacy.load("en_core_web_sm")
text = "There is no evidence of pneumonia."

###########################################
# Add code for extracting target concepts #
###########################################
doc = nlp(text)
print(doc.ents)
>>> (pneumonia,)

item_data = [ConTextItem("no evidence of", "DEFINITE_NEGATED_EXISTENCE", rule="forward")]
context = ConTextComponent(nlp)
context.add(item_data)
nlp.add_pipe(context, last=True)

doc = nlp(text)
print(doc._.context_graph.edges)
>>> [(pneumonia, <TagObject> [No evidence of, definite_negated_existence])]

for target in doc.ents:
    print(target._.is_experienced)

>>> False

Visualization

We can use spaCy's visualizers to display the results of cycontext. This NER-style visualization highlights the targets and modifiers in text:

from cycontext import viz
viz.visualize_ent(doc, colors ={"EVIDENCE_OF_PNEUMONIA": "orange",
                                   "DEFINITE_NEGATED_EXISTENCE": "#34b1eb",
                               })

While this dependency-style visualization shows the relationships between targets and modifiers:

viz.visualize_dep(doc)

Contact Information

Alec Chapman: abchapman93@gmail.com

Project details


Release history Release notifications

This version

0.1.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for cycontext, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size cycontext-0.1.2.tar.gz (8.8 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page