Skip to content

Commit 406ff1d

Browse files
committed
status mapping as defaultdict in disorders
1 parent d25dcd3 commit 406ff1d

File tree

9 files changed

+46
-30
lines changed

9 files changed

+46
-30
lines changed

edsnlp/pipes/ner/behaviors/alcohol/alcohol.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""`eds.alcohol` pipeline"""
22

3+
from collections import defaultdict
34
from typing import Any, Dict, List, Optional, Union
45

56
from spacy.tokens import Doc, Span
@@ -99,10 +100,10 @@ def __init__(
99100
nlp=nlp,
100101
name=name,
101102
patterns=patterns,
102-
detailed_status_mapping={
103-
1: None,
104-
2: "ABSTINENCE",
105-
},
103+
detailed_status_mapping=defaultdict(
104+
lambda: None,
105+
{2: "ABSTINENCE"},
106+
),
106107
label=label,
107108
span_setter=span_setter,
108109
include_assigned=True,
@@ -118,8 +119,8 @@ def process(self, doc: Doc) -> List[Span]:
118119
stopped = self.negation.process(span)
119120
if not any(stopped_token.negation for stopped_token in stopped.tokens):
120121
span._.status = 2
121-
122-
if "zero_after" in span._.assigned.keys():
123-
span._.negation = True
122+
else:
123+
if "zero_after" in span._.assigned.keys():
124+
span._.negation = True
124125

125126
yield span

edsnlp/pipes/ner/behaviors/alcohol/patterns.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
regex=[
44
r"\balco[ol]",
55
r"\bethyl",
6-
r"(?<!(25.?)|(sevrage)).?\boh\b",
6+
r"(?<!(25.{0,10}))\boh\b",
77
r"exogenose",
88
r"delirium.tremens",
99
],

edsnlp/pipes/ner/disorders/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
from collections import defaultdict
23
from typing import Any, Dict, List, Union
34

45
from spacy.tokens import Doc, Span
@@ -48,9 +49,9 @@ def __init__(
4849
include_assigned: bool = True,
4950
ignore_excluded: bool = True,
5051
ignore_space_tokens: bool = True,
51-
detailed_status_mapping: Dict[int, Union[str, None]] = {
52-
1: None,
53-
},
52+
detailed_status_mapping: Dict[Union[int, None], Union[str, None]] = defaultdict(
53+
lambda: None
54+
),
5455
alignment_mode: str = "expand",
5556
regex_flags: Union[re.RegexFlag, int] = re.S,
5657
span_setter: SpanSetterArg,

edsnlp/pipes/ner/disorders/diabetes/diabetes.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""`eds.diabetes` pipeline"""
22

3+
from collections import defaultdict
34
from typing import Any, Dict, List, Optional, Union
45

56
from spacy.tokens import Doc, Span
@@ -102,10 +103,10 @@ def __init__(
102103
nlp=nlp,
103104
name=name,
104105
patterns=patterns,
105-
detailed_status_mapping={
106-
1: "WITHOUT_COMPLICATION",
107-
2: "WITH_COMPLICATION",
108-
},
106+
detailed_status_mapping=defaultdict(
107+
lambda: "WITHOUT_COMPLICATION",
108+
{2: "WITH_COMPLICATION"},
109+
),
109110
label=label,
110111
span_setter=span_setter,
111112
)

edsnlp/pipes/ner/disorders/liver_disease/liver_disease.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""`eds.liver_disease` pipeline"""
22

3+
from collections import defaultdict
34
from typing import Any, Dict, List, Optional, Union
45

56
from spacy.tokens import Doc, Span
@@ -94,10 +95,10 @@ def __init__(
9495
nlp=nlp,
9596
name=name,
9697
patterns=patterns,
97-
detailed_status_mapping={
98-
1: "MILD",
99-
2: "MODERATE_TO_SEVERE",
100-
},
98+
detailed_status_mapping=defaultdict(
99+
lambda: "MILD",
100+
{2: "MODERATE_TO_SEVERE"},
101+
),
101102
label=label,
102103
span_setter=span_setter,
103104
)

edsnlp/pipes/ner/disorders/solid_tumor/solid_tumor.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""`eds.solid_tumor` pipeline"""
22

3+
from collections import defaultdict
34
from typing import Any, Dict, List, Optional, Union
45

56
from spacy.tokens import Doc, Span
@@ -108,10 +109,10 @@ def __init__(
108109
nlp=nlp,
109110
name=name,
110111
patterns=patterns,
111-
detailed_status_mapping={
112-
1: "LOCALIZED",
113-
2: "METASTASIS",
114-
},
112+
detailed_status_mapping=defaultdict(
113+
lambda: "LOCALIZED",
114+
{2: "METASTASIS"},
115+
),
115116
label=label,
116117
span_setter=span_setter,
117118
)

tests/pipelines/ner/disorders/alcohol.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
True,
1010
True,
1111
True,
12+
True,
1213
],
1314
detailled_status=[
1415
None,
@@ -20,28 +21,31 @@
2021
None,
2122
"ABSTINENCE",
2223
None,
24+
None,
2325
],
2426
negation=[
2527
None,
2628
None,
2729
None,
2830
None,
2931
None,
30-
None,
32+
False,
3133
True,
3234
None,
3335
True,
36+
False,
3437
],
3538
assign=None,
3639
texts=[
3740
"Patient alcoolique.",
3841
"OH chronique.",
3942
"Prise d'alcool occasionnelle",
4043
"Application d'un pansement alcoolisé",
41-
"Alcoolisme sevré",
42-
"Alcoolisme non sevré",
44+
"Présence d'un alcoolisme sevré",
45+
"Présence d'un alcoolisme non sevré",
4346
"Alcool: 0",
4447
"Le patient est en cours de sevrage éthylotabagique",
4548
"Patient alcoolique: non.",
49+
"On a un alcoolique non sevré depuis 10 ans.",
4650
],
4751
)

tests/pipelines/ner/disorders/test_all.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ def check(self):
9999
for ent in ents:
100100
assert ent.label_ == self.disorder
101101
if negation is not None:
102-
assert ent._.negation == negation
102+
if negation:
103+
assert ent._.negation == negation
104+
else:
105+
assert ent._.negation is None
103106

104107
if not ents:
105108
continue

tests/pipelines/ner/disorders/tobacco.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
True,
1010
True,
1111
True,
12+
True,
1213
],
1314
detailled_status=[
1415
None,
@@ -20,6 +21,7 @@
2021
"ABSTINENCE",
2122
None,
2223
None,
24+
None,
2325
],
2426
negation=[
2527
None,
@@ -31,17 +33,19 @@
3133
None,
3234
True,
3335
True,
36+
False,
3437
],
3538
assign=[{"PA": 15}] + 8 * [None],
3639
texts=[
3740
"Tabagisme évalué à 15 PA",
3841
"Patient tabagique",
3942
"Tabagisme festif",
4043
"On a un tabagisme ancien",
41-
"Tabac: 0",
42-
"Tabagisme passif",
43-
"Tabac: sevré depuis 5 ans",
44+
"Pour le tabac: 0",
45+
"Notion de tabagisme passif",
46+
"Concernant le tabac: sevré depuis 5 ans",
4447
"Le patient ne fume aucun truc.",
4548
"Le patient fume 0 PA.",
49+
"On a un tabagique non sevré depuis 10 ans.",
4650
],
4751
)

0 commit comments

Comments
 (0)