|
a |
|
b/notebooks/dates/user-guide.md |
|
|
1 |
--- |
|
|
2 |
jupyter: |
|
|
3 |
jupytext: |
|
|
4 |
formats: md,ipynb |
|
|
5 |
main_language: python |
|
|
6 |
text_representation: |
|
|
7 |
extension: .md |
|
|
8 |
format_name: markdown |
|
|
9 |
format_version: '1.3' |
|
|
10 |
jupytext_version: 1.13.8 |
|
|
11 |
kernelspec: |
|
|
12 |
display_name: 'Python 3.9.5 64-bit (''.env'': venv)' |
|
|
13 |
name: python3 |
|
|
14 |
--- |
|
|
15 |
|
|
|
16 |
```python |
|
|
17 |
import context |
|
|
18 |
``` |
|
|
19 |
|
|
|
20 |
```python |
|
|
21 |
from edsnlp.pipelines.misc.dates import Dates, terms |
|
|
22 |
``` |
|
|
23 |
|
|
|
24 |
```python |
|
|
25 |
from datetime import datetime |
|
|
26 |
``` |
|
|
27 |
|
|
|
28 |
```python |
|
|
29 |
import spacy |
|
|
30 |
``` |
|
|
31 |
|
|
|
32 |
# Date detection |
|
|
33 |
|
|
|
34 |
```python |
|
|
35 |
text = ( |
|
|
36 |
"Le patient est arrivé le 23 août (23/08/2021). " |
|
|
37 |
"Il dit avoir eu mal au ventre hier. " |
|
|
38 |
"L'année dernière, on lui avait prescrit du doliprane." |
|
|
39 |
) |
|
|
40 |
``` |
|
|
41 |
|
|
|
42 |
```python |
|
|
43 |
nlp = spacy.blank('fr') |
|
|
44 |
``` |
|
|
45 |
|
|
|
46 |
```python |
|
|
47 |
doc = nlp(text) |
|
|
48 |
``` |
|
|
49 |
|
|
|
50 |
```python |
|
|
51 |
dates = Dates( |
|
|
52 |
nlp, |
|
|
53 |
absolute=terms.absolute, |
|
|
54 |
relative=terms.relative, |
|
|
55 |
no_year=terms.no_year, |
|
|
56 |
) |
|
|
57 |
``` |
|
|
58 |
|
|
|
59 |
```python |
|
|
60 |
dates(doc) |
|
|
61 |
``` |
|
|
62 |
|
|
|
63 |
```python |
|
|
64 |
doc.spans |
|
|
65 |
``` |
|
|
66 |
|
|
|
67 |
```python |
|
|
68 |
print(f"{'expression':<20} label") |
|
|
69 |
print(f"{'----------':<20} -----") |
|
|
70 |
|
|
|
71 |
for span in doc.spans['dates']: |
|
|
72 |
print(f"{span.text:<20} {span._.date}") |
|
|
73 |
``` |
|
|
74 |
|
|
|
75 |
Lorsque la date du document n'est pas connue, le label des dates relatives (hier, il y a quinze jours, etc) devient `TD±<nb-de-jours>` |
|
|
76 |
|
|
|
77 |
Si on renseigne l'extension `note_datetime` : |
|
|
78 |
|
|
|
79 |
```python |
|
|
80 |
doc._.note_datetime = datetime(2020, 10, 10) |
|
|
81 |
``` |
|
|
82 |
|
|
|
83 |
```python |
|
|
84 |
dates(doc) |
|
|
85 |
``` |
|
|
86 |
|
|
|
87 |
```python |
|
|
88 |
print(f"{'expression':<20} label") |
|
|
89 |
print(f"{'----------':<20} -----") |
|
|
90 |
|
|
|
91 |
for span in doc.spans['dates']: |
|
|
92 |
print(f"{span.text:<20} {span._.date}") |
|
|
93 |
``` |