clinlp.ie.qualifier

Functionality for extracting qualifiers from clinical text.

clinlp.ie.qualifier.context_algorithm

The Context Algorithm implemented as a spaCy component.

class clinlp.ie.qualifier.context_algorithm.ContextRuleDirection(*values)

Bases: Enum

Direction of a rule, as in the original Context Algorithm.

PRECEDING = 1

For triggers that precede entities.

FOLLOWING = 2

For triggers that follow entities.

BIDIRECTIONAL = 3

For triggers that can both precede and follow entities.

PSEUDO = 4

For pseudo triggers.

TERMINATION = 5

For termination triggers.

class clinlp.ie.qualifier.context_algorithm.ContextRule(pattern: str | list[dict[str, str]], direction: ContextRuleDirection, qualifier: Qualifier, max_scope: int | None = None)

Bases: object

A Context rule, as in the original Context Algorithm.

pattern: str | list[dict[str, str]]

The pattern to look for in text. Either a string, or a spaCy pattern (list).

direction: ContextRuleDirection

The Context rule direction.

qualifier: Qualifier

The qualifier to apply.

max_scope: int | None = None

The maximum number of tokens a trigger ranges over, or None for using sentence boundaries.

class clinlp.ie.qualifier.context_algorithm.ContextAlgorithm(nlp: Language, *, phrase_matcher_attr: str = 'TEXT', load_rules: bool = True, rules: str | dict | None = '/home/docs/checkouts/readthedocs.org/user_builds/clinlp/checkouts/latest/src/clinlp/resources/context_rules.json', **kwargs)

Bases: QualifierDetector

spaCy pipeline component that implements the Context Algorithm.

For more information, see the original paper: https://doi.org/10.1016%2Fj.jbi.2009.05.002

__init__(nlp: Language, *, phrase_matcher_attr: str = 'TEXT', load_rules: bool = True, rules: str | dict | None = '/home/docs/checkouts/readthedocs.org/user_builds/clinlp/checkouts/latest/src/clinlp/resources/context_rules.json', **kwargs) None

Initialize the Context Algorithm.

Parameters:
  • nlp – The spaCy language model.

  • phrase_matcher_attr – The token attribute to match phrases on (e.g. TEXT, ORTH, NORM).

  • load_rules – Whether to parse any rules. Set this to True to load the default builtin rules. Set this to False to use ContextAlgorithm.add_rules to add ContextRules manually.

  • rules – A dictionary of rules, or a path to a json containing the rules. See the clinlp.resources dir for an example.

Raises:

ValueError – If no rules are provided and load_rules is set to True.

property qualifier_classes: dict[str, QualifierClass]

Obtain the qualifier classes that a QualifierDetector initializes.

These are used to initialize the default qualifiers for each entity.

Returns:

dict[str, QualifierClass] – The qualifier classes.

add_rule(rule: ContextRule) None

Add a rule to the Context Algorithm.

Parameters:

rule – The rule to add.

Raises:

TypeError – If the rule pattern is not a string or a list.

add_rules(rules: list[ContextRule]) None

Add multiple rules to the Context Algorithm.

Parameters:

rules – The rules to add.

clinlp.ie.qualifier.qualifier

Reusable components for detecting qualifiers in clinical text.

clinlp.ie.qualifier.qualifier.qualifiers_to_str(ent: Span) set[str] | None

Get qualifier information in string format.

Parameters:

ent – The entity to get qualifiers from.

Returns:

Optional[set[str]] – The qualifiers in string format, e.g. {'Presence.Present', ...}, or None if no qualifiers are present.

clinlp.ie.qualifier.qualifier.qualifiers_to_dict(ent: Span) list[dict] | None

Get qualifier information in dictionary format.

Parameters:

ent – The entity to get qualifiers from.

Returns:

Optional[list[dict]] – The qualifiers in dict format, e.g. [{'Name': 'Presence', 'Value': 'Present', 'is_default': True}, ...], or None if no qualifiers are present.

clinlp.ie.qualifier.qualifier.get_qualifiers(entity: Span) set[Qualifier]

Get the qualifiers for an entity.

Returns:

set[Qualifier] – The qualifiers.

clinlp.ie.qualifier.qualifier.set_qualifiers(entity: Span, qualifiers: set[Qualifier]) None

Set the qualifiers for an entity.

Parameters:
  • entity – The entity to set qualifiers for.

  • qualifiers – The qualifiers to set.

class clinlp.ie.qualifier.qualifier.Qualifier(name: str, value: str, is_default: bool | None = None, priority: int = 0, prob: float | None = None)

Bases: object

A qualifier for an entity.

A qualifier is a piece of information that provides additional context to an entity. For example, a Presence qualifier with a value of Present or Absent. A qualifier has a fixed value.

name: str

The name of the qualifier.

value: str

The value of the qualifier.

is_default: bool | None = None

Whether the value is the default value.

priority: int = 0

The priority of the qualifier.

prob: float | None = None

The probability of the qualifier.

to_dict() dict

Convert the qualifier to a dictionary.

Returns:

dict – The qualifier as a dictionary.

class clinlp.ie.qualifier.qualifier.QualifierClass(name: str, values: list[str], default: str | None = None, priorities: dict | None = None)

Bases: object

A qualifier class.

A qualifier class defines the set of possible values a qualifier can take on. For example: Presence with values Present and Absent. The qualifier class creates qualifiers, although they can also be created directly.

__init__(name: str, values: list[str], default: str | None = None, priorities: dict | None = None) None

Initialize a qualifier class.

Parameters:
  • name – The name of the qualifier.

  • values – The possible values of the qualifier.

  • default – The default value of the qualifier.

  • priorities – The priorities of the values. If not provided, the order of the values is used.

Raises:
  • ValueError – If there are duplicate values.

  • ValueError – If the default value is not in the provided values.

create(value: str | None = None, **kwargs) Qualifier

Create a qualifier in this qualifier class.

Parameters:

value – The value for the qualifier.

Returns:

Qualifier – The created qualifier.

Raises:

ValueError – If the value is not in the possible values.

class clinlp.ie.qualifier.qualifier.QualifierDetector(spans_key: str = 'ents')

Bases: Pipe

Abstract pipeline component for detecting qualifiers in clinical text.

__init__(spans_key: str = 'ents') None

Initialize a qualifier detector.

Parameters:

spans_key – The key for the spans in the Doc object.

abstract property qualifier_classes: dict[str, QualifierClass]

Obtain the qualifier classes that a QualifierDetector initializes.

These are used to initialize the default qualifiers for each entity.

Returns:

dict[str, QualifierClass] – The qualifier classes.

static add_qualifier_to_ent(entity: Span, new_qualifier: Qualifier) None

Add a qualifier to an entity.

Preferably, qualifiers should not be added in another way than through this method, to ensure consistency.

Parameters:
  • entity – The entity to add the qualifier to.

  • new_qualifier – The qualifier to add.

Raises:

RuntimeError – If the entity does not have initialized qualifiers.

__call__(doc: Doc) Doc

Initialize default qualifiers and run detection.

Parameters:

doc – The document to process.

Returns:

Doc – The processed document.

clinlp.ie.qualifier.transformer