|
a |
|
b/notebooks/pipeline.md |
|
|
1 |
--- |
|
|
2 |
jupyter: |
|
|
3 |
jupytext: |
|
|
4 |
formats: ipynb,md |
|
|
5 |
text_representation: |
|
|
6 |
extension: .md |
|
|
7 |
format_name: markdown |
|
|
8 |
format_version: "1.3" |
|
|
9 |
jupytext_version: 1.11.2 |
|
|
10 |
kernelspec: |
|
|
11 |
display_name: "[2.4.3] Py3" |
|
|
12 |
language: python |
|
|
13 |
name: pyspark-2.4.3 |
|
|
14 |
--- |
|
|
15 |
|
|
|
16 |
```python |
|
|
17 |
%reload_ext autoreload |
|
|
18 |
%autoreload 2 |
|
|
19 |
``` |
|
|
20 |
|
|
|
21 |
```python |
|
|
22 |
# Importation du "contexte", ie la bibliothèque sans installation |
|
|
23 |
import context |
|
|
24 |
``` |
|
|
25 |
|
|
|
26 |
```python |
|
|
27 |
import spacy |
|
|
28 |
``` |
|
|
29 |
|
|
|
30 |
```python |
|
|
31 |
# One-shot import of all declared spaCy components |
|
|
32 |
|
|
|
33 |
``` |
|
|
34 |
|
|
|
35 |
```python |
|
|
36 |
a = spacy.registry.get('factories','charlson') |
|
|
37 |
``` |
|
|
38 |
|
|
|
39 |
```python |
|
|
40 |
a() |
|
|
41 |
``` |
|
|
42 |
|
|
|
43 |
# Baselines |
|
|
44 |
|
|
|
45 |
```python |
|
|
46 |
text = ( |
|
|
47 |
"Le patient est admis pour des douleurs dans le bras droit. mais n'a pas de problème de locomotion. \n" |
|
|
48 |
"Historique d'AVC dans la famille mais pas chez les voisins\n" |
|
|
49 |
"mais ne semble pas en être un\n" |
|
|
50 |
"Charlson 7.\n" |
|
|
51 |
"Pourrait être un cas de rhume du fait d'un hiver rigoureux.\n" |
|
|
52 |
"Motif :\n" |
|
|
53 |
"Douleurs dans le bras droit." |
|
|
54 |
) |
|
|
55 |
``` |
|
|
56 |
|
|
|
57 |
```python |
|
|
58 |
nlp = spacy.blank('fr') |
|
|
59 |
nlp.add_pipe('sentencizer') |
|
|
60 |
# nlp.add_pipe('sentences') |
|
|
61 |
nlp.add_pipe('matcher', config=dict(terms=dict(tabac=['Tabac']))) |
|
|
62 |
nlp.add_pipe('normalizer') |
|
|
63 |
nlp.add_pipe('hypothesis') |
|
|
64 |
nlp.add_pipe('family', config=dict(on_ents_only=False)) |
|
|
65 |
#nlp.add_pipe('charlson') |
|
|
66 |
``` |
|
|
67 |
|
|
|
68 |
```python |
|
|
69 |
text = "Tabac:\n" |
|
|
70 |
``` |
|
|
71 |
|
|
|
72 |
```python |
|
|
73 |
doc = nlp(text) |
|
|
74 |
``` |
|
|
75 |
|
|
|
76 |
```python |
|
|
77 |
print([(token.text, token._.hypothesis_) for token in doc if token._.hypothesis==True]) |
|
|
78 |
``` |
|
|
79 |
|
|
|
80 |
```python |
|
|
81 |
doc.ents[0].end |
|
|
82 |
``` |
|
|
83 |
|
|
|
84 |
```python |
|
|
85 |
from edsnlp.utils.inclusion import check_inclusion |
|
|
86 |
ents = [ent for ent in doc.ents if check_inclusion(ent, 0, 2)] |
|
|
87 |
ents |
|
|
88 |
``` |
|
|
89 |
|
|
|
90 |
```python |
|
|
91 |
print([(ent.text, ent.start, ent.end) for ent in doc.ents]) |
|
|
92 |
``` |
|
|
93 |
|
|
|
94 |
```python |
|
|
95 |
import thinc |
|
|
96 |
|
|
|
97 |
registered_func = spacy.registry.get("misc", "score_norm") |
|
|
98 |
``` |
|
|
99 |
|
|
|
100 |
```python |
|
|
101 |
@spacy.registry.misc("score_normalization.charlson") |
|
|
102 |
def score_normalization(extracted_score): |
|
|
103 |
""" |
|
|
104 |
Charlson score normalization. |
|
|
105 |
If available, returns the integer value of the Charlson score. |
|
|
106 |
""" |
|
|
107 |
score_range = list(range(0, 30)) |
|
|
108 |
if (extracted_score is not None) and (int(extracted_score) in score_range): |
|
|
109 |
return int(extracted_score) |
|
|
110 |
|
|
|
111 |
charlson_config = dict( |
|
|
112 |
score_name = 'charlson', |
|
|
113 |
regex = [r'charlson'], |
|
|
114 |
value_extract = r"(\d+)", |
|
|
115 |
score_normalization = "score_normalization.charlson" |
|
|
116 |
) |
|
|
117 |
|
|
|
118 |
nlp = spacy.blank('fr') |
|
|
119 |
nlp.add_pipe('sentences') |
|
|
120 |
nlp.add_pipe('normalizer') |
|
|
121 |
nlp.add_pipe('score', config = charlson_config) |
|
|
122 |
``` |
|
|
123 |
|
|
|
124 |
```python |
|
|
125 |
# nlp.add_pipe('sentencizer') |
|
|
126 |
nlp.add_pipe('sentences') |
|
|
127 |
nlp.add_pipe('normalizer') |
|
|
128 |
nlp.add_pipe('matcher', config=dict(terms=dict(douleurs=['probleme de locomotion', 'douleurs']), attr='NORM')) |
|
|
129 |
nlp.add_pipe('sections') |
|
|
130 |
nlp.add_pipe('pollution') |
|
|
131 |
``` |
|
|
132 |
|
|
|
133 |
```python |
|
|
134 |
text = ( |
|
|
135 |
"Le patient est admis pour des douleurs dans le bras droit, mais n'a pas de problème de locomotion. " |
|
|
136 |
"Historique d'AVC dans la famille. pourrait être un cas de rhume.\n" |
|
|
137 |
"NBNbWbWbNbWbNBNbNbWbWbNBNbWbNbNbWbNBNbWbNbNBWbWbNbNbNBWbNbWbNbWBNbNbWbNbNBNbWbWbNbWBNbNbWbNBNbWbWbNb\n" |
|
|
138 |
"Pourrait être un cas de rhume.\n" |
|
|
139 |
"Motif :\n" |
|
|
140 |
"Douleurs dans le bras droit." |
|
|
141 |
) |
|
|
142 |
``` |
|
|
143 |
|
|
|
144 |
```python |
|
|
145 |
doc = nlp(text) |
|
|
146 |
``` |
|
|
147 |
|
|
|
148 |
```python |
|
|
149 |
doc.ents[0]._.after_snippet |
|
|
150 |
``` |
|
|
151 |
|
|
|
152 |
```python |
|
|
153 |
doc._.sections |
|
|
154 |
``` |
|
|
155 |
|
|
|
156 |
```python |
|
|
157 |
doc._.clean_ |
|
|
158 |
``` |
|
|
159 |
|
|
|
160 |
```python |
|
|
161 |
doc[17]._.ascii_ |
|
|
162 |
``` |
|
|
163 |
|
|
|
164 |
```python |
|
|
165 |
doc._.clean_ |
|
|
166 |
``` |
|
|
167 |
|
|
|
168 |
On peut tester l'extraction d'entité dans le texte nettoyé : |
|
|
169 |
|
|
|
170 |
```python |
|
|
171 |
doc._.clean_[165:181] |
|
|
172 |
``` |
|
|
173 |
|
|
|
174 |
Les deux textes ne sont plus alignés : |
|
|
175 |
|
|
|
176 |
```python |
|
|
177 |
doc.text[165:181] |
|
|
178 |
``` |
|
|
179 |
|
|
|
180 |
Mais la méthode `char_clean_span` permet de réaligner les deux représentations : |
|
|
181 |
|
|
|
182 |
```python |
|
|
183 |
span = doc._.char_clean_span(165, 181) |
|
|
184 |
span |
|
|
185 |
``` |
|
|
186 |
|
|
|
187 |
```python |
|
|
188 |
doc._.sections[0] |
|
|
189 |
``` |
|
|
190 |
|
|
|
191 |
```python |
|
|
192 |
|
|
|
193 |
``` |