Switch to unified view

a b/docs/tutorials/spacy101.md
1
# SpaCy representations
2
3
EDS-NLP uses spaCy to represent documents and their annotations. You will need to familiarise yourself with some key spaCy concepts.
4
5
!!! tip "Skip if you're familiar with spaCy objects"
6
7
    This page is intended as a crash course for the very basic spaCy concepts that are needed to use EDS-NLP. If you've already used spaCy, you should probably skip to the next page.
8
9
## The `Doc` object
10
11
The `doc` object carries the result of the entire processing.
12
It's the most important abstraction in spaCy, hence its use in EDS-NLP, and holds a token-based representation of the text along with the results of every pipeline components. It also keeps track of the input text in a non-destructive manner, meaning that
13
`#!python doc.text == text` is always true.
14
15
To obtain a doc, run the following code:
16
```python
17
import edsnlp  # (1)
18
19
# Initialize a pipeline
20
nlp = edsnlp.blank("eds")  # (2)
21
22
text = "Michel est un penseur latéral."  # (3)
23
24
# Apply the pipeline
25
doc = nlp(text)  # (4)
26
27
doc.text
28
# Out: 'Michel est un penseur latéral.'
29
30
# If you do not want to run the pipeline but only tokenize the text
31
doc = nlp.make_doc(text)
32
33
# Text processing in spaCy is non-destructive
34
doc.text == text
35
36
# You can access a specific token
37
token = doc[2]  # (5)
38
39
# And create a Span using slices
40
span = doc[:3]  # (6)
41
42
# Entities are tracked in the ents attribute
43
doc.ents  # (7)
44
# Out: ()
45
```
46
47
1. Import edsnlp...
48
2. Load a pipeline. The parameter corresponds to the [language](/tokenizers) code and affects the tokenization.
49
3. Define a text you want to process.
50
4. Apply the pipeline and get a spaCy [`Doc`](https://spacy.io/api/doc) object.
51
5. `token` is a [`Token`](https://spacy.io/api/token) object referencing the third token
52
6. `span` is a [`Span`](https://spacy.io/api/span) object referencing the first three tokens.
53
7. We have not declared any entity recognizer in our pipeline, hence this attribute is empty.
54
55
We just created a pipeline and applied it to a sample text. It's that simple.
56
57
## The `Span` objects
58
59
Span of text are represented by the `Span` object and represent slices of the `Doc` object. You can either create a span by slicing a `Doc` object, or by running a pipeline component that creates spans. There are different types of spans:
60
61
- `doc.ents` are non-overlapping spans that represent entities
62
- `doc.sents` are the sentences of the document
63
- `doc.spans` is dict of groups of spans (that can overlap)
64
65
```python
66
import edsnlp, edsnlp.pipes as eds
67
68
nlp = edsnlp.blank("eds")
69
70
nlp.add_pipe(eds.sentences())  # (1)
71
nlp.add_pipe(eds.dates())  # (2)
72
73
text = "Le 5 mai 2005, Jimothé a été invité à une fête organisée par Michel."
74
75
doc = nlp(text)
76
```
77
78
1. Like the name suggests, this pipeline component is declared by EDS-NLP.
79
   `eds.sentences` is a rule-based sentence boundary prediction.
80
   See [its documentation](/pipes/core/sentences) for detail.
81
2. Like the name suggests, this pipeline component is declared by EDS-NLP.
82
   `eds.dates` is a date extraction and normalisation component.
83
   See [its documentation](/pipes/misc/dates) for detail.
84
85
The `doc` object just became more interesting!
86
87
```{ .python .no-check }
88
# ↑ Omitted code above ↑
89
90
# We can split the document into sentences spans
91
list(doc.sents)  # (1)
92
# Out: [Le 5 mai 2005, Jimothé a été invité à une fête organisée par Michel.]
93
94
# And list dates spans
95
doc.spans["dates"]  # (2)
96
# Out: [5 mai 2005]
97
98
span = doc.spans["dates"][0]  # (3)
99
```
100
101
1. In this example, there is only one sentence...
102
2. The `eds.dates` adds a key to the `doc.spans` attribute
103
3. `span` is a spaCy `Span` object.
104
105
## SpaCy extensions
106
107
We can add custom attributes (or "extensions") to spaCy objects via the `_` attribute. For example, the `eds.dates` pipeline adds a `Span._.date` extension to the `Span` object. The attributes can be any Python object.
108
109
```{ .python .no-check }
110
# ↑ Omitted code above ↑
111
112
span._.date.to_datetime()  # (1)
113
# Out: DateTime(2005, 5, 5, 0, 0, 0, tzinfo=Timezone('Europe/Paris'))
114
```
115
116
1. We use the `to_datetime()` method of the extension to get an object that is usable by Python.