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:
EnumDirection 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:
objectA 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 aspaCypattern (list).
- direction: ContextRuleDirection¶
The Context rule direction.
- max_scope: int | None = None¶
The maximum number of tokens a trigger ranges over, or
Nonefor 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/stable/src/clinlp/resources/context_rules.json', **kwargs)¶
Bases:
QualifierDetectorspaCypipeline 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/stable/src/clinlp/resources/context_rules.json', **kwargs) None¶
Initialize the Context Algorithm.
- Parameters:
nlp – The
spaCylanguage 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
Trueto load the default builtin rules. Set this toFalseto useContextAlgorithm.add_rulesto addContextRulesmanually.rules – A dictionary of rules, or a path to a
jsoncontaining the rules. See theclinlp.resourcesdir for an example.
- Raises:
ValueError – If no rules are provided and
load_rulesis set toTrue.
- property qualifier_classes: dict[str, QualifierClass]¶
Obtain the qualifier classes that a
QualifierDetectorinitializes.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
stringor alist.
- 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', ...}, orNoneif 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 indictformat, e.g.[{'Name': 'Presence', 'Value': 'Present', 'is_default': True}, ...], orNoneif 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:
objectA qualifier for an entity.
A qualifier is a piece of information that provides additional context to an entity. For example, a
Presencequalifier with a value ofPresentorAbsent. 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:
objectA qualifier class.
A qualifier class defines the set of possible values a qualifier can take on. For example:
Presencewith valuesPresentandAbsent. 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.
- class clinlp.ie.qualifier.qualifier.QualifierDetector(spans_key: str = 'ents')¶
Bases:
PipeAbstract 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
Docobject.
- abstract property qualifier_classes: dict[str, QualifierClass]¶
Obtain the qualifier classes that a
QualifierDetectorinitializes.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.