Skip to content

Commit

Permalink
test: verify suicide_attempt_modality and fix typos
Browse files Browse the repository at this point in the history
Co-Authored-By: Ariel Cohen <[email protected]>
  • Loading branch information
percevalw and aricohen93 committed Jul 9, 2024
1 parent 53c79f0 commit da4321c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
22 changes: 11 additions & 11 deletions edsnlp/pipes/ner/suicide_attempt/suicide_attempt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ class SuicideAttemptMatcher(BaseNERComponent):
The `eds.suicide_attempt` pipeline component detects mentions of Suicide Attempt.
It can be used with a span qualifier
for contextualisation of the entity (history) and to detect false positives as
negation, hypothesis or family. We recomend to use a Machine Learning qualifier
negation, hypothesis or family. We recommend to use a machine learning qualifier
to disambiguate polysemic words, as proposed in [@bey_natural_2024].
It sets the entity label `suicide_attempt`.
Every matched entity will be labelled `suicide_attempt`.
Extensions
----------
Each entity span will have the suicide attempt modality as an attribute.
The available modalities are:
- suicide_attempt_unspecific
- autolysis
- intentional_drug_overdose
- jumping_from_height
- cuts
- strangling
- self_destructive_behavior
- burn_gas_caustic
- `suicide_attempt_unspecific`
- `autolysis`
- `intentional_drug_overdose`
- `jumping_from_height`
- `cuts`
- `strangling`
- `self_destructive_behavior`
- `burn_gas_caustic`
Examples
--------
Expand All @@ -55,7 +55,7 @@ class SuicideAttemptMatcher(BaseNERComponent):
# Out: 'intentional_drug_overdose'
```
??? info "Patterns used for the NER"
??? info "Patterns used for the named entity recognition"
```{ .python .no-check }
# fmt: off
--8<-- "edsnlp/pipes/ner/suicide_attempt/patterns.py"
Expand Down
6 changes: 5 additions & 1 deletion edsnlp/utils/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Modifier(BaseModel):
key: str
value: Union[int, float, bool, str, Dict[str, Any]]

@validator("value")
@validator("value", allow_reuse=True)
def optional_dict_parsing(cls, v):
try:
return loads(v)
Expand All @@ -30,6 +30,10 @@ class Entity(BaseModel):
end_char: int
modifiers: List[Modifier]

@property
def modifiers_dict(self) -> Dict[str, Any]:
return {m.key: m.value for m in self.modifiers}


entity_pattern = re.compile(r"(<ent[^<>]*>[^<>]+</ent>)", flags=re.DOTALL)
text_pattern = re.compile(r"<ent.*>(.+)</ent>", flags=re.DOTALL)
Expand Down
28 changes: 16 additions & 12 deletions tests/pipelines/ner/test_suicide_attempt.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from edsnlp.utils.examples import parse_example
from edsnlp.utils.examples import Entity, parse_example

examples = [
"J'ai vu le patient à cause d'une <ent>TS</ent> médicamenetuse."
"J'ai vu le patient à cause d'une ts médicamenetuse.",
"J'ai vu le patient à cause d'une <ent>IMV</ent>.",
"surface TS",
"Patiente hospitalisée à cause d'une <ent>Tentative d'autolyse</ent>.",
"Le patient exprime des idées de défenestration",
"vu aux urgences suite à une <ent>défenestration volontaire</ent>",
"amené par les pompiers à cause d'une <ent>phlebotomie</ent>",
"Antécédents :\n- <ent>pendaison</ent> (2010)",
"copain du patient : plusieurs événements d'<ent>autodestruction</ent>",
"suspicion d'<ent>ingestion de caustique</ent> avec des idées suicidaires",
"J'ai vu le patient à cause d'une <ent modality=suicide_attempt_unspecific>TS</ent> médicamenetuse." # noqa: E501
"J'ai vu le patient à cause d'une ts médicamenetuse.", # noqa: E501
"J'ai vu le patient à cause d'une <ent modality=intentional_drug_overdose>IMV</ent>.", # noqa: E501
"surface TS", # noqa: E501
"Patiente hospitalisée à cause d'une <ent modality=autolysis>Tentative d'autolyse</ent>.", # noqa: E501
"Le patient exprime des idées de défenestration", # noqa: E501
"vu aux urgences suite à une <ent modality=jumping_from_height>défenestration volontaire</ent>", # noqa: E501
"amené par les pompiers à cause d'une <ent modality=cuts>phlebotomie</ent>", # noqa: E501
"Antécédents :\n- <ent modality=strangling>pendaison</ent> (2010)", # noqa: E501
"copain du patient : plusieurs événements d'<ent modality=self_destructive_behavior>autodestruction</ent>", # noqa: E501
"suspicion d'<ent modality=burn_gas_caustic>ingestion de caustique</ent> avec des idées suicidaires", # noqa: E501
]


Expand All @@ -24,4 +24,8 @@ def test_suicide_attempt(blank_nlp):
assert len(doc.ents) == len(entities)

for ent, entity in zip(doc.ents, entities):
entity: Entity
assert ent.text == text[entity.start_char : entity.end_char]
assert ent._.suicide_attempt_modality == entity.modifiers_dict.get(
"modality"
)

0 comments on commit da4321c

Please sign in to comment.