chore: import upstream snapshot with attribution
tests / Test (macos-latest, 3.14) (push) Has been cancelled
tests / Test (ubuntu-latest, 3.10) (push) Has been cancelled
tests / Test (ubuntu-latest, 3.11) (push) Has been cancelled
tests / Test (windows-latest, 3.13) (push) Has been cancelled
tests / Test (windows-latest, 3.14) (push) Has been cancelled
tests / Validate (push) Has been cancelled
tests / Test (macos-latest, 3.10) (push) Has been cancelled
tests / Test (macos-latest, 3.11) (push) Has been cancelled
tests / Test (macos-latest, 3.12) (push) Has been cancelled
tests / Test (macos-latest, 3.13) (push) Has been cancelled
tests / Test (ubuntu-latest, 3.12) (push) Has been cancelled
tests / Test (ubuntu-latest, 3.13) (push) Has been cancelled
tests / Test (ubuntu-latest, 3.14) (push) Has been cancelled
tests / Test (windows-latest, 3.10) (push) Has been cancelled
tests / Test (windows-latest, 3.11) (push) Has been cancelled
tests / Test (windows-latest, 3.12) (push) Has been cancelled
universe validation / Validate (push) Has been cancelled
tests / Test (macos-latest, 3.14) (push) Has been cancelled
tests / Test (ubuntu-latest, 3.10) (push) Has been cancelled
tests / Test (ubuntu-latest, 3.11) (push) Has been cancelled
tests / Test (windows-latest, 3.13) (push) Has been cancelled
tests / Test (windows-latest, 3.14) (push) Has been cancelled
tests / Validate (push) Has been cancelled
tests / Test (macos-latest, 3.10) (push) Has been cancelled
tests / Test (macos-latest, 3.11) (push) Has been cancelled
tests / Test (macos-latest, 3.12) (push) Has been cancelled
tests / Test (macos-latest, 3.13) (push) Has been cancelled
tests / Test (ubuntu-latest, 3.12) (push) Has been cancelled
tests / Test (ubuntu-latest, 3.13) (push) Has been cancelled
tests / Test (ubuntu-latest, 3.14) (push) Has been cancelled
tests / Test (windows-latest, 3.10) (push) Has been cancelled
tests / Test (windows-latest, 3.11) (push) Has been cancelled
tests / Test (windows-latest, 3.12) (push) Has been cancelled
universe validation / Validate (push) Has been cancelled
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,267 @@
|
||||
---
|
||||
title: AttributeRuler
|
||||
tag: class
|
||||
source: spacy/pipeline/attributeruler.py
|
||||
version: 3
|
||||
teaser: 'Pipeline component for rule-based token attribute assignment'
|
||||
api_string_name: attribute_ruler
|
||||
api_trainable: false
|
||||
---
|
||||
|
||||
The attribute ruler lets you set token attributes for tokens identified by
|
||||
[`Matcher` patterns](/usage/rule-based-matching#matcher). The attribute ruler is
|
||||
typically used to handle exceptions for token attributes and to map values
|
||||
between attributes such as mapping fine-grained POS tags to coarse-grained POS
|
||||
tags. See the [usage guide](/usage/linguistic-features/#mappings-exceptions) for
|
||||
examples.
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> config = {"validate": True}
|
||||
> nlp.add_pipe("attribute_ruler", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| ---------- | --------------------------------------------------------------------------------------------- |
|
||||
| `validate` | Whether patterns should be validated (passed to the `Matcher`). Defaults to `False`. ~~bool~~ |
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/attributeruler.py
|
||||
```
|
||||
|
||||
## AttributeRuler.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Initialize the attribute ruler.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe
|
||||
> ruler = nlp.add_pipe("attribute_ruler")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `vocab` | The shared vocabulary to pass to the matcher. ~~Vocab~~ |
|
||||
| `name` | Instance name of the current pipeline component. Typically passed in automatically from the factory when the component is added. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `validate` | Whether patterns should be validated (passed to the [`Matcher`](/api/matcher#init)). Defaults to `False`. ~~bool~~ |
|
||||
| `scorer` | The scoring method. Defaults to [`Scorer.score_token_attr`](/api/scorer#score_token_attr) for the attributes `"tag`", `"pos"`, `"morph"` and `"lemma"` and [`Scorer.score_token_attr_per_feat`](/api/scorer#score_token_attr_per_feat) for the attribute `"morph"`. ~~Optional[Callable]~~ |
|
||||
|
||||
## AttributeRuler.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the attribute ruler to a `Doc`, setting token attributes for tokens
|
||||
matched by the provided patterns.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## AttributeRuler.add {id="add",tag="method"}
|
||||
|
||||
Add patterns to the attribute ruler. The patterns are a list of `Matcher`
|
||||
patterns and the attributes are a dict of attributes to set on the matched
|
||||
token. If the pattern matches a span of more than one token, the `index` can be
|
||||
used to set the attributes for the token at that index in the span. The `index`
|
||||
may be negative to index from the end of the span.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("attribute_ruler")
|
||||
> patterns = [[{"TAG": "VB"}]]
|
||||
> attrs = {"POS": "VERB"}
|
||||
> ruler.add(patterns=patterns, attrs=attrs)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | --------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `patterns` | The `Matcher` patterns to add. ~~Iterable[List[Dict[Union[int, str], Any]]]~~ |
|
||||
| `attrs` | The attributes to assign to the target token in the matched span. ~~Dict[str, Any]~~ |
|
||||
| `index` | The index of the token in the matched span to modify. May be negative to index from the end of the span. Defaults to `0`. ~~int~~ |
|
||||
|
||||
## AttributeRuler.add_patterns {id="add_patterns",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("attribute_ruler")
|
||||
> patterns = [
|
||||
> {
|
||||
> "patterns": [[{"TAG": "VB"}]], "attrs": {"POS": "VERB"}
|
||||
> },
|
||||
> {
|
||||
> "patterns": [[{"LOWER": "two"}, {"LOWER": "apples"}]],
|
||||
> "attrs": {"LEMMA": "apple"},
|
||||
> "index": -1
|
||||
> },
|
||||
> ]
|
||||
> ruler.add_patterns(patterns)
|
||||
> ```
|
||||
|
||||
Add patterns from a list of pattern dicts. Each pattern dict can specify the
|
||||
keys `"patterns"`, `"attrs"` and `"index"`, which match the arguments of
|
||||
[`AttributeRuler.add`](/api/attributeruler#add).
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | -------------------------------------------------------------------------- |
|
||||
| `patterns` | The patterns to add. ~~Iterable[Dict[str, Union[List[dict], dict, int]]]~~ |
|
||||
|
||||
## AttributeRuler.patterns {id="patterns",tag="property"}
|
||||
|
||||
Get all patterns that have been added to the attribute ruler in the
|
||||
`patterns_dict` format accepted by
|
||||
[`AttributeRuler.add_patterns`](/api/attributeruler#add_patterns).
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------------- |
|
||||
| **RETURNS** | The patterns added to the attribute ruler. ~~List[Dict[str, Union[List[dict], dict, int]]]~~ |
|
||||
|
||||
## AttributeRuler.initialize {id="initialize",tag="method"}
|
||||
|
||||
Initialize the component with data and used before training to load in rules
|
||||
from a file. This method is typically called by
|
||||
[`Language.initialize`](/api/language#initialize) and lets you customize
|
||||
arguments it receives via the
|
||||
[`[initialize.components]`](/api/data-formats#config-initialize) block in the
|
||||
config.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("attribute_ruler")
|
||||
> ruler.initialize(lambda: [], nlp=nlp, patterns=patterns)
|
||||
> ```
|
||||
>
|
||||
> ```ini
|
||||
> ### config.cfg
|
||||
> [initialize.components.attribute_ruler]
|
||||
>
|
||||
> [initialize.components.attribute_ruler.patterns]
|
||||
> @readers = "srsly.read_json.v1"
|
||||
> path = "corpus/attribute_ruler_patterns.json
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects (the training data). Not used by this component. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
| `patterns` | A list of pattern dicts with the keys as the arguments to [`AttributeRuler.add`](/api/attributeruler#add) (`patterns`/`attrs`/`index`) to add as patterns. Defaults to `None`. ~~Optional[Iterable[Dict[str, Union[List[dict], dict, int]]]]~~ |
|
||||
| `tag_map` | The tag map that maps fine-grained tags to coarse-grained tags and morphological features. Defaults to `None`. ~~Optional[Dict[str, Dict[Union[int, str], Union[int, str]]]]~~ |
|
||||
| `morph_rules` | The morph rules that map token text and fine-grained tags to coarse-grained tags, lemmas and morphological features. Defaults to `None`. ~~Optional[Dict[str, Dict[str, Dict[Union[int, str], Union[int, str]]]]]~~ |
|
||||
|
||||
## AttributeRuler.load_from_tag_map {id="load_from_tag_map",tag="method"}
|
||||
|
||||
Load attribute ruler patterns from a tag map.
|
||||
|
||||
| Name | Description |
|
||||
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `tag_map` | The tag map that maps fine-grained tags to coarse-grained tags and morphological features. ~~Dict[str, Dict[Union[int, str], Union[int, str]]]~~ |
|
||||
|
||||
## AttributeRuler.load_from_morph_rules {id="load_from_morph_rules",tag="method"}
|
||||
|
||||
Load attribute ruler patterns from morph rules.
|
||||
|
||||
| Name | Description |
|
||||
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `morph_rules` | The morph rules that map token text and fine-grained tags to coarse-grained tags, lemmas and morphological features. ~~Dict[str, Dict[str, Dict[Union[int, str], Union[int, str]]]]~~ |
|
||||
|
||||
## AttributeRuler.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("attribute_ruler")
|
||||
> ruler.to_disk("/path/to/attribute_ruler")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## AttributeRuler.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("attribute_ruler")
|
||||
> ruler.from_disk("/path/to/attribute_ruler")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `AttributeRuler` object. ~~AttributeRuler~~ |
|
||||
|
||||
## AttributeRuler.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("attribute_ruler")
|
||||
> ruler = ruler.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `AttributeRuler` object. ~~bytes~~ |
|
||||
|
||||
## AttributeRuler.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler_bytes = ruler.to_bytes()
|
||||
> ruler = nlp.add_pipe("attribute_ruler")
|
||||
> ruler.from_bytes(ruler_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `AttributeRuler` object. ~~AttributeRuler~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = ruler.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | --------------------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `patterns` | The `Matcher` patterns. You usually don't want to exclude this. |
|
||||
| `attrs` | The attributes to set. You usually don't want to exclude this. |
|
||||
| `indices` | The token indices. You usually don't want to exclude this. |
|
||||
@@ -0,0 +1,77 @@
|
||||
---
|
||||
title: Attributes
|
||||
teaser: Token attributes
|
||||
source: spacy/attrs.pyx
|
||||
---
|
||||
|
||||
[Token](/api/token) attributes are specified using internal IDs in many places
|
||||
including:
|
||||
|
||||
- [`Matcher` patterns](/api/matcher#patterns),
|
||||
- [`Doc.to_array`](/api/doc#to_array) and
|
||||
[`Doc.from_array`](/api/doc#from_array)
|
||||
- [`Doc.has_annotation`](/api/doc#has_annotation)
|
||||
- [`MultiHashEmbed`](/api/architectures#MultiHashEmbed) Tok2Vec architecture
|
||||
`attrs`
|
||||
|
||||
> ```python
|
||||
> import spacy
|
||||
> from spacy.attrs import DEP
|
||||
>
|
||||
> nlp = spacy.blank("en")
|
||||
> doc = nlp("There are many attributes.")
|
||||
>
|
||||
> # DEP always has the same internal value
|
||||
> assert DEP == 76
|
||||
>
|
||||
> # "DEP" is automatically converted to DEP
|
||||
> assert DEP == nlp.vocab.strings["DEP"]
|
||||
> assert doc.has_annotation(DEP) == doc.has_annotation("DEP")
|
||||
>
|
||||
> # look up IDs in spacy.attrs.IDS
|
||||
> from spacy.attrs import IDS
|
||||
> assert IDS["DEP"] == DEP
|
||||
> ```
|
||||
|
||||
All methods automatically convert between the string version of an ID (`"DEP"`)
|
||||
and the internal integer symbols (`DEP`). The internal IDs can be imported from
|
||||
`spacy.attrs` or retrieved from the [`StringStore`](/api/stringstore). A map
|
||||
from string attribute names to internal attribute IDs is stored in
|
||||
`spacy.attrs.IDS`.
|
||||
|
||||
The corresponding [`Token` object attributes](/api/token#attributes) can be
|
||||
accessed using the same names in lowercase, e.g. `token.orth` or `token.length`.
|
||||
For attributes that represent string values, the internal integer ID is accessed
|
||||
as `Token.attr`, e.g. `token.dep`, while the string value can be retrieved by
|
||||
appending `_` as in `token.dep_`.
|
||||
|
||||
| Attribute | Description |
|
||||
| ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `DEP` | The token's dependency label. ~~str~~ |
|
||||
| `ENT_ID` | The token's entity ID (`ent_id`). ~~str~~ |
|
||||
| `ENT_IOB` | The IOB part of the token's entity tag. Uses custom integer values rather than the string store: unset is `0`, `I` is `1`, `O` is `2`, and `B` is `3`. ~~str~~ |
|
||||
| `ENT_KB_ID` | The token's entity knowledge base ID. ~~str~~ |
|
||||
| `ENT_TYPE` | The token's entity label. ~~str~~ |
|
||||
| `IS_ALPHA` | Token text consists of alphabetic characters. ~~bool~~ |
|
||||
| `IS_ASCII` | Token text consists of ASCII characters. ~~bool~~ |
|
||||
| `IS_DIGIT` | Token text consists of digits. ~~bool~~ |
|
||||
| `IS_LOWER` | Token text is in lowercase. ~~bool~~ |
|
||||
| `IS_PUNCT` | Token is punctuation. ~~bool~~ |
|
||||
| `IS_SPACE` | Token is whitespace. ~~bool~~ |
|
||||
| `IS_STOP` | Token is a stop word. ~~bool~~ |
|
||||
| `IS_TITLE` | Token text is in titlecase. ~~bool~~ |
|
||||
| `IS_UPPER` | Token text is in uppercase. ~~bool~~ |
|
||||
| `LEMMA` | The token's lemma. ~~str~~ |
|
||||
| `LENGTH` | The length of the token text. ~~int~~ |
|
||||
| `LIKE_EMAIL` | Token text resembles an email address. ~~bool~~ |
|
||||
| `LIKE_NUM` | Token text resembles a number. ~~bool~~ |
|
||||
| `LIKE_URL` | Token text resembles a URL. ~~bool~~ |
|
||||
| `LOWER` | The lowercase form of the token text. ~~str~~ |
|
||||
| `MORPH` | The token's morphological analysis. ~~MorphAnalysis~~ |
|
||||
| `NORM` | The normalized form of the token text. ~~str~~ |
|
||||
| `ORTH` | The exact verbatim text of a token. ~~str~~ |
|
||||
| `POS` | The token's universal part of speech (UPOS). ~~str~~ |
|
||||
| `SENT_START` | Token is start of sentence. ~~bool~~ |
|
||||
| `SHAPE` | The token's shape. ~~str~~ |
|
||||
| `SPACY` | Token has a trailing space. ~~bool~~ |
|
||||
| `TAG` | The token's fine-grained part of speech. ~~str~~ |
|
||||
@@ -0,0 +1,143 @@
|
||||
---
|
||||
title: BaseVectors
|
||||
teaser: Abstract class for word vectors
|
||||
tag: class
|
||||
source: spacy/vectors.pyx
|
||||
version: 3.7
|
||||
---
|
||||
|
||||
`BaseVectors` is an abstract class to support the development of custom vectors
|
||||
implementations.
|
||||
|
||||
For use in training with [`StaticVectors`](/api/architectures#staticvectors),
|
||||
`get_batch` must be implemented. For improved performance, use efficient
|
||||
batching in `get_batch` and implement `to_ops` to copy the vector data to the
|
||||
current device. See an example custom implementation for
|
||||
[BPEmb subword embeddings](/usage/embeddings-transformers#custom-vectors).
|
||||
|
||||
## BaseVectors.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create a new vector store.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | --------------------------------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `strings` | The string store. A new string store is created if one is not provided. Defaults to `None`. ~~Optional[StringStore]~~ |
|
||||
|
||||
## BaseVectors.\_\_getitem\_\_ {id="getitem",tag="method"}
|
||||
|
||||
Get a vector by key. If the key is not found in the table, a `KeyError` should
|
||||
be raised.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------- |
|
||||
| `key` | The key to get the vector for. ~~Union[int, str]~~ |
|
||||
| **RETURNS** | The vector for the key. ~~numpy.ndarray[ndim=1, dtype=float32]~~ |
|
||||
|
||||
## BaseVectors.\_\_len\_\_ {id="len",tag="method"}
|
||||
|
||||
Return the number of vectors in the table.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| **RETURNS** | The number of vectors in the table. ~~int~~ |
|
||||
|
||||
## BaseVectors.\_\_contains\_\_ {id="contains",tag="method"}
|
||||
|
||||
Check whether there is a vector entry for the given key.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------- |
|
||||
| `key` | The key to check. ~~int~~ |
|
||||
| **RETURNS** | Whether the key has a vector entry. ~~bool~~ |
|
||||
|
||||
## BaseVectors.add {id="add",tag="method"}
|
||||
|
||||
Add a key to the table, if possible. If no keys can be added, return `-1`.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------------------------- |
|
||||
| `key` | The key to add. ~~Union[str, int]~~ |
|
||||
| **RETURNS** | The row the vector was added to, or `-1` if the operation is not supported. ~~int~~ |
|
||||
|
||||
## BaseVectors.shape {id="shape",tag="property"}
|
||||
|
||||
Get `(rows, dims)` tuples of number of rows and number of dimensions in the
|
||||
vector table.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------ |
|
||||
| **RETURNS** | A `(rows, dims)` pair. ~~Tuple[int, int]~~ |
|
||||
|
||||
## BaseVectors.size {id="size",tag="property"}
|
||||
|
||||
The vector size, i.e. `rows * dims`.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------ |
|
||||
| **RETURNS** | The vector size. ~~int~~ |
|
||||
|
||||
## BaseVectors.is_full {id="is_full",tag="property"}
|
||||
|
||||
Whether the vectors table is full and no slots are available for new keys.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| **RETURNS** | Whether the vectors table is full. ~~bool~~ |
|
||||
|
||||
## BaseVectors.get_batch {id="get_batch",tag="method",version="3.2"}
|
||||
|
||||
Get the vectors for the provided keys efficiently as a batch. Required to use
|
||||
the vectors with [`StaticVectors`](/api/architectures#StaticVectors) for
|
||||
training.
|
||||
|
||||
| Name | Description |
|
||||
| ------ | --------------------------------------- |
|
||||
| `keys` | The keys. ~~Iterable[Union[int, str]]~~ |
|
||||
|
||||
## BaseVectors.to_ops {id="to_ops",tag="method"}
|
||||
|
||||
Dummy method. Implement this to change the embedding matrix to use different
|
||||
Thinc ops.
|
||||
|
||||
| Name | Description |
|
||||
| ----- | -------------------------------------------------------- |
|
||||
| `ops` | The Thinc ops to switch the embedding matrix to. ~~Ops~~ |
|
||||
|
||||
## BaseVectors.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Dummy method to allow serialization. Implement to save vector data with the
|
||||
pipeline.
|
||||
|
||||
| Name | Description |
|
||||
| ------ | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
|
||||
## BaseVectors.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Dummy method to allow serialization. Implement to load vector data from a saved
|
||||
pipeline.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| **RETURNS** | The modified vectors object. ~~BaseVectors~~ |
|
||||
|
||||
## BaseVectors.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
Dummy method to allow serialization. Implement to serialize vector data to a
|
||||
binary string.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------- |
|
||||
| **RETURNS** | The serialized form of the vectors object. ~~bytes~~ |
|
||||
|
||||
## BaseVectors.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Dummy method to allow serialization. Implement to load vector data from a binary
|
||||
string.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------- |
|
||||
| `data` | The data to load from. ~~bytes~~ |
|
||||
| **RETURNS** | The vectors object. ~~BaseVectors~~ |
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,353 @@
|
||||
---
|
||||
title: CoreferenceResolver
|
||||
tag: class,experimental
|
||||
source: spacy-experimental/coref/coref_component.py
|
||||
teaser: 'Pipeline component for word-level coreference resolution'
|
||||
api_base_class: /api/pipe
|
||||
api_string_name: coref
|
||||
api_trainable: true
|
||||
---
|
||||
|
||||
> #### Installation
|
||||
>
|
||||
> ```bash
|
||||
> $ pip install -U spacy-experimental
|
||||
> ```
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
This component is not yet integrated into spaCy core, and is available via the
|
||||
extension package
|
||||
[`spacy-experimental`](https://github.com/explosion/spacy-experimental) starting
|
||||
in version 0.6.0. It exposes the component via
|
||||
[entry points](/usage/saving-loading/#entry-points), so if you have the package
|
||||
installed, using `factory = "experimental_coref"` in your
|
||||
[training config](/usage/training#config) or
|
||||
`nlp.add_pipe("experimental_coref")` will work out-of-the-box.
|
||||
|
||||
</Infobox>
|
||||
|
||||
A `CoreferenceResolver` component groups tokens into clusters that refer to the
|
||||
same thing. Clusters are represented as SpanGroups that start with a prefix
|
||||
(`coref_clusters` by default).
|
||||
|
||||
A `CoreferenceResolver` component can be paired with a
|
||||
[`SpanResolver`](/api/span-resolver) to expand single tokens to spans.
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
Predictions will be saved to `Doc.spans` as a [`SpanGroup`](/api/spangroup). The
|
||||
span key will be a prefix plus a serial number referring to the coreference
|
||||
cluster, starting from zero.
|
||||
|
||||
The span key prefix defaults to `"coref_clusters"`, but can be passed as a
|
||||
parameter.
|
||||
|
||||
| Location | Value |
|
||||
| ------------------------------------------ | ------------------------------------------------------------------------------------------------------- |
|
||||
| `Doc.spans[prefix + "_" + cluster_number]` | One coreference cluster, represented as single-token spans. Cluster numbers start from 1. ~~SpanGroup~~ |
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config). See the
|
||||
[model architectures](/api/architectures#coref-architectures) documentation for
|
||||
details on the architectures and their arguments and hyperparameters.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy_experimental.coref.coref_component import DEFAULT_COREF_MODEL
|
||||
> from spacy_experimental.coref.coref_util import DEFAULT_CLUSTER_PREFIX
|
||||
> config={
|
||||
> "model": DEFAULT_COREF_MODEL,
|
||||
> "span_cluster_prefix": DEFAULT_CLUSTER_PREFIX,
|
||||
> }
|
||||
> nlp.add_pipe("experimental_coref", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| --------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `model` | The [`Model`](https://thinc.ai/docs/api-model) powering the pipeline component. Defaults to [Coref](/api/architectures#Coref). ~~Model~~ |
|
||||
| `span_cluster_prefix` | The prefix for the keys for clusters saved to `doc.spans`. Defaults to `coref_clusters`. ~~str~~ |
|
||||
|
||||
## CoreferenceResolver.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe with default model
|
||||
> coref = nlp.add_pipe("experimental_coref")
|
||||
>
|
||||
> # Construction via add_pipe with custom model
|
||||
> config = {"model": {"@architectures": "my_coref.v1"}}
|
||||
> coref = nlp.add_pipe("experimental_coref", config=config)
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy_experimental.coref.coref_component import CoreferenceResolver
|
||||
> coref = CoreferenceResolver(nlp.vocab, model)
|
||||
> ```
|
||||
|
||||
Create a new pipeline instance. In your application, you would normally use a
|
||||
shortcut for this and instantiate the component using its string name and
|
||||
[`nlp.add_pipe`](/api/language#add_pipe).
|
||||
|
||||
| Name | Description |
|
||||
| --------------------- | --------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | The [`Model`](https://thinc.ai/docs/api-model) powering the pipeline component. ~~Model~~ |
|
||||
| `name` | String name of the component instance. Used to add entries to the `losses` during training. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `span_cluster_prefix` | The prefix for the key for saving clusters of spans. ~~bool~~ |
|
||||
|
||||
## CoreferenceResolver.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document. The document is modified in place and returned.
|
||||
This usually happens under the hood when the `nlp` object is called on a text
|
||||
and all pipeline components are applied to the `Doc` in order. Both
|
||||
[`__call__`](/api/coref#call) and [`pipe`](/api/coref#pipe) delegate to the
|
||||
[`predict`](/api/coref#predict) and
|
||||
[`set_annotations`](/api/coref#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> coref = nlp.add_pipe("experimental_coref")
|
||||
> # This usually happens under the hood
|
||||
> processed = coref(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## CoreferenceResolver.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order. Both [`__call__`](/api/coref#call) and
|
||||
[`pipe`](/api/coref#pipe) delegate to the [`predict`](/api/coref#predict) and
|
||||
[`set_annotations`](/api/coref#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> coref = nlp.add_pipe("experimental_coref")
|
||||
> for doc in coref.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `stream` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## CoreferenceResolver.initialize {id="initialize",tag="method"}
|
||||
|
||||
Initialize the component for training. `get_examples` should be a function that
|
||||
returns an iterable of [`Example`](/api/example) objects. **At least one example
|
||||
should be supplied.** The data examples are used to **initialize the model** of
|
||||
the component and can either be the full training data or a representative
|
||||
sample. Initialization includes validating the network,
|
||||
[inferring missing shapes](https://thinc.ai/docs/usage-models#validation) and
|
||||
setting up the label scheme based on the data. This method is typically called
|
||||
by [`Language.initialize`](/api/language#initialize).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> coref = nlp.add_pipe("experimental_coref")
|
||||
> coref.initialize(lambda: examples, nlp=nlp)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Must contain at least one `Example`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
|
||||
## CoreferenceResolver.predict {id="predict",tag="method"}
|
||||
|
||||
Apply the component's model to a batch of [`Doc`](/api/doc) objects, without
|
||||
modifying them. Clusters are returned as a list of `MentionClusters`, one for
|
||||
each input `Doc`. A `MentionClusters` instance is just a list of lists of pairs
|
||||
of `int`s, where each item corresponds to a cluster, and the `int`s correspond
|
||||
to token indices.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> coref = nlp.add_pipe("experimental_coref")
|
||||
> clusters = coref.predict([doc1, doc2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------- |
|
||||
| `docs` | The documents to predict. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | The predicted coreference clusters for the `docs`. ~~List[MentionClusters]~~ |
|
||||
|
||||
## CoreferenceResolver.set_annotations {id="set_annotations",tag="method"}
|
||||
|
||||
Modify a batch of documents, saving coreference clusters in `Doc.spans`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> coref = nlp.add_pipe("experimental_coref")
|
||||
> clusters = coref.predict([doc1, doc2])
|
||||
> coref.set_annotations([doc1, doc2], clusters)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ---------------------------------------------------------------------------- |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `clusters` | The predicted coreference clusters for the `docs`. ~~List[MentionClusters]~~ |
|
||||
|
||||
## CoreferenceResolver.update {id="update",tag="method"}
|
||||
|
||||
Learn from a batch of [`Example`](/api/example) objects. Delegates to
|
||||
[`predict`](/api/coref#predict).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> coref = nlp.add_pipe("experimental_coref")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = coref.update(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## CoreferenceResolver.create_optimizer {id="create_optimizer",tag="method"}
|
||||
|
||||
Create an optimizer for the pipeline component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> coref = nlp.add_pipe("experimental_coref")
|
||||
> optimizer = coref.create_optimizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The optimizer. ~~Optimizer~~ |
|
||||
|
||||
## CoreferenceResolver.use_params {id="use_params",tag="method, contextmanager"}
|
||||
|
||||
Modify the pipe's model, to use the given parameter values. At the end of the
|
||||
context, the original parameters are restored.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> coref = nlp.add_pipe("experimental_coref")
|
||||
> with coref.use_params(optimizer.averages):
|
||||
> coref.to_disk("/best_model")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------- |
|
||||
| `params` | The parameter values to use in the model. ~~dict~~ |
|
||||
|
||||
## CoreferenceResolver.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> coref = nlp.add_pipe("experimental_coref")
|
||||
> coref.to_disk("/path/to/coref")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## CoreferenceResolver.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> coref = nlp.add_pipe("experimental_coref")
|
||||
> coref.from_disk("/path/to/coref")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `CoreferenceResolver` object. ~~CoreferenceResolver~~ |
|
||||
|
||||
## CoreferenceResolver.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> coref = nlp.add_pipe("experimental_coref")
|
||||
> coref_bytes = coref.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring, including the `KnowledgeBase`.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `CoreferenceResolver` object. ~~bytes~~ |
|
||||
|
||||
## CoreferenceResolver.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> coref_bytes = coref.to_bytes()
|
||||
> coref = nlp.add_pipe("experimental_coref")
|
||||
> coref.from_bytes(coref_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `CoreferenceResolver` object. ~~CoreferenceResolver~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = coref.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `cfg` | The config file. You usually don't want to exclude this. |
|
||||
| `model` | The binary model data. You usually don't want to exclude this. |
|
||||
@@ -0,0 +1,242 @@
|
||||
---
|
||||
title: Corpus
|
||||
teaser: An annotated corpus
|
||||
tag: class
|
||||
source: spacy/training/corpus.py
|
||||
version: 3
|
||||
---
|
||||
|
||||
This class manages annotated corpora and can be used for training and
|
||||
development datasets in the [`DocBin`](/api/docbin) (`.spacy`) format. To
|
||||
customize the data loading during training, you can register your own
|
||||
[data readers and batchers](/usage/training#custom-code-readers-batchers). Also
|
||||
see the usage guide on [data utilities](/usage/training#data) for more details
|
||||
and examples.
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
`spacy.Corpus.v1` is a registered function that creates a `Corpus` of training
|
||||
or evaluation data. It takes the same arguments as the `Corpus` class and
|
||||
returns a callable that yields [`Example`](/api/example) objects. You can
|
||||
replace it with your own registered function in the
|
||||
[`@readers` registry](/api/top-level#registry) to customize the data loading and
|
||||
streaming.
|
||||
|
||||
> #### Example config
|
||||
>
|
||||
> ```ini
|
||||
> [paths]
|
||||
> train = "corpus/train.spacy"
|
||||
>
|
||||
> [corpora.train]
|
||||
> @readers = "spacy.Corpus.v1"
|
||||
> path = ${paths.train}
|
||||
> gold_preproc = false
|
||||
> max_length = 0
|
||||
> limit = 0
|
||||
> augmenter = null
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `path` | The directory or filename to read from. Expects data in spaCy's binary [`.spacy` format](/api/data-formats#binary-training). ~~Path~~ |
|
||||
| `gold_preproc` | Whether to set up the Example object with gold-standard sentences and tokens for the predictions. See [`Corpus`](/api/corpus#init) for details. ~~bool~~ |
|
||||
| `max_length` | Maximum document length. Longer documents will be split into sentences, if sentence boundaries are available. Defaults to `0` for no limit. ~~int~~ |
|
||||
| `limit` | Limit corpus to a subset of examples, e.g. for debugging. Defaults to `0` for no limit. ~~int~~ |
|
||||
| `augmenter` | Apply some simply data augmentation, where we replace tokens with variations. This is especially useful for punctuation and case replacement, to help generalize beyond corpora that don't have smart-quotes, or only have smart quotes, etc. Defaults to `None`. ~~Optional[Callable]~~ |
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/training/corpus.py
|
||||
```
|
||||
|
||||
## Corpus.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create a `Corpus` for iterating [Example](/api/example) objects from a file or
|
||||
directory of [`.spacy` data files](/api/data-formats#binary-training). The
|
||||
`gold_preproc` setting lets you specify whether to set up the `Example` object
|
||||
with gold-standard sentences and tokens for the predictions. Gold preprocessing
|
||||
helps the annotations align to the tokenization, and may result in sequences of
|
||||
more consistent length. However, it may reduce runtime accuracy due to
|
||||
train/test skew.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.training import Corpus
|
||||
>
|
||||
> # With a single file
|
||||
> corpus = Corpus("./data/train.spacy")
|
||||
>
|
||||
> # With a directory
|
||||
> corpus = Corpus("./data", limit=10)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `path` | The directory or filename to read from. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `gold_preproc` | Whether to set up the Example object with gold-standard sentences and tokens for the predictions. Defaults to `False`. ~~bool~~ |
|
||||
| `max_length` | Maximum document length. Longer documents will be split into sentences, if sentence boundaries are available. Defaults to `0` for no limit. ~~int~~ |
|
||||
| `limit` | Limit corpus to a subset of examples, e.g. for debugging. Defaults to `0` for no limit. ~~int~~ |
|
||||
| `augmenter` | Optional data augmentation callback. ~~Callable[[Language, Example], Iterable[Example]]~~ |
|
||||
| `shuffle` | Whether to shuffle the examples. Defaults to `False`. ~~bool~~ |
|
||||
|
||||
## Corpus.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Yield examples from the data.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.training import Corpus
|
||||
> import spacy
|
||||
>
|
||||
> corpus = Corpus("./train.spacy")
|
||||
> nlp = spacy.blank("en")
|
||||
> train_data = corpus(nlp)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | -------------------------------------- |
|
||||
| `nlp` | The current `nlp` object. ~~Language~~ |
|
||||
| **YIELDS** | The examples. ~~Example~~ |
|
||||
|
||||
## JsonlCorpus {id="jsonlcorpus",tag="class"}
|
||||
|
||||
Iterate Doc objects from a file or directory of JSONL (newline-delimited JSON)
|
||||
formatted raw text files. Can be used to read the raw text corpus for language
|
||||
model [pretraining](/usage/embeddings-transformers#pretraining) from a JSONL
|
||||
file.
|
||||
|
||||
> #### Tip: Writing JSONL
|
||||
>
|
||||
> Our utility library [`srsly`](https://github.com/explosion/srsly) provides a
|
||||
> handy `write_jsonl` helper that takes a file path and list of dictionaries and
|
||||
> writes out JSONL-formatted data.
|
||||
>
|
||||
> ```python
|
||||
> import srsly
|
||||
> data = [{"text": "Some text"}, {"text": "More..."}]
|
||||
> srsly.write_jsonl("/path/to/text.jsonl", data)
|
||||
> ```
|
||||
|
||||
```json {title="Example"}
|
||||
{"text": "Can I ask where you work now and what you do, and if you enjoy it?"}
|
||||
{"text": "They may just pull out of the Seattle market completely, at least until they have autonomous vehicles."}
|
||||
{"text": "My cynical view on this is that it will never be free to the public. Reason: what would be the draw of joining the military? Right now their selling point is free Healthcare and Education. Ironically both are run horribly and most, that I've talked to, come out wishing they never went in."}
|
||||
```
|
||||
|
||||
### JsonlCorpus.\_\_init\_\_ {id="jsonlcorpus",tag="method"}
|
||||
|
||||
Initialize the reader.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.training import JsonlCorpus
|
||||
>
|
||||
> corpus = JsonlCorpus("./data/texts.jsonl")
|
||||
> ```
|
||||
>
|
||||
> ```ini
|
||||
> ### Example config
|
||||
> [corpora.pretrain]
|
||||
> @readers = "spacy.JsonlCorpus.v1"
|
||||
> path = "corpus/raw_text.jsonl"
|
||||
> min_length = 0
|
||||
> max_length = 0
|
||||
> limit = 0
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `path` | The directory or filename to read from. Expects newline-delimited JSON with a key `"text"` for each record. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `min_length` | Minimum document length (in tokens). Shorter documents will be skipped. Defaults to `0`, which indicates no limit. ~~int~~ |
|
||||
| `max_length` | Maximum document length (in tokens). Longer documents will be skipped. Defaults to `0`, which indicates no limit. ~~int~~ |
|
||||
| `limit` | Limit corpus to a subset of examples, e.g. for debugging. Defaults to `0` for no limit. ~~int~~ |
|
||||
|
||||
### JsonlCorpus.\_\_call\_\_ {id="jsonlcorpus-call",tag="method"}
|
||||
|
||||
Yield examples from the data.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.training import JsonlCorpus
|
||||
> import spacy
|
||||
>
|
||||
> corpus = JsonlCorpus("./texts.jsonl")
|
||||
> nlp = spacy.blank("en")
|
||||
> data = corpus(nlp)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | -------------------------------------- |
|
||||
| `nlp` | The current `nlp` object. ~~Language~~ |
|
||||
| **YIELDS** | The examples. ~~Example~~ |
|
||||
|
||||
## PlainTextCorpus {id="plaintextcorpus",tag="class",version="3.5.1"}
|
||||
|
||||
Iterate over documents from a plain text file. Can be used to read the raw text
|
||||
corpus for language model
|
||||
[pretraining](/usage/embeddings-transformers#pretraining). The expected file
|
||||
format is:
|
||||
|
||||
- UTF-8 encoding
|
||||
- One document per line
|
||||
- Blank lines are ignored.
|
||||
|
||||
```text {title="Example"}
|
||||
Can I ask where you work now and what you do, and if you enjoy it?
|
||||
They may just pull out of the Seattle market completely, at least until they have autonomous vehicles.
|
||||
My cynical view on this is that it will never be free to the public. Reason: what would be the draw of joining the military? Right now their selling point is free Healthcare and Education. Ironically both are run horribly and most, that I've talked to, come out wishing they never went in.
|
||||
```
|
||||
|
||||
### PlainTextCorpus.\_\_init\_\_ {id="plaintextcorpus-init",tag="method"}
|
||||
|
||||
Initialize the reader.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.training import PlainTextCorpus
|
||||
>
|
||||
> corpus = PlainTextCorpus("./data/docs.txt")
|
||||
> ```
|
||||
>
|
||||
> ```ini
|
||||
> ### Example config
|
||||
> [corpora.pretrain]
|
||||
> @readers = "spacy.PlainTextCorpus.v1"
|
||||
> path = "corpus/raw_text.txt"
|
||||
> min_length = 0
|
||||
> max_length = 0
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `path` | The directory or filename to read from. Expects newline-delimited documents in UTF8 format. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `min_length` | Minimum document length (in tokens). Shorter documents will be skipped. Defaults to `0`, which indicates no limit. ~~int~~ |
|
||||
| `max_length` | Maximum document length (in tokens). Longer documents will be skipped. Defaults to `0`, which indicates no limit. ~~int~~ |
|
||||
|
||||
### PlainTextCorpus.\_\_call\_\_ {id="plaintextcorpus-call",tag="method"}
|
||||
|
||||
Yield examples from the data.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.training import PlainTextCorpus
|
||||
> import spacy
|
||||
>
|
||||
> corpus = PlainTextCorpus("./docs.txt")
|
||||
> nlp = spacy.blank("en")
|
||||
> data = corpus(nlp)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | -------------------------------------- |
|
||||
| `nlp` | The current `nlp` object. ~~Language~~ |
|
||||
| **YIELDS** | The examples. ~~Example~~ |
|
||||
@@ -0,0 +1,580 @@
|
||||
---
|
||||
title: CuratedTransformer
|
||||
teaser:
|
||||
Pipeline component for multi-task learning with Curated Transformer models
|
||||
tag: class
|
||||
source: github.com/explosion/spacy-curated-transformers/blob/main/spacy_curated_transformers/pipeline/transformer.py
|
||||
version: 3.7
|
||||
api_base_class: /api/pipe
|
||||
api_string_name: curated_transformer
|
||||
---
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
This component is available via the extension package
|
||||
[`spacy-curated-transformers`](https://github.com/explosion/spacy-curated-transformers).
|
||||
It exposes the component via entry points, so if you have the package installed,
|
||||
using `factory = "curated_transformer"` in your
|
||||
[training config](/usage/training#config) will work out-of-the-box.
|
||||
|
||||
</Infobox>
|
||||
|
||||
This pipeline component lets you use a curated set of transformer models in your
|
||||
pipeline. spaCy Curated Transformers currently supports the following model
|
||||
types:
|
||||
|
||||
- ALBERT
|
||||
- BERT
|
||||
- CamemBERT
|
||||
- RoBERTa
|
||||
- XLM-RoBERT
|
||||
|
||||
If you want to use another type of model, use
|
||||
[spacy-transformers](/api/spacy-transformers), which allows you to use all
|
||||
Hugging Face transformer models with spaCy.
|
||||
|
||||
You will usually connect downstream components to a shared Curated Transformer
|
||||
pipe using one of the Curated Transformer listener layers. This works similarly
|
||||
to spaCy's [Tok2Vec](/api/tok2vec), and the
|
||||
[Tok2VecListener](/api/architectures/#Tok2VecListener) sublayer. The component
|
||||
assigns the output of the transformer to the `Doc`'s extension attributes. To
|
||||
access the values, you can use the custom
|
||||
[`Doc._.trf_data`](#assigned-attributes) attribute.
|
||||
|
||||
For more details, see the [usage documentation](/usage/embeddings-transformers).
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
The component sets the following
|
||||
[custom extension attribute](/usage/processing-pipeline#custom-components-attributes):
|
||||
|
||||
| Location | Value |
|
||||
| ---------------- | -------------------------------------------------------------------------- |
|
||||
| `Doc._.trf_data` | Curated Transformer outputs for the `Doc` object. ~~DocTransformerOutput~~ |
|
||||
|
||||
## Config and Implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config). See the
|
||||
[model architectures](/api/architectures#curated-trf) documentation for details
|
||||
on the curated transformer architectures and their arguments and
|
||||
hyperparameters.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy_curated_transformers.pipeline.transformer import DEFAULT_CONFIG
|
||||
>
|
||||
> nlp.add_pipe("curated_transformer", config=DEFAULT_CONFIG)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `model` | The Thinc [`Model`](https://thinc.ai/docs/api-model) wrapping the transformer. Defaults to [`XlmrTransformer`](/api/architectures#curated-trf). ~~Model~~ |
|
||||
| `frozen` | If `True`, the model's weights are frozen and no backpropagation is performed. ~~bool~~ |
|
||||
| `all_layer_outputs` | If `True`, the model returns the outputs of all the layers. Otherwise, only the output of the last layer is returned. This must be set to `True` if any of the pipe's downstream listeners require the outputs of all transformer layers. ~~bool~~ |
|
||||
|
||||
```python
|
||||
https://github.com/explosion/spacy-curated-transformers/blob/main/spacy_curated_transformers/pipeline/transformer.py
|
||||
```
|
||||
|
||||
## CuratedTransformer.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe with default model
|
||||
> trf = nlp.add_pipe("curated_transformer")
|
||||
>
|
||||
> # Construction via add_pipe with custom config
|
||||
> config = {
|
||||
> "model": {
|
||||
> "@architectures": "spacy-curated-transformers.XlmrTransformer.v1",
|
||||
> "vocab_size": 250002,
|
||||
> "num_hidden_layers": 12,
|
||||
> "hidden_width": 768,
|
||||
> "piece_encoder": {
|
||||
> "@architectures": "spacy-curated-transformers.XlmrSentencepieceEncoder.v1"
|
||||
> }
|
||||
> }
|
||||
> }
|
||||
> trf = nlp.add_pipe("curated_transformer", config=config)
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy_curated_transformers import CuratedTransformer
|
||||
> trf = CuratedTransformer(nlp.vocab, model)
|
||||
> ```
|
||||
|
||||
Construct a `CuratedTransformer` component. One or more subsequent spaCy
|
||||
components can use the transformer outputs as features in its model, with
|
||||
gradients backpropagated to the single shared weights. The activations from the
|
||||
transformer are saved in the [`Doc._.trf_data`](#assigned-attributes) extension
|
||||
attribute. You can also provide a callback to set additional annotations. In
|
||||
your application, you would normally use a shortcut for this and instantiate the
|
||||
component using its string name and [`nlp.add_pipe`](/api/language#create_pipe).
|
||||
|
||||
| Name | Description |
|
||||
| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | One of the supported pre-trained transformer models. ~~Model~~ |
|
||||
| _keyword-only_ | |
|
||||
| `name` | The component instance name. ~~str~~ |
|
||||
| `frozen` | If `True`, the model's weights are frozen and no backpropagation is performed. ~~bool~~ |
|
||||
| `all_layer_outputs` | If `True`, the model returns the outputs of all the layers. Otherwise, only the output of the last layer is returned. This must be set to `True` if any of the pipe's downstream listeners require the outputs of all transformer layers. ~~bool~~ |
|
||||
|
||||
## CuratedTransformer.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document. The document is modified in place, and returned.
|
||||
This usually happens under the hood when the `nlp` object is called on a text
|
||||
and all pipeline components are applied to the `Doc` in order. Both
|
||||
[`__call__`](/api/curatedtransformer#call) and
|
||||
[`pipe`](/api/curatedtransformer#pipe) delegate to the
|
||||
[`predict`](/api/curatedtransformer#predict) and
|
||||
[`set_annotations`](/api/curatedtransformer#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> trf = nlp.add_pipe("curated_transformer")
|
||||
> # This usually happens under the hood
|
||||
> processed = trf(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## CuratedTransformer.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order. Both [`__call__`](/api/curatedtransformer#call)
|
||||
and [`pipe`](/api/curatedtransformer#pipe) delegate to the
|
||||
[`predict`](/api/curatedtransformer#predict) and
|
||||
[`set_annotations`](/api/curatedtransformer#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("curated_transformer")
|
||||
> for doc in trf.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `stream` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## CuratedTransformer.initialize {id="initialize",tag="method"}
|
||||
|
||||
Initialize the component for training and return an
|
||||
[`Optimizer`](https://thinc.ai/docs/api-optimizers). `get_examples` should be a
|
||||
function that returns an iterable of [`Example`](/api/example) objects. **At
|
||||
least one example should be supplied.** The data examples are used to
|
||||
**initialize the model** of the component and can either be the full training
|
||||
data or a representative sample. Initialization includes validating the network,
|
||||
[inferring missing shapes](https://thinc.ai/docs/usage-models#validation) and
|
||||
setting up the label scheme based on the data. This method is typically called
|
||||
by [`Language.initialize`](/api/language#initialize).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("curated_transformer")
|
||||
> trf.initialize(lambda: examples, nlp=nlp)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Must contain at least one `Example`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
| `encoder_loader` | Initialization callback for the transformer model. ~~Optional[Callable]~~ |
|
||||
| `piece_loader` | Initialization callback for the input piece encoder. ~~Optional[Callable]~~ |
|
||||
|
||||
## CuratedTransformer.predict {id="predict",tag="method"}
|
||||
|
||||
Apply the component's model to a batch of [`Doc`](/api/doc) objects without
|
||||
modifying them.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("curated_transformer")
|
||||
> scores = trf.predict([doc1, doc2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| `docs` | The documents to predict. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | The model's prediction for each document. |
|
||||
|
||||
## CuratedTransformer.set_annotations {id="set_annotations",tag="method"}
|
||||
|
||||
Assign the extracted features to the `Doc` objects. By default, the
|
||||
[`DocTransformerOutput`](/api/curatedtransformer#doctransformeroutput) object is
|
||||
written to the [`Doc._.trf_data`](#assigned-attributes) attribute. Your
|
||||
`set_extra_annotations` callback is then called, if provided.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("curated_transformer")
|
||||
> scores = trf.predict(docs)
|
||||
> trf.set_annotations(docs, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | ------------------------------------------------------------ |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `scores` | The scores to set, produced by `CuratedTransformer.predict`. |
|
||||
|
||||
## CuratedTransformer.update {id="update",tag="method"}
|
||||
|
||||
Prepare for an update to the transformer.
|
||||
|
||||
Like the [`Tok2Vec`](api/tok2vec) component, the `CuratedTransformer` component
|
||||
is unusual in that it does not receive "gold standard" annotations to calculate
|
||||
a weight update. The optimal output of the transformer data is unknown; it's a
|
||||
hidden layer inside the network that is updated by backpropagating from output
|
||||
layers.
|
||||
|
||||
The `CuratedTransformer` component therefore does not perform a weight update
|
||||
during its own `update` method. Instead, it runs its transformer model and
|
||||
communicates the output and the backpropagation callback to any downstream
|
||||
components that have been connected to it via the transformer listener sublayer.
|
||||
If there are multiple listeners, the last layer will actually backprop to the
|
||||
transformer and call the optimizer, while the others simply increment the
|
||||
gradients.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("curated_transformer")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = trf.update(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects. Only the [`Example.predicted`](/api/example#predicted) `Doc` object is used, the reference `Doc` is ignored. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## CuratedTransformer.create_optimizer {id="create_optimizer",tag="method"}
|
||||
|
||||
Create an optimizer for the pipeline component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("curated_transformer")
|
||||
> optimizer = trf.create_optimizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The optimizer. ~~Optimizer~~ |
|
||||
|
||||
## CuratedTransformer.use_params {id="use_params",tag="method, contextmanager"}
|
||||
|
||||
Modify the pipe's model to use the given parameter values. At the end of the
|
||||
context, the original parameters are restored.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("curated_transformer")
|
||||
> with trf.use_params(optimizer.averages):
|
||||
> trf.to_disk("/best_model")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------- |
|
||||
| `params` | The parameter values to use in the model. ~~dict~~ |
|
||||
|
||||
## CuratedTransformer.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("curated_transformer")
|
||||
> trf.to_disk("/path/to/transformer")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## CuratedTransformer.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("curated_transformer")
|
||||
> trf.from_disk("/path/to/transformer")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `CuratedTransformer` object. ~~CuratedTransformer~~ |
|
||||
|
||||
## CuratedTransformer.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("curated_transformer")
|
||||
> trf_bytes = trf.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `CuratedTransformer` object. ~~bytes~~ |
|
||||
|
||||
## CuratedTransformer.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf_bytes = trf.to_bytes()
|
||||
> trf = nlp.add_pipe("curated_transformer")
|
||||
> trf.from_bytes(trf_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `CuratedTransformer` object. ~~CuratedTransformer~~ |
|
||||
|
||||
## Serialization Fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = trf.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `cfg` | The config file. You usually don't want to exclude this. |
|
||||
| `model` | The binary model data. You usually don't want to exclude this. |
|
||||
|
||||
## DocTransformerOutput {id="doctransformeroutput",tag="dataclass"}
|
||||
|
||||
Curated Transformer outputs for one `Doc` object. Stores the dense
|
||||
representations generated by the transformer for each piece identifier. Piece
|
||||
identifiers are grouped by token. Instances of this class are typically assigned
|
||||
to the [`Doc._.trf_data`](/api/curatedtransformer#assigned-attributes) extension
|
||||
attribute.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Get the last hidden layer output for "is" (token index 1)
|
||||
> doc = nlp("This is a text.")
|
||||
> tensors = doc._.trf_data.last_hidden_layer_state[1]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `all_outputs` | List of `Ragged` tensors that correspends to outputs of the different transformer layers. Each tensor element corresponds to a piece identifier's representation. ~~List[Ragged]~~ |
|
||||
| `last_layer_only` | If only the last transformer layer's outputs are preserved. ~~bool~~ |
|
||||
|
||||
### DocTransformerOutput.embedding_layer {id="doctransformeroutput-embeddinglayer",tag="property"}
|
||||
|
||||
Return the output of the transformer's embedding layer or `None` if
|
||||
`last_layer_only` is `True`.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------- |
|
||||
| **RETURNS** | Embedding layer output. ~~Optional[Ragged]~~ |
|
||||
|
||||
### DocTransformerOutput.last_hidden_layer_state {id="doctransformeroutput-lasthiddenlayerstate",tag="property"}
|
||||
|
||||
Return the output of the transformer's last hidden layer.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------ |
|
||||
| **RETURNS** | Last hidden layer output. ~~Ragged~~ |
|
||||
|
||||
### DocTransformerOutput.all_hidden_layer_states {id="doctransformeroutput-allhiddenlayerstates",tag="property"}
|
||||
|
||||
Return the outputs of all transformer layers (excluding the embedding layer).
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------- |
|
||||
| **RETURNS** | Hidden layer outputs. ~~List[Ragged]~~ |
|
||||
|
||||
### DocTransformerOutput.num_outputs {id="doctransformeroutput-numoutputs",tag="property"}
|
||||
|
||||
Return the number of layer outputs stored in the `DocTransformerOutput` instance
|
||||
(including the embedding layer).
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------- |
|
||||
| **RETURNS** | Numbef of outputs. ~~int~~ |
|
||||
|
||||
## Span Getters {id="span_getters",source="github.com/explosion/spacy-transformers/blob/master/spacy_curated_transformers/span_getters.py"}
|
||||
|
||||
Span getters are functions that take a batch of [`Doc`](/api/doc) objects and
|
||||
return a lists of [`Span`](/api/span) objects for each doc to be processed by
|
||||
the transformer. This is used to manage long documents by cutting them into
|
||||
smaller sequences before running the transformer. The spans are allowed to
|
||||
overlap, and you can also omit sections of the `Doc` if they are not relevant.
|
||||
Span getters can be referenced in the
|
||||
`[components.transformer.model.with_spans]` block of the config to customize the
|
||||
sequences processed by the transformer.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------- |
|
||||
| `docs` | A batch of `Doc` objects. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | The spans to process by the transformer. ~~List[List[Span]]~~ |
|
||||
|
||||
### WithStridedSpans.v1 {id="strided_spans",tag="registered function"}
|
||||
|
||||
> #### Example config
|
||||
>
|
||||
> ```ini
|
||||
> [transformer.model.with_spans]
|
||||
> @architectures = "spacy-curated-transformers.WithStridedSpans.v1"
|
||||
> stride = 96
|
||||
> window = 128
|
||||
> ```
|
||||
|
||||
Create a span getter for strided spans. If you set the `window` and `stride` to
|
||||
the same value, the spans will cover each token once. Setting `stride` lower
|
||||
than `window` will allow for an overlap, so that some tokens are counted twice.
|
||||
This can be desirable, because it allows all tokens to have both a left and
|
||||
right context.
|
||||
|
||||
| Name | Description |
|
||||
| -------- | ------------------------ |
|
||||
| `window` | The window size. ~~int~~ |
|
||||
| `stride` | The stride size. ~~int~~ |
|
||||
|
||||
## Model Loaders
|
||||
|
||||
[Curated Transformer models](/api/architectures#curated-trf) are constructed
|
||||
with default hyperparameters and randomized weights when the pipeline is
|
||||
created. To load the weights of an existing pre-trained model into the pipeline,
|
||||
one of the following loader callbacks can be used. The pre-trained model must
|
||||
have the same hyperparameters as the model used by the pipeline.
|
||||
|
||||
### HFTransformerEncoderLoader.v1 {id="hf_trfencoder_loader",tag="registered_function"}
|
||||
|
||||
Construct a callback that initializes a supported transformer model with weights
|
||||
from a corresponding HuggingFace model.
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ------------------------------------------ |
|
||||
| `name` | Name of the HuggingFace model. ~~str~~ |
|
||||
| `revision` | Name of the model revision/branch. ~~str~~ |
|
||||
|
||||
### PyTorchCheckpointLoader.v1 {id="pytorch_checkpoint_loader",tag="registered_function"}
|
||||
|
||||
Construct a callback that initializes a supported transformer model with weights
|
||||
from a PyTorch checkpoint.
|
||||
|
||||
| Name | Description |
|
||||
| ------ | ---------------------------------------- |
|
||||
| `path` | Path to the PyTorch checkpoint. ~~Path~~ |
|
||||
|
||||
## Tokenizer Loaders
|
||||
|
||||
[Curated Transformer models](/api/architectures#curated-trf) must be paired with
|
||||
a matching tokenizer (piece encoder) model in a spaCy pipeline. As with the
|
||||
transformer models, tokenizers are constructed with an empty vocabulary during
|
||||
pipeline creation - They need to be initialized with an appropriate loader
|
||||
before use in training/inference.
|
||||
|
||||
### ByteBPELoader.v1 {id="bytebpe_loader",tag="registered_function"}
|
||||
|
||||
Construct a callback that initializes a Byte-BPE piece encoder model.
|
||||
|
||||
| Name | Description |
|
||||
| ------------- | ------------------------------------- |
|
||||
| `vocab_path` | Path to the vocabulary file. ~~Path~~ |
|
||||
| `merges_path` | Path to the merges file. ~~Path~~ |
|
||||
|
||||
### CharEncoderLoader.v1 {id="charencoder_loader",tag="registered_function"}
|
||||
|
||||
Construct a callback that initializes a character piece encoder model.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------------------------- |
|
||||
| `path` | Path to the serialized character model. ~~Path~~ |
|
||||
| `bos_piece` | Piece used as a beginning-of-sentence token. Defaults to `"[BOS]"`. ~~str~~ |
|
||||
| `eos_piece` | Piece used as a end-of-sentence token. Defaults to `"[EOS]"`. ~~str~~ |
|
||||
| `unk_piece` | Piece used as a stand-in for unknown tokens. Defaults to `"[UNK]"`. ~~str~~ |
|
||||
| `normalize` | Unicode normalization form to use. Defaults to `"NFKC"`. ~~str~~ |
|
||||
|
||||
### HFPieceEncoderLoader.v1 {id="hf_pieceencoder_loader",tag="registered_function"}
|
||||
|
||||
Construct a callback that initializes a HuggingFace piece encoder model. Used in
|
||||
conjunction with the HuggingFace model loader.
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ------------------------------------------ |
|
||||
| `name` | Name of the HuggingFace model. ~~str~~ |
|
||||
| `revision` | Name of the model revision/branch. ~~str~~ |
|
||||
|
||||
### SentencepieceLoader.v1 {id="sentencepiece_loader",tag="registered_function"}
|
||||
|
||||
Construct a callback that initializes a SentencePiece piece encoder model.
|
||||
|
||||
| Name | Description |
|
||||
| ------ | ---------------------------------------------------- |
|
||||
| `path` | Path to the serialized SentencePiece model. ~~Path~~ |
|
||||
|
||||
### WordpieceLoader.v1 {id="wordpiece_loader",tag="registered_function"}
|
||||
|
||||
Construct a callback that initializes a WordPiece piece encoder model.
|
||||
|
||||
| Name | Description |
|
||||
| ------ | ------------------------------------------------ |
|
||||
| `path` | Path to the serialized WordPiece model. ~~Path~~ |
|
||||
|
||||
## Callbacks
|
||||
|
||||
### gradual_transformer_unfreezing.v1 {id="gradual_transformer_unfreezing",tag="registered_function"}
|
||||
|
||||
Construct a callback that can be used to gradually unfreeze the weights of one
|
||||
or more Transformer components during training. This can be used to prevent
|
||||
catastrophic forgetting during fine-tuning.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `target_pipes` | A dictionary whose keys and values correspond to the names of Transformer components and the training step at which they should be unfrozen respectively. ~~Dict[str, int]~~ |
|
||||
@@ -0,0 +1,209 @@
|
||||
---
|
||||
title: Cython Classes
|
||||
menu:
|
||||
- ['Doc', 'doc']
|
||||
- ['Token', 'token']
|
||||
- ['Span', 'span']
|
||||
- ['Lexeme', 'lexeme']
|
||||
- ['Vocab', 'vocab']
|
||||
- ['StringStore', 'stringstore']
|
||||
---
|
||||
|
||||
## Doc {id="doc",tag="cdef class",source="spacy/tokens/doc.pxd"}
|
||||
|
||||
The `Doc` object holds an array of [`TokenC`](/api/cython-structs#tokenc)
|
||||
structs.
|
||||
|
||||
<Infobox variant="warning">
|
||||
|
||||
This section documents the extra C-level attributes and methods that can't be
|
||||
accessed from Python. For the Python documentation, see [`Doc`](/api/doc).
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Attributes {id="doc_attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | -------------------------------------------------------------------------------------------------------- |
|
||||
| `mem` | A memory pool. Allocated memory will be freed once the `Doc` object is garbage collected. ~~cymem.Pool~~ |
|
||||
| `vocab` | A reference to the shared `Vocab` object. ~~Vocab~~ |
|
||||
| `c` | A pointer to a [`TokenC`](/api/cython-structs#tokenc) struct. ~~TokenC\*~~ |
|
||||
| `length` | The number of tokens in the document. ~~int~~ |
|
||||
| `max_length` | The underlying size of the `Doc.c` array. ~~int~~ |
|
||||
|
||||
### Doc.push_back {id="doc_push_back",tag="method"}
|
||||
|
||||
Append a token to the `Doc`. The token can be provided as a
|
||||
[`LexemeC`](/api/cython-structs#lexemec) or
|
||||
[`TokenC`](/api/cython-structs#tokenc) pointer, using Cython's
|
||||
[fused types](http://cython.readthedocs.io/en/latest/src/userguide/fusedtypes.html).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens cimport Doc
|
||||
> from spacy.vocab cimport Vocab
|
||||
>
|
||||
> doc = Doc(Vocab())
|
||||
> lexeme = doc.vocab.get("hello")
|
||||
> doc.push_back(lexeme, True)
|
||||
> assert doc.text == "hello "
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | -------------------------------------------------- |
|
||||
| `lex_or_tok` | The word to append to the `Doc`. ~~LexemeOrToken~~ |
|
||||
| `has_space` | Whether the word has trailing whitespace. ~~bint~~ |
|
||||
|
||||
## Token {id="token",tag="cdef class",source="spacy/tokens/token.pxd"}
|
||||
|
||||
A Cython class providing access and methods for a
|
||||
[`TokenC`](/api/cython-structs#tokenc) struct. Note that the `Token` object does
|
||||
not own the struct. It only receives a pointer to it.
|
||||
|
||||
<Infobox variant="warning">
|
||||
|
||||
This section documents the extra C-level attributes and methods that can't be
|
||||
accessed from Python. For the Python documentation, see [`Token`](/api/token).
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Attributes {id="token_attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------------------- |
|
||||
| `vocab` | A reference to the shared `Vocab` object. ~~Vocab~~ |
|
||||
| `c` | A pointer to a [`TokenC`](/api/cython-structs#tokenc) struct. ~~TokenC\*~~ |
|
||||
| `i` | The offset of the token within the document. ~~int~~ |
|
||||
| `doc` | The parent document. ~~Doc~~ |
|
||||
|
||||
### Token.cinit {id="token_cinit",tag="method"}
|
||||
|
||||
Create a `Token` object from a `TokenC*` pointer.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> token = Token.cinit(&doc.c[3], doc, 3)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------------------------------- |
|
||||
| `vocab` | A reference to the shared `Vocab`. ~~Vocab~~ |
|
||||
| `c` | A pointer to a [`TokenC`](/api/cython-structs#tokenc) struct. ~~TokenC\*~~ |
|
||||
| `offset` | The offset of the token within the document. ~~int~~ |
|
||||
| `doc` | The parent document. ~~int~~ |
|
||||
|
||||
## Span {id="span",tag="cdef class",source="spacy/tokens/span.pxd"}
|
||||
|
||||
A Cython class providing access and methods for a slice of a `Doc` object.
|
||||
|
||||
<Infobox variant="warning">
|
||||
|
||||
This section documents the extra C-level attributes and methods that can't be
|
||||
accessed from Python. For the Python documentation, see [`Span`](/api/span).
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Attributes {id="span_attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | ----------------------------------------------------------------------------- |
|
||||
| `doc` | The parent document. ~~Doc~~ |
|
||||
| `start` | The index of the first token of the span. ~~int~~ |
|
||||
| `end` | The index of the first token after the span. ~~int~~ |
|
||||
| `start_char` | The index of the first character of the span. ~~int~~ |
|
||||
| `end_char` | The index of the last character of the span. ~~int~~ |
|
||||
| `label` | A label to attach to the span, e.g. for named entities. ~~attr_t (uint64_t)~~ |
|
||||
|
||||
## Lexeme {id="lexeme",tag="cdef class",source="spacy/lexeme.pxd"}
|
||||
|
||||
A Cython class providing access and methods for an entry in the vocabulary.
|
||||
|
||||
<Infobox variant="warning">
|
||||
|
||||
This section documents the extra C-level attributes and methods that can't be
|
||||
accessed from Python. For the Python documentation, see [`Lexeme`](/api/lexeme).
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Attributes {id="lexeme_attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| ------- | ----------------------------------------------------------------------------- |
|
||||
| `c` | A pointer to a [`LexemeC`](/api/cython-structs#lexemec) struct. ~~LexemeC\*~~ |
|
||||
| `vocab` | A reference to the shared `Vocab` object. ~~Vocab~~ |
|
||||
| `orth` | ID of the verbatim text content. ~~attr_t (uint64_t)~~ |
|
||||
|
||||
## Vocab {id="vocab",tag="cdef class",source="spacy/vocab.pxd"}
|
||||
|
||||
A Cython class providing access and methods for a vocabulary and other data
|
||||
shared across a language.
|
||||
|
||||
<Infobox variant="warning">
|
||||
|
||||
This section documents the extra C-level attributes and methods that can't be
|
||||
accessed from Python. For the Python documentation, see [`Vocab`](/api/vocab).
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Attributes {id="vocab_attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| --------- | ---------------------------------------------------------------------------------------------------------- |
|
||||
| `mem` | A memory pool. Allocated memory will be freed once the `Vocab` object is garbage collected. ~~cymem.Pool~~ |
|
||||
| `strings` | A `StringStore` that maps string to hash values and vice versa. ~~StringStore~~ |
|
||||
| `length` | The number of entries in the vocabulary. ~~int~~ |
|
||||
|
||||
### Vocab.get {id="vocab_get",tag="method"}
|
||||
|
||||
Retrieve a [`LexemeC*`](/api/cython-structs#lexemec) pointer from the
|
||||
vocabulary.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lexeme = vocab.get(vocab.mem, "hello")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------------- |
|
||||
| `mem` | A memory pool. Allocated memory will be freed once the `Vocab` object is garbage collected. ~~cymem.Pool~~ |
|
||||
| `string` | The string of the word to look up. ~~str~~ |
|
||||
| **RETURNS** | The lexeme in the vocabulary. ~~const LexemeC\*~~ |
|
||||
|
||||
### Vocab.get_by_orth {id="vocab_get_by_orth",tag="method"}
|
||||
|
||||
Retrieve a [`LexemeC*`](/api/cython-structs#lexemec) pointer from the
|
||||
vocabulary.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lexeme = vocab.get_by_orth(doc[0].lex.norm)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------------- |
|
||||
| `mem` | A memory pool. Allocated memory will be freed once the `Vocab` object is garbage collected. ~~cymem.Pool~~ |
|
||||
| `orth` | ID of the verbatim text content. ~~attr_t (uint64_t)~~ |
|
||||
| **RETURNS** | The lexeme in the vocabulary. ~~const LexemeC\*~~ |
|
||||
|
||||
## StringStore {id="stringstore",tag="cdef class",source="spacy/strings.pxd"}
|
||||
|
||||
A lookup table to retrieve strings by 64-bit hashes.
|
||||
|
||||
<Infobox variant="warning">
|
||||
|
||||
This section documents the extra C-level attributes and methods that can't be
|
||||
accessed from Python. For the Python documentation, see
|
||||
[`StringStore`](/api/stringstore).
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Attributes {id="stringstore_attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| ------ | ---------------------------------------------------------------------------------------------------------------- |
|
||||
| `mem` | A memory pool. Allocated memory will be freed once the `StringStore` object is garbage collected. ~~cymem.Pool~~ |
|
||||
| `keys` | A list of hash values in the `StringStore`. ~~vector[hash_t] \(vector[uint64_t])~~ |
|
||||
@@ -0,0 +1,253 @@
|
||||
---
|
||||
title: Cython Structs
|
||||
teaser: C-language objects that let you group variables together
|
||||
next: /api/cython-classes
|
||||
menu:
|
||||
- ['TokenC', 'tokenc']
|
||||
- ['LexemeC', 'lexemec']
|
||||
---
|
||||
|
||||
## TokenC {id="tokenc",tag="C struct",source="spacy/structs.pxd"}
|
||||
|
||||
Cython data container for the `Token` object.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> token = &doc.c[3]
|
||||
> token_ptr = &doc.c[3]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `lex` | A pointer to the lexeme for the token. ~~const LexemeC\*~~ |
|
||||
| `morph` | An ID allowing lookup of morphological attributes. ~~uint64_t~~ |
|
||||
| `pos` | Coarse-grained part-of-speech tag. ~~univ_pos_t~~ |
|
||||
| `spacy` | A binary value indicating whether the token has trailing whitespace. ~~bint~~ |
|
||||
| `tag` | Fine-grained part-of-speech tag. ~~attr_t (uint64_t)~~ |
|
||||
| `idx` | The character offset of the token within the parent document. ~~int~~ |
|
||||
| `lemma` | Base form of the token, with no inflectional suffixes. ~~attr_t (uint64_t)~~ |
|
||||
| `sense` | Space for storing a word sense ID, currently unused. ~~attr_t (uint64_t)~~ |
|
||||
| `head` | Offset of the syntactic parent relative to the token. ~~int~~ |
|
||||
| `dep` | Syntactic dependency relation. ~~attr_t (uint64_t)~~ |
|
||||
| `l_kids` | Number of left children. ~~uint32_t~~ |
|
||||
| `r_kids` | Number of right children. ~~uint32_t~~ |
|
||||
| `l_edge` | Offset of the leftmost token of this token's syntactic descendants. ~~uint32_t~~ |
|
||||
| `r_edge` | Offset of the rightmost token of this token's syntactic descendants. ~~uint32_t~~ |
|
||||
| `sent_start` | Ternary value indicating whether the token is the first word of a sentence. `0` indicates a missing value, `-1` indicates `False` and `1` indicates `True`. The default value, 0, is interpreted as no sentence break. Sentence boundary detectors will usually set 0 for all tokens except tokens that follow a sentence boundary. ~~int~~ |
|
||||
| `ent_iob` | IOB code of named entity tag. `0` indicates a missing value, `1` indicates `I`, `2` indicates `0` and `3` indicates `B`. ~~int~~ |
|
||||
| `ent_type` | Named entity type. ~~attr_t (uint64_t)~~ |
|
||||
| `ent_id` | ID of the entity the token is an instance of, if any. Currently not used, but potentially for coreference resolution. ~~attr_t (uint64_t)~~ |
|
||||
|
||||
### Token.get_struct_attr {id="token_get_struct_attr",tag="staticmethod, nogil",source="spacy/tokens/token.pxd"}
|
||||
|
||||
Get the value of an attribute from the `TokenC` struct by attribute ID.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.attrs cimport IS_ALPHA
|
||||
> from spacy.tokens cimport Token
|
||||
>
|
||||
> is_alpha = Token.get_struct_attr(&doc.c[3], IS_ALPHA)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------- |
|
||||
| `token` | A pointer to a `TokenC` struct. ~~const TokenC\*~~ |
|
||||
| `feat_name` | The ID of the attribute to look up. The attributes are enumerated in `spacy.typedefs`. ~~attr_id_t~~ |
|
||||
| **RETURNS** | The value of the attribute. ~~attr_t (uint64_t)~~ |
|
||||
|
||||
### Token.set_struct_attr {id="token_set_struct_attr",tag="staticmethod, nogil",source="spacy/tokens/token.pxd"}
|
||||
|
||||
Set the value of an attribute of the `TokenC` struct by attribute ID.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.attrs cimport TAG
|
||||
> from spacy.tokens cimport Token
|
||||
>
|
||||
> token = &doc.c[3]
|
||||
> Token.set_struct_attr(token, TAG, 0)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------- |
|
||||
| `token` | A pointer to a `TokenC` struct. ~~const TokenC\*~~ |
|
||||
| `feat_name` | The ID of the attribute to look up. The attributes are enumerated in `spacy.typedefs`. ~~attr_id_t~~ |
|
||||
| `value` | The value to set. ~~attr_t (uint64_t)~~ |
|
||||
|
||||
### token_by_start {id="token_by_start",tag="function",source="spacy/tokens/doc.pxd"}
|
||||
|
||||
Find a token in a `TokenC*` array by the offset of its first character.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens.doc cimport Doc, token_by_start
|
||||
> from spacy.vocab cimport Vocab
|
||||
>
|
||||
> doc = Doc(Vocab(), words=["hello", "world"])
|
||||
> assert token_by_start(doc.c, doc.length, 6) == 1
|
||||
> assert token_by_start(doc.c, doc.length, 4) == -1
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | ----------------------------------------------------------------- |
|
||||
| `tokens` | A `TokenC*` array. ~~const TokenC\*~~ |
|
||||
| `length` | The number of tokens in the array. ~~int~~ |
|
||||
| `start_char` | The start index to search for. ~~int~~ |
|
||||
| **RETURNS** | The index of the token in the array or `-1` if not found. ~~int~~ |
|
||||
|
||||
### token_by_end {id="token_by_end",tag="function",source="spacy/tokens/doc.pxd"}
|
||||
|
||||
Find a token in a `TokenC*` array by the offset of its final character.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens.doc cimport Doc, token_by_end
|
||||
> from spacy.vocab cimport Vocab
|
||||
>
|
||||
> doc = Doc(Vocab(), words=["hello", "world"])
|
||||
> assert token_by_end(doc.c, doc.length, 5) == 0
|
||||
> assert token_by_end(doc.c, doc.length, 1) == -1
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------- |
|
||||
| `tokens` | A `TokenC*` array. ~~const TokenC\*~~ |
|
||||
| `length` | The number of tokens in the array. ~~int~~ |
|
||||
| `end_char` | The end index to search for. ~~int~~ |
|
||||
| **RETURNS** | The index of the token in the array or `-1` if not found. ~~int~~ |
|
||||
|
||||
### set_children_from_heads {id="set_children_from_heads",tag="function",source="spacy/tokens/doc.pxd"}
|
||||
|
||||
Set attributes that allow lookup of syntactic children on a `TokenC*` array.
|
||||
This function must be called after making changes to the `TokenC.head`
|
||||
attribute, in order to make the parse tree navigation consistent.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens.doc cimport Doc, set_children_from_heads
|
||||
> from spacy.vocab cimport Vocab
|
||||
>
|
||||
> doc = Doc(Vocab(), words=["Baileys", "from", "a", "shoe"])
|
||||
> doc.c[0].head = 0
|
||||
> doc.c[1].head = 0
|
||||
> doc.c[2].head = 3
|
||||
> doc.c[3].head = 1
|
||||
> set_children_from_heads(doc.c, doc.length)
|
||||
> assert doc.c[3].l_kids == 1
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | ------------------------------------------ |
|
||||
| `tokens` | A `TokenC*` array. ~~const TokenC\*~~ |
|
||||
| `length` | The number of tokens in the array. ~~int~~ |
|
||||
|
||||
## LexemeC {id="lexemec",tag="C struct",source="spacy/structs.pxd"}
|
||||
|
||||
Struct holding information about a lexical type. `LexemeC` structs are usually
|
||||
owned by the `Vocab`, and accessed through a read-only pointer on the `TokenC`
|
||||
struct.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lex = doc.c[3].lex
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `flags` | Bit-field for binary lexical flag values. ~~flags_t (uint64_t)~~ |
|
||||
| `id` | Usually used to map lexemes to rows in a matrix, e.g. for word vectors. Does not need to be unique, so currently misnamed. ~~attr_t (uint64_t)~~ |
|
||||
| `length` | Number of unicode characters in the lexeme. ~~attr_t (uint64_t)~~ |
|
||||
| `orth` | ID of the verbatim text content. ~~attr_t (uint64_t)~~ |
|
||||
| `lower` | ID of the lowercase form of the lexeme. ~~attr_t (uint64_t)~~ |
|
||||
| `norm` | ID of the lexeme's norm, i.e. a normalized form of the text. ~~attr_t (uint64_t)~~ |
|
||||
| `shape` | Transform of the lexeme's string, to show orthographic features. ~~attr_t (uint64_t)~~ |
|
||||
| `prefix` | Length-N substring from the start of the lexeme. Defaults to `N=1`. ~~attr_t (uint64_t)~~ |
|
||||
| `suffix` | Length-N substring from the end of the lexeme. Defaults to `N=3`. ~~attr_t (uint64_t)~~ |
|
||||
|
||||
### Lexeme.get_struct_attr {id="lexeme_get_struct_attr",tag="staticmethod, nogil",source="spacy/lexeme.pxd"}
|
||||
|
||||
Get the value of an attribute from the `LexemeC` struct by attribute ID.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.attrs cimport IS_ALPHA
|
||||
> from spacy.lexeme cimport Lexeme
|
||||
>
|
||||
> lexeme = doc.c[3].lex
|
||||
> is_alpha = Lexeme.get_struct_attr(lexeme, IS_ALPHA)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------- |
|
||||
| `lex` | A pointer to a `LexemeC` struct. ~~const LexemeC\*~~ |
|
||||
| `feat_name` | The ID of the attribute to look up. The attributes are enumerated in `spacy.typedefs`. ~~attr_id_t~~ |
|
||||
| **RETURNS** | The value of the attribute. ~~attr_t (uint64_t)~~ |
|
||||
|
||||
### Lexeme.set_struct_attr {id="lexeme_set_struct_attr",tag="staticmethod, nogil",source="spacy/lexeme.pxd"}
|
||||
|
||||
Set the value of an attribute of the `LexemeC` struct by attribute ID.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.attrs cimport NORM
|
||||
> from spacy.lexeme cimport Lexeme
|
||||
>
|
||||
> lexeme = doc.c[3].lex
|
||||
> Lexeme.set_struct_attr(lexeme, NORM, lexeme.lower)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------- |
|
||||
| `lex` | A pointer to a `LexemeC` struct. ~~const LexemeC\*~~ |
|
||||
| `feat_name` | The ID of the attribute to look up. The attributes are enumerated in `spacy.typedefs`. ~~attr_id_t~~ |
|
||||
| `value` | The value to set. ~~attr_t (uint64_t)~~ |
|
||||
|
||||
### Lexeme.c_check_flag {id="lexeme_c_check_flag",tag="staticmethod, nogil",source="spacy/lexeme.pxd"}
|
||||
|
||||
Check the value of a binary flag attribute.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.attrs cimport IS_STOP
|
||||
> from spacy.lexeme cimport Lexeme
|
||||
>
|
||||
> lexeme = doc.c[3].lex
|
||||
> is_stop = Lexeme.c_check_flag(lexeme, IS_STOP)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------------------------------------------- |
|
||||
| `lexeme` | A pointer to a `LexemeC` struct. ~~const LexemeC\*~~ |
|
||||
| `flag_id` | The ID of the flag to look up. The flag IDs are enumerated in `spacy.typedefs`. ~~attr_id_t~~ |
|
||||
| **RETURNS** | The boolean value of the flag. ~~bint~~ |
|
||||
|
||||
### Lexeme.c_set_flag {id="lexeme_c_set_flag",tag="staticmethod, nogil",source="spacy/lexeme.pxd"}
|
||||
|
||||
Set the value of a binary flag attribute.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.attrs cimport IS_STOP
|
||||
> from spacy.lexeme cimport Lexeme
|
||||
>
|
||||
> lexeme = doc.c[3].lex
|
||||
> Lexeme.c_set_flag(lexeme, IS_STOP, 0)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------- | --------------------------------------------------------------------------------------------- |
|
||||
| `lexeme` | A pointer to a `LexemeC` struct. ~~const LexemeC\*~~ |
|
||||
| `flag_id` | The ID of the flag to look up. The flag IDs are enumerated in `spacy.typedefs`. ~~attr_id_t~~ |
|
||||
| `value` | The value to set. ~~bint~~ |
|
||||
@@ -0,0 +1,134 @@
|
||||
---
|
||||
title: Cython Architecture
|
||||
next: /api/cython-structs
|
||||
menu:
|
||||
- ['Overview', 'overview']
|
||||
- ['Conventions', 'conventions']
|
||||
---
|
||||
|
||||
## Overview {id="overview",hidden="true"}
|
||||
|
||||
> #### What's Cython?
|
||||
>
|
||||
> [Cython](http://cython.org/) is a language for writing C extensions for
|
||||
> Python. Most Python code is also valid Cython, but you can add type
|
||||
> declarations to get efficient memory-managed code just like C or C++.
|
||||
|
||||
This section documents spaCy's C-level data structures and interfaces, intended
|
||||
for use from Cython. Some of the attributes are primarily for internal use, and
|
||||
all C-level functions and methods are designed for speed over safety – if you
|
||||
make a mistake and access an array out-of-bounds, the program may crash
|
||||
abruptly.
|
||||
|
||||
With Cython there are four ways of declaring complex data types. Unfortunately
|
||||
we use all four in different places, as they all have different utility:
|
||||
|
||||
| Declaration | Description | Example |
|
||||
| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
|
||||
| `class` | A normal Python class. | [`Language`](/api/language) |
|
||||
| `cdef class` | A Python extension type. Differs from a normal Python class in that its attributes can be defined on the underlying struct. Can have C-level objects as attributes (notably structs and pointers), and can have methods which have C-level objects as arguments or return types. | [`Lexeme`](/api/cython-classes#lexeme) |
|
||||
| `cdef struct` | A struct is just a collection of variables, sort of like a named tuple, except the memory is contiguous. Structs can't have methods, only attributes. | [`LexemeC`](/api/cython-structs#lexemec) |
|
||||
| `cdef cppclass` | A C++ class. Like a struct, this can be allocated on the stack, but can have methods, a constructor and a destructor. Differs from `cdef class` in that it can be created and destroyed without acquiring the Python global interpreter lock. This style is the most obscure. | [`StateC`](%%GITHUB_SPACY/spacy/pipeline/_parser_internals/_state.pxd) |
|
||||
|
||||
The most important classes in spaCy are defined as `cdef class` objects. The
|
||||
underlying data for these objects is usually gathered into a struct, which is
|
||||
usually named `c`. For instance, the [`Lexeme`](/api/cython-classes#lexeme)
|
||||
class holds a [`LexemeC`](/api/cython-structs#lexemec) struct, at `Lexeme.c`.
|
||||
This lets you shed the Python container, and pass a pointer to the underlying
|
||||
data into C-level functions.
|
||||
|
||||
## Conventions {id="conventions"}
|
||||
|
||||
spaCy's core data structures are implemented as [Cython](http://cython.org/)
|
||||
`cdef` classes. Memory is managed through the
|
||||
[`cymem`](https://github.com/explosion/cymem) `cymem.Pool` class, which allows
|
||||
you to allocate memory which will be freed when the `Pool` object is garbage
|
||||
collected. This means you usually don't have to worry about freeing memory. You
|
||||
just have to decide which Python object owns the memory, and make it own the
|
||||
`Pool`. When that object goes out of scope, the memory will be freed. You do
|
||||
have to take care that no pointers outlive the object that owns them — but this
|
||||
is generally quite easy.
|
||||
|
||||
All Cython modules should have the `# cython: infer_types=True` compiler
|
||||
directive at the top of the file. This makes the code much cleaner, as it avoids
|
||||
the need for many type declarations. If possible, you should prefer to declare
|
||||
your functions `nogil`, even if you don't especially care about multi-threading.
|
||||
The reason is that `nogil` functions help the Cython compiler reason about your
|
||||
code quite a lot — you're telling the compiler that no Python dynamics are
|
||||
possible. This lets many errors be raised, and ensures your function will run at
|
||||
C speed.
|
||||
|
||||
Cython gives you many choices of sequences: you could have a Python list, a
|
||||
numpy array, a memory view, a C++ vector, or a pointer. Pointers are preferred,
|
||||
because they are fastest, have the most explicit semantics, and let the compiler
|
||||
check your code more strictly. C++ vectors are also great — but you should only
|
||||
use them internally in functions. It's less friendly to accept a vector as an
|
||||
argument, because that asks the user to do much more work. Here's how to get a
|
||||
pointer from a numpy array, memory view or vector:
|
||||
|
||||
```python
|
||||
cdef void get_pointers(np.ndarray[int, mode='c'] numpy_array, vector[int] cpp_vector, int[::1] memory_view) nogil:
|
||||
pointer1 = <int*>numpy_array.data
|
||||
pointer2 = cpp_vector.data()
|
||||
pointer3 = &memory_view[0]
|
||||
```
|
||||
|
||||
Both C arrays and C++ vectors reassure the compiler that no Python operations
|
||||
are possible on your variable. This is a big advantage: it lets the Cython
|
||||
compiler raise many more errors for you.
|
||||
|
||||
When getting a pointer from a numpy array or memoryview, take care that the data
|
||||
is actually stored in C-contiguous order — otherwise you'll get a pointer to
|
||||
nonsense. The type-declarations in the code above should generate runtime errors
|
||||
if buffers with incorrect memory layouts are passed in. To iterate over the
|
||||
array, the following style is preferred:
|
||||
|
||||
```python
|
||||
cdef int c_total(const int* int_array, int length) nogil:
|
||||
total = 0
|
||||
for item in int_array[:length]:
|
||||
total += item
|
||||
return total
|
||||
```
|
||||
|
||||
If this is confusing, consider that the compiler couldn't deal with
|
||||
`for item in int_array:` — there's no length attached to a raw pointer, so how
|
||||
could we figure out where to stop? The length is provided in the slice notation
|
||||
as a solution to this. Note that we don't have to declare the type of `item` in
|
||||
the code above — the compiler can easily infer it. This gives us tidy code that
|
||||
looks quite like Python, but is exactly as fast as C — because we've made sure
|
||||
the compilation to C is trivial.
|
||||
|
||||
Your functions cannot be declared `nogil` if they need to create Python objects
|
||||
or call Python functions. This is perfectly okay — you shouldn't torture your
|
||||
code just to get `nogil` functions. However, if your function isn't `nogil`, you
|
||||
should compile your module with `cython -a --cplus my_module.pyx` and open the
|
||||
resulting `my_module.html` file in a browser. This will let you see how Cython
|
||||
is compiling your code. Calls into the Python run-time will be in bright yellow.
|
||||
This lets you easily see whether Cython is able to correctly type your code, or
|
||||
whether there are unexpected problems.
|
||||
|
||||
Working in Cython is very rewarding once you're over the initial learning curve.
|
||||
As with C and C++, the first way you write something in Cython will often be the
|
||||
performance-optimal approach. In contrast, Python optimization generally
|
||||
requires a lot of experimentation. Is it faster to have an `if item in my_dict`
|
||||
check, or to use `.get()`? What about `try`/`except`? Does this numpy operation
|
||||
create a copy? There's no way to guess the answers to these questions, and
|
||||
you'll usually be dissatisfied with your results — so there's no way to know
|
||||
when to stop this process. In the worst case, you'll make a mess that invites
|
||||
the next reader to try their luck too. This is like one of those
|
||||
[volcanic gas-traps](http://www.wemjournal.org/article/S1080-6032%2809%2970088-2/abstract),
|
||||
where the rescuers keep passing out from low oxygen, causing another rescuer to
|
||||
follow — only to succumb themselves. In short, just say no to optimizing your
|
||||
Python. If it's not fast enough the first time, just switch to Cython.
|
||||
|
||||
<Infobox title="Resources" emoji="📖">
|
||||
|
||||
- [Official Cython documentation](http://docs.cython.org/en/latest/)
|
||||
(cython.org)
|
||||
- [Writing C in Cython](https://explosion.ai/blog/writing-c-in-cython)
|
||||
(explosion.ai)
|
||||
- [Multi-threading spaCy’s parser and named entity recognizer](https://explosion.ai/blog/multithreading-with-cython)
|
||||
(explosion.ai)
|
||||
|
||||
</Infobox>
|
||||
@@ -0,0 +1,597 @@
|
||||
---
|
||||
title: Data formats
|
||||
teaser: Details on spaCy's input and output data formats
|
||||
menu:
|
||||
- ['Training Config', 'config']
|
||||
- ['Training Data', 'training']
|
||||
- ['Vocabulary', 'vocab-jsonl']
|
||||
- ['Pipeline Meta', 'meta']
|
||||
---
|
||||
|
||||
This section documents input and output formats of data used by spaCy, including
|
||||
the [training config](/usage/training#config), training data and lexical
|
||||
vocabulary data. For an overview of label schemes used by the models, see the
|
||||
[models directory](/models). Each trained pipeline documents the label schemes
|
||||
used in its components, depending on the data it was trained on.
|
||||
|
||||
## Training config {id="config",version="3"}
|
||||
|
||||
Config files define the training process and pipeline and can be passed to
|
||||
[`spacy train`](/api/cli#train). They use
|
||||
[Thinc's configuration system](https://thinc.ai/docs/usage-config) under the
|
||||
hood. For details on how to use training configs, see the
|
||||
[usage documentation](/usage/training#config). To get started with the
|
||||
recommended settings for your use case, check out the
|
||||
[quickstart widget](/usage/training#quickstart) or run the
|
||||
[`init config`](/api/cli#init-config) command.
|
||||
|
||||
> #### What does the @ mean?
|
||||
>
|
||||
> The `@` syntax lets you refer to function names registered in the
|
||||
> [function registry](/api/top-level#registry). For example,
|
||||
> `@architectures = "spacy.HashEmbedCNN.v2"` refers to a registered function of
|
||||
> the name [spacy.HashEmbedCNN.v2](/api/architectures#HashEmbedCNN) and all
|
||||
> other values defined in its block will be passed into that function as
|
||||
> arguments. Those arguments depend on the registered function. See the usage
|
||||
> guide on [registered functions](/usage/training#config-functions) for details.
|
||||
|
||||
```ini
|
||||
%%GITHUB_SPACY/spacy/default_config.cfg
|
||||
```
|
||||
|
||||
<Infobox title="Notes on data validation" emoji="💡">
|
||||
|
||||
Under the hood, spaCy's configs are powered by our machine learning library
|
||||
[Thinc's config system](https://thinc.ai/docs/usage-config), which uses
|
||||
[`pydantic`](https://github.com/samuelcolvin/pydantic/) for data validation
|
||||
based on type hints. See [`spacy/schemas.py`](%%GITHUB_SPACY/spacy/schemas.py)
|
||||
for the schemas used to validate the default config. Arguments of registered
|
||||
functions are validated against their type annotations, if available. To debug
|
||||
your config and check that it's valid, you can run the
|
||||
[`spacy debug config`](/api/cli#debug-config) command.
|
||||
|
||||
</Infobox>
|
||||
|
||||
### nlp {id="config-nlp",tag="section"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```ini
|
||||
> [nlp]
|
||||
> lang = "en"
|
||||
> pipeline = ["tagger", "parser", "ner"]
|
||||
> before_creation = null
|
||||
> after_creation = null
|
||||
> after_pipeline_creation = null
|
||||
> batch_size = 1000
|
||||
>
|
||||
> [nlp.tokenizer]
|
||||
> @tokenizers = "spacy.Tokenizer.v1"
|
||||
> ```
|
||||
|
||||
Defines the `nlp` object, its tokenizer and
|
||||
[processing pipeline](/usage/processing-pipelines) component names.
|
||||
|
||||
| Name | Description |
|
||||
| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `lang` | Pipeline language [ISO code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes). Defaults to `null`. ~~str~~ |
|
||||
| `pipeline` | Names of pipeline components in order. Should correspond to sections in the `[components]` block, e.g. `[components.ner]`. See docs on [defining components](/usage/training#config-components). Defaults to `[]`. ~~List[str]~~ |
|
||||
| `disabled` | Names of pipeline components that are loaded but disabled by default and not run as part of the pipeline. Should correspond to components listed in `pipeline`. After a pipeline is loaded, disabled components can be enabled using [`Language.enable_pipe`](/api/language#enable_pipe). ~~List[str]~~ |
|
||||
| `before_creation` | Optional [callback](/usage/training#custom-code-nlp-callbacks) to modify `Language` subclass before it's initialized. Defaults to `null`. ~~Optional[Callable[[Type[Language]], Type[Language]]]~~ |
|
||||
| `after_creation` | Optional [callback](/usage/training#custom-code-nlp-callbacks) to modify `nlp` object right after it's initialized. Defaults to `null`. ~~Optional[Callable[[Language], Language]]~~ |
|
||||
| `after_pipeline_creation` | Optional [callback](/usage/training#custom-code-nlp-callbacks) to modify `nlp` object after the pipeline components have been added. Defaults to `null`. ~~Optional[Callable[[Language], Language]]~~ |
|
||||
| `tokenizer` | The tokenizer to use. Defaults to [`Tokenizer`](/api/tokenizer). ~~Callable[[str], Doc]~~ |
|
||||
| `batch_size` | Default batch size for [`Language.pipe`](/api/language#pipe) and [`Language.evaluate`](/api/language#evaluate). ~~int~~ |
|
||||
|
||||
### components {id="config-components",tag="section"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```ini
|
||||
> [components.textcat]
|
||||
> factory = "textcat"
|
||||
>
|
||||
> [components.textcat.model]
|
||||
> @architectures = "spacy.TextCatBOW.v2"
|
||||
> exclusive_classes = true
|
||||
> ngram_size = 1
|
||||
> no_output_layer = false
|
||||
> ```
|
||||
|
||||
This section includes definitions of the
|
||||
[pipeline components](/usage/processing-pipelines) and their models, if
|
||||
available. Components in this section can be referenced in the `pipeline` of the
|
||||
`[nlp]` block. Component blocks need to specify either a `factory` (named
|
||||
function to use to create component) or a `source` (name of path of trained
|
||||
pipeline to copy components from). See the docs on
|
||||
[defining pipeline components](/usage/training#config-components) for details.
|
||||
|
||||
### paths, system {id="config-variables",tag="variables"}
|
||||
|
||||
These sections define variables that can be referenced across the other sections
|
||||
as variables. For example `${paths.train}` uses the value of `train` defined in
|
||||
the block `[paths]`. If your config includes custom registered functions that
|
||||
need paths, you can define them here. All config values can also be
|
||||
[overwritten](/usage/training#config-overrides) on the CLI when you run
|
||||
[`spacy train`](/api/cli#train), which is especially relevant for data paths
|
||||
that you don't want to hard-code in your config file.
|
||||
|
||||
```bash
|
||||
$ python -m spacy train config.cfg --paths.train ./corpus/train.spacy
|
||||
```
|
||||
|
||||
### corpora {id="config-corpora",tag="section"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```ini
|
||||
> [corpora]
|
||||
>
|
||||
> [corpora.train]
|
||||
> @readers = "spacy.Corpus.v1"
|
||||
> path = ${paths:train}
|
||||
>
|
||||
> [corpora.dev]
|
||||
> @readers = "spacy.Corpus.v1"
|
||||
> path = ${paths:dev}
|
||||
>
|
||||
> [corpora.pretrain]
|
||||
> @readers = "spacy.JsonlCorpus.v1"
|
||||
> path = ${paths.raw}
|
||||
>
|
||||
> [corpora.my_custom_data]
|
||||
> @readers = "my_custom_reader.v1"
|
||||
> ```
|
||||
|
||||
This section defines a **dictionary** mapping of string keys to functions. Each
|
||||
function takes an `nlp` object and yields [`Example`](/api/example) objects. By
|
||||
default, the two keys `train` and `dev` are specified and each refer to a
|
||||
[`Corpus`](/api/top-level#Corpus). When pretraining, an additional `pretrain`
|
||||
section is added that defaults to a [`JsonlCorpus`](/api/top-level#jsonlcorpus).
|
||||
You can also register custom functions that return a callable.
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `train` | Training data corpus, typically used in `[training]` block. ~~Callable[[Language], Iterator[Example]]~~ |
|
||||
| `dev` | Development data corpus, typically used in `[training]` block. ~~Callable[[Language], Iterator[Example]]~~ |
|
||||
| `pretrain` | Raw text for [pretraining](/usage/embeddings-transformers#pretraining), typically used in `[pretraining]` block (if available). ~~Callable[[Language], Iterator[Example]]~~ |
|
||||
| ... | Any custom or alternative corpora. ~~Callable[[Language], Iterator[Example]]~~ |
|
||||
|
||||
Alternatively, the `[corpora]` block can refer to **one function** that returns
|
||||
a dictionary keyed by the corpus names. This can be useful if you want to load a
|
||||
single corpus once and then divide it up into `train` and `dev` partitions.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```ini
|
||||
> [corpora]
|
||||
> @readers = "my_custom_reader.v1"
|
||||
> train_path = ${paths:train}
|
||||
> dev_path = ${paths:dev}
|
||||
> shuffle = true
|
||||
>
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `corpora` | A dictionary keyed by string names, mapped to corpus functions that receive the current `nlp` object and return an iterator of [`Example`](/api/example) objects. ~~Dict[str, Callable[[Language], Iterator[Example]]]~~ |
|
||||
|
||||
### training {id="config-training",tag="section"}
|
||||
|
||||
This section defines settings and controls for the training and evaluation
|
||||
process that are used when you run [`spacy train`](/api/cli#train).
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `accumulate_gradient` | Whether to divide the batch up into substeps. Defaults to `1`. ~~int~~ |
|
||||
| `batcher` | Callable that takes an iterator of [`Doc`](/api/doc) objects and yields batches of `Doc`s. Defaults to [`batch_by_words`](/api/top-level#batch_by_words). ~~Callable[[Iterator[Doc], Iterator[List[Doc]]]]~~ |
|
||||
| `before_to_disk` | Optional callback to modify `nlp` object right before it is saved to disk during and after training. Can be used to remove or reset config values or disable components. Defaults to `null`. ~~Optional[Callable[[Language], Language]]~~ |
|
||||
| `before_update` <Tag variant="new">3.5</Tag> | Optional callback that is invoked at the start of each training step with the `nlp` object and a `Dict` containing the following entries: `step`, `epoch`. Can be used to make deferred changes to components. Defaults to `null`. ~~Optional[Callable[[Language, Dict[str, Any]], None]]~~ |
|
||||
| `dev_corpus` | Dot notation of the config location defining the dev corpus. Defaults to `corpora.dev`. ~~str~~ |
|
||||
| `dropout` | The dropout rate. Defaults to `0.1`. ~~float~~ |
|
||||
| `eval_frequency` | How often to evaluate during training (steps). Defaults to `200`. ~~int~~ |
|
||||
| `frozen_components` | Pipeline component names that are "frozen" and shouldn't be initialized or updated during training. See [here](/usage/training#config-components) for details. Defaults to `[]`. ~~List[str]~~ |
|
||||
| `annotating_components` <Tag variant="new">3.1</Tag> | Pipeline component names that should set annotations on the predicted docs during training. See [here](/usage/training#annotating-components) for details. Defaults to `[]`. ~~List[str]~~ |
|
||||
| `gpu_allocator` | Library for cupy to route GPU memory allocation to. Can be `"pytorch"` or `"tensorflow"`. Defaults to variable `${system.gpu_allocator}`. ~~str~~ |
|
||||
| `logger` | Callable that takes the `nlp` and stdout and stderr `IO` objects, sets up the logger, and returns two new callables to log a training step and to finalize the logger. Defaults to [`ConsoleLogger`](/api/top-level#ConsoleLogger). ~~Callable[[Language, IO, IO], [Tuple[Callable[[Dict[str, Any]], None], Callable[[], None]]]]~~ |
|
||||
| `max_epochs` | Maximum number of epochs to train for. `0` means an unlimited number of epochs. `-1` means that the train corpus should be streamed rather than loaded into memory with no shuffling within the training loop. Defaults to `0`. ~~int~~ |
|
||||
| `max_steps` | Maximum number of update steps to train for. `0` means an unlimited number of steps. Defaults to `20000`. ~~int~~ |
|
||||
| `optimizer` | The optimizer. The learning rate schedule and other settings can be configured as part of the optimizer. Defaults to [`Adam`](https://thinc.ai/docs/api-optimizers#adam). ~~Optimizer~~ |
|
||||
| `patience` | How many steps to continue without improvement in evaluation score. `0` disables early stopping. Defaults to `1600`. ~~int~~ |
|
||||
| `score_weights` | Score names shown in metrics mapped to their weight towards the final weighted score. See [here](/usage/training#metrics) for details. Defaults to `{}`. ~~Dict[str, float]~~ |
|
||||
| `seed` | The random seed. Defaults to variable `${system.seed}`. ~~int~~ |
|
||||
| `train_corpus` | Dot notation of the config location defining the train corpus. Defaults to `corpora.train`. ~~str~~ |
|
||||
|
||||
### pretraining {id="config-pretraining",tag="section,optional"}
|
||||
|
||||
This section is optional and defines settings and controls for
|
||||
[language model pretraining](/usage/embeddings-transformers#pretraining). It's
|
||||
used when you run [`spacy pretrain`](/api/cli#pretrain).
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `max_epochs` | Maximum number of epochs. Defaults to `1000`. ~~int~~ |
|
||||
| `dropout` | The dropout rate. Defaults to `0.2`. ~~float~~ |
|
||||
| `n_save_every` | Saving frequency. Defaults to `null`. ~~Optional[int]~~ |
|
||||
| `objective` | The pretraining objective. Defaults to `{"type": "characters", "n_characters": 4}`. ~~Dict[str, Any]~~ |
|
||||
| `optimizer` | The optimizer. The learning rate schedule and other settings can be configured as part of the optimizer. Defaults to [`Adam`](https://thinc.ai/docs/api-optimizers#adam). ~~Optimizer~~ |
|
||||
| `corpus` | Dot notation of the config location defining the corpus with raw text. Defaults to `corpora.pretrain`. ~~str~~ |
|
||||
| `batcher` | Callable that takes an iterator of [`Doc`](/api/doc) objects and yields batches of `Doc`s. Defaults to [`batch_by_words`](/api/top-level#batch_by_words). ~~Callable[[Iterator[Doc], Iterator[List[Doc]]]]~~ |
|
||||
| `component` | Component name to identify the layer with the model to pretrain. Defaults to `"tok2vec"`. ~~str~~ |
|
||||
| `layer` | The specific layer of the model to pretrain. If empty, the whole model will be used. ~~str~~ |
|
||||
|
||||
### initialize {id="config-initialize",tag="section"}
|
||||
|
||||
This config block lets you define resources for **initializing the pipeline**.
|
||||
It's used by [`Language.initialize`](/api/language#initialize) and typically
|
||||
called right before training (but not at runtime). The section allows you to
|
||||
specify local file paths or custom functions to load data resources from,
|
||||
without requiring them at runtime when you load the trained pipeline back in.
|
||||
Also see the usage guides on the
|
||||
[config lifecycle](/usage/training#config-lifecycle) and
|
||||
[custom initialization](/usage/training#initialization).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```ini
|
||||
> [initialize]
|
||||
> vectors = "/path/to/vectors_nlp"
|
||||
> init_tok2vec = "/path/to/pretrain.bin"
|
||||
>
|
||||
> [initialize_components]
|
||||
>
|
||||
> [initialize.components.my_component]
|
||||
> data_path = "/path/to/component_data"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `after_init` | Optional callback to modify the `nlp` object after initialization. ~~Optional[Callable[[Language], Language]]~~ |
|
||||
| `before_init` | Optional callback to modify the `nlp` object before initialization. ~~Optional[Callable[[Language], Language]]~~ |
|
||||
| `components` | Additional arguments passed to the `initialize` method of a pipeline component, keyed by component name. If type annotations are available on the method, the config will be validated against them. The `initialize` methods will always receive the `get_examples` callback and the current `nlp` object. ~~Dict[str, Dict[str, Any]]~~ |
|
||||
| `init_tok2vec` | Optional path to pretrained tok2vec weights created with [`spacy pretrain`](/api/cli#pretrain). Defaults to variable `${paths.init_tok2vec}`. Ignored when actually running pretraining, as you're creating the file to be used later. ~~Optional[str]~~ |
|
||||
| `lookups` | Additional lexeme and vocab data from [`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data). Defaults to `null`. ~~Optional[Lookups]~~ |
|
||||
| `tokenizer` | Additional arguments passed to the `initialize` method of the specified tokenizer. Can be used for languages like Chinese that depend on dictionaries or trained models for tokenization. If type annotations are available on the method, the config will be validated against them. The `initialize` method will always receive the `get_examples` callback and the current `nlp` object. ~~Dict[str, Any]~~ |
|
||||
| `vectors` | Name or path of pipeline containing pretrained word vectors to use, e.g. created with [`init vectors`](/api/cli#init-vectors). Defaults to `null`. ~~Optional[str]~~ |
|
||||
| `vocab_data` | Path to JSONL-formatted [vocabulary file](/api/data-formats#vocab-jsonl) to initialize vocabulary. ~~Optional[str]~~ |
|
||||
|
||||
## Training data {id="training"}
|
||||
|
||||
### Binary training format {id="binary-training",version="3"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import DocBin
|
||||
> from spacy.training import Corpus
|
||||
>
|
||||
> doc_bin = DocBin(docs=docs)
|
||||
> doc_bin.to_disk("./data.spacy")
|
||||
> reader = Corpus("./data.spacy")
|
||||
> ```
|
||||
|
||||
The main data format used in spaCy v3.0 is a **binary format** created by
|
||||
serializing a [`DocBin`](/api/docbin), which represents a collection of `Doc`
|
||||
objects. This means that you can train spaCy pipelines using the same format it
|
||||
outputs: annotated `Doc` objects. The binary format is extremely **efficient in
|
||||
storage**, especially when packing multiple documents together.
|
||||
|
||||
Typically, the extension for these binary files is `.spacy`, and they are used
|
||||
as input format for specifying a [training corpus](/api/corpus) and for spaCy's
|
||||
CLI [`train`](/api/cli#train) command. The built-in
|
||||
[`convert`](/api/cli#convert) command helps you convert spaCy's previous
|
||||
[JSON format](#json-input) to the new binary format. It also supports conversion
|
||||
of the `.conllu` format used by the
|
||||
[Universal Dependencies corpora](https://github.com/UniversalDependencies).
|
||||
|
||||
Note that while this is the format used to save training data, you do not have
|
||||
to understand the internal details to use it or create training data. See the
|
||||
section on [preparing training data](/usage/training#training-data).
|
||||
|
||||
### JSON training format {id="json-input",tag="deprecated"}
|
||||
|
||||
<Infobox variant="warning" title="Changed in v3.0">
|
||||
|
||||
As of v3.0, the JSON input format is deprecated and is replaced by the
|
||||
[binary format](#binary-training). Instead of converting [`Doc`](/api/doc)
|
||||
objects to JSON, you can now serialize them directly using the
|
||||
[`DocBin`](/api/docbin) container and then use them as input data.
|
||||
|
||||
[`spacy convert`](/api/cli) lets you convert your JSON data to the new `.spacy`
|
||||
format:
|
||||
|
||||
```bash
|
||||
$ python -m spacy convert ./data.json .
|
||||
```
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Annotating entities
|
||||
>
|
||||
> Named entities are provided in the
|
||||
> [BILUO](/usage/linguistic-features#accessing-ner) notation. Tokens outside an
|
||||
> entity are set to `"O"` and tokens that are part of an entity are set to the
|
||||
> entity label, prefixed by the BILUO marker. For example `"B-ORG"` describes
|
||||
> the first token of a multi-token `ORG` entity and `"U-PERSON"` a single token
|
||||
> representing a `PERSON` entity. The
|
||||
> [`offsets_to_biluo_tags`](/api/top-level#offsets_to_biluo_tags) function can
|
||||
> help you convert entity offsets to the right format.
|
||||
|
||||
```python {title="Example structure"}
|
||||
[{
|
||||
"id": int, # ID of the document within the corpus
|
||||
"paragraphs": [{ # list of paragraphs in the corpus
|
||||
"raw": string, # raw text of the paragraph
|
||||
"sentences": [{ # list of sentences in the paragraph
|
||||
"tokens": [{ # list of tokens in the sentence
|
||||
"id": int, # index of the token in the document
|
||||
"dep": string, # dependency label
|
||||
"head": int, # offset of token head relative to token index
|
||||
"tag": string, # part-of-speech tag
|
||||
"orth": string, # verbatim text of the token
|
||||
"ner": string # BILUO label, e.g. "O" or "B-ORG"
|
||||
}],
|
||||
"brackets": [{ # phrase structure (NOT USED by current models)
|
||||
"first": int, # index of first token
|
||||
"last": int, # index of last token
|
||||
"label": string # phrase label
|
||||
}]
|
||||
}],
|
||||
"cats": [{ # new in v2.2: categories for text classifier
|
||||
"label": string, # text category label
|
||||
"value": float / bool # label applies (1.0/true) or not (0.0/false)
|
||||
}]
|
||||
}]
|
||||
}]
|
||||
```
|
||||
|
||||
<Accordion title="Sample JSON data" spaced>
|
||||
|
||||
Here's an example of dependencies, part-of-speech tags and named entities, taken
|
||||
from the English Wall Street Journal portion of the Penn Treebank:
|
||||
|
||||
```json
|
||||
https://github.com/explosion/spaCy/blob/v2.3.x/examples/training/training-data.json
|
||||
```
|
||||
|
||||
</Accordion>
|
||||
|
||||
### Annotation format for creating training examples {id="dict-input"}
|
||||
|
||||
An [`Example`](/api/example) object holds the information for one training
|
||||
instance. It stores two [`Doc`](/api/doc) objects: one for holding the
|
||||
gold-standard reference data, and one for holding the predictions of the
|
||||
pipeline. Examples can be created using the
|
||||
[`Example.from_dict`](/api/example#from_dict) method with a reference `Doc` and
|
||||
a dictionary of gold-standard annotations.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> example = Example.from_dict(doc, gold_dict)
|
||||
> ```
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
`Example` objects are used as part of the
|
||||
[internal training API](/usage/training#api) and they're expected when you call
|
||||
[`nlp.update`](/api/language#update). However, for most use cases, you
|
||||
**shouldn't** have to write your own training scripts. It's recommended to train
|
||||
your pipelines via the [`spacy train`](/api/cli#train) command with a config
|
||||
file to keep track of your settings and hyperparameters and your own
|
||||
[registered functions](/usage/training/#custom-code) to customize the setup.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> {
|
||||
> "text": str,
|
||||
> "words": List[str],
|
||||
> "lemmas": List[str],
|
||||
> "spaces": List[bool],
|
||||
> "tags": List[str],
|
||||
> "pos": List[str],
|
||||
> "morphs": List[str],
|
||||
> "sent_starts": List[Optional[bool]],
|
||||
> "deps": List[str],
|
||||
> "heads": List[int],
|
||||
> "entities": List[str],
|
||||
> "entities": List[(int, int, str)],
|
||||
> "cats": Dict[str, float],
|
||||
> "links": Dict[(int, int), dict],
|
||||
> "spans": Dict[str, List[Tuple]],
|
||||
> }
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `text` | Raw text. ~~str~~ |
|
||||
| `words` | List of gold-standard tokens. ~~List[str]~~ |
|
||||
| `lemmas` | List of lemmas. ~~List[str]~~ |
|
||||
| `spaces` | List of boolean values indicating whether the corresponding tokens is followed by a space or not. ~~List[bool]~~ |
|
||||
| `tags` | List of fine-grained [POS tags](/usage/linguistic-features#pos-tagging). ~~List[str]~~ |
|
||||
| `pos` | List of coarse-grained [POS tags](/usage/linguistic-features#pos-tagging). ~~List[str]~~ |
|
||||
| `morphs` | List of [morphological features](/usage/linguistic-features#rule-based-morphology). ~~List[str]~~ |
|
||||
| `sent_starts` | List of boolean values indicating whether each token is the first of a sentence or not. ~~List[bool]~~ |
|
||||
| `deps` | List of string values indicating the [dependency relation](/usage/linguistic-features#dependency-parse) of a token to its head. ~~List[str]~~ |
|
||||
| `heads` | List of integer values indicating the dependency head of each token, referring to the absolute index of each token in the text. ~~List[int]~~ |
|
||||
| `entities` | **Option 1:** List of [BILUO tags](/usage/linguistic-features#accessing-ner) per token of the format `"{action}-{label}"`, or `None` for unannotated tokens. ~~List[str]~~ |
|
||||
| `entities` | **Option 2:** List of `(start_char, end_char, label)` tuples defining all entities in the text. ~~List[Tuple[int, int, str]]~~ |
|
||||
| `cats` | Dictionary of `label`/`value` pairs indicating how relevant a certain [text category](/api/textcategorizer) is for the text. ~~Dict[str, float]~~ |
|
||||
| `links` | Dictionary of `offset`/`dict` pairs defining [named entity links](/usage/linguistic-features#entity-linking). The character offsets are linked to a dictionary of relevant knowledge base IDs. ~~Dict[Tuple[int, int], Dict]~~ |
|
||||
| `spans` | Dictionary of `spans_key`/`List[Tuple]` pairs defining the spans for each spans key as `(start_char, end_char, label, kb_id)` tuples. ~~Dict[str, List[Tuple[int, int, str, str]]~~ |
|
||||
|
||||
<Infobox title="Notes and caveats">
|
||||
|
||||
- Multiple formats are possible for the "entities" entry, but you have to pick
|
||||
one.
|
||||
- Any values for sentence starts will be ignored if there are annotations for
|
||||
dependency relations.
|
||||
- If the dictionary contains values for `"text"` and `"words"`, but not
|
||||
`"spaces"`, the latter are inferred automatically. If "words" is not provided
|
||||
either, the values are inferred from the `Doc` argument.
|
||||
|
||||
</Infobox>
|
||||
|
||||
```python {title="Examples"}
|
||||
# Training data for a part-of-speech tagger
|
||||
doc = Doc(vocab, words=["I", "like", "stuff"])
|
||||
gold_dict = {"tags": ["NOUN", "VERB", "NOUN"]}
|
||||
example = Example.from_dict(doc, gold_dict)
|
||||
|
||||
# Training data for an entity recognizer (option 1)
|
||||
doc = nlp("Laura flew to Silicon Valley.")
|
||||
gold_dict = {"entities": ["U-PERS", "O", "O", "B-LOC", "L-LOC"]}
|
||||
example = Example.from_dict(doc, gold_dict)
|
||||
|
||||
# Training data for an entity recognizer (option 2)
|
||||
doc = nlp("Laura flew to Silicon Valley.")
|
||||
gold_dict = {"entities": [(0, 5, "PERSON"), (14, 28, "LOC")]}
|
||||
example = Example.from_dict(doc, gold_dict)
|
||||
|
||||
# Training data for text categorization
|
||||
doc = nlp("I'm pretty happy about that!")
|
||||
gold_dict = {"cats": {"POSITIVE": 1.0, "NEGATIVE": 0.0}}
|
||||
example = Example.from_dict(doc, gold_dict)
|
||||
|
||||
# Training data for an Entity Linking component (also requires entities & sentences)
|
||||
doc = nlp("Russ Cochran his reprints include EC Comics.")
|
||||
gold_dict = {"entities": [(0, 12, "PERSON")],
|
||||
"links": {(0, 12): {"Q7381115": 1.0, "Q2146908": 0.0}},
|
||||
"sent_starts": [1, -1, -1, -1, -1, -1, -1, -1]}
|
||||
example = Example.from_dict(doc, gold_dict)
|
||||
```
|
||||
|
||||
## Lexical data for vocabulary {id="vocab-jsonl",version="2"}
|
||||
|
||||
This data file can be provided via the `vocab_data` setting in the
|
||||
`[initialize]` block of the training config to pre-define the lexical data to
|
||||
initialize the `nlp` object's vocabulary with. The file should contain one
|
||||
lexical entry per line. The first line defines the language and vocabulary
|
||||
settings. All other lines are expected to be JSON objects describing an
|
||||
individual lexeme. The lexical attributes will be then set as attributes on
|
||||
spaCy's [`Lexeme`](/api/lexeme#attributes) object.
|
||||
|
||||
> #### Example config
|
||||
>
|
||||
> ```ini
|
||||
> [initialize]
|
||||
> vocab_data = "/path/to/vocab-data.jsonl"
|
||||
> ```
|
||||
|
||||
```python {title="First line"}
|
||||
{"lang": "en", "settings": {"oov_prob": -20.502029418945312}}
|
||||
```
|
||||
|
||||
```python {title="Entry structure"}
|
||||
{
|
||||
"orth": string, # the word text
|
||||
"id": int, # can correspond to row in vectors table
|
||||
"lower": string,
|
||||
"norm": string,
|
||||
"shape": string
|
||||
"prefix": string,
|
||||
"suffix": string,
|
||||
"length": int,
|
||||
"cluster": string,
|
||||
"prob": float,
|
||||
"is_alpha": bool,
|
||||
"is_ascii": bool,
|
||||
"is_digit": bool,
|
||||
"is_lower": bool,
|
||||
"is_punct": bool,
|
||||
"is_space": bool,
|
||||
"is_title": bool,
|
||||
"is_upper": bool,
|
||||
"like_url": bool,
|
||||
"like_num": bool,
|
||||
"like_email": bool,
|
||||
"is_stop": bool,
|
||||
"is_oov": bool,
|
||||
"is_quote": bool,
|
||||
"is_left_punct": bool,
|
||||
"is_right_punct": bool
|
||||
}
|
||||
```
|
||||
|
||||
Here's an example of the 20 most frequent lexemes in the English training data:
|
||||
|
||||
```json
|
||||
%%GITHUB_SPACY/extra/example_data/vocab-data.jsonl
|
||||
```
|
||||
|
||||
## Pipeline meta {id="meta"}
|
||||
|
||||
The pipeline meta is available as the file `meta.json` and exported
|
||||
automatically when you save an `nlp` object to disk. Its contents are available
|
||||
as [`nlp.meta`](/api/language#meta).
|
||||
|
||||
<Infobox variant="warning" title="Changed in v3.0">
|
||||
|
||||
As of spaCy v3.0, the `meta.json` **isn't** used to construct the language class
|
||||
and pipeline anymore and only contains meta information for reference and for
|
||||
creating a Python package with [`spacy package`](/api/cli#package). How to set
|
||||
up the `nlp` object is now defined in the
|
||||
[config file](/api/data-formats#config), which includes detailed information
|
||||
about the pipeline components and their model architectures, and all other
|
||||
settings and hyperparameters used to train the pipeline. It's the **single
|
||||
source of truth** used for loading a pipeline.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```json
|
||||
> {
|
||||
> "name": "example_pipeline",
|
||||
> "lang": "en",
|
||||
> "version": "1.0.0",
|
||||
> "spacy_version": ">=3.0.0,<3.1.0",
|
||||
> "parent_package": "spacy",
|
||||
> "requirements": ["spacy-transformers>=1.0.0,<1.1.0"],
|
||||
> "description": "Example pipeline for spaCy",
|
||||
> "author": "You",
|
||||
> "email": "you@example.com",
|
||||
> "url": "https://example.com",
|
||||
> "license": "CC BY-SA 3.0",
|
||||
> "sources": [{ "name": "My Corpus", "license": "MIT" }],
|
||||
> "vectors": { "width": 0, "vectors": 0, "keys": 0, "name": null },
|
||||
> "pipeline": ["tok2vec", "ner", "textcat"],
|
||||
> "labels": {
|
||||
> "ner": ["PERSON", "ORG", "PRODUCT"],
|
||||
> "textcat": ["POSITIVE", "NEGATIVE"]
|
||||
> },
|
||||
> "performance": {
|
||||
> "ents_f": 82.7300930714,
|
||||
> "ents_p": 82.135523614,
|
||||
> "ents_r": 83.3333333333,
|
||||
> "textcat_score": 88.364323811
|
||||
> },
|
||||
> "speed": { "cpu": 7667.8, "gpu": null, "nwords": 10329 },
|
||||
> "spacy_git_version": "61dfdd9fb"
|
||||
> }
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `lang` | Pipeline language [ISO code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes). Defaults to `"en"`. ~~str~~ |
|
||||
| `name` | Pipeline name, e.g. `"core_web_sm"`. The final package name will be `{lang}_{name}`. Defaults to `"pipeline"`. ~~str~~ |
|
||||
| `version` | Pipeline version. Will be used to version a Python package created with [`spacy package`](/api/cli#package). Defaults to `"0.0.0"`. ~~str~~ |
|
||||
| `spacy_version` | spaCy version range the package is compatible with. Defaults to the spaCy version used to create the pipeline, up to next minor version, which is the default compatibility for the available [trained pipelines](/models). For instance, a pipeline trained with v3.0.0 will have the version range `">=3.0.0,<3.1.0"`. ~~str~~ |
|
||||
| `parent_package` | Name of the spaCy package. Typically `"spacy"` or `"spacy_nightly"`. Defaults to `"spacy"`. ~~str~~ |
|
||||
| `requirements` | Python package requirements that the pipeline depends on. Will be used for the Python package setup in [`spacy package`](/api/cli#package). Should be a list of package names with optional version specifiers, just like you'd define them in a `setup.cfg` or `requirements.txt`. Defaults to `[]`. ~~List[str]~~ |
|
||||
| `description` | Pipeline description. Also used for Python package. Defaults to `""`. ~~str~~ |
|
||||
| `author` | Pipeline author name. Also used for Python package. Defaults to `""`. ~~str~~ |
|
||||
| `email` | Pipeline author email. Also used for Python package. Defaults to `""`. ~~str~~ |
|
||||
| `url` | Pipeline author URL. Also used for Python package. Defaults to `""`. ~~str~~ |
|
||||
| `license` | Pipeline license. Also used for Python package. Defaults to `""`. ~~str~~ |
|
||||
| `sources` | Data sources used to train the pipeline. Typically a list of dicts with the keys `"name"`, `"url"`, `"author"` and `"license"`. [See here](https://github.com/explosion/spacy-models/tree/master/meta) for examples. Defaults to `None`. ~~Optional[List[Dict[str, str]]]~~ |
|
||||
| `vectors` | Information about the word vectors included with the pipeline. Typically a dict with the keys `"width"`, `"vectors"` (number of vectors), `"keys"` and `"name"`. ~~Dict[str, Any]~~ |
|
||||
| `pipeline` | Names of pipeline component names, in order. Corresponds to [`nlp.pipe_names`](/api/language#pipe_names). Only exists for reference and is not used to create the components. This information is defined in the [`config.cfg`](/api/data-formats#config). Defaults to `[]`. ~~List[str]~~ |
|
||||
| `labels` | Label schemes of the trained pipeline components, keyed by component name. Corresponds to [`nlp.pipe_labels`](/api/language#pipe_labels). [See here](https://github.com/explosion/spacy-models/tree/master/meta) for examples. Defaults to `{}`. ~~Dict[str, Dict[str, List[str]]]~~ |
|
||||
| `performance` | Training accuracy, added automatically by [`spacy train`](/api/cli#train). Dictionary of [score names](/usage/training#metrics) mapped to scores. Defaults to `{}`. ~~Dict[str, Union[float, Dict[str, float]]]~~ |
|
||||
| `speed` | Inference speed, added automatically by [`spacy train`](/api/cli#train). Typically a dictionary with the keys `"cpu"`, `"gpu"` and `"nwords"` (words per second). Defaults to `{}`. ~~Dict[str, Optional[Union[float, str]]]~~ |
|
||||
| `spacy_git_version` <Tag variant="new">3</Tag> | Git commit of [`spacy`](https://github.com/explosion/spaCy) used to create pipeline. ~~str~~ |
|
||||
| other | Any other custom meta information you want to add. The data is preserved in [`nlp.meta`](/api/language#meta). ~~Any~~ |
|
||||
@@ -0,0 +1,230 @@
|
||||
---
|
||||
title: DependencyMatcher
|
||||
teaser: Match subtrees within a dependency parse
|
||||
tag: class
|
||||
version: 3
|
||||
source: spacy/matcher/dependencymatcher.pyx
|
||||
---
|
||||
|
||||
The `DependencyMatcher` follows the same API as the [`Matcher`](/api/matcher)
|
||||
and [`PhraseMatcher`](/api/phrasematcher) and lets you match on dependency trees
|
||||
using
|
||||
[Semgrex operators](https://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/semgraph/semgrex/SemgrexPattern.html).
|
||||
It requires a pretrained [`DependencyParser`](/api/parser) or other component
|
||||
that sets the `Token.dep` and `Token.head` attributes. See the
|
||||
[usage guide](/usage/rule-based-matching#dependencymatcher) for examples.
|
||||
|
||||
## Pattern format {id="patterns"}
|
||||
|
||||
> ```python
|
||||
> ### Example
|
||||
> # pattern: "[subject] ... initially founded"
|
||||
> [
|
||||
> # anchor token: founded
|
||||
> {
|
||||
> "RIGHT_ID": "founded",
|
||||
> "RIGHT_ATTRS": {"ORTH": "founded"}
|
||||
> },
|
||||
> # founded -> subject
|
||||
> {
|
||||
> "LEFT_ID": "founded",
|
||||
> "REL_OP": ">",
|
||||
> "RIGHT_ID": "subject",
|
||||
> "RIGHT_ATTRS": {"DEP": "nsubj"}
|
||||
> },
|
||||
> # "founded" follows "initially"
|
||||
> {
|
||||
> "LEFT_ID": "founded",
|
||||
> "REL_OP": ";",
|
||||
> "RIGHT_ID": "initially",
|
||||
> "RIGHT_ATTRS": {"ORTH": "initially"}
|
||||
> }
|
||||
> ]
|
||||
> ```
|
||||
|
||||
A pattern added to the `DependencyMatcher` consists of a list of dictionaries,
|
||||
with each dictionary describing a token to match. Except for the first
|
||||
dictionary, which defines an anchor token using only `RIGHT_ID` and
|
||||
`RIGHT_ATTRS`, each pattern should have the following keys:
|
||||
|
||||
| Name | Description |
|
||||
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `LEFT_ID` | The name of the left-hand node in the relation, which has been defined in an earlier node. ~~str~~ |
|
||||
| `REL_OP` | An operator that describes how the two nodes are related. ~~str~~ |
|
||||
| `RIGHT_ID` | A unique name for the right-hand node in the relation. ~~str~~ |
|
||||
| `RIGHT_ATTRS` | The token attributes to match for the right-hand node in the same format as patterns provided to the regular token-based [`Matcher`](/api/matcher). ~~Dict[str, Any]~~ |
|
||||
|
||||
<Infobox title="Designing dependency matcher patterns" emoji="📖">
|
||||
|
||||
For examples of how to construct dependency matcher patterns for different types
|
||||
of relations, see the usage guide on
|
||||
[dependency matching](/usage/rule-based-matching#dependencymatcher).
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Operators {id="operators"}
|
||||
|
||||
The following operators are supported by the `DependencyMatcher`, most of which
|
||||
come directly from
|
||||
[Semgrex](https://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/semgraph/semgrex/SemgrexPattern.html):
|
||||
|
||||
| Symbol | Description |
|
||||
| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `A < B` | `A` is the immediate dependent of `B`. |
|
||||
| `A > B` | `A` is the immediate head of `B`. |
|
||||
| `A << B` | `A` is the dependent in a chain to `B` following dep → head paths. |
|
||||
| `A >> B` | `A` is the head in a chain to `B` following head → dep paths. |
|
||||
| `A . B` | `A` immediately precedes `B`, i.e. `A.i == B.i - 1`, and both are within the same dependency tree. |
|
||||
| `A .* B` | `A` precedes `B`, i.e. `A.i < B.i`, and both are within the same dependency tree _(Semgrex counterpart: `..`)_. |
|
||||
| `A ; B` | `A` immediately follows `B`, i.e. `A.i == B.i + 1`, and both are within the same dependency tree _(Semgrex counterpart: `-`)_. |
|
||||
| `A ;* B` | `A` follows `B`, i.e. `A.i > B.i`, and both are within the same dependency tree _(Semgrex counterpart: `--`)_. |
|
||||
| `A $+ B` | `B` is a right immediate sibling of `A`, i.e. `A` and `B` have the same parent and `A.i == B.i - 1`. |
|
||||
| `A $- B` | `B` is a left immediate sibling of `A`, i.e. `A` and `B` have the same parent and `A.i == B.i + 1`. |
|
||||
| `A $++ B` | `B` is a right sibling of `A`, i.e. `A` and `B` have the same parent and `A.i < B.i`. |
|
||||
| `A $-- B` | `B` is a left sibling of `A`, i.e. `A` and `B` have the same parent and `A.i > B.i`. |
|
||||
| `A >+ B` <Tag variant="new">3.5.1</Tag> | `B` is a right immediate child of `A`, i.e. `A` is a parent of `B` and `A.i == B.i - 1` _(not in Semgrex)_. |
|
||||
| `A >- B` <Tag variant="new">3.5.1</Tag> | `B` is a left immediate child of `A`, i.e. `A` is a parent of `B` and `A.i == B.i + 1` _(not in Semgrex)_. |
|
||||
| `A >++ B` | `B` is a right child of `A`, i.e. `A` is a parent of `B` and `A.i < B.i`. |
|
||||
| `A >-- B` | `B` is a left child of `A`, i.e. `A` is a parent of `B` and `A.i > B.i`. |
|
||||
| `A <+ B` <Tag variant="new">3.5.1</Tag> | `B` is a right immediate parent of `A`, i.e. `A` is a child of `B` and `A.i == B.i - 1` _(not in Semgrex)_. |
|
||||
| `A <- B` <Tag variant="new">3.5.1</Tag> | `B` is a left immediate parent of `A`, i.e. `A` is a child of `B` and `A.i == B.i + 1` _(not in Semgrex)_. |
|
||||
| `A <++ B` | `B` is a right parent of `A`, i.e. `A` is a child of `B` and `A.i < B.i`. |
|
||||
| `A <-- B` | `B` is a left parent of `A`, i.e. `A` is a child of `B` and `A.i > B.i`. |
|
||||
|
||||
## DependencyMatcher.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create a `DependencyMatcher`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.matcher import DependencyMatcher
|
||||
> matcher = DependencyMatcher(nlp.vocab)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The vocabulary object, which must be shared with the documents the matcher will operate on. ~~Vocab~~ |
|
||||
| _keyword-only_ | |
|
||||
| `validate` | Validate all patterns added to this matcher. ~~bool~~ |
|
||||
|
||||
## DependencyMatcher.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Find all tokens matching the supplied patterns on the `Doc` or `Span`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.matcher import DependencyMatcher
|
||||
>
|
||||
> matcher = DependencyMatcher(nlp.vocab)
|
||||
> pattern = [{"RIGHT_ID": "founded_id",
|
||||
> "RIGHT_ATTRS": {"ORTH": "founded"}}]
|
||||
> matcher.add("FOUNDED", [pattern])
|
||||
> doc = nlp("Bill Gates founded Microsoft.")
|
||||
> matches = matcher(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `doclike` | The `Doc` or `Span` to match over. ~~Union[Doc, Span]~~ |
|
||||
| **RETURNS** | A list of `(match_id, token_ids)` tuples, describing the matches. The `match_id` is the ID of the match pattern and `token_ids` is a list of token indices matched by the pattern, where the position of each token in the list corresponds to the position of the node specification in the pattern. ~~List[Tuple[int, List[int]]]~~ |
|
||||
|
||||
## DependencyMatcher.\_\_len\_\_ {id="len",tag="method"}
|
||||
|
||||
Get the number of rules added to the dependency matcher. Note that this only
|
||||
returns the number of rules (identical with the number of IDs), not the number
|
||||
of individual patterns.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> matcher = DependencyMatcher(nlp.vocab)
|
||||
> assert len(matcher) == 0
|
||||
> pattern = [{"RIGHT_ID": "founded_id",
|
||||
> "RIGHT_ATTRS": {"ORTH": "founded"}}]
|
||||
> matcher.add("FOUNDED", [pattern])
|
||||
> assert len(matcher) == 1
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The number of rules. ~~int~~ |
|
||||
|
||||
## DependencyMatcher.\_\_contains\_\_ {id="contains",tag="method"}
|
||||
|
||||
Check whether the matcher contains rules for a match ID.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> matcher = DependencyMatcher(nlp.vocab)
|
||||
> assert "FOUNDED" not in matcher
|
||||
> matcher.add("FOUNDED", [pattern])
|
||||
> assert "FOUNDED" in matcher
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------- |
|
||||
| `key` | The match ID. ~~str~~ |
|
||||
| **RETURNS** | Whether the matcher contains rules for this match ID. ~~bool~~ |
|
||||
|
||||
## DependencyMatcher.add {id="add",tag="method"}
|
||||
|
||||
Add a rule to the matcher, consisting of an ID key, one or more patterns, and an
|
||||
optional callback function to act on the matches. The callback function will
|
||||
receive the arguments `matcher`, `doc`, `i` and `matches`. If a pattern already
|
||||
exists for the given ID, the patterns will be extended. An `on_match` callback
|
||||
will be overwritten.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> def on_match(matcher, doc, id, matches):
|
||||
> print('Matched!', matches)
|
||||
>
|
||||
> matcher = DependencyMatcher(nlp.vocab)
|
||||
> matcher.add("FOUNDED", patterns, on_match=on_match)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `match_id` | An ID for the patterns. ~~str~~ |
|
||||
| `patterns` | A list of match patterns. A pattern consists of a list of dicts, where each dict describes a token in the tree. ~~List[List[Dict[str, Union[str, Dict]]]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `on_match` | Callback function to act on matches. Takes the arguments `matcher`, `doc`, `i` and `matches`. ~~Optional[Callable[[DependencyMatcher, Doc, int, List[Tuple], Any]]~~ |
|
||||
|
||||
## DependencyMatcher.get {id="get",tag="method"}
|
||||
|
||||
Retrieve the pattern stored for a key. Returns the rule as an
|
||||
`(on_match, patterns)` tuple containing the callback and available patterns.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> matcher.add("FOUNDED", patterns, on_match=on_match)
|
||||
> on_match, patterns = matcher.get("FOUNDED")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------------------------------------------------- |
|
||||
| `key` | The ID of the match rule. ~~str~~ |
|
||||
| **RETURNS** | The rule, as an `(on_match, patterns)` tuple. ~~Tuple[Optional[Callable], List[List[Union[Dict, Tuple]]]]~~ |
|
||||
|
||||
## DependencyMatcher.remove {id="remove",tag="method"}
|
||||
|
||||
Remove a rule from the dependency matcher. A `KeyError` is raised if the match
|
||||
ID does not exist.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> matcher.add("FOUNDED", patterns)
|
||||
> assert "FOUNDED" in matcher
|
||||
> matcher.remove("FOUNDED")
|
||||
> assert "FOUNDED" not in matcher
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----- | --------------------------------- |
|
||||
| `key` | The ID of the match rule. ~~str~~ |
|
||||
@@ -0,0 +1,469 @@
|
||||
---
|
||||
title: DependencyParser
|
||||
tag: class
|
||||
source: spacy/pipeline/dep_parser.pyx
|
||||
teaser: 'Pipeline component for syntactic dependency parsing'
|
||||
api_base_class: /api/pipe
|
||||
api_string_name: parser
|
||||
api_trainable: true
|
||||
---
|
||||
|
||||
A transition-based dependency parser component. The dependency parser jointly
|
||||
learns sentence segmentation and labelled dependency parsing, and can optionally
|
||||
learn to merge tokens that had been over-segmented by the tokenizer. The parser
|
||||
uses a variant of the **non-monotonic arc-eager transition-system** described by
|
||||
[Honnibal and Johnson (2014)](https://www.aclweb.org/anthology/D15-1162/), with
|
||||
the addition of a "break" transition to perform the sentence segmentation.
|
||||
[Nivre (2005)](https://www.aclweb.org/anthology/P05-1013/)'s **pseudo-projective
|
||||
dependency transformation** is used to allow the parser to predict
|
||||
non-projective parses.
|
||||
|
||||
The parser is trained using an **imitation learning objective**. It follows the
|
||||
actions predicted by the current weights, and at each state, determines which
|
||||
actions are compatible with the optimal parse that could be reached from the
|
||||
current state. The weights are updated such that the scores assigned to the set
|
||||
of optimal actions is increased, while scores assigned to other actions are
|
||||
decreased. Note that more than one action may be optimal for a given state.
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
Dependency predictions are assigned to the `Token.dep` and `Token.head` fields.
|
||||
Beside the dependencies themselves, the parser decides sentence boundaries,
|
||||
which are saved in `Token.is_sent_start` and accessible via `Doc.sents`.
|
||||
|
||||
| Location | Value |
|
||||
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `Token.dep` | The type of dependency relation (hash). ~~int~~ |
|
||||
| `Token.dep_` | The type of dependency relation. ~~str~~ |
|
||||
| `Token.head` | The syntactic parent, or "governor", of this token. ~~Token~~ |
|
||||
| `Token.is_sent_start` | A boolean value indicating whether the token starts a sentence. After the parser runs this will be `True` or `False` for all tokens. ~~bool~~ |
|
||||
| `Doc.sents` | An iterator over sentences in the `Doc`, determined by `Token.is_sent_start` values. ~~Iterator[Span]~~ |
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config). See the
|
||||
[model architectures](/api/architectures) documentation for details on the
|
||||
architectures and their arguments and hyperparameters.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.pipeline.dep_parser import DEFAULT_PARSER_MODEL
|
||||
> config = {
|
||||
> "moves": None,
|
||||
> "update_with_oracle_cut_size": 100,
|
||||
> "learn_tokens": False,
|
||||
> "min_action_freq": 30,
|
||||
> "model": DEFAULT_PARSER_MODEL,
|
||||
> }
|
||||
> nlp.add_pipe("parser", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `moves` | A list of transition names. Inferred from the data if not provided. Defaults to `None`. ~~Optional[TransitionSystem]~~ |
|
||||
| `update_with_oracle_cut_size` | During training, cut long sequences into shorter segments by creating intermediate states based on the gold-standard history. The model is not very sensitive to this parameter, so you usually won't need to change it. Defaults to `100`. ~~int~~ |
|
||||
| `learn_tokens` | Whether to learn to merge subtokens that are split relative to the gold standard. Experimental. Defaults to `False`. ~~bool~~ |
|
||||
| `min_action_freq` | The minimum frequency of labelled actions to retain. Rarer labelled actions have their label backed-off to "dep". While this primarily affects the label accuracy, it can also affect the attachment structure, as the labels are used to represent the pseudo-projectivity transformation. Defaults to `30`. ~~int~~ |
|
||||
| `model` | The [`Model`](https://thinc.ai/docs/api-model) powering the pipeline component. Defaults to [TransitionBasedParser](/api/architectures#TransitionBasedParser). ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/dep_parser.pyx
|
||||
```
|
||||
|
||||
## DependencyParser.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe with default model
|
||||
> parser = nlp.add_pipe("parser")
|
||||
>
|
||||
> # Construction via add_pipe with custom model
|
||||
> config = {"model": {"@architectures": "my_parser"}}
|
||||
> parser = nlp.add_pipe("parser", config=config)
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy.pipeline import DependencyParser
|
||||
> parser = DependencyParser(nlp.vocab, model)
|
||||
> ```
|
||||
|
||||
Create a new pipeline instance. In your application, you would normally use a
|
||||
shortcut for this and instantiate the component using its string name and
|
||||
[`nlp.add_pipe`](/api/language#add_pipe).
|
||||
|
||||
| Name | Description |
|
||||
| ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | The [`Model`](https://thinc.ai/docs/api-model) powering the pipeline component. ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
| `name` | String name of the component instance. Used to add entries to the `losses` during training. ~~str~~ |
|
||||
| `moves` | A list of transition names. Inferred from the data if not provided. ~~Optional[TransitionSystem]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `update_with_oracle_cut_size` | During training, cut long sequences into shorter segments by creating intermediate states based on the gold-standard history. The model is not very sensitive to this parameter, so you usually won't need to change it. Defaults to `100`. ~~int~~ |
|
||||
| `learn_tokens` | Whether to learn to merge subtokens that are split relative to the gold standard. Experimental. Defaults to `False`. ~~bool~~ |
|
||||
| `min_action_freq` | The minimum frequency of labelled actions to retain. Rarer labelled actions have their label backed-off to "dep". While this primarily affects the label accuracy, it can also affect the attachment structure, as the labels are used to represent the pseudo-projectivity transformation. ~~int~~ |
|
||||
| `scorer` | The scoring method. Defaults to [`Scorer.score_deps`](/api/scorer#score_deps) for the attribute `"dep"` ignoring the labels `p` and `punct` and [`Scorer.score_spans`](/api/scorer/#score_spans) for the attribute `"sents"`. ~~Optional[Callable]~~ |
|
||||
|
||||
## DependencyParser.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document. The document is modified in place, and returned.
|
||||
This usually happens under the hood when the `nlp` object is called on a text
|
||||
and all pipeline components are applied to the `Doc` in order. Both
|
||||
[`__call__`](/api/dependencyparser#call) and
|
||||
[`pipe`](/api/dependencyparser#pipe) delegate to the
|
||||
[`predict`](/api/dependencyparser#predict) and
|
||||
[`set_annotations`](/api/dependencyparser#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> parser = nlp.add_pipe("parser")
|
||||
> # This usually happens under the hood
|
||||
> processed = parser(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## DependencyParser.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order. Both [`__call__`](/api/dependencyparser#call) and
|
||||
[`pipe`](/api/dependencyparser#pipe) delegate to the
|
||||
[`predict`](/api/dependencyparser#predict) and
|
||||
[`set_annotations`](/api/dependencyparser#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> parser = nlp.add_pipe("parser")
|
||||
> for doc in parser.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `docs` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## DependencyParser.initialize {id="initialize",tag="method",version="3"}
|
||||
|
||||
Initialize the component for training. `get_examples` should be a function that
|
||||
returns an iterable of [`Example`](/api/example) objects. **At least one example
|
||||
should be supplied.** The data examples are used to **initialize the model** of
|
||||
the component and can either be the full training data or a representative
|
||||
sample. Initialization includes validating the network,
|
||||
[inferring missing shapes](https://thinc.ai/docs/usage-models#validation) and
|
||||
setting up the label scheme based on the data. This method is typically called
|
||||
by [`Language.initialize`](/api/language#initialize) and lets you customize
|
||||
arguments it receives via the
|
||||
[`[initialize.components]`](/api/data-formats#config-initialize) block in the
|
||||
config.
|
||||
|
||||
<Infobox variant="warning" title="Changed in v3.0" id="begin_training">
|
||||
|
||||
This method was previously called `begin_training`.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> parser = nlp.add_pipe("parser")
|
||||
> parser.initialize(lambda: examples, nlp=nlp)
|
||||
> ```
|
||||
>
|
||||
> ```ini
|
||||
> ### config.cfg
|
||||
> [initialize.components.parser]
|
||||
>
|
||||
> [initialize.components.parser.labels]
|
||||
> @readers = "spacy.read_labels.v1"
|
||||
> path = "corpus/labels/parser.json
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Must contain at least one `Example`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
| `labels` | The label information to add to the component, as provided by the [`label_data`](#label_data) property after initialization. To generate a reusable JSON file from your data, you should run the [`init labels`](/api/cli#init-labels) command. If no labels are provided, the `get_examples` callback is used to extract the labels from the data, which may be a lot slower. ~~Optional[Dict[str, Dict[str, int]]]~~ |
|
||||
|
||||
## DependencyParser.predict {id="predict",tag="method"}
|
||||
|
||||
Apply the component's model to a batch of [`Doc`](/api/doc) objects, without
|
||||
modifying them.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> parser = nlp.add_pipe("parser")
|
||||
> scores = parser.predict([doc1, doc2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------- |
|
||||
| `docs` | The documents to predict. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | A helper class for the parse state (internal). ~~StateClass~~ |
|
||||
|
||||
## DependencyParser.set_annotations {id="set_annotations",tag="method"}
|
||||
|
||||
Modify a batch of [`Doc`](/api/doc) objects, using pre-computed scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> parser = nlp.add_pipe("parser")
|
||||
> scores = parser.predict([doc1, doc2])
|
||||
> parser.set_annotations([doc1, doc2], scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | ------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `scores` | The scores to set, produced by `DependencyParser.predict`. Returns an internal helper class for the parse state. ~~List[StateClass]~~ |
|
||||
|
||||
## DependencyParser.update {id="update",tag="method"}
|
||||
|
||||
Learn from a batch of [`Example`](/api/example) objects, updating the pipe's
|
||||
model. Delegates to [`predict`](/api/dependencyparser#predict) and
|
||||
[`get_loss`](/api/dependencyparser#get_loss).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> parser = nlp.add_pipe("parser")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = parser.update(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## DependencyParser.get_loss {id="get_loss",tag="method"}
|
||||
|
||||
Find the loss and gradient of loss for the batch of documents and their
|
||||
predicted scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> parser = nlp.add_pipe("parser")
|
||||
> scores = parser.predict([eg.predicted for eg in examples])
|
||||
> loss, d_loss = parser.get_loss(examples, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------------------------- |
|
||||
| `examples` | The batch of examples. ~~Iterable[Example]~~ |
|
||||
| `scores` | Scores representing the model's predictions. ~~StateClass~~ |
|
||||
| **RETURNS** | The loss and the gradient, i.e. `(loss, gradient)`. ~~Tuple[float, float]~~ |
|
||||
|
||||
## DependencyParser.create_optimizer {id="create_optimizer",tag="method"}
|
||||
|
||||
Create an [`Optimizer`](https://thinc.ai/docs/api-optimizers) for the pipeline
|
||||
component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> parser = nlp.add_pipe("parser")
|
||||
> optimizer = parser.create_optimizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The optimizer. ~~Optimizer~~ |
|
||||
|
||||
## DependencyParser.use_params {id="use_params",tag="method, contextmanager"}
|
||||
|
||||
Modify the pipe's model, to use the given parameter values. At the end of the
|
||||
context, the original parameters are restored.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> parser = DependencyParser(nlp.vocab)
|
||||
> with parser.use_params(optimizer.averages):
|
||||
> parser.to_disk("/best_model")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------- |
|
||||
| `params` | The parameter values to use in the model. ~~dict~~ |
|
||||
|
||||
## DependencyParser.add_label {id="add_label",tag="method"}
|
||||
|
||||
Add a new label to the pipe. Note that you don't have to call this method if you
|
||||
provide a **representative data sample** to the [`initialize`](#initialize)
|
||||
method. In this case, all labels found in the sample will be automatically added
|
||||
to the model, and the output dimension will be
|
||||
[inferred](/usage/layers-architectures#thinc-shape-inference) automatically.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> parser = nlp.add_pipe("parser")
|
||||
> parser.add_label("MY_LABEL")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------- |
|
||||
| `label` | The label to add. ~~str~~ |
|
||||
| **RETURNS** | `0` if the label is already present, otherwise `1`. ~~int~~ |
|
||||
|
||||
## DependencyParser.set_output {id="set_output",tag="method"}
|
||||
|
||||
Change the output dimension of the component's model by calling the model's
|
||||
attribute `resize_output`. This is a function that takes the original model and
|
||||
the new output dimension `nO`, and changes the model in place. When resizing an
|
||||
already trained model, care should be taken to avoid the "catastrophic
|
||||
forgetting" problem.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> parser = nlp.add_pipe("parser")
|
||||
> parser.set_output(512)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---- | --------------------------------- |
|
||||
| `nO` | The new output dimension. ~~int~~ |
|
||||
|
||||
## DependencyParser.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> parser = nlp.add_pipe("parser")
|
||||
> parser.to_disk("/path/to/parser")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## DependencyParser.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> parser = nlp.add_pipe("parser")
|
||||
> parser.from_disk("/path/to/parser")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `DependencyParser` object. ~~DependencyParser~~ |
|
||||
|
||||
## DependencyParser.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> parser = nlp.add_pipe("parser")
|
||||
> parser_bytes = parser.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `DependencyParser` object. ~~bytes~~ |
|
||||
|
||||
## DependencyParser.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> parser_bytes = parser.to_bytes()
|
||||
> parser = nlp.add_pipe("parser")
|
||||
> parser.from_bytes(parser_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `DependencyParser` object. ~~DependencyParser~~ |
|
||||
|
||||
## DependencyParser.labels {id="labels",tag="property"}
|
||||
|
||||
The labels currently added to the component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> parser.add_label("MY_LABEL")
|
||||
> assert "MY_LABEL" in parser.labels
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------ |
|
||||
| **RETURNS** | The labels added to the component. ~~Tuple[str, ...]~~ |
|
||||
|
||||
## DependencyParser.label_data {id="label_data",tag="property",version="3"}
|
||||
|
||||
The labels currently added to the component and their internal meta information.
|
||||
This is the data generated by [`init labels`](/api/cli#init-labels) and used by
|
||||
[`DependencyParser.initialize`](/api/dependencyparser#initialize) to initialize
|
||||
the model with a pre-defined label set.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> labels = parser.label_data
|
||||
> parser.initialize(lambda: [], nlp=nlp, labels=labels)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------------------------- |
|
||||
| **RETURNS** | The label data added to the component. ~~Dict[str, Dict[str, Dict[str, int]]]~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = parser.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `cfg` | The config file. You usually don't want to exclude this. |
|
||||
| `model` | The binary model data. You usually don't want to exclude this. |
|
||||
@@ -0,0 +1,792 @@
|
||||
---
|
||||
title: Doc
|
||||
tag: class
|
||||
teaser: A container for accessing linguistic annotations.
|
||||
source: spacy/tokens/doc.pyx
|
||||
---
|
||||
|
||||
A `Doc` is a sequence of [`Token`](/api/token) objects. Access sentences and
|
||||
named entities, export annotations to numpy arrays, losslessly serialize to
|
||||
compressed binary strings. The `Doc` object holds an array of
|
||||
[`TokenC`](/api/cython-structs#tokenc) structs. The Python-level `Token` and
|
||||
[`Span`](/api/span) objects are views of this array, i.e. they don't own the
|
||||
data themselves.
|
||||
|
||||
## Doc.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Construct a `Doc` object. The most common way to get a `Doc` object is via the
|
||||
`nlp` object.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction 1
|
||||
> doc = nlp("Some text")
|
||||
>
|
||||
> # Construction 2
|
||||
> from spacy.tokens import Doc
|
||||
>
|
||||
> words = ["hello", "world", "!"]
|
||||
> spaces = [True, False, False]
|
||||
> doc = Doc(nlp.vocab, words=words, spaces=spaces)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | A storage container for lexical types. ~~Vocab~~ |
|
||||
| `words` | A list of strings or integer hash values to add to the document as words. ~~Optional[List[Union[str,int]]]~~ |
|
||||
| `spaces` | A list of boolean values indicating whether each word has a subsequent space. Must have the same length as `words`, if specified. Defaults to a sequence of `True`. ~~Optional[List[bool]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `user_data` | Optional extra data to attach to the Doc. ~~Dict~~ |
|
||||
| `tags` <Tag variant="new">3</Tag> | A list of strings, of the same length as `words`, to assign as `token.tag` for each word. Defaults to `None`. ~~Optional[List[str]]~~ |
|
||||
| `pos` <Tag variant="new">3</Tag> | A list of strings, of the same length as `words`, to assign as `token.pos` for each word. Defaults to `None`. ~~Optional[List[str]]~~ |
|
||||
| `morphs` <Tag variant="new">3</Tag> | A list of strings, of the same length as `words`, to assign as `token.morph` for each word. Defaults to `None`. ~~Optional[List[str]]~~ |
|
||||
| `lemmas` <Tag variant="new">3</Tag> | A list of strings, of the same length as `words`, to assign as `token.lemma` for each word. Defaults to `None`. ~~Optional[List[str]]~~ |
|
||||
| `heads` <Tag variant="new">3</Tag> | A list of values, of the same length as `words`, to assign as the head for each word. Head indices are the absolute position of the head in the `Doc`. Defaults to `None`. ~~Optional[List[int]]~~ |
|
||||
| `deps` <Tag variant="new">3</Tag> | A list of strings, of the same length as `words`, to assign as `token.dep` for each word. Defaults to `None`. ~~Optional[List[str]]~~ |
|
||||
| `sent_starts` <Tag variant="new">3</Tag> | A list of values, of the same length as `words`, to assign as `token.is_sent_start`. Will be overridden by heads if `heads` is provided. Defaults to `None`. ~~Optional[List[Union[bool, int, None]]]~~ |
|
||||
| `ents` <Tag variant="new">3</Tag> | A list of strings, of the same length of `words`, to assign the token-based IOB tag. Defaults to `None`. ~~Optional[List[str]]~~ |
|
||||
|
||||
## Doc.\_\_getitem\_\_ {id="getitem",tag="method"}
|
||||
|
||||
Get a [`Token`](/api/token) object at position `i`, where `i` is an integer.
|
||||
Negative indexing is supported, and follows the usual Python semantics, i.e.
|
||||
`doc[-2]` is `doc[len(doc) - 2]`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> assert doc[0].text == "Give"
|
||||
> assert doc[-1].text == "."
|
||||
> span = doc[1:3]
|
||||
> assert span.text == "it back"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `i` | The index of the token. ~~int~~ |
|
||||
| **RETURNS** | The token at `doc[i]`. ~~Token~~ |
|
||||
|
||||
Get a [`Span`](/api/span) object, starting at position `start` (token index) and
|
||||
ending at position `end` (token index). For instance, `doc[2:5]` produces a span
|
||||
consisting of tokens 2, 3 and 4. Stepped slices (e.g. `doc[start : end : step]`)
|
||||
are not supported, as `Span` objects must be contiguous (cannot have gaps). You
|
||||
can use negative indices and open-ended ranges, which have their normal Python
|
||||
semantics.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------- |
|
||||
| `start_end` | The slice of the document to get. ~~Tuple[int, int]~~ |
|
||||
| **RETURNS** | The span at `doc[start:end]`. ~~Span~~ |
|
||||
|
||||
## Doc.\_\_iter\_\_ {id="iter",tag="method"}
|
||||
|
||||
Iterate over `Token` objects, from which the annotations can be easily accessed.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back")
|
||||
> assert [t.text for t in doc] == ["Give", "it", "back"]
|
||||
> ```
|
||||
|
||||
This is the main way of accessing [`Token`](/api/token) objects, which are the
|
||||
main way annotations are accessed from Python. If faster-than-Python speeds are
|
||||
required, you can instead access the annotations as a numpy array, or access the
|
||||
underlying C data directly from Cython.
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | --------------------------- |
|
||||
| **YIELDS** | A `Token` object. ~~Token~~ |
|
||||
|
||||
## Doc.\_\_len\_\_ {id="len",tag="method"}
|
||||
|
||||
Get the number of tokens in the document.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> assert len(doc) == 7
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------- |
|
||||
| **RETURNS** | The number of tokens in the document. ~~int~~ |
|
||||
|
||||
## Doc.set_extension {id="set_extension",tag="classmethod",version="2"}
|
||||
|
||||
Define a custom attribute on the `Doc` which becomes available via `Doc._`. For
|
||||
details, see the documentation on
|
||||
[custom attributes](/usage/processing-pipelines#custom-components-attributes).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Doc
|
||||
> city_getter = lambda doc: any(city in doc.text for city in ("New York", "Paris", "Berlin"))
|
||||
> Doc.set_extension("has_city", getter=city_getter)
|
||||
> doc = nlp("I like New York")
|
||||
> assert doc._.has_city
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `name` | Name of the attribute to set by the extension. For example, `"my_attr"` will be available as `doc._.my_attr`. ~~str~~ |
|
||||
| `default` | Optional default value of the attribute if no getter or method is defined. ~~Optional[Any]~~ |
|
||||
| `method` | Set a custom method on the object, for example `doc._.compare(other_doc)`. ~~Optional[Callable[[Doc, ...], Any]]~~ |
|
||||
| `getter` | Getter function that takes the object and returns an attribute value. Is called when the user accesses the `._` attribute. ~~Optional[Callable[[Doc], Any]]~~ |
|
||||
| `setter` | Setter function that takes the `Doc` and a value, and modifies the object. Is called when the user writes to the `Doc._` attribute. ~~Optional[Callable[[Doc, Any], None]]~~ |
|
||||
| `force` | Force overwriting existing attribute. ~~bool~~ |
|
||||
|
||||
## Doc.get_extension {id="get_extension",tag="classmethod",version="2"}
|
||||
|
||||
Look up a previously registered extension by name. Returns a 4-tuple
|
||||
`(default, method, getter, setter)` if the extension is registered. Raises a
|
||||
`KeyError` otherwise.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Doc
|
||||
> Doc.set_extension("has_city", default=False)
|
||||
> extension = Doc.get_extension("has_city")
|
||||
> assert extension == (False, None, None, None)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `name` | Name of the extension. ~~str~~ |
|
||||
| **RETURNS** | A `(default, method, getter, setter)` tuple of the extension. ~~Tuple[Optional[Any], Optional[Callable], Optional[Callable], Optional[Callable]]~~ |
|
||||
|
||||
## Doc.has_extension {id="has_extension",tag="classmethod",version="2"}
|
||||
|
||||
Check whether an extension has been registered on the `Doc` class.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Doc
|
||||
> Doc.set_extension("has_city", default=False)
|
||||
> assert Doc.has_extension("has_city")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------- |
|
||||
| `name` | Name of the extension to check. ~~str~~ |
|
||||
| **RETURNS** | Whether the extension has been registered. ~~bool~~ |
|
||||
|
||||
## Doc.remove_extension {id="remove_extension",tag="classmethod",version="2.0.12"}
|
||||
|
||||
Remove a previously registered extension.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Doc
|
||||
> Doc.set_extension("has_city", default=False)
|
||||
> removed = Doc.remove_extension("has_city")
|
||||
> assert not Doc.has_extension("has_city")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `name` | Name of the extension. ~~str~~ |
|
||||
| **RETURNS** | A `(default, method, getter, setter)` tuple of the removed extension. ~~Tuple[Optional[Any], Optional[Callable], Optional[Callable], Optional[Callable]]~~ |
|
||||
|
||||
## Doc.char_span {id="char_span",tag="method",version="2"}
|
||||
|
||||
Create a `Span` object from the slice `doc.text[start_idx:end_idx]`. Returns
|
||||
`None` if the character indices don't map to a valid span using the default
|
||||
alignment mode `"strict".
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like New York")
|
||||
> span = doc.char_span(7, 15, label="GPE")
|
||||
> assert span.text == "New York"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `start` | The index of the first character of the span. ~~int~~ |
|
||||
| `end` | The index of the last character after the span. ~~int~~ |
|
||||
| `label` | A label to attach to the span, e.g. for named entities. ~~Union[int, str]~~ |
|
||||
| `kb_id` | An ID from a knowledge base to capture the meaning of a named entity. ~~Union[int, str]~~ |
|
||||
| `vector` | A meaning representation of the span. ~~numpy.ndarray[ndim=1, dtype=float32]~~ |
|
||||
| `alignment_mode` | How character indices snap to token boundaries. Options: `"strict"` (no snapping), `"contract"` (span of all tokens completely within the character span), `"expand"` (span of all tokens at least partially covered by the character span). Defaults to `"strict"`. ~~str~~ |
|
||||
| `span_id` <Tag variant="new">3.3.1</Tag> | An identifier to associate with the span. ~~Union[int, str]~~ |
|
||||
| **RETURNS** | The newly constructed object or `None`. ~~Optional[Span]~~ |
|
||||
|
||||
## Doc.set_ents {id="set_ents",tag="method",version="3"}
|
||||
|
||||
Set the named entities in the document.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Span
|
||||
> doc = nlp("Mr. Best flew to New York on Saturday morning.")
|
||||
> doc.set_ents([Span(doc, 0, 2, "PERSON")])
|
||||
> ents = list(doc.ents)
|
||||
> assert ents[0].label_ == "PERSON"
|
||||
> assert ents[0].text == "Mr. Best"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `entities` | Spans with labels to set as entities. ~~List[Span]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `blocked` | Spans to set as "blocked" (never an entity) for spacy's built-in NER component. Other components may ignore this setting. ~~Optional[List[Span]]~~ |
|
||||
| `missing` | Spans with missing/unknown entity information. ~~Optional[List[Span]]~~ |
|
||||
| `outside` | Spans outside of entities (O in IOB). ~~Optional[List[Span]]~~ |
|
||||
| `default` | How to set entity annotation for tokens outside of any provided spans. Options: `"blocked"`, `"missing"`, `"outside"` and `"unmodified"` (preserve current state). Defaults to `"outside"`. ~~str~~ |
|
||||
|
||||
## Doc.similarity {id="similarity",tag="method",model="vectors"}
|
||||
|
||||
Make a semantic similarity estimate. The default estimate is cosine similarity
|
||||
using an average of word vectors.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> apples = nlp("I like apples")
|
||||
> oranges = nlp("I like oranges")
|
||||
> apples_oranges = apples.similarity(oranges)
|
||||
> oranges_apples = oranges.similarity(apples)
|
||||
> assert apples_oranges == oranges_apples
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `other` | The object to compare with. By default, accepts `Doc`, `Span`, `Token` and `Lexeme` objects. ~~Union[Doc, Span, Token, Lexeme]~~ |
|
||||
| **RETURNS** | A scalar similarity score. Higher is more similar. ~~float~~ |
|
||||
|
||||
## Doc.count_by {id="count_by",tag="method"}
|
||||
|
||||
Count the frequencies of a given attribute. Produces a dict of
|
||||
`{attr (int): count (ints)}` frequencies, keyed by the values of the given
|
||||
attribute ID.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.attrs import ORTH
|
||||
> doc = nlp("apple apple orange banana")
|
||||
> assert doc.count_by(ORTH) == {7024: 1, 119552: 1, 2087: 2}
|
||||
> doc.to_array([ORTH])
|
||||
> # array([[11880], [11880], [7561], [12800]])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------------------- |
|
||||
| `attr_id` | The attribute ID. ~~int~~ |
|
||||
| **RETURNS** | A dictionary mapping attributes to integer counts. ~~Dict[int, int]~~ |
|
||||
|
||||
## Doc.get_lca_matrix {id="get_lca_matrix",tag="method"}
|
||||
|
||||
Calculates the lowest common ancestor matrix for a given `Doc`. Returns LCA
|
||||
matrix containing the integer index of the ancestor, or `-1` if no common
|
||||
ancestor is found, e.g. if span excludes a necessary ancestor.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a test")
|
||||
> matrix = doc.get_lca_matrix()
|
||||
> # array([[0, 1, 1, 1], [1, 1, 1, 1], [1, 1, 2, 3], [1, 1, 3, 3]], dtype=int32)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------- |
|
||||
| **RETURNS** | The lowest common ancestor matrix of the `Doc`. ~~numpy.ndarray[ndim=2, dtype=int32]~~ |
|
||||
|
||||
## Doc.has_annotation {id="has_annotation",tag="method"}
|
||||
|
||||
Check whether the doc contains annotation on a
|
||||
[`Token` attribute](/api/token#attributes).
|
||||
|
||||
<Infobox title="Changed in v3.0" variant="warning">
|
||||
|
||||
This method replaces the previous boolean attributes like `Doc.is_tagged`,
|
||||
`Doc.is_parsed` or `Doc.is_sentenced`.
|
||||
|
||||
```diff
|
||||
doc = nlp("This is a text")
|
||||
- assert doc.is_parsed
|
||||
+ assert doc.has_annotation("DEP")
|
||||
```
|
||||
|
||||
</Infobox>
|
||||
|
||||
| Name | Description |
|
||||
| ------------------ | --------------------------------------------------------------------------------------------------- |
|
||||
| `attr` | The attribute string name or int ID. ~~Union[int, str]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `require_complete` | Whether to check that the attribute is set on every token in the doc. Defaults to `False`. ~~bool~~ |
|
||||
| **RETURNS** | Whether specified annotation is present in the doc. ~~bool~~ |
|
||||
|
||||
## Doc.to_array {id="to_array",tag="method"}
|
||||
|
||||
Export given token attributes to a numpy `ndarray`. If `attr_ids` is a sequence
|
||||
of `M` attributes, the output array will be of shape `(N, M)`, where `N` is the
|
||||
length of the `Doc` (in tokens). If `attr_ids` is a single attribute, the output
|
||||
shape will be `(N,)`. You can specify attributes by integer ID (e.g.
|
||||
`spacy.attrs.LEMMA`) or string name (e.g. "LEMMA" or "lemma"). The values will
|
||||
be 64-bit integers.
|
||||
|
||||
Returns a 2D array with one row per token and one column per attribute (when
|
||||
`attr_ids` is a list), or as a 1D numpy array, with one item per attribute (when
|
||||
`attr_ids` is a single value).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.attrs import LOWER, POS, ENT_TYPE, IS_ALPHA
|
||||
> doc = nlp(text)
|
||||
> # All strings mapped to integers, for easy export to numpy
|
||||
> np_array = doc.to_array([LOWER, POS, ENT_TYPE, IS_ALPHA])
|
||||
> np_array = doc.to_array("POS")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `attr_ids` | A list of attributes (int IDs or string names) or a single attribute (int ID or string name). ~~Union[int, str, List[Union[int, str]]]~~ |
|
||||
| **RETURNS** | The exported attributes as a numpy array. ~~Union[numpy.ndarray[ndim=2, dtype=uint64], numpy.ndarray[ndim=1, dtype=uint64]]~~ |
|
||||
|
||||
## Doc.from_array {id="from_array",tag="method"}
|
||||
|
||||
Load attributes from a numpy array. Write to a `Doc` object, from an `(M, N)`
|
||||
array of attributes.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.attrs import LOWER, POS, ENT_TYPE, IS_ALPHA
|
||||
> from spacy.tokens import Doc
|
||||
> doc = nlp("Hello world!")
|
||||
> np_array = doc.to_array([LOWER, POS, ENT_TYPE, IS_ALPHA])
|
||||
> doc2 = Doc(doc.vocab, words=[t.text for t in doc])
|
||||
> doc2.from_array([LOWER, POS, ENT_TYPE, IS_ALPHA], np_array)
|
||||
> assert doc[0].pos_ == doc2[0].pos_
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------------------------------------- |
|
||||
| `attrs` | A list of attribute ID ints. ~~List[int]~~ |
|
||||
| `array` | The attribute values to load. ~~numpy.ndarray[ndim=2, dtype=int32]~~ |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `Doc` itself. ~~Doc~~ |
|
||||
|
||||
## Doc.from_docs {id="from_docs",tag="staticmethod",version="3"}
|
||||
|
||||
Concatenate multiple `Doc` objects to form a new one. Raises an error if the
|
||||
`Doc` objects do not all share the same `Vocab`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Doc
|
||||
> texts = ["London is the capital of the United Kingdom.",
|
||||
> "The River Thames flows through London.",
|
||||
> "The famous Tower Bridge crosses the River Thames."]
|
||||
> docs = list(nlp.pipe(texts))
|
||||
> c_doc = Doc.from_docs(docs)
|
||||
> assert str(c_doc) == " ".join(texts)
|
||||
> assert len(list(c_doc.sents)) == len(docs)
|
||||
> assert [str(ent) for ent in c_doc.ents] == \
|
||||
> [str(ent) for doc in docs for ent in doc.ents]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
|
||||
| `docs` | A list of `Doc` objects. ~~List[Doc]~~ |
|
||||
| `ensure_whitespace` | Insert a space between two adjacent docs whenever the first doc does not end in whitespace. ~~bool~~ |
|
||||
| `attrs` | Optional list of attribute ID ints or attribute name strings. ~~Optional[List[Union[str, int]]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` <Tag variant="new">3.3</Tag> | String names of Doc attributes to exclude. Supported: `spans`, `tensor`, `user_data`. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The new `Doc` object that is containing the other docs or `None`, if `docs` is empty or `None`. ~~Optional[Doc]~~ |
|
||||
|
||||
## Doc.to_disk {id="to_disk",tag="method",version="2"}
|
||||
|
||||
Save the current state to a directory.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc.to_disk("/path/to/doc")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## Doc.from_disk {id="from_disk",tag="method",version="2"}
|
||||
|
||||
Loads state from a directory. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Doc
|
||||
> from spacy.vocab import Vocab
|
||||
> doc = Doc(Vocab()).from_disk("/path/to/doc")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `Doc` object. ~~Doc~~ |
|
||||
|
||||
## Doc.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
Serialize, i.e. export the document contents to a binary string.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> doc_bytes = doc.to_bytes()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | A losslessly serialized copy of the `Doc`, including all annotations. ~~bytes~~ |
|
||||
|
||||
## Doc.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Deserialize, i.e. import the document contents from a binary string.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Doc
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> doc_bytes = doc.to_bytes()
|
||||
> doc2 = Doc(doc.vocab).from_bytes(doc_bytes)
|
||||
> assert doc.text == doc2.text
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `data` | The string to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `Doc` object. ~~Doc~~ |
|
||||
|
||||
## Doc.to_json {id="to_json",tag="method"}
|
||||
|
||||
Serializes a document to JSON. Note that this is format differs from the
|
||||
deprecated [`JSON training format`](/api/data-formats#json-input).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("All we have to decide is what to do with the time that is given us.")
|
||||
> assert doc.to_json()["text"] == doc.text
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `underscore` | Optional list of string names of custom `Doc` attributes. Attribute values need to be JSON-serializable. Values will be added to an `"_"` key in the data, e.g. `"_": {"foo": "bar"}`. ~~Optional[List[str]]~~ |
|
||||
| **RETURNS** | The data in JSON format. ~~Dict[str, Any]~~ |
|
||||
|
||||
## Doc.from_json {id="from_json",tag="method",version="3.3.1"}
|
||||
|
||||
Deserializes a document from JSON, i.e. generates a document from the provided
|
||||
JSON data as generated by [`Doc.to_json()`](/api/doc#to_json).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Doc
|
||||
> doc = nlp("All we have to decide is what to do with the time that is given us.")
|
||||
> doc_json = doc.to_json()
|
||||
> deserialized_doc = Doc(nlp.vocab).from_json(doc_json)
|
||||
> assert deserialized_doc.text == doc.text == doc_json["text"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------- |
|
||||
| `doc_json` | The Doc data in JSON format from [`Doc.to_json`](#to_json). ~~Dict[str, Any]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `validate` | Whether to validate the JSON input against the expected schema for detailed debugging. Defaults to `False`. ~~bool~~ |
|
||||
| **RETURNS** | A `Doc` corresponding to the provided JSON. ~~Doc~~ |
|
||||
|
||||
## Doc.retokenize {id="retokenize",tag="contextmanager",version="2.1"}
|
||||
|
||||
Context manager to handle retokenization of the `Doc`. Modifications to the
|
||||
`Doc`'s tokenization are stored, and then made all at once when the context
|
||||
manager exits. This is much more efficient, and less error-prone. All views of
|
||||
the `Doc` (`Span` and `Token`) created before the retokenization are
|
||||
invalidated, although they may accidentally continue to work.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Hello world!")
|
||||
> with doc.retokenize() as retokenizer:
|
||||
> retokenizer.merge(doc[0:2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| **RETURNS** | The retokenizer. ~~Retokenizer~~ |
|
||||
|
||||
### Retokenizer.merge {id="retokenizer.merge",tag="method"}
|
||||
|
||||
Mark a span for merging. The `attrs` will be applied to the resulting token (if
|
||||
they're context-dependent token attributes like `LEMMA` or `DEP`) or to the
|
||||
underlying lexeme (if they're context-independent lexical attributes like
|
||||
`LOWER` or `IS_STOP`). Writable custom extension attributes can be provided
|
||||
using the `"_"` key and specifying a dictionary that maps attribute names to
|
||||
values.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like David Bowie")
|
||||
> with doc.retokenize() as retokenizer:
|
||||
> attrs = {"LEMMA": "David Bowie"}
|
||||
> retokenizer.merge(doc[2:4], attrs=attrs)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | --------------------------------------------------------------------- |
|
||||
| `span` | The span to merge. ~~Span~~ |
|
||||
| `attrs` | Attributes to set on the merged token. ~~Dict[Union[str, int], Any]~~ |
|
||||
|
||||
### Retokenizer.split {id="retokenizer.split",tag="method"}
|
||||
|
||||
Mark a token for splitting, into the specified `orths`. The `heads` are required
|
||||
to specify how the new subtokens should be integrated into the dependency tree.
|
||||
The list of per-token heads can either be a token in the original document, e.g.
|
||||
`doc[2]`, or a tuple consisting of the token in the original document and its
|
||||
subtoken index. For example, `(doc[3], 1)` will attach the subtoken to the
|
||||
second subtoken of `doc[3]`.
|
||||
|
||||
This mechanism allows attaching subtokens to other newly created subtokens,
|
||||
without having to keep track of the changing token indices. If the specified
|
||||
head token will be split within the retokenizer block and no subtoken index is
|
||||
specified, it will default to `0`. Attributes to set on subtokens can be
|
||||
provided as a list of values. They'll be applied to the resulting token (if
|
||||
they're context-dependent token attributes like `LEMMA` or `DEP`) or to the
|
||||
underlying lexeme (if they're context-independent lexical attributes like
|
||||
`LOWER` or `IS_STOP`).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I live in NewYork")
|
||||
> with doc.retokenize() as retokenizer:
|
||||
> heads = [(doc[3], 1), doc[2]]
|
||||
> attrs = {"POS": ["PROPN", "PROPN"],
|
||||
> "DEP": ["pobj", "compound"]}
|
||||
> retokenizer.split(doc[3], ["New", "York"], heads=heads, attrs=attrs)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `token` | The token to split. ~~Token~~ |
|
||||
| `orths` | The verbatim text of the split tokens. Needs to match the text of the original token. ~~List[str]~~ |
|
||||
| `heads` | List of `token` or `(token, subtoken)` tuples specifying the tokens to attach the newly split subtokens to. ~~List[Union[Token, Tuple[Token, int]]]~~ |
|
||||
| `attrs` | Attributes to set on all split tokens. Attribute names mapped to list of per-token attribute values. ~~Dict[Union[str, int], List[Any]]~~ |
|
||||
|
||||
## Doc.ents {id="ents",tag="property",model="NER"}
|
||||
|
||||
The named entities in the document. Returns a tuple of named entity `Span`
|
||||
objects, if the entity recognizer has been applied.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Mr. Best flew to New York on Saturday morning.")
|
||||
> ents = list(doc.ents)
|
||||
> assert ents[0].label_ == "PERSON"
|
||||
> assert ents[0].text == "Mr. Best"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------- |
|
||||
| **RETURNS** | Entities in the document, one `Span` per entity. ~~Tuple[Span]~~ |
|
||||
|
||||
## Doc.spans {id="spans",tag="property"}
|
||||
|
||||
A dictionary of named span groups, to store and access additional span
|
||||
annotations. You can write to it by assigning a list of [`Span`](/api/span)
|
||||
objects or a [`SpanGroup`](/api/spangroup) to a given key.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Their goi ng home")
|
||||
> doc.spans["errors"] = [doc[0:1], doc[1:3]]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------------ |
|
||||
| **RETURNS** | The span groups assigned to the document. ~~Dict[str, SpanGroup]~~ |
|
||||
|
||||
## Doc.cats {id="cats",tag="property",model="text classifier"}
|
||||
|
||||
Maps a label to a score for categories applied to the document. Typically set by
|
||||
the [`TextCategorizer`](/api/textcategorizer).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a text about football.")
|
||||
> print(doc.cats)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------- |
|
||||
| **RETURNS** | The text categories mapped to scores. ~~Dict[str, float]~~ |
|
||||
|
||||
## Doc.noun_chunks {id="noun_chunks",tag="property",model="parser"}
|
||||
|
||||
Iterate over the base noun phrases in the document. Yields base noun-phrase
|
||||
`Span` objects, if the document has been syntactically parsed. A base noun
|
||||
phrase, or "NP chunk", is a noun phrase that does not permit other NPs to be
|
||||
nested within it – so no NP-level coordination, no prepositional phrases, and no
|
||||
relative clauses.
|
||||
|
||||
To customize the noun chunk iterator in a loaded pipeline, modify
|
||||
[`nlp.vocab.get_noun_chunks`](/api/vocab#attributes). If the `noun_chunk`
|
||||
[syntax iterator](/usage/linguistic-features#language-data) has not been
|
||||
implemented for the given language, a `NotImplementedError` is raised.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("A phrase with another phrase occurs.")
|
||||
> chunks = list(doc.noun_chunks)
|
||||
> assert len(chunks) == 2
|
||||
> assert chunks[0].text == "A phrase"
|
||||
> assert chunks[1].text == "another phrase"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ------------------------------------- |
|
||||
| **YIELDS** | Noun chunks in the document. ~~Span~~ |
|
||||
|
||||
## Doc.sents {id="sents",tag="property",model="sentences"}
|
||||
|
||||
Iterate over the sentences in the document. Sentence spans have no label.
|
||||
|
||||
This property is only available when
|
||||
[sentence boundaries](/usage/linguistic-features#sbd) have been set on the
|
||||
document by the `parser`, `senter`, `sentencizer` or some custom function. It
|
||||
will raise an error otherwise.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence. Here's another...")
|
||||
> sents = list(doc.sents)
|
||||
> assert len(sents) == 2
|
||||
> assert [s.root.text for s in sents] == ["is", "'s"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ----------------------------------- |
|
||||
| **YIELDS** | Sentences in the document. ~~Span~~ |
|
||||
|
||||
## Doc.has_vector {id="has_vector",tag="property",model="vectors"}
|
||||
|
||||
A boolean value indicating whether a word vector is associated with the object.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like apples")
|
||||
> assert doc.has_vector
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------- |
|
||||
| **RETURNS** | Whether the document has a vector data attached. ~~bool~~ |
|
||||
|
||||
## Doc.vector {id="vector",tag="property",model="vectors"}
|
||||
|
||||
A real-valued meaning representation. Defaults to an average of the token
|
||||
vectors.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like apples")
|
||||
> assert doc.vector.dtype == "float32"
|
||||
> assert doc.vector.shape == (300,)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------------------- |
|
||||
| **RETURNS** | A 1-dimensional array representing the document's vector. ~~numpy.ndarray[ndim=1, dtype=float32]~~ |
|
||||
|
||||
## Doc.vector_norm {id="vector_norm",tag="property",model="vectors"}
|
||||
|
||||
The L2 norm of the document's vector representation.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc1 = nlp("I like apples")
|
||||
> doc2 = nlp("I like oranges")
|
||||
> doc1.vector_norm # 4.54232424414368
|
||||
> doc2.vector_norm # 3.304373298575751
|
||||
> assert doc1.vector_norm != doc2.vector_norm
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------- |
|
||||
| **RETURNS** | The L2 norm of the vector representation. ~~float~~ |
|
||||
|
||||
## Attributes {id="attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `text` | A string representation of the document text. ~~str~~ |
|
||||
| `text_with_ws` | An alias of `Doc.text`, provided for duck-type compatibility with `Span` and `Token`. ~~str~~ |
|
||||
| `mem` | The document's local memory heap, for all C data it owns. ~~cymem.Pool~~ |
|
||||
| `vocab` | The store of lexical types. ~~Vocab~~ |
|
||||
| `tensor` | Container for dense vector representations. ~~numpy.ndarray~~ |
|
||||
| `user_data` | A generic storage area, for user custom data. ~~Dict[str, Any]~~ |
|
||||
| `lang` | Language of the document's vocabulary. ~~int~~ |
|
||||
| `lang_` | Language of the document's vocabulary. ~~str~~ |
|
||||
| `sentiment` | The document's positivity/negativity score, if available. ~~float~~ |
|
||||
| `user_hooks` | A dictionary that allows customization of the `Doc`'s properties. ~~Dict[str, Callable]~~ |
|
||||
| `user_token_hooks` | A dictionary that allows customization of properties of `Token` children. ~~Dict[str, Callable]~~ |
|
||||
| `user_span_hooks` | A dictionary that allows customization of properties of `Span` children. ~~Dict[str, Callable]~~ |
|
||||
| `has_unknown_spaces` | Whether the document was constructed without known spacing between tokens (typically when created from gold tokenization). ~~bool~~ |
|
||||
| `_` | User space for adding custom [attribute extensions](/usage/processing-pipelines#custom-components-attributes). ~~Underscore~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = doc.to_bytes(exclude=["text", "tensor"])
|
||||
> doc.from_disk("./doc.bin", exclude=["user_data"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------------ | --------------------------------------------- |
|
||||
| `text` | The value of the `Doc.text` attribute. |
|
||||
| `sentiment` | The value of the `Doc.sentiment` attribute. |
|
||||
| `tensor` | The value of the `Doc.tensor` attribute. |
|
||||
| `user_data` | The value of the `Doc.user_data` dictionary. |
|
||||
| `user_data_keys` | The keys of the `Doc.user_data` dictionary. |
|
||||
| `user_data_values` | The values of the `Doc.user_data` dictionary. |
|
||||
@@ -0,0 +1,183 @@
|
||||
---
|
||||
title: DocBin
|
||||
tag: class
|
||||
version: 2.2
|
||||
teaser: Pack Doc objects for binary serialization
|
||||
source: spacy/tokens/_serialize.py
|
||||
---
|
||||
|
||||
The `DocBin` class lets you efficiently serialize the information from a
|
||||
collection of `Doc` objects. You can control which information is serialized by
|
||||
passing a list of attribute IDs, and optionally also specify whether the user
|
||||
data is serialized. The `DocBin` is faster and produces smaller data sizes than
|
||||
pickle, and allows you to deserialize without executing arbitrary Python code. A
|
||||
notable downside to this format is that you can't easily extract just one
|
||||
document from the `DocBin`. The serialization format is gzipped msgpack, where
|
||||
the msgpack object has the following structure:
|
||||
|
||||
```python {title="msgpack object structure"}
|
||||
{
|
||||
"version": str, # DocBin version number
|
||||
"attrs": List[uint64], # e.g. [TAG, HEAD, ENT_IOB, ENT_TYPE]
|
||||
"tokens": bytes, # Serialized numpy uint64 array with the token data
|
||||
"spaces": bytes, # Serialized numpy boolean array with spaces data
|
||||
"lengths": bytes, # Serialized numpy int32 array with the doc lengths
|
||||
"strings": List[str] # List of unique strings in the token data
|
||||
}
|
||||
```
|
||||
|
||||
Strings for the words, tags, labels etc are represented by 64-bit hashes in the
|
||||
token data, and every string that occurs at least once is passed via the strings
|
||||
object. This means the storage is more efficient if you pack more documents
|
||||
together, because you have less duplication in the strings. For usage examples,
|
||||
see the docs on [serializing `Doc` objects](/usage/saving-loading#docs).
|
||||
|
||||
## DocBin.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create a `DocBin` object to hold serialized annotations.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import DocBin
|
||||
> doc_bin = DocBin(attrs=["ENT_IOB", "ENT_TYPE"])
|
||||
> ```
|
||||
|
||||
| Argument | Description |
|
||||
| ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `attrs` | List of attributes to serialize. `ORTH` (hash of token text) and `SPACY` (whether the token is followed by whitespace) are always serialized, so they're not required. Defaults to `("ORTH", "TAG", "HEAD", "DEP", "ENT_IOB", "ENT_TYPE", "ENT_KB_ID", "LEMMA", "MORPH", "POS")`. ~~Iterable[str]~~ |
|
||||
| `store_user_data` | Whether to write the `Doc.user_data` and the values of custom extension attributes to file/bytes. Defaults to `False`. ~~bool~~ |
|
||||
| `docs` | `Doc` objects to add on initialization. ~~Iterable[Doc]~~ |
|
||||
|
||||
## DocBin.\_\_len\_\_ {id="len",tag="method"}
|
||||
|
||||
Get the number of `Doc` objects that were added to the `DocBin`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc_bin = DocBin(attrs=["LEMMA"])
|
||||
> doc = nlp("This is a document to serialize.")
|
||||
> doc_bin.add(doc)
|
||||
> assert len(doc_bin) == 1
|
||||
> ```
|
||||
|
||||
| Argument | Description |
|
||||
| ----------- | --------------------------------------------------- |
|
||||
| **RETURNS** | The number of `Doc`s added to the `DocBin`. ~~int~~ |
|
||||
|
||||
## DocBin.add {id="add",tag="method"}
|
||||
|
||||
Add a `Doc`'s annotations to the `DocBin` for serialization.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc_bin = DocBin(attrs=["LEMMA"])
|
||||
> doc = nlp("This is a document to serialize.")
|
||||
> doc_bin.add(doc)
|
||||
> ```
|
||||
|
||||
| Argument | Description |
|
||||
| -------- | -------------------------------- |
|
||||
| `doc` | The `Doc` object to add. ~~Doc~~ |
|
||||
|
||||
## DocBin.get_docs {id="get_docs",tag="method"}
|
||||
|
||||
Recover `Doc` objects from the annotations, using the given vocab.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> docs = list(doc_bin.get_docs(nlp.vocab))
|
||||
> ```
|
||||
|
||||
| Argument | Description |
|
||||
| ---------- | --------------------------- |
|
||||
| `vocab` | The shared vocab. ~~Vocab~~ |
|
||||
| **YIELDS** | The `Doc` objects. ~~Doc~~ |
|
||||
|
||||
## DocBin.merge {id="merge",tag="method"}
|
||||
|
||||
Extend the annotations of this `DocBin` with the annotations from another. Will
|
||||
raise an error if the pre-defined `attrs` of the two `DocBin`s don't match.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc_bin1 = DocBin(attrs=["LEMMA", "POS"])
|
||||
> doc_bin1.add(nlp("Hello world"))
|
||||
> doc_bin2 = DocBin(attrs=["LEMMA", "POS"])
|
||||
> doc_bin2.add(nlp("This is a sentence"))
|
||||
> doc_bin1.merge(doc_bin2)
|
||||
> assert len(doc_bin1) == 2
|
||||
> ```
|
||||
|
||||
| Argument | Description |
|
||||
| -------- | ------------------------------------------------------ |
|
||||
| `other` | The `DocBin` to merge into the current bin. ~~DocBin~~ |
|
||||
|
||||
## DocBin.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
Serialize the `DocBin`'s annotations to a bytestring.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> docs = [nlp("Hello world!")]
|
||||
> doc_bin = DocBin(docs=docs)
|
||||
> doc_bin_bytes = doc_bin.to_bytes()
|
||||
> ```
|
||||
|
||||
| Argument | Description |
|
||||
| ----------- | ---------------------------------- |
|
||||
| **RETURNS** | The serialized `DocBin`. ~~bytes~~ |
|
||||
|
||||
## DocBin.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Deserialize the `DocBin`'s annotations from a bytestring.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc_bin_bytes = doc_bin.to_bytes()
|
||||
> new_doc_bin = DocBin().from_bytes(doc_bin_bytes)
|
||||
> ```
|
||||
|
||||
| Argument | Description |
|
||||
| ------------ | -------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| **RETURNS** | The loaded `DocBin`. ~~DocBin~~ |
|
||||
|
||||
## DocBin.to_disk {id="to_disk",tag="method",version="3"}
|
||||
|
||||
Save the serialized `DocBin` to a file. Typically uses the `.spacy` extension
|
||||
and the result can be used as the input data for
|
||||
[`spacy train`](/api/cli#train).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> docs = [nlp("Hello world!")]
|
||||
> doc_bin = DocBin(docs=docs)
|
||||
> doc_bin.to_disk("./data.spacy")
|
||||
> ```
|
||||
|
||||
| Argument | Description |
|
||||
| -------- | -------------------------------------------------------------------------- |
|
||||
| `path` | The file path, typically with the `.spacy` extension. ~~Union[str, Path]~~ |
|
||||
|
||||
## DocBin.from_disk {id="from_disk",tag="method",version="3"}
|
||||
|
||||
Load a serialized `DocBin` from a file. Typically uses the `.spacy` extension.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc_bin = DocBin().from_disk("./data.spacy")
|
||||
> ```
|
||||
|
||||
| Argument | Description |
|
||||
| ----------- | -------------------------------------------------------------------------- |
|
||||
| `path` | The file path, typically with the `.spacy` extension. ~~Union[str, Path]~~ |
|
||||
| **RETURNS** | The loaded `DocBin`. ~~DocBin~~ |
|
||||
@@ -0,0 +1,409 @@
|
||||
---
|
||||
title: EditTreeLemmatizer
|
||||
tag: class
|
||||
source: spacy/pipeline/edit_tree_lemmatizer.py
|
||||
version: 3.3
|
||||
teaser: 'Pipeline component for lemmatization'
|
||||
api_base_class: /api/pipe
|
||||
api_string_name: trainable_lemmatizer
|
||||
api_trainable: true
|
||||
---
|
||||
|
||||
A trainable component for assigning base forms to tokens. This lemmatizer uses
|
||||
**edit trees** to transform tokens into base forms. The lemmatization model
|
||||
predicts which edit tree is applicable to a token. The edit tree data structure
|
||||
and construction method used by this lemmatizer were proposed in
|
||||
[Joint Lemmatization and Morphological Tagging with Lemming](https://aclanthology.org/D15-1272.pdf)
|
||||
(Thomas Müller et al., 2015).
|
||||
|
||||
For a lookup and rule-based lemmatizer, see [`Lemmatizer`](/api/lemmatizer).
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
Predictions are assigned to `Token.lemma`.
|
||||
|
||||
| Location | Value |
|
||||
| -------------- | ------------------------- |
|
||||
| `Token.lemma` | The lemma (hash). ~~int~~ |
|
||||
| `Token.lemma_` | The lemma. ~~str~~ |
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config). See the
|
||||
[model architectures](/api/architectures) documentation for details on the
|
||||
architectures and their arguments and hyperparameters.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.pipeline.edit_tree_lemmatizer import DEFAULT_EDIT_TREE_LEMMATIZER_MODEL
|
||||
> config = {"model": DEFAULT_EDIT_TREE_LEMMATIZER_MODEL}
|
||||
> nlp.add_pipe("trainable_lemmatizer", config=config, name="lemmatizer")
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `model` | A model instance that predicts the edit tree probabilities. The output vectors should match the number of edit trees in size, and be normalized as probabilities (all scores between 0 and 1, with the rows summing to `1`). Defaults to [Tagger](/api/architectures#Tagger). ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
| `backoff` | ~~Token~~ attribute to use when no applicable edit tree is found. Defaults to `orth`. ~~str~~ |
|
||||
| `min_tree_freq` | Minimum frequency of an edit tree in the training set to be used. Defaults to `3`. ~~int~~ |
|
||||
| `overwrite` | Whether existing annotation is overwritten. Defaults to `False`. ~~bool~~ |
|
||||
| `top_k` | The number of most probable edit trees to try before resorting to `backoff`. Defaults to `1`. ~~int~~ |
|
||||
| `scorer` | The scoring method. Defaults to [`Scorer.score_token_attr`](/api/scorer#score_token_attr) for the attribute `"lemma"`. ~~Optional[Callable]~~ |
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/edit_tree_lemmatizer.py
|
||||
```
|
||||
|
||||
## EditTreeLemmatizer.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe with default model
|
||||
> lemmatizer = nlp.add_pipe("trainable_lemmatizer", name="lemmatizer")
|
||||
>
|
||||
> # Construction via create_pipe with custom model
|
||||
> config = {"model": {"@architectures": "my_tagger"}}
|
||||
> lemmatizer = nlp.add_pipe("trainable_lemmatizer", config=config, name="lemmatizer")
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy.pipeline import EditTreeLemmatizer
|
||||
> lemmatizer = EditTreeLemmatizer(nlp.vocab, model)
|
||||
> ```
|
||||
|
||||
Create a new pipeline instance. In your application, you would normally use a
|
||||
shortcut for this and instantiate the component using its string name and
|
||||
[`nlp.add_pipe`](/api/language#add_pipe).
|
||||
|
||||
| Name | Description |
|
||||
| --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | A model instance that predicts the edit tree probabilities. The output vectors should match the number of edit trees in size, and be normalized as probabilities (all scores between 0 and 1, with the rows summing to `1`). ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
| `name` | String name of the component instance. Used to add entries to the `losses` during training. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `backoff` | ~~Token~~ attribute to use when no applicable edit tree is found. Defaults to `orth`. ~~str~~ |
|
||||
| `min_tree_freq` | Minimum frequency of an edit tree in the training set to be used. Defaults to `3`. ~~int~~ |
|
||||
| `overwrite` | Whether existing annotation is overwritten. Defaults to `False`. ~~bool~~ |
|
||||
| `top_k` | The number of most probable edit trees to try before resorting to `backoff`. Defaults to `1`. ~~int~~ |
|
||||
| `scorer` | The scoring method. Defaults to [`Scorer.score_token_attr`](/api/scorer#score_token_attr) for the attribute `"lemma"`. ~~Optional[Callable]~~ |
|
||||
|
||||
## EditTreeLemmatizer.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document. The document is modified in place, and returned.
|
||||
This usually happens under the hood when the `nlp` object is called on a text
|
||||
and all pipeline components are applied to the `Doc` in order. Both
|
||||
[`__call__`](/api/edittreelemmatizer#call) and
|
||||
[`pipe`](/api/edittreelemmatizer#pipe) delegate to the
|
||||
[`predict`](/api/edittreelemmatizer#predict) and
|
||||
[`set_annotations`](/api/edittreelemmatizer#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> lemmatizer = nlp.add_pipe("trainable_lemmatizer", name="lemmatizer")
|
||||
> # This usually happens under the hood
|
||||
> processed = lemmatizer(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## EditTreeLemmatizer.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order. Both [`__call__`](/api/edittreelemmatizer#call)
|
||||
and [`pipe`](/api/edittreelemmatizer#pipe) delegate to the
|
||||
[`predict`](/api/edittreelemmatizer#predict) and
|
||||
[`set_annotations`](/api/edittreelemmatizer#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer = nlp.add_pipe("trainable_lemmatizer", name="lemmatizer")
|
||||
> for doc in lemmatizer.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `stream` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## EditTreeLemmatizer.initialize {id="initialize",tag="method",version="3"}
|
||||
|
||||
Initialize the component for training. `get_examples` should be a function that
|
||||
returns an iterable of [`Example`](/api/example) objects. **At least one example
|
||||
should be supplied.** The data examples are used to **initialize the model** of
|
||||
the component and can either be the full training data or a representative
|
||||
sample. Initialization includes validating the network,
|
||||
[inferring missing shapes](https://thinc.ai/docs/usage-models#validation) and
|
||||
setting up the label scheme based on the data. This method is typically called
|
||||
by [`Language.initialize`](/api/language#initialize) and lets you customize
|
||||
arguments it receives via the
|
||||
[`[initialize.components]`](/api/data-formats#config-initialize) block in the
|
||||
config.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer = nlp.add_pipe("trainable_lemmatizer", name="lemmatizer")
|
||||
> lemmatizer.initialize(lambda: examples, nlp=nlp)
|
||||
> ```
|
||||
>
|
||||
> ```ini
|
||||
> ### config.cfg
|
||||
> [initialize.components.lemmatizer]
|
||||
>
|
||||
> [initialize.components.lemmatizer.labels]
|
||||
> @readers = "spacy.read_labels.v1"
|
||||
> path = "corpus/labels/lemmatizer.json
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Must contain at least one `Example`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
| `labels` | The label information to add to the component, as provided by the [`label_data`](#label_data) property after initialization. To generate a reusable JSON file from your data, you should run the [`init labels`](/api/cli#init-labels) command. If no labels are provided, the `get_examples` callback is used to extract the labels from the data, which may be a lot slower. ~~Optional[Iterable[str]]~~ |
|
||||
|
||||
## EditTreeLemmatizer.predict {id="predict",tag="method"}
|
||||
|
||||
Apply the component's model to a batch of [`Doc`](/api/doc) objects, without
|
||||
modifying them.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer = nlp.add_pipe("trainable_lemmatizer", name="lemmatizer")
|
||||
> tree_ids = lemmatizer.predict([doc1, doc2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| `docs` | The documents to predict. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | The model's prediction for each document. |
|
||||
|
||||
## EditTreeLemmatizer.set_annotations {id="set_annotations",tag="method"}
|
||||
|
||||
Modify a batch of [`Doc`](/api/doc) objects, using pre-computed tree
|
||||
identifiers.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer = nlp.add_pipe("trainable_lemmatizer", name="lemmatizer")
|
||||
> tree_ids = lemmatizer.predict([doc1, doc2])
|
||||
> lemmatizer.set_annotations([doc1, doc2], tree_ids)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ------------------------------------------------------------------------------------- |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `tree_ids` | The identifiers of the edit trees to apply, produced by `EditTreeLemmatizer.predict`. |
|
||||
|
||||
## EditTreeLemmatizer.update {id="update",tag="method"}
|
||||
|
||||
Learn from a batch of [`Example`](/api/example) objects containing the
|
||||
predictions and gold-standard annotations, and update the component's model.
|
||||
Delegates to [`predict`](/api/edittreelemmatizer#predict) and
|
||||
[`get_loss`](/api/edittreelemmatizer#get_loss).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer = nlp.add_pipe("trainable_lemmatizer", name="lemmatizer")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = lemmatizer.update(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## EditTreeLemmatizer.get_loss {id="get_loss",tag="method"}
|
||||
|
||||
Find the loss and gradient of loss for the batch of documents and their
|
||||
predicted scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer = nlp.add_pipe("trainable_lemmatizer", name="lemmatizer")
|
||||
> scores = lemmatizer.model.begin_update([eg.predicted for eg in examples])
|
||||
> loss, d_loss = lemmatizer.get_loss(examples, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------------------------- |
|
||||
| `examples` | The batch of examples. ~~Iterable[Example]~~ |
|
||||
| `scores` | Scores representing the model's predictions. |
|
||||
| **RETURNS** | The loss and the gradient, i.e. `(loss, gradient)`. ~~Tuple[float, float]~~ |
|
||||
|
||||
## EditTreeLemmatizer.create_optimizer {id="create_optimizer",tag="method"}
|
||||
|
||||
Create an optimizer for the pipeline component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer = nlp.add_pipe("trainable_lemmatizer", name="lemmatizer")
|
||||
> optimizer = lemmatizer.create_optimizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The optimizer. ~~Optimizer~~ |
|
||||
|
||||
## EditTreeLemmatizer.use_params {id="use_params",tag="method, contextmanager"}
|
||||
|
||||
Modify the pipe's model, to use the given parameter values. At the end of the
|
||||
context, the original parameters are restored.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer = nlp.add_pipe("trainable_lemmatizer", name="lemmatizer")
|
||||
> with lemmatizer.use_params(optimizer.averages):
|
||||
> lemmatizer.to_disk("/best_model")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------- |
|
||||
| `params` | The parameter values to use in the model. ~~dict~~ |
|
||||
|
||||
## EditTreeLemmatizer.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer = nlp.add_pipe("trainable_lemmatizer", name="lemmatizer")
|
||||
> lemmatizer.to_disk("/path/to/lemmatizer")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## EditTreeLemmatizer.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer = nlp.add_pipe("trainable_lemmatizer", name="lemmatizer")
|
||||
> lemmatizer.from_disk("/path/to/lemmatizer")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `EditTreeLemmatizer` object. ~~EditTreeLemmatizer~~ |
|
||||
|
||||
## EditTreeLemmatizer.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer = nlp.add_pipe("trainable_lemmatizer", name="lemmatizer")
|
||||
> lemmatizer_bytes = lemmatizer.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `EditTreeLemmatizer` object. ~~bytes~~ |
|
||||
|
||||
## EditTreeLemmatizer.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer_bytes = lemmatizer.to_bytes()
|
||||
> lemmatizer = nlp.add_pipe("trainable_lemmatizer", name="lemmatizer")
|
||||
> lemmatizer.from_bytes(lemmatizer_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `EditTreeLemmatizer` object. ~~EditTreeLemmatizer~~ |
|
||||
|
||||
## EditTreeLemmatizer.labels {id="labels",tag="property"}
|
||||
|
||||
The labels currently added to the component.
|
||||
|
||||
<Infobox variant="warning" title="Interpretability of the labels">
|
||||
|
||||
The `EditTreeLemmatizer` labels are not useful by themselves, since they are
|
||||
identifiers of edit trees.
|
||||
|
||||
</Infobox>
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------ |
|
||||
| **RETURNS** | The labels added to the component. ~~Tuple[str, ...]~~ |
|
||||
|
||||
## EditTreeLemmatizer.label_data {id="label_data",tag="property",version="3"}
|
||||
|
||||
The labels currently added to the component and their internal meta information.
|
||||
This is the data generated by [`init labels`](/api/cli#init-labels) and used by
|
||||
[`EditTreeLemmatizer.initialize`](/api/edittreelemmatizer#initialize) to
|
||||
initialize the model with a pre-defined label set.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> labels = lemmatizer.label_data
|
||||
> lemmatizer.initialize(lambda: [], nlp=nlp, labels=labels)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------- |
|
||||
| **RETURNS** | The label data added to the component. ~~Tuple[str, ...]~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = lemmatizer.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `cfg` | The config file. You usually don't want to exclude this. |
|
||||
| `model` | The binary model data. You usually don't want to exclude this. |
|
||||
| `trees` | The edit trees. You usually don't want to exclude this. |
|
||||
@@ -0,0 +1,403 @@
|
||||
---
|
||||
title: EntityLinker
|
||||
tag: class
|
||||
source: spacy/pipeline/entity_linker.py
|
||||
version: 2.2
|
||||
teaser: 'Pipeline component for named entity linking and disambiguation'
|
||||
api_base_class: /api/pipe
|
||||
api_string_name: entity_linker
|
||||
api_trainable: true
|
||||
---
|
||||
|
||||
An `EntityLinker` component disambiguates textual mentions (tagged as named
|
||||
entities) to unique identifiers, grounding the named entities into the "real
|
||||
world". It requires a `KnowledgeBase`, as well as a function to generate
|
||||
plausible candidates from that `KnowledgeBase` given a certain textual mention,
|
||||
and a machine learning model to pick the right candidate, given the local
|
||||
context of the mention. `EntityLinker` defaults to using the
|
||||
[`InMemoryLookupKB`](/api/inmemorylookupkb) implementation.
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
Predictions, in the form of knowledge base IDs, will be assigned to
|
||||
`Token.ent_kb_id_`.
|
||||
|
||||
| Location | Value |
|
||||
| ------------------ | --------------------------------- |
|
||||
| `Token.ent_kb_id` | Knowledge base ID (hash). ~~int~~ |
|
||||
| `Token.ent_kb_id_` | Knowledge base ID. ~~str~~ |
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config). See the
|
||||
[model architectures](/api/architectures) documentation for details on the
|
||||
architectures and their arguments and hyperparameters.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.pipeline.entity_linker import DEFAULT_NEL_MODEL
|
||||
> config = {
|
||||
> "labels_discard": [],
|
||||
> "n_sents": 0,
|
||||
> "incl_prior": True,
|
||||
> "incl_context": True,
|
||||
> "model": DEFAULT_NEL_MODEL,
|
||||
> "entity_vector_length": 64,
|
||||
> "get_candidates": {'@misc': 'spacy.CandidateGenerator.v1'},
|
||||
> "threshold": None,
|
||||
> }
|
||||
> nlp.add_pipe("entity_linker", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| --------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `labels_discard` | NER labels that will automatically get a "NIL" prediction. Defaults to `[]`. ~~Iterable[str]~~ |
|
||||
| `n_sents` | The number of neighbouring sentences to take into account. Defaults to 0. ~~int~~ |
|
||||
| `incl_prior` | Whether or not to include prior probabilities from the KB in the model. Defaults to `True`. ~~bool~~ |
|
||||
| `incl_context` | Whether or not to include the local context in the model. Defaults to `True`. ~~bool~~ |
|
||||
| `model` | The [`Model`](https://thinc.ai/docs/api-model) powering the pipeline component. Defaults to [EntityLinker](/api/architectures#EntityLinker). ~~Model~~ |
|
||||
| `entity_vector_length` | Size of encoding vectors in the KB. Defaults to `64`. ~~int~~ |
|
||||
| `use_gold_ents` | Whether to copy entities from the gold docs or not. Defaults to `True`. If `False`, entities must be set in the training data or by an annotating component in the pipeline. ~~bool~~ |
|
||||
| `get_candidates` | Function that generates plausible candidates for a given `Span` object. Defaults to [CandidateGenerator](/api/architectures#CandidateGenerator), a function looking up exact, case-dependent aliases in the KB. ~~Callable[[KnowledgeBase, Span], Iterable[Candidate]]~~ |
|
||||
| `get_candidates_batch` <Tag variant="new">3.5</Tag> | Function that generates plausible candidates for a given batch of `Span` objects. Defaults to [CandidateBatchGenerator](/api/architectures#CandidateBatchGenerator), a function looking up exact, case-dependent aliases in the KB. ~~Callable[[KnowledgeBase, Iterable[Span]], Iterable[Iterable[Candidate]]]~~ |
|
||||
| `generate_empty_kb` <Tag variant="new">3.5.1</Tag> | Function that generates an empty `KnowledgeBase` object. Defaults to [`spacy.EmptyKB.v2`](/api/architectures#EmptyKB), which generates an empty [`InMemoryLookupKB`](/api/inmemorylookupkb). ~~Callable[[Vocab, int], KnowledgeBase]~~ |
|
||||
| `overwrite` <Tag variant="new">3.2</Tag> | Whether existing annotation is overwritten. Defaults to `True`. ~~bool~~ |
|
||||
| `scorer` <Tag variant="new">3.2</Tag> | The scoring method. Defaults to [`Scorer.score_links`](/api/scorer#score_links). ~~Optional[Callable]~~ |
|
||||
| `threshold` <Tag variant="new">3.4</Tag> | Confidence threshold for entity predictions. The default of `None` implies that all predictions are accepted, otherwise those with a score beneath the threshold are discarded. If there are no predictions with scores above the threshold, the linked entity is `NIL`. ~~Optional[float]~~ |
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/entity_linker.py
|
||||
```
|
||||
|
||||
## EntityLinker.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe with default model
|
||||
> entity_linker = nlp.add_pipe("entity_linker")
|
||||
>
|
||||
> # Construction via add_pipe with custom model
|
||||
> config = {"model": {"@architectures": "my_el.v1"}}
|
||||
> entity_linker = nlp.add_pipe("entity_linker", config=config)
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy.pipeline import EntityLinker
|
||||
> entity_linker = EntityLinker(nlp.vocab, model)
|
||||
> ```
|
||||
|
||||
Create a new pipeline instance. In your application, you would normally use a
|
||||
shortcut for this and instantiate the component using its string name and
|
||||
[`nlp.add_pipe`](/api/language#add_pipe).
|
||||
|
||||
Upon construction of the entity linker component, an empty knowledge base is
|
||||
constructed with the provided `entity_vector_length`. If you want to use a
|
||||
custom knowledge base, you should either call
|
||||
[`set_kb`](/api/entitylinker#set_kb) or provide a `kb_loader` in the
|
||||
[`initialize`](/api/entitylinker#initialize) call.
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | The [`Model`](https://thinc.ai/docs/api-model) powering the pipeline component. ~~Model~~ |
|
||||
| `name` | String name of the component instance. Used to add entries to the `losses` during training. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `entity_vector_length` | Size of encoding vectors in the KB. ~~int~~ |
|
||||
| `get_candidates` | Function that generates plausible candidates for a given `Span` object. ~~Callable[[KnowledgeBase, Span], Iterable[Candidate]]~~ |
|
||||
| `labels_discard` | NER labels that will automatically get a `"NIL"` prediction. ~~Iterable[str]~~ |
|
||||
| `n_sents` | The number of neighbouring sentences to take into account. ~~int~~ |
|
||||
| `incl_prior` | Whether or not to include prior probabilities from the KB in the model. ~~bool~~ |
|
||||
| `incl_context` | Whether or not to include the local context in the model. ~~bool~~ |
|
||||
| `overwrite` <Tag variant="new">3.2</Tag> | Whether existing annotation is overwritten. Defaults to `True`. ~~bool~~ |
|
||||
| `scorer` <Tag variant="new">3.2</Tag> | The scoring method. Defaults to [`Scorer.score_links`](/api/scorer#score_links). ~~Optional[Callable]~~ |
|
||||
| `threshold` <Tag variant="new">3.4</Tag> | Confidence threshold for entity predictions. The default of `None` implies that all predictions are accepted, otherwise those with a score beneath the threshold are discarded. If there are no predictions with scores above the threshold, the linked entity is `NIL`. ~~Optional[float]~~ |
|
||||
|
||||
## EntityLinker.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document. The document is modified in place and returned.
|
||||
This usually happens under the hood when the `nlp` object is called on a text
|
||||
and all pipeline components are applied to the `Doc` in order. Both
|
||||
[`__call__`](/api/entitylinker#call) and [`pipe`](/api/entitylinker#pipe)
|
||||
delegate to the [`predict`](/api/entitylinker#predict) and
|
||||
[`set_annotations`](/api/entitylinker#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> entity_linker = nlp.add_pipe("entity_linker")
|
||||
> # This usually happens under the hood
|
||||
> processed = entity_linker(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## EntityLinker.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order. Both [`__call__`](/api/entitylinker#call) and
|
||||
[`pipe`](/api/entitylinker#pipe) delegate to the
|
||||
[`predict`](/api/entitylinker#predict) and
|
||||
[`set_annotations`](/api/entitylinker#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> entity_linker = nlp.add_pipe("entity_linker")
|
||||
> for doc in entity_linker.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `stream` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## EntityLinker.set_kb {id="set_kb",tag="method",version="3"}
|
||||
|
||||
The `kb_loader` should be a function that takes a `Vocab` instance and creates
|
||||
the `KnowledgeBase`, ensuring that the strings of the knowledge base are synced
|
||||
with the current vocab.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> def create_kb(vocab):
|
||||
> kb = InMemoryLookupKB(vocab, entity_vector_length=128)
|
||||
> kb.add_entity(...)
|
||||
> kb.add_alias(...)
|
||||
> return kb
|
||||
> entity_linker = nlp.add_pipe("entity_linker")
|
||||
> entity_linker.set_kb(create_kb)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------------------- |
|
||||
| `kb_loader` | Function that creates a [`KnowledgeBase`](/api/kb) from a `Vocab` instance. ~~Callable[[Vocab], KnowledgeBase]~~ |
|
||||
|
||||
## EntityLinker.initialize {id="initialize",tag="method",version="3"}
|
||||
|
||||
Initialize the component for training. `get_examples` should be a function that
|
||||
returns an iterable of [`Example`](/api/example) objects. **At least one example
|
||||
should be supplied.** The data examples are used to **initialize the model** of
|
||||
the component and can either be the full training data or a representative
|
||||
sample. Initialization includes validating the network,
|
||||
[inferring missing shapes](https://thinc.ai/docs/usage-models#validation) and
|
||||
setting up the label scheme based on the data. This method is typically called
|
||||
by [`Language.initialize`](/api/language#initialize).
|
||||
|
||||
Optionally, a `kb_loader` argument may be specified to change the internal
|
||||
knowledge base. This argument should be a function that takes a `Vocab` instance
|
||||
and creates the `KnowledgeBase`, ensuring that the strings of the knowledge base
|
||||
are synced with the current vocab.
|
||||
|
||||
<Infobox variant="warning" title="Changed in v3.0" id="begin_training">
|
||||
|
||||
This method was previously called `begin_training`.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> entity_linker = nlp.add_pipe("entity_linker")
|
||||
> entity_linker.initialize(lambda: examples, nlp=nlp, kb_loader=my_kb)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Must contain at least one `Example`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
| `kb_loader` | Function that creates a [`KnowledgeBase`](/api/kb) from a `Vocab` instance. ~~Callable[[Vocab], KnowledgeBase]~~ |
|
||||
|
||||
## EntityLinker.predict {id="predict",tag="method"}
|
||||
|
||||
Apply the component's model to a batch of [`Doc`](/api/doc) objects, without
|
||||
modifying them. Returns the KB IDs for each entity in each doc, including `NIL`
|
||||
if there is no prediction.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> entity_linker = nlp.add_pipe("entity_linker")
|
||||
> kb_ids = entity_linker.predict([doc1, doc2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------- |
|
||||
| `docs` | The documents to predict. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | The predicted KB identifiers for the entities in the `docs`. ~~List[str]~~ |
|
||||
|
||||
## EntityLinker.set_annotations {id="set_annotations",tag="method"}
|
||||
|
||||
Modify a batch of documents, using pre-computed entity IDs for a list of named
|
||||
entities.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> entity_linker = nlp.add_pipe("entity_linker")
|
||||
> kb_ids = entity_linker.predict([doc1, doc2])
|
||||
> entity_linker.set_annotations([doc1, doc2], kb_ids)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | --------------------------------------------------------------------------------------------------------------- |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `kb_ids` | The knowledge base identifiers for the entities in the docs, predicted by `EntityLinker.predict`. ~~List[str]~~ |
|
||||
|
||||
## EntityLinker.update {id="update",tag="method"}
|
||||
|
||||
Learn from a batch of [`Example`](/api/example) objects, updating both the
|
||||
pipe's entity linking model and context encoder. Delegates to
|
||||
[`predict`](/api/entitylinker#predict).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> entity_linker = nlp.add_pipe("entity_linker")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = entity_linker.update(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## EntityLinker.create_optimizer {id="create_optimizer",tag="method"}
|
||||
|
||||
Create an optimizer for the pipeline component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> entity_linker = nlp.add_pipe("entity_linker")
|
||||
> optimizer = entity_linker.create_optimizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The optimizer. ~~Optimizer~~ |
|
||||
|
||||
## EntityLinker.use_params {id="use_params",tag="method, contextmanager"}
|
||||
|
||||
Modify the pipe's model, to use the given parameter values. At the end of the
|
||||
context, the original parameters are restored.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> entity_linker = nlp.add_pipe("entity_linker")
|
||||
> with entity_linker.use_params(optimizer.averages):
|
||||
> entity_linker.to_disk("/best_model")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------- |
|
||||
| `params` | The parameter values to use in the model. ~~dict~~ |
|
||||
|
||||
## EntityLinker.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> entity_linker = nlp.add_pipe("entity_linker")
|
||||
> entity_linker.to_disk("/path/to/entity_linker")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## EntityLinker.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> entity_linker = nlp.add_pipe("entity_linker")
|
||||
> entity_linker.from_disk("/path/to/entity_linker")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `EntityLinker` object. ~~EntityLinker~~ |
|
||||
|
||||
## EntityLinker.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> entity_linker = nlp.add_pipe("entity_linker")
|
||||
> entity_linker_bytes = entity_linker.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring, including the `KnowledgeBase`.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `EntityLinker` object. ~~bytes~~ |
|
||||
|
||||
## EntityLinker.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> entity_linker_bytes = entity_linker.to_bytes()
|
||||
> entity_linker = nlp.add_pipe("entity_linker")
|
||||
> entity_linker.from_bytes(entity_linker_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `EntityLinker` object. ~~EntityLinker~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = entity_linker.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `cfg` | The config file. You usually don't want to exclude this. |
|
||||
| `model` | The binary model data. You usually don't want to exclude this. |
|
||||
| `kb` | The knowledge base. You usually don't want to exclude this. |
|
||||
@@ -0,0 +1,464 @@
|
||||
---
|
||||
title: EntityRecognizer
|
||||
tag: class
|
||||
source: spacy/pipeline/ner.pyx
|
||||
teaser: 'Pipeline component for named entity recognition'
|
||||
api_base_class: /api/pipe
|
||||
api_string_name: ner
|
||||
api_trainable: true
|
||||
---
|
||||
|
||||
A transition-based named entity recognition component. The entity recognizer
|
||||
identifies **non-overlapping labelled spans** of tokens. The transition-based
|
||||
algorithm used encodes certain assumptions that are effective for "traditional"
|
||||
named entity recognition tasks, but may not be a good fit for every span
|
||||
identification problem. Specifically, the loss function optimizes for **whole
|
||||
entity accuracy**, so if your inter-annotator agreement on boundary tokens is
|
||||
low, the component will likely perform poorly on your problem. The
|
||||
transition-based algorithm also assumes that the most decisive information about
|
||||
your entities will be close to their initial tokens. If your entities are long
|
||||
and characterized by tokens in their middle, the component will likely not be a
|
||||
good fit for your task.
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
Predictions will be saved to `Doc.ents` as a tuple. Each label will also be
|
||||
reflected to each underlying token, where it is saved in the `Token.ent_type`
|
||||
and `Token.ent_iob` fields. Note that by definition each token can only have one
|
||||
label.
|
||||
|
||||
When setting `Doc.ents` to create training data, all the spans must be valid and
|
||||
non-overlapping, or an error will be thrown.
|
||||
|
||||
| Location | Value |
|
||||
| ----------------- | ----------------------------------------------------------------- |
|
||||
| `Doc.ents` | The annotated spans. ~~Tuple[Span]~~ |
|
||||
| `Token.ent_iob` | An enum encoding of the IOB part of the named entity tag. ~~int~~ |
|
||||
| `Token.ent_iob_` | The IOB part of the named entity tag. ~~str~~ |
|
||||
| `Token.ent_type` | The label part of the named entity tag (hash). ~~int~~ |
|
||||
| `Token.ent_type_` | The label part of the named entity tag. ~~str~~ |
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config). See the
|
||||
[model architectures](/api/architectures) documentation for details on the
|
||||
architectures and their arguments and hyperparameters.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.pipeline.ner import DEFAULT_NER_MODEL
|
||||
> config = {
|
||||
> "moves": None,
|
||||
> "update_with_oracle_cut_size": 100,
|
||||
> "model": DEFAULT_NER_MODEL,
|
||||
> "incorrect_spans_key": "incorrect_spans",
|
||||
> }
|
||||
> nlp.add_pipe("ner", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `moves` | A list of transition names. Inferred from the data if not provided. Defaults to `None`. ~~Optional[TransitionSystem]~~ |
|
||||
| `update_with_oracle_cut_size` | During training, cut long sequences into shorter segments by creating intermediate states based on the gold-standard history. The model is not very sensitive to this parameter, so you usually won't need to change it. Defaults to `100`. ~~int~~ |
|
||||
| `model` | The [`Model`](https://thinc.ai/docs/api-model) powering the pipeline component. Defaults to [TransitionBasedParser](/api/architectures#TransitionBasedParser). ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
| `incorrect_spans_key` | This key refers to a `SpanGroup` in `doc.spans` that specifies incorrect spans. The NER will learn not to predict (exactly) those spans. Defaults to `None`. ~~Optional[str]~~ |
|
||||
| `scorer` | The scoring method. Defaults to [`spacy.scorer.get_ner_prf`](/api/scorer#get_ner_prf). ~~Optional[Callable]~~ |
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/ner.pyx
|
||||
```
|
||||
|
||||
## EntityRecognizer.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe with default model
|
||||
> ner = nlp.add_pipe("ner")
|
||||
>
|
||||
> # Construction via add_pipe with custom model
|
||||
> config = {"model": {"@architectures": "my_ner"}}
|
||||
> parser = nlp.add_pipe("ner", config=config)
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy.pipeline import EntityRecognizer
|
||||
> ner = EntityRecognizer(nlp.vocab, model)
|
||||
> ```
|
||||
|
||||
Create a new pipeline instance. In your application, you would normally use a
|
||||
shortcut for this and instantiate the component using its string name and
|
||||
[`nlp.add_pipe`](/api/language#add_pipe).
|
||||
|
||||
| Name | Description |
|
||||
| ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | The [`Model`](https://thinc.ai/docs/api-model) powering the pipeline component. ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
| `name` | String name of the component instance. Used to add entries to the `losses` during training. ~~str~~ |
|
||||
| `moves` | A list of transition names. Inferred from the data if set to `None`, which is the default. ~~Optional[TransitionSystem]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `update_with_oracle_cut_size` | During training, cut long sequences into shorter segments by creating intermediate states based on the gold-standard history. The model is not very sensitive to this parameter, so you usually won't need to change it. Defaults to `100`. ~~int~~ |
|
||||
| `incorrect_spans_key` | Identifies spans that are known to be incorrect entity annotations. The incorrect entity annotations can be stored in the span group in [`Doc.spans`](/api/doc#spans), under this key. Defaults to `None`. ~~Optional[str]~~ |
|
||||
|
||||
## EntityRecognizer.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document. The document is modified in place and returned.
|
||||
This usually happens under the hood when the `nlp` object is called on a text
|
||||
and all pipeline components are applied to the `Doc` in order. Both
|
||||
[`__call__`](/api/entityrecognizer#call) and
|
||||
[`pipe`](/api/entityrecognizer#pipe) delegate to the
|
||||
[`predict`](/api/entityrecognizer#predict) and
|
||||
[`set_annotations`](/api/entityrecognizer#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> ner = nlp.add_pipe("ner")
|
||||
> # This usually happens under the hood
|
||||
> processed = ner(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## EntityRecognizer.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order. Both [`__call__`](/api/entityrecognizer#call) and
|
||||
[`pipe`](/api/entityrecognizer#pipe) delegate to the
|
||||
[`predict`](/api/entityrecognizer#predict) and
|
||||
[`set_annotations`](/api/entityrecognizer#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ner = nlp.add_pipe("ner")
|
||||
> for doc in ner.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `docs` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## EntityRecognizer.initialize {id="initialize",tag="method",version="3"}
|
||||
|
||||
Initialize the component for training. `get_examples` should be a function that
|
||||
returns an iterable of [`Example`](/api/example) objects. **At least one example
|
||||
should be supplied.** The data examples are used to **initialize the model** of
|
||||
the component and can either be the full training data or a representative
|
||||
sample. Initialization includes validating the network,
|
||||
[inferring missing shapes](https://thinc.ai/docs/usage-models#validation) and
|
||||
setting up the label scheme based on the data. This method is typically called
|
||||
by [`Language.initialize`](/api/language#initialize) and lets you customize
|
||||
arguments it receives via the
|
||||
[`[initialize.components]`](/api/data-formats#config-initialize) block in the
|
||||
config.
|
||||
|
||||
<Infobox variant="warning" title="Changed in v3.0" id="begin_training">
|
||||
|
||||
This method was previously called `begin_training`.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ner = nlp.add_pipe("ner")
|
||||
> ner.initialize(lambda: examples, nlp=nlp)
|
||||
> ```
|
||||
>
|
||||
> ```ini
|
||||
> ### config.cfg
|
||||
> [initialize.components.ner]
|
||||
>
|
||||
> [initialize.components.ner.labels]
|
||||
> @readers = "spacy.read_labels.v1"
|
||||
> path = "corpus/labels/ner.json
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Must contain at least one `Example`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
| `labels` | The label information to add to the component, as provided by the [`label_data`](#label_data) property after initialization. To generate a reusable JSON file from your data, you should run the [`init labels`](/api/cli#init-labels) command. If no labels are provided, the `get_examples` callback is used to extract the labels from the data, which may be a lot slower. ~~Optional[Dict[str, Dict[str, int]]]~~ |
|
||||
|
||||
## EntityRecognizer.predict {id="predict",tag="method"}
|
||||
|
||||
Apply the component's model to a batch of [`Doc`](/api/doc) objects, without
|
||||
modifying them.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ner = nlp.add_pipe("ner")
|
||||
> scores = ner.predict([doc1, doc2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------- |
|
||||
| `docs` | The documents to predict. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | A helper class for the parse state (internal). ~~StateClass~~ |
|
||||
|
||||
## EntityRecognizer.set_annotations {id="set_annotations",tag="method"}
|
||||
|
||||
Modify a batch of [`Doc`](/api/doc) objects, using pre-computed scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ner = nlp.add_pipe("ner")
|
||||
> scores = ner.predict([doc1, doc2])
|
||||
> ner.set_annotations([doc1, doc2], scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | ------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `scores` | The scores to set, produced by `EntityRecognizer.predict`. Returns an internal helper class for the parse state. ~~List[StateClass]~~ |
|
||||
|
||||
## EntityRecognizer.update {id="update",tag="method"}
|
||||
|
||||
Learn from a batch of [`Example`](/api/example) objects, updating the pipe's
|
||||
model. Delegates to [`predict`](/api/entityrecognizer#predict) and
|
||||
[`get_loss`](/api/entityrecognizer#get_loss).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ner = nlp.add_pipe("ner")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = ner.update(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## EntityRecognizer.get_loss {id="get_loss",tag="method"}
|
||||
|
||||
Find the loss and gradient of loss for the batch of documents and their
|
||||
predicted scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ner = nlp.add_pipe("ner")
|
||||
> scores = ner.predict([eg.predicted for eg in examples])
|
||||
> loss, d_loss = ner.get_loss(examples, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------------------------- |
|
||||
| `examples` | The batch of examples. ~~Iterable[Example]~~ |
|
||||
| `scores` | Scores representing the model's predictions. ~~StateClass~~ |
|
||||
| **RETURNS** | The loss and the gradient, i.e. `(loss, gradient)`. ~~Tuple[float, float]~~ |
|
||||
|
||||
## EntityRecognizer.create_optimizer {id="create_optimizer",tag="method"}
|
||||
|
||||
Create an optimizer for the pipeline component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ner = nlp.add_pipe("ner")
|
||||
> optimizer = ner.create_optimizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The optimizer. ~~Optimizer~~ |
|
||||
|
||||
## EntityRecognizer.use_params {id="use_params",tag="method, contextmanager"}
|
||||
|
||||
Modify the pipe's model, to use the given parameter values. At the end of the
|
||||
context, the original parameters are restored.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ner = EntityRecognizer(nlp.vocab)
|
||||
> with ner.use_params(optimizer.averages):
|
||||
> ner.to_disk("/best_model")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------- |
|
||||
| `params` | The parameter values to use in the model. ~~dict~~ |
|
||||
|
||||
## EntityRecognizer.add_label {id="add_label",tag="method"}
|
||||
|
||||
Add a new label to the pipe. Note that you don't have to call this method if you
|
||||
provide a **representative data sample** to the [`initialize`](#initialize)
|
||||
method. In this case, all labels found in the sample will be automatically added
|
||||
to the model, and the output dimension will be
|
||||
[inferred](/usage/layers-architectures#thinc-shape-inference) automatically.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ner = nlp.add_pipe("ner")
|
||||
> ner.add_label("MY_LABEL")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------- |
|
||||
| `label` | The label to add. ~~str~~ |
|
||||
| **RETURNS** | `0` if the label is already present, otherwise `1`. ~~int~~ |
|
||||
|
||||
## EntityRecognizer.set_output {id="set_output",tag="method"}
|
||||
|
||||
Change the output dimension of the component's model by calling the model's
|
||||
attribute `resize_output`. This is a function that takes the original model and
|
||||
the new output dimension `nO`, and changes the model in place. When resizing an
|
||||
already trained model, care should be taken to avoid the "catastrophic
|
||||
forgetting" problem.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ner = nlp.add_pipe("ner")
|
||||
> ner.set_output(512)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---- | --------------------------------- |
|
||||
| `nO` | The new output dimension. ~~int~~ |
|
||||
|
||||
## EntityRecognizer.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ner = nlp.add_pipe("ner")
|
||||
> ner.to_disk("/path/to/ner")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## EntityRecognizer.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ner = nlp.add_pipe("ner")
|
||||
> ner.from_disk("/path/to/ner")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `EntityRecognizer` object. ~~EntityRecognizer~~ |
|
||||
|
||||
## EntityRecognizer.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ner = nlp.add_pipe("ner")
|
||||
> ner_bytes = ner.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `EntityRecognizer` object. ~~bytes~~ |
|
||||
|
||||
## EntityRecognizer.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ner_bytes = ner.to_bytes()
|
||||
> ner = nlp.add_pipe("ner")
|
||||
> ner.from_bytes(ner_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `EntityRecognizer` object. ~~EntityRecognizer~~ |
|
||||
|
||||
## EntityRecognizer.labels {id="labels",tag="property"}
|
||||
|
||||
The labels currently added to the component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ner.add_label("MY_LABEL")
|
||||
> assert "MY_LABEL" in ner.labels
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------ |
|
||||
| **RETURNS** | The labels added to the component. ~~Tuple[str, ...]~~ |
|
||||
|
||||
## EntityRecognizer.label_data {id="label_data",tag="property",version="3"}
|
||||
|
||||
The labels currently added to the component and their internal meta information.
|
||||
This is the data generated by [`init labels`](/api/cli#init-labels) and used by
|
||||
[`EntityRecognizer.initialize`](/api/entityrecognizer#initialize) to initialize
|
||||
the model with a pre-defined label set.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> labels = ner.label_data
|
||||
> ner.initialize(lambda: [], nlp=nlp, labels=labels)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------------------------- |
|
||||
| **RETURNS** | The label data added to the component. ~~Dict[str, Dict[str, Dict[str, int]]]~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = ner.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `cfg` | The config file. You usually don't want to exclude this. |
|
||||
| `model` | The binary model data. You usually don't want to exclude this. |
|
||||
@@ -0,0 +1,336 @@
|
||||
---
|
||||
title: EntityRuler
|
||||
tag: class
|
||||
source: spacy/pipeline/entityruler.py
|
||||
version: 2.1
|
||||
teaser: 'Pipeline component for rule-based named entity recognition'
|
||||
api_string_name: entity_ruler
|
||||
api_trainable: false
|
||||
---
|
||||
|
||||
The entity ruler lets you add spans to the [`Doc.ents`](/api/doc#ents) using
|
||||
token-based rules or exact phrase matches. It can be combined with the
|
||||
statistical [`EntityRecognizer`](/api/entityrecognizer) to boost accuracy, or
|
||||
used on its own to implement a purely rule-based entity recognition system. For
|
||||
usage examples, see the docs on
|
||||
[rule-based entity recognition](/usage/rule-based-matching#entityruler).
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
This component assigns predictions basically the same way as the
|
||||
[`EntityRecognizer`](/api/entityrecognizer).
|
||||
|
||||
Predictions can be accessed under `Doc.ents` as a tuple. Each label will also be
|
||||
reflected in each underlying token, where it is saved in the `Token.ent_type`
|
||||
and `Token.ent_iob` fields. Note that by definition each token can only have one
|
||||
label.
|
||||
|
||||
When setting `Doc.ents` to create training data, all the spans must be valid and
|
||||
non-overlapping, or an error will be thrown.
|
||||
|
||||
| Location | Value |
|
||||
| ----------------- | ----------------------------------------------------------------- |
|
||||
| `Doc.ents` | The annotated spans. ~~Tuple[Span]~~ |
|
||||
| `Token.ent_iob` | An enum encoding of the IOB part of the named entity tag. ~~int~~ |
|
||||
| `Token.ent_iob_` | The IOB part of the named entity tag. ~~str~~ |
|
||||
| `Token.ent_type` | The label part of the named entity tag (hash). ~~int~~ |
|
||||
| `Token.ent_type_` | The label part of the named entity tag. ~~str~~ |
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> config = {
|
||||
> "phrase_matcher_attr": None,
|
||||
> "validate": True,
|
||||
> "overwrite_ents": False,
|
||||
> "ent_id_sep": "||",
|
||||
> }
|
||||
> nlp.add_pipe("entity_ruler", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| ---------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `phrase_matcher_attr` | Optional attribute name match on for the internal [`PhraseMatcher`](/api/phrasematcher), e.g. `LOWER` to match on the lowercase token text. Defaults to `None`. ~~Optional[Union[int, str]]~~ |
|
||||
| `matcher_fuzzy_compare` <Tag variant="new">3.5</Tag> | The fuzzy comparison method, passed on to the internal `Matcher`. Defaults to `spacy.matcher.levenshtein.levenshtein_compare`. ~~Callable~~ |
|
||||
| `validate` | Whether patterns should be validated (passed to the `Matcher` and `PhraseMatcher`). Defaults to `False`. ~~bool~~ |
|
||||
| `overwrite_ents` | If existing entities are present, e.g. entities added by the model, overwrite them by matches if necessary. Defaults to `False`. ~~bool~~ |
|
||||
| `ent_id_sep` | Separator used internally for entity IDs. Defaults to `"\|\|"`. ~~str~~ |
|
||||
| `scorer` | The scoring method. Defaults to [`spacy.scorer.get_ner_prf`](/api/scorer#get_ner_prf). ~~Optional[Callable]~~ |
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/entityruler.py
|
||||
```
|
||||
|
||||
## EntityRuler.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Initialize the entity ruler. If patterns are supplied here, they need to be a
|
||||
list of dictionaries with a `"label"` and `"pattern"` key. A pattern can either
|
||||
be a token pattern (list) or a phrase pattern (string). For example:
|
||||
`{"label": "ORG", "pattern": "Apple"}`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe
|
||||
> ruler = nlp.add_pipe("entity_ruler")
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy.pipeline import EntityRuler
|
||||
> ruler = EntityRuler(nlp, overwrite_ents=True)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `nlp` | The shared nlp object to pass the vocab to the matchers and process phrase patterns. ~~Language~~ |
|
||||
| `name` <Tag variant="new">3</Tag> | Instance name of the current pipeline component. Typically passed in automatically from the factory when the component is added. Used to disable the current entity ruler while creating phrase patterns with the nlp object. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `phrase_matcher_attr` | Optional attribute name match on for the internal [`PhraseMatcher`](/api/phrasematcher), e.g. `LOWER` to match on the lowercase token text. Defaults to `None`. ~~Optional[Union[int, str]]~~ |
|
||||
| `matcher_fuzzy_compare` <Tag variant="new">3.5</Tag> | The fuzzy comparison method, passed on to the internal `Matcher`. Defaults to `spacy.matcher.levenshtein.levenshtein_compare`. ~~Callable~~ |
|
||||
| `validate` | Whether patterns should be validated, passed to Matcher and PhraseMatcher as `validate`. Defaults to `False`. ~~bool~~ |
|
||||
| `overwrite_ents` | If existing entities are present, e.g. entities added by the model, overwrite them by matches if necessary. Defaults to `False`. ~~bool~~ |
|
||||
| `ent_id_sep` | Separator used internally for entity IDs. Defaults to `"\|\|"`. ~~str~~ |
|
||||
| `patterns` | Optional patterns to load in on initialization. ~~Optional[List[Dict[str, Union[str, List[dict]]]]]~~ |
|
||||
| `scorer` | The scoring method. Defaults to [`spacy.scorer.get_ner_prf`](/api/scorer#get_ner_prf). ~~Optional[Callable]~~ |
|
||||
|
||||
## EntityRuler.initialize {id="initialize",tag="method",version="3"}
|
||||
|
||||
Initialize the component with data and used before training to load in rules
|
||||
from a [pattern file](/usage/rule-based-matching/#entityruler-files). This
|
||||
method is typically called by [`Language.initialize`](/api/language#initialize)
|
||||
and lets you customize arguments it receives via the
|
||||
[`[initialize.components]`](/api/data-formats#config-initialize) block in the
|
||||
config.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> entity_ruler = nlp.add_pipe("entity_ruler")
|
||||
> entity_ruler.initialize(lambda: [], nlp=nlp, patterns=patterns)
|
||||
> ```
|
||||
>
|
||||
> ```ini
|
||||
> ### config.cfg
|
||||
> [initialize.components.entity_ruler]
|
||||
>
|
||||
> [initialize.components.entity_ruler.patterns]
|
||||
> @readers = "srsly.read_jsonl.v1"
|
||||
> path = "corpus/entity_ruler_patterns.jsonl
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Not used by the `EntityRuler`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
| `patterns` | The list of patterns. Defaults to `None`. ~~Optional[Sequence[Dict[str, Union[str, List[Dict[str, Any]]]]]]~~ |
|
||||
|
||||
## EntityRuler.\_\_len\_\_ {id="len",tag="method"}
|
||||
|
||||
The number of all patterns added to the entity ruler.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("entity_ruler")
|
||||
> assert len(ruler) == 0
|
||||
> ruler.add_patterns([{"label": "ORG", "pattern": "Apple"}])
|
||||
> assert len(ruler) == 1
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------- |
|
||||
| **RETURNS** | The number of patterns. ~~int~~ |
|
||||
|
||||
## EntityRuler.\_\_contains\_\_ {id="contains",tag="method"}
|
||||
|
||||
Whether a label is present in the patterns.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("entity_ruler")
|
||||
> ruler.add_patterns([{"label": "ORG", "pattern": "Apple"}])
|
||||
> assert "ORG" in ruler
|
||||
> assert not "PERSON" in ruler
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------- |
|
||||
| `label` | The label to check. ~~str~~ |
|
||||
| **RETURNS** | Whether the entity ruler contains the label. ~~bool~~ |
|
||||
|
||||
## EntityRuler.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Find matches in the `Doc` and add them to the `doc.ents`. Typically, this
|
||||
happens automatically after the component has been added to the pipeline using
|
||||
[`nlp.add_pipe`](/api/language#add_pipe). If the entity ruler was initialized
|
||||
with `overwrite_ents=True`, existing entities will be replaced if they overlap
|
||||
with the matches. When matches overlap in a Doc, the entity ruler prioritizes
|
||||
longer patterns over shorter, and if equal the match occurring first in the Doc
|
||||
is chosen.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("entity_ruler")
|
||||
> ruler.add_patterns([{"label": "ORG", "pattern": "Apple"}])
|
||||
>
|
||||
> doc = nlp("A text about Apple.")
|
||||
> ents = [(ent.text, ent.label_) for ent in doc.ents]
|
||||
> assert ents == [("Apple", "ORG")]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------- |
|
||||
| `doc` | The `Doc` object to process, e.g. the `Doc` in the pipeline. ~~Doc~~ |
|
||||
| **RETURNS** | The modified `Doc` with added entities, if available. ~~Doc~~ |
|
||||
|
||||
## EntityRuler.add_patterns {id="add_patterns",tag="method"}
|
||||
|
||||
Add patterns to the entity ruler. A pattern can either be a token pattern (list
|
||||
of dicts) or a phrase pattern (string). For more details, see the usage guide on
|
||||
[rule-based matching](/usage/rule-based-matching).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> patterns = [
|
||||
> {"label": "ORG", "pattern": "Apple"},
|
||||
> {"label": "GPE", "pattern": [{"lower": "san"}, {"lower": "francisco"}]}
|
||||
> ]
|
||||
> ruler = nlp.add_pipe("entity_ruler")
|
||||
> ruler.add_patterns(patterns)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ---------------------------------------------------------------- |
|
||||
| `patterns` | The patterns to add. ~~List[Dict[str, Union[str, List[dict]]]]~~ |
|
||||
|
||||
## EntityRuler.remove {id="remove",tag="method",version="3.2.1"}
|
||||
|
||||
Remove a pattern by its ID from the entity ruler. A `ValueError` is raised if
|
||||
the ID does not exist.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> patterns = [{"label": "ORG", "pattern": "Apple", "id": "apple"}]
|
||||
> ruler = nlp.add_pipe("entity_ruler")
|
||||
> ruler.add_patterns(patterns)
|
||||
> ruler.remove("apple")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---- | ----------------------------------- |
|
||||
| `id` | The ID of the pattern rule. ~~str~~ |
|
||||
|
||||
## EntityRuler.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Save the entity ruler patterns to a directory. The patterns will be saved as
|
||||
newline-delimited JSON (JSONL). If a file with the suffix `.jsonl` is provided,
|
||||
only the patterns are saved as JSONL. If a directory name is provided, a
|
||||
`patterns.jsonl` and `cfg` file with the component configuration is exported.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("entity_ruler")
|
||||
> ruler.to_disk("/path/to/patterns.jsonl") # saves patterns only
|
||||
> ruler.to_disk("/path/to/entity_ruler") # saves patterns and config
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a JSONL file or directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
|
||||
## EntityRuler.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the entity ruler from a path. Expects either a file containing
|
||||
newline-delimited JSON (JSONL) with one entry per line, or a directory
|
||||
containing a `patterns.jsonl` file and a `cfg` file with the component
|
||||
configuration.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("entity_ruler")
|
||||
> ruler.from_disk("/path/to/patterns.jsonl") # loads patterns only
|
||||
> ruler.from_disk("/path/to/entity_ruler") # loads patterns and config
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a JSONL file or directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| **RETURNS** | The modified `EntityRuler` object. ~~EntityRuler~~ |
|
||||
|
||||
## EntityRuler.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
Serialize the entity ruler patterns to a bytestring.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("entity_ruler")
|
||||
> ruler_bytes = ruler.to_bytes()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------- |
|
||||
| **RETURNS** | The serialized patterns. ~~bytes~~ |
|
||||
|
||||
## EntityRuler.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler_bytes = ruler.to_bytes()
|
||||
> ruler = nlp.add_pipe("entity_ruler")
|
||||
> ruler.from_bytes(ruler_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | -------------------------------------------------- |
|
||||
| `bytes_data` | The bytestring to load. ~~bytes~~ |
|
||||
| **RETURNS** | The modified `EntityRuler` object. ~~EntityRuler~~ |
|
||||
|
||||
## EntityRuler.labels {id="labels",tag="property"}
|
||||
|
||||
All labels present in the match patterns.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------- |
|
||||
| **RETURNS** | The string labels. ~~Tuple[str, ...]~~ |
|
||||
|
||||
## EntityRuler.ent_ids {id="ent_ids",tag="property",version="2.2.2"}
|
||||
|
||||
All entity IDs present in the `id` properties of the match patterns.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------- |
|
||||
| **RETURNS** | The string IDs. ~~Tuple[str, ...]~~ |
|
||||
|
||||
## EntityRuler.patterns {id="patterns",tag="property"}
|
||||
|
||||
Get all patterns that were added to the entity ruler.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------- |
|
||||
| **RETURNS** | The original patterns, one dictionary per pattern. ~~List[Dict[str, Union[str, dict]]]~~ |
|
||||
|
||||
## Attributes {id="attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| ----------------- | --------------------------------------------------------------------------------------------------------------------- |
|
||||
| `matcher` | The underlying matcher used to process token patterns. ~~Matcher~~ |
|
||||
| `phrase_matcher` | The underlying phrase matcher used to process phrase patterns. ~~PhraseMatcher~~ |
|
||||
| `token_patterns` | The token patterns present in the entity ruler, keyed by label. ~~Dict[str, List[Dict[str, Union[str, List[dict]]]]~~ |
|
||||
| `phrase_patterns` | The phrase patterns present in the entity ruler, keyed by label. ~~Dict[str, List[Doc]]~~ |
|
||||
@@ -0,0 +1,330 @@
|
||||
---
|
||||
title: Example
|
||||
teaser: A training instance
|
||||
tag: class
|
||||
source: spacy/training/example.pyx
|
||||
version: 3.0
|
||||
---
|
||||
|
||||
An `Example` holds the information for one training instance. It stores two
|
||||
`Doc` objects: one for holding the gold-standard reference data, and one for
|
||||
holding the predictions of the pipeline. An
|
||||
[`Alignment`](/api/example#alignment-object) object stores the alignment between
|
||||
these two documents, as they can differ in tokenization.
|
||||
|
||||
## Example.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Construct an `Example` object from the `predicted` document and the `reference`
|
||||
document. If `alignment` is `None`, it will be initialized from the words in
|
||||
both documents.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Doc
|
||||
> from spacy.training import Example
|
||||
> pred_words = ["Apply", "some", "sunscreen"]
|
||||
> pred_spaces = [True, True, False]
|
||||
> gold_words = ["Apply", "some", "sun", "screen"]
|
||||
> gold_spaces = [True, True, False, False]
|
||||
> gold_tags = ["VERB", "DET", "NOUN", "NOUN"]
|
||||
> predicted = Doc(nlp.vocab, words=pred_words, spaces=pred_spaces)
|
||||
> reference = Doc(nlp.vocab, words=gold_words, spaces=gold_spaces, tags=gold_tags)
|
||||
> example = Example(predicted, reference)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `predicted` | The document containing (partial) predictions. Cannot be `None`. ~~Doc~~ |
|
||||
| `reference` | The document containing gold-standard annotations. Cannot be `None`. ~~Doc~~ |
|
||||
| _keyword-only_ | |
|
||||
| `alignment` | An object holding the alignment between the tokens of the `predicted` and `reference` documents. ~~Optional[Alignment]~~ |
|
||||
|
||||
## Example.from_dict {id="from_dict",tag="classmethod"}
|
||||
|
||||
Construct an `Example` object from the `predicted` document and the reference
|
||||
annotations provided as a dictionary. For more details on the required format,
|
||||
see the [training format documentation](/api/data-formats#dict-input).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Doc
|
||||
> from spacy.training import Example
|
||||
>
|
||||
> predicted = Doc(vocab, words=["Apply", "some", "sunscreen"])
|
||||
> token_ref = ["Apply", "some", "sun", "screen"]
|
||||
> tags_ref = ["VERB", "DET", "NOUN", "NOUN"]
|
||||
> example = Example.from_dict(predicted, {"words": token_ref, "tags": tags_ref})
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------- |
|
||||
| `predicted` | The document containing (partial) predictions. Cannot be `None`. ~~Doc~~ |
|
||||
| `example_dict` | The gold-standard annotations as a dictionary. Cannot be `None`. ~~Dict[str, Any]~~ |
|
||||
| **RETURNS** | The newly constructed object. ~~Example~~ |
|
||||
|
||||
## Example.text {id="text",tag="property"}
|
||||
|
||||
The text of the `predicted` document in this `Example`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> raw_text = example.text
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------- |
|
||||
| **RETURNS** | The text of the `predicted` document. ~~str~~ |
|
||||
|
||||
## Example.predicted {id="predicted",tag="property"}
|
||||
|
||||
The `Doc` holding the predictions. Occasionally also referred to as `example.x`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> docs = [eg.predicted for eg in examples]
|
||||
> predictions, _ = model.begin_update(docs)
|
||||
> set_annotations(docs, predictions)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------ |
|
||||
| **RETURNS** | The document containing (partial) predictions. ~~Doc~~ |
|
||||
|
||||
## Example.reference {id="reference",tag="property"}
|
||||
|
||||
The `Doc` holding the gold-standard annotations. Occasionally also referred to
|
||||
as `example.y`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> for i, eg in enumerate(examples):
|
||||
> for j, label in enumerate(all_labels):
|
||||
> gold_labels[i][j] = eg.reference.cats.get(label, 0.0)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------- |
|
||||
| **RETURNS** | The document containing gold-standard annotations. ~~Doc~~ |
|
||||
|
||||
## Example.alignment {id="alignment",tag="property"}
|
||||
|
||||
The [`Alignment`](/api/example#alignment-object) object mapping the tokens of
|
||||
the `predicted` document to those of the `reference` document.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tokens_x = ["Apply", "some", "sunscreen"]
|
||||
> x = Doc(vocab, words=tokens_x)
|
||||
> tokens_y = ["Apply", "some", "sun", "screen"]
|
||||
> example = Example.from_dict(x, {"words": tokens_y})
|
||||
> alignment = example.alignment
|
||||
> assert list(alignment.y2x.data) == [[0], [1], [2], [2]]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------- |
|
||||
| **RETURNS** | The document containing gold-standard annotations. ~~Alignment~~ |
|
||||
|
||||
## Example.get_aligned {id="get_aligned",tag="method"}
|
||||
|
||||
Get the aligned view of a certain token attribute, denoted by its int ID or
|
||||
string name.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> predicted = Doc(vocab, words=["Apply", "some", "sunscreen"])
|
||||
> token_ref = ["Apply", "some", "sun", "screen"]
|
||||
> tags_ref = ["VERB", "DET", "NOUN", "NOUN"]
|
||||
> example = Example.from_dict(predicted, {"words": token_ref, "tags": tags_ref})
|
||||
> assert example.get_aligned("TAG", as_string=True) == ["VERB", "DET", "NOUN"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------------------- |
|
||||
| `field` | Attribute ID or string name. ~~Union[int, str]~~ |
|
||||
| `as_string` | Whether or not to return the list of values as strings. Defaults to `False`. ~~bool~~ |
|
||||
| **RETURNS** | List of integer values, or string values if `as_string` is `True`. ~~Union[List[int], List[str]]~~ |
|
||||
|
||||
## Example.get_aligned_parse {id="get_aligned_parse",tag="method"}
|
||||
|
||||
Get the aligned view of the dependency parse. If `projectivize` is set to
|
||||
`True`, non-projective dependency trees are made projective through the
|
||||
Pseudo-Projective Dependency Parsing algorithm by Nivre and Nilsson (2005).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("He pretty quickly walks away")
|
||||
> example = Example.from_dict(doc, {"heads": [3, 2, 3, 0, 2]})
|
||||
> proj_heads, proj_labels = example.get_aligned_parse(projectivize=True)
|
||||
> assert proj_heads == [3, 2, 3, 0, 3]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------- |
|
||||
| `projectivize` | Whether or not to projectivize the dependency trees. Defaults to `True`. ~~bool~~ |
|
||||
| **RETURNS** | List of integer values, or string values if `as_string` is `True`. ~~Union[List[int], List[str]]~~ |
|
||||
|
||||
## Example.get_aligned_ner {id="get_aligned_ner",tag="method"}
|
||||
|
||||
Get the aligned view of the NER
|
||||
[BILUO](/usage/linguistic-features#accessing-ner) tags.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> words = ["Mrs", "Smith", "flew", "to", "New York"]
|
||||
> doc = Doc(en_vocab, words=words)
|
||||
> entities = [(0, 9, "PERSON"), (18, 26, "LOC")]
|
||||
> gold_words = ["Mrs Smith", "flew", "to", "New", "York"]
|
||||
> example = Example.from_dict(doc, {"words": gold_words, "entities": entities})
|
||||
> ner_tags = example.get_aligned_ner()
|
||||
> assert ner_tags == ["B-PERSON", "L-PERSON", "O", "O", "U-LOC"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------------------------------------------- |
|
||||
| **RETURNS** | List of BILUO values, denoting whether tokens are part of an NER annotation or not. ~~List[str]~~ |
|
||||
|
||||
## Example.get_aligned_spans_y2x {id="get_aligned_spans_y2x",tag="method"}
|
||||
|
||||
Get the aligned view of any set of [`Span`](/api/span) objects defined over
|
||||
[`Example.reference`](/api/example#reference). The resulting span indices will
|
||||
align to the tokenization in [`Example.predicted`](/api/example#predicted).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> words = ["Mr and Mrs Smith", "flew", "to", "New York"]
|
||||
> doc = Doc(en_vocab, words=words)
|
||||
> entities = [(0, 16, "PERSON")]
|
||||
> tokens_ref = ["Mr", "and", "Mrs", "Smith", "flew", "to", "New", "York"]
|
||||
> example = Example.from_dict(doc, {"words": tokens_ref, "entities": entities})
|
||||
> ents_ref = example.reference.ents
|
||||
> assert [(ent.start, ent.end) for ent in ents_ref] == [(0, 4)]
|
||||
> ents_y2x = example.get_aligned_spans_y2x(ents_ref)
|
||||
> assert [(ent.start, ent.end) for ent in ents_y2x] == [(0, 1)]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------------- | -------------------------------------------------------------------------------------------- |
|
||||
| `y_spans` | `Span` objects aligned to the tokenization of `reference`. ~~Iterable[Span]~~ |
|
||||
| `allow_overlap` | Whether the resulting `Span` objects may overlap or not. Set to `False` by default. ~~bool~~ |
|
||||
| **RETURNS** | `Span` objects aligned to the tokenization of `predicted`. ~~List[Span]~~ |
|
||||
|
||||
## Example.get_aligned_spans_x2y {id="get_aligned_spans_x2y",tag="method"}
|
||||
|
||||
Get the aligned view of any set of [`Span`](/api/span) objects defined over
|
||||
[`Example.predicted`](/api/example#predicted). The resulting span indices will
|
||||
align to the tokenization in [`Example.reference`](/api/example#reference). This
|
||||
method is particularly useful to assess the accuracy of predicted entities
|
||||
against the original gold-standard annotation.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> nlp.add_pipe("my_ner")
|
||||
> doc = nlp("Mr and Mrs Smith flew to New York")
|
||||
> tokens_ref = ["Mr and Mrs", "Smith", "flew", "to", "New York"]
|
||||
> example = Example.from_dict(doc, {"words": tokens_ref})
|
||||
> ents_pred = example.predicted.ents
|
||||
> # Assume the NER model has found "Mr and Mrs Smith" as a named entity
|
||||
> assert [(ent.start, ent.end) for ent in ents_pred] == [(0, 4)]
|
||||
> ents_x2y = example.get_aligned_spans_x2y(ents_pred)
|
||||
> assert [(ent.start, ent.end) for ent in ents_x2y] == [(0, 2)]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------------- | -------------------------------------------------------------------------------------------- |
|
||||
| `x_spans` | `Span` objects aligned to the tokenization of `predicted`. ~~Iterable[Span]~~ |
|
||||
| `allow_overlap` | Whether the resulting `Span` objects may overlap or not. Set to `False` by default. ~~bool~~ |
|
||||
| **RETURNS** | `Span` objects aligned to the tokenization of `reference`. ~~List[Span]~~ |
|
||||
|
||||
## Example.to_dict {id="to_dict",tag="method"}
|
||||
|
||||
Return a [dictionary representation](/api/data-formats#dict-input) of the
|
||||
reference annotation contained in this `Example`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> eg_dict = example.to_dict()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------------------- |
|
||||
| **RETURNS** | Dictionary representation of the reference annotation. ~~Dict[str, Any]~~ |
|
||||
|
||||
## Example.split_sents {id="split_sents",tag="method"}
|
||||
|
||||
Split one `Example` into multiple `Example` objects, one for each sentence.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I went yesterday had lots of fun")
|
||||
> tokens_ref = ["I", "went", "yesterday", "had", "lots", "of", "fun"]
|
||||
> sents_ref = [True, False, False, True, False, False, False]
|
||||
> example = Example.from_dict(doc, {"words": tokens_ref, "sent_starts": sents_ref})
|
||||
> split_examples = example.split_sents()
|
||||
> assert split_examples[0].text == "I went yesterday "
|
||||
> assert split_examples[1].text == "had lots of fun"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------- |
|
||||
| **RETURNS** | List of `Example` objects, one for each original sentence. ~~List[Example]~~ |
|
||||
|
||||
## Alignment {id="alignment-object",version="3"}
|
||||
|
||||
Calculate alignment tables between two tokenizations.
|
||||
|
||||
### Alignment attributes {id="alignment-attributes"}
|
||||
|
||||
Alignment attributes are managed using `AlignmentArray`, which is a simplified
|
||||
version of Thinc's [Ragged](https://thinc.ai/docs/api-types#ragged) type that
|
||||
only supports the `data` and `length` attributes.
|
||||
|
||||
| Name | Description |
|
||||
| ----- | ------------------------------------------------------------------------------------- |
|
||||
| `x2y` | The `AlignmentArray` object holding the alignment from `x` to `y`. ~~AlignmentArray~~ |
|
||||
| `y2x` | The `AlignmentArray` object holding the alignment from `y` to `x`. ~~AlignmentArray~~ |
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
The current implementation of the alignment algorithm assumes that both
|
||||
tokenizations add up to the same string. For example, you'll be able to align
|
||||
`["I", "'", "m"]` and `["I", "'m"]`, which both add up to `"I'm"`, but not
|
||||
`["I", "'m"]` and `["I", "am"]`.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.training import Alignment
|
||||
>
|
||||
> bert_tokens = ["obama", "'", "s", "podcast"]
|
||||
> spacy_tokens = ["obama", "'s", "podcast"]
|
||||
> alignment = Alignment.from_strings(bert_tokens, spacy_tokens)
|
||||
> a2b = alignment.x2y
|
||||
> assert list(a2b.data) == [0, 1, 1, 2]
|
||||
> ```
|
||||
>
|
||||
> If `a2b.data[1] == a2b.data[2] == 1`, that means that `A[1]` (`"'"`) and
|
||||
> `A[2]` (`"s"`) both align to `B[1]` (`"'s"`).
|
||||
|
||||
### Alignment.from_strings {id="classmethod",tag="function"}
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------- |
|
||||
| `A` | String values of candidate tokens to align. ~~List[str]~~ |
|
||||
| `B` | String values of reference tokens to align. ~~List[str]~~ |
|
||||
| **RETURNS** | An `Alignment` object describing the alignment. ~~Alignment~~ |
|
||||
@@ -0,0 +1,6 @@
|
||||
---
|
||||
title: Library Architecture
|
||||
next: /api/architectures
|
||||
---
|
||||
|
||||
<Architecture101 />
|
||||
@@ -0,0 +1,303 @@
|
||||
---
|
||||
title: InMemoryLookupKB
|
||||
teaser:
|
||||
The default implementation of the KnowledgeBase interface. Stores all
|
||||
information in-memory.
|
||||
tag: class
|
||||
source: spacy/kb/kb_in_memory.pyx
|
||||
version: 3.5
|
||||
---
|
||||
|
||||
The `InMemoryLookupKB` class inherits from [`KnowledgeBase`](/api/kb) and
|
||||
implements all of its methods. It stores all KB data in-memory and generates
|
||||
[`Candidate`](/api/kb#candidate) objects by exactly matching mentions with
|
||||
entity names. It's highly optimized for both a low memory footprint and speed of
|
||||
retrieval.
|
||||
|
||||
## InMemoryLookupKB.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create the knowledge base.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.kb import InMemoryLookupKB
|
||||
> vocab = nlp.vocab
|
||||
> kb = InMemoryLookupKB(vocab=vocab, entity_vector_length=64)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------- | ------------------------------------------------ |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `entity_vector_length` | Length of the fixed-size entity vectors. ~~int~~ |
|
||||
|
||||
## InMemoryLookupKB.entity_vector_length {id="entity_vector_length",tag="property"}
|
||||
|
||||
The length of the fixed-size entity vectors in the knowledge base.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------ |
|
||||
| **RETURNS** | Length of the fixed-size entity vectors. ~~int~~ |
|
||||
|
||||
## InMemoryLookupKB.add_entity {id="add_entity",tag="method"}
|
||||
|
||||
Add an entity to the knowledge base, specifying its corpus frequency and entity
|
||||
vector, which should be of length
|
||||
[`entity_vector_length`](/api/inmemorylookupkb#entity_vector_length).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> kb.add_entity(entity="Q42", freq=32, entity_vector=vector1)
|
||||
> kb.add_entity(entity="Q463035", freq=111, entity_vector=vector2)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------------- | ---------------------------------------------------------- |
|
||||
| `entity` | The unique entity identifier. ~~str~~ |
|
||||
| `freq` | The frequency of the entity in a typical corpus. ~~float~~ |
|
||||
| `entity_vector` | The pretrained vector of the entity. ~~numpy.ndarray~~ |
|
||||
|
||||
## InMemoryLookupKB.set_entities {id="set_entities",tag="method"}
|
||||
|
||||
Define the full list of entities in the knowledge base, specifying the corpus
|
||||
frequency and entity vector for each entity.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> kb.set_entities(entity_list=["Q42", "Q463035"], freq_list=[32, 111], vector_list=[vector1, vector2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------- | ---------------------------------------------------------------- |
|
||||
| `entity_list` | List of unique entity identifiers. ~~Iterable[Union[str, int]]~~ |
|
||||
| `freq_list` | List of entity frequencies. ~~Iterable[int]~~ |
|
||||
| `vector_list` | List of entity vectors. ~~Iterable[numpy.ndarray]~~ |
|
||||
|
||||
## InMemoryLookupKB.add_alias {id="add_alias",tag="method"}
|
||||
|
||||
Add an alias or mention to the knowledge base, specifying its potential KB
|
||||
identifiers and their prior probabilities. The entity identifiers should refer
|
||||
to entities previously added with
|
||||
[`add_entity`](/api/inmemorylookupkb#add_entity) or
|
||||
[`set_entities`](/api/inmemorylookupkb#set_entities). The sum of the prior
|
||||
probabilities should not exceed 1. Note that an empty string can not be used as
|
||||
alias.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> kb.add_alias(alias="Douglas", entities=["Q42", "Q463035"], probabilities=[0.6, 0.3])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------------- | --------------------------------------------------------------------------------- |
|
||||
| `alias` | The textual mention or alias. Can not be the empty string. ~~str~~ |
|
||||
| `entities` | The potential entities that the alias may refer to. ~~Iterable[Union[str, int]]~~ |
|
||||
| `probabilities` | The prior probabilities of each entity. ~~Iterable[float]~~ |
|
||||
|
||||
## InMemoryLookupKB.\_\_len\_\_ {id="len",tag="method"}
|
||||
|
||||
Get the total number of entities in the knowledge base.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> total_entities = len(kb)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------- |
|
||||
| **RETURNS** | The number of entities in the knowledge base. ~~int~~ |
|
||||
|
||||
## InMemoryLookupKB.get_entity_strings {id="get_entity_strings",tag="method"}
|
||||
|
||||
Get a list of all entity IDs in the knowledge base.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> all_entities = kb.get_entity_strings()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------- |
|
||||
| **RETURNS** | The list of entities in the knowledge base. ~~List[str]~~ |
|
||||
|
||||
## InMemoryLookupKB.get_size_aliases {id="get_size_aliases",tag="method"}
|
||||
|
||||
Get the total number of aliases in the knowledge base.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> total_aliases = kb.get_size_aliases()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------- |
|
||||
| **RETURNS** | The number of aliases in the knowledge base. ~~int~~ |
|
||||
|
||||
## InMemoryLookupKB.get_alias_strings {id="get_alias_strings",tag="method"}
|
||||
|
||||
Get a list of all aliases in the knowledge base.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> all_aliases = kb.get_alias_strings()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------- |
|
||||
| **RETURNS** | The list of aliases in the knowledge base. ~~List[str]~~ |
|
||||
|
||||
## InMemoryLookupKB.get_candidates {id="get_candidates",tag="method"}
|
||||
|
||||
Given a certain textual mention as input, retrieve a list of candidate entities
|
||||
of type [`Candidate`](/api/kb#candidate). Wraps
|
||||
[`get_alias_candidates()`](/api/inmemorylookupkb#get_alias_candidates).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.lang.en import English
|
||||
> nlp = English()
|
||||
> doc = nlp("Douglas Adams wrote 'The Hitchhiker's Guide to the Galaxy'.")
|
||||
> candidates = kb.get_candidates(doc[0:2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------- |
|
||||
| `mention` | The textual mention or alias. ~~Span~~ |
|
||||
| **RETURNS** | An iterable of relevant `Candidate` objects. ~~Iterable[Candidate]~~ |
|
||||
|
||||
## InMemoryLookupKB.get_candidates_batch {id="get_candidates_batch",tag="method"}
|
||||
|
||||
Same as [`get_candidates()`](/api/inmemorylookupkb#get_candidates), but for an
|
||||
arbitrary number of mentions. The [`EntityLinker`](/api/entitylinker) component
|
||||
will call `get_candidates_batch()` instead of `get_candidates()`, if the config
|
||||
parameter `candidates_batch_size` is greater or equal than 1.
|
||||
|
||||
The default implementation of `get_candidates_batch()` executes
|
||||
`get_candidates()` in a loop. We recommend implementing a more efficient way to
|
||||
retrieve candidates for multiple mentions at once, if performance is of concern
|
||||
to you.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.lang.en import English
|
||||
> nlp = English()
|
||||
> doc = nlp("Douglas Adams wrote 'The Hitchhiker's Guide to the Galaxy'.")
|
||||
> candidates = kb.get_candidates((doc[0:2], doc[3:]))
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------------- |
|
||||
| `mentions` | The textual mention or alias. ~~Iterable[Span]~~ |
|
||||
| **RETURNS** | An iterable of iterable with relevant `Candidate` objects. ~~Iterable[Iterable[Candidate]]~~ |
|
||||
|
||||
## InMemoryLookupKB.get_alias_candidates {id="get_alias_candidates",tag="method"}
|
||||
|
||||
Given a certain textual mention as input, retrieve a list of candidate entities
|
||||
of type [`Candidate`](/api/kb#candidate).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> candidates = kb.get_alias_candidates("Douglas")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------- |
|
||||
| `alias` | The textual mention or alias. ~~str~~ |
|
||||
| **RETURNS** | The list of relevant `Candidate` objects. ~~List[Candidate]~~ |
|
||||
|
||||
## InMemoryLookupKB.get_vector {id="get_vector",tag="method"}
|
||||
|
||||
Given a certain entity ID, retrieve its pretrained entity vector.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> vector = kb.get_vector("Q42")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------ |
|
||||
| `entity` | The entity ID. ~~str~~ |
|
||||
| **RETURNS** | The entity vector. ~~numpy.ndarray~~ |
|
||||
|
||||
## InMemoryLookupKB.get_vectors {id="get_vectors",tag="method"}
|
||||
|
||||
Same as [`get_vector()`](/api/inmemorylookupkb#get_vector), but for an arbitrary
|
||||
number of entity IDs.
|
||||
|
||||
The default implementation of `get_vectors()` executes `get_vector()` in a loop.
|
||||
We recommend implementing a more efficient way to retrieve vectors for multiple
|
||||
entities at once, if performance is of concern to you.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> vectors = kb.get_vectors(("Q42", "Q3107329"))
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------- |
|
||||
| `entities` | The entity IDs. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The entity vectors. ~~Iterable[Iterable[numpy.ndarray]]~~ |
|
||||
|
||||
## InMemoryLookupKB.get_prior_prob {id="get_prior_prob",tag="method"}
|
||||
|
||||
Given a certain entity ID and a certain textual mention, retrieve the prior
|
||||
probability of the fact that the mention links to the entity ID.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> probability = kb.get_prior_prob("Q42", "Douglas")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------------------- |
|
||||
| `entity` | The entity ID. ~~str~~ |
|
||||
| `alias` | The textual mention or alias. ~~str~~ |
|
||||
| **RETURNS** | The prior probability of the `alias` referring to the `entity`. ~~float~~ |
|
||||
|
||||
## InMemoryLookupKB.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Save the current state of the knowledge base to a directory.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> kb.to_disk(path)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| `exclude` | List of components to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## InMemoryLookupKB.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Restore the state of the knowledge base from a given directory. Note that the
|
||||
[`Vocab`](/api/vocab) should also be the same as the one used to create the KB.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.vocab import Vocab
|
||||
> vocab = Vocab().from_disk("/path/to/vocab")
|
||||
> kb = InMemoryLookupKB(vocab=vocab, entity_vector_length=64)
|
||||
> kb.from_disk("/path/to/kb")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `loc` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| `exclude` | List of components to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `KnowledgeBase` object. ~~KnowledgeBase~~ |
|
||||
@@ -0,0 +1,232 @@
|
||||
---
|
||||
title: KnowledgeBase
|
||||
teaser:
|
||||
A storage class for entities and aliases of a specific knowledge base
|
||||
(ontology)
|
||||
tag: class
|
||||
source: spacy/kb/kb.pyx
|
||||
version: 2.2
|
||||
---
|
||||
|
||||
The `KnowledgeBase` object is an abstract class providing a method to generate
|
||||
[`Candidate`](/api/kb#candidate) objects, which are plausible external
|
||||
identifiers given a certain textual mention. Each such `Candidate` holds
|
||||
information from the relevant KB entities, such as its frequency in text and
|
||||
possible aliases. Each entity in the knowledge base also has a pretrained entity
|
||||
vector of a fixed size.
|
||||
|
||||
Beyond that, `KnowledgeBase` classes have to implement a number of utility
|
||||
functions called by the [`EntityLinker`](/api/entitylinker) component.
|
||||
|
||||
<Infobox variant="warning">
|
||||
|
||||
This class was not abstract up to spaCy version 3.5. The `KnowledgeBase`
|
||||
implementation up to that point is available as
|
||||
[`InMemoryLookupKB`](/api/inmemorylookupkb) from 3.5 onwards.
|
||||
|
||||
</Infobox>
|
||||
|
||||
## KnowledgeBase.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
`KnowledgeBase` is an abstract class and cannot be instantiated. Its child
|
||||
classes should call `__init__()` to set up some necessary attributes.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.kb import KnowledgeBase
|
||||
> from spacy.vocab import Vocab
|
||||
>
|
||||
> class FullyImplementedKB(KnowledgeBase):
|
||||
> def __init__(self, vocab: Vocab, entity_vector_length: int):
|
||||
> super().__init__(vocab, entity_vector_length)
|
||||
> ...
|
||||
> vocab = nlp.vocab
|
||||
> kb = FullyImplementedKB(vocab=vocab, entity_vector_length=64)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------- | ------------------------------------------------ |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `entity_vector_length` | Length of the fixed-size entity vectors. ~~int~~ |
|
||||
|
||||
## KnowledgeBase.entity_vector_length {id="entity_vector_length",tag="property"}
|
||||
|
||||
The length of the fixed-size entity vectors in the knowledge base.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------ |
|
||||
| **RETURNS** | Length of the fixed-size entity vectors. ~~int~~ |
|
||||
|
||||
## KnowledgeBase.get_candidates {id="get_candidates",tag="method"}
|
||||
|
||||
Given a certain textual mention as input, retrieve a list of candidate entities
|
||||
of type [`Candidate`](/api/kb#candidate).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.lang.en import English
|
||||
> nlp = English()
|
||||
> doc = nlp("Douglas Adams wrote 'The Hitchhiker's Guide to the Galaxy'.")
|
||||
> candidates = kb.get_candidates(doc[0:2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------- |
|
||||
| `mention` | The textual mention or alias. ~~Span~~ |
|
||||
| **RETURNS** | An iterable of relevant `Candidate` objects. ~~Iterable[Candidate]~~ |
|
||||
|
||||
## KnowledgeBase.get_candidates_batch {id="get_candidates_batch",tag="method"}
|
||||
|
||||
Same as [`get_candidates()`](/api/kb#get_candidates), but for an arbitrary
|
||||
number of mentions. The [`EntityLinker`](/api/entitylinker) component will call
|
||||
`get_candidates_batch()` instead of `get_candidates()`, if the config parameter
|
||||
`candidates_batch_size` is greater or equal than 1.
|
||||
|
||||
The default implementation of `get_candidates_batch()` executes
|
||||
`get_candidates()` in a loop. We recommend implementing a more efficient way to
|
||||
retrieve candidates for multiple mentions at once, if performance is of concern
|
||||
to you.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.lang.en import English
|
||||
> nlp = English()
|
||||
> doc = nlp("Douglas Adams wrote 'The Hitchhiker's Guide to the Galaxy'.")
|
||||
> candidates = kb.get_candidates((doc[0:2], doc[3:]))
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------------- |
|
||||
| `mentions` | The textual mention or alias. ~~Iterable[Span]~~ |
|
||||
| **RETURNS** | An iterable of iterable with relevant `Candidate` objects. ~~Iterable[Iterable[Candidate]]~~ |
|
||||
|
||||
## KnowledgeBase.get_alias_candidates {id="get_alias_candidates",tag="method"}
|
||||
|
||||
<Infobox variant="warning">
|
||||
This method is _not_ available from spaCy 3.5 onwards.
|
||||
</Infobox>
|
||||
|
||||
From spaCy 3.5 on `KnowledgeBase` is an abstract class (with
|
||||
[`InMemoryLookupKB`](/api/inmemorylookupkb) being a drop-in replacement) to
|
||||
allow more flexibility in customizing knowledge bases. Some of its methods were
|
||||
moved to [`InMemoryLookupKB`](/api/inmemorylookupkb) during this refactoring,
|
||||
one of those being `get_alias_candidates()`. This method is now available as
|
||||
[`InMemoryLookupKB.get_alias_candidates()`](/api/inmemorylookupkb#get_alias_candidates).
|
||||
Note:
|
||||
[`InMemoryLookupKB.get_candidates()`](/api/inmemorylookupkb#get_candidates)
|
||||
defaults to
|
||||
[`InMemoryLookupKB.get_alias_candidates()`](/api/inmemorylookupkb#get_alias_candidates).
|
||||
|
||||
## KnowledgeBase.get_vector {id="get_vector",tag="method"}
|
||||
|
||||
Given a certain entity ID, retrieve its pretrained entity vector.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> vector = kb.get_vector("Q42")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------- |
|
||||
| `entity` | The entity ID. ~~str~~ |
|
||||
| **RETURNS** | The entity vector. ~~Iterable[float]~~ |
|
||||
|
||||
## KnowledgeBase.get_vectors {id="get_vectors",tag="method"}
|
||||
|
||||
Same as [`get_vector()`](/api/kb#get_vector), but for an arbitrary number of
|
||||
entity IDs.
|
||||
|
||||
The default implementation of `get_vectors()` executes `get_vector()` in a loop.
|
||||
We recommend implementing a more efficient way to retrieve vectors for multiple
|
||||
entities at once, if performance is of concern to you.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> vectors = kb.get_vectors(("Q42", "Q3107329"))
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------- |
|
||||
| `entities` | The entity IDs. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The entity vectors. ~~Iterable[Iterable[numpy.ndarray]]~~ |
|
||||
|
||||
## KnowledgeBase.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Save the current state of the knowledge base to a directory.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> kb.to_disk(path)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| `exclude` | List of components to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## KnowledgeBase.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Restore the state of the knowledge base from a given directory. Note that the
|
||||
[`Vocab`](/api/vocab) should also be the same as the one used to create the KB.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.vocab import Vocab
|
||||
> vocab = Vocab().from_disk("/path/to/vocab")
|
||||
> kb = FullyImplementedKB(vocab=vocab, entity_vector_length=64)
|
||||
> kb.from_disk("/path/to/kb")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `loc` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| `exclude` | List of components to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `KnowledgeBase` object. ~~KnowledgeBase~~ |
|
||||
|
||||
## Candidate {id="candidate",tag="class"}
|
||||
|
||||
A `Candidate` object refers to a textual mention (alias) that may or may not be
|
||||
resolved to a specific entity from a `KnowledgeBase`. This will be used as input
|
||||
for the entity linking algorithm which will disambiguate the various candidates
|
||||
to the correct one. Each candidate `(alias, entity)` pair is assigned to a
|
||||
certain prior probability.
|
||||
|
||||
### Candidate.\_\_init\_\_ {id="candidate-init",tag="method"}
|
||||
|
||||
Construct a `Candidate` object. Usually this constructor is not called directly,
|
||||
but instead these objects are returned by the `get_candidates` method of the
|
||||
[`entity_linker`](/api/entitylinker) pipe.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.kb import Candidate
|
||||
> candidate = Candidate(kb, entity_hash, entity_freq, entity_vector, alias_hash, prior_prob)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------- | ------------------------------------------------------------------------- |
|
||||
| `kb` | The knowledge base that defined this candidate. ~~KnowledgeBase~~ |
|
||||
| `entity_hash` | The hash of the entity's KB ID. ~~int~~ |
|
||||
| `entity_freq` | The entity frequency as recorded in the KB. ~~float~~ |
|
||||
| `alias_hash` | The hash of the textual mention or alias. ~~int~~ |
|
||||
| `prior_prob` | The prior probability of the `alias` referring to the `entity`. ~~float~~ |
|
||||
|
||||
## Candidate attributes {id="candidate-attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| --------------- | ------------------------------------------------------------------------ |
|
||||
| `entity` | The entity's unique KB identifier. ~~int~~ |
|
||||
| `entity_` | The entity's unique KB identifier. ~~str~~ |
|
||||
| `alias` | The alias or textual mention. ~~int~~ |
|
||||
| `alias_` | The alias or textual mention. ~~str~~ |
|
||||
| `prior_prob` | The prior probability of the `alias` referring to the `entity`. ~~long~~ |
|
||||
| `entity_freq` | The frequency of the entity in a typical corpus. ~~long~~ |
|
||||
| `entity_vector` | The pretrained vector of the entity. ~~numpy.ndarray~~ |
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,386 @@
|
||||
---
|
||||
title: Legacy functions and architectures
|
||||
teaser: Archived implementations available through spacy-legacy
|
||||
source: spacy/legacy
|
||||
---
|
||||
|
||||
The [`spacy-legacy`](https://github.com/explosion/spacy-legacy) package includes
|
||||
outdated registered functions and architectures. It is installed automatically
|
||||
as a dependency of spaCy, and provides backwards compatibility for archived
|
||||
functions that may still be used in projects.
|
||||
|
||||
You can find the detailed documentation of each such legacy function on this
|
||||
page.
|
||||
|
||||
## Architectures {id="architectures"}
|
||||
|
||||
These functions are available from `@spacy.registry.architectures`.
|
||||
|
||||
### spacy.Tok2Vec.v1 {id="Tok2Vec_v1"}
|
||||
|
||||
The `spacy.Tok2Vec.v1` architecture was expecting an `encode` model of type
|
||||
`Model[Floats2D, Floats2D]` such as `spacy.MaxoutWindowEncoder.v1` or
|
||||
`spacy.MishWindowEncoder.v1`.
|
||||
|
||||
> #### Example config
|
||||
>
|
||||
> ```ini
|
||||
> [model]
|
||||
> @architectures = "spacy.Tok2Vec.v1"
|
||||
>
|
||||
> [model.embed]
|
||||
> @architectures = "spacy.CharacterEmbed.v1"
|
||||
> # ...
|
||||
>
|
||||
> [model.encode]
|
||||
> @architectures = "spacy.MaxoutWindowEncoder.v1"
|
||||
> # ...
|
||||
> ```
|
||||
|
||||
Construct a tok2vec model out of two subnetworks: one for embedding and one for
|
||||
encoding. See the
|
||||
["Embed, Encode, Attend, Predict"](https://explosion.ai/blog/deep-learning-formula-nlp)
|
||||
blog post for background.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `embed` | Embed tokens into context-independent word vector representations. For example, [CharacterEmbed](/api/architectures#CharacterEmbed) or [MultiHashEmbed](/api/architectures#MultiHashEmbed). ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
| `encode` | Encode context into the embeddings, using an architecture such as a CNN, BiLSTM or transformer. For example, [MaxoutWindowEncoder.v1](/api/legacy#MaxoutWindowEncoder_v1). ~~Model[Floats2d, Floats2d]~~ |
|
||||
| **CREATES** | The model using the architecture. ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
|
||||
### spacy.MaxoutWindowEncoder.v1 {id="MaxoutWindowEncoder_v1"}
|
||||
|
||||
The `spacy.MaxoutWindowEncoder.v1` architecture was producing a model of type
|
||||
`Model[Floats2D, Floats2D]`. Since `spacy.MaxoutWindowEncoder.v2`, this has been
|
||||
changed to output type `Model[List[Floats2d], List[Floats2d]]`.
|
||||
|
||||
> #### Example config
|
||||
>
|
||||
> ```ini
|
||||
> [model]
|
||||
> @architectures = "spacy.MaxoutWindowEncoder.v1"
|
||||
> width = 128
|
||||
> window_size = 1
|
||||
> maxout_pieces = 3
|
||||
> depth = 4
|
||||
> ```
|
||||
|
||||
Encode context using convolutions with maxout activation, layer normalization
|
||||
and residual connections.
|
||||
|
||||
| Name | Description |
|
||||
| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `width` | The input and output width. These are required to be the same, to allow residual connections. This value will be determined by the width of the inputs. Recommended values are between `64` and `300`. ~~int~~ |
|
||||
| `window_size` | The number of words to concatenate around each token to construct the convolution. Recommended value is `1`. ~~int~~ |
|
||||
| `maxout_pieces` | The number of maxout pieces to use. Recommended values are `2` or `3`. ~~int~~ |
|
||||
| `depth` | The number of convolutional layers. Recommended value is `4`. ~~int~~ |
|
||||
| **CREATES** | The model using the architecture. ~~Model[Floats2d, Floats2d]~~ |
|
||||
|
||||
### spacy.MishWindowEncoder.v1 {id="MishWindowEncoder_v1"}
|
||||
|
||||
The `spacy.MishWindowEncoder.v1` architecture was producing a model of type
|
||||
`Model[Floats2D, Floats2D]`. Since `spacy.MishWindowEncoder.v2`, this has been
|
||||
changed to output type `Model[List[Floats2d], List[Floats2d]]`.
|
||||
|
||||
> #### Example config
|
||||
>
|
||||
> ```ini
|
||||
> [model]
|
||||
> @architectures = "spacy.MishWindowEncoder.v1"
|
||||
> width = 64
|
||||
> window_size = 1
|
||||
> depth = 4
|
||||
> ```
|
||||
|
||||
Encode context using convolutions with
|
||||
[`Mish`](https://thinc.ai/docs/api-layers#mish) activation, layer normalization
|
||||
and residual connections.
|
||||
|
||||
| Name | Description |
|
||||
| ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `width` | The input and output width. These are required to be the same, to allow residual connections. This value will be determined by the width of the inputs. Recommended values are between `64` and `300`. ~~int~~ |
|
||||
| `window_size` | The number of words to concatenate around each token to construct the convolution. Recommended value is `1`. ~~int~~ |
|
||||
| `depth` | The number of convolutional layers. Recommended value is `4`. ~~int~~ |
|
||||
| **CREATES** | The model using the architecture. ~~Model[Floats2d, Floats2d]~~ |
|
||||
|
||||
### spacy.HashEmbedCNN.v1 {id="HashEmbedCNN_v1"}
|
||||
|
||||
Identical to [`spacy.HashEmbedCNN.v2`](/api/architectures#HashEmbedCNN) except
|
||||
using [`spacy.StaticVectors.v1`](#StaticVectors_v1) if vectors are included.
|
||||
|
||||
### spacy.MultiHashEmbed.v1 {id="MultiHashEmbed_v1"}
|
||||
|
||||
Identical to [`spacy.MultiHashEmbed.v2`](/api/architectures#MultiHashEmbed)
|
||||
except with [`spacy.StaticVectors.v1`](#StaticVectors_v1) if vectors are
|
||||
included.
|
||||
|
||||
### spacy.CharacterEmbed.v1 {id="CharacterEmbed_v1"}
|
||||
|
||||
Identical to [`spacy.CharacterEmbed.v2`](/api/architectures#CharacterEmbed)
|
||||
except using [`spacy.StaticVectors.v1`](#StaticVectors_v1) if vectors are
|
||||
included.
|
||||
|
||||
### spacy.TextCatEnsemble.v1 {id="TextCatEnsemble_v1"}
|
||||
|
||||
The `spacy.TextCatEnsemble.v1` architecture built an internal `tok2vec` and
|
||||
`linear_model`. Since `spacy.TextCatEnsemble.v2`, this has been refactored so
|
||||
that the `TextCatEnsemble` takes these two sublayers as input.
|
||||
|
||||
> #### Example Config
|
||||
>
|
||||
> ```ini
|
||||
> [model]
|
||||
> @architectures = "spacy.TextCatEnsemble.v1"
|
||||
> exclusive_classes = false
|
||||
> pretrained_vectors = null
|
||||
> width = 64
|
||||
> embed_size = 2000
|
||||
> conv_depth = 2
|
||||
> window_size = 1
|
||||
> ngram_size = 1
|
||||
> dropout = null
|
||||
> nO = null
|
||||
> ```
|
||||
|
||||
Stacked ensemble of a bag-of-words model and a neural network model. The neural
|
||||
network has an internal CNN Tok2Vec layer and uses attention.
|
||||
|
||||
| Name | Description |
|
||||
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `exclusive_classes` | Whether or not categories are mutually exclusive. ~~bool~~ |
|
||||
| `pretrained_vectors` | Whether or not pretrained vectors will be used in addition to the feature vectors. ~~bool~~ |
|
||||
| `width` | Output dimension of the feature encoding step. ~~int~~ |
|
||||
| `embed_size` | Input dimension of the feature encoding step. ~~int~~ |
|
||||
| `conv_depth` | Depth of the tok2vec layer. ~~int~~ |
|
||||
| `window_size` | The number of contextual vectors to [concatenate](https://thinc.ai/docs/api-layers#expand_window) from the left and from the right. ~~int~~ |
|
||||
| `ngram_size` | Determines the maximum length of the n-grams in the BOW model. For instance, `ngram_size=3`would give unigram, trigram and bigram features. ~~int~~ |
|
||||
| `dropout` | The dropout rate. ~~float~~ |
|
||||
| `nO` | Output dimension, determined by the number of different labels. If not set, the [`TextCategorizer`](/api/textcategorizer) component will set it when `initialize` is called. ~~Optional[int]~~ |
|
||||
| **CREATES** | The model using the architecture. ~~Model[List[Doc], Floats2d]~~ |
|
||||
|
||||
### spacy.TextCatCNN.v1 {id="TextCatCNN_v1"}
|
||||
|
||||
Since `spacy.TextCatCNN.v2`, this architecture has become resizable, which means
|
||||
that you can add labels to a previously trained textcat. `TextCatCNN` v1 did not
|
||||
yet support that. `TextCatCNN` has been replaced by the more general
|
||||
[`TextCatReduce`](/api/architectures#TextCatReduce) layer. `TextCatCNN` is
|
||||
identical to `TextCatReduce` with `use_reduce_mean=true`,
|
||||
`use_reduce_first=false`, `reduce_last=false` and `use_reduce_max=false`.
|
||||
|
||||
> #### Example Config
|
||||
>
|
||||
> ```ini
|
||||
> [model]
|
||||
> @architectures = "spacy.TextCatCNN.v1"
|
||||
> exclusive_classes = false
|
||||
> nO = null
|
||||
>
|
||||
> [model.tok2vec]
|
||||
> @architectures = "spacy.HashEmbedCNN.v1"
|
||||
> pretrained_vectors = null
|
||||
> width = 96
|
||||
> depth = 4
|
||||
> embed_size = 2000
|
||||
> window_size = 1
|
||||
> maxout_pieces = 3
|
||||
> subword_features = true
|
||||
> ```
|
||||
|
||||
A neural network model where token vectors are calculated using a CNN. The
|
||||
vectors are mean pooled and used as features in a feed-forward network. This
|
||||
architecture is usually less accurate than the ensemble, but runs faster.
|
||||
|
||||
| Name | Description |
|
||||
| ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `exclusive_classes` | Whether or not categories are mutually exclusive. ~~bool~~ |
|
||||
| `tok2vec` | The [`tok2vec`](#tok2vec) layer of the model. ~~Model~~ |
|
||||
| `nO` | Output dimension, determined by the number of different labels. If not set, the [`TextCategorizer`](/api/textcategorizer) component will set it when `initialize` is called. ~~Optional[int]~~ |
|
||||
| **CREATES** | The model using the architecture. ~~Model[List[Doc], Floats2d]~~ |
|
||||
|
||||
### spacy.TextCatCNN.v2 {id="TextCatCNN_v2"}
|
||||
|
||||
> #### Example Config
|
||||
>
|
||||
> ```ini
|
||||
> [model]
|
||||
> @architectures = "spacy.TextCatCNN.v2"
|
||||
> exclusive_classes = false
|
||||
> nO = null
|
||||
>
|
||||
> [model.tok2vec]
|
||||
> @architectures = "spacy.HashEmbedCNN.v2"
|
||||
> pretrained_vectors = null
|
||||
> width = 96
|
||||
> depth = 4
|
||||
> embed_size = 2000
|
||||
> window_size = 1
|
||||
> maxout_pieces = 3
|
||||
> subword_features = true
|
||||
> ```
|
||||
|
||||
A neural network model where token vectors are calculated using a CNN. The
|
||||
vectors are mean pooled and used as features in a feed-forward network. This
|
||||
architecture is usually less accurate than the ensemble, but runs faster.
|
||||
|
||||
`TextCatCNN` has been replaced by the more general
|
||||
[`TextCatReduce`](/api/architectures#TextCatReduce) layer. `TextCatCNN` is
|
||||
identical to `TextCatReduce` with `use_reduce_mean=true`,
|
||||
`use_reduce_first=false`, `reduce_last=false` and `use_reduce_max=false`.
|
||||
|
||||
| Name | Description |
|
||||
| ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `exclusive_classes` | Whether or not categories are mutually exclusive. ~~bool~~ |
|
||||
| `tok2vec` | The [`tok2vec`](#tok2vec) layer of the model. ~~Model~~ |
|
||||
| `nO` | Output dimension, determined by the number of different labels. If not set, the [`TextCategorizer`](/api/textcategorizer) component will set it when `initialize` is called. ~~Optional[int]~~ |
|
||||
| **CREATES** | The model using the architecture. ~~Model[List[Doc], Floats2d]~~ |
|
||||
|
||||
<Accordion title="spacy.TextCatCNN.v1 definition" spaced>
|
||||
|
||||
[TextCatCNN.v1](/api/legacy#TextCatCNN_v1) had the exact same signature, but was
|
||||
not yet resizable. Since v2, new labels can be added to this component, even
|
||||
after training.
|
||||
|
||||
</Accordion>
|
||||
|
||||
### spacy.TextCatBOW.v1 {id="TextCatBOW_v1"}
|
||||
|
||||
Since `spacy.TextCatBOW.v2`, this architecture has become resizable, which means
|
||||
that you can add labels to a previously trained textcat. `TextCatBOW` v1 did not
|
||||
yet support that. Versions of this model before `spacy.TextCatBOW.v3` used an
|
||||
erroneous sparse linear layer that only used a small number of the allocated
|
||||
parameters.
|
||||
|
||||
> #### Example Config
|
||||
>
|
||||
> ```ini
|
||||
> [model]
|
||||
> @architectures = "spacy.TextCatBOW.v1"
|
||||
> exclusive_classes = false
|
||||
> ngram_size = 1
|
||||
> no_output_layer = false
|
||||
> nO = null
|
||||
> ```
|
||||
|
||||
An n-gram "bag-of-words" model. This architecture should run much faster than
|
||||
the others, but may not be as accurate, especially if texts are short.
|
||||
|
||||
| Name | Description |
|
||||
| ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `exclusive_classes` | Whether or not categories are mutually exclusive. ~~bool~~ |
|
||||
| `ngram_size` | Determines the maximum length of the n-grams in the BOW model. For instance, `ngram_size=3` would give unigram, trigram and bigram features. ~~int~~ |
|
||||
| `no_output_layer` | Whether or not to add an output layer to the model (`Softmax` activation if `exclusive_classes` is `True`, else `Logistic`). ~~bool~~ |
|
||||
| `nO` | Output dimension, determined by the number of different labels. If not set, the [`TextCategorizer`](/api/textcategorizer) component will set it when `initialize` is called. ~~Optional[int]~~ |
|
||||
| **CREATES** | The model using the architecture. ~~Model[List[Doc], Floats2d]~~ |
|
||||
|
||||
### spacy.TextCatBOW.v2 {id="TextCatBOW"}
|
||||
|
||||
Versions of this model before `spacy.TextCatBOW.v3` used an erroneous sparse
|
||||
linear layer that only used a small number of the allocated parameters.
|
||||
|
||||
> #### Example Config
|
||||
>
|
||||
> ```ini
|
||||
> [model]
|
||||
> @architectures = "spacy.TextCatBOW.v2"
|
||||
> exclusive_classes = false
|
||||
> ngram_size = 1
|
||||
> no_output_layer = false
|
||||
> nO = null
|
||||
> ```
|
||||
|
||||
An n-gram "bag-of-words" model. This architecture should run much faster than
|
||||
the others, but may not be as accurate, especially if texts are short.
|
||||
|
||||
| Name | Description |
|
||||
| ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `exclusive_classes` | Whether or not categories are mutually exclusive. ~~bool~~ |
|
||||
| `ngram_size` | Determines the maximum length of the n-grams in the BOW model. For instance, `ngram_size=3` would give unigram, trigram and bigram features. ~~int~~ |
|
||||
| `no_output_layer` | Whether or not to add an output layer to the model (`Softmax` activation if `exclusive_classes` is `True`, else `Logistic`). ~~bool~~ |
|
||||
| `nO` | Output dimension, determined by the number of different labels. If not set, the [`TextCategorizer`](/api/textcategorizer) component will set it when `initialize` is called. ~~Optional[int]~~ |
|
||||
| **CREATES** | The model using the architecture. ~~Model[List[Doc], Floats2d]~~ |
|
||||
|
||||
### spacy.TransitionBasedParser.v1 {id="TransitionBasedParser_v1"}
|
||||
|
||||
Identical to
|
||||
[`spacy.TransitionBasedParser.v2`](/api/architectures#TransitionBasedParser)
|
||||
except the `use_upper` was set to `True` by default.
|
||||
|
||||
## Layers {id="layers"}
|
||||
|
||||
These functions are available from `@spacy.registry.layers`.
|
||||
|
||||
### spacy.StaticVectors.v1 {id="StaticVectors_v1"}
|
||||
|
||||
Identical to [`spacy.StaticVectors.v2`](/api/architectures#StaticVectors) except
|
||||
for the handling of tokens without vectors.
|
||||
|
||||
<Infobox title="Bugs for tokens without vectors" variant="warning">
|
||||
|
||||
`spacy.StaticVectors.v1` maps tokens without vectors to the final row in the
|
||||
vectors table, which causes the model predictions to change if new vectors are
|
||||
added to an existing vectors table. See more details in
|
||||
[issue #7662](https://github.com/explosion/spaCy/issues/7662#issuecomment-813925655).
|
||||
|
||||
</Infobox>
|
||||
|
||||
## Loggers {id="loggers"}
|
||||
|
||||
These functions are available from `@spacy.registry.loggers`.
|
||||
|
||||
### spacy.ConsoleLogger.v1 {id="ConsoleLogger_v1"}
|
||||
|
||||
> #### Example config
|
||||
>
|
||||
> ```ini
|
||||
> [training.logger]
|
||||
> @loggers = "spacy.ConsoleLogger.v1"
|
||||
> progress_bar = true
|
||||
> ```
|
||||
|
||||
Writes the results of a training step to the console in a tabular format.
|
||||
|
||||
<Accordion title="Example console output" spaced>
|
||||
|
||||
```bash
|
||||
$ python -m spacy train config.cfg
|
||||
```
|
||||
|
||||
```
|
||||
ℹ Using CPU
|
||||
ℹ Loading config and nlp from: config.cfg
|
||||
ℹ Pipeline: ['tok2vec', 'tagger']
|
||||
ℹ Start training
|
||||
ℹ Training. Initial learn rate: 0.0
|
||||
|
||||
E # LOSS TOK2VEC LOSS TAGGER TAG_ACC SCORE
|
||||
--- ------ ------------ ----------- ------- ------
|
||||
0 0 0.00 86.20 0.22 0.00
|
||||
0 200 3.08 18968.78 34.00 0.34
|
||||
0 400 31.81 22539.06 33.64 0.34
|
||||
0 600 92.13 22794.91 43.80 0.44
|
||||
0 800 183.62 21541.39 56.05 0.56
|
||||
0 1000 352.49 25461.82 65.15 0.65
|
||||
0 1200 422.87 23708.82 71.84 0.72
|
||||
0 1400 601.92 24994.79 76.57 0.77
|
||||
0 1600 662.57 22268.02 80.20 0.80
|
||||
0 1800 1101.50 28413.77 82.56 0.83
|
||||
0 2000 1253.43 28736.36 85.00 0.85
|
||||
0 2200 1411.02 28237.53 87.42 0.87
|
||||
0 2400 1605.35 28439.95 88.70 0.89
|
||||
```
|
||||
|
||||
Note that the cumulative loss keeps increasing within one epoch, but should
|
||||
start decreasing across epochs.
|
||||
|
||||
</Accordion>
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | --------------------------------------------------------- |
|
||||
| `progress_bar` | Whether the logger should print the progress bar ~~bool~~ |
|
||||
|
||||
Logging utilities for spaCy are implemented in the
|
||||
[`spacy-loggers`](https://github.com/explosion/spacy-loggers) repo, and the
|
||||
functions are typically available from `@spacy.registry.loggers`.
|
||||
|
||||
More documentation can be found in that repo's
|
||||
[readme](https://github.com/explosion/spacy-loggers/blob/main/README.md) file.
|
||||
@@ -0,0 +1,328 @@
|
||||
---
|
||||
title: Lemmatizer
|
||||
tag: class
|
||||
source: spacy/pipeline/lemmatizer.py
|
||||
version: 3
|
||||
teaser: 'Pipeline component for lemmatization'
|
||||
api_string_name: lemmatizer
|
||||
api_trainable: false
|
||||
---
|
||||
|
||||
Component for assigning base forms to tokens using rules based on part-of-speech
|
||||
tags, or lookup tables. Different [`Language`](/api/language) subclasses can
|
||||
implement their own lemmatizer components via
|
||||
[language-specific factories](/usage/processing-pipelines#factories-language).
|
||||
The default data used is provided by the
|
||||
[`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data)
|
||||
extension package.
|
||||
|
||||
For a trainable lemmatizer, see [`EditTreeLemmatizer`](/api/edittreelemmatizer).
|
||||
|
||||
<Infobox variant="warning" title="New in v3.0">
|
||||
|
||||
As of v3.0, the `Lemmatizer` is a **standalone pipeline component** that can be
|
||||
added to your pipeline, and not a hidden part of the vocab that runs behind the
|
||||
scenes. This makes it easier to customize how lemmas should be assigned in your
|
||||
pipeline.
|
||||
|
||||
If the lemmatization mode is set to `"rule"`, which requires coarse-grained POS
|
||||
(`Token.pos`) to be assigned, make sure a [`Tagger`](/api/tagger),
|
||||
[`Morphologizer`](/api/morphologizer) or another component assigning POS is
|
||||
available in the pipeline and runs _before_ the lemmatizer.
|
||||
|
||||
</Infobox>
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
Lemmas generated by rules or predicted will be saved to `Token.lemma`.
|
||||
|
||||
| Location | Value |
|
||||
| -------------- | ------------------------- |
|
||||
| `Token.lemma` | The lemma (hash). ~~int~~ |
|
||||
| `Token.lemma_` | The lemma. ~~str~~ |
|
||||
|
||||
## Config and implementation
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config). For examples of the lookups
|
||||
data format used by the lookup and rule-based lemmatizers, see
|
||||
[`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> config = {"mode": "rule"}
|
||||
> nlp.add_pipe("lemmatizer", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `mode` | The lemmatizer mode, e.g. `"lookup"` or `"rule"`. Defaults to `lookup` if no language-specific lemmatizer is available (see the following table). ~~str~~ |
|
||||
| `overwrite` | Whether to overwrite existing lemmas. Defaults to `False`. ~~bool~~ |
|
||||
| `model` | **Not yet implemented:** the model to use. ~~Model~~ |
|
||||
| _keyword-only_ | |
|
||||
| `scorer` | The scoring method. Defaults to [`Scorer.score_token_attr`](/api/scorer#score_token_attr) for the attribute `"lemma"`. ~~Optional[Callable]~~ |
|
||||
|
||||
Many languages specify a default lemmatizer mode other than `lookup` if a better
|
||||
lemmatizer is available. The lemmatizer modes `rule` and `pos_lookup` require
|
||||
[`token.pos`](/api/token) from a previous pipeline component (see example
|
||||
pipeline configurations in the
|
||||
[pretrained pipeline design details](/models#design-cnn)) or rely on third-party
|
||||
libraries (`pymorphy3`).
|
||||
|
||||
| Language | Default Mode |
|
||||
| -------- | ------------ |
|
||||
| `bn` | `rule` |
|
||||
| `ca` | `pos_lookup` |
|
||||
| `el` | `rule` |
|
||||
| `en` | `rule` |
|
||||
| `es` | `rule` |
|
||||
| `fa` | `rule` |
|
||||
| `fr` | `rule` |
|
||||
| `it` | `pos_lookup` |
|
||||
| `mk` | `rule` |
|
||||
| `nb` | `rule` |
|
||||
| `nl` | `rule` |
|
||||
| `pl` | `pos_lookup` |
|
||||
| `ru` | `pymorphy3` |
|
||||
| `sv` | `rule` |
|
||||
| `uk` | `pymorphy3` |
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/lemmatizer.py
|
||||
```
|
||||
|
||||
## Lemmatizer.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe with default model
|
||||
> lemmatizer = nlp.add_pipe("lemmatizer")
|
||||
>
|
||||
> # Construction via add_pipe with custom settings
|
||||
> config = {"mode": "rule", "overwrite": True}
|
||||
> lemmatizer = nlp.add_pipe("lemmatizer", config=config)
|
||||
> ```
|
||||
|
||||
Create a new pipeline instance. In your application, you would normally use a
|
||||
shortcut for this and instantiate the component using its string name and
|
||||
[`nlp.add_pipe`](/api/language#add_pipe).
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | --------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | **Not yet implemented:** The model to use. ~~Model~~ |
|
||||
| `name` | String name of the component instance. Used to add entries to the `losses` during training. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| mode | The lemmatizer mode, e.g. `"lookup"` or `"rule"`. Defaults to `"lookup"`. ~~str~~ |
|
||||
| overwrite | Whether to overwrite existing lemmas. ~~bool~~ |
|
||||
|
||||
## Lemmatizer.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document. The document is modified in place, and returned.
|
||||
This usually happens under the hood when the `nlp` object is called on a text
|
||||
and all pipeline components are applied to the `Doc` in order.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> lemmatizer = nlp.add_pipe("lemmatizer")
|
||||
> # This usually happens under the hood
|
||||
> processed = lemmatizer(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## Lemmatizer.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer = nlp.add_pipe("lemmatizer")
|
||||
> for doc in lemmatizer.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `stream` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## Lemmatizer.initialize {id="initialize",tag="method"}
|
||||
|
||||
Initialize the lemmatizer and load any data resources. This method is typically
|
||||
called by [`Language.initialize`](/api/language#initialize) and lets you
|
||||
customize arguments it receives via the
|
||||
[`[initialize.components]`](/api/data-formats#config-initialize) block in the
|
||||
config. The loading only happens during initialization, typically before
|
||||
training. At runtime, all data is loaded from disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer = nlp.add_pipe("lemmatizer")
|
||||
> lemmatizer.initialize(lookups=lookups)
|
||||
> ```
|
||||
>
|
||||
> ```ini
|
||||
> ### config.cfg
|
||||
> [initialize.components.lemmatizer]
|
||||
>
|
||||
> [initialize.components.lemmatizer.lookups]
|
||||
> @misc = "load_my_lookups.v1"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Defaults to `None`. ~~Optional[Callable[[], Iterable[Example]]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
| `lookups` | The lookups object containing the tables such as `"lemma_rules"`, `"lemma_index"`, `"lemma_exc"` and `"lemma_lookup"`. If `None`, default tables are loaded from [`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data). Defaults to `None`. ~~Optional[Lookups]~~ |
|
||||
|
||||
## Lemmatizer.lookup_lemmatize {id="lookup_lemmatize",tag="method"}
|
||||
|
||||
Lemmatize a token using a lookup-based approach. If no lemma is found, the
|
||||
original string is returned.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------- |
|
||||
| `token` | The token to lemmatize. ~~Token~~ |
|
||||
| **RETURNS** | A list containing one or more lemmas. ~~List[str]~~ |
|
||||
|
||||
## Lemmatizer.rule_lemmatize {id="rule_lemmatize",tag="method"}
|
||||
|
||||
Lemmatize a token using a rule-based approach. Typically relies on POS tags.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------- |
|
||||
| `token` | The token to lemmatize. ~~Token~~ |
|
||||
| **RETURNS** | A list containing one or more lemmas. ~~List[str]~~ |
|
||||
|
||||
## Lemmatizer.is_base_form {id="is_base_form",tag="method"}
|
||||
|
||||
Check whether we're dealing with an uninflected paradigm, so we can avoid
|
||||
lemmatization entirely.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------------------- |
|
||||
| `token` | The token to analyze. ~~Token~~ |
|
||||
| **RETURNS** | Whether the token's attributes (e.g., part-of-speech tag, morphological features) describe a base form. ~~bool~~ |
|
||||
|
||||
## Lemmatizer.get_lookups_config {id="get_lookups_config",tag="classmethod"}
|
||||
|
||||
Returns the lookups configuration settings for a given mode for use in
|
||||
[`Lemmatizer.load_lookups`](/api/lemmatizer#load_lookups).
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------- |
|
||||
| `mode` | The lemmatizer mode. ~~str~~ |
|
||||
| **RETURNS** | The required table names and the optional table names. ~~Tuple[List[str], List[str]]~~ |
|
||||
|
||||
## Lemmatizer.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer = nlp.add_pipe("lemmatizer")
|
||||
> lemmatizer.to_disk("/path/to/lemmatizer")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## Lemmatizer.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer = nlp.add_pipe("lemmatizer")
|
||||
> lemmatizer.from_disk("/path/to/lemmatizer")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `Lemmatizer` object. ~~Lemmatizer~~ |
|
||||
|
||||
## Lemmatizer.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer = nlp.add_pipe("lemmatizer")
|
||||
> lemmatizer_bytes = lemmatizer.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `Lemmatizer` object. ~~bytes~~ |
|
||||
|
||||
## Lemmatizer.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lemmatizer_bytes = lemmatizer.to_bytes()
|
||||
> lemmatizer = nlp.add_pipe("lemmatizer")
|
||||
> lemmatizer.from_bytes(lemmatizer_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `Lemmatizer` object. ~~Lemmatizer~~ |
|
||||
|
||||
## Attributes {id="attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| --------- | ------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). ~~Vocab~~ |
|
||||
| `lookups` | The lookups object. ~~Lookups~~ |
|
||||
| `mode` | The lemmatizer mode. ~~str~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = lemmatizer.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------- | ---------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `lookups` | The lookups. You usually don't want to exclude this. |
|
||||
@@ -0,0 +1,164 @@
|
||||
---
|
||||
title: Lexeme
|
||||
teaser: An entry in the vocabulary
|
||||
tag: class
|
||||
source: spacy/lexeme.pyx
|
||||
---
|
||||
|
||||
A `Lexeme` has no string context – it's a word type, as opposed to a word token.
|
||||
It therefore has no part-of-speech tag, dependency parse, or lemma (if
|
||||
lemmatization depends on the part-of-speech tag).
|
||||
|
||||
## Lexeme.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create a `Lexeme` object.
|
||||
|
||||
| Name | Description |
|
||||
| ------- | ---------------------------------- |
|
||||
| `vocab` | The parent vocabulary. ~~Vocab~~ |
|
||||
| `orth` | The orth id of the lexeme. ~~int~~ |
|
||||
|
||||
## Lexeme.set_flag {id="set_flag",tag="method"}
|
||||
|
||||
Change the value of a boolean flag.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> COOL_FLAG = nlp.vocab.add_flag(lambda text: False)
|
||||
> nlp.vocab["spaCy"].set_flag(COOL_FLAG, True)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------- | -------------------------------------------- |
|
||||
| `flag_id` | The attribute ID of the flag to set. ~~int~~ |
|
||||
| `value` | The new value of the flag. ~~bool~~ |
|
||||
|
||||
## Lexeme.check_flag {id="check_flag",tag="method"}
|
||||
|
||||
Check the value of a boolean flag.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> is_my_library = lambda text: text in ["spaCy", "Thinc"]
|
||||
> MY_LIBRARY = nlp.vocab.add_flag(is_my_library)
|
||||
> assert nlp.vocab["spaCy"].check_flag(MY_LIBRARY) == True
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------- |
|
||||
| `flag_id` | The attribute ID of the flag to query. ~~int~~ |
|
||||
| **RETURNS** | The value of the flag. ~~bool~~ |
|
||||
|
||||
## Lexeme.similarity {id="similarity",tag="method",model="vectors"}
|
||||
|
||||
Compute a semantic similarity estimate. Defaults to cosine over vectors.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> apple = nlp.vocab["apple"]
|
||||
> orange = nlp.vocab["orange"]
|
||||
> apple_orange = apple.similarity(orange)
|
||||
> orange_apple = orange.similarity(apple)
|
||||
> assert apple_orange == orange_apple
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| other | The object to compare with. By default, accepts `Doc`, `Span`, `Token` and `Lexeme` objects. ~~Union[Doc, Span, Token, Lexeme]~~ |
|
||||
| **RETURNS** | A scalar similarity score. Higher is more similar. ~~float~~ |
|
||||
|
||||
## Lexeme.has_vector {id="has_vector",tag="property",model="vectors"}
|
||||
|
||||
A boolean value indicating whether a word vector is associated with the lexeme.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> apple = nlp.vocab["apple"]
|
||||
> assert apple.has_vector
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------- |
|
||||
| **RETURNS** | Whether the lexeme has a vector data attached. ~~bool~~ |
|
||||
|
||||
## Lexeme.vector {id="vector",tag="property",model="vectors"}
|
||||
|
||||
A real-valued meaning representation.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> apple = nlp.vocab["apple"]
|
||||
> assert apple.vector.dtype == "float32"
|
||||
> assert apple.vector.shape == (300,)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------------------------------------------ |
|
||||
| **RETURNS** | A 1-dimensional array representing the lexeme's vector. ~~numpy.ndarray[ndim=1, dtype=float32]~~ |
|
||||
|
||||
## Lexeme.vector_norm {id="vector_norm",tag="property",model="vectors"}
|
||||
|
||||
The L2 norm of the lexeme's vector representation.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> apple = nlp.vocab["apple"]
|
||||
> pasta = nlp.vocab["pasta"]
|
||||
> apple.vector_norm # 7.1346845626831055
|
||||
> pasta.vector_norm # 7.759851932525635
|
||||
> assert apple.vector_norm != pasta.vector_norm
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------- |
|
||||
| **RETURNS** | The L2 norm of the vector representation. ~~float~~ |
|
||||
|
||||
## Attributes {id="attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The lexeme's vocabulary. ~~Vocab~~ |
|
||||
| `text` | Verbatim text content. ~~str~~ |
|
||||
| `orth` | ID of the verbatim text content. ~~int~~ |
|
||||
| `orth_` | Verbatim text content (identical to `Lexeme.text`). Exists mostly for consistency with the other attributes. ~~str~~ |
|
||||
| `rank` | Sequential ID of the lexeme's lexical type, used to index into tables, e.g. for word vectors. ~~int~~ |
|
||||
| `flags` | Container of the lexeme's binary flags. ~~int~~ |
|
||||
| `norm` | The lexeme's norm, i.e. a normalized form of the lexeme text. ~~int~~ |
|
||||
| `norm_` | The lexeme's norm, i.e. a normalized form of the lexeme text. ~~str~~ |
|
||||
| `lower` | Lowercase form of the word. ~~int~~ |
|
||||
| `lower_` | Lowercase form of the word. ~~str~~ |
|
||||
| `shape` | Transform of the word's string, to show orthographic features. Alphabetic characters are replaced by `x` or `X`, and numeric characters are replaced by `d`, and sequences of the same character are truncated after length 4. For example,`"Xxxx"`or`"dd"`. ~~int~~ |
|
||||
| `shape_` | Transform of the word's string, to show orthographic features. Alphabetic characters are replaced by `x` or `X`, and numeric characters are replaced by `d`, and sequences of the same character are truncated after length 4. For example,`"Xxxx"`or`"dd"`. ~~str~~ |
|
||||
| `prefix` | Length-N substring from the start of the word. Defaults to `N=1`. ~~int~~ |
|
||||
| `prefix_` | Length-N substring from the start of the word. Defaults to `N=1`. ~~str~~ |
|
||||
| `suffix` | Length-N substring from the end of the word. Defaults to `N=3`. ~~int~~ |
|
||||
| `suffix_` | Length-N substring from the end of the word. Defaults to `N=3`. ~~str~~ |
|
||||
| `is_alpha` | Does the lexeme consist of alphabetic characters? Equivalent to `lexeme.text.isalpha()`. ~~bool~~ |
|
||||
| `is_ascii` | Does the lexeme consist of ASCII characters? Equivalent to `[any(ord(c) >= 128 for c in lexeme.text)]`. ~~bool~~ |
|
||||
| `is_digit` | Does the lexeme consist of digits? Equivalent to `lexeme.text.isdigit()`. ~~bool~~ |
|
||||
| `is_lower` | Is the lexeme in lowercase? Equivalent to `lexeme.text.islower()`. ~~bool~~ |
|
||||
| `is_upper` | Is the lexeme in uppercase? Equivalent to `lexeme.text.isupper()`. ~~bool~~ |
|
||||
| `is_title` | Is the lexeme in titlecase? Equivalent to `lexeme.text.istitle()`. ~~bool~~ |
|
||||
| `is_punct` | Is the lexeme punctuation? ~~bool~~ |
|
||||
| `is_left_punct` | Is the lexeme a left punctuation mark, e.g. `(`? ~~bool~~ |
|
||||
| `is_right_punct` | Is the lexeme a right punctuation mark, e.g. `)`? ~~bool~~ |
|
||||
| `is_space` | Does the lexeme consist of whitespace characters? Equivalent to `lexeme.text.isspace()`. ~~bool~~ |
|
||||
| `is_bracket` | Is the lexeme a bracket? ~~bool~~ |
|
||||
| `is_quote` | Is the lexeme a quotation mark? ~~bool~~ |
|
||||
| `is_currency` | Is the lexeme a currency symbol? ~~bool~~ |
|
||||
| `like_url` | Does the lexeme resemble a URL? ~~bool~~ |
|
||||
| `like_num` | Does the lexeme represent a number? e.g. "10.9", "10", "ten", etc. ~~bool~~ |
|
||||
| `like_email` | Does the lexeme resemble an email address? ~~bool~~ |
|
||||
| `is_oov` | Is the lexeme out-of-vocabulary (i.e. does it not have a word vector)? ~~bool~~ |
|
||||
| `is_stop` | Is the lexeme part of a "stop list"? ~~bool~~ |
|
||||
| `lang` | Language of the parent vocabulary. ~~int~~ |
|
||||
| `lang_` | Language of the parent vocabulary. ~~str~~ |
|
||||
| `prob` | Smoothed log probability estimate of the lexeme's word type (context-independent entry in the vocabulary). ~~float~~ |
|
||||
| `cluster` | Brown cluster ID. ~~int~~ |
|
||||
| `sentiment` | A scalar value indicating the positivity or negativity of the lexeme. ~~float~~ |
|
||||
@@ -0,0 +1,313 @@
|
||||
---
|
||||
title: Lookups
|
||||
teaser: A container for large lookup tables and dictionaries
|
||||
tag: class
|
||||
source: spacy/lookups.py
|
||||
version: 2.2
|
||||
---
|
||||
|
||||
This class allows convenient access to large lookup tables and dictionaries,
|
||||
e.g. lemmatization data or tokenizer exception lists using Bloom filters.
|
||||
Lookups are available via the [`Vocab`](/api/vocab) as `vocab.lookups`, so they
|
||||
can be accessed before the pipeline components are applied (e.g. in the
|
||||
tokenizer and lemmatizer), as well as within the pipeline components via
|
||||
`doc.vocab.lookups`.
|
||||
|
||||
## Lookups.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create a `Lookups` object.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.lookups import Lookups
|
||||
> lookups = Lookups()
|
||||
> ```
|
||||
|
||||
## Lookups.\_\_len\_\_ {id="len",tag="method"}
|
||||
|
||||
Get the current number of tables in the lookups.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lookups = Lookups()
|
||||
> assert len(lookups) == 0
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------- |
|
||||
| **RETURNS** | The number of tables in the lookups. ~~int~~ |
|
||||
|
||||
## Lookups.\_\_contains\_\_ {id="contains",tag="method"}
|
||||
|
||||
Check if the lookups contain a table of a given name. Delegates to
|
||||
[`Lookups.has_table`](/api/lookups#has_table).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lookups = Lookups()
|
||||
> lookups.add_table("some_table")
|
||||
> assert "some_table" in lookups
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------- |
|
||||
| `name` | Name of the table. ~~str~~ |
|
||||
| **RETURNS** | Whether a table of that name is in the lookups. ~~bool~~ |
|
||||
|
||||
## Lookups.tables {id="tables",tag="property"}
|
||||
|
||||
Get the names of all tables in the lookups.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lookups = Lookups()
|
||||
> lookups.add_table("some_table")
|
||||
> assert lookups.tables == ["some_table"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------- |
|
||||
| **RETURNS** | Names of the tables in the lookups. ~~List[str]~~ |
|
||||
|
||||
## Lookups.add_table {id="add_table",tag="method"}
|
||||
|
||||
Add a new table with optional data to the lookups. Raises an error if the table
|
||||
exists.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lookups = Lookups()
|
||||
> lookups.add_table("some_table", {"foo": "bar"})
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| `name` | Unique name of the table. ~~str~~ |
|
||||
| `data` | Optional data to add to the table. ~~dict~~ |
|
||||
| **RETURNS** | The newly added table. ~~Table~~ |
|
||||
|
||||
## Lookups.get_table {id="get_table",tag="method"}
|
||||
|
||||
Get a table from the lookups. Raises an error if the table doesn't exist.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lookups = Lookups()
|
||||
> lookups.add_table("some_table", {"foo": "bar"})
|
||||
> table = lookups.get_table("some_table")
|
||||
> assert table["foo"] == "bar"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------- |
|
||||
| `name` | Name of the table. ~~str~~ |
|
||||
| **RETURNS** | The table. ~~Table~~ |
|
||||
|
||||
## Lookups.remove_table {id="remove_table",tag="method"}
|
||||
|
||||
Remove a table from the lookups. Raises an error if the table doesn't exist.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lookups = Lookups()
|
||||
> lookups.add_table("some_table")
|
||||
> removed_table = lookups.remove_table("some_table")
|
||||
> assert "some_table" not in lookups
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------ |
|
||||
| `name` | Name of the table to remove. ~~str~~ |
|
||||
| **RETURNS** | The removed table. ~~Table~~ |
|
||||
|
||||
## Lookups.has_table {id="has_table",tag="method"}
|
||||
|
||||
Check if the lookups contain a table of a given name. Equivalent to
|
||||
[`Lookups.__contains__`](/api/lookups#contains).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lookups = Lookups()
|
||||
> lookups.add_table("some_table")
|
||||
> assert lookups.has_table("some_table")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------- |
|
||||
| `name` | Name of the table. ~~str~~ |
|
||||
| **RETURNS** | Whether a table of that name is in the lookups. ~~bool~~ |
|
||||
|
||||
## Lookups.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
Serialize the lookups to a bytestring.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lookup_bytes = lookups.to_bytes()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------- |
|
||||
| **RETURNS** | The serialized lookups. ~~bytes~~ |
|
||||
|
||||
## Lookups.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the lookups from a bytestring.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lookup_bytes = lookups.to_bytes()
|
||||
> lookups = Lookups()
|
||||
> lookups.from_bytes(lookup_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | -------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| **RETURNS** | The loaded lookups. ~~Lookups~~ |
|
||||
|
||||
## Lookups.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Save the lookups to a directory as `lookups.bin`. Expects a path to a directory,
|
||||
which will be created if it doesn't exist.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> lookups.to_disk("/path/to/lookups")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------ | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
|
||||
## Lookups.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load lookups from a directory containing a `lookups.bin`. Will skip loading if
|
||||
the file doesn't exist.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.lookups import Lookups
|
||||
> lookups = Lookups()
|
||||
> lookups.from_disk("/path/to/lookups")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| **RETURNS** | The loaded lookups. ~~Lookups~~ |
|
||||
|
||||
## Table {id="table",tag="class, ordererddict"}
|
||||
|
||||
A table in the lookups. Subclass of `OrderedDict` that implements a slightly
|
||||
more consistent and unified API and includes a Bloom filter to speed up missed
|
||||
lookups. Supports **all other methods and attributes** of `OrderedDict` /
|
||||
`dict`, and the customized methods listed here. Methods that get or set keys
|
||||
accept both integers and strings (which will be hashed before being added to the
|
||||
table).
|
||||
|
||||
### Table.\_\_init\_\_ {id="table.init",tag="method"}
|
||||
|
||||
Initialize a new table.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.lookups import Table
|
||||
> data = {"foo": "bar", "baz": 100}
|
||||
> table = Table(name="some_table", data=data)
|
||||
> assert "foo" in table
|
||||
> assert table["foo"] == "bar"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------ | ------------------------------------------ |
|
||||
| `name` | Optional table name for reference. ~~str~~ |
|
||||
|
||||
### Table.from_dict {id="table.from_dict",tag="classmethod"}
|
||||
|
||||
Initialize a new table from a dict.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.lookups import Table
|
||||
> data = {"foo": "bar", "baz": 100}
|
||||
> table = Table.from_dict(data, name="some_table")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------ |
|
||||
| `data` | The dictionary. ~~dict~~ |
|
||||
| `name` | Optional table name for reference. ~~str~~ |
|
||||
| **RETURNS** | The newly constructed object. ~~Table~~ |
|
||||
|
||||
### Table.set {id="table.set",tag="method"}
|
||||
|
||||
Set a new key / value pair. String keys will be hashed. Same as
|
||||
`table[key] = value`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.lookups import Table
|
||||
> table = Table()
|
||||
> table.set("foo", "bar")
|
||||
> assert table["foo"] == "bar"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | ---------------------------- |
|
||||
| `key` | The key. ~~Union[str, int]~~ |
|
||||
| `value` | The value. |
|
||||
|
||||
### Table.to_bytes {id="table.to_bytes",tag="method"}
|
||||
|
||||
Serialize the table to a bytestring.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> table_bytes = table.to_bytes()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------- |
|
||||
| **RETURNS** | The serialized table. ~~bytes~~ |
|
||||
|
||||
### Table.from_bytes {id="table.from_bytes",tag="method"}
|
||||
|
||||
Load a table from a bytestring.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> table_bytes = table.to_bytes()
|
||||
> table = Table()
|
||||
> table.from_bytes(table_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | --------------------------- |
|
||||
| `bytes_data` | The data to load. ~~bytes~~ |
|
||||
| **RETURNS** | The loaded table. ~~Table~~ |
|
||||
|
||||
### Attributes {id="table-attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `name` | Table name. ~~str~~ |
|
||||
| `default_size` | Default size of bloom filters if no data is provided. ~~int~~ |
|
||||
| `bloom` | The bloom filters. ~~preshed.BloomFilter~~ |
|
||||
@@ -0,0 +1,269 @@
|
||||
---
|
||||
title: Matcher
|
||||
teaser: Match sequences of tokens, based on pattern rules
|
||||
tag: class
|
||||
source: spacy/matcher/matcher.pyx
|
||||
---
|
||||
|
||||
The `Matcher` lets you find words and phrases using rules describing their token
|
||||
attributes. Rules can refer to token annotations (like the text or
|
||||
part-of-speech tags), as well as lexical attributes like `Token.is_punct`.
|
||||
Applying the matcher to a [`Doc`](/api/doc) gives you access to the matched
|
||||
tokens in context. For in-depth examples and workflows for combining rules and
|
||||
statistical models, see the [usage guide](/usage/rule-based-matching) on
|
||||
rule-based matching.
|
||||
|
||||
## Pattern format {id="patterns"}
|
||||
|
||||
> ```json
|
||||
> ### Example
|
||||
> [
|
||||
> {"LOWER": "i"},
|
||||
> {"LEMMA": {"IN": ["like", "love"]}},
|
||||
> {"POS": "NOUN", "OP": "+"}
|
||||
> ]
|
||||
> ```
|
||||
|
||||
A pattern added to the `Matcher` consists of a list of dictionaries. Each
|
||||
dictionary describes **one token** and its attributes. The available token
|
||||
pattern keys correspond to a number of
|
||||
[`Token` attributes](/api/token#attributes). The supported attributes for
|
||||
rule-based matching are:
|
||||
|
||||
| Attribute | Description |
|
||||
| ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `ORTH` | The exact verbatim text of a token. ~~str~~ |
|
||||
| `TEXT` | The exact verbatim text of a token. ~~str~~ |
|
||||
| `NORM` | The normalized form of the token text. ~~str~~ |
|
||||
| `LOWER` | The lowercase form of the token text. ~~str~~ |
|
||||
| `LENGTH` | The length of the token text. ~~int~~ |
|
||||
| `IS_ALPHA`, `IS_ASCII`, `IS_DIGIT` | Token text consists of alphabetic characters, ASCII characters, digits. ~~bool~~ |
|
||||
| `IS_LOWER`, `IS_UPPER`, `IS_TITLE` | Token text is in lowercase, uppercase, titlecase. ~~bool~~ |
|
||||
| `IS_PUNCT`, `IS_SPACE`, `IS_STOP` | Token is punctuation, whitespace, stop word. ~~bool~~ |
|
||||
| `IS_SENT_START` | Token is start of sentence. ~~bool~~ |
|
||||
| `LIKE_NUM`, `LIKE_URL`, `LIKE_EMAIL` | Token text resembles a number, URL, email. ~~bool~~ |
|
||||
| `SPACY` | Token has a trailing space. ~~bool~~ |
|
||||
| `POS`, `TAG`, `MORPH`, `DEP`, `LEMMA`, `SHAPE` | The token's simple and extended part-of-speech tag, morphological analysis, dependency label, lemma, shape. ~~str~~ |
|
||||
| `ENT_TYPE` | The token's entity label. ~~str~~ |
|
||||
| `ENT_IOB` | The IOB part of the token's entity tag. ~~str~~ |
|
||||
| `ENT_ID` | The token's entity ID (`ent_id`). ~~str~~ |
|
||||
| `ENT_KB_ID` | The token's entity knowledge base ID (`ent_kb_id`). ~~str~~ |
|
||||
| `_` | Properties in [custom extension attributes](/usage/processing-pipelines#custom-components-attributes). ~~Dict[str, Any]~~ |
|
||||
| `OP` | Operator or quantifier to determine how often to match a token pattern. ~~str~~ |
|
||||
|
||||
Operators and quantifiers define **how often** a token pattern should be
|
||||
matched:
|
||||
|
||||
> ```json
|
||||
> ### Example
|
||||
> [
|
||||
> {"POS": "ADJ", "OP": "*"},
|
||||
> {"POS": "NOUN", "OP": "+"}
|
||||
> {"POS": "PROPN", "OP": "{2}"}
|
||||
> ]
|
||||
> ```
|
||||
|
||||
| OP | Description |
|
||||
| ------- | ---------------------------------------------------------------------- |
|
||||
| `!` | Negate the pattern, by requiring it to match exactly 0 times. |
|
||||
| `?` | Make the pattern optional, by allowing it to match 0 or 1 times. |
|
||||
| `+` | Require the pattern to match 1 or more times. |
|
||||
| `*` | Allow the pattern to match 0 or more times. |
|
||||
| `{n}` | Require the pattern to match exactly _n_ times. |
|
||||
| `{n,m}` | Require the pattern to match at least _n_ but not more than _m_ times. |
|
||||
| `{n,}` | Require the pattern to match at least _n_ times. |
|
||||
| `{,m}` | Require the pattern to match at most _m_ times. |
|
||||
|
||||
Token patterns can also map to a **dictionary of properties** instead of a
|
||||
single value to indicate whether the expected value is a member of a list or how
|
||||
it compares to another value.
|
||||
|
||||
> ```json
|
||||
> ### Example
|
||||
> [
|
||||
> {"LEMMA": {"IN": ["like", "love", "enjoy"]}},
|
||||
> {"POS": "PROPN", "LENGTH": {">=": 10}},
|
||||
> ]
|
||||
> ```
|
||||
|
||||
| Attribute | Description |
|
||||
| -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `REGEX` | Attribute value matches the regular expression at any position in the string. ~~Any~~ |
|
||||
| `FUZZY` | Attribute value matches if the `fuzzy_compare` method matches for `(value, pattern, -1)`. The default method allows a Levenshtein edit distance of at least 2 and up to 30% of the pattern string length. ~~Any~~ |
|
||||
| `FUZZY1`, `FUZZY2`, ... `FUZZY9` | Attribute value matches if the `fuzzy_compare` method matches for `(value, pattern, N)`. The default method allows a Levenshtein edit distance of at most N (1-9). ~~Any~~ |
|
||||
| `IN` | Attribute value is member of a list. ~~Any~~ |
|
||||
| `NOT_IN` | Attribute value is _not_ member of a list. ~~Any~~ |
|
||||
| `IS_SUBSET` | Attribute value (for `MORPH` or custom list attributes) is a subset of a list. ~~Any~~ |
|
||||
| `IS_SUPERSET` | Attribute value (for `MORPH` or custom list attributes) is a superset of a list. ~~Any~~ |
|
||||
| `INTERSECTS` | Attribute value (for `MORPH` or custom list attribute) has a non-empty intersection with a list. ~~Any~~ |
|
||||
| `==`, `>=`, `<=`, `>`, `<` | Attribute value is equal, greater or equal, smaller or equal, greater or smaller. ~~Union[int, float]~~ |
|
||||
|
||||
As of spaCy v3.5, `REGEX` and `FUZZY` can be used in combination with `IN` and
|
||||
`NOT_IN`.
|
||||
|
||||
## Matcher.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create the rule-based `Matcher`. If `validate=True` is set, all patterns added
|
||||
to the matcher will be validated against a JSON schema and a `MatchPatternError`
|
||||
is raised if problems are found. Those can include incorrect types (e.g. a
|
||||
string where an integer is expected) or unexpected property names.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.matcher import Matcher
|
||||
> matcher = Matcher(nlp.vocab)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------------- | ----------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The vocabulary object, which must be shared with the documents the matcher will operate on. ~~Vocab~~ |
|
||||
| `validate` | Validate all patterns added to this matcher. ~~bool~~ |
|
||||
| `fuzzy_compare` | The comparison method used for the `FUZZY` operators. ~~Callable[[str, str, int], bool]~~ |
|
||||
|
||||
## Matcher.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Find all token sequences matching the supplied patterns on the `Doc` or `Span`.
|
||||
|
||||
Note that if a single label has multiple patterns associated with it, the
|
||||
returned matches don't provide a way to tell which pattern was responsible for
|
||||
the match.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.matcher import Matcher
|
||||
>
|
||||
> matcher = Matcher(nlp.vocab)
|
||||
> pattern = [{"LOWER": "hello"}, {"LOWER": "world"}]
|
||||
> matcher.add("HelloWorld", [pattern])
|
||||
> doc = nlp("hello world!")
|
||||
> matches = matcher(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `doclike` | The `Doc` or `Span` to match over. ~~Union[Doc, Span]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `as_spans` <Tag variant="new">3</Tag> | Instead of tuples, return a list of [`Span`](/api/span) objects of the matches, with the `match_id` assigned as the span label. Defaults to `False`. ~~bool~~ |
|
||||
| `allow_missing` <Tag variant="new">3</Tag> | Whether to skip checks for missing annotation for attributes included in patterns. Defaults to `False`. ~~bool~~ |
|
||||
| `with_alignments` <Tag variant="new">3.0.6</Tag> | Return match alignment information as part of the match tuple as `List[int]` with the same length as the matched span. Each entry denotes the corresponding index of the token in the pattern. If `as_spans` is set to `True`, this setting is ignored. Defaults to `False`. ~~bool~~ |
|
||||
| **RETURNS** | A list of `(match_id, start, end)` tuples, describing the matches. A match tuple describes a span `doc[start:end`]. The `match_id` is the ID of the added match pattern. If `as_spans` is set to `True`, a list of `Span` objects is returned instead. ~~Union[List[Tuple[int, int, int]], List[Span]]~~ |
|
||||
|
||||
## Matcher.\_\_len\_\_ {id="len",tag="method",version="2"}
|
||||
|
||||
Get the number of rules added to the matcher. Note that this only returns the
|
||||
number of rules (identical with the number of IDs), not the number of individual
|
||||
patterns.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> matcher = Matcher(nlp.vocab)
|
||||
> assert len(matcher) == 0
|
||||
> matcher.add("Rule", [[{"ORTH": "test"}]])
|
||||
> assert len(matcher) == 1
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The number of rules. ~~int~~ |
|
||||
|
||||
## Matcher.\_\_contains\_\_ {id="contains",tag="method",version="2"}
|
||||
|
||||
Check whether the matcher contains rules for a match ID.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> matcher = Matcher(nlp.vocab)
|
||||
> assert "Rule" not in matcher
|
||||
> matcher.add("Rule", [[{'ORTH': 'test'}]])
|
||||
> assert "Rule" in matcher
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------- |
|
||||
| `key` | The match ID. ~~str~~ |
|
||||
| **RETURNS** | Whether the matcher contains rules for this match ID. ~~bool~~ |
|
||||
|
||||
## Matcher.add {id="add",tag="method",version="2"}
|
||||
|
||||
Add a rule to the matcher, consisting of an ID key, one or more patterns, and an
|
||||
optional callback function to act on the matches. The callback function will
|
||||
receive the arguments `matcher`, `doc`, `i` and `matches`. If a pattern already
|
||||
exists for the given ID, the patterns will be extended. An `on_match` callback
|
||||
will be overwritten.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> def on_match(matcher, doc, id, matches):
|
||||
> print('Matched!', matches)
|
||||
>
|
||||
> matcher = Matcher(nlp.vocab)
|
||||
> patterns = [
|
||||
> [{"LOWER": "hello"}, {"LOWER": "world"}],
|
||||
> [{"ORTH": "Google"}, {"ORTH": "Maps"}]
|
||||
> ]
|
||||
> matcher.add("TEST_PATTERNS", patterns, on_match=on_match)
|
||||
> doc = nlp("HELLO WORLD on Google Maps.")
|
||||
> matches = matcher(doc)
|
||||
> ```
|
||||
|
||||
<Infobox title="Changed in v3.0" variant="warning">
|
||||
|
||||
As of spaCy v3.0, `Matcher.add` takes a list of patterns as the second argument
|
||||
(instead of a variable number of arguments). The `on_match` callback becomes an
|
||||
optional keyword argument.
|
||||
|
||||
```diff
|
||||
patterns = [[{"TEXT": "Google"}, {"TEXT": "Now"}], [{"TEXT": "GoogleNow"}]]
|
||||
- matcher.add("GoogleNow", on_match, *patterns)
|
||||
+ matcher.add("GoogleNow", patterns, on_match=on_match)
|
||||
```
|
||||
|
||||
</Infobox>
|
||||
|
||||
| Name | Description |
|
||||
| ----------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `match_id` | An ID for the thing you're matching. ~~str~~ |
|
||||
| `patterns` | Match pattern. A pattern consists of a list of dicts, where each dict describes a token. ~~List[List[Dict[str, Any]]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `on_match` | Callback function to act on matches. Takes the arguments `matcher`, `doc`, `i` and `matches`. ~~Optional[Callable[[Matcher, Doc, int, List[tuple], Any]]~~ |
|
||||
| `greedy` <Tag variant="new">3</Tag> | Optional filter for greedy matches. Can either be `"FIRST"` or `"LONGEST"`. ~~Optional[str]~~ |
|
||||
|
||||
## Matcher.remove {id="remove",tag="method",version="2"}
|
||||
|
||||
Remove a rule from the matcher. A `KeyError` is raised if the match ID does not
|
||||
exist.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> matcher.add("Rule", [[{"ORTH": "test"}]])
|
||||
> assert "Rule" in matcher
|
||||
> matcher.remove("Rule")
|
||||
> assert "Rule" not in matcher
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----- | --------------------------------- |
|
||||
| `key` | The ID of the match rule. ~~str~~ |
|
||||
|
||||
## Matcher.get {id="get",tag="method",version="2"}
|
||||
|
||||
Retrieve the pattern stored for a key. Returns the rule as an
|
||||
`(on_match, patterns)` tuple containing the callback and available patterns.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> matcher.add("Rule", [[{"ORTH": "test"}]])
|
||||
> on_match, patterns = matcher.get("Rule")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------------------------------------------- |
|
||||
| `key` | The ID of the match rule. ~~str~~ |
|
||||
| **RETURNS** | The rule, as an `(on_match, patterns)` tuple. ~~Tuple[Optional[Callable], List[List[dict]]]~~ |
|
||||
@@ -0,0 +1,441 @@
|
||||
---
|
||||
title: Morphologizer
|
||||
tag: class
|
||||
source: spacy/pipeline/morphologizer.pyx
|
||||
version: 3
|
||||
teaser: 'Pipeline component for predicting morphological features'
|
||||
api_base_class: /api/tagger
|
||||
api_string_name: morphologizer
|
||||
api_trainable: true
|
||||
---
|
||||
|
||||
A trainable pipeline component to predict morphological features and
|
||||
coarse-grained POS tags following the Universal Dependencies
|
||||
[UPOS](https://universaldependencies.org/u/pos/index.html) and
|
||||
[FEATS](https://universaldependencies.org/format.html#morphological-annotation)
|
||||
annotation guidelines.
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
Predictions are saved to `Token.morph` and `Token.pos`.
|
||||
|
||||
| Location | Value |
|
||||
| ------------- | ----------------------------------------- |
|
||||
| `Token.pos` | The UPOS part of speech (hash). ~~int~~ |
|
||||
| `Token.pos_` | The UPOS part of speech. ~~str~~ |
|
||||
| `Token.morph` | Morphological features. ~~MorphAnalysis~~ |
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config). See the
|
||||
[model architectures](/api/architectures) documentation for details on the
|
||||
architectures and their arguments and hyperparameters.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.pipeline.morphologizer import DEFAULT_MORPH_MODEL
|
||||
> config = {"model": DEFAULT_MORPH_MODEL}
|
||||
> nlp.add_pipe("morphologizer", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| ---------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `model` | The model to use. Defaults to [Tagger](/api/architectures#Tagger). ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
| `overwrite` <Tag variant="new">3.2</Tag> | Whether the values of existing features are overwritten. Defaults to `True`. ~~bool~~ |
|
||||
| `extend` <Tag variant="new">3.2</Tag> | Whether existing feature types (whose values may or may not be overwritten depending on `overwrite`) are preserved. Defaults to `False`. ~~bool~~ |
|
||||
| `scorer` <Tag variant="new">3.2</Tag> | The scoring method. Defaults to [`Scorer.score_token_attr`](/api/scorer#score_token_attr) for the attributes `"pos"` and `"morph"` and [`Scorer.score_token_attr_per_feat`](/api/scorer#score_token_attr_per_feat) for the attribute `"morph"`. ~~Optional[Callable]~~ |
|
||||
| `label_smoothing` <Tag variant="new">3.6</Tag> | [Label smoothing](https://arxiv.org/abs/1906.02629) factor. Defaults to `0.0`. ~~float~~ |
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/morphologizer.pyx
|
||||
```
|
||||
|
||||
## Morphologizer.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create a new pipeline instance. In your application, you would normally use a
|
||||
shortcut for this and instantiate the component using its string name and
|
||||
[`nlp.add_pipe`](/api/language#add_pipe).
|
||||
|
||||
The `overwrite` and `extend` settings determine how existing annotation is
|
||||
handled (with the example for existing annotation `A=B|C=D` + predicted
|
||||
annotation `C=E|X=Y`):
|
||||
|
||||
- `overwrite=True, extend=True`: overwrite values of existing features, add any
|
||||
new features (`A=B|C=D` + `C=E|X=Y` → `A=B|C=E|X=Y`)
|
||||
- `overwrite=True, extend=False`: overwrite completely, removing any existing
|
||||
features (`A=B|C=D` + `C=E|X=Y` → `C=E|X=Y`)
|
||||
- `overwrite=False, extend=True`: keep values of existing features, add any new
|
||||
features (`A=B|C=D` + `C=E|X=Y` → `A=B|C=D|X=Y`)
|
||||
- `overwrite=False, extend=False`: do not modify the existing annotation if set
|
||||
(`A=B|C=D` + `C=E|X=Y` → `A=B|C=D`)
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe with default model
|
||||
> morphologizer = nlp.add_pipe("morphologizer")
|
||||
>
|
||||
> # Construction via create_pipe with custom model
|
||||
> config = {"model": {"@architectures": "my_morphologizer"}}
|
||||
> morphologizer = nlp.add_pipe("morphologizer", config=config)
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy.pipeline import Morphologizer
|
||||
> morphologizer = Morphologizer(nlp.vocab, model)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | The [`Model`](https://thinc.ai/docs/api-model) powering the pipeline component. ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
| `name` | String name of the component instance. Used to add entries to the `losses` during training. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `overwrite` <Tag variant="new">3.2</Tag> | Whether the values of existing features are overwritten. Defaults to `True`. ~~bool~~ |
|
||||
| `extend` <Tag variant="new">3.2</Tag> | Whether existing feature types (whose values may or may not be overwritten depending on `overwrite`) are preserved. Defaults to `False`. ~~bool~~ |
|
||||
| `scorer` <Tag variant="new">3.2</Tag> | The scoring method. Defaults to [`Scorer.score_token_attr`](/api/scorer#score_token_attr) for the attributes `"pos"` and `"morph"` and [`Scorer.score_token_attr_per_feat`](/api/scorer#score_token_attr_per_feat) for the attribute `"morph"`. ~~Optional[Callable]~~ |
|
||||
|
||||
## Morphologizer.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document. The document is modified in place, and returned.
|
||||
This usually happens under the hood when the `nlp` object is called on a text
|
||||
and all pipeline components are applied to the `Doc` in order. Both
|
||||
[`__call__`](/api/morphologizer#call) and [`pipe`](/api/morphologizer#pipe)
|
||||
delegate to the [`predict`](/api/morphologizer#predict) and
|
||||
[`set_annotations`](/api/morphologizer#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> morphologizer = nlp.add_pipe("morphologizer")
|
||||
> # This usually happens under the hood
|
||||
> processed = morphologizer(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## Morphologizer.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order. Both [`__call__`](/api/morphologizer#call) and
|
||||
[`pipe`](/api/morphologizer#pipe) delegate to the
|
||||
[`predict`](/api/morphologizer#predict) and
|
||||
[`set_annotations`](/api/morphologizer#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> morphologizer = nlp.add_pipe("morphologizer")
|
||||
> for doc in morphologizer.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `stream` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## Morphologizer.initialize {id="initialize",tag="method"}
|
||||
|
||||
Initialize the component for training. `get_examples` should be a function that
|
||||
returns an iterable of [`Example`](/api/example) objects. **At least one example
|
||||
should be supplied.** The data examples are used to **initialize the model** of
|
||||
the component and can either be the full training data or a representative
|
||||
sample. Initialization includes validating the network,
|
||||
[inferring missing shapes](https://thinc.ai/docs/usage-models#validation) and
|
||||
setting up the label scheme based on the data. This method is typically called
|
||||
by [`Language.initialize`](/api/language#initialize) and lets you customize
|
||||
arguments it receives via the
|
||||
[`[initialize.components]`](/api/data-formats#config-initialize) block in the
|
||||
config.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> morphologizer = nlp.add_pipe("morphologizer")
|
||||
> morphologizer.initialize(lambda: examples, nlp=nlp)
|
||||
> ```
|
||||
>
|
||||
> ```ini
|
||||
> ### config.cfg
|
||||
> [initialize.components.morphologizer]
|
||||
>
|
||||
> [initialize.components.morphologizer.labels]
|
||||
> @readers = "spacy.read_labels.v1"
|
||||
> path = "corpus/labels/morphologizer.json
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Must contain at least one `Example`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
| `labels` | The label information to add to the component, as provided by the [`label_data`](#label_data) property after initialization. To generate a reusable JSON file from your data, you should run the [`init labels`](/api/cli#init-labels) command. If no labels are provided, the `get_examples` callback is used to extract the labels from the data, which may be a lot slower. ~~Optional[dict]~~ |
|
||||
|
||||
## Morphologizer.predict {id="predict",tag="method"}
|
||||
|
||||
Apply the component's model to a batch of [`Doc`](/api/doc) objects, without
|
||||
modifying them.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> morphologizer = nlp.add_pipe("morphologizer")
|
||||
> scores = morphologizer.predict([doc1, doc2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| `docs` | The documents to predict. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | The model's prediction for each document. |
|
||||
|
||||
## Morphologizer.set_annotations {id="set_annotations",tag="method"}
|
||||
|
||||
Modify a batch of [`Doc`](/api/doc) objects, using pre-computed scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> morphologizer = nlp.add_pipe("morphologizer")
|
||||
> scores = morphologizer.predict([doc1, doc2])
|
||||
> morphologizer.set_annotations([doc1, doc2], scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | ------------------------------------------------------- |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `scores` | The scores to set, produced by `Morphologizer.predict`. |
|
||||
|
||||
## Morphologizer.update {id="update",tag="method"}
|
||||
|
||||
Learn from a batch of [`Example`](/api/example) objects containing the
|
||||
predictions and gold-standard annotations, and update the component's model.
|
||||
Delegates to [`predict`](/api/morphologizer#predict) and
|
||||
[`get_loss`](/api/morphologizer#get_loss).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> morphologizer = nlp.add_pipe("morphologizer")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = morphologizer.update(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## Morphologizer.get_loss {id="get_loss",tag="method"}
|
||||
|
||||
Find the loss and gradient of loss for the batch of documents and their
|
||||
predicted scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> morphologizer = nlp.add_pipe("morphologizer")
|
||||
> scores = morphologizer.predict([eg.predicted for eg in examples])
|
||||
> loss, d_loss = morphologizer.get_loss(examples, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------------------------- |
|
||||
| `examples` | The batch of examples. ~~Iterable[Example]~~ |
|
||||
| `scores` | Scores representing the model's predictions. |
|
||||
| **RETURNS** | The loss and the gradient, i.e. `(loss, gradient)`. ~~Tuple[float, float]~~ |
|
||||
|
||||
## Morphologizer.create_optimizer {id="create_optimizer",tag="method"}
|
||||
|
||||
Create an optimizer for the pipeline component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> morphologizer = nlp.add_pipe("morphologizer")
|
||||
> optimizer = morphologizer.create_optimizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The optimizer. ~~Optimizer~~ |
|
||||
|
||||
## Morphologizer.use_params {id="use_params",tag="method, contextmanager"}
|
||||
|
||||
Modify the pipe's model, to use the given parameter values. At the end of the
|
||||
context, the original parameters are restored.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> morphologizer = nlp.add_pipe("morphologizer")
|
||||
> with morphologizer.use_params(optimizer.averages):
|
||||
> morphologizer.to_disk("/best_model")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------- |
|
||||
| `params` | The parameter values to use in the model. ~~dict~~ |
|
||||
|
||||
## Morphologizer.add_label {id="add_label",tag="method"}
|
||||
|
||||
Add a new label to the pipe. If the `Morphologizer` should set annotations for
|
||||
both `pos` and `morph`, the label should include the UPOS as the feature `POS`.
|
||||
Raises an error if the output dimension is already set, or if the model has
|
||||
already been fully [initialized](#initialize). Note that you don't have to call
|
||||
this method if you provide a **representative data sample** to the
|
||||
[`initialize`](#initialize) method. In this case, all labels found in the sample
|
||||
will be automatically added to the model, and the output dimension will be
|
||||
[inferred](/usage/layers-architectures#thinc-shape-inference) automatically.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> morphologizer = nlp.add_pipe("morphologizer")
|
||||
> morphologizer.add_label("Mood=Ind|POS=VERB|Tense=Past|VerbForm=Fin")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------- |
|
||||
| `label` | The label to add. ~~str~~ |
|
||||
| **RETURNS** | `0` if the label is already present, otherwise `1`. ~~int~~ |
|
||||
|
||||
## Morphologizer.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> morphologizer = nlp.add_pipe("morphologizer")
|
||||
> morphologizer.to_disk("/path/to/morphologizer")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## Morphologizer.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> morphologizer = nlp.add_pipe("morphologizer")
|
||||
> morphologizer.from_disk("/path/to/morphologizer")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `Morphologizer` object. ~~Morphologizer~~ |
|
||||
|
||||
## Morphologizer.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> morphologizer = nlp.add_pipe("morphologizer")
|
||||
> morphologizer_bytes = morphologizer.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `Morphologizer` object. ~~bytes~~ |
|
||||
|
||||
## Morphologizer.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> morphologizer_bytes = morphologizer.to_bytes()
|
||||
> morphologizer = nlp.add_pipe("morphologizer")
|
||||
> morphologizer.from_bytes(morphologizer_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `Morphologizer` object. ~~Morphologizer~~ |
|
||||
|
||||
## Morphologizer.labels {id="labels",tag="property"}
|
||||
|
||||
The labels currently added to the component in the Universal Dependencies
|
||||
[FEATS](https://universaldependencies.org/format.html#morphological-annotation)
|
||||
format. Note that even for a blank component, this will always include the
|
||||
internal empty label `_`. If POS features are used, the labels will include the
|
||||
coarse-grained POS as the feature `POS`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> morphologizer.add_label("Mood=Ind|POS=VERB|Tense=Past|VerbForm=Fin")
|
||||
> assert "Mood=Ind|POS=VERB|Tense=Past|VerbForm=Fin" in morphologizer.labels
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------ |
|
||||
| **RETURNS** | The labels added to the component. ~~Tuple[str, ...]~~ |
|
||||
|
||||
## Morphologizer.label_data {id="label_data",tag="property",version="3"}
|
||||
|
||||
The labels currently added to the component and their internal meta information.
|
||||
This is the data generated by [`init labels`](/api/cli#init-labels) and used by
|
||||
[`Morphologizer.initialize`](/api/morphologizer#initialize) to initialize the
|
||||
model with a pre-defined label set.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> labels = morphologizer.label_data
|
||||
> morphologizer.initialize(lambda: [], nlp=nlp, labels=labels)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------- |
|
||||
| **RETURNS** | The label data added to the component. ~~dict~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = morphologizer.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `cfg` | The config file. You usually don't want to exclude this. |
|
||||
| `model` | The binary model data. You usually don't want to exclude this. |
|
||||
@@ -0,0 +1,256 @@
|
||||
---
|
||||
title: Morphology
|
||||
tag: class
|
||||
source: spacy/morphology.pyx
|
||||
---
|
||||
|
||||
Store the possible morphological analyses for a language, and index them by
|
||||
hash. To save space on each token, tokens only know the hash of their
|
||||
morphological analysis, so queries of morphological attributes are delegated to
|
||||
this class. See [`MorphAnalysis`](/api/morphology#morphanalysis) for the
|
||||
container storing a single morphological analysis.
|
||||
|
||||
## Morphology.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create a `Morphology` object.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.morphology import Morphology
|
||||
>
|
||||
> morphology = Morphology(strings)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------- | --------------------------------- |
|
||||
| `strings` | The string store. ~~StringStore~~ |
|
||||
|
||||
## Morphology.add {id="add",tag="method"}
|
||||
|
||||
Insert a morphological analysis in the morphology table, if not already present.
|
||||
The morphological analysis may be provided in the Universal Dependencies
|
||||
[FEATS](https://universaldependencies.org/format.html#morphological-annotation)
|
||||
format as a string or in the tag map dictionary format. Returns the hash of the
|
||||
new analysis.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> feats = "Feat1=Val1|Feat2=Val2"
|
||||
> hash = nlp.vocab.morphology.add(feats)
|
||||
> assert hash == nlp.vocab.strings[feats]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ------------------------------------------------ |
|
||||
| `features` | The morphological features. ~~Union[Dict, str]~~ |
|
||||
|
||||
## Morphology.get {id="get",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> feats = "Feat1=Val1|Feat2=Val2"
|
||||
> hash = nlp.vocab.morphology.add(feats)
|
||||
> assert nlp.vocab.morphology.get(hash) == feats
|
||||
> ```
|
||||
|
||||
Get the
|
||||
[FEATS](https://universaldependencies.org/format.html#morphological-annotation)
|
||||
string for the hash of the morphological analysis.
|
||||
|
||||
| Name | Description |
|
||||
| ------- | ----------------------------------------------- |
|
||||
| `morph` | The hash of the morphological analysis. ~~int~~ |
|
||||
|
||||
## Morphology.feats_to_dict {id="feats_to_dict",tag="staticmethod"}
|
||||
|
||||
Convert a string
|
||||
[FEATS](https://universaldependencies.org/format.html#morphological-annotation)
|
||||
representation to a dictionary of features and values in the same format as the
|
||||
tag map.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.morphology import Morphology
|
||||
> d = Morphology.feats_to_dict("Feat1=Val1|Feat2=Val2")
|
||||
> assert d == {"Feat1": "Val1", "Feat2": "Val2"}
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `feats` | The morphological features in Universal Dependencies [FEATS](https://universaldependencies.org/format.html#morphological-annotation) format. ~~str~~ |
|
||||
| **RETURNS** | The morphological features as a dictionary. ~~Dict[str, str]~~ |
|
||||
|
||||
## Morphology.dict_to_feats {id="dict_to_feats",tag="staticmethod"}
|
||||
|
||||
Convert a dictionary of features and values to a string
|
||||
[FEATS](https://universaldependencies.org/format.html#morphological-annotation)
|
||||
representation.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.morphology import Morphology
|
||||
> f = Morphology.dict_to_feats({"Feat1": "Val1", "Feat2": "Val2"})
|
||||
> assert f == "Feat1=Val1|Feat2=Val2"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `feats_dict` | The morphological features as a dictionary. ~~Dict[str, str]~~ |
|
||||
| **RETURNS** | The morphological features in Universal Dependencies [FEATS](https://universaldependencies.org/format.html#morphological-annotation) format. ~~str~~ |
|
||||
|
||||
## Attributes {id="attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| ------------- | ------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `FEATURE_SEP` | The [FEATS](https://universaldependencies.org/format.html#morphological-annotation) feature separator. Default is `\|`. ~~str~~ |
|
||||
| `FIELD_SEP` | The [FEATS](https://universaldependencies.org/format.html#morphological-annotation) field separator. Default is `=`. ~~str~~ |
|
||||
| `VALUE_SEP` | The [FEATS](https://universaldependencies.org/format.html#morphological-annotation) value separator. Default is `,`. ~~str~~ |
|
||||
|
||||
## MorphAnalysis {id="morphanalysis",tag="class",source="spacy/tokens/morphanalysis.pyx"}
|
||||
|
||||
Stores a single morphological analysis.
|
||||
|
||||
### MorphAnalysis.\_\_init\_\_ {id="morphanalysis-init",tag="method"}
|
||||
|
||||
Initialize a MorphAnalysis object from a Universal Dependencies
|
||||
[FEATS](https://universaldependencies.org/format.html#morphological-annotation)
|
||||
string or a dictionary of morphological features.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import MorphAnalysis
|
||||
>
|
||||
> feats = "Feat1=Val1|Feat2=Val2"
|
||||
> m = MorphAnalysis(nlp.vocab, feats)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ---------------------------------------------------------- |
|
||||
| `vocab` | The vocab. ~~Vocab~~ |
|
||||
| `features` | The morphological features. ~~Union[Dict[str, str], str]~~ |
|
||||
|
||||
### MorphAnalysis.\_\_contains\_\_ {id="morphanalysis-contains",tag="method"}
|
||||
|
||||
Whether a feature/value pair is in the analysis.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> feats = "Feat1=Val1,Val2|Feat2=Val2"
|
||||
> morph = MorphAnalysis(nlp.vocab, feats)
|
||||
> assert "Feat1=Val1" in morph
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | --------------------------------------------------------------------- |
|
||||
| `feature` | A feature/value pair. ~~str~~ |
|
||||
| **RETURNS** | Whether the feature/value pair is contained in the analysis. ~~bool~~ |
|
||||
|
||||
### MorphAnalysis.\_\_iter\_\_ {id="morphanalysis-iter",tag="method"}
|
||||
|
||||
Iterate over the feature/value pairs in the analysis.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> feats = "Feat1=Val1,Val3|Feat2=Val2"
|
||||
> morph = MorphAnalysis(nlp.vocab, feats)
|
||||
> assert list(morph) == ["Feat1=Va1", "Feat1=Val3", "Feat2=Val2"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | --------------------------------------------- |
|
||||
| **YIELDS** | A feature/value pair in the analysis. ~~str~~ |
|
||||
|
||||
### MorphAnalysis.\_\_len\_\_ {id="morphanalysis-len",tag="method"}
|
||||
|
||||
Returns the number of features in the analysis.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> feats = "Feat1=Val1,Val2|Feat2=Val2"
|
||||
> morph = MorphAnalysis(nlp.vocab, feats)
|
||||
> assert len(morph) == 3
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------- |
|
||||
| **RETURNS** | The number of features in the analysis. ~~int~~ |
|
||||
|
||||
### MorphAnalysis.\_\_str\_\_ {id="morphanalysis-str",tag="method"}
|
||||
|
||||
Returns the morphological analysis in the Universal Dependencies
|
||||
[FEATS](https://universaldependencies.org/format.html#morphological-annotation)
|
||||
string format.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> feats = "Feat1=Val1,Val2|Feat2=Val2"
|
||||
> morph = MorphAnalysis(nlp.vocab, feats)
|
||||
> assert str(morph) == feats
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| **RETURNS** | The analysis in the Universal Dependencies [FEATS](https://universaldependencies.org/format.html#morphological-annotation) format. ~~str~~ |
|
||||
|
||||
### MorphAnalysis.get {id="morphanalysis-get",tag="method"}
|
||||
|
||||
Retrieve values for a feature by field.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> feats = "Feat1=Val1,Val2"
|
||||
> morph = MorphAnalysis(nlp.vocab, feats)
|
||||
> assert morph.get("Feat1") == ["Val1", "Val2"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `field` | The field to retrieve. ~~str~~ |
|
||||
| `default` <Tag variant="new">3.5.3</Tag> | The value to return if the field is not present. If unset or `None`, the default return value is `[]`. ~~Optional[List[str]]~~ |
|
||||
| **RETURNS** | A list of the individual features. ~~List[str]~~ |
|
||||
|
||||
### MorphAnalysis.to_dict {id="morphanalysis-to_dict",tag="method"}
|
||||
|
||||
Produce a dict representation of the analysis, in the same format as the tag
|
||||
map.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> feats = "Feat1=Val1,Val2|Feat2=Val2"
|
||||
> morph = MorphAnalysis(nlp.vocab, feats)
|
||||
> assert morph.to_dict() == {"Feat1": "Val1,Val2", "Feat2": "Val2"}
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------- |
|
||||
| **RETURNS** | The dict representation of the analysis. ~~Dict[str, str]~~ |
|
||||
|
||||
### MorphAnalysis.from_id {id="morphanalysis-from_id",tag="classmethod"}
|
||||
|
||||
Create a morphological analysis from a given hash ID.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> feats = "Feat1=Val1|Feat2=Val2"
|
||||
> hash = nlp.vocab.strings[feats]
|
||||
> morph = MorphAnalysis.from_id(nlp.vocab, hash)
|
||||
> assert str(morph) == feats
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | ---------------------------------------- |
|
||||
| `vocab` | The vocab. ~~Vocab~~ |
|
||||
| `key` | The hash of the features string. ~~int~~ |
|
||||
@@ -0,0 +1,175 @@
|
||||
---
|
||||
title: PhraseMatcher
|
||||
teaser: Match sequences of tokens, based on documents
|
||||
tag: class
|
||||
source: spacy/matcher/phrasematcher.pyx
|
||||
version: 2
|
||||
---
|
||||
|
||||
The `PhraseMatcher` lets you efficiently match large terminology lists. While
|
||||
the [`Matcher`](/api/matcher) lets you match sequences based on lists of token
|
||||
descriptions, the `PhraseMatcher` accepts match patterns in the form of `Doc`
|
||||
objects. See the [usage guide](/usage/rule-based-matching#phrasematcher) for
|
||||
examples.
|
||||
|
||||
## PhraseMatcher.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create the rule-based `PhraseMatcher`. Setting a different `attr` to match on
|
||||
will change the token attributes that will be compared to determine a match. By
|
||||
default, the incoming `Doc` is checked for sequences of tokens with the same
|
||||
`ORTH` value, i.e. the verbatim token text. Matching on the attribute `LOWER`
|
||||
will result in case-insensitive matching, since only the lowercase token texts
|
||||
are compared. In theory, it's also possible to match on sequences of the same
|
||||
part-of-speech tags or dependency labels.
|
||||
|
||||
If `validate=True` is set, additional validation is performed when pattern are
|
||||
added. At the moment, it will check whether a `Doc` has attributes assigned that
|
||||
aren't necessary to produce the matches (for example, part-of-speech tags if the
|
||||
`PhraseMatcher` matches on the token text). Since this can often lead to
|
||||
significantly worse performance when creating the pattern, a `UserWarning` will
|
||||
be shown.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.matcher import PhraseMatcher
|
||||
> matcher = PhraseMatcher(nlp.vocab)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ------------------------------------------------------------------------------------------------------ |
|
||||
| `vocab` | The vocabulary object, which must be shared with the documents the matcher will operate on. ~~Vocab~~ |
|
||||
| `attr` | The token attribute to match on. Defaults to `ORTH`, i.e. the verbatim token text. ~~Union[int, str]~~ |
|
||||
| `validate` | Validate patterns added to the matcher. ~~bool~~ |
|
||||
|
||||
## PhraseMatcher.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Find all token sequences matching the supplied patterns on the `Doc` or `Span`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.matcher import PhraseMatcher
|
||||
>
|
||||
> matcher = PhraseMatcher(nlp.vocab)
|
||||
> matcher.add("OBAMA", [nlp("Barack Obama")])
|
||||
> doc = nlp("Barack Obama lifts America one last time in emotional farewell")
|
||||
> matches = matcher(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `doclike` | The `Doc` or `Span` to match over. ~~Union[Doc, Span]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `as_spans` <Tag variant="new">3</Tag> | Instead of tuples, return a list of [`Span`](/api/span) objects of the matches, with the `match_id` assigned as the span label. Defaults to `False`. ~~bool~~ |
|
||||
| **RETURNS** | A list of `(match_id, start, end)` tuples, describing the matches. A match tuple describes a span `doc[start:end`]. The `match_id` is the ID of the added match pattern. If `as_spans` is set to `True`, a list of `Span` objects is returned instead. ~~Union[List[Tuple[int, int, int]], List[Span]]~~ |
|
||||
|
||||
<Infobox title="Note on retrieving the string representation of the match_id" variant="warning">
|
||||
|
||||
Because spaCy stores all strings as integers, the `match_id` you get back will
|
||||
be an integer, too – but you can always get the string representation by looking
|
||||
it up in the vocabulary's `StringStore`, i.e. `nlp.vocab.strings`:
|
||||
|
||||
```python
|
||||
match_id_string = nlp.vocab.strings[match_id]
|
||||
```
|
||||
|
||||
</Infobox>
|
||||
|
||||
## PhraseMatcher.\_\_len\_\_ {id="len",tag="method"}
|
||||
|
||||
Get the number of rules added to the matcher. Note that this only returns the
|
||||
number of rules (identical with the number of IDs), not the number of individual
|
||||
patterns.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> matcher = PhraseMatcher(nlp.vocab)
|
||||
> assert len(matcher) == 0
|
||||
> matcher.add("OBAMA", [nlp("Barack Obama")])
|
||||
> assert len(matcher) == 1
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The number of rules. ~~int~~ |
|
||||
|
||||
## PhraseMatcher.\_\_contains\_\_ {id="contains",tag="method"}
|
||||
|
||||
Check whether the matcher contains rules for a match ID.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> matcher = PhraseMatcher(nlp.vocab)
|
||||
> assert "OBAMA" not in matcher
|
||||
> matcher.add("OBAMA", [nlp("Barack Obama")])
|
||||
> assert "OBAMA" in matcher
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------- |
|
||||
| `key` | The match ID. ~~str~~ |
|
||||
| **RETURNS** | Whether the matcher contains rules for this match ID. ~~bool~~ |
|
||||
|
||||
## PhraseMatcher.add {id="add",tag="method"}
|
||||
|
||||
Add a rule to the matcher, consisting of an ID key, one or more patterns, and a
|
||||
callback function to act on the matches. The callback function will receive the
|
||||
arguments `matcher`, `doc`, `i` and `matches`. If a pattern already exists for
|
||||
the given ID, the patterns will be extended. An `on_match` callback will be
|
||||
overwritten.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> def on_match(matcher, doc, id, matches):
|
||||
> print('Matched!', matches)
|
||||
>
|
||||
> matcher = PhraseMatcher(nlp.vocab)
|
||||
> matcher.add("OBAMA", [nlp("Barack Obama")], on_match=on_match)
|
||||
> matcher.add("HEALTH", [nlp("health care reform"), nlp("healthcare reform")], on_match=on_match)
|
||||
> doc = nlp("Barack Obama urges Congress to find courage to defend his healthcare reforms")
|
||||
> matches = matcher(doc)
|
||||
> ```
|
||||
|
||||
<Infobox title="Changed in v3.0" variant="warning">
|
||||
|
||||
As of spaCy v3.0, `PhraseMatcher.add` takes a list of patterns as the second
|
||||
argument (instead of a variable number of arguments). The `on_match` callback
|
||||
becomes an optional keyword argument.
|
||||
|
||||
```diff
|
||||
patterns = [nlp("health care reform"), nlp("healthcare reform")]
|
||||
- matcher.add("HEALTH", on_match, *patterns)
|
||||
+ matcher.add("HEALTH", patterns, on_match=on_match)
|
||||
```
|
||||
|
||||
</Infobox>
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `key` | An ID for the thing you're matching. ~~str~~ |
|
||||
| `docs` | `Doc` objects of the phrases to match. ~~List[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `on_match` | Callback function to act on matches. Takes the arguments `matcher`, `doc`, `i` and `matches`. ~~Optional[Callable[[Matcher, Doc, int, List[tuple], Any]]~~ |
|
||||
|
||||
## PhraseMatcher.remove {id="remove",tag="method",version="2.2"}
|
||||
|
||||
Remove a rule from the matcher by match ID. A `KeyError` is raised if the key
|
||||
does not exist.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> matcher = PhraseMatcher(nlp.vocab)
|
||||
> matcher.add("OBAMA", [nlp("Barack Obama")])
|
||||
> assert "OBAMA" in matcher
|
||||
> matcher.remove("OBAMA")
|
||||
> assert "OBAMA" not in matcher
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----- | --------------------------------- |
|
||||
| `key` | The ID of the match rule. ~~str~~ |
|
||||
@@ -0,0 +1,539 @@
|
||||
---
|
||||
title: TrainablePipe
|
||||
tag: class
|
||||
teaser: Base class for trainable pipeline components
|
||||
---
|
||||
|
||||
This class is a base class and **not instantiated directly**. Trainable pipeline
|
||||
components like the [`EntityRecognizer`](/api/entityrecognizer) or
|
||||
[`TextCategorizer`](/api/textcategorizer) inherit from it and it defines the
|
||||
interface that components should follow to function as trainable components in a
|
||||
spaCy pipeline. See the docs on
|
||||
[writing trainable components](/usage/processing-pipelines#trainable-components)
|
||||
for how to use the `TrainablePipe` base class to implement custom components.
|
||||
|
||||
{/* TODO: Pipe vs TrainablePipe, check methods below (all renamed to TrainablePipe for now) */}
|
||||
|
||||
> #### Why is it implemented in Cython?
|
||||
>
|
||||
> The `TrainablePipe` class is implemented in a `.pyx` module, the extension
|
||||
> used by [Cython](/api/cython). This is needed so that **other** Cython
|
||||
> classes, like the [`EntityRecognizer`](/api/entityrecognizer) can inherit from
|
||||
> it. But it doesn't mean you have to implement trainable components in Cython –
|
||||
> pure Python components like the [`TextCategorizer`](/api/textcategorizer) can
|
||||
> also inherit from `TrainablePipe`.
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/trainable_pipe.pyx
|
||||
```
|
||||
|
||||
## TrainablePipe.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.pipeline import TrainablePipe
|
||||
> from spacy.language import Language
|
||||
>
|
||||
> class CustomPipe(TrainablePipe):
|
||||
> ...
|
||||
>
|
||||
> @Language.factory("your_custom_pipe", default_config={"model": MODEL})
|
||||
> def make_custom_pipe(nlp, name, model):
|
||||
> return CustomPipe(nlp.vocab, model, name)
|
||||
> ```
|
||||
|
||||
Create a new pipeline instance. In your application, you would normally use a
|
||||
shortcut for this and instantiate the component using its string name and
|
||||
[`nlp.add_pipe`](/api/language#create_pipe).
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | The Thinc [`Model`](https://thinc.ai/docs/api-model) powering the pipeline component. ~~Model[List[Doc], Any]~~ |
|
||||
| `name` | String name of the component instance. Used to add entries to the `losses` during training. ~~str~~ |
|
||||
| `**cfg` | Additional config parameters and settings. Will be available as the dictionary `cfg` and is serialized with the component. |
|
||||
|
||||
## TrainablePipe.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document. The document is modified in place, and returned.
|
||||
This usually happens under the hood when the `nlp` object is called on a text
|
||||
and all pipeline components are applied to the `Doc` in order. Both
|
||||
[`__call__`](/api/pipe#call) and [`pipe`](/api/pipe#pipe) delegate to the
|
||||
[`predict`](/api/pipe#predict) and
|
||||
[`set_annotations`](/api/pipe#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> pipe = nlp.add_pipe("your_custom_pipe")
|
||||
> # This usually happens under the hood
|
||||
> processed = pipe(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## TrainablePipe.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order. Both [`__call__`](/api/pipe#call) and
|
||||
[`pipe`](/api/pipe#pipe) delegate to the [`predict`](/api/pipe#predict) and
|
||||
[`set_annotations`](/api/pipe#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> pipe = nlp.add_pipe("your_custom_pipe")
|
||||
> for doc in pipe.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `stream` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## TrainablePipe.set_error_handler {id="set_error_handler",tag="method",version="3"}
|
||||
|
||||
Define a callback that will be invoked when an error is thrown during processing
|
||||
of one or more documents with either [`__call__`](/api/pipe#call) or
|
||||
[`pipe`](/api/pipe#pipe). The error handler will be invoked with the original
|
||||
component's name, the component itself, the list of documents that was being
|
||||
processed, and the original error.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> def warn_error(proc_name, proc, docs, e):
|
||||
> print(f"An error occurred when applying component {proc_name}.")
|
||||
>
|
||||
> pipe = nlp.add_pipe("ner")
|
||||
> pipe.set_error_handler(warn_error)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------------- | -------------------------------------------------------------------------------------------------------------- |
|
||||
| `error_handler` | A function that performs custom error handling. ~~Callable[[str, Callable[[Doc], Doc], List[Doc], Exception]~~ |
|
||||
|
||||
## TrainablePipe.get_error_handler {id="get_error_handler",tag="method",version="3"}
|
||||
|
||||
Retrieve the callback that performs error handling for this component's
|
||||
[`__call__`](/api/pipe#call) and [`pipe`](/api/pipe#pipe) methods. If no custom
|
||||
function was previously defined with
|
||||
[`set_error_handler`](/api/pipe#set_error_handler), a default function is
|
||||
returned that simply reraises the exception.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> pipe = nlp.add_pipe("ner")
|
||||
> error_handler = pipe.get_error_handler()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------------------- |
|
||||
| **RETURNS** | The function that performs custom error handling. ~~Callable[[str, Callable[[Doc], Doc], List[Doc], Exception]~~ |
|
||||
|
||||
## TrainablePipe.initialize {id="initialize",tag="method",version="3"}
|
||||
|
||||
Initialize the component for training. `get_examples` should be a function that
|
||||
returns an iterable of [`Example`](/api/example) objects. The data examples are
|
||||
used to **initialize the model** of the component and can either be the full
|
||||
training data or a representative sample. Initialization includes validating the
|
||||
network,
|
||||
[inferring missing shapes](https://thinc.ai/docs/usage-models#validation) and
|
||||
setting up the label scheme based on the data. This method is typically called
|
||||
by [`Language.initialize`](/api/language#initialize).
|
||||
|
||||
<Infobox variant="warning" title="Changed in v3.0" id="begin_training">
|
||||
|
||||
This method was previously called `begin_training`.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> pipe = nlp.add_pipe("your_custom_pipe")
|
||||
> pipe.initialize(lambda: [], pipeline=nlp.pipeline)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
|
||||
## TrainablePipe.predict {id="predict",tag="method"}
|
||||
|
||||
Apply the component's model to a batch of [`Doc`](/api/doc) objects, without
|
||||
modifying them.
|
||||
|
||||
<Infobox variant="danger">
|
||||
|
||||
This method needs to be overwritten with your own custom `predict` method.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> pipe = nlp.add_pipe("your_custom_pipe")
|
||||
> scores = pipe.predict([doc1, doc2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| `docs` | The documents to predict. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | The model's prediction for each document. |
|
||||
|
||||
## TrainablePipe.set_annotations {id="set_annotations",tag="method"}
|
||||
|
||||
Modify a batch of [`Doc`](/api/doc) objects, using pre-computed scores.
|
||||
|
||||
<Infobox variant="danger">
|
||||
|
||||
This method needs to be overwritten with your own custom `set_annotations`
|
||||
method.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> pipe = nlp.add_pipe("your_custom_pipe")
|
||||
> scores = pipe.predict(docs)
|
||||
> pipe.set_annotations(docs, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | ------------------------------------------------ |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `scores` | The scores to set, produced by `Tagger.predict`. |
|
||||
|
||||
## TrainablePipe.update {id="update",tag="method"}
|
||||
|
||||
Learn from a batch of [`Example`](/api/example) objects containing the
|
||||
predictions and gold-standard annotations, and update the component's model.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> pipe = nlp.add_pipe("your_custom_pipe")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = pipe.update(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## TrainablePipe.rehearse {id="rehearse",tag="method,experimental",version="3"}
|
||||
|
||||
Perform a "rehearsal" update from a batch of data. Rehearsal updates teach the
|
||||
current model to make predictions similar to an initial model, to try to address
|
||||
the "catastrophic forgetting" problem. This feature is experimental.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> pipe = nlp.add_pipe("your_custom_pipe")
|
||||
> optimizer = nlp.resume_training()
|
||||
> losses = pipe.rehearse(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## TrainablePipe.get_loss {id="get_loss",tag="method"}
|
||||
|
||||
Find the loss and gradient of loss for the batch of documents and their
|
||||
predicted scores.
|
||||
|
||||
<Infobox variant="danger">
|
||||
|
||||
This method needs to be overwritten with your own custom `get_loss` method.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ner = nlp.add_pipe("ner")
|
||||
> scores = ner.predict([eg.predicted for eg in examples])
|
||||
> loss, d_loss = ner.get_loss(examples, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------------------------- |
|
||||
| `examples` | The batch of examples. ~~Iterable[Example]~~ |
|
||||
| `scores` | Scores representing the model's predictions. |
|
||||
| **RETURNS** | The loss and the gradient, i.e. `(loss, gradient)`. ~~Tuple[float, float]~~ |
|
||||
|
||||
## TrainablePipe.score {id="score",tag="method",version="3"}
|
||||
|
||||
Score a batch of examples.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> scores = pipe.score(examples)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------- |
|
||||
| `examples` | The examples to score. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ |
|
||||
| `\*\*kwargs` | Any additional settings to pass on to the scorer. ~~Any~~ |
|
||||
| **RETURNS** | The scores, e.g. produced by the [`Scorer`](/api/scorer). ~~Dict[str, Union[float, Dict[str, float]]]~~ |
|
||||
|
||||
## TrainablePipe.create_optimizer {id="create_optimizer",tag="method"}
|
||||
|
||||
Create an optimizer for the pipeline component. Defaults to
|
||||
[`Adam`](https://thinc.ai/docs/api-optimizers#adam) with default settings.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> pipe = nlp.add_pipe("your_custom_pipe")
|
||||
> optimizer = pipe.create_optimizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The optimizer. ~~Optimizer~~ |
|
||||
|
||||
## TrainablePipe.use_params {id="use_params",tag="method, contextmanager"}
|
||||
|
||||
Modify the pipe's model, to use the given parameter values. At the end of the
|
||||
context, the original parameters are restored.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> pipe = nlp.add_pipe("your_custom_pipe")
|
||||
> with pipe.use_params(optimizer.averages):
|
||||
> pipe.to_disk("/best_model")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------- |
|
||||
| `params` | The parameter values to use in the model. ~~dict~~ |
|
||||
|
||||
## TrainablePipe.finish_update {id="finish_update",tag="method"}
|
||||
|
||||
Update parameters using the current parameter gradients. Defaults to calling
|
||||
[`self.model.finish_update`](https://thinc.ai/docs/api-model#finish_update).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> pipe = nlp.add_pipe("your_custom_pipe")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = pipe.update(examples, sgd=None)
|
||||
> pipe.finish_update(sgd)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----- | ------------------------------------- |
|
||||
| `sgd` | An optimizer. ~~Optional[Optimizer]~~ |
|
||||
|
||||
## TrainablePipe.add_label {id="add_label",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> pipe = nlp.add_pipe("your_custom_pipe")
|
||||
> pipe.add_label("MY_LABEL")
|
||||
> ```
|
||||
|
||||
Add a new label to the pipe, to be predicted by the model. The actual
|
||||
implementation depends on the specific component, but in general `add_label`
|
||||
shouldn't be called if the output dimension is already set, or if the model has
|
||||
already been fully [initialized](#initialize). If these conditions are violated,
|
||||
the function will raise an Error. The exception to this rule is when the
|
||||
component is [resizable](#is_resizable), in which case
|
||||
[`set_output`](#set_output) should be called to ensure that the model is
|
||||
properly resized.
|
||||
|
||||
<Infobox variant="danger">
|
||||
|
||||
This method needs to be overwritten with your own custom `add_label` method.
|
||||
|
||||
</Infobox>
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------- |
|
||||
| `label` | The label to add. ~~str~~ |
|
||||
| **RETURNS** | 0 if the label is already present, otherwise 1. ~~int~~ |
|
||||
|
||||
Note that in general, you don't have to call `pipe.add_label` if you provide a
|
||||
representative data sample to the [`initialize`](#initialize) method. In this
|
||||
case, all labels found in the sample will be automatically added to the model,
|
||||
and the output dimension will be
|
||||
[inferred](/usage/layers-architectures#thinc-shape-inference) automatically.
|
||||
|
||||
## TrainablePipe.is_resizable {id="is_resizable",tag="property"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> can_resize = pipe.is_resizable
|
||||
> ```
|
||||
>
|
||||
> With custom resizing implemented by a component:
|
||||
>
|
||||
> ```python
|
||||
> def custom_resize(model, new_nO):
|
||||
> # adjust model
|
||||
> return model
|
||||
>
|
||||
> custom_model.attrs["resize_output"] = custom_resize
|
||||
> ```
|
||||
|
||||
Check whether or not the output dimension of the component's model can be
|
||||
resized. If this method returns `True`, [`set_output`](#set_output) can be
|
||||
called to change the model's output dimension.
|
||||
|
||||
For built-in components that are not resizable, you have to create and train a
|
||||
new model from scratch with the appropriate architecture and output dimension.
|
||||
For custom components, you can implement a `resize_output` function and add it
|
||||
as an attribute to the component's model.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------- |
|
||||
| **RETURNS** | Whether or not the output dimension of the model can be changed after initialization. ~~bool~~ |
|
||||
|
||||
## TrainablePipe.set_output {id="set_output",tag="method"}
|
||||
|
||||
Change the output dimension of the component's model. If the component is not
|
||||
[resizable](#is_resizable), this method will raise a `NotImplementedError`. If a
|
||||
component is resizable, the model's attribute `resize_output` will be called.
|
||||
This is a function that takes the original model and the new output dimension
|
||||
`nO`, and changes the model in place. When resizing an already trained model,
|
||||
care should be taken to avoid the "catastrophic forgetting" problem.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> if pipe.is_resizable:
|
||||
> pipe.set_output(512)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---- | --------------------------------- |
|
||||
| `nO` | The new output dimension. ~~int~~ |
|
||||
|
||||
## TrainablePipe.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> pipe = nlp.add_pipe("your_custom_pipe")
|
||||
> pipe.to_disk("/path/to/pipe")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## TrainablePipe.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> pipe = nlp.add_pipe("your_custom_pipe")
|
||||
> pipe.from_disk("/path/to/pipe")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified pipe. ~~TrainablePipe~~ |
|
||||
|
||||
## TrainablePipe.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> pipe = nlp.add_pipe("your_custom_pipe")
|
||||
> pipe_bytes = pipe.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the pipe. ~~bytes~~ |
|
||||
|
||||
## TrainablePipe.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> pipe_bytes = pipe.to_bytes()
|
||||
> pipe = nlp.add_pipe("your_custom_pipe")
|
||||
> pipe.from_bytes(pipe_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The pipe. ~~TrainablePipe~~ |
|
||||
|
||||
## Attributes {id="attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| ------- | --------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary that's passed in on initialization. ~~Vocab~~ |
|
||||
| `model` | The model powering the component. ~~Model[List[Doc], Any]~~ |
|
||||
| `name` | The name of the component instance in the pipeline. Can be used in the losses. ~~str~~ |
|
||||
| `cfg` | Keyword arguments passed to [`TrainablePipe.__init__`](/api/pipe#init). Will be serialized with the component. ~~Dict[str, Any]~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = pipe.to_disk("/path")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------- |
|
||||
| `cfg` | The config file. You usually don't want to exclude this. |
|
||||
| `model` | The binary model data. You usually don't want to exclude this. |
|
||||
@@ -0,0 +1,188 @@
|
||||
---
|
||||
title: Pipeline Functions
|
||||
teaser: Other built-in pipeline components and helpers
|
||||
source: spacy/pipeline/functions.py
|
||||
menu:
|
||||
- ['merge_noun_chunks', 'merge_noun_chunks']
|
||||
- ['merge_entities', 'merge_entities']
|
||||
- ['merge_subtokens', 'merge_subtokens']
|
||||
- ['token_splitter', 'token_splitter']
|
||||
- ['doc_cleaner', 'doc_cleaner']
|
||||
---
|
||||
|
||||
## merge_noun_chunks {id="merge_noun_chunks",tag="function"}
|
||||
|
||||
Merge noun chunks into a single token. Also available via the string name
|
||||
`"merge_noun_chunks"`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> texts = [t.text for t in nlp("I have a blue car")]
|
||||
> assert texts == ["I", "have", "a", "blue", "car"]
|
||||
>
|
||||
> nlp.add_pipe("merge_noun_chunks")
|
||||
> texts = [t.text for t in nlp("I have a blue car")]
|
||||
> assert texts == ["I", "have", "a blue car"]
|
||||
> ```
|
||||
|
||||
<Infobox variant="warning">
|
||||
|
||||
Since noun chunks require part-of-speech tags and the dependency parse, make
|
||||
sure to add this component _after_ the `"tagger"` and `"parser"` components. By
|
||||
default, `nlp.add_pipe` will add components to the end of the pipeline and after
|
||||
all other components.
|
||||
|
||||
</Infobox>
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------- |
|
||||
| `doc` | The `Doc` object to process, e.g. the `Doc` in the pipeline. ~~Doc~~ |
|
||||
| **RETURNS** | The modified `Doc` with merged noun chunks. ~~Doc~~ |
|
||||
|
||||
## merge_entities {id="merge_entities",tag="function"}
|
||||
|
||||
Merge named entities into a single token. Also available via the string name
|
||||
`"merge_entities"`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> texts = [t.text for t in nlp("I like David Bowie")]
|
||||
> assert texts == ["I", "like", "David", "Bowie"]
|
||||
>
|
||||
> nlp.add_pipe("merge_entities")
|
||||
>
|
||||
> texts = [t.text for t in nlp("I like David Bowie")]
|
||||
> assert texts == ["I", "like", "David Bowie"]
|
||||
> ```
|
||||
|
||||
<Infobox variant="warning">
|
||||
|
||||
Since named entities are set by the entity recognizer, make sure to add this
|
||||
component _after_ the `"ner"` component. By default, `nlp.add_pipe` will add
|
||||
components to the end of the pipeline and after all other components.
|
||||
|
||||
</Infobox>
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------- |
|
||||
| `doc` | The `Doc` object to process, e.g. the `Doc` in the pipeline. ~~Doc~~ |
|
||||
| **RETURNS** | The modified `Doc` with merged entities. ~~Doc~~ |
|
||||
|
||||
## merge_subtokens {id="merge_subtokens",tag="function",version="2.1"}
|
||||
|
||||
Merge subtokens into a single token. Also available via the string name
|
||||
`"merge_subtokens"`. As of v2.1, the parser is able to predict "subtokens" that
|
||||
should be merged into one single token later on. This is especially relevant for
|
||||
languages like Chinese, Japanese or Korean, where a "word" isn't defined as a
|
||||
whitespace-delimited sequence of characters. Under the hood, this component uses
|
||||
the [`Matcher`](/api/matcher) to find sequences of tokens with the dependency
|
||||
label `"subtok"` and then merges them into a single token.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> Note that this example assumes a custom Chinese model that oversegments and
|
||||
> was trained to predict subtokens.
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("拜托")
|
||||
> print([(token.text, token.dep_) for token in doc])
|
||||
> # [('拜', 'subtok'), ('托', 'subtok')]
|
||||
>
|
||||
> nlp.add_pipe("merge_subtokens")
|
||||
> doc = nlp("拜托")
|
||||
> print([token.text for token in doc])
|
||||
> # ['拜托']
|
||||
> ```
|
||||
|
||||
<Infobox variant="warning">
|
||||
|
||||
Since subtokens are set by the parser, make sure to add this component _after_
|
||||
the `"parser"` component. By default, `nlp.add_pipe` will add components to the
|
||||
end of the pipeline and after all other components.
|
||||
|
||||
</Infobox>
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------- |
|
||||
| `doc` | The `Doc` object to process, e.g. the `Doc` in the pipeline. ~~Doc~~ |
|
||||
| `label` | The subtoken dependency label. Defaults to `"subtok"`. ~~str~~ |
|
||||
| **RETURNS** | The modified `Doc` with merged subtokens. ~~Doc~~ |
|
||||
|
||||
## token_splitter {id="token_splitter",tag="function",version="3.0"}
|
||||
|
||||
Split tokens longer than a minimum length into shorter tokens. Intended for use
|
||||
with transformer pipelines where long spaCy tokens lead to input text that
|
||||
exceed the transformer model max length.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> config = {"min_length": 20, "split_length": 5}
|
||||
> nlp.add_pipe("token_splitter", config=config, first=True)
|
||||
> doc = nlp("aaaaabbbbbcccccdddddee")
|
||||
> print([token.text for token in doc])
|
||||
> # ['aaaaa', 'bbbbb', 'ccccc', 'ddddd', 'ee']
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| -------------- | --------------------------------------------------------------------- |
|
||||
| `min_length` | The minimum length for a token to be split. Defaults to `25`. ~~int~~ |
|
||||
| `split_length` | The length of the split tokens. Defaults to `5`. ~~int~~ |
|
||||
| **RETURNS** | The modified `Doc` with the split tokens. ~~Doc~~ |
|
||||
|
||||
## doc_cleaner {id="doc_cleaner",tag="function",version="3.2.1"}
|
||||
|
||||
Clean up `Doc` attributes. Intended for use at the end of pipelines with
|
||||
`tok2vec` or `transformer` pipeline components that store tensors and other
|
||||
values that can require a lot of memory and frequently aren't needed after the
|
||||
whole pipeline has run.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> config = {"attrs": {"tensor": None}}
|
||||
> nlp.add_pipe("doc_cleaner", config=config)
|
||||
> doc = nlp("text")
|
||||
> assert doc.tensor is None
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `attrs` | A dict of the `Doc` attributes and the values to set them to. Defaults to `{"tensor": None, "_.trf_data": None}` to clean up after `tok2vec` and `transformer` components. ~~dict~~ |
|
||||
| `silent` | If `False`, show warnings if attributes aren't found or can't be set. Defaults to `True`. ~~bool~~ |
|
||||
| **RETURNS** | The modified `Doc` with the modified attributes. ~~Doc~~ |
|
||||
|
||||
## span_cleaner {id="span_cleaner",tag="function,experimental"}
|
||||
|
||||
Remove `SpanGroup`s from `doc.spans` based on a key prefix. This is used to
|
||||
clean up after the [`CoreferenceResolver`](/api/coref) when it's paired with a
|
||||
[`SpanResolver`](/api/span-resolver).
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
This pipeline function is not yet integrated into spaCy core, and is available
|
||||
via the extension package
|
||||
[`spacy-experimental`](https://github.com/explosion/spacy-experimental) starting
|
||||
in version 0.6.0. It exposes the component via
|
||||
[entry points](/usage/saving-loading/#entry-points), so if you have the package
|
||||
installed, using `factory = "span_cleaner"` in your
|
||||
[training config](/usage/training#config) or `nlp.add_pipe("span_cleaner")` will
|
||||
work out-of-the-box.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> config = {"prefix": "coref_head_clusters"}
|
||||
> nlp.add_pipe("span_cleaner", config=config)
|
||||
> doc = nlp("text")
|
||||
> assert "coref_head_clusters_1" not in doc.spans
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| ----------- | ------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `prefix` | A prefix to check `SpanGroup` keys for. Any matching groups will be removed. Defaults to `"coref_head_clusters"`. ~~str~~ |
|
||||
| **RETURNS** | The modified `Doc` with any matching spans removed. ~~Doc~~ |
|
||||
@@ -0,0 +1,334 @@
|
||||
---
|
||||
title: Scorer
|
||||
teaser: Compute evaluation scores
|
||||
tag: class
|
||||
source: spacy/scorer.py
|
||||
---
|
||||
|
||||
The `Scorer` computes evaluation scores. It's typically created by
|
||||
[`Language.evaluate`](/api/language#evaluate). In addition, the `Scorer`
|
||||
provides a number of evaluation methods for evaluating [`Token`](/api/token) and
|
||||
[`Doc`](/api/doc) attributes.
|
||||
|
||||
## Scorer.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create a new `Scorer`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.scorer import Scorer
|
||||
>
|
||||
> # Default scoring pipeline
|
||||
> scorer = Scorer()
|
||||
>
|
||||
> # Provided scoring pipeline
|
||||
> nlp = spacy.load("en_core_web_sm")
|
||||
> scorer = Scorer(nlp)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `nlp` | The pipeline to use for scoring, where each pipeline component may provide a scoring method. If none is provided, then a default pipeline is constructed using the `default_lang` and `default_pipeline` settings. ~~Optional[Language]~~ |
|
||||
| `default_lang` | The language to use for a default pipeline if `nlp` is not provided. Defaults to `xx`. ~~str~~ |
|
||||
| `default_pipeline` | The pipeline components to use for a default pipeline if `nlp` is not provided. Defaults to `("senter", "tagger", "morphologizer", "parser", "ner", "textcat")`. ~~Iterable[string]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `**kwargs` | Any additional settings to pass on to the individual scoring methods. ~~Any~~ |
|
||||
|
||||
## Scorer.score {id="score",tag="method"}
|
||||
|
||||
Calculate the scores for a list of [`Example`](/api/example) objects using the
|
||||
scoring methods provided by the components in the pipeline.
|
||||
|
||||
The returned `Dict` contains the scores provided by the individual pipeline
|
||||
components. For the scoring methods provided by the `Scorer` and used by the
|
||||
core pipeline components, the individual score names start with the `Token` or
|
||||
`Doc` attribute being scored:
|
||||
|
||||
- `token_acc`, `token_p`, `token_r`, `token_f`
|
||||
- `sents_p`, `sents_r`, `sents_f`
|
||||
- `tag_acc`
|
||||
- `pos_acc`
|
||||
- `morph_acc`, `morph_micro_p`, `morph_micro_r`, `morph_micro_f`,
|
||||
`morph_per_feat`
|
||||
- `lemma_acc`
|
||||
- `dep_uas`, `dep_las`, `dep_las_per_type`
|
||||
- `ents_p`, `ents_r` `ents_f`, `ents_per_type`
|
||||
- `spans_sc_p`, `spans_sc_r`, `spans_sc_f`
|
||||
- `cats_score` (depends on config, description provided in `cats_score_desc`),
|
||||
`cats_micro_p`, `cats_micro_r`, `cats_micro_f`, `cats_macro_p`,
|
||||
`cats_macro_r`, `cats_macro_f`, `cats_macro_auc`, `cats_f_per_type`,
|
||||
`cats_auc_per_type`
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> scorer = Scorer()
|
||||
> scores = scorer.score(examples)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
|
||||
| `examples` | The `Example` objects holding both the predictions and the correct gold-standard annotations. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `per_component` <Tag variant="new">3.6</Tag> | Whether to return the scores keyed by component name. Defaults to `False`. ~~bool~~ |
|
||||
| **RETURNS** | A dictionary of scores. ~~Dict[str, Union[float, Dict[str, float]]]~~ |
|
||||
|
||||
## Scorer.score_tokenization {id="score_tokenization",tag="staticmethod",version="3"}
|
||||
|
||||
Scores the tokenization:
|
||||
|
||||
- `token_acc`: number of correct tokens / number of predicted tokens
|
||||
- `token_p`, `token_r`, `token_f`: precision, recall and F-score for token
|
||||
character spans
|
||||
|
||||
Docs with `has_unknown_spaces` are skipped during scoring.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> scores = Scorer.score_tokenization(examples)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | The `Example` objects holding both the predictions and the correct gold-standard annotations. ~~Iterable[Example]~~ |
|
||||
| **RETURNS** | `Dict` | A dictionary containing the scores `token_acc`, `token_p`, `token_r`, `token_f`. ~~Dict[str, float]]~~ |
|
||||
|
||||
## Scorer.score_token_attr {id="score_token_attr",tag="staticmethod",version="3"}
|
||||
|
||||
Scores a single token attribute. Tokens with missing values in the reference doc
|
||||
are skipped during scoring.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> scores = Scorer.score_token_attr(examples, "pos")
|
||||
> print(scores["pos_acc"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `examples` | The `Example` objects holding both the predictions and the correct gold-standard annotations. ~~Iterable[Example]~~ |
|
||||
| `attr` | The attribute to score. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `getter` | Defaults to `getattr`. If provided, `getter(token, attr)` should return the value of the attribute for an individual `Token`. ~~Callable[[Token, str], Any]~~ |
|
||||
| `missing_values` | Attribute values to treat as missing annotation in the reference annotation. Defaults to `{0, None, ""}`. ~~Set[Any]~~ |
|
||||
| **RETURNS** | A dictionary containing the score `{attr}_acc`. ~~Dict[str, float]~~ |
|
||||
|
||||
## Scorer.score_token_attr_per_feat {id="score_token_attr_per_feat",tag="staticmethod",version="3"}
|
||||
|
||||
Scores a single token attribute per feature for a token attribute in the
|
||||
Universal Dependencies
|
||||
[FEATS](https://universaldependencies.org/format.html#morphological-annotation)
|
||||
format. Tokens with missing values in the reference doc are skipped during
|
||||
scoring.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> scores = Scorer.score_token_attr_per_feat(examples, "morph")
|
||||
> print(scores["morph_per_feat"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `examples` | The `Example` objects holding both the predictions and the correct gold-standard annotations. ~~Iterable[Example]~~ |
|
||||
| `attr` | The attribute to score. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `getter` | Defaults to `getattr`. If provided, `getter(token, attr)` should return the value of the attribute for an individual `Token`. ~~Callable[[Token, str], Any]~~ |
|
||||
| `missing_values` | Attribute values to treat as missing annotation in the reference annotation. Defaults to `{0, None, ""}`. ~~Set[Any]~~ |
|
||||
| **RETURNS** | A dictionary containing the micro PRF scores under the key `{attr}_micro_p/r/f` and the per-feature PRF scores under `{attr}_per_feat`. ~~Dict[str, Dict[str, float]]~~ |
|
||||
|
||||
## Scorer.score_spans {id="score_spans",tag="staticmethod",version="3"}
|
||||
|
||||
Returns PRF scores for labeled or unlabeled spans.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> scores = Scorer.score_spans(examples, "ents")
|
||||
> print(scores["ents_f"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `examples` | The `Example` objects holding both the predictions and the correct gold-standard annotations. ~~Iterable[Example]~~ |
|
||||
| `attr` | The attribute to score. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `getter` | Defaults to `getattr`. If provided, `getter(doc, attr)` should return the `Span` objects for an individual `Doc`. ~~Callable[[Doc, str], Iterable[Span]]~~ |
|
||||
| `has_annotation` | Defaults to `None`. If provided, `has_annotation(doc)` should return whether a `Doc` has annotation for this `attr`. Docs without annotation are skipped for scoring purposes. ~~str~~ |
|
||||
| `labeled` | Defaults to `True`. If set to `False`, two spans will be considered equal if their start and end match, irrespective of their label. ~~bool~~ |
|
||||
| `allow_overlap` | Defaults to `False`. Whether or not to allow overlapping spans. If set to `False`, the alignment will automatically resolve conflicts. ~~bool~~ |
|
||||
| **RETURNS** | A dictionary containing the PRF scores under the keys `{attr}_p`, `{attr}_r`, `{attr}_f` and the per-type PRF scores under `{attr}_per_type`. ~~Dict[str, Union[float, Dict[str, float]]]~~ |
|
||||
|
||||
## Scorer.score_deps {id="score_deps",tag="staticmethod",version="3"}
|
||||
|
||||
Calculate the UAS, LAS, and LAS per type scores for dependency parses. Tokens
|
||||
with missing values for the `attr` (typically `dep`) are skipped during scoring.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> def dep_getter(token, attr):
|
||||
> dep = getattr(token, attr)
|
||||
> dep = token.vocab.strings.as_string(dep).lower()
|
||||
> return dep
|
||||
>
|
||||
> scores = Scorer.score_deps(
|
||||
> examples,
|
||||
> "dep",
|
||||
> getter=dep_getter,
|
||||
> ignore_labels=("p", "punct")
|
||||
> )
|
||||
> print(scores["dep_uas"], scores["dep_las"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `examples` | The `Example` objects holding both the predictions and the correct gold-standard annotations. ~~Iterable[Example]~~ |
|
||||
| `attr` | The attribute to score. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `getter` | Defaults to `getattr`. If provided, `getter(token, attr)` should return the value of the attribute for an individual `Token`. ~~Callable[[Token, str], Any]~~ |
|
||||
| `head_attr` | The attribute containing the head token. ~~str~~ |
|
||||
| `head_getter` | Defaults to `getattr`. If provided, `head_getter(token, attr)` should return the head for an individual `Token`. ~~Callable[[Doc, str], Token]~~ |
|
||||
| `ignore_labels` | Labels to ignore while scoring (e.g. `"punct"`). ~~Iterable[str]~~ |
|
||||
| `missing_values` | Attribute values to treat as missing annotation in the reference annotation. Defaults to `{0, None, ""}`. ~~Set[Any]~~ |
|
||||
| **RETURNS** | A dictionary containing the scores: `{attr}_uas`, `{attr}_las`, and `{attr}_las_per_type`. ~~Dict[str, Union[float, Dict[str, float]]]~~ |
|
||||
|
||||
## Scorer.score_cats {id="score_cats",tag="staticmethod",version="3"}
|
||||
|
||||
Calculate PRF and ROC AUC scores for a doc-level attribute that is a dict
|
||||
containing scores for each label like `Doc.cats`. The returned dictionary
|
||||
contains the following scores:
|
||||
|
||||
- `{attr}_micro_p`, `{attr}_micro_r` and `{attr}_micro_f`: each instance across
|
||||
each label is weighted equally
|
||||
- `{attr}_macro_p`, `{attr}_macro_r` and `{attr}_macro_f`: the average values
|
||||
across evaluations per label
|
||||
- `{attr}_f_per_type` and `{attr}_auc_per_type`: each contains a dictionary of
|
||||
scores, keyed by label
|
||||
- A final `{attr}_score` and corresponding `{attr}_score_desc` (text
|
||||
description)
|
||||
|
||||
The reported `{attr}_score` depends on the classification properties:
|
||||
|
||||
- **binary exclusive with positive label:** `{attr}_score` is set to the F-score
|
||||
of the positive label
|
||||
- **3+ exclusive classes**, macro-averaged F-score:
|
||||
`{attr}_score = {attr}_macro_f`
|
||||
- **multilabel**, macro-averaged AUC: `{attr}_score = {attr}_macro_auc`
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> labels = ["LABEL_A", "LABEL_B", "LABEL_C"]
|
||||
> scores = Scorer.score_cats(
|
||||
> examples,
|
||||
> "cats",
|
||||
> labels=labels
|
||||
> )
|
||||
> print(scores["cats_macro_auc"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `examples` | The `Example` objects holding both the predictions and the correct gold-standard annotations. ~~Iterable[Example]~~ |
|
||||
| `attr` | The attribute to score. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `getter` | Defaults to `getattr`. If provided, `getter(doc, attr)` should return the cats for an individual `Doc`. ~~Callable[[Doc, str], Dict[str, float]]~~ |
|
||||
| labels | The set of possible labels. Defaults to `[]`. ~~Iterable[str]~~ |
|
||||
| `multi_label` | Whether the attribute allows multiple labels. Defaults to `True`. When set to `False` (exclusive labels), missing gold labels are interpreted as `0.0` and the threshold is set to `0.0`. ~~bool~~ |
|
||||
| `positive_label` | The positive label for a binary task with exclusive classes. Defaults to `None`. ~~Optional[str]~~ |
|
||||
| `threshold` | Cutoff to consider a prediction "positive". Defaults to `0.5` for multi-label, and `0.0` (i.e. whatever's highest scoring) otherwise. ~~float~~ |
|
||||
| **RETURNS** | A dictionary containing the scores, with inapplicable scores as `None`. ~~Dict[str, Optional[float]]~~ |
|
||||
|
||||
## Scorer.score_links {id="score_links",tag="staticmethod",version="3"}
|
||||
|
||||
Returns PRF for predicted links on the entity level. To disentangle the
|
||||
performance of the NEL from the NER, this method only evaluates NEL links for
|
||||
entities that overlap between the gold reference and the predictions.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> scores = Scorer.score_links(
|
||||
> examples,
|
||||
> negative_labels=["NIL", ""]
|
||||
> )
|
||||
> print(scores["nel_micro_f"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------------- | ------------------------------------------------------------------------------------------------------------------- |
|
||||
| `examples` | The `Example` objects holding both the predictions and the correct gold-standard annotations. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `negative_labels` | The string values that refer to no annotation (e.g. "NIL"). ~~Iterable[str]~~ |
|
||||
| **RETURNS** | A dictionary containing the scores. ~~Dict[str, Optional[float]]~~ |
|
||||
|
||||
## get_ner_prf {id="get_ner_prf",version="3"}
|
||||
|
||||
Compute micro-PRF and per-entity PRF scores.
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ------------------------------------------------------------------------------------------------------------------- |
|
||||
| `examples` | The `Example` objects holding both the predictions and the correct gold-standard annotations. ~~Iterable[Example]~~ |
|
||||
|
||||
## score_coref_clusters {id="score_coref_clusters",tag="experimental"}
|
||||
|
||||
Returns LEA ([Moosavi and Strube, 2016](https://aclanthology.org/P16-1060/)) PRF
|
||||
scores for coreference clusters.
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
Note this scoring function is not yet included in spaCy core - for details, see
|
||||
the [CoreferenceResolver](/api/coref) docs.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> scores = score_coref_clusters(
|
||||
> examples,
|
||||
> span_cluster_prefix="coref_clusters",
|
||||
> )
|
||||
> print(scores["coref_f"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------------------- | ------------------------------------------------------------------------------------------------------------------- |
|
||||
| `examples` | The `Example` objects holding both the predictions and the correct gold-standard annotations. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `span_cluster_prefix` | The prefix used for spans representing coreference clusters. ~~str~~ |
|
||||
| **RETURNS** | A dictionary containing the scores. ~~Dict[str, Optional[float]]~~ |
|
||||
|
||||
## score_span_predictions {id="score_span_predictions",tag="experimental"}
|
||||
|
||||
Return accuracy for reconstructions of spans from single tokens. Only exactly
|
||||
correct predictions are counted as correct, there is no partial credit for near
|
||||
answers. Used by the [SpanResolver](/api/span-resolver).
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
Note this scoring function is not yet included in spaCy core - for details, see
|
||||
the [SpanResolver](/api/span-resolver) docs.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> scores = score_span_predictions(
|
||||
> examples,
|
||||
> output_prefix="coref_clusters",
|
||||
> )
|
||||
> print(scores["span_coref_clusters_accuracy"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------------- | ------------------------------------------------------------------------------------------------------------------- |
|
||||
| `examples` | The `Example` objects holding both the predictions and the correct gold-standard annotations. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `output_prefix` | The prefix used for spans representing the final predicted spans. ~~str~~ |
|
||||
| **RETURNS** | A dictionary containing the scores. ~~Dict[str, Optional[float]]~~ |
|
||||
@@ -0,0 +1,375 @@
|
||||
---
|
||||
title: SentenceRecognizer
|
||||
tag: class
|
||||
source: spacy/pipeline/senter.pyx
|
||||
version: 3
|
||||
teaser: 'Pipeline component for sentence segmentation'
|
||||
api_base_class: /api/tagger
|
||||
api_string_name: senter
|
||||
api_trainable: true
|
||||
---
|
||||
|
||||
A trainable pipeline component for sentence segmentation. For a simpler,
|
||||
rule-based strategy, see the [`Sentencizer`](/api/sentencizer).
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
Predicted values will be assigned to `Token.is_sent_start`. The resulting
|
||||
sentences can be accessed using `Doc.sents`.
|
||||
|
||||
| Location | Value |
|
||||
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `Token.is_sent_start` | A boolean value indicating whether the token starts a sentence. This will be either `True` or `False` for all tokens. ~~bool~~ |
|
||||
| `Doc.sents` | An iterator over sentences in the `Doc`, determined by `Token.is_sent_start` values. ~~Iterator[Span]~~ |
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config). See the
|
||||
[model architectures](/api/architectures) documentation for details on the
|
||||
architectures and their arguments and hyperparameters.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.pipeline.senter import DEFAULT_SENTER_MODEL
|
||||
> config = {"model": DEFAULT_SENTER_MODEL,}
|
||||
> nlp.add_pipe("senter", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| ---------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `model` | The [`Model`](https://thinc.ai/docs/api-model) powering the pipeline component. Defaults to [Tagger](/api/architectures#Tagger). ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
| `overwrite` <Tag variant="new">3.2</Tag> | Whether existing annotation is overwritten. Defaults to `False`. ~~bool~~ |
|
||||
| `scorer` <Tag variant="new">3.2</Tag> | The scoring method. Defaults to [`Scorer.score_spans`](/api/scorer#score_spans) for the attribute `"sents"`. ~~Optional[Callable]~~ |
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/senter.pyx
|
||||
```
|
||||
|
||||
## SentenceRecognizer.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Initialize the sentence recognizer.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe with default model
|
||||
> senter = nlp.add_pipe("senter")
|
||||
>
|
||||
> # Construction via create_pipe with custom model
|
||||
> config = {"model": {"@architectures": "my_senter"}}
|
||||
> senter = nlp.add_pipe("senter", config=config)
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy.pipeline import SentenceRecognizer
|
||||
> senter = SentenceRecognizer(nlp.vocab, model)
|
||||
> ```
|
||||
|
||||
Create a new pipeline instance. In your application, you would normally use a
|
||||
shortcut for this and instantiate the component using its string name and
|
||||
[`nlp.add_pipe`](/api/language#add_pipe).
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | The [`Model`](https://thinc.ai/docs/api-model) powering the pipeline component. ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
| `name` | String name of the component instance. Used to add entries to the `losses` during training. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `overwrite` <Tag variant="new">3.2</Tag> | Whether existing annotation is overwritten. Defaults to `False`. ~~bool~~ |
|
||||
| `scorer` <Tag variant="new">3.2</Tag> | The scoring method. Defaults to [`Scorer.score_spans`](/api/scorer#score_spans) for the attribute `"sents"`. ~~Optional[Callable]~~ |
|
||||
|
||||
## SentenceRecognizer.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document. The document is modified in place, and returned.
|
||||
This usually happens under the hood when the `nlp` object is called on a text
|
||||
and all pipeline components are applied to the `Doc` in order. Both
|
||||
[`__call__`](/api/sentencerecognizer#call) and
|
||||
[`pipe`](/api/sentencerecognizer#pipe) delegate to the
|
||||
[`predict`](/api/sentencerecognizer#predict) and
|
||||
[`set_annotations`](/api/sentencerecognizer#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> senter = nlp.add_pipe("senter")
|
||||
> # This usually happens under the hood
|
||||
> processed = senter(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## SentenceRecognizer.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order. Both [`__call__`](/api/sentencerecognizer#call)
|
||||
and [`pipe`](/api/sentencerecognizer#pipe) delegate to the
|
||||
[`predict`](/api/sentencerecognizer#predict) and
|
||||
[`set_annotations`](/api/sentencerecognizer#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> senter = nlp.add_pipe("senter")
|
||||
> for doc in senter.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `stream` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## SentenceRecognizer.initialize {id="initialize",tag="method"}
|
||||
|
||||
Initialize the component for training. `get_examples` should be a function that
|
||||
returns an iterable of [`Example`](/api/example) objects. **At least one example
|
||||
should be supplied.** The data examples are used to **initialize the model** of
|
||||
the component and can either be the full training data or a representative
|
||||
sample. Initialization includes validating the network,
|
||||
[inferring missing shapes](https://thinc.ai/docs/usage-models#validation) and
|
||||
setting up the label scheme based on the data. This method is typically called
|
||||
by [`Language.initialize`](/api/language#initialize).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> senter = nlp.add_pipe("senter")
|
||||
> senter.initialize(lambda: examples, nlp=nlp)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Must contain at least one `Example`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
|
||||
## SentenceRecognizer.predict {id="predict",tag="method"}
|
||||
|
||||
Apply the component's model to a batch of [`Doc`](/api/doc) objects, without
|
||||
modifying them.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> senter = nlp.add_pipe("senter")
|
||||
> scores = senter.predict([doc1, doc2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| `docs` | The documents to predict. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | The model's prediction for each document. |
|
||||
|
||||
## SentenceRecognizer.set_annotations {id="set_annotations",tag="method"}
|
||||
|
||||
Modify a batch of [`Doc`](/api/doc) objects, using pre-computed scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> senter = nlp.add_pipe("senter")
|
||||
> scores = senter.predict([doc1, doc2])
|
||||
> senter.set_annotations([doc1, doc2], scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | ------------------------------------------------------------ |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `scores` | The scores to set, produced by `SentenceRecognizer.predict`. |
|
||||
|
||||
## SentenceRecognizer.update {id="update",tag="method"}
|
||||
|
||||
Learn from a batch of [`Example`](/api/example) objects containing the
|
||||
predictions and gold-standard annotations, and update the component's model.
|
||||
Delegates to [`predict`](/api/sentencerecognizer#predict) and
|
||||
[`get_loss`](/api/sentencerecognizer#get_loss).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> senter = nlp.add_pipe("senter")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = senter.update(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## SentenceRecognizer.rehearse {id="rehearse",tag="method,experimental",version="3"}
|
||||
|
||||
Perform a "rehearsal" update from a batch of data. Rehearsal updates teach the
|
||||
current model to make predictions similar to an initial model to try to address
|
||||
the "catastrophic forgetting" problem. This feature is experimental.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> senter = nlp.add_pipe("senter")
|
||||
> optimizer = nlp.resume_training()
|
||||
> losses = senter.rehearse(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## SentenceRecognizer.get_loss {id="get_loss",tag="method"}
|
||||
|
||||
Find the loss and gradient of loss for the batch of documents and their
|
||||
predicted scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> senter = nlp.add_pipe("senter")
|
||||
> scores = senter.predict([eg.predicted for eg in examples])
|
||||
> loss, d_loss = senter.get_loss(examples, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------------------------- |
|
||||
| `examples` | The batch of examples. ~~Iterable[Example]~~ |
|
||||
| `scores` | Scores representing the model's predictions. |
|
||||
| **RETURNS** | The loss and the gradient, i.e. `(loss, gradient)`. ~~Tuple[float, float]~~ |
|
||||
|
||||
## SentenceRecognizer.create_optimizer {id="create_optimizer",tag="method"}
|
||||
|
||||
Create an optimizer for the pipeline component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> senter = nlp.add_pipe("senter")
|
||||
> optimizer = senter.create_optimizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The optimizer. ~~Optimizer~~ |
|
||||
|
||||
## SentenceRecognizer.use_params {id="use_params",tag="method, contextmanager"}
|
||||
|
||||
Modify the pipe's model, to use the given parameter values. At the end of the
|
||||
context, the original parameters are restored.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> senter = nlp.add_pipe("senter")
|
||||
> with senter.use_params(optimizer.averages):
|
||||
> senter.to_disk("/best_model")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------- |
|
||||
| `params` | The parameter values to use in the model. ~~dict~~ |
|
||||
|
||||
## SentenceRecognizer.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> senter = nlp.add_pipe("senter")
|
||||
> senter.to_disk("/path/to/senter")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## SentenceRecognizer.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> senter = nlp.add_pipe("senter")
|
||||
> senter.from_disk("/path/to/senter")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `SentenceRecognizer` object. ~~SentenceRecognizer~~ |
|
||||
|
||||
## SentenceRecognizer.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> senter = nlp.add_pipe("senter")
|
||||
> senter_bytes = senter.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `SentenceRecognizer` object. ~~bytes~~ |
|
||||
|
||||
## SentenceRecognizer.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> senter_bytes = senter.to_bytes()
|
||||
> senter = nlp.add_pipe("senter")
|
||||
> senter.from_bytes(senter_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `SentenceRecognizer` object. ~~SentenceRecognizer~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = senter.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `cfg` | The config file. You usually don't want to exclude this. |
|
||||
| `model` | The binary model data. You usually don't want to exclude this. |
|
||||
@@ -0,0 +1,195 @@
|
||||
---
|
||||
title: Sentencizer
|
||||
tag: class
|
||||
source: spacy/pipeline/sentencizer.pyx
|
||||
teaser: 'Pipeline component for rule-based sentence boundary detection'
|
||||
api_string_name: sentencizer
|
||||
api_trainable: false
|
||||
---
|
||||
|
||||
A simple pipeline component to allow custom sentence boundary detection logic
|
||||
that doesn't require the dependency parse. By default, sentence segmentation is
|
||||
performed by the [`DependencyParser`](/api/dependencyparser), so the
|
||||
`Sentencizer` lets you implement a simpler, rule-based strategy that doesn't
|
||||
require a statistical model to be loaded.
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
Calculated values will be assigned to `Token.is_sent_start`. The resulting
|
||||
sentences can be accessed using `Doc.sents`.
|
||||
|
||||
| Location | Value |
|
||||
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `Token.is_sent_start` | A boolean value indicating whether the token starts a sentence. This will be either `True` or `False` for all tokens. ~~bool~~ |
|
||||
| `Doc.sents` | An iterator over sentences in the `Doc`, determined by `Token.is_sent_start` values. ~~Iterator[Span]~~ |
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> config = {"punct_chars": None}
|
||||
> nlp.add_pipe("sentencizer", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `punct_chars` | Optional custom list of punctuation characters that mark sentence ends. See below for defaults if not set. Defaults to `None`. ~~Optional[List[str]]~~ |
|
||||
| `overwrite` <Tag variant="new">3.2</Tag> | Whether existing annotation is overwritten. Defaults to `False`. ~~bool~~ |
|
||||
| `scorer` <Tag variant="new">3.2</Tag> | The scoring method. Defaults to [`Scorer.score_spans`](/api/scorer#score_spans) for the attribute `"sents"` ~~Optional[Callable]~~ |
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/sentencizer.pyx
|
||||
```
|
||||
|
||||
## Sentencizer.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Initialize the sentencizer.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe
|
||||
> sentencizer = nlp.add_pipe("sentencizer")
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy.pipeline import Sentencizer
|
||||
> sentencizer = Sentencizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `punct_chars` | Optional custom list of punctuation characters that mark sentence ends. See below for defaults. ~~Optional[List[str]]~~ |
|
||||
| `overwrite` <Tag variant="new">3.2</Tag> | Whether existing annotation is overwritten. Defaults to `False`. ~~bool~~ |
|
||||
| `scorer` <Tag variant="new">3.2</Tag> | The scoring method. Defaults to [`Scorer.score_spans`](/api/scorer#score_spans) for the attribute `"sents"` ~~Optional[Callable]~~ |
|
||||
|
||||
```python {title="punct_chars defaults"}
|
||||
['!', '.', '?', '։', '؟', '۔', '܀', '܁', '܂', '߹', '।', '॥', '၊', '။', '።',
|
||||
'፧', '፨', '᙮', '᜵', '᜶', '᠃', '᠉', '᥄', '᥅', '᪨', '᪩', '᪪', '᪫',
|
||||
'᭚', '᭛', '᭞', '᭟', '᰻', '᰼', '᱾', '᱿', '‼', '‽', '⁇', '⁈', '⁉',
|
||||
'⸮', '⸼', '꓿', '꘎', '꘏', '꛳', '꛷', '꡶', '꡷', '꣎', '꣏', '꤯', '꧈',
|
||||
'꧉', '꩝', '꩞', '꩟', '꫰', '꫱', '꯫', '﹒', '﹖', '﹗', '!', '.', '?',
|
||||
'𐩖', '𐩗', '𑁇', '𑁈', '𑂾', '𑂿', '𑃀', '𑃁', '𑅁', '𑅂', '𑅃', '𑇅',
|
||||
'𑇆', '𑇍', '𑇞', '𑇟', '𑈸', '𑈹', '𑈻', '𑈼', '𑊩', '𑑋', '𑑌', '𑗂',
|
||||
'𑗃', '𑗉', '𑗊', '𑗋', '𑗌', '𑗍', '𑗎', '𑗏', '𑗐', '𑗑', '𑗒', '𑗓',
|
||||
'𑗔', '𑗕', '𑗖', '𑗗', '𑙁', '𑙂', '𑜼', '𑜽', '𑜾', '𑩂', '𑩃', '𑪛',
|
||||
'𑪜', '𑱁', '𑱂', '𖩮', '𖩯', '𖫵', '𖬷', '𖬸', '𖭄', '𛲟', '𝪈', '。', '。']
|
||||
```
|
||||
|
||||
## Sentencizer.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the sentencizer on a `Doc`. Typically, this happens automatically after
|
||||
the component has been added to the pipeline using
|
||||
[`nlp.add_pipe`](/api/language#add_pipe).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.lang.en import English
|
||||
>
|
||||
> nlp = English()
|
||||
> nlp.add_pipe("sentencizer")
|
||||
> doc = nlp("This is a sentence. This is another sentence.")
|
||||
> assert len(list(doc.sents)) == 2
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------- |
|
||||
| `doc` | The `Doc` object to process, e.g. the `Doc` in the pipeline. ~~Doc~~ |
|
||||
| **RETURNS** | The modified `Doc` with added sentence boundaries. ~~Doc~~ |
|
||||
|
||||
## Sentencizer.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> sentencizer = nlp.add_pipe("sentencizer")
|
||||
> for doc in sentencizer.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `stream` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## Sentencizer.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Save the sentencizer settings (punctuation characters) to a directory. Will
|
||||
create a file `sentencizer.json`. This also happens automatically when you save
|
||||
an `nlp` object with a sentencizer added to its pipeline.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> config = {"punct_chars": [".", "?", "!", "。"]}
|
||||
> sentencizer = nlp.add_pipe("sentencizer", config=config)
|
||||
> sentencizer.to_disk("/path/to/sentencizer.json")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------ | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a JSON file, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
|
||||
## Sentencizer.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the sentencizer settings from a file. Expects a JSON file. This also
|
||||
happens automatically when you load an `nlp` object or model with a sentencizer
|
||||
added to its pipeline.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> sentencizer = nlp.add_pipe("sentencizer")
|
||||
> sentencizer.from_disk("/path/to/sentencizer.json")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a JSON file. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| **RETURNS** | The modified `Sentencizer` object. ~~Sentencizer~~ |
|
||||
|
||||
## Sentencizer.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
Serialize the sentencizer settings to a bytestring.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> config = {"punct_chars": [".", "?", "!", "。"]}
|
||||
> sentencizer = nlp.add_pipe("sentencizer", config=config)
|
||||
> sentencizer_bytes = sentencizer.to_bytes()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------ |
|
||||
| **RETURNS** | The serialized data. ~~bytes~~ |
|
||||
|
||||
## Sentencizer.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> sentencizer_bytes = sentencizer.to_bytes()
|
||||
> sentencizer = nlp.add_pipe("sentencizer")
|
||||
> sentencizer.from_bytes(sentencizer_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | -------------------------------------------------- |
|
||||
| `bytes_data` | The bytestring to load. ~~bytes~~ |
|
||||
| **RETURNS** | The modified `Sentencizer` object. ~~Sentencizer~~ |
|
||||
@@ -0,0 +1,356 @@
|
||||
---
|
||||
title: SpanResolver
|
||||
tag: class,experimental
|
||||
source: spacy-experimental/coref/span_resolver_component.py
|
||||
teaser: 'Pipeline component for resolving tokens into spans'
|
||||
api_base_class: /api/pipe
|
||||
api_string_name: span_resolver
|
||||
api_trainable: true
|
||||
---
|
||||
|
||||
> #### Installation
|
||||
>
|
||||
> ```bash
|
||||
> $ pip install -U spacy-experimental
|
||||
> ```
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
This component not yet integrated into spaCy core, and is available via the
|
||||
extension package
|
||||
[`spacy-experimental`](https://github.com/explosion/spacy-experimental) starting
|
||||
in version 0.6.0. It exposes the component via
|
||||
[entry points](/usage/saving-loading/#entry-points), so if you have the package
|
||||
installed, using `factory = "experimental_span_resolver"` in your
|
||||
[training config](/usage/training#config) or
|
||||
`nlp.add_pipe("experimental_span_resolver")` will work out-of-the-box.
|
||||
|
||||
</Infobox>
|
||||
|
||||
A `SpanResolver` component takes in tokens (represented as `Span` objects of
|
||||
length 1) and resolves them into `Span` objects of arbitrary length. The initial
|
||||
use case is as a post-processing step on word-level
|
||||
[coreference resolution](/api/coref). The input and output keys used to store
|
||||
`Span` objects are configurable.
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
Predictions will be saved to `Doc.spans` as [`SpanGroup`s](/api/spangroup).
|
||||
|
||||
Input token spans will be read in using an input prefix, by default
|
||||
`"coref_head_clusters"`, and output spans will be saved using an output prefix
|
||||
(default `"coref_clusters"`) plus a serial number starting from one. The
|
||||
prefixes are configurable.
|
||||
|
||||
| Location | Value |
|
||||
| ------------------------------------------------- | ------------------------------------------------------------------------- |
|
||||
| `Doc.spans[output_prefix + "_" + cluster_number]` | One group of predicted spans. Cluster number starts from 1. ~~SpanGroup~~ |
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config). See the
|
||||
[model architectures](/api/architectures#coref-architectures) documentation for
|
||||
details on the architectures and their arguments and hyperparameters.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy_experimental.coref.span_resolver_component import DEFAULT_SPAN_RESOLVER_MODEL
|
||||
> from spacy_experimental.coref.coref_util import DEFAULT_CLUSTER_PREFIX, DEFAULT_CLUSTER_HEAD_PREFIX
|
||||
> config={
|
||||
> "model": DEFAULT_SPAN_RESOLVER_MODEL,
|
||||
> "input_prefix": DEFAULT_CLUSTER_HEAD_PREFIX,
|
||||
> "output_prefix": DEFAULT_CLUSTER_PREFIX,
|
||||
> },
|
||||
> nlp.add_pipe("experimental_span_resolver", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `model` | The [`Model`](https://thinc.ai/docs/api-model) powering the pipeline component. Defaults to [SpanResolver](/api/architectures#SpanResolver). ~~Model~~ |
|
||||
| `input_prefix` | The prefix to use for input `SpanGroup`s. Defaults to `coref_head_clusters`. ~~str~~ |
|
||||
| `output_prefix` | The prefix for predicted `SpanGroup`s. Defaults to `coref_clusters`. ~~str~~ |
|
||||
|
||||
## SpanResolver.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe with default model
|
||||
> span_resolver = nlp.add_pipe("experimental_span_resolver")
|
||||
>
|
||||
> # Construction via add_pipe with custom model
|
||||
> config = {"model": {"@architectures": "my_span_resolver.v1"}}
|
||||
> span_resolver = nlp.add_pipe("experimental_span_resolver", config=config)
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy_experimental.coref.span_resolver_component import SpanResolver
|
||||
> span_resolver = SpanResolver(nlp.vocab, model)
|
||||
> ```
|
||||
|
||||
Create a new pipeline instance. In your application, you would normally use a
|
||||
shortcut for this and instantiate the component using its string name and
|
||||
[`nlp.add_pipe`](/api/language#add_pipe).
|
||||
|
||||
| Name | Description |
|
||||
| --------------- | --------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | The [`Model`](https://thinc.ai/docs/api-model) powering the pipeline component. ~~Model~~ |
|
||||
| `name` | String name of the component instance. Used to add entries to the `losses` during training. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `input_prefix` | The prefix to use for input `SpanGroup`s. Defaults to `coref_head_clusters`. ~~str~~ |
|
||||
| `output_prefix` | The prefix for predicted `SpanGroup`s. Defaults to `coref_clusters`. ~~str~~ |
|
||||
|
||||
## SpanResolver.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document. The document is modified in place and returned.
|
||||
This usually happens under the hood when the `nlp` object is called on a text
|
||||
and all pipeline components are applied to the `Doc` in order. Both
|
||||
[`__call__`](#call) and [`pipe`](#pipe) delegate to the [`predict`](#predict)
|
||||
and [`set_annotations`](#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> span_resolver = nlp.add_pipe("experimental_span_resolver")
|
||||
> # This usually happens under the hood
|
||||
> processed = span_resolver(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## SpanResolver.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order. Both [`__call__`](/api/span-resolver#call) and
|
||||
[`pipe`](/api/span-resolver#pipe) delegate to the
|
||||
[`predict`](/api/span-resolver#predict) and
|
||||
[`set_annotations`](/api/span-resolver#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_resolver = nlp.add_pipe("experimental_span_resolver")
|
||||
> for doc in span_resolver.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `stream` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## SpanResolver.initialize {id="initialize",tag="method"}
|
||||
|
||||
Initialize the component for training. `get_examples` should be a function that
|
||||
returns an iterable of [`Example`](/api/example) objects. **At least one example
|
||||
should be supplied.** The data examples are used to **initialize the model** of
|
||||
the component and can either be the full training data or a representative
|
||||
sample. Initialization includes validating the network,
|
||||
[inferring missing shapes](https://thinc.ai/docs/usage-models#validation) and
|
||||
setting up the label scheme based on the data. This method is typically called
|
||||
by [`Language.initialize`](/api/language#initialize).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_resolver = nlp.add_pipe("experimental_span_resolver")
|
||||
> span_resolver.initialize(lambda: examples, nlp=nlp)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Must contain at least one `Example`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
|
||||
## SpanResolver.predict {id="predict",tag="method"}
|
||||
|
||||
Apply the component's model to a batch of [`Doc`](/api/doc) objects, without
|
||||
modifying them. Predictions are returned as a list of `MentionClusters`, one for
|
||||
each input `Doc`. A `MentionClusters` instance is just a list of lists of pairs
|
||||
of `int`s, where each item corresponds to an input `SpanGroup`, and the `int`s
|
||||
correspond to token indices.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_resolver = nlp.add_pipe("experimental_span_resolver")
|
||||
> spans = span_resolver.predict([doc1, doc2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------- |
|
||||
| `docs` | The documents to predict. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | The predicted spans for the `Doc`s. ~~List[MentionClusters]~~ |
|
||||
|
||||
## SpanResolver.set_annotations {id="set_annotations",tag="method"}
|
||||
|
||||
Modify a batch of documents, saving predictions using the output prefix in
|
||||
`Doc.spans`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_resolver = nlp.add_pipe("experimental_span_resolver")
|
||||
> spans = span_resolver.predict([doc1, doc2])
|
||||
> span_resolver.set_annotations([doc1, doc2], spans)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | ------------------------------------------------------------- |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `spans` | The predicted spans for the `docs`. ~~List[MentionClusters]~~ |
|
||||
|
||||
## SpanResolver.update {id="update",tag="method"}
|
||||
|
||||
Learn from a batch of [`Example`](/api/example) objects. Delegates to
|
||||
[`predict`](/api/span-resolver#predict).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_resolver = nlp.add_pipe("experimental_span_resolver")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = span_resolver.update(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## SpanResolver.create_optimizer {id="create_optimizer",tag="method"}
|
||||
|
||||
Create an optimizer for the pipeline component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_resolver = nlp.add_pipe("experimental_span_resolver")
|
||||
> optimizer = span_resolver.create_optimizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The optimizer. ~~Optimizer~~ |
|
||||
|
||||
## SpanResolver.use_params {id="use_params",tag="method, contextmanager"}
|
||||
|
||||
Modify the pipe's model, to use the given parameter values. At the end of the
|
||||
context, the original parameters are restored.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_resolver = nlp.add_pipe("experimental_span_resolver")
|
||||
> with span_resolver.use_params(optimizer.averages):
|
||||
> span_resolver.to_disk("/best_model")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------- |
|
||||
| `params` | The parameter values to use in the model. ~~dict~~ |
|
||||
|
||||
## SpanResolver.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_resolver = nlp.add_pipe("experimental_span_resolver")
|
||||
> span_resolver.to_disk("/path/to/span_resolver")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## SpanResolver.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_resolver = nlp.add_pipe("experimental_span_resolver")
|
||||
> span_resolver.from_disk("/path/to/span_resolver")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `SpanResolver` object. ~~SpanResolver~~ |
|
||||
|
||||
## SpanResolver.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_resolver = nlp.add_pipe("experimental_span_resolver")
|
||||
> span_resolver_bytes = span_resolver.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `SpanResolver` object. ~~bytes~~ |
|
||||
|
||||
## SpanResolver.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_resolver_bytes = span_resolver.to_bytes()
|
||||
> span_resolver = nlp.add_pipe("experimental_span_resolver")
|
||||
> span_resolver.from_bytes(span_resolver_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `SpanResolver` object. ~~SpanResolver~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = span_resolver.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `cfg` | The config file. You usually don't want to exclude this. |
|
||||
| `model` | The binary model data. You usually don't want to exclude this. |
|
||||
@@ -0,0 +1,572 @@
|
||||
---
|
||||
title: Span
|
||||
tag: class
|
||||
source: spacy/tokens/span.pyx
|
||||
---
|
||||
|
||||
A slice from a [`Doc`](/api/doc) object.
|
||||
|
||||
## Span.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create a `Span` object from the slice `doc[start : end]`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> span = doc[1:4]
|
||||
> assert [t.text for t in span] == ["it", "back", "!"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------- | --------------------------------------------------------------------------------------- |
|
||||
| `doc` | The parent document. ~~Doc~~ |
|
||||
| `start` | The index of the first token of the span. ~~int~~ |
|
||||
| `end` | The index of the first token after the span. ~~int~~ |
|
||||
| `label` | A label to attach to the span, e.g. for named entities. ~~Union[str, int]~~ |
|
||||
| `vector` | A meaning representation of the span. ~~numpy.ndarray[ndim=1, dtype=float32]~~ |
|
||||
| `vector_norm` | The L2 norm of the document's vector representation. ~~float~~ |
|
||||
| `kb_id` | A knowledge base ID to attach to the span, e.g. for named entities. ~~Union[str, int]~~ |
|
||||
| `span_id` | An ID to associate with the span. ~~Union[str, int]~~ |
|
||||
|
||||
## Span.\_\_getitem\_\_ {id="getitem",tag="method"}
|
||||
|
||||
Get a `Token` object.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> span = doc[1:4]
|
||||
> assert span[1].text == "back"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------- |
|
||||
| `i` | The index of the token within the span. ~~int~~ |
|
||||
| **RETURNS** | The token at `span[i]`. ~~Token~~ |
|
||||
|
||||
Get a `Span` object.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> span = doc[1:4]
|
||||
> assert span[1:3].text == "back!"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------- |
|
||||
| `start_end` | The slice of the span to get. ~~Tuple[int, int]~~ |
|
||||
| **RETURNS** | The span at `span[start : end]`. ~~Span~~ |
|
||||
|
||||
## Span.\_\_iter\_\_ {id="iter",tag="method"}
|
||||
|
||||
Iterate over `Token` objects.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> span = doc[1:4]
|
||||
> assert [t.text for t in span] == ["it", "back", "!"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | --------------------------- |
|
||||
| **YIELDS** | A `Token` object. ~~Token~~ |
|
||||
|
||||
## Span.\_\_len\_\_ {id="len",tag="method"}
|
||||
|
||||
Get the number of tokens in the span.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> span = doc[1:4]
|
||||
> assert len(span) == 3
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------- |
|
||||
| **RETURNS** | The number of tokens in the span. ~~int~~ |
|
||||
|
||||
## Span.set_extension {id="set_extension",tag="classmethod",version="2"}
|
||||
|
||||
Define a custom attribute on the `Span` which becomes available via `Span._`.
|
||||
For details, see the documentation on
|
||||
[custom attributes](/usage/processing-pipelines#custom-components-attributes).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Span
|
||||
> city_getter = lambda span: any(city in span.text for city in ("New York", "Paris", "Berlin"))
|
||||
> Span.set_extension("has_city", getter=city_getter)
|
||||
> doc = nlp("I like New York in Autumn")
|
||||
> assert doc[1:4]._.has_city
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `name` | Name of the attribute to set by the extension. For example, `"my_attr"` will be available as `span._.my_attr`. ~~str~~ |
|
||||
| `default` | Optional default value of the attribute if no getter or method is defined. ~~Optional[Any]~~ |
|
||||
| `method` | Set a custom method on the object, for example `span._.compare(other_span)`. ~~Optional[Callable[[Span, ...], Any]]~~ |
|
||||
| `getter` | Getter function that takes the object and returns an attribute value. Is called when the user accesses the `._` attribute. ~~Optional[Callable[[Span], Any]]~~ |
|
||||
| `setter` | Setter function that takes the `Span` and a value, and modifies the object. Is called when the user writes to the `Span._` attribute. ~~Optional[Callable[[Span, Any], None]]~~ |
|
||||
| `force` | Force overwriting existing attribute. ~~bool~~ |
|
||||
|
||||
## Span.get_extension {id="get_extension",tag="classmethod",version="2"}
|
||||
|
||||
Look up a previously registered extension by name. Returns a 4-tuple
|
||||
`(default, method, getter, setter)` if the extension is registered. Raises a
|
||||
`KeyError` otherwise.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Span
|
||||
> Span.set_extension("is_city", default=False)
|
||||
> extension = Span.get_extension("is_city")
|
||||
> assert extension == (False, None, None, None)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `name` | Name of the extension. ~~str~~ |
|
||||
| **RETURNS** | A `(default, method, getter, setter)` tuple of the extension. ~~Tuple[Optional[Any], Optional[Callable], Optional[Callable], Optional[Callable]]~~ |
|
||||
|
||||
## Span.has_extension {id="has_extension",tag="classmethod",version="2"}
|
||||
|
||||
Check whether an extension has been registered on the `Span` class.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Span
|
||||
> Span.set_extension("is_city", default=False)
|
||||
> assert Span.has_extension("is_city")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------- |
|
||||
| `name` | Name of the extension to check. ~~str~~ |
|
||||
| **RETURNS** | Whether the extension has been registered. ~~bool~~ |
|
||||
|
||||
## Span.remove_extension {id="remove_extension",tag="classmethod",version="2.0.12"}
|
||||
|
||||
Remove a previously registered extension.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Span
|
||||
> Span.set_extension("is_city", default=False)
|
||||
> removed = Span.remove_extension("is_city")
|
||||
> assert not Span.has_extension("is_city")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `name` | Name of the extension. ~~str~~ |
|
||||
| **RETURNS** | A `(default, method, getter, setter)` tuple of the removed extension. ~~Tuple[Optional[Any], Optional[Callable], Optional[Callable], Optional[Callable]]~~ |
|
||||
|
||||
## Span.char_span {id="char_span",tag="method",version="2.2.4"}
|
||||
|
||||
Create a `Span` object from the slice `span.text[start:end]`. Returns `None` if
|
||||
the character indices don't map to a valid span.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like New York")
|
||||
> span = doc[1:4].char_span(5, 13, label="GPE")
|
||||
> assert span.text == "New York"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `start` | The index of the first character of the span. ~~int~~ |
|
||||
| `end` | The index of the last character after the span. ~~int~~ |
|
||||
| `label` | A label to attach to the span, e.g. for named entities. ~~Union[int, str]~~ |
|
||||
| `kb_id` | An ID from a knowledge base to capture the meaning of a named entity. ~~Union[int, str]~~ |
|
||||
| `vector` | A meaning representation of the span. ~~numpy.ndarray[ndim=1, dtype=float32]~~ |
|
||||
| `id` | Unused. ~~Union[int, str]~~ |
|
||||
| `alignment_mode` <Tag variant="new">3.5.1</Tag> | How character indices snap to token boundaries. Options: `"strict"` (no snapping), `"contract"` (span of all tokens completely within the character span), `"expand"` (span of all tokens at least partially covered by the character span). Defaults to `"strict"`. ~~str~~ |
|
||||
| `span_id` <Tag variant="new">3.5.1</Tag> | An identifier to associate with the span. ~~Union[int, str]~~ |
|
||||
| **RETURNS** | The newly constructed object or `None`. ~~Optional[Span]~~ |
|
||||
|
||||
## Span.similarity {id="similarity",tag="method",model="vectors"}
|
||||
|
||||
Make a semantic similarity estimate. The default estimate is cosine similarity
|
||||
using an average of word vectors.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("green apples and red oranges")
|
||||
> green_apples = doc[:2]
|
||||
> red_oranges = doc[3:]
|
||||
> apples_oranges = green_apples.similarity(red_oranges)
|
||||
> oranges_apples = red_oranges.similarity(green_apples)
|
||||
> assert apples_oranges == oranges_apples
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `other` | The object to compare with. By default, accepts `Doc`, `Span`, `Token` and `Lexeme` objects. ~~Union[Doc, Span, Token, Lexeme]~~ |
|
||||
| **RETURNS** | A scalar similarity score. Higher is more similar. ~~float~~ |
|
||||
|
||||
## Span.get_lca_matrix {id="get_lca_matrix",tag="method"}
|
||||
|
||||
Calculates the lowest common ancestor matrix for a given `Span`. Returns LCA
|
||||
matrix containing the integer index of the ancestor, or `-1` if no common
|
||||
ancestor is found, e.g. if span excludes a necessary ancestor.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like New York in Autumn")
|
||||
> span = doc[1:4]
|
||||
> matrix = span.get_lca_matrix()
|
||||
> # array([[0, 0, 0], [0, 1, 2], [0, 2, 2]], dtype=int32)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------------------------------------- |
|
||||
| **RETURNS** | The lowest common ancestor matrix of the `Span`. ~~numpy.ndarray[ndim=2, dtype=int32]~~ |
|
||||
|
||||
## Span.to_array {id="to_array",tag="method",version="2"}
|
||||
|
||||
Given a list of `M` attribute IDs, export the tokens to a numpy `ndarray` of
|
||||
shape `(N, M)`, where `N` is the length of the document. The values will be
|
||||
32-bit integers.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.attrs import LOWER, POS, ENT_TYPE, IS_ALPHA
|
||||
> doc = nlp("I like New York in Autumn.")
|
||||
> span = doc[2:3]
|
||||
> # All strings mapped to integers, for easy export to numpy
|
||||
> np_array = span.to_array([LOWER, POS, ENT_TYPE, IS_ALPHA])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `attr_ids` | A list of attributes (int IDs or string names) or a single attribute (int ID or string name). ~~Union[int, str, List[Union[int, str]]]~~ |
|
||||
| **RETURNS** | The exported attributes as a numpy array. ~~Union[numpy.ndarray[ndim=2, dtype=uint64], numpy.ndarray[ndim=1, dtype=uint64]]~~ |
|
||||
|
||||
## Span.ents {id="ents",tag="property",version="2.0.13",model="ner"}
|
||||
|
||||
The named entities that fall completely within the span. Returns a tuple of
|
||||
`Span` objects.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Mr. Best flew to New York on Saturday morning.")
|
||||
> span = doc[0:6]
|
||||
> ents = list(span.ents)
|
||||
> assert ents[0].label == 346
|
||||
> assert ents[0].label_ == "PERSON"
|
||||
> assert ents[0].text == "Mr. Best"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------- |
|
||||
| **RETURNS** | Entities in the span, one `Span` per entity. ~~Tuple[Span, ...]~~ |
|
||||
|
||||
## Span.noun_chunks {id="noun_chunks",tag="property",model="parser"}
|
||||
|
||||
Iterate over the base noun phrases in the span. Yields base noun-phrase `Span`
|
||||
objects, if the document has been syntactically parsed. A base noun phrase, or
|
||||
"NP chunk", is a noun phrase that does not permit other NPs to be nested within
|
||||
it – so no NP-level coordination, no prepositional phrases, and no relative
|
||||
clauses.
|
||||
|
||||
If the `noun_chunk` [syntax iterator](/usage/linguistic-features#language-data)
|
||||
has not been implemented for the given language, a `NotImplementedError` is
|
||||
raised.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("A phrase with another phrase occurs.")
|
||||
> span = doc[3:5]
|
||||
> chunks = list(span.noun_chunks)
|
||||
> assert len(chunks) == 1
|
||||
> assert chunks[0].text == "another phrase"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | --------------------------------- |
|
||||
| **YIELDS** | Noun chunks in the span. ~~Span~~ |
|
||||
|
||||
## Span.as_doc {id="as_doc",tag="method"}
|
||||
|
||||
Create a new `Doc` object corresponding to the `Span`, with a copy of the data.
|
||||
|
||||
When calling this on many spans from the same doc, passing in a precomputed
|
||||
array representation of the doc using the `array_head` and `array` args can save
|
||||
time.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like New York in Autumn.")
|
||||
> span = doc[2:4]
|
||||
> doc2 = span.as_doc()
|
||||
> assert doc2.text == "New York"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------- | -------------------------------------------------------------------------------------------------------------------- |
|
||||
| `copy_user_data` | Whether or not to copy the original doc's user data. ~~bool~~ |
|
||||
| `array_head` | Precomputed array attributes (headers) of the original doc, as generated by `Doc._get_array_attrs()`. ~~Tuple~~ |
|
||||
| `array` | Precomputed array version of the original doc as generated by [`Doc.to_array`](/api/doc#to_array). ~~numpy.ndarray~~ |
|
||||
| **RETURNS** | A `Doc` object of the `Span`'s content. ~~Doc~~ |
|
||||
|
||||
## Span.root {id="root",tag="property",model="parser"}
|
||||
|
||||
The token with the shortest path to the root of the sentence (or the root
|
||||
itself). If multiple tokens are equally high in the tree, the first token is
|
||||
taken.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like New York in Autumn.")
|
||||
> i, like, new, york, in_, autumn, dot = range(len(doc))
|
||||
> assert doc[new].head.text == "York"
|
||||
> assert doc[york].head.text == "like"
|
||||
> new_york = doc[new:york+1]
|
||||
> assert new_york.root.text == "York"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------- |
|
||||
| **RETURNS** | The root token. ~~Token~~ |
|
||||
|
||||
## Span.conjuncts {id="conjuncts",tag="property",model="parser"}
|
||||
|
||||
A tuple of tokens coordinated to `span.root`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like apples and oranges")
|
||||
> apples_conjuncts = doc[2:3].conjuncts
|
||||
> assert [t.text for t in apples_conjuncts] == ["oranges"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------- |
|
||||
| **RETURNS** | The coordinated tokens. ~~Tuple[Token, ...]~~ |
|
||||
|
||||
## Span.lefts {id="lefts",tag="property",model="parser"}
|
||||
|
||||
Tokens that are to the left of the span, whose heads are within the span.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like New York in Autumn.")
|
||||
> lefts = [t.text for t in doc[3:7].lefts]
|
||||
> assert lefts == ["New"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ---------------------------------------------- |
|
||||
| **YIELDS** | A left-child of a token of the span. ~~Token~~ |
|
||||
|
||||
## Span.rights {id="rights",tag="property",model="parser"}
|
||||
|
||||
Tokens that are to the right of the span, whose heads are within the span.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like New York in Autumn.")
|
||||
> rights = [t.text for t in doc[2:4].rights]
|
||||
> assert rights == ["in"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ----------------------------------------------- |
|
||||
| **YIELDS** | A right-child of a token of the span. ~~Token~~ |
|
||||
|
||||
## Span.n_lefts {id="n_lefts",tag="property",model="parser"}
|
||||
|
||||
The number of tokens that are to the left of the span, whose heads are within
|
||||
the span.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like New York in Autumn.")
|
||||
> assert doc[3:7].n_lefts == 1
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------- |
|
||||
| **RETURNS** | The number of left-child tokens. ~~int~~ |
|
||||
|
||||
## Span.n_rights {id="n_rights",tag="property",model="parser"}
|
||||
|
||||
The number of tokens that are to the right of the span, whose heads are within
|
||||
the span.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like New York in Autumn.")
|
||||
> assert doc[2:4].n_rights == 1
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------- |
|
||||
| **RETURNS** | The number of right-child tokens. ~~int~~ |
|
||||
|
||||
## Span.subtree {id="subtree",tag="property",model="parser"}
|
||||
|
||||
Tokens within the span and tokens which descend from them.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> subtree = [t.text for t in doc[:3].subtree]
|
||||
> assert subtree == ["Give", "it", "back", "!"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ----------------------------------------------------------- |
|
||||
| **YIELDS** | A token within the span, or a descendant from it. ~~Token~~ |
|
||||
|
||||
## Span.has_vector {id="has_vector",tag="property",model="vectors"}
|
||||
|
||||
A boolean value indicating whether a word vector is associated with the object.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like apples")
|
||||
> assert doc[1:].has_vector
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------- |
|
||||
| **RETURNS** | Whether the span has a vector data attached. ~~bool~~ |
|
||||
|
||||
## Span.vector {id="vector",tag="property",model="vectors"}
|
||||
|
||||
A real-valued meaning representation. Defaults to an average of the token
|
||||
vectors.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like apples")
|
||||
> assert doc[1:].vector.dtype == "float32"
|
||||
> assert doc[1:].vector.shape == (300,)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------------------------------------- |
|
||||
| **RETURNS** | A 1-dimensional array representing the span's vector. ~~`numpy.ndarray[ndim=1, dtype=float32]~~ |
|
||||
|
||||
## Span.vector_norm {id="vector_norm",tag="property",model="vectors"}
|
||||
|
||||
The L2 norm of the span's vector representation.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like apples")
|
||||
> doc[1:].vector_norm # 4.800883928527915
|
||||
> doc[2:].vector_norm # 6.895897646384268
|
||||
> assert doc[1:].vector_norm != doc[2:].vector_norm
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------- |
|
||||
| **RETURNS** | The L2 norm of the vector representation. ~~float~~ |
|
||||
|
||||
## Span.sent {id="sent",tag="property",model="sentences"}
|
||||
|
||||
The sentence span that this span is a part of. This property is only available
|
||||
when [sentence boundaries](/usage/linguistic-features#sbd) have been set on the
|
||||
document by the `parser`, `senter`, `sentencizer` or some custom function. It
|
||||
will raise an error otherwise.
|
||||
|
||||
If the span happens to cross sentence boundaries, only the first sentence will
|
||||
be returned. If it is required that the sentence always includes the full span,
|
||||
the result can be adjusted as such:
|
||||
|
||||
```python
|
||||
sent = span.sent
|
||||
sent = doc[sent.start : max(sent.end, span.end)]
|
||||
```
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> span = doc[1:3]
|
||||
> assert span.sent.text == "Give it back!"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------- |
|
||||
| **RETURNS** | The sentence span that this span is a part of. ~~Span~~ |
|
||||
|
||||
## Span.sents {id="sents",tag="property",model="sentences",version="3.2.1"}
|
||||
|
||||
Returns a generator over the sentences the span belongs to. This property is
|
||||
only available when [sentence boundaries](/usage/linguistic-features#sbd) have
|
||||
been set on the document by the `parser`, `senter`, `sentencizer` or some custom
|
||||
function. It will raise an error otherwise.
|
||||
|
||||
If the span happens to cross sentence boundaries, all sentences the span
|
||||
overlaps with will be returned.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> span = doc[2:4]
|
||||
> assert len(span.sents) == 2
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------- |
|
||||
| **RETURNS** | A generator yielding sentences this `Span` is a part of ~~Iterable[Span]~~ |
|
||||
|
||||
## Attributes {id="attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `doc` | The parent document. ~~Doc~~ |
|
||||
| `tensor` | The span's slice of the parent `Doc`'s tensor. ~~numpy.ndarray~~ |
|
||||
| `start` | The token offset for the start of the span. ~~int~~ |
|
||||
| `end` | The token offset for the end of the span. ~~int~~ |
|
||||
| `start_char` | The character offset for the start of the span. ~~int~~ |
|
||||
| `end_char` | The character offset for the end of the span. ~~int~~ |
|
||||
| `text` | A string representation of the span text. ~~str~~ |
|
||||
| `text_with_ws` | The text content of the span with a trailing whitespace character if the last token has one. ~~str~~ |
|
||||
| `orth` | ID of the verbatim text content. ~~int~~ |
|
||||
| `orth_` | Verbatim text content (identical to `Span.text`). Exists mostly for consistency with the other attributes. ~~str~~ |
|
||||
| `label` | The hash value of the span's label. ~~int~~ |
|
||||
| `label_` | The span's label. ~~str~~ |
|
||||
| `lemma_` | The span's lemma. Equivalent to `"".join(token.lemma_ + token.whitespace_ for token in span).strip()`. ~~str~~ |
|
||||
| `kb_id` | The hash value of the knowledge base ID referred to by the span. ~~int~~ |
|
||||
| `kb_id_` | The knowledge base ID referred to by the span. ~~str~~ |
|
||||
| `ent_id` | The hash value of the named entity the root token is an instance of. ~~int~~ |
|
||||
| `ent_id_` | The string ID of the named entity the root token is an instance of. ~~str~~ |
|
||||
| `id` | The hash value of the span's ID. ~~int~~ |
|
||||
| `id_` | The span's ID. ~~str~~ |
|
||||
| `sentiment` | A scalar value indicating the positivity or negativity of the span. ~~float~~ |
|
||||
| `_` | User space for adding custom [attribute extensions](/usage/processing-pipelines#custom-components-attributes). ~~Underscore~~ |
|
||||
@@ -0,0 +1,559 @@
|
||||
---
|
||||
title: SpanCategorizer
|
||||
tag: class,experimental
|
||||
source: spacy/pipeline/spancat.py
|
||||
version: 3.1
|
||||
teaser: 'Pipeline component for labeling potentially overlapping spans of text'
|
||||
api_base_class: /api/pipe
|
||||
api_string_name: spancat
|
||||
api_trainable: true
|
||||
---
|
||||
|
||||
A span categorizer consists of two parts: a [suggester function](#suggesters)
|
||||
that proposes candidate spans, which may or may not overlap, and a labeler model
|
||||
that predicts zero or more labels for each candidate.
|
||||
|
||||
This component comes in two forms: `spancat` and `spancat_singlelabel` (added in
|
||||
spaCy v3.5.1). When you need to perform multi-label classification on your
|
||||
spans, use `spancat`. The `spancat` component uses a `Logistic` layer where the
|
||||
output class probabilities are independent for each class. However, if you need
|
||||
to predict at most one true class for a span, then use `spancat_singlelabel`. It
|
||||
uses a `Softmax` layer and treats the task as a multi-class problem.
|
||||
|
||||
Predicted spans will be saved in a [`SpanGroup`](/api/spangroup) on the doc
|
||||
under `doc.spans[spans_key]`, where `spans_key` is a component config setting.
|
||||
Individual span scores are stored in `doc.spans[spans_key].attrs["scores"]`.
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
Predictions will be saved to `Doc.spans[spans_key]` as a
|
||||
[`SpanGroup`](/api/spangroup). The scores for the spans in the `SpanGroup` will
|
||||
be saved in `SpanGroup.attrs["scores"]`.
|
||||
|
||||
`spans_key` defaults to `"sc"`, but can be passed as a parameter. The `spancat`
|
||||
component will overwrite any existing spans under the spans key
|
||||
`doc.spans[spans_key]`.
|
||||
|
||||
| Location | Value |
|
||||
| -------------------------------------- | -------------------------------------------------------- |
|
||||
| `Doc.spans[spans_key]` | The annotated spans. ~~SpanGroup~~ |
|
||||
| `Doc.spans[spans_key].attrs["scores"]` | The score for each span in the `SpanGroup`. ~~Floats1d~~ |
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config). See the
|
||||
[model architectures](/api/architectures) documentation for details on the
|
||||
architectures and their arguments and hyperparameters.
|
||||
|
||||
> #### Example (spancat)
|
||||
>
|
||||
> ```python
|
||||
> from spacy.pipeline.spancat import DEFAULT_SPANCAT_MODEL
|
||||
> config = {
|
||||
> "threshold": 0.5,
|
||||
> "spans_key": "labeled_spans",
|
||||
> "max_positive": None,
|
||||
> "model": DEFAULT_SPANCAT_MODEL,
|
||||
> "suggester": {"@misc": "spacy.ngram_suggester.v1", "sizes": [1, 2, 3]},
|
||||
> }
|
||||
> nlp.add_pipe("spancat", config=config)
|
||||
> ```
|
||||
|
||||
> #### Example (spancat_singlelabel)
|
||||
>
|
||||
> ```python
|
||||
> from spacy.pipeline.spancat import DEFAULT_SPANCAT_SINGLELABEL_MODEL
|
||||
> config = {
|
||||
> "spans_key": "labeled_spans",
|
||||
> "model": DEFAULT_SPANCAT_SINGLELABEL_MODEL,
|
||||
> "suggester": {"@misc": "spacy.ngram_suggester.v1", "sizes": [1, 2, 3]},
|
||||
> # Additional spancat_singlelabel parameters
|
||||
> "negative_weight": 0.8,
|
||||
> "allow_overlap": True,
|
||||
> }
|
||||
> nlp.add_pipe("spancat_singlelabel", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `suggester` | A function that [suggests spans](#suggesters). Spans are returned as a ragged array with two integer columns, for the start and end positions. Defaults to [`ngram_suggester`](#ngram_suggester). ~~Callable[[Iterable[Doc], Optional[Ops]], Ragged]~~ |
|
||||
| `model` | A model instance that is given a a list of documents and `(start, end)` indices representing candidate span offsets. The model predicts a probability for each category for each span. Defaults to [SpanCategorizer](/api/architectures#SpanCategorizer). ~~Model[Tuple[List[Doc], Ragged], Floats2d]~~ |
|
||||
| `spans_key` | Key of the [`Doc.spans`](/api/doc#spans) dict to save the spans under. During initialization and training, the component will look for spans on the reference document under the same key. Defaults to `"sc"`. ~~str~~ |
|
||||
| `threshold` | Minimum probability to consider a prediction positive. Spans with a positive prediction will be saved on the Doc. Meant to be used in combination with the multi-class `spancat` component with a `Logistic` scoring layer. Defaults to `0.5`. ~~float~~ |
|
||||
| `max_positive` | Maximum number of labels to consider positive per span. Defaults to `None`, indicating no limit. Meant to be used together with the `spancat` component and defaults to 0 with `spancat_singlelabel`. ~~Optional[int]~~ |
|
||||
| `scorer` | The scoring method. Defaults to [`Scorer.score_spans`](/api/scorer#score_spans) for `Doc.spans[spans_key]` with overlapping spans allowed. ~~Optional[Callable]~~ |
|
||||
| `add_negative_label` <Tag variant="new">3.5.1</Tag> | Whether to learn to predict a special negative label for each unannotated `Span` . This should be `True` when using a `Softmax` classifier layer and so its `True` by default for `spancat_singlelabel`. Spans with negative labels and their scores are not stored as annotations. ~~bool~~ |
|
||||
| `negative_weight` <Tag variant="new">3.5.1</Tag> | Multiplier for the loss terms. It can be used to downweight the negative samples if there are too many. It is only used when `add_negative_label` is `True`. Defaults to `1.0`. ~~float~~ |
|
||||
| `allow_overlap` <Tag variant="new">3.5.1</Tag> | If `True`, the data is assumed to contain overlapping spans. It is only available when `max_positive` is exactly 1. Defaults to `True`. ~~bool~~ |
|
||||
|
||||
<Infobox variant="warning">
|
||||
|
||||
If you set a non-default value for `spans_key`, you'll have to update
|
||||
`[training.score_weights]` as well so that weights are computed properly. E. g.
|
||||
for `spans_key == "myspankey"`, include this in your config:
|
||||
|
||||
```ini
|
||||
[training.score_weights]
|
||||
spans_myspankey_f = 1.0
|
||||
spans_myspankey_p = 0.0
|
||||
spans_myspankey_r = 0.0
|
||||
```
|
||||
|
||||
</Infobox>
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/spancat.py
|
||||
```
|
||||
|
||||
## SpanCategorizer.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe with default model
|
||||
> # Replace 'spancat' with 'spancat_singlelabel' for exclusive classes
|
||||
> spancat = nlp.add_pipe("spancat")
|
||||
>
|
||||
> # Construction via add_pipe with custom model
|
||||
> config = {"model": {"@architectures": "my_spancat"}}
|
||||
> spancat = nlp.add_pipe("spancat", config=config)
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy.pipeline import SpanCategorizer
|
||||
> spancat = SpanCategorizer(nlp.vocab, model, suggester)
|
||||
> ```
|
||||
|
||||
Create a new pipeline instance. In your application, you would normally use a
|
||||
shortcut for this and instantiate the component using its string name and
|
||||
[`nlp.add_pipe`](/api/language#create_pipe).
|
||||
|
||||
| Name | Description |
|
||||
| --------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | A model instance that is given a a list of documents and `(start, end)` indices representing candidate span offsets. The model predicts a probability for each category for each span. ~~Model[Tuple[List[Doc], Ragged], Floats2d]~~ |
|
||||
| `suggester` | A function that [suggests spans](#suggesters). Spans are returned as a ragged array with two integer columns, for the start and end positions. ~~Callable[[Iterable[Doc], Optional[Ops]], Ragged]~~ |
|
||||
| `name` | String name of the component instance. Used to add entries to the `losses` during training. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `spans_key` | Key of the [`Doc.spans`](/api/doc#sans) dict to save the spans under. During initialization and training, the component will look for spans on the reference document under the same key. Defaults to `"sc"`. ~~str~~ |
|
||||
| `threshold` | Minimum probability to consider a prediction positive. Spans with a positive prediction will be saved on the Doc. Defaults to `0.5`. ~~float~~ |
|
||||
| `max_positive` | Maximum number of labels to consider positive per span. Defaults to `None`, indicating no limit. ~~Optional[int]~~ |
|
||||
| `allow_overlap` <Tag variant="new">3.5.1</Tag> | If `True`, the data is assumed to contain overlapping spans. It is only available when `max_positive` is exactly 1. Defaults to `True`. ~~bool~~ |
|
||||
| `add_negative_label` <Tag variant="new">3.5.1</Tag> | Whether to learn to predict a special negative label for each unannotated `Span`. This should be `True` when using a `Softmax` classifier layer and so its `True` by default for `spancat_singlelabel` . Spans with negative labels and their scores are not stored as annotations. ~~bool~~ |
|
||||
| `negative_weight` <Tag variant="new">3.5.1</Tag> | Multiplier for the loss terms. It can be used to downweight the negative samples if there are too many . It is only used when `add_negative_label` is `True`. Defaults to `1.0`. ~~float~~ |
|
||||
|
||||
## SpanCategorizer.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document. The document is modified in place, and returned.
|
||||
This usually happens under the hood when the `nlp` object is called on a text
|
||||
and all pipeline components are applied to the `Doc` in order. Both
|
||||
[`__call__`](/api/spancategorizer#call) and [`pipe`](/api/spancategorizer#pipe)
|
||||
delegate to the [`predict`](/api/spancategorizer#predict) and
|
||||
[`set_annotations`](/api/spancategorizer#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> spancat = nlp.add_pipe("spancat")
|
||||
> # This usually happens under the hood
|
||||
> processed = spancat(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## SpanCategorizer.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order. Both [`__call__`](/api/spancategorizer#call) and
|
||||
[`pipe`](/api/spancategorizer#pipe) delegate to the
|
||||
[`predict`](/api/spancategorizer#predict) and
|
||||
[`set_annotations`](/api/spancategorizer#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> spancat = nlp.add_pipe("spancat")
|
||||
> for doc in spancat.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `stream` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## SpanCategorizer.initialize {id="initialize",tag="method"}
|
||||
|
||||
Initialize the component for training. `get_examples` should be a function that
|
||||
returns an iterable of [`Example`](/api/example) objects. **At least one example
|
||||
should be supplied.** The data examples are used to **initialize the model** of
|
||||
the component and can either be the full training data or a representative
|
||||
sample. Initialization includes validating the network,
|
||||
[inferring missing shapes](https://thinc.ai/docs/usage-models#validation) and
|
||||
setting up the label scheme based on the data. This method is typically called
|
||||
by [`Language.initialize`](/api/language#initialize) and lets you customize
|
||||
arguments it receives via the
|
||||
[`[initialize.components]`](/api/data-formats#config-initialize) block in the
|
||||
config.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> spancat = nlp.add_pipe("spancat")
|
||||
> spancat.initialize(lambda: examples, nlp=nlp)
|
||||
> ```
|
||||
>
|
||||
> ```ini
|
||||
> ### config.cfg
|
||||
> [initialize.components.spancat]
|
||||
>
|
||||
> [initialize.components.spancat.labels]
|
||||
> @readers = "spacy.read_labels.v1"
|
||||
> path = "corpus/labels/spancat.json
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Must contain at least one `Example`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
| `labels` | The label information to add to the component, as provided by the [`label_data`](#label_data) property after initialization. To generate a reusable JSON file from your data, you should run the [`init labels`](/api/cli#init-labels) command. If no labels are provided, the `get_examples` callback is used to extract the labels from the data, which may be a lot slower. ~~Optional[Iterable[str]]~~ |
|
||||
|
||||
## SpanCategorizer.predict {id="predict",tag="method"}
|
||||
|
||||
Apply the component's model to a batch of [`Doc`](/api/doc) objects without
|
||||
modifying them.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> spancat = nlp.add_pipe("spancat")
|
||||
> scores = spancat.predict([doc1, doc2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| `docs` | The documents to predict. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | The model's prediction for each document. |
|
||||
|
||||
## SpanCategorizer.set_annotations {id="set_annotations",tag="method"}
|
||||
|
||||
Modify a batch of [`Doc`](/api/doc) objects using pre-computed scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> spancat = nlp.add_pipe("spancat")
|
||||
> scores = spancat.predict(docs)
|
||||
> spancat.set_annotations(docs, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | --------------------------------------------------------- |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `scores` | The scores to set, produced by `SpanCategorizer.predict`. |
|
||||
|
||||
## SpanCategorizer.update {id="update",tag="method"}
|
||||
|
||||
Learn from a batch of [`Example`](/api/example) objects containing the
|
||||
predictions and gold-standard annotations, and update the component's model.
|
||||
Delegates to [`predict`](/api/spancategorizer#predict) and
|
||||
[`get_loss`](/api/spancategorizer#get_loss).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> spancat = nlp.add_pipe("spancat")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = spancat.update(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## SpanCategorizer.set_candidates {id="set_candidates",tag="method", version="3.3"}
|
||||
|
||||
Use the suggester to add a list of [`Span`](/api/span) candidates to a list of
|
||||
[`Doc`](/api/doc) objects. This method is intended to be used for debugging
|
||||
purposes.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> spancat = nlp.add_pipe("spancat")
|
||||
> spancat.set_candidates(docs, "candidates")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------- | -------------------------------------------------------------------- |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `candidates_key` | Key of the Doc.spans dict to save the candidate spans under. ~~str~~ |
|
||||
|
||||
## SpanCategorizer.get_loss {id="get_loss",tag="method"}
|
||||
|
||||
Find the loss and gradient of loss for the batch of documents and their
|
||||
predicted scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> spancat = nlp.add_pipe("spancat")
|
||||
> scores = spancat.predict([eg.predicted for eg in examples])
|
||||
> loss, d_loss = spancat.get_loss(examples, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | --------------------------------------------------------------------------- |
|
||||
| `examples` | The batch of examples. ~~Iterable[Example]~~ |
|
||||
| `spans_scores` | Scores representing the model's predictions. ~~Tuple[Ragged, Floats2d]~~ |
|
||||
| **RETURNS** | The loss and the gradient, i.e. `(loss, gradient)`. ~~Tuple[float, float]~~ |
|
||||
|
||||
## SpanCategorizer.create_optimizer {id="create_optimizer",tag="method"}
|
||||
|
||||
Create an optimizer for the pipeline component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> spancat = nlp.add_pipe("spancat")
|
||||
> optimizer = spancat.create_optimizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The optimizer. ~~Optimizer~~ |
|
||||
|
||||
## SpanCategorizer.use_params {id="use_params",tag="method, contextmanager"}
|
||||
|
||||
Modify the pipe's model to use the given parameter values.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> spancat = nlp.add_pipe("spancat")
|
||||
> with spancat.use_params(optimizer.averages):
|
||||
> spancat.to_disk("/best_model")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------- |
|
||||
| `params` | The parameter values to use in the model. ~~dict~~ |
|
||||
|
||||
## SpanCategorizer.add_label {id="add_label",tag="method"}
|
||||
|
||||
Add a new label to the pipe. Raises an error if the output dimension is already
|
||||
set, or if the model has already been fully [initialized](#initialize). Note
|
||||
that you don't have to call this method if you provide a **representative data
|
||||
sample** to the [`initialize`](#initialize) method. In this case, all labels
|
||||
found in the sample will be automatically added to the model, and the output
|
||||
dimension will be [inferred](/usage/layers-architectures#thinc-shape-inference)
|
||||
automatically.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> spancat = nlp.add_pipe("spancat")
|
||||
> spancat.add_label("MY_LABEL")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------- |
|
||||
| `label` | The label to add. ~~str~~ |
|
||||
| **RETURNS** | `0` if the label is already present, otherwise `1`. ~~int~~ |
|
||||
|
||||
## SpanCategorizer.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> spancat = nlp.add_pipe("spancat")
|
||||
> spancat.to_disk("/path/to/spancat")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## SpanCategorizer.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> spancat = nlp.add_pipe("spancat")
|
||||
> spancat.from_disk("/path/to/spancat")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `SpanCategorizer` object. ~~SpanCategorizer~~ |
|
||||
|
||||
## SpanCategorizer.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> spancat = nlp.add_pipe("spancat")
|
||||
> spancat_bytes = spancat.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `SpanCategorizer` object. ~~bytes~~ |
|
||||
|
||||
## SpanCategorizer.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> spancat_bytes = spancat.to_bytes()
|
||||
> spancat = nlp.add_pipe("spancat")
|
||||
> spancat.from_bytes(spancat_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `SpanCategorizer` object. ~~SpanCategorizer~~ |
|
||||
|
||||
## SpanCategorizer.labels {id="labels",tag="property"}
|
||||
|
||||
The labels currently added to the component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> spancat.add_label("MY_LABEL")
|
||||
> assert "MY_LABEL" in spancat.labels
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------ |
|
||||
| **RETURNS** | The labels added to the component. ~~Tuple[str, ...]~~ |
|
||||
|
||||
## SpanCategorizer.label_data {id="label_data",tag="property"}
|
||||
|
||||
The labels currently added to the component and their internal meta information.
|
||||
This is the data generated by [`init labels`](/api/cli#init-labels) and used by
|
||||
[`SpanCategorizer.initialize`](/api/spancategorizer#initialize) to initialize
|
||||
the model with a pre-defined label set.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> labels = spancat.label_data
|
||||
> spancat.initialize(lambda: [], nlp=nlp, labels=labels)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------- |
|
||||
| **RETURNS** | The label data added to the component. ~~Tuple[str, ...]~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = spancat.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `cfg` | The config file. You usually don't want to exclude this. |
|
||||
| `model` | The binary model data. You usually don't want to exclude this. |
|
||||
|
||||
## Suggesters {id="suggesters",tag="registered functions",source="spacy/pipeline/spancat.py"}
|
||||
|
||||
### spacy.ngram_suggester.v1 {id="ngram_suggester"}
|
||||
|
||||
> #### Example Config
|
||||
>
|
||||
> ```ini
|
||||
> [components.spancat.suggester]
|
||||
> @misc = "spacy.ngram_suggester.v1"
|
||||
> sizes = [1, 2, 3]
|
||||
> ```
|
||||
|
||||
Suggest all spans of the given lengths. Spans are returned as a ragged array of
|
||||
integers. The array has two columns, indicating the start and end position.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------------------------------------- |
|
||||
| `sizes` | The phrase lengths to suggest. For example, `[1, 2]` will suggest phrases consisting of 1 or 2 tokens. ~~List[int]~~ |
|
||||
| **CREATES** | The suggester function. ~~Callable[[Iterable[Doc], Optional[Ops]], Ragged]~~ |
|
||||
|
||||
### spacy.ngram_range_suggester.v1 {id="ngram_range_suggester"}
|
||||
|
||||
> #### Example Config
|
||||
>
|
||||
> ```ini
|
||||
> [components.spancat.suggester]
|
||||
> @misc = "spacy.ngram_range_suggester.v1"
|
||||
> min_size = 2
|
||||
> max_size = 4
|
||||
> ```
|
||||
|
||||
Suggest all spans of at least length `min_size` and at most length `max_size`
|
||||
(both inclusive). Spans are returned as a ragged array of integers. The array
|
||||
has two columns, indicating the start and end position.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------- |
|
||||
| `min_size` | The minimal phrase lengths to suggest (inclusive). ~~[int]~~ |
|
||||
| `max_size` | The maximal phrase lengths to suggest (inclusive). ~~[int]~~ |
|
||||
| **CREATES** | The suggester function. ~~Callable[[Iterable[Doc], Optional[Ops]], Ragged]~~ |
|
||||
|
||||
### spacy.preset_spans_suggester.v1 {id="preset_spans_suggester"}
|
||||
|
||||
> #### Example Config
|
||||
>
|
||||
> ```ini
|
||||
> [components.spancat.suggester]
|
||||
> @misc = "spacy.preset_spans_suggester.v1"
|
||||
> spans_key = "my_spans"
|
||||
> ```
|
||||
|
||||
Suggest all spans that are already stored in doc.spans[spans_key]. This is
|
||||
useful when an upstream component is used to set the spans on the Doc such as a
|
||||
[`SpanRuler`](/api/spanruler) or [`SpanFinder`](/api/spanfinder).
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------------------- |
|
||||
| `spans_key` | Key of [`Doc.spans`](/api/doc/#spans) that provides spans to suggest. ~~str~~ |
|
||||
| **CREATES** | The suggester function. ~~Callable[[Iterable[Doc], Optional[Ops]], Ragged]~~ |
|
||||
@@ -0,0 +1,372 @@
|
||||
---
|
||||
title: SpanFinder
|
||||
tag: class,experimental
|
||||
source: spacy/pipeline/span_finder.py
|
||||
version: 3.6
|
||||
teaser:
|
||||
'Pipeline component for identifying potentially overlapping spans of text'
|
||||
api_base_class: /api/pipe
|
||||
api_string_name: span_finder
|
||||
api_trainable: true
|
||||
---
|
||||
|
||||
The span finder identifies potentially overlapping, unlabeled spans. It
|
||||
identifies tokens that start or end spans and annotates unlabeled spans between
|
||||
starts and ends, with optional filters for min and max span length. It is
|
||||
intended for use in combination with a component like
|
||||
[`SpanCategorizer`](/api/spancategorizer) that may further filter or label the
|
||||
spans. Predicted spans will be saved in a [`SpanGroup`](/api/spangroup) on the
|
||||
doc under `doc.spans[spans_key]`, where `spans_key` is a component config
|
||||
setting.
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
Predictions will be saved to `Doc.spans[spans_key]` as a
|
||||
[`SpanGroup`](/api/spangroup).
|
||||
|
||||
`spans_key` defaults to `"sc"`, but can be passed as a parameter. The
|
||||
`span_finder` component will overwrite any existing spans under the spans key
|
||||
`doc.spans[spans_key]`.
|
||||
|
||||
| Location | Value |
|
||||
| ---------------------- | ---------------------------------- |
|
||||
| `Doc.spans[spans_key]` | The unlabeled spans. ~~SpanGroup~~ |
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config). See the
|
||||
[model architectures](/api/architectures) documentation for details on the
|
||||
architectures and their arguments and hyperparameters.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.pipeline.span_finder import DEFAULT_SPAN_FINDER_MODEL
|
||||
> config = {
|
||||
> "threshold": 0.5,
|
||||
> "spans_key": "my_spans",
|
||||
> "max_length": None,
|
||||
> "min_length": None,
|
||||
> "model": DEFAULT_SPAN_FINDER_MODEL,
|
||||
> }
|
||||
> nlp.add_pipe("span_finder", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `model` | A model instance that is given a list of documents and predicts a probability for each token. ~~Model[List[Doc], Floats2d]~~ |
|
||||
| `spans_key` | Key of the [`Doc.spans`](/api/doc#spans) dict to save the spans under. During initialization and training, the component will look for spans on the reference document under the same key. Defaults to `"sc"`. ~~str~~ |
|
||||
| `threshold` | Minimum probability to consider a prediction positive. Defaults to `0.5`. ~~float~~ |
|
||||
| `max_length` | Maximum length of the produced spans, defaults to `25`. ~~Optional[int]~~ |
|
||||
| `min_length` | Minimum length of the produced spans, defaults to `None` meaning shortest span length is 1. ~~Optional[int]~~ |
|
||||
| `scorer` | The scoring method. Defaults to [`Scorer.score_spans`](/api/scorer#score_spans) for `Doc.spans[spans_key]` with overlapping spans allowed. ~~Optional[Callable]~~ |
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/span_finder.py
|
||||
```
|
||||
|
||||
## SpanFinder.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe with default model
|
||||
> span_finder = nlp.add_pipe("span_finder")
|
||||
>
|
||||
> # Construction via add_pipe with custom model
|
||||
> config = {"model": {"@architectures": "my_span_finder"}}
|
||||
> span_finder = nlp.add_pipe("span_finder", config=config)
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy.pipeline import SpanFinder
|
||||
> span_finder = SpanFinder(nlp.vocab, model)
|
||||
> ```
|
||||
|
||||
Create a new pipeline instance. In your application, you would normally use a
|
||||
shortcut for this and instantiate the component using its string name and
|
||||
[`nlp.add_pipe`](/api/language#create_pipe).
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | A model instance that is given a list of documents and predicts a probability for each token. ~~Model[List[Doc], Floats2d]~~ |
|
||||
| `name` | String name of the component instance. Used to add entries to the `losses` during training. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `spans_key` | Key of the [`Doc.spans`](/api/doc#spans) dict to save the spans under. During initialization and training, the component will look for spans on the reference document under the same key. Defaults to `"sc"`. ~~str~~ |
|
||||
| `threshold` | Minimum probability to consider a prediction positive. Defaults to `0.5`. ~~float~~ |
|
||||
| `max_length` | Maximum length of the produced spans, defaults to `None` meaning unlimited length. ~~Optional[int]~~ |
|
||||
| `min_length` | Minimum length of the produced spans, defaults to `None` meaning shortest span length is 1. ~~Optional[int]~~ |
|
||||
| `scorer` | The scoring method. Defaults to [`Scorer.score_spans`](/api/scorer#score_spans) for `Doc.spans[spans_key]` with overlapping spans allowed. ~~Optional[Callable]~~ |
|
||||
|
||||
## SpanFinder.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document. The document is modified in place, and returned.
|
||||
This usually happens under the hood when the `nlp` object is called on a text
|
||||
and all pipeline components are applied to the `Doc` in order. Both
|
||||
[`__call__`](/api/spanfinder#call) and [`pipe`](/api/spanfinder#pipe) delegate
|
||||
to the [`predict`](/api/spanfinder#predict) and
|
||||
[`set_annotations`](/api/spanfinder#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> span_finder = nlp.add_pipe("span_finder")
|
||||
> # This usually happens under the hood
|
||||
> processed = span_finder(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## SpanFinder.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order. Both [`__call__`](/api/spanfinder#call) and
|
||||
[`pipe`](/api/spanfinder#pipe) delegate to the
|
||||
[`predict`](/api/spanfinder#predict) and
|
||||
[`set_annotations`](/api/spanfinder#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_finder = nlp.add_pipe("span_finder")
|
||||
> for doc in span_finder.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `stream` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## SpanFinder.initialize {id="initialize",tag="method"}
|
||||
|
||||
Initialize the component for training. `get_examples` should be a function that
|
||||
returns an iterable of [`Example`](/api/example) objects. **At least one example
|
||||
should be supplied.** The data examples are used to **initialize the model** of
|
||||
the component and can either be the full training data or a representative
|
||||
sample. Initialization includes validating the network and
|
||||
[inferring missing shapes](https://thinc.ai/docs/usage-models#validation) This
|
||||
method is typically called by [`Language.initialize`](/api/language#initialize)
|
||||
and lets you customize arguments it receives via the
|
||||
[`[initialize.components]`](/api/data-formats#config-initialize) block in the
|
||||
config.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_finder = nlp.add_pipe("span_finder")
|
||||
> span_finder.initialize(lambda: examples, nlp=nlp)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Must contain at least one `Example`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
|
||||
## SpanFinder.predict {id="predict",tag="method"}
|
||||
|
||||
Apply the component's model to a batch of [`Doc`](/api/doc) objects without
|
||||
modifying them.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_finder = nlp.add_pipe("span_finder")
|
||||
> scores = span_finder.predict([doc1, doc2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| `docs` | The documents to predict. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | The model's prediction for each document. |
|
||||
|
||||
## SpanFinder.set_annotations {id="set_annotations",tag="method"}
|
||||
|
||||
Modify a batch of [`Doc`](/api/doc) objects using pre-computed scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_finder = nlp.add_pipe("span_finder")
|
||||
> scores = span_finder.predict(docs)
|
||||
> span_finder.set_annotations(docs, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | ---------------------------------------------------- |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `scores` | The scores to set, produced by `SpanFinder.predict`. |
|
||||
|
||||
## SpanFinder.update {id="update",tag="method"}
|
||||
|
||||
Learn from a batch of [`Example`](/api/example) objects containing the
|
||||
predictions and gold-standard annotations, and update the component's model.
|
||||
Delegates to [`predict`](/api/spanfinder#predict) and
|
||||
[`get_loss`](/api/spanfinder#get_loss).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_finder = nlp.add_pipe("span_finder")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = span_finder.update(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## SpanFinder.get_loss {id="get_loss",tag="method"}
|
||||
|
||||
Find the loss and gradient of loss for the batch of documents and their
|
||||
predicted scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_finder = nlp.add_pipe("span_finder")
|
||||
> scores = span_finder.predict([eg.predicted for eg in examples])
|
||||
> loss, d_loss = span_finder.get_loss(examples, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------ |
|
||||
| `examples` | The batch of examples. ~~Iterable[Example]~~ |
|
||||
| `spans_scores` | Scores representing the model's predictions. ~~Tuple[Ragged, Floats2d]~~ |
|
||||
| **RETURNS** | The loss and the gradient, i.e. `(loss, gradient)`. ~~Tuple[float, Floats2d]~~ |
|
||||
|
||||
## SpanFinder.create_optimizer {id="create_optimizer",tag="method"}
|
||||
|
||||
Create an optimizer for the pipeline component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_finder = nlp.add_pipe("span_finder")
|
||||
> optimizer = span_finder.create_optimizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The optimizer. ~~Optimizer~~ |
|
||||
|
||||
## SpanFinder.use_params {id="use_params",tag="method, contextmanager"}
|
||||
|
||||
Modify the pipe's model to use the given parameter values.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_finder = nlp.add_pipe("span_finder")
|
||||
> with span_finder.use_params(optimizer.averages):
|
||||
> span_finder.to_disk("/best_model")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------- |
|
||||
| `params` | The parameter values to use in the model. ~~dict~~ |
|
||||
|
||||
## SpanFinder.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_finder = nlp.add_pipe("span_finder")
|
||||
> span_finder.to_disk("/path/to/span_finder")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## SpanFinder.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_finder = nlp.add_pipe("span_finder")
|
||||
> span_finder.from_disk("/path/to/span_finder")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `SpanFinder` object. ~~SpanFinder~~ |
|
||||
|
||||
## SpanFinder.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_finder = nlp.add_pipe("span_finder")
|
||||
> span_finder_bytes = span_finder.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `SpanFinder` object. ~~bytes~~ |
|
||||
|
||||
## SpanFinder.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_finder_bytes = span_finder.to_bytes()
|
||||
> span_finder = nlp.add_pipe("span_finder")
|
||||
> span_finder.from_bytes(span_finder_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `SpanFinder` object. ~~SpanFinder~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = span_finder.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `cfg` | The config file. You usually don't want to exclude this. |
|
||||
| `model` | The binary model data. You usually don't want to exclude this. |
|
||||
@@ -0,0 +1,317 @@
|
||||
---
|
||||
title: SpanGroup
|
||||
tag: class
|
||||
source: spacy/tokens/span_group.pyx
|
||||
version: 3
|
||||
---
|
||||
|
||||
A group of arbitrary, potentially overlapping [`Span`](/api/span) objects that
|
||||
all belong to the same [`Doc`](/api/doc) object. The group can be named, and you
|
||||
can attach additional attributes to it. Span groups are generally accessed via
|
||||
the [`Doc.spans`](/api/doc#spans) attribute, which will convert lists of spans
|
||||
into a `SpanGroup` object for you automatically on assignment. `SpanGroup`
|
||||
objects behave similar to `list`s, so you can append `Span` objects to them or
|
||||
access a member at a given index.
|
||||
|
||||
## SpanGroup.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create a `SpanGroup`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Their goi ng home")
|
||||
> spans = [doc[0:1], doc[1:3]]
|
||||
>
|
||||
> # Construction 1
|
||||
> from spacy.tokens import SpanGroup
|
||||
>
|
||||
> group = SpanGroup(doc, name="errors", spans=spans, attrs={"annotator": "matt"})
|
||||
> doc.spans["errors"] = group
|
||||
>
|
||||
> # Construction 2
|
||||
> doc.spans["errors"] = spans
|
||||
> assert isinstance(doc.spans["errors"], SpanGroup)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `doc` | The document the span group belongs to. ~~Doc~~ |
|
||||
| _keyword-only_ | |
|
||||
| `name` | The name of the span group. If the span group is created automatically on assignment to `doc.spans`, the key name is used. Defaults to `""`. ~~str~~ |
|
||||
| `attrs` | Optional JSON-serializable attributes to attach to the span group. ~~Dict[str, Any]~~ |
|
||||
| `spans` | The spans to add to the span group. ~~Iterable[Span]~~ |
|
||||
|
||||
## SpanGroup.doc {id="doc",tag="property"}
|
||||
|
||||
The [`Doc`](/api/doc) object the span group is referring to.
|
||||
|
||||
<Infobox title="SpanGroup and Doc lifecycle" variant="warning">
|
||||
|
||||
When a `Doc` object is garbage collected, any related `SpanGroup` object won't
|
||||
be functional anymore, as these objects use a `weakref` to refer to the
|
||||
document. An error will be raised as the internal `doc` object will be `None`.
|
||||
To avoid this, make sure that the original `Doc` objects are still available in
|
||||
the scope of your function.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Their goi ng home")
|
||||
> doc.spans["errors"] = [doc[0:1], doc[1:3]]
|
||||
> assert doc.spans["errors"].doc == doc
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------- |
|
||||
| **RETURNS** | The reference document. ~~Doc~~ |
|
||||
|
||||
## SpanGroup.has_overlap {id="has_overlap",tag="property"}
|
||||
|
||||
Check whether the span group contains overlapping spans.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Their goi ng home")
|
||||
> doc.spans["errors"] = [doc[0:1], doc[1:3]]
|
||||
> assert not doc.spans["errors"].has_overlap
|
||||
> doc.spans["errors"].append(doc[2:4])
|
||||
> assert doc.spans["errors"].has_overlap
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------- |
|
||||
| **RETURNS** | Whether the span group contains overlaps. ~~bool~~ |
|
||||
|
||||
## SpanGroup.\_\_len\_\_ {id="len",tag="method"}
|
||||
|
||||
Get the number of spans in the group.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Their goi ng home")
|
||||
> doc.spans["errors"] = [doc[0:1], doc[1:3]]
|
||||
> assert len(doc.spans["errors"]) == 2
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------- |
|
||||
| **RETURNS** | The number of spans in the group. ~~int~~ |
|
||||
|
||||
## SpanGroup.\_\_getitem\_\_ {id="getitem",tag="method"}
|
||||
|
||||
Get a span from the group. Note that a copy of the span is returned, so if any
|
||||
changes are made to this span, they are not reflected in the corresponding
|
||||
member of the span group. The item or group will need to be reassigned for
|
||||
changes to be reflected in the span group.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Their goi ng home")
|
||||
> doc.spans["errors"] = [doc[0:1], doc[1:3]]
|
||||
> span = doc.spans["errors"][1]
|
||||
> assert span.text == "goi ng"
|
||||
> span.label_ = 'LABEL'
|
||||
> assert doc.spans["errors"][1].label_ != 'LABEL' # The span within the group was not updated
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------- |
|
||||
| `i` | The item index. ~~int~~ |
|
||||
| **RETURNS** | The span at the given index. ~~Span~~ |
|
||||
|
||||
## SpanGroup.\_\_setitem\_\_ {id="setitem",tag="method", version="3.3"}
|
||||
|
||||
Set a span in the span group.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Their goi ng home")
|
||||
> doc.spans["errors"] = [doc[0:1], doc[1:3]]
|
||||
> span = doc[0:2]
|
||||
> doc.spans["errors"][0] = span
|
||||
> assert doc.spans["errors"][0].text == "Their goi"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------ | ----------------------- |
|
||||
| `i` | The item index. ~~int~~ |
|
||||
| `span` | The new value. ~~Span~~ |
|
||||
|
||||
## SpanGroup.\_\_delitem\_\_ {id="delitem",tag="method", version="3.3"}
|
||||
|
||||
Delete a span from the span group.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Their goi ng home")
|
||||
> doc.spans["errors"] = [doc[0:1], doc[1:3]]
|
||||
> del doc.spans[0]
|
||||
> assert len(doc.spans["errors"]) == 1
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---- | ----------------------- |
|
||||
| `i` | The item index. ~~int~~ |
|
||||
|
||||
## SpanGroup.\_\_add\_\_ {id="add",tag="method", version="3.3"}
|
||||
|
||||
Concatenate the current span group with another span group and return the result
|
||||
in a new span group. Any `attrs` from the first span group will have precedence
|
||||
over `attrs` in the second.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Their goi ng home")
|
||||
> doc.spans["errors"] = [doc[0:1], doc[1:3]]
|
||||
> doc.spans["other"] = [doc[0:2], doc[2:4]]
|
||||
> span_group = doc.spans["errors"] + doc.spans["other"]
|
||||
> assert len(span_group) == 4
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------- |
|
||||
| `other` | The span group or spans to concatenate. ~~Union[SpanGroup, Iterable[Span]]~~ |
|
||||
| **RETURNS** | The new span group. ~~SpanGroup~~ |
|
||||
|
||||
## SpanGroup.\_\_iadd\_\_ {id="iadd",tag="method", version="3.3"}
|
||||
|
||||
Append an iterable of spans or the content of a span group to the current span
|
||||
group. Any `attrs` in the other span group will be added for keys that are not
|
||||
already present in the current span group.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Their goi ng home")
|
||||
> doc.spans["errors"] = [doc[0:1], doc[1:3]]
|
||||
> doc.spans["errors"] += [doc[3:4], doc[2:3]]
|
||||
> assert len(doc.spans["errors"]) == 4
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------------- |
|
||||
| `other` | The span group or spans to append. ~~Union[SpanGroup, Iterable[Span]]~~ |
|
||||
| **RETURNS** | The span group. ~~SpanGroup~~ |
|
||||
|
||||
## SpanGroup.\_\_iter\_\_ {id="iter",tag="method",version="3.5"}
|
||||
|
||||
Iterate over the spans in this span group.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Their goi ng home")
|
||||
> doc.spans["errors"] = [doc[0:1], doc[1:3]]
|
||||
> for error_span in doc.spans["errors"]:
|
||||
> print(error_span)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ----------------------------------- |
|
||||
| **YIELDS** | A span in this span group. ~~Span~~ |
|
||||
|
||||
|
||||
## SpanGroup.append {id="append",tag="method"}
|
||||
|
||||
Add a [`Span`](/api/span) object to the group. The span must refer to the same
|
||||
[`Doc`](/api/doc) object as the span group.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Their goi ng home")
|
||||
> doc.spans["errors"] = [doc[0:1]]
|
||||
> doc.spans["errors"].append(doc[1:3])
|
||||
> assert len(doc.spans["errors"]) == 2
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------ | ---------------------------- |
|
||||
| `span` | The span to append. ~~Span~~ |
|
||||
|
||||
## SpanGroup.extend {id="extend",tag="method"}
|
||||
|
||||
Add multiple [`Span`](/api/span) objects or contents of another `SpanGroup` to
|
||||
the group. All spans must refer to the same [`Doc`](/api/doc) object as the span
|
||||
group.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Their goi ng home")
|
||||
> doc.spans["errors"] = []
|
||||
> doc.spans["errors"].extend([doc[1:3], doc[0:1]])
|
||||
> assert len(doc.spans["errors"]) == 2
|
||||
> span_group = SpanGroup(doc, spans=[doc[1:4], doc[0:3]])
|
||||
> doc.spans["errors"].extend(span_group)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------- |
|
||||
| `spans` | The spans to add. ~~Union[SpanGroup, Iterable["Span"]]~~ |
|
||||
|
||||
## SpanGroup.copy {id="copy",tag="method", version="3.3"}
|
||||
|
||||
Return a copy of the span group.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import SpanGroup
|
||||
>
|
||||
> doc = nlp("Their goi ng home")
|
||||
> doc.spans["errors"] = [doc[1:3], doc[0:3]]
|
||||
> new_group = doc.spans["errors"].copy()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------------------- |
|
||||
| `doc` | The document to which the copy is bound. Defaults to `None` for the current doc. ~~Optional[Doc]~~ |
|
||||
| **RETURNS** | A copy of the `SpanGroup` object. ~~SpanGroup~~ |
|
||||
|
||||
## SpanGroup.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
Serialize the span group to a bytestring.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Their goi ng home")
|
||||
> doc.spans["errors"] = [doc[0:1], doc[1:3]]
|
||||
> group_bytes = doc.spans["errors"].to_bytes()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------- |
|
||||
| **RETURNS** | The serialized `SpanGroup`. ~~bytes~~ |
|
||||
|
||||
## SpanGroup.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the span group from a bytestring. Modifies the object in place and returns
|
||||
it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import SpanGroup
|
||||
>
|
||||
> doc = nlp("Their goi ng home")
|
||||
> doc.spans["errors"] = [doc[0:1], doc[1:3]]
|
||||
> group_bytes = doc.spans["errors"].to_bytes()
|
||||
> new_group = SpanGroup()
|
||||
> new_group.from_bytes(group_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | ------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| **RETURNS** | The `SpanGroup` object. ~~SpanGroup~~ |
|
||||
@@ -0,0 +1,353 @@
|
||||
---
|
||||
title: SpanRuler
|
||||
tag: class
|
||||
source: spacy/pipeline/span_ruler.py
|
||||
version: 3.3
|
||||
teaser: 'Pipeline component for rule-based span and named entity recognition'
|
||||
api_string_name: span_ruler
|
||||
api_trainable: false
|
||||
---
|
||||
|
||||
The span ruler lets you add spans to [`Doc.spans`](/api/doc#spans) and/or
|
||||
[`Doc.ents`](/api/doc#ents) using token-based rules or exact phrase matches. For
|
||||
usage examples, see the docs on
|
||||
[rule-based span matching](/usage/rule-based-matching#spanruler).
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
Matches will be saved to `Doc.spans[spans_key]` as a
|
||||
[`SpanGroup`](/api/spangroup) and/or to `Doc.ents`, where the annotation is
|
||||
saved in the `Token.ent_type` and `Token.ent_iob` fields.
|
||||
|
||||
| Location | Value |
|
||||
| ---------------------- | ----------------------------------------------------------------- |
|
||||
| `Doc.spans[spans_key]` | The annotated spans. ~~SpanGroup~~ |
|
||||
| `Doc.ents` | The annotated spans. ~~Tuple[Span]~~ |
|
||||
| `Token.ent_iob` | An enum encoding of the IOB part of the named entity tag. ~~int~~ |
|
||||
| `Token.ent_iob_` | The IOB part of the named entity tag. ~~str~~ |
|
||||
| `Token.ent_type` | The label part of the named entity tag (hash). ~~int~~ |
|
||||
| `Token.ent_type_` | The label part of the named entity tag. ~~str~~ |
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg`](/usage/training#config).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> config = {
|
||||
> "spans_key": "my_spans",
|
||||
> "validate": True,
|
||||
> "overwrite": False,
|
||||
> }
|
||||
> nlp.add_pipe("span_ruler", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| ---------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `spans_key` | The spans key to save the spans under. If `None`, no spans are saved. Defaults to `"ruler"`. ~~Optional[str]~~ |
|
||||
| `spans_filter` | The optional method to filter spans before they are assigned to doc.spans. Defaults to `None`. ~~Optional[Callable[[Iterable[Span], Iterable[Span]], List[Span]]]~~ |
|
||||
| `annotate_ents` | Whether to save spans to doc.ents. Defaults to `False`. ~~bool~~ |
|
||||
| `ents_filter` | The method to filter spans before they are assigned to doc.ents. Defaults to `util.filter_chain_spans`. ~~Callable[[Iterable[Span], Iterable[Span]], List[Span]]~~ |
|
||||
| `phrase_matcher_attr` | Token attribute to match on, passed to the internal `PhraseMatcher` as `attr`. Defaults to `None`. ~~Optional[Union[int, str]]~~ |
|
||||
| `matcher_fuzzy_compare` <Tag variant="new">3.5</Tag> | The fuzzy comparison method, passed on to the internal `Matcher`. Defaults to `spacy.matcher.levenshtein.levenshtein_compare`. ~~Callable~~ |
|
||||
| `validate` | Whether patterns should be validated, passed to `Matcher` and `PhraseMatcher` as `validate`. Defaults to `False`. ~~bool~~ |
|
||||
| `overwrite` | Whether to remove any existing spans under `Doc.spans[spans key]` if `spans_key` is set, or to remove any ents under `Doc.ents` if `annotate_ents` is set. Defaults to `True`. ~~bool~~ |
|
||||
| `scorer` | The scoring method. Defaults to [`Scorer.score_spans`](/api/scorer#score_spans) for `Doc.spans[spans_key]` with overlapping spans allowed. ~~Optional[Callable]~~ |
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/span_ruler.py
|
||||
```
|
||||
|
||||
## SpanRuler.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Initialize the span ruler. If patterns are supplied here, they need to be a list
|
||||
of dictionaries with a `"label"` and `"pattern"` key. A pattern can either be a
|
||||
token pattern (list) or a phrase pattern (string). For example:
|
||||
`{"label": "ORG", "pattern": "Apple"}`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe
|
||||
> ruler = nlp.add_pipe("span_ruler")
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy.pipeline import SpanRuler
|
||||
> ruler = SpanRuler(nlp, overwrite=True)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `nlp` | The shared nlp object to pass the vocab to the matchers and process phrase patterns. ~~Language~~ |
|
||||
| `name` | Instance name of the current pipeline component. Typically passed in automatically from the factory when the component is added. Used to disable the current span ruler while creating phrase patterns with the nlp object. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `spans_key` | The spans key to save the spans under. If `None`, no spans are saved. Defaults to `"ruler"`. ~~Optional[str]~~ |
|
||||
| `spans_filter` | The optional method to filter spans before they are assigned to doc.spans. Defaults to `None`. ~~Optional[Callable[[Iterable[Span], Iterable[Span]], List[Span]]]~~ |
|
||||
| `annotate_ents` | Whether to save spans to doc.ents. Defaults to `False`. ~~bool~~ |
|
||||
| `ents_filter` | The method to filter spans before they are assigned to doc.ents. Defaults to `util.filter_chain_spans`. ~~Callable[[Iterable[Span], Iterable[Span]], List[Span]]~~ |
|
||||
| `phrase_matcher_attr` | Token attribute to match on, passed to the internal PhraseMatcher as `attr`. Defaults to `None`. ~~Optional[Union[int, str]]~~ |
|
||||
| `matcher_fuzzy_compare` <Tag variant="new">3.5</Tag> | The fuzzy comparison method, passed on to the internal `Matcher`. Defaults to `spacy.matcher.levenshtein.levenshtein_compare`. ~~Callable~~ |
|
||||
| `validate` | Whether patterns should be validated, passed to Matcher and PhraseMatcher as `validate`. Defaults to `False`. ~~bool~~ |
|
||||
| `overwrite` | Whether to remove any existing spans under `Doc.spans[spans key]` if `spans_key` is set, or to remove any ents under `Doc.ents` if `annotate_ents` is set. Defaults to `True`. ~~bool~~ |
|
||||
| `scorer` | The scoring method. Defaults to [`Scorer.score_spans`](/api/scorer#score_spans) for `Doc.spans[spans_key]` with overlapping spans allowed. ~~Optional[Callable]~~ |
|
||||
|
||||
## SpanRuler.initialize {id="initialize",tag="method"}
|
||||
|
||||
Initialize the component with data and used before training to load in rules
|
||||
from a [pattern file](/usage/rule-based-matching/#spanruler-files). This method
|
||||
is typically called by [`Language.initialize`](/api/language#initialize) and
|
||||
lets you customize arguments it receives via the
|
||||
[`[initialize.components]`](/api/data-formats#config-initialize) block in the
|
||||
config. Any existing patterns are removed on initialization.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> span_ruler = nlp.add_pipe("span_ruler")
|
||||
> span_ruler.initialize(lambda: [], nlp=nlp, patterns=patterns)
|
||||
> ```
|
||||
>
|
||||
> ```ini
|
||||
> ### config.cfg
|
||||
> [initialize.components.span_ruler]
|
||||
>
|
||||
> [initialize.components.span_ruler.patterns]
|
||||
> @readers = "srsly.read_jsonl.v1"
|
||||
> path = "corpus/span_ruler_patterns.jsonl"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Not used by the `SpanRuler`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
| `patterns` | The list of patterns. Defaults to `None`. ~~Optional[Sequence[Dict[str, Union[str, List[Dict[str, Any]]]]]]~~ |
|
||||
|
||||
## SpanRuler.\_\_len\_\_ {id="len",tag="method"}
|
||||
|
||||
The number of all patterns added to the span ruler.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("span_ruler")
|
||||
> assert len(ruler) == 0
|
||||
> ruler.add_patterns([{"label": "ORG", "pattern": "Apple"}])
|
||||
> assert len(ruler) == 1
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------- |
|
||||
| **RETURNS** | The number of patterns. ~~int~~ |
|
||||
|
||||
## SpanRuler.\_\_contains\_\_ {id="contains",tag="method"}
|
||||
|
||||
Whether a label is present in the patterns.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("span_ruler")
|
||||
> ruler.add_patterns([{"label": "ORG", "pattern": "Apple"}])
|
||||
> assert "ORG" in ruler
|
||||
> assert not "PERSON" in ruler
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------- |
|
||||
| `label` | The label to check. ~~str~~ |
|
||||
| **RETURNS** | Whether the span ruler contains the label. ~~bool~~ |
|
||||
|
||||
## SpanRuler.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Find matches in the `Doc` and add them to `doc.spans[span_key]` and/or
|
||||
`doc.ents`. Typically, this happens automatically after the component has been
|
||||
added to the pipeline using [`nlp.add_pipe`](/api/language#add_pipe). If the
|
||||
span ruler was initialized with `overwrite=True`, existing spans and entities
|
||||
will be removed.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("span_ruler")
|
||||
> ruler.add_patterns([{"label": "ORG", "pattern": "Apple"}])
|
||||
>
|
||||
> doc = nlp("A text about Apple.")
|
||||
> spans = [(span.text, span.label_) for span in doc.spans["ruler"]]
|
||||
> assert spans == [("Apple", "ORG")]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------- |
|
||||
| `doc` | The `Doc` object to process, e.g. the `Doc` in the pipeline. ~~Doc~~ |
|
||||
| **RETURNS** | The modified `Doc` with added spans/entities. ~~Doc~~ |
|
||||
|
||||
## SpanRuler.add_patterns {id="add_patterns",tag="method"}
|
||||
|
||||
Add patterns to the span ruler. A pattern can either be a token pattern (list of
|
||||
dicts) or a phrase pattern (string). For more details, see the usage guide on
|
||||
[rule-based matching](/usage/rule-based-matching).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> patterns = [
|
||||
> {"label": "ORG", "pattern": "Apple"},
|
||||
> {"label": "GPE", "pattern": [{"lower": "san"}, {"lower": "francisco"}]}
|
||||
> ]
|
||||
> ruler = nlp.add_pipe("span_ruler")
|
||||
> ruler.add_patterns(patterns)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ---------------------------------------------------------------- |
|
||||
| `patterns` | The patterns to add. ~~List[Dict[str, Union[str, List[dict]]]]~~ |
|
||||
|
||||
## SpanRuler.remove {id="remove",tag="method"}
|
||||
|
||||
Remove patterns by label from the span ruler. A `ValueError` is raised if the
|
||||
label does not exist in any patterns.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> patterns = [{"label": "ORG", "pattern": "Apple", "id": "apple"}]
|
||||
> ruler = nlp.add_pipe("span_ruler")
|
||||
> ruler.add_patterns(patterns)
|
||||
> ruler.remove("ORG")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------- |
|
||||
| `label` | The label of the pattern rule. ~~str~~ |
|
||||
|
||||
## SpanRuler.remove_by_id {id="remove_by_id",tag="method"}
|
||||
|
||||
Remove patterns by ID from the span ruler. A `ValueError` is raised if the ID
|
||||
does not exist in any patterns.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> patterns = [{"label": "ORG", "pattern": "Apple", "id": "apple"}]
|
||||
> ruler = nlp.add_pipe("span_ruler")
|
||||
> ruler.add_patterns(patterns)
|
||||
> ruler.remove_by_id("apple")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | ----------------------------------- |
|
||||
| `pattern_id` | The ID of the pattern rule. ~~str~~ |
|
||||
|
||||
## SpanRuler.clear {id="clear",tag="method"}
|
||||
|
||||
Remove all patterns the span ruler.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> patterns = [{"label": "ORG", "pattern": "Apple", "id": "apple"}]
|
||||
> ruler = nlp.add_pipe("span_ruler")
|
||||
> ruler.add_patterns(patterns)
|
||||
> ruler.clear()
|
||||
> ```
|
||||
|
||||
## SpanRuler.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Save the span ruler patterns to a directory. The patterns will be saved as
|
||||
newline-delimited JSON (JSONL).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("span_ruler")
|
||||
> ruler.to_disk("/path/to/span_ruler")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------ | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
|
||||
## SpanRuler.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the span ruler from a path.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("span_ruler")
|
||||
> ruler.from_disk("/path/to/span_ruler")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| **RETURNS** | The modified `SpanRuler` object. ~~SpanRuler~~ |
|
||||
|
||||
## SpanRuler.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
Serialize the span ruler to a bytestring.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler = nlp.add_pipe("span_ruler")
|
||||
> ruler_bytes = ruler.to_bytes()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------- |
|
||||
| **RETURNS** | The serialized patterns. ~~bytes~~ |
|
||||
|
||||
## SpanRuler.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> ruler_bytes = ruler.to_bytes()
|
||||
> ruler = nlp.add_pipe("span_ruler")
|
||||
> ruler.from_bytes(ruler_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | ---------------------------------------------- |
|
||||
| `bytes_data` | The bytestring to load. ~~bytes~~ |
|
||||
| **RETURNS** | The modified `SpanRuler` object. ~~SpanRuler~~ |
|
||||
|
||||
## SpanRuler.labels {id="labels",tag="property"}
|
||||
|
||||
All labels present in the match patterns.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------- |
|
||||
| **RETURNS** | The string labels. ~~Tuple[str, ...]~~ |
|
||||
|
||||
## SpanRuler.ids {id="ids",tag="property"}
|
||||
|
||||
All IDs present in the `id` property of the match patterns.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------- |
|
||||
| **RETURNS** | The string IDs. ~~Tuple[str, ...]~~ |
|
||||
|
||||
## SpanRuler.patterns {id="patterns",tag="property"}
|
||||
|
||||
All patterns that were added to the span ruler.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------- |
|
||||
| **RETURNS** | The original patterns, one dictionary per pattern. ~~List[Dict[str, Union[str, dict]]]~~ |
|
||||
|
||||
## Attributes {id="attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| ---------------- | -------------------------------------------------------------------------------- |
|
||||
| `key` | The spans key that spans are saved under. ~~Optional[str]~~ |
|
||||
| `matcher` | The underlying matcher used to process token patterns. ~~Matcher~~ |
|
||||
| `phrase_matcher` | The underlying phrase matcher used to process phrase patterns. ~~PhraseMatcher~~ |
|
||||
@@ -0,0 +1,197 @@
|
||||
---
|
||||
title: StringStore
|
||||
tag: class
|
||||
source: spacy/strings.pyx
|
||||
---
|
||||
|
||||
Look up strings by 64-bit hashes. As of v2.0, spaCy uses hash values instead of
|
||||
integer IDs. This ensures that strings always map to the same ID, even from
|
||||
different `StringStores`.
|
||||
|
||||
<Infobox variant ="warning">
|
||||
|
||||
Note that a `StringStore` instance is not static. It increases in size as texts
|
||||
with new tokens are processed.
|
||||
|
||||
</Infobox>
|
||||
|
||||
## StringStore.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create the `StringStore`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.strings import StringStore
|
||||
> stringstore = StringStore(["apple", "orange"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------- | ---------------------------------------------------------------------- |
|
||||
| `strings` | A sequence of strings to add to the store. ~~Optional[Iterable[str]]~~ |
|
||||
|
||||
## StringStore.\_\_len\_\_ {id="len",tag="method"}
|
||||
|
||||
Get the number of strings in the store.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> stringstore = StringStore(["apple", "orange"])
|
||||
> assert len(stringstore) == 2
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| **RETURNS** | The number of strings in the store. ~~int~~ |
|
||||
|
||||
## StringStore.\_\_getitem\_\_ {id="getitem",tag="method"}
|
||||
|
||||
Retrieve a string from a given hash, or vice versa.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> stringstore = StringStore(["apple", "orange"])
|
||||
> apple_hash = stringstore["apple"]
|
||||
> assert apple_hash == 8566208034543834098
|
||||
> assert stringstore[apple_hash] == "apple"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------- |
|
||||
| `string_or_id` | The value to encode. ~~Union[bytes, str, int]~~ |
|
||||
| **RETURNS** | The value to be retrieved. ~~Union[str, int]~~ |
|
||||
|
||||
## StringStore.\_\_contains\_\_ {id="contains",tag="method"}
|
||||
|
||||
Check whether a string is in the store.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> stringstore = StringStore(["apple", "orange"])
|
||||
> assert "apple" in stringstore
|
||||
> assert not "cherry" in stringstore
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------- |
|
||||
| `string` | The string to check. ~~str~~ |
|
||||
| **RETURNS** | Whether the store contains the string. ~~bool~~ |
|
||||
|
||||
## StringStore.\_\_iter\_\_ {id="iter",tag="method"}
|
||||
|
||||
Iterate over the strings in the store, in order. Note that a newly initialized
|
||||
store will always include an empty string `""` at position `0`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> stringstore = StringStore(["apple", "orange"])
|
||||
> all_strings = [s for s in stringstore]
|
||||
> assert all_strings == ["apple", "orange"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ------------------------------ |
|
||||
| **YIELDS** | A string in the store. ~~str~~ |
|
||||
|
||||
## StringStore.add {id="add",tag="method",version="2"}
|
||||
|
||||
Add a string to the `StringStore`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> stringstore = StringStore(["apple", "orange"])
|
||||
> banana_hash = stringstore.add("banana")
|
||||
> assert len(stringstore) == 3
|
||||
> assert banana_hash == 2525716904149915114
|
||||
> assert stringstore[banana_hash] == "banana"
|
||||
> assert stringstore["banana"] == banana_hash
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `string` | The string to add. ~~str~~ |
|
||||
| **RETURNS** | The string's hash value. ~~int~~ |
|
||||
|
||||
## StringStore.to_disk {id="to_disk",tag="method",version="2"}
|
||||
|
||||
Save the current state to a directory.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> stringstore.to_disk("/path/to/strings")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------ | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
|
||||
## StringStore.from_disk {id="from_disk",tag="method",version="2"}
|
||||
|
||||
Loads state from a directory. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.strings import StringStore
|
||||
> stringstore = StringStore().from_disk("/path/to/strings")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| **RETURNS** | The modified `StringStore` object. ~~StringStore~~ |
|
||||
|
||||
## StringStore.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
Serialize the current state to a binary string.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> store_bytes = stringstore.to_bytes()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------- |
|
||||
| **RETURNS** | The serialized form of the `StringStore` object. ~~bytes~~ |
|
||||
|
||||
## StringStore.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load state from a binary string.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.strings import StringStore
|
||||
> store_bytes = stringstore.to_bytes()
|
||||
> new_store = StringStore().from_bytes(store_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | ----------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| **RETURNS** | The `StringStore` object. ~~StringStore~~ |
|
||||
|
||||
## Utilities {id="util"}
|
||||
|
||||
### strings.hash_string {id="hash_string",tag="function"}
|
||||
|
||||
Get a 64-bit hash for a given string.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.strings import hash_string
|
||||
> assert hash_string("apple") == 8566208034543834098
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------- |
|
||||
| `string` | The string to hash. ~~str~~ |
|
||||
| **RETURNS** | The hash. ~~int~~ |
|
||||
@@ -0,0 +1,448 @@
|
||||
---
|
||||
title: Tagger
|
||||
tag: class
|
||||
source: spacy/pipeline/tagger.pyx
|
||||
teaser: 'Pipeline component for part-of-speech tagging'
|
||||
api_base_class: /api/pipe
|
||||
api_string_name: tagger
|
||||
api_trainable: true
|
||||
---
|
||||
|
||||
A trainable pipeline component to predict part-of-speech tags for any
|
||||
part-of-speech tag set.
|
||||
|
||||
In the pre-trained pipelines, the tag schemas vary by language; see the
|
||||
[individual model pages](/models) for details.
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
Predictions are assigned to `Token.tag`.
|
||||
|
||||
| Location | Value |
|
||||
| ------------ | ---------------------------------- |
|
||||
| `Token.tag` | The part of speech (hash). ~~int~~ |
|
||||
| `Token.tag_` | The part of speech. ~~str~~ |
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config). See the
|
||||
[model architectures](/api/architectures) documentation for details on the
|
||||
architectures and their arguments and hyperparameters.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.pipeline.tagger import DEFAULT_TAGGER_MODEL
|
||||
> config = {"model": DEFAULT_TAGGER_MODEL}
|
||||
> nlp.add_pipe("tagger", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `model` | A model instance that predicts the tag probabilities. The output vectors should match the number of tags in size, and be normalized as probabilities (all scores between 0 and 1, with the rows summing to `1`). Defaults to [Tagger](/api/architectures#Tagger). ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
| `overwrite` <Tag variant="new">3.2</Tag> | Whether existing annotation is overwritten. Defaults to `False`. ~~bool~~ |
|
||||
| `scorer` <Tag variant="new">3.2</Tag> | The scoring method. Defaults to [`Scorer.score_token_attr`](/api/scorer#score_token_attr) for the attribute `"tag"`. ~~Optional[Callable]~~ |
|
||||
| `neg_prefix` <Tag variant="new">3.2.1</Tag> | The prefix used to specify incorrect tags while training. The tagger will learn not to predict exactly this tag. Defaults to `!`. ~~str~~ |
|
||||
| `label_smoothing` <Tag variant="new">3.6</Tag> | [Label smoothing](https://arxiv.org/abs/1906.02629) factor. Defaults to `0.0`. ~~float~~ |
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/tagger.pyx
|
||||
```
|
||||
|
||||
## Tagger.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe with default model
|
||||
> tagger = nlp.add_pipe("tagger")
|
||||
>
|
||||
> # Construction via create_pipe with custom model
|
||||
> config = {"model": {"@architectures": "my_tagger"}}
|
||||
> tagger = nlp.add_pipe("tagger", config=config)
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy.pipeline import Tagger
|
||||
> tagger = Tagger(nlp.vocab, model)
|
||||
> ```
|
||||
|
||||
Create a new pipeline instance. In your application, you would normally use a
|
||||
shortcut for this and instantiate the component using its string name and
|
||||
[`nlp.add_pipe`](/api/language#add_pipe).
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | A model instance that predicts the tag probabilities. The output vectors should match the number of tags in size, and be normalized as probabilities (all scores between 0 and 1, with the rows summing to `1`). ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
| `name` | String name of the component instance. Used to add entries to the `losses` during training. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `overwrite` <Tag variant="new">3.2</Tag> | Whether existing annotation is overwritten. Defaults to `False`. ~~bool~~ |
|
||||
| `scorer` <Tag variant="new">3.2</Tag> | The scoring method. Defaults to [`Scorer.score_token_attr`](/api/scorer#score_token_attr) for the attribute `"tag"`. ~~Optional[Callable]~~ |
|
||||
|
||||
## Tagger.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document. The document is modified in place, and returned.
|
||||
This usually happens under the hood when the `nlp` object is called on a text
|
||||
and all pipeline components are applied to the `Doc` in order. Both
|
||||
[`__call__`](/api/tagger#call) and [`pipe`](/api/tagger#pipe) delegate to the
|
||||
[`predict`](/api/tagger#predict) and
|
||||
[`set_annotations`](/api/tagger#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> tagger = nlp.add_pipe("tagger")
|
||||
> # This usually happens under the hood
|
||||
> processed = tagger(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## Tagger.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order. Both [`__call__`](/api/tagger#call) and
|
||||
[`pipe`](/api/tagger#pipe) delegate to the [`predict`](/api/tagger#predict) and
|
||||
[`set_annotations`](/api/tagger#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tagger = nlp.add_pipe("tagger")
|
||||
> for doc in tagger.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `stream` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## Tagger.initialize {id="initialize",tag="method",version="3"}
|
||||
|
||||
Initialize the component for training. `get_examples` should be a function that
|
||||
returns an iterable of [`Example`](/api/example) objects. **At least one example
|
||||
should be supplied.** The data examples are used to **initialize the model** of
|
||||
the component and can either be the full training data or a representative
|
||||
sample. Initialization includes validating the network,
|
||||
[inferring missing shapes](https://thinc.ai/docs/usage-models#validation) and
|
||||
setting up the label scheme based on the data. This method is typically called
|
||||
by [`Language.initialize`](/api/language#initialize) and lets you customize
|
||||
arguments it receives via the
|
||||
[`[initialize.components]`](/api/data-formats#config-initialize) block in the
|
||||
config.
|
||||
|
||||
<Infobox variant="warning" title="Changed in v3.0" id="begin_training">
|
||||
|
||||
This method was previously called `begin_training`.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tagger = nlp.add_pipe("tagger")
|
||||
> tagger.initialize(lambda: examples, nlp=nlp)
|
||||
> ```
|
||||
>
|
||||
> ```ini
|
||||
> ### config.cfg
|
||||
> [initialize.components.tagger]
|
||||
>
|
||||
> [initialize.components.tagger.labels]
|
||||
> @readers = "spacy.read_labels.v1"
|
||||
> path = "corpus/labels/tagger.json
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Must contain at least one `Example`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
| `labels` | The label information to add to the component, as provided by the [`label_data`](#label_data) property after initialization. To generate a reusable JSON file from your data, you should run the [`init labels`](/api/cli#init-labels) command. If no labels are provided, the `get_examples` callback is used to extract the labels from the data, which may be a lot slower. ~~Optional[Iterable[str]]~~ |
|
||||
|
||||
## Tagger.predict {id="predict",tag="method"}
|
||||
|
||||
Apply the component's model to a batch of [`Doc`](/api/doc) objects, without
|
||||
modifying them.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tagger = nlp.add_pipe("tagger")
|
||||
> scores = tagger.predict([doc1, doc2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| `docs` | The documents to predict. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | The model's prediction for each document. |
|
||||
|
||||
## Tagger.set_annotations {id="set_annotations",tag="method"}
|
||||
|
||||
Modify a batch of [`Doc`](/api/doc) objects, using pre-computed scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tagger = nlp.add_pipe("tagger")
|
||||
> scores = tagger.predict([doc1, doc2])
|
||||
> tagger.set_annotations([doc1, doc2], scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | ------------------------------------------------ |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `scores` | The scores to set, produced by `Tagger.predict`. |
|
||||
|
||||
## Tagger.update {id="update",tag="method"}
|
||||
|
||||
Learn from a batch of [`Example`](/api/example) objects containing the
|
||||
predictions and gold-standard annotations, and update the component's model.
|
||||
Delegates to [`predict`](/api/tagger#predict) and
|
||||
[`get_loss`](/api/tagger#get_loss).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tagger = nlp.add_pipe("tagger")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = tagger.update(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## Tagger.rehearse {id="rehearse",tag="method,experimental",version="3"}
|
||||
|
||||
Perform a "rehearsal" update from a batch of data. Rehearsal updates teach the
|
||||
current model to make predictions similar to an initial model, to try to address
|
||||
the "catastrophic forgetting" problem. This feature is experimental.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tagger = nlp.add_pipe("tagger")
|
||||
> optimizer = nlp.resume_training()
|
||||
> losses = tagger.rehearse(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## Tagger.get_loss {id="get_loss",tag="method"}
|
||||
|
||||
Find the loss and gradient of loss for the batch of documents and their
|
||||
predicted scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tagger = nlp.add_pipe("tagger")
|
||||
> scores = tagger.predict([eg.predicted for eg in examples])
|
||||
> loss, d_loss = tagger.get_loss(examples, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------------------------- |
|
||||
| `examples` | The batch of examples. ~~Iterable[Example]~~ |
|
||||
| `scores` | Scores representing the model's predictions. |
|
||||
| **RETURNS** | The loss and the gradient, i.e. `(loss, gradient)`. ~~Tuple[float, float]~~ |
|
||||
|
||||
## Tagger.create_optimizer {id="create_optimizer",tag="method"}
|
||||
|
||||
Create an optimizer for the pipeline component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tagger = nlp.add_pipe("tagger")
|
||||
> optimizer = tagger.create_optimizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The optimizer. ~~Optimizer~~ |
|
||||
|
||||
## Tagger.use_params {id="use_params",tag="method, contextmanager"}
|
||||
|
||||
Modify the pipe's model, to use the given parameter values. At the end of the
|
||||
context, the original parameters are restored.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tagger = nlp.add_pipe("tagger")
|
||||
> with tagger.use_params(optimizer.averages):
|
||||
> tagger.to_disk("/best_model")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------- |
|
||||
| `params` | The parameter values to use in the model. ~~dict~~ |
|
||||
|
||||
## Tagger.add_label {id="add_label",tag="method"}
|
||||
|
||||
Add a new label to the pipe. Raises an error if the output dimension is already
|
||||
set, or if the model has already been fully [initialized](#initialize). Note
|
||||
that you don't have to call this method if you provide a **representative data
|
||||
sample** to the [`initialize`](#initialize) method. In this case, all labels
|
||||
found in the sample will be automatically added to the model, and the output
|
||||
dimension will be [inferred](/usage/layers-architectures#thinc-shape-inference)
|
||||
automatically.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tagger = nlp.add_pipe("tagger")
|
||||
> tagger.add_label("MY_LABEL")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------- |
|
||||
| `label` | The label to add. ~~str~~ |
|
||||
| **RETURNS** | `0` if the label is already present, otherwise `1`. ~~int~~ |
|
||||
|
||||
## Tagger.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tagger = nlp.add_pipe("tagger")
|
||||
> tagger.to_disk("/path/to/tagger")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## Tagger.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tagger = nlp.add_pipe("tagger")
|
||||
> tagger.from_disk("/path/to/tagger")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `Tagger` object. ~~Tagger~~ |
|
||||
|
||||
## Tagger.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tagger = nlp.add_pipe("tagger")
|
||||
> tagger_bytes = tagger.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `Tagger` object. ~~bytes~~ |
|
||||
|
||||
## Tagger.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tagger_bytes = tagger.to_bytes()
|
||||
> tagger = nlp.add_pipe("tagger")
|
||||
> tagger.from_bytes(tagger_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `Tagger` object. ~~Tagger~~ |
|
||||
|
||||
## Tagger.labels {id="labels",tag="property"}
|
||||
|
||||
The labels currently added to the component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tagger.add_label("MY_LABEL")
|
||||
> assert "MY_LABEL" in tagger.labels
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------ |
|
||||
| **RETURNS** | The labels added to the component. ~~Tuple[str, ...]~~ |
|
||||
|
||||
## Tagger.label_data {id="label_data",tag="property",version="3"}
|
||||
|
||||
The labels currently added to the component and their internal meta information.
|
||||
This is the data generated by [`init labels`](/api/cli#init-labels) and used by
|
||||
[`Tagger.initialize`](/api/tagger#initialize) to initialize the model with a
|
||||
pre-defined label set.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> labels = tagger.label_data
|
||||
> tagger.initialize(lambda: [], nlp=nlp, labels=labels)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------- |
|
||||
| **RETURNS** | The label data added to the component. ~~Tuple[str, ...]~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = tagger.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `cfg` | The config file. You usually don't want to exclude this. |
|
||||
| `model` | The binary model data. You usually don't want to exclude this. |
|
||||
@@ -0,0 +1,509 @@
|
||||
---
|
||||
title: TextCategorizer
|
||||
tag: class
|
||||
source: spacy/pipeline/textcat.py
|
||||
version: 2
|
||||
teaser: 'Pipeline component for text classification'
|
||||
api_base_class: /api/pipe
|
||||
api_string_name: textcat
|
||||
api_trainable: true
|
||||
---
|
||||
|
||||
The text categorizer predicts **categories over a whole document**. and comes in
|
||||
two flavors: `textcat` and `textcat_multilabel`. When you need to predict
|
||||
exactly one true label per document, use the `textcat` which has mutually
|
||||
exclusive labels. If you want to perform multi-label classification and predict
|
||||
zero, one or more true labels per document, use the `textcat_multilabel`
|
||||
component instead. For a binary classification task, you can use `textcat` with
|
||||
**two** labels or `textcat_multilabel` with **one** label.
|
||||
|
||||
Both components are documented on this page.
|
||||
|
||||
<Infobox title="Migration from v2" variant="warning">
|
||||
|
||||
In spaCy v2, the `textcat` component could also perform **multi-label
|
||||
classification**, and even used this setting by default. Since v3.0, the
|
||||
component `textcat_multilabel` should be used for multi-label classification
|
||||
instead. The `textcat` component is now used for mutually exclusive classes
|
||||
only.
|
||||
|
||||
</Infobox>
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
Predictions will be saved to `doc.cats` as a dictionary, where the key is the
|
||||
name of the category and the value is a score between 0 and 1 (inclusive). For
|
||||
`textcat` (exclusive categories), the scores will sum to 1, while for
|
||||
`textcat_multilabel` there is no particular guarantee about their sum. This also
|
||||
means that for `textcat`, missing values are equated to a value of 0 (i.e.
|
||||
`False`) and are counted as such towards the loss and scoring metrics. This is
|
||||
not the case for `textcat_multilabel`, where missing values in the gold standard
|
||||
data do not influence the loss or accuracy calculations.
|
||||
|
||||
Note that when assigning values to create training data, the score of each
|
||||
category must be 0 or 1. Using other values, for example to create a document
|
||||
that is a little bit in category A and a little bit in category B, is not
|
||||
supported.
|
||||
|
||||
| Location | Value |
|
||||
| ---------- | ------------------------------------- |
|
||||
| `Doc.cats` | Category scores. ~~Dict[str, float]~~ |
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config). See the
|
||||
[model architectures](/api/architectures) documentation for details on the
|
||||
architectures and their arguments and hyperparameters.
|
||||
|
||||
> #### Example (textcat)
|
||||
>
|
||||
> ```python
|
||||
> from spacy.pipeline.textcat import DEFAULT_SINGLE_TEXTCAT_MODEL
|
||||
> config = {
|
||||
> "model": DEFAULT_SINGLE_TEXTCAT_MODEL,
|
||||
> }
|
||||
> nlp.add_pipe("textcat", config=config)
|
||||
> ```
|
||||
|
||||
> #### Example (textcat_multilabel)
|
||||
>
|
||||
> ```python
|
||||
> from spacy.pipeline.textcat_multilabel import DEFAULT_MULTI_TEXTCAT_MODEL
|
||||
> config = {
|
||||
> "threshold": 0.5,
|
||||
> "model": DEFAULT_MULTI_TEXTCAT_MODEL,
|
||||
> }
|
||||
> nlp.add_pipe("textcat_multilabel", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `threshold` | Cutoff to consider a prediction "positive", relevant for `textcat_multilabel` when calculating accuracy scores. ~~float~~ |
|
||||
| `model` | A model instance that predicts scores for each category. Defaults to [TextCatEnsemble](/api/architectures#TextCatEnsemble). ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
| `scorer` | The scoring method. Defaults to [`Scorer.score_cats`](/api/scorer#score_cats) for the attribute `"cats"`. ~~Optional[Callable]~~ |
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/textcat.py
|
||||
```
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/textcat_multilabel.py
|
||||
```
|
||||
|
||||
## TextCategorizer.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe with default model
|
||||
> # Use 'textcat_multilabel' for multi-label classification
|
||||
> textcat = nlp.add_pipe("textcat")
|
||||
>
|
||||
> # Construction via add_pipe with custom model
|
||||
> config = {"model": {"@architectures": "my_textcat"}}
|
||||
> parser = nlp.add_pipe("textcat", config=config)
|
||||
>
|
||||
> # Construction from class
|
||||
> # Use 'MultiLabel_TextCategorizer' for multi-label classification
|
||||
> from spacy.pipeline import TextCategorizer
|
||||
> textcat = TextCategorizer(nlp.vocab, model, threshold=0.5)
|
||||
> ```
|
||||
|
||||
Create a new pipeline instance. In your application, you would normally use a
|
||||
shortcut for this and instantiate the component using its string name and
|
||||
[`nlp.add_pipe`](/api/language#create_pipe).
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | The Thinc [`Model`](https://thinc.ai/docs/api-model) powering the pipeline component. ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
| `name` | String name of the component instance. Used to add entries to the `losses` during training. ~~str~~ |
|
||||
| _keyword-only_ | |
|
||||
| `threshold` | Cutoff to consider a prediction "positive", relevant for `textcat_multilabel` when calculating accuracy scores. ~~float~~ |
|
||||
| `scorer` | The scoring method. Defaults to [`Scorer.score_cats`](/api/scorer#score_cats) for the attribute `"cats"`. ~~Optional[Callable]~~ |
|
||||
|
||||
## TextCategorizer.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document. The document is modified in place, and returned.
|
||||
This usually happens under the hood when the `nlp` object is called on a text
|
||||
and all pipeline components are applied to the `Doc` in order. Both
|
||||
[`__call__`](/api/textcategorizer#call) and [`pipe`](/api/textcategorizer#pipe)
|
||||
delegate to the [`predict`](/api/textcategorizer#predict) and
|
||||
[`set_annotations`](/api/textcategorizer#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> textcat = nlp.add_pipe("textcat")
|
||||
> # This usually happens under the hood
|
||||
> processed = textcat(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## TextCategorizer.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order. Both [`__call__`](/api/textcategorizer#call) and
|
||||
[`pipe`](/api/textcategorizer#pipe) delegate to the
|
||||
[`predict`](/api/textcategorizer#predict) and
|
||||
[`set_annotations`](/api/textcategorizer#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> textcat = nlp.add_pipe("textcat")
|
||||
> for doc in textcat.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `stream` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## TextCategorizer.initialize {id="initialize",tag="method",version="3"}
|
||||
|
||||
Initialize the component for training. `get_examples` should be a function that
|
||||
returns an iterable of [`Example`](/api/example) objects. **At least one example
|
||||
should be supplied.** The data examples are used to **initialize the model** of
|
||||
the component and can either be the full training data or a representative
|
||||
sample. Initialization includes validating the network,
|
||||
[inferring missing shapes](https://thinc.ai/docs/usage-models#validation) and
|
||||
setting up the label scheme based on the data. This method is typically called
|
||||
by [`Language.initialize`](/api/language#initialize) and lets you customize
|
||||
arguments it receives via the
|
||||
[`[initialize.components]`](/api/data-formats#config-initialize) block in the
|
||||
config.
|
||||
|
||||
<Infobox variant="warning" title="Changed in v3.0" id="begin_training">
|
||||
|
||||
This method was previously called `begin_training`.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> textcat = nlp.add_pipe("textcat")
|
||||
> textcat.initialize(lambda: examples, nlp=nlp)
|
||||
> ```
|
||||
>
|
||||
> ```ini
|
||||
> ### config.cfg
|
||||
> [initialize.components.textcat]
|
||||
> positive_label = "POS"
|
||||
>
|
||||
> [initialize.components.textcat.labels]
|
||||
> @readers = "spacy.read_labels.v1"
|
||||
> path = "corpus/labels/textcat.json
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Must contain at least one `Example`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
| `labels` | The label information to add to the component, as provided by the [`label_data`](#label_data) property after initialization. To generate a reusable JSON file from your data, you should run the [`init labels`](/api/cli#init-labels) command. If no labels are provided, the `get_examples` callback is used to extract the labels from the data, which may be a lot slower. ~~Optional[Iterable[str]]~~ |
|
||||
| `positive_label` | The positive label for a binary task with exclusive classes, `None` otherwise and by default. This parameter is only used during scoring. It is not available when using the `textcat_multilabel` component. ~~Optional[str]~~ |
|
||||
|
||||
## TextCategorizer.predict {id="predict",tag="method"}
|
||||
|
||||
Apply the component's model to a batch of [`Doc`](/api/doc) objects without
|
||||
modifying them.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> textcat = nlp.add_pipe("textcat")
|
||||
> scores = textcat.predict([doc1, doc2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| `docs` | The documents to predict. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | The model's prediction for each document. |
|
||||
|
||||
## TextCategorizer.set_annotations {id="set_annotations",tag="method"}
|
||||
|
||||
Modify a batch of [`Doc`](/api/doc) objects using pre-computed scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> textcat = nlp.add_pipe("textcat")
|
||||
> scores = textcat.predict(docs)
|
||||
> textcat.set_annotations(docs, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | --------------------------------------------------------- |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `scores` | The scores to set, produced by `TextCategorizer.predict`. |
|
||||
|
||||
## TextCategorizer.update {id="update",tag="method"}
|
||||
|
||||
Learn from a batch of [`Example`](/api/example) objects containing the
|
||||
predictions and gold-standard annotations, and update the component's model.
|
||||
Delegates to [`predict`](/api/textcategorizer#predict) and
|
||||
[`get_loss`](/api/textcategorizer#get_loss).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> textcat = nlp.add_pipe("textcat")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = textcat.update(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## TextCategorizer.rehearse {id="rehearse",tag="method,experimental",version="3"}
|
||||
|
||||
Perform a "rehearsal" update from a batch of data. Rehearsal updates teach the
|
||||
current model to make predictions similar to an initial model to try to address
|
||||
the "catastrophic forgetting" problem. This feature is experimental.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> textcat = nlp.add_pipe("textcat")
|
||||
> optimizer = nlp.resume_training()
|
||||
> losses = textcat.rehearse(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## TextCategorizer.get_loss {id="get_loss",tag="method"}
|
||||
|
||||
Find the loss and gradient of loss for the batch of documents and their
|
||||
predicted scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> textcat = nlp.add_pipe("textcat")
|
||||
> scores = textcat.predict([eg.predicted for eg in examples])
|
||||
> loss, d_loss = textcat.get_loss(examples, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------------------------------- |
|
||||
| `examples` | The batch of examples. ~~Iterable[Example]~~ |
|
||||
| `scores` | Scores representing the model's predictions. |
|
||||
| **RETURNS** | The loss and the gradient, i.e. `(loss, gradient)`. ~~Tuple[float, float]~~ |
|
||||
|
||||
## TextCategorizer.score {id="score",tag="method",version="3"}
|
||||
|
||||
Score a batch of examples.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> scores = textcat.score(examples)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------- |
|
||||
| `examples` | The examples to score. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| **RETURNS** | The scores, produced by [`Scorer.score_cats`](/api/scorer#score_cats). ~~Dict[str, Union[float, Dict[str, float]]]~~ |
|
||||
|
||||
## TextCategorizer.create_optimizer {id="create_optimizer",tag="method"}
|
||||
|
||||
Create an optimizer for the pipeline component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> textcat = nlp.add_pipe("textcat")
|
||||
> optimizer = textcat.create_optimizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The optimizer. ~~Optimizer~~ |
|
||||
|
||||
## TextCategorizer.use_params {id="use_params",tag="method, contextmanager"}
|
||||
|
||||
Modify the pipe's model to use the given parameter values.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> textcat = nlp.add_pipe("textcat")
|
||||
> with textcat.use_params(optimizer.averages):
|
||||
> textcat.to_disk("/best_model")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------- |
|
||||
| `params` | The parameter values to use in the model. ~~dict~~ |
|
||||
|
||||
## TextCategorizer.add_label {id="add_label",tag="method"}
|
||||
|
||||
Add a new label to the pipe. Raises an error if the output dimension is already
|
||||
set, or if the model has already been fully [initialized](#initialize). Note
|
||||
that you don't have to call this method if you provide a **representative data
|
||||
sample** to the [`initialize`](#initialize) method. In this case, all labels
|
||||
found in the sample will be automatically added to the model, and the output
|
||||
dimension will be [inferred](/usage/layers-architectures#thinc-shape-inference)
|
||||
automatically.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> textcat = nlp.add_pipe("textcat")
|
||||
> textcat.add_label("MY_LABEL")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------- |
|
||||
| `label` | The label to add. ~~str~~ |
|
||||
| **RETURNS** | `0` if the label is already present, otherwise `1`. ~~int~~ |
|
||||
|
||||
## TextCategorizer.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> textcat = nlp.add_pipe("textcat")
|
||||
> textcat.to_disk("/path/to/textcat")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## TextCategorizer.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> textcat = nlp.add_pipe("textcat")
|
||||
> textcat.from_disk("/path/to/textcat")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `TextCategorizer` object. ~~TextCategorizer~~ |
|
||||
|
||||
## TextCategorizer.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> textcat = nlp.add_pipe("textcat")
|
||||
> textcat_bytes = textcat.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `TextCategorizer` object. ~~bytes~~ |
|
||||
|
||||
## TextCategorizer.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> textcat_bytes = textcat.to_bytes()
|
||||
> textcat = nlp.add_pipe("textcat")
|
||||
> textcat.from_bytes(textcat_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `TextCategorizer` object. ~~TextCategorizer~~ |
|
||||
|
||||
## TextCategorizer.labels {id="labels",tag="property"}
|
||||
|
||||
The labels currently added to the component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> textcat.add_label("MY_LABEL")
|
||||
> assert "MY_LABEL" in textcat.labels
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------ |
|
||||
| **RETURNS** | The labels added to the component. ~~Tuple[str, ...]~~ |
|
||||
|
||||
## TextCategorizer.label_data {id="label_data",tag="property",version="3"}
|
||||
|
||||
The labels currently added to the component and their internal meta information.
|
||||
This is the data generated by [`init labels`](/api/cli#init-labels) and used by
|
||||
[`TextCategorizer.initialize`](/api/textcategorizer#initialize) to initialize
|
||||
the model with a pre-defined label set.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> labels = textcat.label_data
|
||||
> textcat.initialize(lambda: [], nlp=nlp, labels=labels)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------- |
|
||||
| **RETURNS** | The label data added to the component. ~~Tuple[str, ...]~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = textcat.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `cfg` | The config file. You usually don't want to exclude this. |
|
||||
| `model` | The binary model data. You usually don't want to exclude this. |
|
||||
@@ -0,0 +1,327 @@
|
||||
---
|
||||
title: Tok2Vec
|
||||
source: spacy/pipeline/tok2vec.py
|
||||
version: 3
|
||||
teaser: null
|
||||
api_base_class: /api/pipe
|
||||
api_string_name: tok2vec
|
||||
api_trainable: true
|
||||
---
|
||||
|
||||
Apply a "token-to-vector" model and set its outputs in the `Doc.tensor`
|
||||
attribute. This is mostly useful to **share a single subnetwork** between
|
||||
multiple components, e.g. to have one embedding and CNN network shared between a
|
||||
[`DependencyParser`](/api/dependencyparser), [`Tagger`](/api/tagger) and
|
||||
[`EntityRecognizer`](/api/entityrecognizer).
|
||||
|
||||
In order to use the `Tok2Vec` predictions, subsequent components should use the
|
||||
[Tok2VecListener](/api/architectures#Tok2VecListener) layer as the `tok2vec`
|
||||
subnetwork of their model. This layer will read data from the `doc.tensor`
|
||||
attribute during prediction. During training, the `Tok2Vec` component will save
|
||||
its prediction and backprop callback for each batch, so that the subsequent
|
||||
components can backpropagate to the shared weights. This implementation is used
|
||||
because it allows us to avoid relying on object identity within the models to
|
||||
achieve the parameter sharing.
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config). See the
|
||||
[model architectures](/api/architectures) documentation for details on the
|
||||
architectures and their arguments and hyperparameters.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.pipeline.tok2vec import DEFAULT_TOK2VEC_MODEL
|
||||
> config = {"model": DEFAULT_TOK2VEC_MODEL}
|
||||
> nlp.add_pipe("tok2vec", config=config)
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| ------- | ------------------------------------------------------------------------------------------------------------------ |
|
||||
| `model` | The model to use. Defaults to [HashEmbedCNN](/api/architectures#HashEmbedCNN). ~~Model[List[Doc], List[Floats2d]~~ |
|
||||
|
||||
```python
|
||||
%%GITHUB_SPACY/spacy/pipeline/tok2vec.py
|
||||
```
|
||||
|
||||
## Tok2Vec.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe with default model
|
||||
> tok2vec = nlp.add_pipe("tok2vec")
|
||||
>
|
||||
> # Construction via add_pipe with custom model
|
||||
> config = {"model": {"@architectures": "my_tok2vec"}}
|
||||
> parser = nlp.add_pipe("tok2vec", config=config)
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy.pipeline import Tok2Vec
|
||||
> tok2vec = Tok2Vec(nlp.vocab, model)
|
||||
> ```
|
||||
|
||||
Create a new pipeline instance. In your application, you would normally use a
|
||||
shortcut for this and instantiate the component using its string name and
|
||||
[`nlp.add_pipe`](/api/language#create_pipe).
|
||||
|
||||
| Name | Description |
|
||||
| ------- | ------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | The Thinc [`Model`](https://thinc.ai/docs/api-model) powering the pipeline component. ~~Model[List[Doc], List[Floats2d]~~ |
|
||||
| `name` | String name of the component instance. Used to add entries to the `losses` during training. ~~str~~ |
|
||||
|
||||
## Tok2Vec.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document and add context-sensitive embeddings to the
|
||||
`Doc.tensor` attribute, allowing them to be used as features by downstream
|
||||
components. The document is modified in place, and returned. This usually
|
||||
happens under the hood when the `nlp` object is called on a text and all
|
||||
pipeline components are applied to the `Doc` in order. Both
|
||||
[`__call__`](/api/tok2vec#call) and [`pipe`](/api/tok2vec#pipe) delegate to the
|
||||
[`predict`](/api/tok2vec#predict) and
|
||||
[`set_annotations`](/api/tok2vec#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> tok2vec = nlp.add_pipe("tok2vec")
|
||||
> # This usually happens under the hood
|
||||
> processed = tok2vec(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## Tok2Vec.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order. Both [`__call__`](/api/tok2vec#call) and
|
||||
[`pipe`](/api/tok2vec#pipe) delegate to the [`predict`](/api/tok2vec#predict)
|
||||
and [`set_annotations`](/api/tok2vec#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tok2vec = nlp.add_pipe("tok2vec")
|
||||
> for doc in tok2vec.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `stream` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## Tok2Vec.initialize {id="initialize",tag="method"}
|
||||
|
||||
Initialize the component for training and return an
|
||||
[`Optimizer`](https://thinc.ai/docs/api-optimizers). `get_examples` should be a
|
||||
function that returns an iterable of [`Example`](/api/example) objects. **At
|
||||
least one example should be supplied.** The data examples are used to
|
||||
**initialize the model** of the component and can either be the full training
|
||||
data or a representative sample. Initialization includes validating the network,
|
||||
[inferring missing shapes](https://thinc.ai/docs/usage-models#validation) and
|
||||
setting up the label scheme based on the data. This method is typically called
|
||||
by [`Language.initialize`](/api/language#initialize).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tok2vec = nlp.add_pipe("tok2vec")
|
||||
> tok2vec.initialize(lambda: examples, nlp=nlp)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Must contain at least one `Example`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
|
||||
## Tok2Vec.predict {id="predict",tag="method"}
|
||||
|
||||
Apply the component's model to a batch of [`Doc`](/api/doc) objects without
|
||||
modifying them.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tok2vec = nlp.add_pipe("tok2vec")
|
||||
> scores = tok2vec.predict([doc1, doc2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| `docs` | The documents to predict. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | The model's prediction for each document. |
|
||||
|
||||
## Tok2Vec.set_annotations {id="set_annotations",tag="method"}
|
||||
|
||||
Modify a batch of [`Doc`](/api/doc) objects, using pre-computed scores.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tok2vec = nlp.add_pipe("tok2vec")
|
||||
> scores = tok2vec.predict(docs)
|
||||
> tok2vec.set_annotations(docs, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | ------------------------------------------------- |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `scores` | The scores to set, produced by `Tok2Vec.predict`. |
|
||||
|
||||
## Tok2Vec.update {id="update",tag="method"}
|
||||
|
||||
Learn from a batch of [`Example`](/api/example) objects containing the
|
||||
predictions and gold-standard annotations, and update the component's model.
|
||||
Delegates to [`predict`](/api/tok2vec#predict).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tok2vec = nlp.add_pipe("tok2vec")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = tok2vec.update(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects to learn from. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## Tok2Vec.create_optimizer {id="create_optimizer",tag="method"}
|
||||
|
||||
Create an optimizer for the pipeline component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tok2vec = nlp.add_pipe("tok2vec")
|
||||
> optimizer = tok2vec.create_optimizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The optimizer. ~~Optimizer~~ |
|
||||
|
||||
## Tok2Vec.use_params {id="use_params",tag="method, contextmanager"}
|
||||
|
||||
Modify the pipe's model to use the given parameter values. At the end of the
|
||||
context, the original parameters are restored.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tok2vec = nlp.add_pipe("tok2vec")
|
||||
> with tok2vec.use_params(optimizer.averages):
|
||||
> tok2vec.to_disk("/best_model")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------- |
|
||||
| `params` | The parameter values to use in the model. ~~dict~~ |
|
||||
|
||||
## Tok2Vec.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tok2vec = nlp.add_pipe("tok2vec")
|
||||
> tok2vec.to_disk("/path/to/tok2vec")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## Tok2Vec.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tok2vec = nlp.add_pipe("tok2vec")
|
||||
> tok2vec.from_disk("/path/to/tok2vec")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `Tok2Vec` object. ~~Tok2Vec~~ |
|
||||
|
||||
## Tok2Vec.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tok2vec = nlp.add_pipe("tok2vec")
|
||||
> tok2vec_bytes = tok2vec.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `Tok2Vec` object. ~~bytes~~ |
|
||||
|
||||
## Tok2Vec.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tok2vec_bytes = tok2vec.to_bytes()
|
||||
> tok2vec = nlp.add_pipe("tok2vec")
|
||||
> tok2vec.from_bytes(tok2vec_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `Tok2Vec` object. ~~Tok2Vec~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = tok2vec.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `cfg` | The config file. You usually don't want to exclude this. |
|
||||
| `model` | The binary model data. You usually don't want to exclude this. |
|
||||
@@ -0,0 +1,477 @@
|
||||
---
|
||||
title: Token
|
||||
teaser: An individual token — i.e. a word, punctuation symbol, whitespace, etc.
|
||||
tag: class
|
||||
source: spacy/tokens/token.pyx
|
||||
---
|
||||
|
||||
## Token.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Construct a `Token` object.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> token = doc[0]
|
||||
> assert token.text == "Give"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | --------------------------------------------------- |
|
||||
| `vocab` | A storage container for lexical types. ~~Vocab~~ |
|
||||
| `doc` | The parent document. ~~Doc~~ |
|
||||
| `offset` | The index of the token within the document. ~~int~~ |
|
||||
|
||||
## Token.\_\_len\_\_ {id="len",tag="method"}
|
||||
|
||||
The number of unicode characters in the token, i.e. `token.text`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> token = doc[0]
|
||||
> assert len(token) == 4
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------ |
|
||||
| **RETURNS** | The number of unicode characters in the token. ~~int~~ |
|
||||
|
||||
## Token.set_extension {id="set_extension",tag="classmethod",version="2"}
|
||||
|
||||
Define a custom attribute on the `Token` which becomes available via `Token._`.
|
||||
For details, see the documentation on
|
||||
[custom attributes](/usage/processing-pipelines#custom-components-attributes).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Token
|
||||
> fruit_getter = lambda token: token.text in ("apple", "pear", "banana")
|
||||
> Token.set_extension("is_fruit", getter=fruit_getter)
|
||||
> doc = nlp("I have an apple")
|
||||
> assert doc[3]._.is_fruit
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `name` | Name of the attribute to set by the extension. For example, `"my_attr"` will be available as `token._.my_attr`. ~~str~~ |
|
||||
| `default` | Optional default value of the attribute if no getter or method is defined. ~~Optional[Any]~~ |
|
||||
| `method` | Set a custom method on the object, for example `token._.compare(other_token)`. ~~Optional[Callable[[Token, ...], Any]]~~ |
|
||||
| `getter` | Getter function that takes the object and returns an attribute value. Is called when the user accesses the `._` attribute. ~~Optional[Callable[[Token], Any]]~~ |
|
||||
| `setter` | Setter function that takes the `Token` and a value, and modifies the object. Is called when the user writes to the `Token._` attribute. ~~Optional[Callable[[Token, Any], None]]~~ |
|
||||
| `force` | Force overwriting existing attribute. ~~bool~~ |
|
||||
|
||||
## Token.get_extension {id="get_extension",tag="classmethod",version="2"}
|
||||
|
||||
Look up a previously registered extension by name. Returns a 4-tuple
|
||||
`(default, method, getter, setter)` if the extension is registered. Raises a
|
||||
`KeyError` otherwise.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Token
|
||||
> Token.set_extension("is_fruit", default=False)
|
||||
> extension = Token.get_extension("is_fruit")
|
||||
> assert extension == (False, None, None, None)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `name` | Name of the extension. ~~str~~ |
|
||||
| **RETURNS** | A `(default, method, getter, setter)` tuple of the extension. ~~Tuple[Optional[Any], Optional[Callable], Optional[Callable], Optional[Callable]]~~ |
|
||||
|
||||
## Token.has_extension {id="has_extension",tag="classmethod",version="2"}
|
||||
|
||||
Check whether an extension has been registered on the `Token` class.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Token
|
||||
> Token.set_extension("is_fruit", default=False)
|
||||
> assert Token.has_extension("is_fruit")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------- |
|
||||
| `name` | Name of the extension to check. ~~str~~ |
|
||||
| **RETURNS** | Whether the extension has been registered. ~~bool~~ |
|
||||
|
||||
## Token.remove_extension {id="remove_extension",tag="classmethod",version="2.0.11"}
|
||||
|
||||
Remove a previously registered extension.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import Token
|
||||
> Token.set_extension("is_fruit", default=False)
|
||||
> removed = Token.remove_extension("is_fruit")
|
||||
> assert not Token.has_extension("is_fruit")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `name` | Name of the extension. ~~str~~ |
|
||||
| **RETURNS** | A `(default, method, getter, setter)` tuple of the removed extension. ~~Tuple[Optional[Any], Optional[Callable], Optional[Callable], Optional[Callable]]~~ |
|
||||
|
||||
## Token.check_flag {id="check_flag",tag="method"}
|
||||
|
||||
Check the value of a boolean flag.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.attrs import IS_TITLE
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> token = doc[0]
|
||||
> assert token.check_flag(IS_TITLE) == True
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------- |
|
||||
| `flag_id` | The attribute ID of the flag to check. ~~int~~ |
|
||||
| **RETURNS** | Whether the flag is set. ~~bool~~ |
|
||||
|
||||
## Token.similarity {id="similarity",tag="method",model="vectors"}
|
||||
|
||||
Compute a semantic similarity estimate. Defaults to cosine over vectors.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> apples, _, oranges = nlp("apples and oranges")
|
||||
> apples_oranges = apples.similarity(oranges)
|
||||
> oranges_apples = oranges.similarity(apples)
|
||||
> assert apples_oranges == oranges_apples
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| other | The object to compare with. By default, accepts `Doc`, `Span`, `Token` and `Lexeme` objects. ~~Union[Doc, Span, Token, Lexeme]~~ |
|
||||
| **RETURNS** | A scalar similarity score. Higher is more similar. ~~float~~ |
|
||||
|
||||
## Token.nbor {id="nbor",tag="method"}
|
||||
|
||||
Get a neighboring token.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> give_nbor = doc[0].nbor()
|
||||
> assert give_nbor.text == "it"
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------------- |
|
||||
| `i` | The relative position of the token to get. Defaults to `1`. ~~int~~ |
|
||||
| **RETURNS** | The token at position `self.doc[self.i+i]`. ~~Token~~ |
|
||||
|
||||
## Token.set_morph {id="set_morph",tag="method"}
|
||||
|
||||
Set the morphological analysis from a UD FEATS string, hash value of a UD FEATS
|
||||
string, features dict or `MorphAnalysis`. The value `None` can be used to reset
|
||||
the morph to an unset state.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> doc[0].set_morph("Mood=Imp|VerbForm=Fin")
|
||||
> assert "Mood=Imp" in doc[0].morph
|
||||
> assert doc[0].morph.get("Mood") == ["Imp"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | --------------------------------------------------------------------------------- |
|
||||
| features | The morphological features to set. ~~Union[int, dict, str, MorphAnalysis, None]~~ |
|
||||
|
||||
## Token.has_morph {id="has_morph",tag="method"}
|
||||
|
||||
Check whether the token has annotated morph information. Return `False` when the
|
||||
morph annotation is unset/missing.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------- |
|
||||
| **RETURNS** | Whether the morph annotation is set. ~~bool~~ |
|
||||
|
||||
## Token.is_ancestor {id="is_ancestor",tag="method",model="parser"}
|
||||
|
||||
Check whether this token is a parent, grandparent, etc. of another in the
|
||||
dependency tree.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> give = doc[0]
|
||||
> it = doc[1]
|
||||
> assert give.is_ancestor(it)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------- |
|
||||
| descendant | Another token. ~~Token~~ |
|
||||
| **RETURNS** | Whether this token is the ancestor of the descendant. ~~bool~~ |
|
||||
|
||||
## Token.ancestors {id="ancestors",tag="property",model="parser"}
|
||||
|
||||
A sequence of the token's syntactic ancestors (parents, grandparents, etc).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> it_ancestors = doc[1].ancestors
|
||||
> assert [t.text for t in it_ancestors] == ["Give"]
|
||||
> he_ancestors = doc[4].ancestors
|
||||
> assert [t.text for t in he_ancestors] == ["pleaded"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ------------------------------------------------------------------------------- |
|
||||
| **YIELDS** | A sequence of ancestor tokens such that `ancestor.is_ancestor(self)`. ~~Token~~ |
|
||||
|
||||
## Token.conjuncts {id="conjuncts",tag="property",model="parser"}
|
||||
|
||||
A tuple of coordinated tokens, not including the token itself.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like apples and oranges")
|
||||
> apples_conjuncts = doc[2].conjuncts
|
||||
> assert [t.text for t in apples_conjuncts] == ["oranges"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------- |
|
||||
| **RETURNS** | The coordinated tokens. ~~Tuple[Token, ...]~~ |
|
||||
|
||||
## Token.children {id="children",tag="property",model="parser"}
|
||||
|
||||
A sequence of the token's immediate syntactic children.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> give_children = doc[0].children
|
||||
> assert [t.text for t in give_children] == ["it", "back", "!"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ------------------------------------------------------- |
|
||||
| **YIELDS** | A child token such that `child.head == self`. ~~Token~~ |
|
||||
|
||||
## Token.lefts {id="lefts",tag="property",model="parser"}
|
||||
|
||||
The leftward immediate children of the word in the syntactic dependency parse.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like New York in Autumn.")
|
||||
> lefts = [t.text for t in doc[3].lefts]
|
||||
> assert lefts == ["New"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ------------------------------------ |
|
||||
| **YIELDS** | A left-child of the token. ~~Token~~ |
|
||||
|
||||
## Token.rights {id="rights",tag="property",model="parser"}
|
||||
|
||||
The rightward immediate children of the word in the syntactic dependency parse.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like New York in Autumn.")
|
||||
> rights = [t.text for t in doc[3].rights]
|
||||
> assert rights == ["in"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ------------------------------------- |
|
||||
| **YIELDS** | A right-child of the token. ~~Token~~ |
|
||||
|
||||
## Token.n_lefts {id="n_lefts",tag="property",model="parser"}
|
||||
|
||||
The number of leftward immediate children of the word in the syntactic
|
||||
dependency parse.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like New York in Autumn.")
|
||||
> assert doc[3].n_lefts == 1
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------- |
|
||||
| **RETURNS** | The number of left-child tokens. ~~int~~ |
|
||||
|
||||
## Token.n_rights {id="n_rights",tag="property",model="parser"}
|
||||
|
||||
The number of rightward immediate children of the word in the syntactic
|
||||
dependency parse.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like New York in Autumn.")
|
||||
> assert doc[3].n_rights == 1
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------- |
|
||||
| **RETURNS** | The number of right-child tokens. ~~int~~ |
|
||||
|
||||
## Token.subtree {id="subtree",tag="property",model="parser"}
|
||||
|
||||
A sequence containing the token and all the token's syntactic descendants.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("Give it back! He pleaded.")
|
||||
> give_subtree = doc[0].subtree
|
||||
> assert [t.text for t in give_subtree] == ["Give", "it", "back", "!"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ------------------------------------------------------------------------------------ |
|
||||
| **YIELDS** | A descendant token such that `self.is_ancestor(token)` or `token == self`. ~~Token~~ |
|
||||
|
||||
## Token.has_vector {id="has_vector",tag="property",model="vectors"}
|
||||
|
||||
A boolean value indicating whether a word vector is associated with the token.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like apples")
|
||||
> apples = doc[2]
|
||||
> assert apples.has_vector
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------ |
|
||||
| **RETURNS** | Whether the token has a vector data attached. ~~bool~~ |
|
||||
|
||||
## Token.vector {id="vector",tag="property",model="vectors"}
|
||||
|
||||
A real-valued meaning representation.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like apples")
|
||||
> apples = doc[2]
|
||||
> assert apples.vector.dtype == "float32"
|
||||
> assert apples.vector.shape == (300,)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------------------------------------- |
|
||||
| **RETURNS** | A 1-dimensional array representing the token's vector. ~~numpy.ndarray[ndim=1, dtype=float32]~~ |
|
||||
|
||||
## Token.vector_norm {id="vector_norm",tag="property",model="vectors"}
|
||||
|
||||
The L2 norm of the token's vector representation.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like apples and pasta")
|
||||
> apples = doc[2]
|
||||
> pasta = doc[4]
|
||||
> apples.vector_norm # 6.89589786529541
|
||||
> pasta.vector_norm # 7.759851932525635
|
||||
> assert apples.vector_norm != pasta.vector_norm
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------------------------- |
|
||||
| **RETURNS** | The L2 norm of the vector representation. ~~float~~ |
|
||||
|
||||
## Attributes {id="attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `doc` | The parent document. ~~Doc~~ |
|
||||
| `lex` <Tag variant="new">3</Tag> | The underlying lexeme. ~~Lexeme~~ |
|
||||
| `sent` | The sentence span that this token is a part of. ~~Span~~ |
|
||||
| `text` | Verbatim text content. ~~str~~ |
|
||||
| `text_with_ws` | Text content, with trailing space character if present. ~~str~~ |
|
||||
| `whitespace_` | Trailing space character if present. ~~str~~ |
|
||||
| `orth` | ID of the verbatim text content. ~~int~~ |
|
||||
| `orth_` | Verbatim text content (identical to `Token.text`). Exists mostly for consistency with the other attributes. ~~str~~ |
|
||||
| `vocab` | The vocab object of the parent `Doc`. ~~vocab~~ |
|
||||
| `tensor` | The token's slice of the parent `Doc`'s tensor. ~~numpy.ndarray~~ |
|
||||
| `head` | The syntactic parent, or "governor", of this token. ~~Token~~ |
|
||||
| `left_edge` | The leftmost token of this token's syntactic descendants. ~~Token~~ |
|
||||
| `right_edge` | The rightmost token of this token's syntactic descendants. ~~Token~~ |
|
||||
| `i` | The index of the token within the parent document. ~~int~~ |
|
||||
| `ent_type` | Named entity type. ~~int~~ |
|
||||
| `ent_type_` | Named entity type. ~~str~~ |
|
||||
| `ent_iob` | IOB code of named entity tag. `3` means the token begins an entity, `2` means it is outside an entity, `1` means it is inside an entity, and `0` means no entity tag is set. ~~int~~ |
|
||||
| `ent_iob_` | IOB code of named entity tag. "B" means the token begins an entity, "I" means it is inside an entity, "O" means it is outside an entity, and "" means no entity tag is set. ~~str~~ |
|
||||
| `ent_kb_id` | Knowledge base ID that refers to the named entity this token is a part of, if any. ~~int~~ |
|
||||
| `ent_kb_id_` | Knowledge base ID that refers to the named entity this token is a part of, if any. ~~str~~ |
|
||||
| `ent_id` | ID of the entity the token is an instance of, if any. Currently not used, but potentially for coreference resolution. ~~int~~ |
|
||||
| `ent_id_` | ID of the entity the token is an instance of, if any. Currently not used, but potentially for coreference resolution. ~~str~~ |
|
||||
| `lemma` | Base form of the token, with no inflectional suffixes. ~~int~~ |
|
||||
| `lemma_` | Base form of the token, with no inflectional suffixes. ~~str~~ |
|
||||
| `norm` | The token's norm, i.e. a normalized form of the token text. Can be set in the language's [tokenizer exceptions](/usage/linguistic-features#language-data). ~~int~~ |
|
||||
| `norm_` | The token's norm, i.e. a normalized form of the token text. Can be set in the language's [tokenizer exceptions](/usage/linguistic-features#language-data). ~~str~~ |
|
||||
| `lower` | Lowercase form of the token. ~~int~~ |
|
||||
| `lower_` | Lowercase form of the token text. Equivalent to `Token.text.lower()`. ~~str~~ |
|
||||
| `shape` | Transform of the token's string to show orthographic features. Alphabetic characters are replaced by `x` or `X`, and numeric characters are replaced by `d`, and sequences of the same character are truncated after length 4. For example,`"Xxxx"`or`"dd"`. ~~int~~ |
|
||||
| `shape_` | Transform of the token's string to show orthographic features. Alphabetic characters are replaced by `x` or `X`, and numeric characters are replaced by `d`, and sequences of the same character are truncated after length 4. For example,`"Xxxx"`or`"dd"`. ~~str~~ |
|
||||
| `prefix` | Hash value of a length-N substring from the start of the token. Defaults to `N=1`. ~~int~~ |
|
||||
| `prefix_` | A length-N substring from the start of the token. Defaults to `N=1`. ~~str~~ |
|
||||
| `suffix` | Hash value of a length-N substring from the end of the token. Defaults to `N=3`. ~~int~~ |
|
||||
| `suffix_` | Length-N substring from the end of the token. Defaults to `N=3`. ~~str~~ |
|
||||
| `is_alpha` | Does the token consist of alphabetic characters? Equivalent to `token.text.isalpha()`. ~~bool~~ |
|
||||
| `is_ascii` | Does the token consist of ASCII characters? Equivalent to `all(ord(c) < 128 for c in token.text)`. ~~bool~~ |
|
||||
| `is_digit` | Does the token consist of digits? Equivalent to `token.text.isdigit()`. ~~bool~~ |
|
||||
| `is_lower` | Is the token in lowercase? Equivalent to `token.text.islower()`. ~~bool~~ |
|
||||
| `is_upper` | Is the token in uppercase? Equivalent to `token.text.isupper()`. ~~bool~~ |
|
||||
| `is_title` | Is the token in titlecase? Equivalent to `token.text.istitle()`. ~~bool~~ |
|
||||
| `is_punct` | Is the token punctuation? ~~bool~~ |
|
||||
| `is_left_punct` | Is the token a left punctuation mark, e.g. `"("` ? ~~bool~~ |
|
||||
| `is_right_punct` | Is the token a right punctuation mark, e.g. `")"` ? ~~bool~~ |
|
||||
| `is_sent_start` | Does the token start a sentence? ~~bool~~ or `None` if unknown. Defaults to `True` for the first token in the `Doc`. |
|
||||
| `is_sent_end` | Does the token end a sentence? ~~bool~~ or `None` if unknown. |
|
||||
| `is_space` | Does the token consist of whitespace characters? Equivalent to `token.text.isspace()`. ~~bool~~ |
|
||||
| `is_bracket` | Is the token a bracket? ~~bool~~ |
|
||||
| `is_quote` | Is the token a quotation mark? ~~bool~~ |
|
||||
| `is_currency` | Is the token a currency symbol? ~~bool~~ |
|
||||
| `like_url` | Does the token resemble a URL? ~~bool~~ |
|
||||
| `like_num` | Does the token represent a number? e.g. "10.9", "10", "ten", etc. ~~bool~~ |
|
||||
| `like_email` | Does the token resemble an email address? ~~bool~~ |
|
||||
| `is_oov` | Is the token out-of-vocabulary (i.e. does it not have a word vector)? ~~bool~~ |
|
||||
| `is_stop` | Is the token part of a "stop list"? ~~bool~~ |
|
||||
| `pos` | Coarse-grained part-of-speech from the [Universal POS tag set](https://universaldependencies.org/u/pos/). ~~int~~ |
|
||||
| `pos_` | Coarse-grained part-of-speech from the [Universal POS tag set](https://universaldependencies.org/u/pos/). ~~str~~ |
|
||||
| `tag` | Fine-grained part-of-speech. ~~int~~ |
|
||||
| `tag_` | Fine-grained part-of-speech. ~~str~~ |
|
||||
| `morph` <Tag variant="new">3</Tag> | Morphological analysis. ~~MorphAnalysis~~ |
|
||||
| `dep` | Syntactic dependency relation. ~~int~~ |
|
||||
| `dep_` | Syntactic dependency relation. ~~str~~ |
|
||||
| `lang` | Language of the parent document's vocabulary. ~~int~~ |
|
||||
| `lang_` | Language of the parent document's vocabulary. ~~str~~ |
|
||||
| `prob` | Smoothed log probability estimate of token's word type (context-independent entry in the vocabulary). ~~float~~ |
|
||||
| `idx` | The character offset of the token within the parent document. ~~int~~ |
|
||||
| `sentiment` | A scalar value indicating the positivity or negativity of the token. ~~float~~ |
|
||||
| `lex_id` | Sequential ID of the token's lexical type, used to index into tables, e.g. for word vectors. ~~int~~ |
|
||||
| `rank` | Sequential ID of the token's lexical type, used to index into tables, e.g. for word vectors. ~~int~~ |
|
||||
| `cluster` | Brown cluster ID. ~~int~~ |
|
||||
| `_` | User space for adding custom [attribute extensions](/usage/processing-pipelines#custom-components-attributes). ~~Underscore~~ |
|
||||
@@ -0,0 +1,264 @@
|
||||
---
|
||||
title: Tokenizer
|
||||
teaser: Segment text into words, punctuations marks, etc.
|
||||
tag: class
|
||||
source: spacy/tokenizer.pyx
|
||||
---
|
||||
|
||||
> #### Default config
|
||||
>
|
||||
> ```ini
|
||||
> [nlp.tokenizer]
|
||||
> @tokenizers = "spacy.Tokenizer.v1"
|
||||
> ```
|
||||
|
||||
Segment text, and create `Doc` objects with the discovered segment boundaries.
|
||||
For a deeper understanding, see the docs on
|
||||
[how spaCy's tokenizer works](/usage/linguistic-features#how-tokenizer-works).
|
||||
The tokenizer is typically created automatically when a
|
||||
[`Language`](/api/language) subclass is initialized and it reads its settings
|
||||
like punctuation and special case rules from the
|
||||
[`Language.Defaults`](/api/language#defaults) provided by the language subclass.
|
||||
|
||||
## Tokenizer.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create a `Tokenizer` to create `Doc` objects given unicode text. For examples of
|
||||
how to construct a custom tokenizer with different tokenization rules, see the
|
||||
[usage documentation](https://spacy.io/usage/linguistic-features#native-tokenizers).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction 1
|
||||
> from spacy.tokenizer import Tokenizer
|
||||
> from spacy.lang.en import English
|
||||
> nlp = English()
|
||||
> # Create a blank Tokenizer with just the English vocab
|
||||
> tokenizer = Tokenizer(nlp.vocab)
|
||||
>
|
||||
> # Construction 2
|
||||
> from spacy.lang.en import English
|
||||
> nlp = English()
|
||||
> # Create a Tokenizer with the default settings for English
|
||||
> # including punctuation rules and exceptions
|
||||
> tokenizer = nlp.tokenizer
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | A storage container for lexical types. ~~Vocab~~ |
|
||||
| `rules` | Exceptions and special-cases for the tokenizer. ~~Optional[Dict[str, List[Dict[int, str]]]]~~ |
|
||||
| `prefix_search` | A function matching the signature of `re.compile(string).search` to match prefixes. ~~Optional[Callable[[str], Optional[Match]]]~~ |
|
||||
| `suffix_search` | A function matching the signature of `re.compile(string).search` to match suffixes. ~~Optional[Callable[[str], Optional[Match]]]~~ |
|
||||
| `infix_finditer` | A function matching the signature of `re.compile(string).finditer` to find infixes. ~~Optional[Callable[[str], Iterator[Match]]]~~ |
|
||||
| `token_match` | A function matching the signature of `re.compile(string).match` to find token matches. ~~Optional[Callable[[str], Optional[Match]]]~~ |
|
||||
| `url_match` | A function matching the signature of `re.compile(string).match` to find token matches after considering prefixes and suffixes. ~~Optional[Callable[[str], Optional[Match]]]~~ |
|
||||
| `faster_heuristics` <Tag variant="new">3.3.0</Tag> | Whether to restrict the final `Matcher`-based pass for rules to those containing affixes or space. Defaults to `True`. ~~bool~~ |
|
||||
|
||||
## Tokenizer.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Tokenize a string.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tokens = tokenizer("This is a sentence")
|
||||
> assert len(tokens) == 4
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------- |
|
||||
| `string` | The string to tokenize. ~~str~~ |
|
||||
| **RETURNS** | A container for linguistic annotations. ~~Doc~~ |
|
||||
|
||||
## Tokenizer.pipe {id="pipe",tag="method"}
|
||||
|
||||
Tokenize a stream of texts.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> texts = ["One document.", "...", "Lots of documents"]
|
||||
> for doc in tokenizer.pipe(texts, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | ------------------------------------------------------------------------------------ |
|
||||
| `texts` | A sequence of unicode texts. ~~Iterable[str]~~ |
|
||||
| `batch_size` | The number of texts to accumulate in an internal buffer. Defaults to `1000`. ~~int~~ |
|
||||
| **YIELDS** | The tokenized `Doc` objects, in order. ~~Doc~~ |
|
||||
|
||||
## Tokenizer.find_infix {id="find_infix",tag="method"}
|
||||
|
||||
Find internal split points of the string.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `string` | The string to split. ~~str~~ |
|
||||
| **RETURNS** | A list of `re.MatchObject` objects that have `.start()` and `.end()` methods, denoting the placement of internal segment separators, e.g. hyphens. ~~List[Match]~~ |
|
||||
|
||||
## Tokenizer.find_prefix {id="find_prefix",tag="method"}
|
||||
|
||||
Find the length of a prefix that should be segmented from the string, or `None`
|
||||
if no prefix rules match.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------------------ |
|
||||
| `string` | The string to segment. ~~str~~ |
|
||||
| **RETURNS** | The length of the prefix if present, otherwise `None`. ~~Optional[int]~~ |
|
||||
|
||||
## Tokenizer.find_suffix {id="find_suffix",tag="method"}
|
||||
|
||||
Find the length of a suffix that should be segmented from the string, or `None`
|
||||
if no suffix rules match.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------------------ |
|
||||
| `string` | The string to segment. ~~str~~ |
|
||||
| **RETURNS** | The length of the suffix if present, otherwise `None`. ~~Optional[int]~~ |
|
||||
|
||||
## Tokenizer.add_special_case {id="add_special_case",tag="method"}
|
||||
|
||||
Add a special-case tokenization rule. This mechanism is also used to add custom
|
||||
tokenizer exceptions to the language data. See the usage guide on the
|
||||
[languages data](/usage/linguistic-features#language-data) and
|
||||
[tokenizer special cases](/usage/linguistic-features#special-cases) for more
|
||||
details and examples.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.attrs import ORTH, NORM
|
||||
> case = [{ORTH: "do"}, {ORTH: "n't", NORM: "not"}]
|
||||
> tokenizer.add_special_case("don't", case)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `string` | The string to specially tokenize. ~~str~~ |
|
||||
| `token_attrs` | A sequence of dicts, where each dict describes a token and its attributes. The `ORTH` fields of the attributes must exactly match the string when they are concatenated. ~~Iterable[Dict[int, str]]~~ |
|
||||
|
||||
## Tokenizer.explain {id="explain",tag="method"}
|
||||
|
||||
Tokenize a string with a slow debugging tokenizer that provides information
|
||||
about which tokenizer rule or pattern was matched for each token. The tokens
|
||||
produced are identical to `Tokenizer.__call__` except for whitespace tokens.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tok_exp = nlp.tokenizer.explain("(don't)")
|
||||
> assert [t[0] for t in tok_exp] == ["PREFIX", "SPECIAL-1", "SPECIAL-2", "SUFFIX"]
|
||||
> assert [t[1] for t in tok_exp] == ["(", "do", "n't", ")"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------- |
|
||||
| `string` | The string to tokenize with the debugging tokenizer. ~~str~~ |
|
||||
| **RETURNS** | A list of `(pattern_string, token_string)` tuples. ~~List[Tuple[str, str]]~~ |
|
||||
|
||||
## Tokenizer.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the tokenizer to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tokenizer = Tokenizer(nlp.vocab)
|
||||
> tokenizer.to_disk("/path/to/tokenizer")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## Tokenizer.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the tokenizer from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tokenizer = Tokenizer(nlp.vocab)
|
||||
> tokenizer.from_disk("/path/to/tokenizer")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `Tokenizer` object. ~~Tokenizer~~ |
|
||||
|
||||
## Tokenizer.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tokenizer = tokenizer(nlp.vocab)
|
||||
> tokenizer_bytes = tokenizer.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the tokenizer to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `Tokenizer` object. ~~bytes~~ |
|
||||
|
||||
## Tokenizer.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the tokenizer from a bytestring. Modifies the object in place and returns
|
||||
it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> tokenizer_bytes = tokenizer.to_bytes()
|
||||
> tokenizer = Tokenizer(nlp.vocab)
|
||||
> tokenizer.from_bytes(tokenizer_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `Tokenizer` object. ~~Tokenizer~~ |
|
||||
|
||||
## Attributes {id="attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The vocab object of the parent `Doc`. ~~Vocab~~ |
|
||||
| `prefix_search` | A function to find segment boundaries from the start of a string. Returns the length of the segment, or `None`. ~~Optional[Callable[[str], Optional[Match]]]~~ |
|
||||
| `suffix_search` | A function to find segment boundaries from the end of a string. Returns the length of the segment, or `None`. ~~Optional[Callable[[str], Optional[Match]]]~~ |
|
||||
| `infix_finditer` | A function to find internal segment separators, e.g. hyphens. Returns a (possibly empty) sequence of `re.MatchObject` objects. ~~Optional[Callable[[str], Iterator[Match]]]~~ |
|
||||
| `token_match` | A function matching the signature of `re.compile(string).match` to find token matches. Returns an `re.MatchObject` or `None`. ~~Optional[Callable[[str], Optional[Match]]]~~ |
|
||||
| `rules` | A dictionary of tokenizer exceptions and special cases. ~~Optional[Dict[str, List[Dict[int, str]]]]~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = tokenizer.to_bytes(exclude=["vocab", "exceptions"])
|
||||
> tokenizer.from_disk("./data", exclude=["token_match"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------- | --------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `prefix_search` | The prefix rules. |
|
||||
| `suffix_search` | The suffix rules. |
|
||||
| `infix_finditer` | The infix rules. |
|
||||
| `token_match` | The token match expression. |
|
||||
| `exceptions` | The tokenizer exception rules. |
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,595 @@
|
||||
---
|
||||
title: Transformer
|
||||
teaser: Pipeline component for multi-task learning with transformer models
|
||||
tag: class
|
||||
source: github.com/explosion/spacy-transformers/blob/master/spacy_transformers/pipeline_component.py
|
||||
version: 3
|
||||
api_base_class: /api/pipe
|
||||
api_string_name: transformer
|
||||
---
|
||||
|
||||
> #### Installation
|
||||
>
|
||||
> ```bash
|
||||
> $ pip install -U %%SPACY_PKG_NAME[transformers] %%SPACY_PKG_FLAGS
|
||||
> ```
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
This component is available via the extension package
|
||||
[`spacy-transformers`](https://github.com/explosion/spacy-transformers). It
|
||||
exposes the component via entry points, so if you have the package installed,
|
||||
using `factory = "transformer"` in your
|
||||
[training config](/usage/training#config) or `nlp.add_pipe("transformer")` will
|
||||
work out-of-the-box.
|
||||
|
||||
</Infobox>
|
||||
|
||||
This pipeline component lets you use transformer models in your pipeline. It
|
||||
supports all models that are available via the
|
||||
[HuggingFace `transformers`](https://huggingface.co/transformers) library.
|
||||
Usually you will connect subsequent components to the shared transformer using
|
||||
the [TransformerListener](/api/architectures#TransformerListener) layer. This
|
||||
works similarly to spaCy's [Tok2Vec](/api/tok2vec) component and
|
||||
[Tok2VecListener](/api/architectures/#Tok2VecListener) sublayer.
|
||||
|
||||
The component assigns the output of the transformer to the `Doc`'s extension
|
||||
attributes. We also calculate an alignment between the word-piece tokens and the
|
||||
spaCy tokenization, so that we can use the last hidden states to set the
|
||||
`Doc.tensor` attribute. When multiple word-piece tokens align to the same spaCy
|
||||
token, the spaCy token receives the sum of their values. To access the values,
|
||||
you can use the custom [`Doc._.trf_data`](#assigned-attributes) attribute. The
|
||||
package also adds the function registries [`@span_getters`](#span_getters) and
|
||||
[`@annotation_setters`](#annotation_setters) with several built-in registered
|
||||
functions. For more details, see the
|
||||
[usage documentation](/usage/embeddings-transformers).
|
||||
|
||||
## Assigned Attributes {id="assigned-attributes"}
|
||||
|
||||
The component sets the following
|
||||
[custom extension attribute](/usage/processing-pipeline#custom-components-attributes):
|
||||
|
||||
| Location | Value |
|
||||
| ---------------- | ------------------------------------------------------------------------ |
|
||||
| `Doc._.trf_data` | Transformer tokens and outputs for the `Doc` object. ~~TransformerData~~ |
|
||||
|
||||
## Config and implementation {id="config"}
|
||||
|
||||
The default config is defined by the pipeline component factory and describes
|
||||
how the component should be configured. You can override its settings via the
|
||||
`config` argument on [`nlp.add_pipe`](/api/language#add_pipe) or in your
|
||||
[`config.cfg` for training](/usage/training#config). See the
|
||||
[model architectures](/api/architectures#transformers) documentation for details
|
||||
on the transformer architectures and their arguments and hyperparameters.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy_transformers import Transformer
|
||||
> from spacy_transformers.pipeline_component import DEFAULT_CONFIG
|
||||
>
|
||||
> nlp.add_pipe("transformer", config=DEFAULT_CONFIG["transformer"])
|
||||
> ```
|
||||
|
||||
| Setting | Description |
|
||||
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `max_batch_items` | Maximum size of a padded batch. Defaults to `4096`. ~~int~~ |
|
||||
| `set_extra_annotations` | Function that takes a batch of `Doc` objects and transformer outputs to set additional annotations on the `Doc`. The `Doc._.trf_data` attribute is set prior to calling the callback. Defaults to `null_annotation_setter` (no additional annotations). ~~Callable[[List[Doc], FullTransformerBatch], None]~~ |
|
||||
| `model` | The Thinc [`Model`](https://thinc.ai/docs/api-model) wrapping the transformer. Defaults to [TransformerModel](/api/architectures#TransformerModel). ~~Model[List[Doc], FullTransformerBatch]~~ |
|
||||
|
||||
```python
|
||||
https://github.com/explosion/spacy-transformers/blob/master/spacy_transformers/pipeline_component.py
|
||||
```
|
||||
|
||||
## Transformer.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Construction via add_pipe with default model
|
||||
> trf = nlp.add_pipe("transformer")
|
||||
>
|
||||
> # Construction via add_pipe with custom config
|
||||
> config = {
|
||||
> "model": {
|
||||
> "@architectures": "spacy-transformers.TransformerModel.v3",
|
||||
> "name": "bert-base-uncased",
|
||||
> "tokenizer_config": {"use_fast": True},
|
||||
> "transformer_config": {"output_attentions": True},
|
||||
> "mixed_precision": True,
|
||||
> "grad_scaler_config": {"init_scale": 32768}
|
||||
> }
|
||||
> }
|
||||
> trf = nlp.add_pipe("transformer", config=config)
|
||||
>
|
||||
> # Construction from class
|
||||
> from spacy_transformers import Transformer
|
||||
> trf = Transformer(nlp.vocab, model)
|
||||
> ```
|
||||
|
||||
Construct a `Transformer` component. One or more subsequent spaCy components can
|
||||
use the transformer outputs as features in its model, with gradients
|
||||
backpropagated to the single shared weights. The activations from the
|
||||
transformer are saved in the [`Doc._.trf_data`](#assigned-attributes) extension
|
||||
attribute. You can also provide a callback to set additional annotations. In
|
||||
your application, you would normally use a shortcut for this and instantiate the
|
||||
component using its string name and [`nlp.add_pipe`](/api/language#create_pipe).
|
||||
|
||||
| Name | Description |
|
||||
| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | The Thinc [`Model`](https://thinc.ai/docs/api-model) wrapping the transformer. Usually you will want to use the [TransformerModel](/api/architectures#TransformerModel) layer for this. ~~Model[List[Doc], FullTransformerBatch]~~ |
|
||||
| `set_extra_annotations` | Function that takes a batch of `Doc` objects and transformer outputs and stores the annotations on the `Doc`. The `Doc._.trf_data` attribute is set prior to calling the callback. By default, no additional annotations are set. ~~Callable[[List[Doc], FullTransformerBatch], None]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `name` | String name of the component instance. Used to add entries to the `losses` during training. ~~str~~ |
|
||||
| `max_batch_items` | Maximum size of a padded batch. Defaults to `128*32`. ~~int~~ |
|
||||
|
||||
## Transformer.\_\_call\_\_ {id="call",tag="method"}
|
||||
|
||||
Apply the pipe to one document. The document is modified in place, and returned.
|
||||
This usually happens under the hood when the `nlp` object is called on a text
|
||||
and all pipeline components are applied to the `Doc` in order. Both
|
||||
[`__call__`](/api/transformer#call) and [`pipe`](/api/transformer#pipe) delegate
|
||||
to the [`predict`](/api/transformer#predict) and
|
||||
[`set_annotations`](/api/transformer#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> trf = nlp.add_pipe("transformer")
|
||||
> # This usually happens under the hood
|
||||
> processed = transformer(doc)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------- |
|
||||
| `doc` | The document to process. ~~Doc~~ |
|
||||
| **RETURNS** | The processed document. ~~Doc~~ |
|
||||
|
||||
## Transformer.pipe {id="pipe",tag="method"}
|
||||
|
||||
Apply the pipe to a stream of documents. This usually happens under the hood
|
||||
when the `nlp` object is called on a text and all pipeline components are
|
||||
applied to the `Doc` in order. Both [`__call__`](/api/transformer#call) and
|
||||
[`pipe`](/api/transformer#pipe) delegate to the
|
||||
[`predict`](/api/transformer#predict) and
|
||||
[`set_annotations`](/api/transformer#set_annotations) methods.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("transformer")
|
||||
> for doc in trf.pipe(docs, batch_size=50):
|
||||
> pass
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------- |
|
||||
| `stream` | A stream of documents. ~~Iterable[Doc]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The number of documents to buffer. Defaults to `128`. ~~int~~ |
|
||||
| **YIELDS** | The processed documents in order. ~~Doc~~ |
|
||||
|
||||
## Transformer.initialize {id="initialize",tag="method"}
|
||||
|
||||
Initialize the component for training and return an
|
||||
[`Optimizer`](https://thinc.ai/docs/api-optimizers). `get_examples` should be a
|
||||
function that returns an iterable of [`Example`](/api/example) objects. **At
|
||||
least one example should be supplied.** The data examples are used to
|
||||
**initialize the model** of the component and can either be the full training
|
||||
data or a representative sample. Initialization includes validating the network,
|
||||
[inferring missing shapes](https://thinc.ai/docs/usage-models#validation) and
|
||||
setting up the label scheme based on the data. This method is typically called
|
||||
by [`Language.initialize`](/api/language#initialize).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("transformer")
|
||||
> trf.initialize(lambda: examples, nlp=nlp)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `get_examples` | Function that returns gold-standard annotations in the form of [`Example`](/api/example) objects. Must contain at least one `Example`. ~~Callable[[], Iterable[Example]]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `nlp` | The current `nlp` object. Defaults to `None`. ~~Optional[Language]~~ |
|
||||
|
||||
## Transformer.predict {id="predict",tag="method"}
|
||||
|
||||
Apply the component's model to a batch of [`Doc`](/api/doc) objects without
|
||||
modifying them.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("transformer")
|
||||
> scores = trf.predict([doc1, doc2])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| `docs` | The documents to predict. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | The model's prediction for each document. |
|
||||
|
||||
## Transformer.set_annotations {id="set_annotations",tag="method"}
|
||||
|
||||
Assign the extracted features to the `Doc` objects. By default, the
|
||||
[`TransformerData`](/api/transformer#transformerdata) object is written to the
|
||||
[`Doc._.trf_data`](#assigned-attributes) attribute. Your `set_extra_annotations`
|
||||
callback is then called, if provided.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("transformer")
|
||||
> scores = trf.predict(docs)
|
||||
> trf.set_annotations(docs, scores)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | ----------------------------------------------------- |
|
||||
| `docs` | The documents to modify. ~~Iterable[Doc]~~ |
|
||||
| `scores` | The scores to set, produced by `Transformer.predict`. |
|
||||
|
||||
## Transformer.update {id="update",tag="method"}
|
||||
|
||||
Prepare for an update to the transformer. Like the [`Tok2Vec`](/api/tok2vec)
|
||||
component, the `Transformer` component is unusual in that it does not receive
|
||||
"gold standard" annotations to calculate a weight update. The optimal output of
|
||||
the transformer data is unknown – it's a hidden layer inside the network that is
|
||||
updated by backpropagating from output layers.
|
||||
|
||||
The `Transformer` component therefore does **not** perform a weight update
|
||||
during its own `update` method. Instead, it runs its transformer model and
|
||||
communicates the output and the backpropagation callback to any **downstream
|
||||
components** that have been connected to it via the
|
||||
[TransformerListener](/api/architectures#TransformerListener) sublayer. If there
|
||||
are multiple listeners, the last layer will actually backprop to the transformer
|
||||
and call the optimizer, while the others simply increment the gradients.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("transformer")
|
||||
> optimizer = nlp.initialize()
|
||||
> losses = trf.update(examples, sgd=optimizer)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `examples` | A batch of [`Example`](/api/example) objects. Only the [`Example.predicted`](/api/example#predicted) `Doc` object is used, the reference `Doc` is ignored. ~~Iterable[Example]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `drop` | The dropout rate. ~~float~~ |
|
||||
| `sgd` | An optimizer. Will be created via [`create_optimizer`](#create_optimizer) if not set. ~~Optional[Optimizer]~~ |
|
||||
| `losses` | Optional record of the loss during training. Updated using the component name as the key. ~~Optional[Dict[str, float]]~~ |
|
||||
| **RETURNS** | The updated `losses` dictionary. ~~Dict[str, float]~~ |
|
||||
|
||||
## Transformer.create_optimizer {id="create_optimizer",tag="method"}
|
||||
|
||||
Create an optimizer for the pipeline component.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("transformer")
|
||||
> optimizer = trf.create_optimizer()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------- |
|
||||
| **RETURNS** | The optimizer. ~~Optimizer~~ |
|
||||
|
||||
## Transformer.use_params {id="use_params",tag="method, contextmanager"}
|
||||
|
||||
Modify the pipe's model to use the given parameter values. At the end of the
|
||||
context, the original parameters are restored.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("transformer")
|
||||
> with trf.use_params(optimizer.averages):
|
||||
> trf.to_disk("/best_model")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------- |
|
||||
| `params` | The parameter values to use in the model. ~~dict~~ |
|
||||
|
||||
## Transformer.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Serialize the pipe to disk.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("transformer")
|
||||
> trf.to_disk("/path/to/transformer")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## Transformer.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Load the pipe from disk. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("transformer")
|
||||
> trf.from_disk("/path/to/transformer")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `Transformer` object. ~~Transformer~~ |
|
||||
|
||||
## Transformer.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf = nlp.add_pipe("transformer")
|
||||
> trf_bytes = trf.to_bytes()
|
||||
> ```
|
||||
|
||||
Serialize the pipe to a bytestring.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `Transformer` object. ~~bytes~~ |
|
||||
|
||||
## Transformer.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load the pipe from a bytestring. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> trf_bytes = trf.to_bytes()
|
||||
> trf = nlp.add_pipe("transformer")
|
||||
> trf.from_bytes(trf_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `Transformer` object. ~~Transformer~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = trf.to_disk("/path", exclude=["vocab"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------- | -------------------------------------------------------------- |
|
||||
| `vocab` | The shared [`Vocab`](/api/vocab). |
|
||||
| `cfg` | The config file. You usually don't want to exclude this. |
|
||||
| `model` | The binary model data. You usually don't want to exclude this. |
|
||||
|
||||
## TransformerData {id="transformerdata",tag="dataclass"}
|
||||
|
||||
Transformer tokens and outputs for one `Doc` object. The transformer models
|
||||
return tensors that refer to a whole padded batch of documents. These tensors
|
||||
are wrapped into the
|
||||
[FullTransformerBatch](/api/transformer#fulltransformerbatch) object. The
|
||||
`FullTransformerBatch` then splits out the per-document data, which is handled
|
||||
by this class. Instances of this class are typically assigned to the
|
||||
[`Doc._.trf_data`](/api/transformer#assigned-attributes) extension attribute.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Get the last hidden layer output for "is" (token index 1)
|
||||
> doc = nlp("This is a text.")
|
||||
> indices = doc._.trf_data.align[1].data.flatten()
|
||||
> last_hidden_state = doc._.trf_data.model_output.last_hidden_state
|
||||
> dim = last_hidden_state.shape[-1]
|
||||
> tensors = last_hidden_state.reshape(-1, dim)[indices]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `tokens` | A slice of the tokens data produced by the tokenizer. This may have several fields, including the token IDs, the texts and the attention mask. See the [`transformers.BatchEncoding`](https://huggingface.co/transformers/main_classes/tokenizer.html#transformers.BatchEncoding) object for details. ~~dict~~ |
|
||||
| `model_output` | The model output from the transformer model, determined by the model and transformer config. New in `spacy-transformers` v1.1.0. ~~transformers.file_utils.ModelOutput~~ |
|
||||
| `tensors` | The `model_output` in the earlier `transformers` tuple format converted using [`ModelOutput.to_tuple()`](https://huggingface.co/transformers/main_classes/output.html#transformers.file_utils.ModelOutput.to_tuple). Returns `Tuple` instead of `List` as of `spacy-transformers` v1.1.0. ~~Tuple[Union[FloatsXd, List[FloatsXd]]]~~ |
|
||||
| `align` | Alignment from the `Doc`'s tokenization to the wordpieces. This is a ragged array, where `align.lengths[i]` indicates the number of wordpiece tokens that token `i` aligns against. The actual indices are provided at `align[i].dataXd`. ~~Ragged~~ |
|
||||
| `width` | The width of the last hidden layer. ~~int~~ |
|
||||
|
||||
### TransformerData.empty {id="transformerdata-empty",tag="classmethod"}
|
||||
|
||||
Create an empty `TransformerData` container.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------- |
|
||||
| **RETURNS** | The container. ~~TransformerData~~ |
|
||||
|
||||
<Accordion title="Previous versions of TransformerData" spaced>
|
||||
|
||||
In `spacy-transformers` v1.0, the model output is stored in
|
||||
`TransformerData.tensors` as `List[Union[FloatsXd]]` and only includes the
|
||||
activations for the `Doc` from the transformer. Usually the last tensor that is
|
||||
3-dimensional will be the most important, as that will provide the final hidden
|
||||
state. Generally activations that are 2-dimensional will be attention weights.
|
||||
Details of this variable will differ depending on the underlying transformer
|
||||
model.
|
||||
|
||||
</Accordion>
|
||||
|
||||
## FullTransformerBatch {id="fulltransformerbatch",tag="dataclass"}
|
||||
|
||||
Holds a batch of input and output objects for a transformer model. The data can
|
||||
then be split to a list of [`TransformerData`](/api/transformer#transformerdata)
|
||||
objects to associate the outputs to each [`Doc`](/api/doc) in the batch.
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `spans` | The batch of input spans. The outer list refers to the Doc objects in the batch, and the inner list are the spans for that `Doc`. Note that spans are allowed to overlap or exclude tokens, but each `Span` can only refer to one `Doc` (by definition). This means that within a `Doc`, the regions of the output tensors that correspond to each `Span` may overlap or have gaps, but for each `Doc`, there is a non-overlapping contiguous slice of the outputs. ~~List[List[Span]]~~ |
|
||||
| `tokens` | The output of the tokenizer. ~~transformers.BatchEncoding~~ |
|
||||
| `model_output` | The model output from the transformer model, determined by the model and transformer config. New in `spacy-transformers` v1.1.0. ~~transformers.file_utils.ModelOutput~~ |
|
||||
| `tensors` | The `model_output` in the earlier `transformers` tuple format converted using [`ModelOutput.to_tuple()`](https://huggingface.co/transformers/main_classes/output.html#transformers.file_utils.ModelOutput.to_tuple). Returns `Tuple` instead of `List` as of `spacy-transformers` v1.1.0. ~~Tuple[Union[torch.Tensor, Tuple[torch.Tensor]]]~~ |
|
||||
| `align` | Alignment from the spaCy tokenization to the wordpieces. This is a ragged array, where `align.lengths[i]` indicates the number of wordpiece tokens that token `i` aligns against. The actual indices are provided at `align[i].dataXd`. ~~Ragged~~ |
|
||||
| `doc_data` | The outputs, split per `Doc` object. ~~List[TransformerData]~~ |
|
||||
|
||||
### FullTransformerBatch.unsplit_by_doc {id="fulltransformerbatch-unsplit_by_doc",tag="method"}
|
||||
|
||||
Return a new `FullTransformerBatch` from a split batch of activations, using the
|
||||
current object's spans, tokens and alignment. This is used during the backward
|
||||
pass, in order to construct the gradients to pass back into the transformer
|
||||
model.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------- |
|
||||
| `arrays` | The split batch of activations. ~~List[List[Floats3d]]~~ |
|
||||
| **RETURNS** | The transformer batch. ~~FullTransformerBatch~~ |
|
||||
|
||||
### FullTransformerBatch.split_by_doc {id="fulltransformerbatch-split_by_doc",tag="method"}
|
||||
|
||||
Split a `TransformerData` object that represents a batch into a list with one
|
||||
`TransformerData` per `Doc`.
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------ |
|
||||
| **RETURNS** | The split batch. ~~List[TransformerData]~~ |
|
||||
|
||||
<Accordion title="Previous versions of FullTransformerBatch" spaced>
|
||||
|
||||
In `spacy-transformers` v1.0, the model output is stored in
|
||||
`FullTransformerBatch.tensors` as `List[torch.Tensor]`.
|
||||
|
||||
</Accordion>
|
||||
|
||||
## Span getters {id="span_getters",source="github.com/explosion/spacy-transformers/blob/master/spacy_transformers/span_getters.py"}
|
||||
|
||||
Span getters are functions that take a batch of [`Doc`](/api/doc) objects and
|
||||
return a lists of [`Span`](/api/span) objects for each doc to be processed by
|
||||
the transformer. This is used to manage long documents by cutting them into
|
||||
smaller sequences before running the transformer. The spans are allowed to
|
||||
overlap, and you can also omit sections of the `Doc` if they are not relevant.
|
||||
|
||||
Span getters can be referenced in the `[components.transformer.model.get_spans]`
|
||||
block of the config to customize the sequences processed by the transformer. You
|
||||
can also register
|
||||
[custom span getters](/usage/embeddings-transformers#transformers-training-custom-settings)
|
||||
using the `@spacy.registry.span_getters` decorator.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> @spacy.registry.span_getters("custom_sent_spans")
|
||||
> def configure_get_sent_spans() -> Callable:
|
||||
> def get_sent_spans(docs: Iterable[Doc]) -> List[List[Span]]:
|
||||
> return [list(doc.sents) for doc in docs]
|
||||
>
|
||||
> return get_sent_spans
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------------- |
|
||||
| `docs` | A batch of `Doc` objects. ~~Iterable[Doc]~~ |
|
||||
| **RETURNS** | The spans to process by the transformer. ~~List[List[Span]]~~ |
|
||||
|
||||
### doc_spans.v1 {id="doc_spans",tag="registered function"}
|
||||
|
||||
> #### Example config
|
||||
>
|
||||
> ```ini
|
||||
> [transformer.model.get_spans]
|
||||
> @span_getters = "spacy-transformers.doc_spans.v1"
|
||||
> ```
|
||||
|
||||
Create a span getter that uses the whole document as its spans. This is the best
|
||||
approach if your [`Doc`](/api/doc) objects already refer to relatively short
|
||||
texts.
|
||||
|
||||
### sent_spans.v1 {id="sent_spans",tag="registered function"}
|
||||
|
||||
> #### Example config
|
||||
>
|
||||
> ```ini
|
||||
> [transformer.model.get_spans]
|
||||
> @span_getters = "spacy-transformers.sent_spans.v1"
|
||||
> ```
|
||||
|
||||
Create a span getter that uses sentence boundary markers to extract the spans.
|
||||
This requires sentence boundaries to be set (e.g. by the
|
||||
[`Sentencizer`](/api/sentencizer)), and may result in somewhat uneven batches,
|
||||
depending on the sentence lengths. However, it does provide the transformer with
|
||||
more meaningful windows to attend over.
|
||||
|
||||
To set sentence boundaries with the `sentencizer` during training, add a
|
||||
`sentencizer` to the beginning of the pipeline and include it in
|
||||
[`[training.annotating_components]`](/usage/training#annotating-components) to
|
||||
have it set the sentence boundaries before the `transformer` component runs.
|
||||
|
||||
### strided_spans.v1 {id="strided_spans",tag="registered function"}
|
||||
|
||||
> #### Example config
|
||||
>
|
||||
> ```ini
|
||||
> [transformer.model.get_spans]
|
||||
> @span_getters = "spacy-transformers.strided_spans.v1"
|
||||
> window = 128
|
||||
> stride = 96
|
||||
> ```
|
||||
|
||||
Create a span getter for strided spans. If you set the `window` and `stride` to
|
||||
the same value, the spans will cover each token once. Setting `stride` lower
|
||||
than `window` will allow for an overlap, so that some tokens are counted twice.
|
||||
This can be desirable, because it allows all tokens to have both a left and
|
||||
right context.
|
||||
|
||||
| Name | Description |
|
||||
| -------- | ------------------------ |
|
||||
| `window` | The window size. ~~int~~ |
|
||||
| `stride` | The stride size. ~~int~~ |
|
||||
|
||||
## Annotation setters {id="annotation_setters",tag="registered functions",source="github.com/explosion/spacy-transformers/blob/master/spacy_transformers/annotation_setters.py"}
|
||||
|
||||
Annotation setters are functions that take a batch of `Doc` objects and a
|
||||
[`FullTransformerBatch`](/api/transformer#fulltransformerbatch) and can set
|
||||
additional annotations on the `Doc`, e.g. to set custom or built-in attributes.
|
||||
You can register custom annotation setters using the
|
||||
`@registry.annotation_setters` decorator.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> @registry.annotation_setters("spacy-transformers.null_annotation_setter.v1")
|
||||
> def configure_null_annotation_setter() -> Callable:
|
||||
> def setter(docs: List[Doc], trf_data: FullTransformerBatch) -> None:
|
||||
> pass
|
||||
>
|
||||
> return setter
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ------------------------------------------------------------- |
|
||||
| `docs` | A batch of `Doc` objects. ~~List[Doc]~~ |
|
||||
| `trf_data` | The transformers data for the batch. ~~FullTransformerBatch~~ |
|
||||
|
||||
The following built-in functions are available:
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------------------------------- | ------------------------------------- |
|
||||
| `spacy-transformers.null_annotation_setter.v1` | Don't set any additional annotations. |
|
||||
@@ -0,0 +1,461 @@
|
||||
---
|
||||
title: Vectors
|
||||
teaser: Store, save and load word vectors
|
||||
tag: class
|
||||
source: spacy/vectors.pyx
|
||||
version: 2
|
||||
---
|
||||
|
||||
Vectors data is kept in the `Vectors.data` attribute, which should be an
|
||||
instance of `numpy.ndarray` (for CPU vectors) or `cupy.ndarray` (for GPU
|
||||
vectors).
|
||||
|
||||
As of spaCy v3.2, `Vectors` supports two types of vector tables:
|
||||
|
||||
- `default`: A standard vector table (as in spaCy v3.1 and earlier) where each
|
||||
key is mapped to one row in the vector table. Multiple keys can be mapped to
|
||||
the same vector, and not all of the rows in the table need to be assigned – so
|
||||
`vectors.n_keys` may be greater or smaller than `vectors.shape[0]`.
|
||||
- `floret`: Only supports vectors trained with
|
||||
[floret](https://github.com/explosion/floret), an extended version of
|
||||
[fastText](https://fasttext.cc) that produces compact vector tables by
|
||||
combining fastText's subword ngrams with Bloom embeddings. The compact tables
|
||||
are similar to the [`HashEmbed`](https://thinc.ai/docs/api-layers#hashembed)
|
||||
embeddings already used in many spaCy components. Each word is represented as
|
||||
the sum of one or more rows as determined by the settings related to character
|
||||
ngrams and the hash table.
|
||||
|
||||
## Vectors.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create a new vector store. With the default mode, you can set the vector values
|
||||
and keys directly on initialization, or supply a `shape` keyword argument to
|
||||
create an empty table you can add vectors to later. In floret mode, the complete
|
||||
vector data and settings must be provided on initialization and cannot be
|
||||
modified later.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.vectors import Vectors
|
||||
>
|
||||
> empty_vectors = Vectors(shape=(10000, 300))
|
||||
>
|
||||
> data = numpy.zeros((3, 300), dtype='f')
|
||||
> keys = ["cat", "dog", "rat"]
|
||||
> vectors = Vectors(data=data, keys=keys)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `strings` | The string store. A new string store is created if one is not provided. Defaults to `None`. ~~Optional[StringStore]~~ |
|
||||
| `shape` | Size of the table as `(n_entries, n_columns)`, the number of entries and number of columns. Not required if you're initializing the object with `data` and `keys`. ~~Tuple[int, int]~~ |
|
||||
| `data` | The vector data. ~~numpy.ndarray[ndim=2, dtype=float32]~~ |
|
||||
| `keys` | An iterable of keys aligned with the data. ~~Iterable[Union[str, int]]~~ |
|
||||
| `name` | A name to identify the vectors table. ~~str~~ |
|
||||
| `mode` <Tag variant="new">3.2</Tag> | Vectors mode: `"default"` or [`"floret"`](https://github.com/explosion/floret) (default: `"default"`). ~~str~~ |
|
||||
| `minn` <Tag variant="new">3.2</Tag> | The floret char ngram minn (default: `0`). ~~int~~ |
|
||||
| `maxn` <Tag variant="new">3.2</Tag> | The floret char ngram maxn (default: `0`). ~~int~~ |
|
||||
| `hash_count` <Tag variant="new">3.2</Tag> | The floret hash count. Supported values: 1--4 (default: `1`). ~~int~~ |
|
||||
| `hash_seed` <Tag variant="new">3.2</Tag> | The floret hash seed (default: `0`). ~~int~~ |
|
||||
| `bow` <Tag variant="new">3.2</Tag> | The floret BOW string (default: `"<"`). ~~str~~ |
|
||||
| `eow` <Tag variant="new">3.2</Tag> | The floret EOW string (default: `">"`). ~~str~~ |
|
||||
| `attr` <Tag variant="new">3.6</Tag> | The token attribute for the vector keys (default: `"ORTH"`). ~~Union[int, str]~~ |
|
||||
|
||||
## Vectors.\_\_getitem\_\_ {id="getitem",tag="method"}
|
||||
|
||||
Get a vector by key. If the key is not found in the table, a `KeyError` is
|
||||
raised.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> cat_id = nlp.vocab.strings["cat"]
|
||||
> cat_vector = nlp.vocab.vectors[cat_id]
|
||||
> assert cat_vector == nlp.vocab["cat"].vector
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------- |
|
||||
| `key` | The key to get the vector for. ~~Union[int, str]~~ |
|
||||
| **RETURNS** | The vector for the key. ~~numpy.ndarray[ndim=1, dtype=float32]~~ |
|
||||
|
||||
## Vectors.\_\_setitem\_\_ {id="setitem",tag="method"}
|
||||
|
||||
Set a vector for the given key. Not supported for `floret` mode.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> cat_id = nlp.vocab.strings["cat"]
|
||||
> vector = numpy.random.uniform(-1, 1, (300,))
|
||||
> nlp.vocab.vectors[cat_id] = vector
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | ----------------------------------------------------------- |
|
||||
| `key` | The key to set the vector for. ~~int~~ |
|
||||
| `vector` | The vector to set. ~~numpy.ndarray[ndim=1, dtype=float32]~~ |
|
||||
|
||||
## Vectors.\_\_iter\_\_ {id="iter",tag="method"}
|
||||
|
||||
Iterate over the keys in the table. In `floret` mode, the keys table is not
|
||||
used.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> for key in nlp.vocab.vectors:
|
||||
> print(key, nlp.vocab.strings[key])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | --------------------------- |
|
||||
| **YIELDS** | A key in the table. ~~int~~ |
|
||||
|
||||
## Vectors.\_\_len\_\_ {id="len",tag="method"}
|
||||
|
||||
Return the number of vectors in the table.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> vectors = Vectors(shape=(3, 300))
|
||||
> assert len(vectors) == 3
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| **RETURNS** | The number of vectors in the table. ~~int~~ |
|
||||
|
||||
## Vectors.\_\_contains\_\_ {id="contains",tag="method"}
|
||||
|
||||
Check whether a key has been mapped to a vector entry in the table. In `floret`
|
||||
mode, returns `True` for all keys.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> cat_id = nlp.vocab.strings["cat"]
|
||||
> nlp.vocab.vectors.add(cat_id, numpy.random.uniform(-1, 1, (300,)))
|
||||
> assert cat_id in vectors
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------- |
|
||||
| `key` | The key to check. ~~int~~ |
|
||||
| **RETURNS** | Whether the key has a vector entry. ~~bool~~ |
|
||||
|
||||
## Vectors.add {id="add",tag="method"}
|
||||
|
||||
Add a key to the table, optionally setting a vector value as well. Keys can be
|
||||
mapped to an existing vector by setting `row`, or a new vector can be added. Not
|
||||
supported for `floret` mode.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> vector = numpy.random.uniform(-1, 1, (300,))
|
||||
> cat_id = nlp.vocab.strings["cat"]
|
||||
> nlp.vocab.vectors.add(cat_id, vector=vector)
|
||||
> nlp.vocab.vectors.add("dog", row=0)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------- |
|
||||
| `key` | The key to add. ~~Union[str, int]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `vector` | An optional vector to add for the key. ~~numpy.ndarray[ndim=1, dtype=float32]~~ |
|
||||
| `row` | An optional row number of a vector to map the key to. ~~int~~ |
|
||||
| **RETURNS** | The row the vector was added to. ~~int~~ |
|
||||
|
||||
## Vectors.resize {id="resize",tag="method"}
|
||||
|
||||
Resize the underlying vectors array. If `inplace=True`, the memory is
|
||||
reallocated. This may cause other references to the data to become invalid, so
|
||||
only use `inplace=True` if you're sure that's what you want. If the number of
|
||||
vectors is reduced, keys mapped to rows that have been deleted are removed.
|
||||
These removed items are returned as a list of `(key, row)` tuples. Not supported
|
||||
for `floret` mode.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> removed = nlp.vocab.vectors.resize((10000, 300))
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------- |
|
||||
| `shape` | A `(rows, dims)` tuple describing the number of rows and dimensions. ~~Tuple[int, int]~~ |
|
||||
| `inplace` | Reallocate the memory. ~~bool~~ |
|
||||
| **RETURNS** | The removed items as a list of `(key, row)` tuples. ~~List[Tuple[int, int]]~~ |
|
||||
|
||||
## Vectors.keys {id="keys",tag="method"}
|
||||
|
||||
A sequence of the keys in the table. In `floret` mode, the keys table is not
|
||||
used.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> for key in nlp.vocab.vectors.keys():
|
||||
> print(key, nlp.vocab.strings[key])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------- |
|
||||
| **RETURNS** | The keys. ~~Iterable[int]~~ |
|
||||
|
||||
## Vectors.values {id="values",tag="method"}
|
||||
|
||||
Iterate over vectors that have been assigned to at least one key. Note that some
|
||||
vectors may be unassigned, so the number of vectors returned may be less than
|
||||
the length of the vectors table. In `floret` mode, the keys table is not used.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> for vector in nlp.vocab.vectors.values():
|
||||
> print(vector)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | --------------------------------------------------------------- |
|
||||
| **YIELDS** | A vector in the table. ~~numpy.ndarray[ndim=1, dtype=float32]~~ |
|
||||
|
||||
## Vectors.items {id="items",tag="method"}
|
||||
|
||||
Iterate over `(key, vector)` pairs, in order. In `floret` mode, the keys table
|
||||
is empty.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> for key, vector in nlp.vocab.vectors.items():
|
||||
> print(key, nlp.vocab.strings[key], vector)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | ------------------------------------------------------------------------------------- |
|
||||
| **YIELDS** | `(key, vector)` pairs, in order. ~~Tuple[int, numpy.ndarray[ndim=1, dtype=float32]]~~ |
|
||||
|
||||
## Vectors.find {id="find",tag="method"}
|
||||
|
||||
Look up one or more keys by row, or vice versa. Not supported for `floret` mode.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> row = nlp.vocab.vectors.find(key="cat")
|
||||
> rows = nlp.vocab.vectors.find(keys=["cat", "dog"])
|
||||
> key = nlp.vocab.vectors.find(row=256)
|
||||
> keys = nlp.vocab.vectors.find(rows=[18, 256, 985])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | -------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `key` | Find the row that the given key points to. Returns int, `-1` if missing. ~~Union[str, int]~~ |
|
||||
| `keys` | Find rows that the keys point to. Returns `numpy.ndarray`. ~~Iterable[Union[str, int]]~~ |
|
||||
| `row` | Find the first key that points to the row. Returns integer. ~~int~~ |
|
||||
| `rows` | Find the keys that point to the rows. Returns `numpy.ndarray`. ~~Iterable[int]~~ |
|
||||
| **RETURNS** | The requested key, keys, row or rows. ~~Union[int, numpy.ndarray[ndim=1, dtype=float32]]~~ |
|
||||
|
||||
## Vectors.shape {id="shape",tag="property"}
|
||||
|
||||
Get `(rows, dims)` tuples of number of rows and number of dimensions in the
|
||||
vector table.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> vectors = Vectors(shape(1, 300))
|
||||
> vectors.add("cat", numpy.random.uniform(-1, 1, (300,)))
|
||||
> rows, dims = vectors.shape
|
||||
> assert rows == 1
|
||||
> assert dims == 300
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------ |
|
||||
| **RETURNS** | A `(rows, dims)` pair. ~~Tuple[int, int]~~ |
|
||||
|
||||
## Vectors.size {id="size",tag="property"}
|
||||
|
||||
The vector size, i.e. `rows * dims`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> vectors = Vectors(shape=(500, 300))
|
||||
> assert vectors.size == 150000
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------ |
|
||||
| **RETURNS** | The vector size. ~~int~~ |
|
||||
|
||||
## Vectors.is_full {id="is_full",tag="property"}
|
||||
|
||||
Whether the vectors table is full and no slots are available for new keys. If a
|
||||
table is full, it can be resized using [`Vectors.resize`](/api/vectors#resize).
|
||||
In `floret` mode, the table is always full and cannot be resized.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> vectors = Vectors(shape=(1, 300))
|
||||
> vectors.add("cat", numpy.random.uniform(-1, 1, (300,)))
|
||||
> assert vectors.is_full
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------- |
|
||||
| **RETURNS** | Whether the vectors table is full. ~~bool~~ |
|
||||
|
||||
## Vectors.n_keys {id="n_keys",tag="property"}
|
||||
|
||||
Get the number of keys in the table. Note that this is the number of _all_ keys,
|
||||
not just unique vectors. If several keys are mapped to the same vectors, they
|
||||
will be counted individually. In `floret` mode, the keys table is not used.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> vectors = Vectors(shape=(10, 300))
|
||||
> assert len(vectors) == 10
|
||||
> assert vectors.n_keys == 0
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------------------- |
|
||||
| **RETURNS** | The number of all keys in the table. Returns `-1` for floret vectors. ~~int~~ |
|
||||
|
||||
## Vectors.most_similar {id="most_similar",tag="method"}
|
||||
|
||||
For each of the given vectors, find the `n` most similar entries to it by
|
||||
cosine. Queries are by vector. Results are returned as a
|
||||
`(keys, best_rows, scores)` tuple. If `queries` is large, the calculations are
|
||||
performed in chunks to avoid consuming too much memory. You can set the
|
||||
`batch_size` to control the size/space trade-off during the calculations. Not
|
||||
supported for `floret` mode.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> queries = numpy.asarray([numpy.random.uniform(-1, 1, (300,))])
|
||||
> most_similar = nlp.vocab.vectors.most_similar(queries, n=10)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------------------------------- |
|
||||
| `queries` | An array with one or more vectors. ~~numpy.ndarray~~ |
|
||||
| _keyword-only_ | |
|
||||
| `batch_size` | The batch size to use. Default to `1024`. ~~int~~ |
|
||||
| `n` | The number of entries to return for each query. Defaults to `1`. ~~int~~ |
|
||||
| `sort` | Whether to sort the entries returned by score. Defaults to `True`. ~~bool~~ |
|
||||
| **RETURNS** | The most similar entries as a `(keys, best_rows, scores)` tuple. ~~Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]~~ |
|
||||
|
||||
## Vectors.get_batch {id="get_batch",tag="method",version="3.2"}
|
||||
|
||||
Get the vectors for the provided keys efficiently as a batch.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> words = ["cat", "dog"]
|
||||
> vectors = nlp.vocab.vectors.get_batch(words)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------ | --------------------------------------- |
|
||||
| `keys` | The keys. ~~Iterable[Union[int, str]]~~ |
|
||||
|
||||
## Vectors.to_ops {id="to_ops",tag="method"}
|
||||
|
||||
Change the embedding matrix to use different Thinc ops.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from thinc.api import NumpyOps
|
||||
>
|
||||
> vectors.to_ops(NumpyOps())
|
||||
>
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----- | -------------------------------------------------------- |
|
||||
| `ops` | The Thinc ops to switch the embedding matrix to. ~~Ops~~ |
|
||||
|
||||
## Vectors.to_disk {id="to_disk",tag="method"}
|
||||
|
||||
Save the current state to a directory.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> vectors.to_disk("/path/to/vectors")
|
||||
>
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------ | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
|
||||
## Vectors.from_disk {id="from_disk",tag="method"}
|
||||
|
||||
Loads state from a directory. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> vectors = Vectors(StringStore())
|
||||
> vectors.from_disk("/path/to/vectors")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| **RETURNS** | The modified `Vectors` object. ~~Vectors~~ |
|
||||
|
||||
## Vectors.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
Serialize the current state to a binary string.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> vectors_bytes = vectors.to_bytes()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------------ |
|
||||
| **RETURNS** | The serialized form of the `Vectors` object. ~~bytes~~ |
|
||||
|
||||
## Vectors.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load state from a binary string.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.vectors import Vectors
|
||||
> vectors_bytes = vectors.to_bytes()
|
||||
> new_vectors = Vectors(StringStore())
|
||||
> new_vectors.from_bytes(vectors_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | --------------------------------- |
|
||||
| `data` | The data to load from. ~~bytes~~ |
|
||||
| **RETURNS** | The `Vectors` object. ~~Vectors~~ |
|
||||
|
||||
## Attributes {id="attributes"}
|
||||
|
||||
| Name | Description |
|
||||
| ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `data` | Stored vectors data. `numpy` is used for CPU vectors, `cupy` for GPU vectors. ~~Union[numpy.ndarray[ndim=1, dtype=float32], cupy.ndarray[ndim=1, dtype=float32]]~~ |
|
||||
| `key2row` | Dictionary mapping word hashes to rows in the `Vectors.data` table. ~~Dict[int, int]~~ |
|
||||
| `keys` | Array keeping the keys in order, such that `keys[vectors.key2row[key]] == key`. ~~Union[numpy.ndarray[ndim=1, dtype=float32], cupy.ndarray[ndim=1, dtype=float32]]~~ |
|
||||
| `attr` <Tag variant="new">3.6</Tag> | The token attribute for the vector keys. ~~int~~ |
|
||||
@@ -0,0 +1,345 @@
|
||||
---
|
||||
title: Vocab
|
||||
teaser: A storage class for vocabulary and other data shared across a language
|
||||
tag: class
|
||||
source: spacy/vocab.pyx
|
||||
---
|
||||
|
||||
The `Vocab` object provides a lookup table that allows you to access
|
||||
[`Lexeme`](/api/lexeme) objects, as well as the
|
||||
[`StringStore`](/api/stringstore). It also owns underlying C-data that is shared
|
||||
between `Doc` objects.
|
||||
|
||||
<Infobox variant ="warning">
|
||||
|
||||
Note that a `Vocab` instance is not static. It increases in size as texts with
|
||||
new tokens are processed. Some models may have an empty vocab at initialization.
|
||||
|
||||
</Infobox>
|
||||
|
||||
## Vocab.\_\_init\_\_ {id="init",tag="method"}
|
||||
|
||||
Create the vocabulary.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.vocab import Vocab
|
||||
> vocab = Vocab(strings=["hello", "world"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `lex_attr_getters` | A dictionary mapping attribute IDs to functions to compute them. Defaults to `None`. ~~Optional[Dict[str, Callable[[str], Any]]]~~ |
|
||||
| `strings` | A [`StringStore`](/api/stringstore) that maps strings to hash values, and vice versa, or a list of strings. ~~Union[List[str], StringStore]~~ |
|
||||
| `lookups` | A [`Lookups`](/api/lookups) that stores the `lexeme_norm` and other large lookup tables. Defaults to `None`. ~~Optional[Lookups]~~ |
|
||||
| `oov_prob` | The default OOV probability. Defaults to `-20.0`. ~~float~~ |
|
||||
| `vectors_name` | A name to identify the vectors table. ~~str~~ |
|
||||
| `writing_system` | A dictionary describing the language's writing system. Typically provided by [`Language.Defaults`](/api/language#defaults). ~~Dict[str, Any]~~ |
|
||||
| `get_noun_chunks` | A function that yields base noun phrases used for [`Doc.noun_chunks`](/api/doc#noun_chunks). ~~Optional[Callable[[Union[Doc, Span], Iterator[Tuple[int, int, int]]]]]~~ |
|
||||
|
||||
## Vocab.\_\_len\_\_ {id="len",tag="method"}
|
||||
|
||||
Get the current number of lexemes in the vocabulary.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a sentence.")
|
||||
> assert len(nlp.vocab) > 0
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ------------------------------------------------ |
|
||||
| **RETURNS** | The number of lexemes in the vocabulary. ~~int~~ |
|
||||
|
||||
## Vocab.\_\_getitem\_\_ {id="getitem",tag="method"}
|
||||
|
||||
Retrieve a lexeme, given an int ID or a string. If a previously unseen string is
|
||||
given, a new lexeme is created and stored.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> apple = nlp.vocab.strings["apple"]
|
||||
> assert nlp.vocab[apple] == nlp.vocab["apple"]
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------ |
|
||||
| `id_or_string` | The hash value of a word, or its string. ~~Union[int, str]~~ |
|
||||
| **RETURNS** | The lexeme indicated by the given ID. ~~Lexeme~~ |
|
||||
|
||||
## Vocab.\_\_iter\_\_ {id="iter",tag="method"}
|
||||
|
||||
Iterate over the lexemes in the vocabulary.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> stop_words = (lex for lex in nlp.vocab if lex.is_stop)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------- | -------------------------------------- |
|
||||
| **YIELDS** | An entry in the vocabulary. ~~Lexeme~~ |
|
||||
|
||||
## Vocab.\_\_contains\_\_ {id="contains",tag="method"}
|
||||
|
||||
Check whether the string has an entry in the vocabulary. To get the ID for a
|
||||
given string, you need to look it up in
|
||||
[`vocab.strings`](/api/vocab#attributes).
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> nlp("I'm eating an apple")
|
||||
> apple = nlp.vocab.strings["apple"]
|
||||
> oov = nlp.vocab.strings["dskfodkfos"]
|
||||
> assert apple in nlp.vocab
|
||||
> assert oov not in nlp.vocab
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ----------------------------------------------------------- |
|
||||
| `string` | The ID string. ~~str~~ |
|
||||
| **RETURNS** | Whether the string has an entry in the vocabulary. ~~bool~~ |
|
||||
|
||||
## Vocab.add_flag {id="add_flag",tag="method"}
|
||||
|
||||
Set a new boolean flag to words in the vocabulary. The `flag_getter` function
|
||||
will be called over the words currently in the vocab, and then applied to new
|
||||
words as they occur. You'll then be able to access the flag value on each token,
|
||||
using `token.check_flag(flag_id)`.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> def is_my_product(text):
|
||||
> products = ["spaCy", "Thinc", "displaCy"]
|
||||
> return text in products
|
||||
>
|
||||
> MY_PRODUCT = nlp.vocab.add_flag(is_my_product)
|
||||
> doc = nlp("I like spaCy")
|
||||
> assert doc[2].check_flag(MY_PRODUCT) == True
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `flag_getter` | A function that takes the lexeme text and returns the boolean flag value. ~~Callable[[str], bool]~~ |
|
||||
| `flag_id` | An integer between `1` and `63` (inclusive), specifying the bit at which the flag will be stored. If `-1`, the lowest available bit will be chosen. ~~int~~ |
|
||||
| **RETURNS** | The integer ID by which the flag value can be checked. ~~int~~ |
|
||||
|
||||
## Vocab.reset_vectors {id="reset_vectors",tag="method",version="2"}
|
||||
|
||||
Drop the current vector table. Because all vectors must be the same width, you
|
||||
have to call this to change the size of the vectors. Only one of the `width` and
|
||||
`shape` keyword arguments can be specified.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> nlp.vocab.reset_vectors(width=300)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ---------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `width` | The new width. ~~int~~ |
|
||||
| `shape` | The new shape. ~~int~~ |
|
||||
|
||||
## Vocab.prune_vectors {id="prune_vectors",tag="method",version="2"}
|
||||
|
||||
Reduce the current vector table to `nr_row` unique entries. Words mapped to the
|
||||
discarded vectors will be remapped to the closest vector among those remaining.
|
||||
For example, suppose the original table had vectors for the words:
|
||||
`['sat', 'cat', 'feline', 'reclined']`. If we prune the vector table to, two
|
||||
rows, we would discard the vectors for "feline" and "reclined". These words
|
||||
would then be remapped to the closest remaining vector – so "feline" would have
|
||||
the same vector as "cat", and "reclined" would have the same vector as "sat".
|
||||
The similarities are judged by cosine. The original vectors may be large, so the
|
||||
cosines are calculated in minibatches to reduce memory usage.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> nlp.vocab.prune_vectors(10000)
|
||||
> assert len(nlp.vocab.vectors) <= 10000
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `nr_row` | The number of rows to keep in the vector table. ~~int~~ |
|
||||
| `batch_size` | Batch of vectors for calculating the similarities. Larger batch sizes might be faster, while temporarily requiring more memory. ~~int~~ |
|
||||
| **RETURNS** | A dictionary keyed by removed words mapped to `(string, score)` tuples, where `string` is the entry the removed word was mapped to, and `score` the similarity score between the two words. ~~Dict[str, Tuple[str, float]]~~ |
|
||||
|
||||
## Vocab.deduplicate_vectors {id="deduplicate_vectors",tag="method",version="3.3"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> nlp.vocab.deduplicate_vectors()
|
||||
> ```
|
||||
|
||||
Remove any duplicate rows from the current vector table, maintaining the
|
||||
mappings for all words in the vectors.
|
||||
|
||||
## Vocab.get_vector {id="get_vector",tag="method",version="2"}
|
||||
|
||||
Retrieve a vector for a word in the vocabulary. Words can be looked up by string
|
||||
or hash value. If the current vectors do not contain an entry for the word, a
|
||||
0-vector with the same number of dimensions
|
||||
([`Vocab.vectors_length`](#attributes)) as the current vectors is returned.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> nlp.vocab.get_vector("apple")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | ---------------------------------------------------------------------------------------------------------------------- |
|
||||
| `orth` | The hash value of a word, or its unicode string. ~~Union[int, str]~~ |
|
||||
| **RETURNS** | A word vector. Size and shape are determined by the `Vocab.vectors` instance. ~~numpy.ndarray[ndim=1, dtype=float32]~~ |
|
||||
|
||||
## Vocab.set_vector {id="set_vector",tag="method",version="2"}
|
||||
|
||||
Set a vector for a word in the vocabulary. Words can be referenced by string or
|
||||
hash value.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> nlp.vocab.set_vector("apple", array([...]))
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------- | -------------------------------------------------------------------- |
|
||||
| `orth` | The hash value of a word, or its unicode string. ~~Union[int, str]~~ |
|
||||
| `vector` | The vector to set. ~~numpy.ndarray[ndim=1, dtype=float32]~~ |
|
||||
|
||||
## Vocab.has_vector {id="has_vector",tag="method",version="2"}
|
||||
|
||||
Check whether a word has a vector. Returns `False` if no vectors are loaded.
|
||||
Words can be looked up by string or hash value.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> if nlp.vocab.has_vector("apple"):
|
||||
> vector = nlp.vocab.get_vector("apple")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------- | -------------------------------------------------------------------- |
|
||||
| `orth` | The hash value of a word, or its unicode string. ~~Union[int, str]~~ |
|
||||
| **RETURNS** | Whether the word has a vector. ~~bool~~ |
|
||||
|
||||
## Vocab.to_disk {id="to_disk",tag="method",version="2"}
|
||||
|
||||
Save the current state to a directory.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> nlp.vocab.to_disk("/path/to/vocab")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
|
||||
## Vocab.from_disk {id="from_disk",tag="method",version="2"}
|
||||
|
||||
Loads state from a directory. Modifies the object in place and returns it.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.vocab import Vocab
|
||||
> vocab = Vocab().from_disk("/path/to/vocab")
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| `path` | A path to a directory. Paths may be either strings or `Path`-like objects. ~~Union[str, Path]~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The modified `Vocab` object. ~~Vocab~~ |
|
||||
|
||||
## Vocab.to_bytes {id="to_bytes",tag="method"}
|
||||
|
||||
Serialize the current state to a binary string.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> vocab_bytes = nlp.vocab.to_bytes()
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The serialized form of the `Vocab` object. ~~bytes~~ |
|
||||
|
||||
## Vocab.from_bytes {id="from_bytes",tag="method"}
|
||||
|
||||
Load state from a binary string.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.vocab import Vocab
|
||||
> vocab_bytes = nlp.vocab.to_bytes()
|
||||
> vocab = Vocab()
|
||||
> vocab.from_bytes(vocab_bytes)
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `bytes_data` | The data to load from. ~~bytes~~ |
|
||||
| _keyword-only_ | |
|
||||
| `exclude` | String names of [serialization fields](#serialization-fields) to exclude. ~~Iterable[str]~~ |
|
||||
| **RETURNS** | The `Vocab` object. ~~Vocab~~ |
|
||||
|
||||
## Attributes {id="attributes"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> apple_id = nlp.vocab.strings["apple"]
|
||||
> assert type(apple_id) == int
|
||||
> PERSON = nlp.vocab.strings["PERSON"]
|
||||
> assert type(PERSON) == int
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `strings` | A table managing the string-to-int mapping. ~~StringStore~~ |
|
||||
| `vectors` | A table associating word IDs to word vectors. ~~Vectors~~ |
|
||||
| `vectors_length` | Number of dimensions for each word vector. ~~int~~ |
|
||||
| `lookups` | The available lookup tables in this vocab. ~~Lookups~~ |
|
||||
| `writing_system` | A dict with information about the language's writing system. ~~Dict[str, Any]~~ |
|
||||
| `get_noun_chunks` <Tag variant="new">3.0</Tag> | A function that yields base noun phrases used for [`Doc.noun_chunks`](/api/doc#noun_chunks). ~~Optional[Callable[[Union[Doc, Span], Iterator[Tuple[int, int, int]]]]]~~ |
|
||||
|
||||
## Serialization fields {id="serialization-fields"}
|
||||
|
||||
During serialization, spaCy will export several data fields used to restore
|
||||
different aspects of the object. If needed, you can exclude them from
|
||||
serialization by passing in the string names via the `exclude` argument.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = vocab.to_bytes(exclude=["strings", "vectors"])
|
||||
> vocab.from_disk("./vocab", exclude=["strings"])
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------- | ----------------------------------------------------- |
|
||||
| `strings` | The strings in the [`StringStore`](/api/stringstore). |
|
||||
| `vectors` | The word vectors, if available. |
|
||||
| `lookups` | The lookup tables, if available. |
|
||||
@@ -0,0 +1,212 @@
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:xlink="http://www.w3.org/1999/xlink"
|
||||
id="0"
|
||||
class="displacy"
|
||||
width="1275"
|
||||
height="399.5"
|
||||
style="
|
||||
max-width: none;
|
||||
height: 399.5px;
|
||||
color: #000000;
|
||||
background: #ffffff;
|
||||
font-family: Arial;
|
||||
"
|
||||
>
|
||||
<text class="displacy-token" fill="currentColor" text-anchor="middle" y="309.5">
|
||||
<tspan class="displacy-word" fill="currentColor" x="50">Autonomous</tspan>
|
||||
<tspan class="displacy-tag" dy="2em" fill="currentColor" x="50">ADJ</tspan>
|
||||
</text>
|
||||
|
||||
<text class="displacy-token" fill="currentColor" text-anchor="middle" y="309.5">
|
||||
<tspan class="displacy-word" fill="currentColor" x="225">cars</tspan>
|
||||
<tspan class="displacy-tag" dy="2em" fill="currentColor" x="225">NOUN</tspan>
|
||||
</text>
|
||||
|
||||
<text class="displacy-token" fill="currentColor" text-anchor="middle" y="309.5">
|
||||
<tspan class="displacy-word" fill="currentColor" x="400">shift</tspan>
|
||||
<tspan class="displacy-tag" dy="2em" fill="currentColor" x="400">VERB</tspan>
|
||||
</text>
|
||||
|
||||
<text class="displacy-token" fill="currentColor" text-anchor="middle" y="309.5">
|
||||
<tspan class="displacy-word" fill="currentColor" x="575">insurance</tspan>
|
||||
<tspan class="displacy-tag" dy="2em" fill="currentColor" x="575">NOUN</tspan>
|
||||
</text>
|
||||
|
||||
<text class="displacy-token" fill="currentColor" text-anchor="middle" y="309.5">
|
||||
<tspan class="displacy-word" fill="currentColor" x="750">liability</tspan>
|
||||
<tspan class="displacy-tag" dy="2em" fill="currentColor" x="750">NOUN</tspan>
|
||||
</text>
|
||||
|
||||
<text class="displacy-token" fill="currentColor" text-anchor="middle" y="309.5">
|
||||
<tspan class="displacy-word" fill="currentColor" x="925">toward</tspan>
|
||||
<tspan class="displacy-tag" dy="2em" fill="currentColor" x="925">ADP</tspan>
|
||||
</text>
|
||||
|
||||
<text class="displacy-token" fill="currentColor" text-anchor="middle" y="309.5">
|
||||
<tspan class="displacy-word" fill="currentColor" x="1100">manufacturers</tspan>
|
||||
<tspan class="displacy-tag" dy="2em" fill="currentColor" x="1100">NOUN</tspan>
|
||||
</text>
|
||||
|
||||
<g class="displacy-arrow">
|
||||
<path
|
||||
class="displacy-arc"
|
||||
id="arrow-0-0"
|
||||
stroke-width="2px"
|
||||
d="M70,264.5 C70,177.0 215.0,177.0 215.0,264.5"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
></path>
|
||||
<text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">
|
||||
<textpath
|
||||
xlink:href="#arrow-0-0"
|
||||
class="displacy-label"
|
||||
startOffset="50%"
|
||||
fill="currentColor"
|
||||
text-anchor="middle"
|
||||
>
|
||||
amod
|
||||
</textpath>
|
||||
</text>
|
||||
<path
|
||||
class="displacy-arrowhead"
|
||||
d="M70,266.5 L62,254.5 78,254.5"
|
||||
fill="currentColor"
|
||||
></path>
|
||||
</g>
|
||||
|
||||
<g class="displacy-arrow">
|
||||
<path
|
||||
class="displacy-arc"
|
||||
id="arrow-0-1"
|
||||
stroke-width="2px"
|
||||
d="M245,264.5 C245,177.0 390.0,177.0 390.0,264.5"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
></path>
|
||||
<text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">
|
||||
<textpath
|
||||
xlink:href="#arrow-0-1"
|
||||
class="displacy-label"
|
||||
startOffset="50%"
|
||||
fill="currentColor"
|
||||
text-anchor="middle"
|
||||
>
|
||||
nsubj
|
||||
</textpath>
|
||||
</text>
|
||||
<path
|
||||
class="displacy-arrowhead"
|
||||
d="M245,266.5 L237,254.5 253,254.5"
|
||||
fill="currentColor"
|
||||
></path>
|
||||
</g>
|
||||
|
||||
<g class="displacy-arrow">
|
||||
<path
|
||||
class="displacy-arc"
|
||||
id="arrow-0-2"
|
||||
stroke-width="2px"
|
||||
d="M595,264.5 C595,177.0 740.0,177.0 740.0,264.5"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
></path>
|
||||
<text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">
|
||||
<textpath
|
||||
xlink:href="#arrow-0-2"
|
||||
class="displacy-label"
|
||||
startOffset="50%"
|
||||
fill="currentColor"
|
||||
text-anchor="middle"
|
||||
>
|
||||
compound
|
||||
</textpath>
|
||||
</text>
|
||||
<path
|
||||
class="displacy-arrowhead"
|
||||
d="M595,266.5 L587,254.5 603,254.5"
|
||||
fill="currentColor"
|
||||
></path>
|
||||
</g>
|
||||
|
||||
<g class="displacy-arrow">
|
||||
<path
|
||||
class="displacy-arc"
|
||||
id="arrow-0-3"
|
||||
stroke-width="2px"
|
||||
d="M420,264.5 C420,89.5 745.0,89.5 745.0,264.5"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
></path>
|
||||
<text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">
|
||||
<textpath
|
||||
xlink:href="#arrow-0-3"
|
||||
class="displacy-label"
|
||||
startOffset="50%"
|
||||
fill="currentColor"
|
||||
text-anchor="middle"
|
||||
>
|
||||
dobj
|
||||
</textpath>
|
||||
</text>
|
||||
<path
|
||||
class="displacy-arrowhead"
|
||||
d="M745.0,266.5 L753.0,254.5 737.0,254.5"
|
||||
fill="currentColor"
|
||||
></path>
|
||||
</g>
|
||||
|
||||
<g class="displacy-arrow">
|
||||
<path
|
||||
class="displacy-arc"
|
||||
id="arrow-0-4"
|
||||
stroke-width="2px"
|
||||
d="M420,264.5 C420,2.0 925.0,2.0 925.0,264.5"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
></path>
|
||||
<text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">
|
||||
<textpath
|
||||
xlink:href="#arrow-0-4"
|
||||
class="displacy-label"
|
||||
startOffset="50%"
|
||||
fill="currentColor"
|
||||
text-anchor="middle"
|
||||
>
|
||||
prep
|
||||
</textpath>
|
||||
</text>
|
||||
<path
|
||||
class="displacy-arrowhead"
|
||||
d="M925.0,266.5 L933.0,254.5 917.0,254.5"
|
||||
fill="currentColor"
|
||||
></path>
|
||||
</g>
|
||||
|
||||
<g class="displacy-arrow">
|
||||
<path
|
||||
class="displacy-arc"
|
||||
id="arrow-0-5"
|
||||
stroke-width="2px"
|
||||
d="M945,264.5 C945,177.0 1090.0,177.0 1090.0,264.5"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
></path>
|
||||
<text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">
|
||||
<textpath
|
||||
xlink:href="#arrow-0-5"
|
||||
class="displacy-label"
|
||||
startOffset="50%"
|
||||
fill="currentColor"
|
||||
text-anchor="middle"
|
||||
>
|
||||
pobj
|
||||
</textpath>
|
||||
</text>
|
||||
<path
|
||||
class="displacy-arrowhead"
|
||||
d="M1090.0,266.5 L1098.0,254.5 1082.0,254.5"
|
||||
fill="currentColor"
|
||||
></path>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 6.8 KiB |
@@ -0,0 +1,271 @@
|
||||
---
|
||||
title: Trained Models & Pipelines
|
||||
teaser: Downloadable trained pipelines and weights for spaCy
|
||||
menu:
|
||||
- ['Quickstart', 'quickstart']
|
||||
- ['Conventions', 'conventions']
|
||||
- ['Pipeline Design', 'design']
|
||||
---
|
||||
|
||||
{/* TODO: include interactive demo */}
|
||||
|
||||
### Quickstart {hidden="true"}
|
||||
|
||||
> #### 📖 Installation and usage
|
||||
>
|
||||
> For more details on how to use trained pipelines with spaCy, see the
|
||||
> [usage guide](/usage/models).
|
||||
|
||||
<QuickstartModels id="quickstart" />
|
||||
|
||||
## Package naming conventions {id="conventions"}
|
||||
|
||||
In general, spaCy expects all pipeline packages to follow the naming convention
|
||||
of `[lang]_[name]`. For spaCy's pipelines, we also chose to divide the name into
|
||||
three components:
|
||||
|
||||
1. **Type:** Capabilities (e.g. `core` for general-purpose pipeline with
|
||||
tagging, parsing, lemmatization and named entity recognition, or `dep` for
|
||||
only tagging, parsing and lemmatization).
|
||||
2. **Genre:** Type of text the pipeline is trained on, e.g. `web` or `news`.
|
||||
3. **Size:** Package size indicator, `sm`, `md`, `lg` or `trf`.
|
||||
|
||||
`sm` and `trf` pipelines have no static word vectors.
|
||||
|
||||
For pipelines with default vectors, `md` has a reduced word vector table with
|
||||
20k unique vectors for ~500k words and `lg` has a large word vector table
|
||||
with ~500k entries.
|
||||
|
||||
For pipelines with floret vectors, `md` vector tables have 50k entries and
|
||||
`lg` vector tables have 200k entries.
|
||||
|
||||
For example, [`en_core_web_sm`](/models/en#en_core_web_sm) is a small English
|
||||
pipeline trained on written web text (blogs, news, comments), that includes
|
||||
vocabulary, syntax and entities.
|
||||
|
||||
### Package versioning {id="model-versioning"}
|
||||
|
||||
Additionally, the pipeline package versioning reflects both the compatibility
|
||||
with spaCy, as well as the model version. A package version `a.b.c` translates
|
||||
to:
|
||||
|
||||
- `a`: **spaCy major version**. For example, `2` for spaCy v2.x.
|
||||
- `b`: **spaCy minor version**. For example, `3` for spaCy v2.3.x.
|
||||
- `c`: **Model version**. Different model config: e.g. from being trained on
|
||||
different data, with different parameters, for different numbers of
|
||||
iterations, with different vectors, etc.
|
||||
|
||||
For a detailed compatibility overview, see the
|
||||
[`compatibility.json`](https://github.com/explosion/spacy-models/tree/master/compatibility.json).
|
||||
This is also the source of spaCy's internal compatibility check, performed when
|
||||
you run the [`download`](/api/cli#download) command.
|
||||
|
||||
## Trained pipeline design {id="design"}
|
||||
|
||||
The spaCy v3 trained pipelines are designed to be efficient and configurable.
|
||||
For example, multiple components can share a common "token-to-vector" model and
|
||||
it's easy to swap out or disable the lemmatizer. The pipelines are designed to
|
||||
be efficient in terms of speed and size and work well when the pipeline is run
|
||||
in full.
|
||||
|
||||
When modifying a trained pipeline, it's important to understand how the
|
||||
components **depend on** each other. Unlike spaCy v2, where the `tagger`,
|
||||
`parser` and `ner` components were all independent, some v3 components depend on
|
||||
earlier components in the pipeline. As a result, disabling or reordering
|
||||
components can affect the annotation quality or lead to warnings and errors.
|
||||
|
||||
Main changes from spaCy v2 models:
|
||||
|
||||
- The [`Tok2Vec`](/api/tok2vec) component may be a separate, shared component. A
|
||||
component like a tagger or parser can
|
||||
[listen](/api/architectures#Tok2VecListener) to an earlier `tok2vec` or
|
||||
`transformer` rather than having its own separate tok2vec layer.
|
||||
- Rule-based exceptions move from individual components to the
|
||||
`attribute_ruler`. Lemma and POS exceptions move from the tokenizer exceptions
|
||||
to the attribute ruler and the tag map and morph rules move from the tagger to
|
||||
the attribute ruler.
|
||||
- The lemmatizer tables and processing move from the vocab and tagger to a
|
||||
separate `lemmatizer` component.
|
||||
|
||||
### CNN/CPU pipeline design {id="design-cnn"}
|
||||
|
||||

|
||||
|
||||
In the `sm`/`md`/`lg` models:
|
||||
|
||||
- The `tagger`, `morphologizer` and `parser` components listen to the `tok2vec`
|
||||
component. If the lemmatizer is trainable (v3.3+), `lemmatizer` also listens
|
||||
to `tok2vec`.
|
||||
- The `attribute_ruler` maps `token.tag` to `token.pos` if there is no
|
||||
`morphologizer`. The `attribute_ruler` additionally makes sure whitespace is
|
||||
tagged consistently and copies `token.pos` to `token.tag` if there is no
|
||||
tagger. For English, the attribute ruler can improve its mapping from
|
||||
`token.tag` to `token.pos` if dependency parses from a `parser` are present,
|
||||
but the parser is not required.
|
||||
- The `lemmatizer` component for many languages requires `token.pos` annotation
|
||||
from either `tagger`+`attribute_ruler` or `morphologizer`.
|
||||
- The `ner` component is independent with its own internal tok2vec layer.
|
||||
|
||||
#### CNN/CPU pipelines with floret vectors
|
||||
|
||||
The Croatian, Finnish, Korean, Slovenian, Swedish and Ukrainian `md` and `lg`
|
||||
pipelines use [floret vectors](/usage/v3-2#vectors) instead of default vectors.
|
||||
If you're running a trained pipeline on texts and working with [`Doc`](/api/doc)
|
||||
objects, you shouldn't notice any difference with floret vectors. With floret
|
||||
vectors no tokens are out-of-vocabulary, so
|
||||
[`Token.is_oov`](/api/token#attributes) will return `False` for all tokens.
|
||||
|
||||
If you access vectors directly for similarity comparisons, there are a few
|
||||
differences because floret vectors don't include a fixed word list like the
|
||||
vector keys for default vectors.
|
||||
|
||||
- If your workflow iterates over the vector keys, you need to use an external
|
||||
word list instead:
|
||||
|
||||
```diff
|
||||
- lexemes = [nlp.vocab[orth] for orth in nlp.vocab.vectors]
|
||||
+ lexemes = [nlp.vocab[word] for word in external_word_list]
|
||||
```
|
||||
|
||||
- [`Vectors.most_similar`](/api/vectors#most_similar) is not supported because
|
||||
there's no fixed list of vectors to compare your vectors to.
|
||||
|
||||
### Transformer pipeline design {id="design-trf"}
|
||||
|
||||
In the transformer (`trf`) pipelines, the `tagger`, `parser` and `ner` (if
|
||||
present) all listen to the `transformer` component. The `attribute_ruler` and
|
||||
`lemmatizer` have the same configuration as in the CNN models.
|
||||
|
||||
For spaCy v3.0-v3.6, `trf` pipelines use
|
||||
[`spacy-transformers`](https://github.com/explosion/spacy-transformers) and the
|
||||
transformer output in `doc._.trf_data` is a
|
||||
[`TransformerData`](/api/transformer#transformerdata) object.
|
||||
|
||||
For spaCy v3.7+, `trf` pipelines use
|
||||
[`spacy-curated-transformers`](https://github.com/explosion/spacy-curated-transformers)
|
||||
and `doc._.trf_data` is a
|
||||
[`DocTransformerOutput`](/api/curatedtransformer#doctransformeroutput) object.
|
||||
|
||||
### Modifying the default pipeline {id="design-modify"}
|
||||
|
||||
For faster processing, you may only want to run a subset of the components in a
|
||||
trained pipeline. The `disable` and `exclude` arguments to
|
||||
[`spacy.load`](/api/top-level#spacy.load) let you control which components are
|
||||
loaded and run. Disabled components are loaded in the background so it's
|
||||
possible to reenable them in the same pipeline in the future with
|
||||
[`nlp.enable_pipe`](/api/language/#enable_pipe). To skip loading a component
|
||||
completely, use `exclude` instead of `disable`.
|
||||
|
||||
#### Disable part-of-speech tagging and lemmatization
|
||||
|
||||
To disable part-of-speech tagging and lemmatization, disable the `tagger`,
|
||||
`morphologizer`, `attribute_ruler` and `lemmatizer` components.
|
||||
|
||||
```python
|
||||
# Note: English doesn't include a morphologizer
|
||||
nlp = spacy.load("en_core_web_sm", disable=["tagger", "attribute_ruler", "lemmatizer"])
|
||||
nlp = spacy.load("en_core_web_trf", disable=["tagger", "attribute_ruler", "lemmatizer"])
|
||||
```
|
||||
|
||||
<Infobox variant="warning" title="Rule-based and POS-lookup lemmatizers require
|
||||
Token.pos">
|
||||
|
||||
The lemmatizer depends on `tagger`+`attribute_ruler` or `morphologizer` for a
|
||||
number of languages. If you disable any of these components, you'll see
|
||||
lemmatizer warnings unless the lemmatizer is also disabled.
|
||||
|
||||
**v3.3**: Catalan, English, French, Russian and Spanish
|
||||
|
||||
**v3.0-v3.2**: Catalan, Dutch, English, French, Greek, Italian, Macedonian,
|
||||
Norwegian, Polish, Russian and Spanish
|
||||
|
||||
</Infobox>
|
||||
|
||||
#### Use senter rather than parser for fast sentence segmentation
|
||||
|
||||
If you need fast sentence segmentation without dependency parses, disable the
|
||||
`parser` use the `senter` component instead:
|
||||
|
||||
```python
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
nlp.disable_pipe("parser")
|
||||
nlp.enable_pipe("senter")
|
||||
```
|
||||
|
||||
The `senter` component is ~10× faster than the parser and more accurate
|
||||
than the rule-based `sentencizer`.
|
||||
|
||||
#### Switch from trainable lemmatizer to default lemmatizer
|
||||
|
||||
Since v3.3, a number of pipelines use a trainable lemmatizer. You can check
|
||||
whether the lemmatizer is trainable:
|
||||
|
||||
```python
|
||||
nlp = spacy.load("de_core_web_sm")
|
||||
assert nlp.get_pipe("lemmatizer").is_trainable
|
||||
```
|
||||
|
||||
If you'd like to switch to a non-trainable lemmatizer that's similar to v3.2 or
|
||||
earlier, you can replace the trainable lemmatizer with the default non-trainable
|
||||
lemmatizer:
|
||||
|
||||
```python
|
||||
# Requirements: pip install spacy-lookups-data
|
||||
nlp = spacy.load("de_core_web_sm")
|
||||
# Remove existing lemmatizer
|
||||
nlp.remove_pipe("lemmatizer")
|
||||
# Add non-trainable lemmatizer from language defaults
|
||||
# and load lemmatizer tables from spacy-lookups-data
|
||||
nlp.add_pipe("lemmatizer").initialize()
|
||||
```
|
||||
|
||||
#### Switch from rule-based to lookup lemmatization
|
||||
|
||||
For the Dutch, English, French, Greek, Macedonian, Norwegian and Spanish
|
||||
pipelines, you can swap out a trainable or rule-based lemmatizer for a lookup
|
||||
lemmatizer:
|
||||
|
||||
```python
|
||||
# Requirements: pip install spacy-lookups-data
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
nlp.remove_pipe("lemmatizer")
|
||||
nlp.add_pipe("lemmatizer", config={"mode": "lookup"}).initialize()
|
||||
```
|
||||
|
||||
#### Disable everything except NER
|
||||
|
||||
For the non-transformer models, the `ner` component is independent, so you can
|
||||
disable everything else:
|
||||
|
||||
```python
|
||||
nlp = spacy.load("en_core_web_sm", disable=["tok2vec", "tagger", "parser", "attribute_ruler", "lemmatizer"])
|
||||
```
|
||||
|
||||
In the transformer models, `ner` listens to the `transformer` component, so you
|
||||
can disable all components related tagging, parsing, and lemmatization.
|
||||
|
||||
```python
|
||||
nlp = spacy.load("en_core_web_trf", disable=["tagger", "parser", "attribute_ruler", "lemmatizer"])
|
||||
```
|
||||
|
||||
#### Move NER to the end of the pipeline
|
||||
|
||||
<Infobox title="For v3.0.x models only" variant="warning">
|
||||
|
||||
As of v3.1, the NER component is at the end of the pipeline by default.
|
||||
|
||||
</Infobox>
|
||||
|
||||
For access to `POS` and `LEMMA` features in an `entity_ruler`, move `ner` to the
|
||||
end of the pipeline after `attribute_ruler` and `lemmatizer`:
|
||||
|
||||
```python
|
||||
# load without NER
|
||||
nlp = spacy.load("en_core_web_sm", exclude=["ner"])
|
||||
|
||||
# source NER from the same pipeline package as the last component
|
||||
nlp.add_pipe("ner", source=spacy.load("en_core_web_sm"))
|
||||
|
||||
# insert the entity ruler
|
||||
nlp.add_pipe("entity_ruler", before="ner")
|
||||
```
|
||||
@@ -0,0 +1,615 @@
|
||||
---
|
||||
title: Styleguide
|
||||
section: styleguide
|
||||
search_exclude: true
|
||||
menu:
|
||||
- ['Logo', 'logo']
|
||||
- ['Colors', 'colors']
|
||||
- ['Typography', 'typography']
|
||||
- ['Elements', 'elements']
|
||||
- ['Components', 'components']
|
||||
- ['Markdown Reference', 'markdown']
|
||||
- ['Editorial', 'editorial']
|
||||
sidebar:
|
||||
- label: Styleguide
|
||||
items:
|
||||
- text: ''
|
||||
url: '/styleguide'
|
||||
- label: Resources
|
||||
items:
|
||||
- text: Website Source
|
||||
url: https://github.com/explosion/spacy/tree/master/website
|
||||
- text: Contributing Guide
|
||||
url: https://github.com/explosion/spaCy/blob/master/CONTRIBUTING.md
|
||||
---
|
||||
|
||||
The [spacy.io](https://spacy.io) website is implemented using
|
||||
[Gatsby](https://www.gatsbyjs.org) with
|
||||
[Remark](https://github.com/remarkjs/remark) and [MDX](https://mdxjs.com/). This
|
||||
allows authoring content in **straightforward Markdown** without the usual
|
||||
limitations. Standard elements can be overwritten with powerful
|
||||
[React](http://reactjs.org/) components and wherever Markdown syntax isn't
|
||||
enough, JSX components can be used.
|
||||
|
||||
> #### Contributing to the site
|
||||
>
|
||||
> The docs can always use another example or more detail, and they should always
|
||||
> be up to date and not misleading. We always appreciate a
|
||||
> [pull request](https://github.com/explosion/spaCy/pulls). To quickly find the
|
||||
> correct file to edit, simply click on the "Suggest edits" button at the bottom
|
||||
> of a page.
|
||||
>
|
||||
> For more details on editing the site locally, see the installation
|
||||
> instructions and markdown reference below.
|
||||
|
||||
## Logo {id="logo",source="website/src/images/logo.svg"}
|
||||
|
||||
If you would like to use the spaCy logo on your site, please get in touch and
|
||||
ask us first. However, if you want to show support and tell others that your
|
||||
project is using spaCy, you can grab one of our
|
||||
[spaCy badges](/usage/spacy-101#faq-project-with-spacy).
|
||||
|
||||
<Logos />
|
||||
|
||||
## Colors {id="colors"}
|
||||
|
||||
<Colors />
|
||||
|
||||
### Patterns
|
||||
|
||||
<Patterns />
|
||||
|
||||
## Typography {id="typography"}
|
||||
|
||||
> #### Markdown
|
||||
>
|
||||
> ```markdown
|
||||
> ## Headline 2
|
||||
>
|
||||
> ## Headline 2 {id="some_id"}
|
||||
>
|
||||
> ## Headline 2 {id="some_id" tag="method"}
|
||||
> ```
|
||||
>
|
||||
> #### JSX
|
||||
>
|
||||
> ```jsx
|
||||
> <H2>Headline 2</H2>
|
||||
> <H2 id="some_id">Headline 2</H2>
|
||||
> <H2 id="some_id" tag="method">Headline 2</H2>
|
||||
> ```
|
||||
|
||||
Headlines are set in
|
||||
[HK Grotesk](http://cargocollective.com/hanken/HK-Grotesk-Open-Source-Font) by
|
||||
Hanken Design. All other body text and code uses the best-matching default
|
||||
system font to provide a "native" reading experience. All code uses the
|
||||
[JetBrains Mono](https://www.jetbrains.com/lp/mono/) typeface by JetBrains.
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
Level 2 headings are automatically wrapped in `<section>` elements at compile
|
||||
time, using a custom
|
||||
[Markdown transformer](https://github.com/explosion/spaCy/tree/master/website/plugins/remark-wrap-section.js).
|
||||
This makes it easier to highlight the section that's currently in the viewpoint
|
||||
in the sidebar menu.
|
||||
|
||||
</Infobox>
|
||||
|
||||
<div>
|
||||
<H2>Headline 2</H2>
|
||||
<H3>Headline 3</H3>
|
||||
<H4>Headline 4</H4>
|
||||
<H5>Headline 5</H5>
|
||||
<Label>Label</Label>
|
||||
</div>
|
||||
|
||||
---
|
||||
|
||||
The following optional attributes can be set on the headline to modify it. For
|
||||
example, to add a tag for the documented type or mark features that have been
|
||||
introduced in a specific version or require statistical models to be loaded.
|
||||
Tags are also available as standalone `<Tag />` components.
|
||||
|
||||
| Argument | Example | Result |
|
||||
| --------- | -------------------------- | ----------------------------------------- |
|
||||
| `tag` | `{tag="method"}` | <Tag>method</Tag> |
|
||||
| `version` | `{version="3"}` | <Tag variant="new">3</Tag> |
|
||||
| `model` | `{model="tagger, parser"}` | <Tag variant="model">tagger, parser</Tag> |
|
||||
| `hidden` | `{hidden="true"}` | |
|
||||
|
||||
## Elements {id="elements"}
|
||||
|
||||
### Links {id="links"}
|
||||
|
||||
> #### Markdown
|
||||
>
|
||||
> ```markdown
|
||||
> [I am a link](https://spacy.io)
|
||||
> ```
|
||||
>
|
||||
> #### JSX
|
||||
>
|
||||
> ```jsx
|
||||
> <Link to="https://spacy.io">I am a link</Link>
|
||||
> ```
|
||||
|
||||
Special link styles are used depending on the link URL.
|
||||
|
||||
- [I am a regular external link](https://explosion.ai)
|
||||
- [I am a link to the documentation](/api/doc)
|
||||
- [I am a link to an architecture](/api/architectures#HashEmbedCNN)
|
||||
- [I am a link to a model](/models/en#en_core_web_sm)
|
||||
- [I am a link to GitHub](https://github.com/explosion/spaCy)
|
||||
|
||||
### Abbreviations {id="abbr"}
|
||||
|
||||
> #### JSX
|
||||
>
|
||||
> ```jsx
|
||||
> <Abbr title="Explanation">Abbreviation</Abbr>
|
||||
> ```
|
||||
|
||||
Some text with <Abbr title="Explanation here">an abbreviation</Abbr>. On small
|
||||
screens, I collapse and the explanation text is displayed next to the
|
||||
abbreviation.
|
||||
|
||||
### Tags {id="tags"}
|
||||
|
||||
> ```jsx
|
||||
> <Tag>method</Tag>
|
||||
> <Tag variant="version">4</Tag>
|
||||
> <Tag variant="model">tagger, parser</Tag>
|
||||
> ```
|
||||
|
||||
Tags can be used together with headlines, or next to properties across the
|
||||
documentation, and combined with tooltips to provide additional information. An
|
||||
optional `variant` argument can be used for special tags. `variant="new"` makes
|
||||
the tag take a version number to mark new features. Using the component,
|
||||
visibility of this tag can later be toggled once the feature isn't considered
|
||||
new anymore. Setting `variant="model"` takes a description of model capabilities
|
||||
and can be used to mark features that require a respective model to be
|
||||
installed.
|
||||
|
||||
<p>
|
||||
<Tag>method</Tag>
|
||||
<Tag variant="new">4</Tag>
|
||||
<Tag variant="model">tagger, parser</Tag>
|
||||
</p>
|
||||
|
||||
### Buttons {id="buttons"}
|
||||
|
||||
> ```jsx
|
||||
> <Button to="#" variant="primary">Primary small</Button>
|
||||
> <Button to="#" variant="secondary">Secondary small</Button>
|
||||
> ```
|
||||
|
||||
Link buttons come in two variants, `primary` and `secondary` and two sizes, with
|
||||
an optional `large` size modifier. Since they're mostly used as enhanced links,
|
||||
the buttons are implemented as styled links instead of native button elements.
|
||||
|
||||
<p>
|
||||
<Button to="#" variant="primary">Primary small</Button>
|
||||
|
||||
{' '}
|
||||
|
||||
<Button to="#" variant="secondary">Secondary small</Button>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<Button to="#" variant="primary">Primary small</Button>
|
||||
|
||||
{' '}
|
||||
|
||||
<Button to="#" variant="secondary">Secondary small</Button>
|
||||
</p>
|
||||
|
||||
## Components
|
||||
|
||||
### Table {id="table"}
|
||||
|
||||
> #### Markdown
|
||||
>
|
||||
> ```markdown
|
||||
> | Header 1 | Header 2 |
|
||||
> | -------- | -------- |
|
||||
> | Column 1 | Column 2 |
|
||||
> ```
|
||||
>
|
||||
> #### JSX
|
||||
>
|
||||
> ```markup
|
||||
> <Table>
|
||||
> <Tr><Th>Header 1</Th><Th>Header 2</Th></Tr></thead>
|
||||
> <Tr><Td>Column 1</Td><Td>Column 2</Td></Tr>
|
||||
> </Table>
|
||||
> ```
|
||||
|
||||
Tables are used to present data and API documentation. Certain keywords can be
|
||||
used to mark a footer row with a distinct style, for example to visualize the
|
||||
return values of a documented function.
|
||||
|
||||
| Header 1 | Header 2 | Header 3 | Header 4 |
|
||||
| ----------- | -------- | :------: | -------: |
|
||||
| Column 1 | Column 2 | Column 3 | Column 4 |
|
||||
| Column 1 | Column 2 | Column 3 | Column 4 |
|
||||
| Column 1 | Column 2 | Column 3 | Column 4 |
|
||||
| Column 1 | Column 2 | Column 3 | Column 4 |
|
||||
| **RETURNS** | Column 2 | Column 3 | Column 4 |
|
||||
|
||||
Tables also support optional "divider" rows that are typically used to denote
|
||||
keyword-only arguments in API documentation. To turn a row into a dividing
|
||||
headline, it should only include content in its first cell, and its value should
|
||||
be italicized:
|
||||
|
||||
> #### Markdown
|
||||
>
|
||||
> ```markdown
|
||||
> | Header 1 | Header 2 | Header 3 |
|
||||
> | -------- | -------- | -------- |
|
||||
> | Column 1 | Column 2 | Column 3 |
|
||||
> | _Hello_ | | |
|
||||
> | Column 1 | Column 2 | Column 3 |
|
||||
> ```
|
||||
|
||||
| Header 1 | Header 2 | Header 3 |
|
||||
| -------- | -------- | -------- |
|
||||
| Column 1 | Column 2 | Column 3 |
|
||||
| _Hello_ | | |
|
||||
| Column 1 | Column 2 | Column 3 |
|
||||
|
||||
### Type Annotations {id="type-annotations"}
|
||||
|
||||
> #### Markdown
|
||||
>
|
||||
> ```markdown
|
||||
> ~~Model[List[Doc], Floats2d]~~
|
||||
> ```
|
||||
>
|
||||
> #### JSX
|
||||
>
|
||||
> ```markup
|
||||
> <TypeAnnotation>Model[List[Doc], Floats2d]</Typeannotation>
|
||||
> ```
|
||||
|
||||
Type annotations are special inline code blocks are used to describe Python
|
||||
types in the [type hints](https://docs.python.org/3/library/typing.html) format.
|
||||
The special component will split the type, apply syntax highlighting and link
|
||||
all types that specify links in `meta/type-annotations.json`. Types can link to
|
||||
internal or external documentation pages. To make it easy to represent the type
|
||||
annotations in Markdown, the rendering "hijacks" the `~~` tags that would
|
||||
typically be converted to a `<del>` element – but in this case, text surrounded
|
||||
by `~~` becomes a type annotation.
|
||||
|
||||
- ~~Dict[str, List[Union[Doc, Span]]]~~
|
||||
- ~~Model[List[Doc], List[numpy.ndarray]]~~
|
||||
|
||||
Type annotations support a special visual style in tables and will render as a
|
||||
separate row, under the cell text. This allows the API docs to display complex
|
||||
types without taking up too much space in the cell. The type annotation should
|
||||
always be the **last element** in the row.
|
||||
|
||||
> #### Markdown
|
||||
>
|
||||
> ```markdown
|
||||
> | Header 1 | Header 2 |
|
||||
> | -------- | ---------------------- |
|
||||
> | Column 1 | Column 2 ~~List[Doc]~~ |
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vocab` | The shared vocabulary. ~~Vocab~~ |
|
||||
| `model` | The Thinc [`Model`](https://thinc.ai/docs/api-model) wrapping the transformer. ~~Model[List[Doc], FullTransformerBatch]~~ |
|
||||
| `set_extra_annotations` | Function that takes a batch of `Doc` objects and transformer outputs and can set additional annotations on the `Doc`. ~~Callable[[List[Doc], FullTransformerBatch], None]~~ |
|
||||
|
||||
### List {id="list"}
|
||||
|
||||
> #### Markdown
|
||||
>
|
||||
> ```markdown
|
||||
> 1. One
|
||||
> 2. Two
|
||||
> ```
|
||||
>
|
||||
> #### JSX
|
||||
>
|
||||
> ```markup
|
||||
> <Ol>
|
||||
> <Li>One</Li>
|
||||
> <Li>Two</Li>
|
||||
> </Ol>
|
||||
> ```
|
||||
|
||||
Lists are available as bulleted and numbered. Markdown lists are transformed
|
||||
automatically.
|
||||
|
||||
- I am a bulleted list
|
||||
- I have nice bullets
|
||||
- Lorem ipsum dolor
|
||||
- consectetur adipiscing elit
|
||||
|
||||
1. I am an ordered list
|
||||
2. I have nice numbers
|
||||
3. Lorem ipsum dolor
|
||||
4. consectetur adipiscing elit
|
||||
|
||||
### Aside {id="aside"}
|
||||
|
||||
> #### Markdown
|
||||
>
|
||||
> ```markdown
|
||||
> > #### Aside title
|
||||
> >
|
||||
> > This is aside text.
|
||||
> ```
|
||||
>
|
||||
> #### JSX
|
||||
>
|
||||
> ```jsx
|
||||
> <Aside title="Aside title">This is aside text.</Aside>
|
||||
> ```
|
||||
|
||||
Asides can be used to display additional notes and content in the right-hand
|
||||
column. Asides can contain text, code and other elements if needed. Visually,
|
||||
asides are moved to the side on the X-axis, and displayed at the same level they
|
||||
were inserted. On small screens, they collapse and are rendered in their
|
||||
original position, in between the text.
|
||||
|
||||
To make them easier to use in Markdown, paragraphs formatted as blockquotes will
|
||||
turn into asides by default. Level 4 headlines (with a leading `####`) will
|
||||
become aside titles.
|
||||
|
||||
### Code Block {id="code-block"}
|
||||
|
||||
> #### Markdown
|
||||
>
|
||||
> ````markdown
|
||||
> ```python
|
||||
> ### This is a title
|
||||
> import spacy
|
||||
> ```
|
||||
> ````
|
||||
>
|
||||
> #### JSX
|
||||
>
|
||||
> ```jsx
|
||||
> <CodeBlock title="This is a title" lang="python">
|
||||
> import spacy
|
||||
> </CodeBlock>
|
||||
> ```
|
||||
|
||||
Code blocks use the [Prism](http://prismjs.com/) syntax highlighter with a
|
||||
custom theme. The language can be set individually on each block, and defaults
|
||||
to raw text with no highlighting. An optional label can be added as the first
|
||||
line with the prefix `####` (Python-like) and `///` (JavaScript-like). the
|
||||
indented block as plain text and preserve whitespace.
|
||||
|
||||
```python {title="Using spaCy"}
|
||||
import spacy
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
doc = nlp("This is a sentence.")
|
||||
for token in doc:
|
||||
print(token.text, token.pos_)
|
||||
```
|
||||
|
||||
Code blocks and also specify an optional range of line numbers to highlight by
|
||||
adding `{highlight="..."}` to the headline. Acceptable ranges are spans like
|
||||
`5-7`, but also `5-7,10` or `5-7,10,13-14`.
|
||||
|
||||
> #### Markdown
|
||||
>
|
||||
> ````markdown
|
||||
> ```python
|
||||
> ### This is a title {highlight="1-2"}
|
||||
> import spacy
|
||||
> nlp = spacy.load("en_core_web_sm")
|
||||
> ```
|
||||
> ````
|
||||
|
||||
```python {title="Using the matcher",highlight="5-7"}
|
||||
import spacy
|
||||
from spacy.matcher import Matcher
|
||||
|
||||
nlp = spacy.load('en_core_web_sm')
|
||||
matcher = Matcher(nlp.vocab)
|
||||
pattern = [{"LOWER": "hello"}, {"IS_PUNCT": True}, {"LOWER": "world"}]
|
||||
matcher.add("HelloWorld", None, pattern)
|
||||
doc = nlp("Hello, world! Hello world!")
|
||||
matches = matcher(doc)
|
||||
```
|
||||
|
||||
Adding `{executable="true"}` to the title turns the code into an executable
|
||||
block, powered by [Binder](https://mybinder.org) and
|
||||
[Juniper](https://github.com/ines/juniper). If JavaScript is disabled, the
|
||||
interactive widget defaults to a regular code block.
|
||||
|
||||
> #### Markdown
|
||||
>
|
||||
> ````markdown
|
||||
> ```python
|
||||
> ### {executable="true"}
|
||||
> import spacy
|
||||
> nlp = spacy.load("en_core_web_sm")
|
||||
> ```
|
||||
> ````
|
||||
|
||||
```python {executable="true"}
|
||||
import spacy
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
doc = nlp("This is a sentence.")
|
||||
for token in doc:
|
||||
print(token.text, token.pos_)
|
||||
```
|
||||
|
||||
If a code block only contains a URL to a GitHub file, the raw file contents are
|
||||
embedded automatically and syntax highlighting is applied. The link to the
|
||||
original file is shown at the top of the widget.
|
||||
|
||||
> #### Markdown
|
||||
>
|
||||
> ````markdown
|
||||
> ```python
|
||||
> https://github.com/...
|
||||
> ```
|
||||
> ````
|
||||
>
|
||||
> #### JSX
|
||||
>
|
||||
> ```jsx
|
||||
> <GitHubCode url="https://github.com/..." lang="python" />
|
||||
> ```
|
||||
|
||||
```python
|
||||
https://github.com/explosion/spaCy/tree/master/spacy/language.py
|
||||
```
|
||||
|
||||
### Infobox {id="infobox"}
|
||||
|
||||
> #### JSX
|
||||
>
|
||||
> ```jsx
|
||||
> <Infobox title="Information">Regular infobox</Infobox>
|
||||
> <Infobox title="Important note" variant="warning">This is a warning.</Infobox>
|
||||
> <Infobox title="Be careful!" variant="danger">This is dangerous.</Infobox>
|
||||
> ```
|
||||
|
||||
Infoboxes can be used to add notes, updates, warnings or additional information
|
||||
to a page or section. Semantically, they're implemented and interpreted as an
|
||||
`aside` element. Infoboxes can take an optional `title` argument, as well as an
|
||||
optional `variant` (either `"warning"` or `"danger"`).
|
||||
|
||||
<Infobox title="This is an infobox">
|
||||
|
||||
If needed, an infobox can contain regular text, `inline code`, lists and other
|
||||
blocks.
|
||||
|
||||
</Infobox>
|
||||
|
||||
<Infobox title="This is a warning" variant="warning">
|
||||
|
||||
If needed, an infobox can contain regular text, `inline code`, lists and other
|
||||
blocks.
|
||||
|
||||
</Infobox>
|
||||
|
||||
<Infobox title="This is dangerous" variant="danger">
|
||||
|
||||
If needed, an infobox can contain regular text, `inline code`, lists and other
|
||||
blocks.
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Accordion {id="accordion"}
|
||||
|
||||
> #### JSX
|
||||
>
|
||||
> ```jsx
|
||||
> <Accordion title="This is an accordion">
|
||||
> Accordion content goes here.
|
||||
> </Accordion>
|
||||
> ```
|
||||
|
||||
Accordions are collapsible sections that are mostly used for lengthy tables,
|
||||
like the tag and label annotation schemes for different languages. They all need
|
||||
to be presented – but chances are the user doesn't actually care about _all_ of
|
||||
them, especially not at the same time. So it's fairly reasonable to hide them
|
||||
begin a click. This particular implementation was inspired by the amazing
|
||||
[Inclusive Components blog](https://inclusive-components.design/collapsible-sections/).
|
||||
|
||||
<Accordion title="This is an accordion">
|
||||
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque enim ante,
|
||||
pretium a orci eget, varius dignissim augue. Nam eu dictum mauris, id tincidunt
|
||||
nisi. Integer commodo pellentesque tincidunt. Nam at turpis finibus tortor
|
||||
gravida sodales tincidunt sit amet est. Nullam euismod arcu in tortor auctor,
|
||||
sit amet dignissim justo congue.
|
||||
|
||||
</Accordion>
|
||||
|
||||
## Markdown reference {id="markdown"}
|
||||
|
||||
All page content and page meta lives in the `.mdx` files in the `/docs`
|
||||
directory. The frontmatter block at the top of each file defines the page title
|
||||
and other settings like the sidebar menu.
|
||||
|
||||
````markdown
|
||||
---
|
||||
title: Page title
|
||||
---
|
||||
|
||||
## Headline starting a section {id="some_id"}
|
||||
|
||||
This is a regular paragraph with a [link](https://spacy.io) and **bold text**.
|
||||
|
||||
> #### This is an aside title
|
||||
>
|
||||
> This is aside text.
|
||||
|
||||
### Subheadline
|
||||
|
||||
| Header 1 | Header 2 |
|
||||
| -------- | -------- |
|
||||
| Column 1 | Column 2 |
|
||||
|
||||
```python {title="Code block title",highlight="2-3"}
|
||||
import spacy
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
doc = nlp("Hello world")
|
||||
```
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
This is content in the infobox.
|
||||
|
||||
</Infobox>
|
||||
````
|
||||
|
||||
In addition to the native markdown elements, you can use the components
|
||||
[`<Infobox />`][infobox], [`<Accordion />`][accordion], [`<Abbr />`][abbr] and
|
||||
[`<Tag />`][tag] via their JSX syntax.
|
||||
|
||||
[infobox]: https://spacy.io/styleguide#infobox
|
||||
[accordion]: https://spacy.io/styleguide#accordion
|
||||
[abbr]: https://spacy.io/styleguide#abbr
|
||||
[tag]: https://spacy.io/styleguide#tag
|
||||
|
||||
## Editorial {id="editorial"}
|
||||
|
||||
- "spaCy" should always be spelled with a lowercase "s" and a capital "C",
|
||||
unless it specifically refers to the Python package or Python import `spacy`
|
||||
(in which case it should be formatted as code).
|
||||
- ✅ spaCy is a library for advanced NLP in Python.
|
||||
- ❌ Spacy is a library for advanced NLP in Python.
|
||||
- ✅ First, you need to install the `spacy` package from pip.
|
||||
- Mentions of code, like function names, classes, variable names etc. in inline
|
||||
text should be formatted as `code`.
|
||||
- ✅ "Calling the `nlp` object on a text returns a `Doc`."
|
||||
- Objects that have pages in the [API docs](/api) should be linked – for
|
||||
example, [`Doc`](/api/doc) or [`Language.to_disk`](/api/language#to_disk). The
|
||||
mentions should still be formatted as code within the link. Links pointing to
|
||||
the API docs will automatically receive a little icon. However, if a paragraph
|
||||
includes many references to the API, the links can easily get messy. In that
|
||||
case, we typically only link the first mention of an object and not any
|
||||
subsequent ones.
|
||||
- ✅ The [`Span`](/api/span) and [`Token`](/api/token) objects are views of a
|
||||
[`Doc`](/api/doc). [`Span.as_doc`](/api/span#as_doc) creates a `Doc` object
|
||||
from a `Span`.
|
||||
- ❌ The [`Span`](/api/span) and [`Token`](/api/token) objects are views of a
|
||||
[`Doc`](/api/doc). [`Span.as_doc`](/api/span#as_doc) creates a
|
||||
[`Doc`](/api/doc) object from a [`Span`](/api/span).
|
||||
- Other things we format as code are: references to trained pipeline packages
|
||||
like `en_core_web_sm` or file names like `code.py` or `meta.json`.
|
||||
- ✅ After training, the `config.cfg` is saved to disk.
|
||||
- [Type annotations](#type-annotations) are a special type of code formatting,
|
||||
expressed by wrapping the text in `~~` instead of backticks. The result looks
|
||||
like this: ~~List[Doc]~~. All references to known types will be linked
|
||||
automatically.
|
||||
- ✅ The model has the input type ~~List[Doc]~~ and it outputs a
|
||||
~~List[Array2d]~~.
|
||||
- We try to keep links meaningful but short.
|
||||
- ✅ For details, see the usage guide on
|
||||
[training with custom code](/usage/training#custom-code).
|
||||
- ❌ For details, see
|
||||
[the usage guide on training with custom code](/usage/training#custom-code).
|
||||
- ❌ For details, see the usage guide on training with custom code
|
||||
[here](/usage/training#custom-code).
|
||||
@@ -0,0 +1,90 @@
|
||||
The central data structures in spaCy are the [`Language`](/api/language) class,
|
||||
the [`Vocab`](/api/vocab) and the [`Doc`](/api/doc) object. The `Language` class
|
||||
is used to process a text and turn it into a `Doc` object. It's typically stored
|
||||
as a variable called `nlp`. The `Doc` object owns the **sequence of tokens** and
|
||||
all their annotations. By centralizing strings, word vectors and lexical
|
||||
attributes in the `Vocab`, we avoid storing multiple copies of this data. This
|
||||
saves memory, and ensures there's a **single source of truth**.
|
||||
|
||||
Text annotations are also designed to allow a single source of truth: the `Doc`
|
||||
object owns the data, and [`Span`](/api/span) and [`Token`](/api/token) are
|
||||
**views that point into it**. The `Doc` object is constructed by the
|
||||
[`Tokenizer`](/api/tokenizer), and then **modified in place** by the components
|
||||
of the pipeline. The `Language` object coordinates these components. It takes
|
||||
raw text and sends it through the pipeline, returning an **annotated document**.
|
||||
It also orchestrates training and serialization.
|
||||
|
||||

|
||||
|
||||
### Container objects {id="architecture-containers"}
|
||||
|
||||
| Name | Description |
|
||||
| ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| [`Doc`](/api/doc) | A container for accessing linguistic annotations. |
|
||||
| [`DocBin`](/api/docbin) | A collection of `Doc` objects for efficient binary serialization. Also used for [training data](/api/data-formats#binary-training). |
|
||||
| [`Example`](/api/example) | A collection of training annotations, containing two `Doc` objects: the reference data and the predictions. |
|
||||
| [`Language`](/api/language) | Processing class that turns text into `Doc` objects. Different languages implement their own subclasses of it. The variable is typically called `nlp`. |
|
||||
| [`Lexeme`](/api/lexeme) | An entry in the vocabulary. It's a word type with no context, as opposed to a word token. It therefore has no part-of-speech tag, dependency parse etc. |
|
||||
| [`Span`](/api/span) | A slice from a `Doc` object. |
|
||||
| [`SpanGroup`](/api/spangroup) | A named collection of spans belonging to a `Doc`. |
|
||||
| [`Token`](/api/token) | An individual token — i.e. a word, punctuation symbol, whitespace, etc. |
|
||||
|
||||
### Processing pipeline {id="architecture-pipeline"}
|
||||
|
||||
The processing pipeline consists of one or more **pipeline components** that are
|
||||
called on the `Doc` in order. The tokenizer runs before the components. Pipeline
|
||||
components can be added using [`Language.add_pipe`](/api/language#add_pipe).
|
||||
They can contain a statistical model and trained weights, or only make
|
||||
rule-based modifications to the `Doc`. spaCy provides a range of built-in
|
||||
components for different language processing tasks and also allows adding
|
||||
[custom components](/usage/processing-pipelines#custom-components).
|
||||
|
||||

|
||||
|
||||
| Name | Description |
|
||||
| ----------------------------------------------- | ------------------------------------------------------------------------------------------- |
|
||||
| [`AttributeRuler`](/api/attributeruler) | Set token attributes using matcher rules. |
|
||||
| [`DependencyParser`](/api/dependencyparser) | Predict syntactic dependencies. |
|
||||
| [`EditTreeLemmatizer`](/api/edittreelemmatizer) | Predict base forms of words. |
|
||||
| [`EntityLinker`](/api/entitylinker) | Disambiguate named entities to nodes in a knowledge base. |
|
||||
| [`EntityRecognizer`](/api/entityrecognizer) | Predict named entities, e.g. persons or products. |
|
||||
| [`EntityRuler`](/api/entityruler) | Add entity spans to the `Doc` using token-based rules or exact phrase matches. |
|
||||
| [`Lemmatizer`](/api/lemmatizer) | Determine the base forms of words using rules and lookups. |
|
||||
| [`Morphologizer`](/api/morphologizer) | Predict morphological features and coarse-grained part-of-speech tags. |
|
||||
| [`SentenceRecognizer`](/api/sentencerecognizer) | Predict sentence boundaries. |
|
||||
| [`Sentencizer`](/api/sentencizer) | Implement rule-based sentence boundary detection that doesn't require the dependency parse. |
|
||||
| [`Tagger`](/api/tagger) | Predict part-of-speech tags. |
|
||||
| [`TextCategorizer`](/api/textcategorizer) | Predict categories or labels over the whole document. |
|
||||
| [`Tok2Vec`](/api/tok2vec) | Apply a "token-to-vector" model and set its outputs. |
|
||||
| [`Tokenizer`](/api/tokenizer) | Segment raw text and create `Doc` objects from the words. |
|
||||
| [`TrainablePipe`](/api/pipe) | Class that all trainable pipeline components inherit from. |
|
||||
| [`Transformer`](/api/transformer) | Use a transformer model and set its outputs. |
|
||||
| [Other functions](/api/pipeline-functions) | Automatically apply something to the `Doc`, e.g. to merge spans of tokens. |
|
||||
|
||||
### Matchers {id="architecture-matchers"}
|
||||
|
||||
Matchers help you find and extract information from [`Doc`](/api/doc) objects
|
||||
based on match patterns describing the sequences you're looking for. A matcher
|
||||
operates on a `Doc` and gives you access to the matched tokens **in context**.
|
||||
|
||||
| Name | Description |
|
||||
| --------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| [`DependencyMatcher`](/api/dependencymatcher) | Match sequences of tokens based on dependency trees using [Semgrex operators](https://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/semgraph/semgrex/SemgrexPattern.html). |
|
||||
| [`Matcher`](/api/matcher) | Match sequences of tokens, based on pattern rules, similar to regular expressions. |
|
||||
| [`PhraseMatcher`](/api/phrasematcher) | Match sequences of tokens based on phrases. |
|
||||
|
||||
### Other classes {id="architecture-other"}
|
||||
|
||||
| Name | Description |
|
||||
| ------------------------------------------------ | -------------------------------------------------------------------------------------------------- |
|
||||
| [`Corpus`](/api/corpus) | Class for managing annotated corpora for training and evaluation data. |
|
||||
| [`KnowledgeBase`](/api/kb) | Abstract base class for storage and retrieval of data for entity linking. |
|
||||
| [`InMemoryLookupKB`](/api/inmemorylookupkb) | Implementation of `KnowledgeBase` storing all data in memory. |
|
||||
| [`Candidate`](/api/kb#candidate) | Object associating a textual mention with a specific entity contained in a `KnowledgeBase`. |
|
||||
| [`Lookups`](/api/lookups) | Container for convenient access to large lookup tables and dictionaries. |
|
||||
| [`MorphAnalysis`](/api/morphology#morphanalysis) | A morphological analysis. |
|
||||
| [`Morphology`](/api/morphology) | Store morphological analyses and map them to and from hash values. |
|
||||
| [`Scorer`](/api/scorer) | Compute evaluation scores. |
|
||||
| [`StringStore`](/api/stringstore) | Map strings to and from hash values. |
|
||||
| [`Vectors`](/api/vectors) | Container class for vector data keyed by string. |
|
||||
| [`Vocab`](/api/vocab) | The shared vocabulary that stores strings and gives you access to [`Lexeme`](/api/lexeme) objects. |
|
||||
@@ -0,0 +1,32 @@
|
||||
Every language is different – and usually full of **exceptions and special
|
||||
cases**, especially amongst the most common words. Some of these exceptions are
|
||||
shared across languages, while others are **entirely specific** – usually so
|
||||
specific that they need to be hard-coded. The
|
||||
[`lang`](%%GITHUB_SPACY/spacy/lang) module contains all language-specific data,
|
||||
organized in simple Python files. This makes the data easy to update and extend.
|
||||
|
||||
The **shared language data** in the directory root includes rules that can be
|
||||
generalized across languages – for example, rules for basic punctuation, emoji,
|
||||
emoticons and single-letter abbreviations. The **individual language data** in a
|
||||
submodule contains rules that are only relevant to a particular language. It
|
||||
also takes care of putting together all components and creating the
|
||||
[`Language`](/api/language) subclass – for example, `English` or `German`. The
|
||||
values are defined in the [`Language.Defaults`](/api/language#defaults).
|
||||
|
||||
> ```python
|
||||
> from spacy.lang.en import English
|
||||
> from spacy.lang.de import German
|
||||
>
|
||||
> nlp_en = English() # Includes English data
|
||||
> nlp_de = German() # Includes German data
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| --------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| **Stop words**<br />[`stop_words.py`](%%GITHUB_SPACY/spacy/lang/en/stop_words.py) | List of most common words of a language that are often useful to filter out, for example "and" or "I". Matching tokens will return `True` for `is_stop`. |
|
||||
| **Tokenizer exceptions**<br />[`tokenizer_exceptions.py`](%%GITHUB_SPACY/spacy/lang/de/tokenizer_exceptions.py) | Special-case rules for the tokenizer, for example, contractions like "can't" and abbreviations with punctuation, like "U.K.". |
|
||||
| **Punctuation rules**<br />[`punctuation.py`](%%GITHUB_SPACY/spacy/lang/punctuation.py) | Regular expressions for splitting tokens, e.g. on punctuation or special characters like emoji. Includes rules for prefixes, suffixes and infixes. |
|
||||
| **Character classes**<br />[`char_classes.py`](%%GITHUB_SPACY/spacy/lang/char_classes.py) | Character classes to be used in regular expressions, for example, Latin characters, quotes, hyphens or icons. |
|
||||
| **Lexical attributes**<br />[`lex_attrs.py`](%%GITHUB_SPACY/spacy/lang/en/lex_attrs.py) | Custom functions for setting lexical attributes on tokens, e.g. `like_num`, which includes language-specific words like "ten" or "hundred". |
|
||||
| **Syntax iterators**<br />[`syntax_iterators.py`](%%GITHUB_SPACY/spacy/lang/en/syntax_iterators.py) | Functions that compute views of a `Doc` object based on its syntax. At the moment, only used for [noun chunks](/usage/linguistic-features#noun-chunks). |
|
||||
| **Lemmatizer**<br />[`lemmatizer.py`](%%GITHUB_SPACY/spacy/lang/fr/lemmatizer.py) [`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data) | Custom lemmatizer implementation and lemmatization tables. |
|
||||
@@ -0,0 +1,36 @@
|
||||
A named entity is a "real-world object" that's assigned a name – for example, a
|
||||
person, a country, a product or a book title. spaCy can **recognize various
|
||||
types of named entities in a document, by asking the model for a prediction**.
|
||||
Because models are statistical and strongly depend on the examples they were
|
||||
trained on, this doesn't always work _perfectly_ and might need some tuning
|
||||
later, depending on your use case.
|
||||
|
||||
Named entities are available as the `ents` property of a `Doc`:
|
||||
|
||||
```python {executable="true"}
|
||||
import spacy
|
||||
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
doc = nlp("Apple is looking at buying U.K. startup for $1 billion")
|
||||
|
||||
for ent in doc.ents:
|
||||
print(ent.text, ent.start_char, ent.end_char, ent.label_)
|
||||
```
|
||||
|
||||
> - **Text:** The original entity text.
|
||||
> - **Start:** Index of start of entity in the `Doc`.
|
||||
> - **End:** Index of end of entity in the `Doc`.
|
||||
> - **Label:** Entity label, i.e. type.
|
||||
|
||||
| Text | Start | End | Label | Description |
|
||||
| ----------- | :---: | :-: | ------- | ---------------------------------------------------- |
|
||||
| Apple | 0 | 5 | `ORG` | Companies, agencies, institutions. |
|
||||
| U.K. | 27 | 31 | `GPE` | Geopolitical entity, i.e. countries, cities, states. |
|
||||
| \$1 billion | 44 | 54 | `MONEY` | Monetary values, including unit. |
|
||||
|
||||
Using spaCy's built-in [displaCy visualizer](/usage/visualizers), here's what
|
||||
our example sentence and its named entities look like:
|
||||
|
||||
<Standalone height={120}>
|
||||
<div style={{lineHeight: 2.5, fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'", fontSize: 18}}><mark style={{ background: '#7aecec', padding: '0.45em 0.6em', margin: '0 0.25em', lineHeight: 1, borderRadius: '0.35em'}}>Apple <span style={{ fontSize: '0.8em', fontWeight: 'bold', lineHeight: 1, borderRadius: '0.35em', marginLeft: '0.5rem'}}>ORG</span></mark> is looking at buying <mark style={{ background: '#feca74', padding: '0.45em 0.6em', margin: '0 0.25em', lineHeight: 1, borderRadius: '0.35em'}}>U.K. <span style={{ fontSize: '0.8em', fontWeight: 'bold', lineHeight: 1, borderRadius: '0.35em', marginLeft: '0.5rem'}}>GPE</span></mark> startup for <mark style={{ background: '#e4e7d2', padding: '0.45em 0.6em', margin: '0 0.25em', lineHeight: 1, borderRadius: '0.35em'}}>$1 billion <span style={{ fontSize: '0.8em', fontWeight: 'bold', lineHeight: 1, borderRadius: '0.35em', marginLeft: '0.5rem'}}>MONEY</span></mark></div>
|
||||
</Standalone>
|
||||
@@ -0,0 +1,77 @@
|
||||
When you call `nlp` on a text, spaCy first tokenizes the text to produce a `Doc`
|
||||
object. The `Doc` is then processed in several different steps – this is also
|
||||
referred to as the **processing pipeline**. The pipeline used by the
|
||||
[trained pipelines](/models) typically include a tagger, a lemmatizer, a parser
|
||||
and an entity recognizer. Each pipeline component returns the processed `Doc`,
|
||||
which is then passed on to the next component.
|
||||
|
||||

|
||||
|
||||
> - **Name**: ID of the pipeline component.
|
||||
> - **Component:** spaCy's implementation of the component.
|
||||
> - **Creates:** Objects, attributes and properties modified and set by the
|
||||
> component.
|
||||
|
||||
| Name | Component | Creates | Description |
|
||||
| --------------------- | ------------------------------------------------------------------ | --------------------------------------------------------- | ------------------------------------------------ |
|
||||
| **tokenizer** | [`Tokenizer`](/api/tokenizer) | `Doc` | Segment text into tokens. |
|
||||
| _processing pipeline_ | | |
|
||||
| **tagger** | [`Tagger`](/api/tagger) | `Token.tag` | Assign part-of-speech tags. |
|
||||
| **parser** | [`DependencyParser`](/api/dependencyparser) | `Token.head`, `Token.dep`, `Doc.sents`, `Doc.noun_chunks` | Assign dependency labels. |
|
||||
| **ner** | [`EntityRecognizer`](/api/entityrecognizer) | `Doc.ents`, `Token.ent_iob`, `Token.ent_type` | Detect and label named entities. |
|
||||
| **lemmatizer** | [`Lemmatizer`](/api/lemmatizer) | `Token.lemma` | Assign base forms. |
|
||||
| **textcat** | [`TextCategorizer`](/api/textcategorizer) | `Doc.cats` | Assign document labels. |
|
||||
| **custom** | [custom components](/usage/processing-pipelines#custom-components) | `Doc._.xxx`, `Token._.xxx`, `Span._.xxx` | Assign custom attributes, methods or properties. |
|
||||
|
||||
The capabilities of a processing pipeline always depend on the components, their
|
||||
models and how they were trained. For example, a pipeline for named entity
|
||||
recognition needs to include a trained named entity recognizer component with a
|
||||
statistical model and weights that enable it to **make predictions** of entity
|
||||
labels. This is why each pipeline specifies its components and their settings in
|
||||
the [config](/usage/training#config):
|
||||
|
||||
```ini
|
||||
[nlp]
|
||||
pipeline = ["tok2vec", "tagger", "parser", "ner"]
|
||||
```
|
||||
|
||||
<Accordion title="Does the order of pipeline components matter?" id="pipeline-components-order">
|
||||
|
||||
The statistical components like the tagger or parser are typically independent
|
||||
and don't share any data between each other. For example, the named entity
|
||||
recognizer doesn't use any features set by the tagger and parser, and so on.
|
||||
This means that you can swap them, or remove single components from the pipeline
|
||||
without affecting the others. However, components may share a "token-to-vector"
|
||||
component like [`Tok2Vec`](/api/tok2vec) or [`Transformer`](/api/transformer).
|
||||
You can read more about this in the docs on
|
||||
[embedding layers](/usage/embeddings-transformers#embedding-layers).
|
||||
|
||||
Custom components may also depend on annotations set by other components. For
|
||||
example, a custom lemmatizer may need the part-of-speech tags assigned, so it'll
|
||||
only work if it's added after the tagger. The parser will respect pre-defined
|
||||
sentence boundaries, so if a previous component in the pipeline sets them, its
|
||||
dependency predictions may be different. Similarly, it matters if you add the
|
||||
[`EntityRuler`](/api/entityruler) before or after the statistical entity
|
||||
recognizer: if it's added before, the entity recognizer will take the existing
|
||||
entities into account when making predictions. The
|
||||
[`EntityLinker`](/api/entitylinker), which resolves named entities to knowledge
|
||||
base IDs, should be preceded by a pipeline component that recognizes entities
|
||||
such as the [`EntityRecognizer`](/api/entityrecognizer).
|
||||
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="Why is the tokenizer special?" id="pipeline-components-tokenizer">
|
||||
|
||||
The tokenizer is a "special" component and isn't part of the regular pipeline.
|
||||
It also doesn't show up in `nlp.pipe_names`. The reason is that there can only
|
||||
really be one tokenizer, and while all other pipeline components take a `Doc`
|
||||
and return it, the tokenizer takes a **string of text** and turns it into a
|
||||
`Doc`. You can still customize the tokenizer, though. `nlp.tokenizer` is
|
||||
writable, so you can either create your own
|
||||
[`Tokenizer` class from scratch](/usage/linguistic-features#native-tokenizers),
|
||||
or even replace it with an
|
||||
[entirely custom function](/usage/linguistic-features#custom-tokenizer).
|
||||
|
||||
</Accordion>
|
||||
|
||||
---
|
||||
@@ -0,0 +1,62 @@
|
||||
After tokenization, spaCy can **parse** and **tag** a given `Doc`. This is where
|
||||
the trained pipeline and its statistical models come in, which enable spaCy to
|
||||
**make predictions** of which tag or label most likely applies in this context.
|
||||
A trained component includes binary data that is produced by showing a system
|
||||
enough examples for it to make predictions that generalize across the language –
|
||||
for example, a word following "the" in English is most likely a noun.
|
||||
|
||||
Linguistic annotations are available as
|
||||
[`Token` attributes](/api/token#attributes). Like many NLP libraries, spaCy
|
||||
**encodes all strings to hash values** to reduce memory usage and improve
|
||||
efficiency. So to get the readable string representation of an attribute, we
|
||||
need to add an underscore `_` to its name:
|
||||
|
||||
```python {executable="true"}
|
||||
import spacy
|
||||
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
doc = nlp("Apple is looking at buying U.K. startup for $1 billion")
|
||||
|
||||
for token in doc:
|
||||
print(token.text, token.lemma_, token.pos_, token.tag_, token.dep_,
|
||||
token.shape_, token.is_alpha, token.is_stop)
|
||||
```
|
||||
|
||||
> - **Text:** The original word text.
|
||||
> - **Lemma:** The base form of the word.
|
||||
> - **POS:** The simple [UPOS](https://universaldependencies.org/u/pos/)
|
||||
> part-of-speech tag.
|
||||
> - **Tag:** The detailed part-of-speech tag.
|
||||
> - **Dep:** Syntactic dependency, i.e. the relation between tokens.
|
||||
> - **Shape:** The word shape – capitalization, punctuation, digits.
|
||||
> - **is alpha:** Is the token an alpha character?
|
||||
> - **is stop:** Is the token part of a stop list, i.e. the most common words of
|
||||
> the language?
|
||||
|
||||
| Text | Lemma | POS | Tag | Dep | Shape | alpha | stop |
|
||||
| ------- | ------- | ------- | ----- | ---------- | ------- | ------- | ------- |
|
||||
| Apple | apple | `PROPN` | `NNP` | `nsubj` | `Xxxxx` | `True` | `False` |
|
||||
| is | be | `AUX` | `VBZ` | `aux` | `xx` | `True` | `True` |
|
||||
| looking | look | `VERB` | `VBG` | `ROOT` | `xxxx` | `True` | `False` |
|
||||
| at | at | `ADP` | `IN` | `prep` | `xx` | `True` | `True` |
|
||||
| buying | buy | `VERB` | `VBG` | `pcomp` | `xxxx` | `True` | `False` |
|
||||
| U.K. | u.k. | `PROPN` | `NNP` | `compound` | `X.X.` | `False` | `False` |
|
||||
| startup | startup | `NOUN` | `NN` | `dobj` | `xxxx` | `True` | `False` |
|
||||
| for | for | `ADP` | `IN` | `prep` | `xxx` | `True` | `True` |
|
||||
| \$ | \$ | `SYM` | `$` | `quantmod` | `$` | `False` | `False` |
|
||||
| 1 | 1 | `NUM` | `CD` | `compound` | `d` | `False` | `False` |
|
||||
| billion | billion | `NUM` | `CD` | `pobj` | `xxxx` | `True` | `False` |
|
||||
|
||||
> #### Tip: Understanding tags and labels
|
||||
>
|
||||
> Most of the tags and labels look pretty abstract, and they vary between
|
||||
> languages. `spacy.explain` will show you a short description – for example,
|
||||
> `spacy.explain("VBZ")` returns "verb, 3rd person singular present".
|
||||
|
||||
Using spaCy's built-in [displaCy visualizer](/usage/visualizers), here's what
|
||||
our example sentence and its dependencies look like:
|
||||
|
||||
<ImageScrollable
|
||||
src="/images/displacy-long.svg"
|
||||
width={1975}
|
||||
/>
|
||||
@@ -0,0 +1,29 @@
|
||||
If you've been modifying the pipeline, vocabulary, vectors and entities, or made
|
||||
updates to the component models, you'll eventually want to **save your
|
||||
progress** – for example, everything that's in your `nlp` object. This means
|
||||
you'll have to translate its contents and structure into a format that can be
|
||||
saved, like a file or a byte string. This process is called serialization. spaCy
|
||||
comes with **built-in serialization methods** and supports the
|
||||
[Pickle protocol](https://www.diveinto.org/python3/serializing.html#dump).
|
||||
|
||||
> #### What's pickle?
|
||||
>
|
||||
> Pickle is Python's built-in object persistence system. It lets you transfer
|
||||
> arbitrary Python objects between processes. This is usually used to load an
|
||||
> object to and from disk, but it's also used for distributed computing, e.g.
|
||||
> with
|
||||
> [PySpark](https://spark.apache.org/docs/0.9.0/python-programming-guide.html)
|
||||
> or [Dask](https://dask.org). When you unpickle an object, you're agreeing to
|
||||
> execute whatever code it contains. It's like calling `eval()` on a string – so
|
||||
> don't unpickle objects from untrusted sources.
|
||||
|
||||
All container classes, i.e. [`Language`](/api/language) (`nlp`),
|
||||
[`Doc`](/api/doc), [`Vocab`](/api/vocab) and [`StringStore`](/api/stringstore)
|
||||
have the following methods available:
|
||||
|
||||
| Method | Returns | Example |
|
||||
| ------------ | ------- | ------------------------ |
|
||||
| `to_bytes` | bytes | `data = nlp.to_bytes()` |
|
||||
| `from_bytes` | object | `nlp.from_bytes(data)` |
|
||||
| `to_disk` | - | `nlp.to_disk("/path")` |
|
||||
| `from_disk` | object | `nlp.from_disk("/path")` |
|
||||
@@ -0,0 +1,49 @@
|
||||
During processing, spaCy first **tokenizes** the text, i.e. segments it into
|
||||
words, punctuation and so on. This is done by applying rules specific to each
|
||||
language. For example, punctuation at the end of a sentence should be split off
|
||||
– whereas "U.K." should remain one token. Each `Doc` consists of individual
|
||||
tokens, and we can iterate over them:
|
||||
|
||||
```python {executable="true"}
|
||||
import spacy
|
||||
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
doc = nlp("Apple is looking at buying U.K. startup for $1 billion")
|
||||
for token in doc:
|
||||
print(token.text)
|
||||
```
|
||||
|
||||
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|
||||
| :---: | :-: | :-----: | :-: | :----: | :--: | :-----: | :-: | :-: | :-: | :-----: |
|
||||
| Apple | is | looking | at | buying | U.K. | startup | for | \$ | 1 | billion |
|
||||
|
||||
First, the raw text is split on whitespace characters, similar to
|
||||
`text.split(' ')`. Then, the tokenizer processes the text from left to right. On
|
||||
each substring, it performs two checks:
|
||||
|
||||
1. **Does the substring match a tokenizer exception rule?** For example, "don't"
|
||||
does not contain whitespace, but should be split into two tokens, "do" and
|
||||
"n't", while "U.K." should always remain one token.
|
||||
|
||||
2. **Can a prefix, suffix or infix be split off?** For example punctuation like
|
||||
commas, periods, hyphens or quotes.
|
||||
|
||||
If there's a match, the rule is applied and the tokenizer continues its loop,
|
||||
starting with the newly split substrings. This way, spaCy can split **complex,
|
||||
nested tokens** like combinations of abbreviations and multiple punctuation
|
||||
marks.
|
||||
|
||||
> - **Tokenizer exception:** Special-case rule to split a string into several
|
||||
> tokens or prevent a token from being split when punctuation rules are
|
||||
> applied.
|
||||
> - **Prefix:** Character(s) at the beginning, e.g. `$`, `(`, `“`, `¿`.
|
||||
> - **Suffix:** Character(s) at the end, e.g. `km`, `)`, `”`, `!`.
|
||||
> - **Infix:** Character(s) in between, e.g. `-`, `--`, `/`, `…`.
|
||||
|
||||

|
||||
|
||||
While punctuation rules are usually pretty general, tokenizer exceptions
|
||||
strongly depend on the specifics of the individual language. This is why each
|
||||
[available language](/usage/models#languages) has its own subclass, like
|
||||
`English` or `German`, that loads in lists of hard-coded data and exception
|
||||
rules.
|
||||
@@ -0,0 +1,41 @@
|
||||
spaCy's tagger, parser, text categorizer and many other components are powered
|
||||
by **statistical models**. Every "decision" these components make – for example,
|
||||
which part-of-speech tag to assign, or whether a word is a named entity – is a
|
||||
**prediction** based on the model's current **weight values**. The weight values
|
||||
are estimated based on examples the model has seen during **training**. To train
|
||||
a model, you first need training data – examples of text, and the labels you
|
||||
want the model to predict. This could be a part-of-speech tag, a named entity or
|
||||
any other information.
|
||||
|
||||
Training is an iterative process in which the model's predictions are compared
|
||||
against the reference annotations in order to estimate the **gradient of the
|
||||
loss**. The gradient of the loss is then used to calculate the gradient of the
|
||||
weights through [backpropagation](https://thinc.ai/docs/backprop101). The
|
||||
gradients indicate how the weight values should be changed so that the model's
|
||||
predictions become more similar to the reference labels over time.
|
||||
|
||||
> - **Training data:** Examples and their annotations.
|
||||
> - **Text:** The input text the model should predict a label for.
|
||||
> - **Label:** The label the model should predict.
|
||||
> - **Gradient:** The direction and rate of change for a numeric value.
|
||||
> Minimising the gradient of the weights should result in predictions that are
|
||||
> closer to the reference labels on the training data.
|
||||
|
||||

|
||||
|
||||
When training a model, we don't just want it to memorize our examples – we want
|
||||
it to come up with a theory that can be **generalized across unseen data**.
|
||||
After all, we don't just want the model to learn that this one instance of
|
||||
"Amazon" right here is a company – we want it to learn that "Amazon", in
|
||||
contexts _like this_, is most likely a company. That's why the training data
|
||||
should always be representative of the data we want to process. A model trained
|
||||
on Wikipedia, where sentences in the first person are extremely rare, will
|
||||
likely perform badly on Twitter. Similarly, a model trained on romantic novels
|
||||
will likely perform badly on legal text.
|
||||
|
||||
This also means that in order to know how the model is performing, and whether
|
||||
it's learning the right things, you don't only need **training data** – you'll
|
||||
also need **evaluation data**. If you only test the model with the data it was
|
||||
trained on, you'll have no idea how well it's generalizing. If you want to train
|
||||
a model from scratch, you usually need at least a few hundred examples for both
|
||||
training and evaluation.
|
||||
@@ -0,0 +1,150 @@
|
||||
Similarity is determined by comparing **word vectors** or "word embeddings",
|
||||
multi-dimensional meaning representations of a word. Word vectors can be
|
||||
generated using an algorithm like
|
||||
[word2vec](https://en.wikipedia.org/wiki/Word2vec) and usually look like this:
|
||||
|
||||
```python {title="banana.vector"}
|
||||
array([2.02280000e-01, -7.66180009e-02, 3.70319992e-01,
|
||||
3.28450017e-02, -4.19569999e-01, 7.20689967e-02,
|
||||
-3.74760002e-01, 5.74599989e-02, -1.24009997e-02,
|
||||
5.29489994e-01, -5.23800015e-01, -1.97710007e-01,
|
||||
-3.41470003e-01, 5.33169985e-01, -2.53309999e-02,
|
||||
1.73800007e-01, 1.67720005e-01, 8.39839995e-01,
|
||||
5.51070012e-02, 1.05470002e-01, 3.78719985e-01,
|
||||
2.42750004e-01, 1.47449998e-02, 5.59509993e-01,
|
||||
1.25210002e-01, -6.75960004e-01, 3.58420014e-01,
|
||||
# ... and so on ...
|
||||
3.66849989e-01, 2.52470002e-03, -6.40089989e-01,
|
||||
-2.97650009e-01, 7.89430022e-01, 3.31680000e-01,
|
||||
-1.19659996e+00, -4.71559986e-02, 5.31750023e-01], dtype=float32)
|
||||
```
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
To make them compact and fast, spaCy's small [pipeline packages](/models) (all
|
||||
packages that end in `sm`) **don't ship with word vectors**, and only include
|
||||
context-sensitive **tensors**. This means you can still use the `similarity()`
|
||||
methods to compare documents, spans and tokens – but the result won't be as
|
||||
good, and individual tokens won't have any vectors assigned. So in order to use
|
||||
_real_ word vectors, you need to download a larger pipeline package:
|
||||
|
||||
```diff
|
||||
- python -m spacy download en_core_web_sm
|
||||
+ python -m spacy download en_core_web_lg
|
||||
```
|
||||
|
||||
</Infobox>
|
||||
|
||||
Pipeline packages that come with built-in word vectors make them available as
|
||||
the [`Token.vector`](/api/token#vector) attribute.
|
||||
[`Doc.vector`](/api/doc#vector) and [`Span.vector`](/api/span#vector) will
|
||||
default to an average of their token vectors. You can also check if a token has
|
||||
a vector assigned, and get the L2 norm, which can be used to normalize vectors.
|
||||
|
||||
```python {executable="true"}
|
||||
import spacy
|
||||
|
||||
nlp = spacy.load("en_core_web_md")
|
||||
tokens = nlp("dog cat banana afskfsd")
|
||||
|
||||
for token in tokens:
|
||||
print(token.text, token.has_vector, token.vector_norm, token.is_oov)
|
||||
```
|
||||
|
||||
> - **Text**: The original token text.
|
||||
> - **has vector**: Does the token have a vector representation?
|
||||
> - **Vector norm**: The L2 norm of the token's vector (the square root of the
|
||||
> sum of the values squared)
|
||||
> - **OOV**: Out-of-vocabulary
|
||||
|
||||
The words "dog", "cat" and "banana" are all pretty common in English, so they're
|
||||
part of the pipeline's vocabulary, and come with a vector. The word "afskfsd" on
|
||||
the other hand is a lot less common and out-of-vocabulary – so its vector
|
||||
representation consists of 300 dimensions of `0`, which means it's practically
|
||||
nonexistent. If your application will benefit from a **large vocabulary** with
|
||||
more vectors, you should consider using one of the larger pipeline packages or
|
||||
loading in a full vector package, for example,
|
||||
[`en_core_web_lg`](/models/en#en_core_web_lg), which includes **685k unique
|
||||
vectors**.
|
||||
|
||||
spaCy is able to compare two objects, and make a prediction of **how similar
|
||||
they are**. Predicting similarity is useful for building recommendation systems
|
||||
or flagging duplicates. For example, you can suggest a user content that's
|
||||
similar to what they're currently looking at, or label a support ticket as a
|
||||
duplicate if it's very similar to an already existing one.
|
||||
|
||||
Each [`Doc`](/api/doc), [`Span`](/api/span), [`Token`](/api/token) and
|
||||
[`Lexeme`](/api/lexeme) comes with a [`.similarity`](/api/token#similarity)
|
||||
method that lets you compare it with another object, and determine the
|
||||
similarity. Of course similarity is always subjective – whether two words, spans
|
||||
or documents are similar really depends on how you're looking at it. spaCy's
|
||||
similarity implementation usually assumes a pretty general-purpose definition of
|
||||
similarity.
|
||||
|
||||
> #### 📝 Things to try
|
||||
>
|
||||
> 1. Compare two different tokens and try to find the two most _dissimilar_
|
||||
> tokens in the texts with the lowest similarity score (according to the
|
||||
> vectors).
|
||||
> 2. Compare the similarity of two [`Lexeme`](/api/lexeme) objects, entries in
|
||||
> the vocabulary. You can get a lexeme via the `.lex` attribute of a token.
|
||||
> You should see that the similarity results are identical to the token
|
||||
> similarity.
|
||||
|
||||
```python {executable="true"}
|
||||
import spacy
|
||||
|
||||
nlp = spacy.load("en_core_web_md") # make sure to use larger package!
|
||||
doc1 = nlp("I like salty fries and hamburgers.")
|
||||
doc2 = nlp("Fast food tastes very good.")
|
||||
|
||||
# Similarity of two documents
|
||||
print(doc1, "<->", doc2, doc1.similarity(doc2))
|
||||
# Similarity of tokens and spans
|
||||
french_fries = doc1[2:4]
|
||||
burgers = doc1[5]
|
||||
print(french_fries, "<->", burgers, french_fries.similarity(burgers))
|
||||
```
|
||||
|
||||
### What to expect from similarity results {id="similarity-expectations"}
|
||||
|
||||
Computing similarity scores can be helpful in many situations, but it's also
|
||||
important to maintain **realistic expectations** about what information it can
|
||||
provide. Words can be related to each other in many ways, so a single
|
||||
"similarity" score will always be a **mix of different signals**, and vectors
|
||||
trained on different data can produce very different results that may not be
|
||||
useful for your purpose. Here are some important considerations to keep in mind:
|
||||
|
||||
- There's no objective definition of similarity. Whether "I like burgers" and "I
|
||||
like pasta" is similar **depends on your application**. Both talk about food
|
||||
preferences, which makes them very similar – but if you're analyzing mentions
|
||||
of food, those sentences are pretty dissimilar, because they talk about very
|
||||
different foods.
|
||||
- The similarity of [`Doc`](/api/doc) and [`Span`](/api/span) objects defaults
|
||||
to the **average** of the token vectors. This means that the vector for "fast
|
||||
food" is the average of the vectors for "fast" and "food", which isn't
|
||||
necessarily representative of the phrase "fast food".
|
||||
- Vector averaging means that the vector of multiple tokens is **insensitive to
|
||||
the order** of the words. Two documents expressing the same meaning with
|
||||
dissimilar wording will return a lower similarity score than two documents
|
||||
that happen to contain the same words while expressing different meanings.
|
||||
|
||||
<Infobox title="Tip: Check out sense2vec" emoji="💡">
|
||||
|
||||
<Image
|
||||
src="/images/sense2vec.jpg"
|
||||
href="https://github.com/explosion/sense2vec"
|
||||
alt="sense2vec Screenshot"
|
||||
/>
|
||||
|
||||
[`sense2vec`](https://github.com/explosion/sense2vec) is a library developed by
|
||||
us that builds on top of spaCy and lets you train and query more interesting and
|
||||
detailed word vectors. It combines noun phrases like "fast food" or "fair game"
|
||||
and includes the part-of-speech tags and entity labels. The library also
|
||||
includes annotation recipes for our annotation tool [Prodigy](https://prodi.gy)
|
||||
that let you evaluate vectors and create terminology lists. For more details,
|
||||
check out [our blog post](https://explosion.ai/blog/sense2vec-reloaded). To
|
||||
explore the semantic similarities across all Reddit comments of 2015 and 2019,
|
||||
see the [interactive demo](https://explosion.ai/demos/sense2vec).
|
||||
|
||||
</Infobox>
|
||||
@@ -0,0 +1,40 @@
|
||||
<figure>
|
||||
|
||||
| Pipeline | Parser | Tagger | NER |
|
||||
| ---------------------------------------------------------- | -----: | -----: | ---: |
|
||||
| [`en_core_web_trf`](/models/en#en_core_web_trf) (spaCy v3) | 95.1 | 97.8 | 89.8 |
|
||||
| [`en_core_web_lg`](/models/en#en_core_web_lg) (spaCy v3) | 92.0 | 97.4 | 85.5 |
|
||||
| `en_core_web_lg` (spaCy v2) | 91.9 | 97.2 | 85.5 |
|
||||
|
||||
<figcaption className="caption">
|
||||
|
||||
**Full pipeline accuracy** on the
|
||||
[OntoNotes 5.0](https://catalog.ldc.upenn.edu/LDC2013T19) corpus (reported on
|
||||
the development set).
|
||||
|
||||
</figcaption>
|
||||
|
||||
</figure>
|
||||
|
||||
<figure>
|
||||
|
||||
| Named Entity Recognition System | OntoNotes | CoNLL '03 |
|
||||
| -------------------------------- | --------: | --------: |
|
||||
| spaCy RoBERTa (2020) | 89.8 | 91.6 |
|
||||
| Stanza (StanfordNLP)<sup>1</sup> | 88.8 | 92.1 |
|
||||
| Flair<sup>2</sup> | 89.7 | 93.1 |
|
||||
|
||||
<figcaption className="caption">
|
||||
|
||||
**Named entity recognition accuracy** on the
|
||||
[OntoNotes 5.0](https://catalog.ldc.upenn.edu/LDC2013T19) and
|
||||
[CoNLL-2003](https://www.aclweb.org/anthology/W03-0419.pdf) corpora. See
|
||||
[NLP-progress](http://nlpprogress.com/english/named_entity_recognition.html) for
|
||||
more results. Project template:
|
||||
[`benchmarks/ner_conll03`](%%GITHUB_PROJECTS/benchmarks/ner_conll03). **1.**
|
||||
[Qi et al. (2020)](https://arxiv.org/pdf/2003.07082.pdf). **2.**
|
||||
[Akbik et al. (2018)](https://www.aclweb.org/anthology/C18-1139/).
|
||||
|
||||
</figcaption>
|
||||
|
||||
</figure>
|
||||
@@ -0,0 +1,987 @@
|
||||
---
|
||||
title: Embeddings, Transformers and Transfer Learning
|
||||
teaser: Using transformer embeddings like BERT in spaCy
|
||||
menu:
|
||||
- ['Embedding Layers', 'embedding-layers']
|
||||
- ['Transformers', 'transformers']
|
||||
- ['Static Vectors', 'static-vectors']
|
||||
- ['Pretraining', 'pretraining']
|
||||
next: /usage/training
|
||||
---
|
||||
|
||||
spaCy supports a number of **transfer and multi-task learning** workflows that
|
||||
can often help improve your pipeline's efficiency or accuracy. Transfer learning
|
||||
refers to techniques such as word vector tables and language model pretraining.
|
||||
These techniques can be used to import knowledge from raw text into your
|
||||
pipeline, so that your models are able to generalize better from your annotated
|
||||
examples.
|
||||
|
||||
You can convert **word vectors** from popular tools like
|
||||
[FastText](https://fasttext.cc) and [Gensim](https://radimrehurek.com/gensim),
|
||||
or you can load in any pretrained **transformer model** if you install
|
||||
[`spacy-transformers`](https://github.com/explosion/spacy-transformers). You can
|
||||
also do your own language model pretraining via the
|
||||
[`spacy pretrain`](/api/cli#pretrain) command. You can even **share** your
|
||||
transformer or other contextual embedding model across multiple components,
|
||||
which can make long pipelines several times more efficient. To use transfer
|
||||
learning, you'll need at least a few annotated examples for what you're trying
|
||||
to predict. Otherwise, you could try using a "one-shot learning" approach using
|
||||
[vectors and similarity](/usage/linguistic-features#vectors-similarity).
|
||||
|
||||
<Accordion title="What’s the difference between word vectors and language models?" id="vectors-vs-language-models">
|
||||
|
||||
[Transformers](#transformers) are large and powerful neural networks that give
|
||||
you better accuracy, but are harder to deploy in production, as they require a
|
||||
GPU to run effectively. [Word vectors](#word-vectors) are a slightly older
|
||||
technique that can give your models a smaller improvement in accuracy, and can
|
||||
also provide some additional capabilities.
|
||||
|
||||
The key difference between word-vectors and contextual language models such as
|
||||
transformers is that word vectors model **lexical types**, rather than _tokens_.
|
||||
If you have a list of terms with no context around them, a transformer model
|
||||
like BERT can't really help you. BERT is designed to understand language **in
|
||||
context**, which isn't what you have. A word vectors table will be a much better
|
||||
fit for your task. However, if you do have words in context – whole sentences or
|
||||
paragraphs of running text – word vectors will only provide a very rough
|
||||
approximation of what the text is about.
|
||||
|
||||
Word vectors are also very computationally efficient, as they map a word to a
|
||||
vector with a single indexing operation. Word vectors are therefore useful as a
|
||||
way to **improve the accuracy** of neural network models, especially models that
|
||||
are small or have received little or no pretraining. In spaCy, word vector
|
||||
tables are only used as **static features**. spaCy does not backpropagate
|
||||
gradients to the pretrained word vectors table. The static vectors table is
|
||||
usually used in combination with a smaller table of learned task-specific
|
||||
embeddings.
|
||||
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="When should I add word vectors to my model?">
|
||||
|
||||
Word vectors are not compatible with most [transformer models](#transformers),
|
||||
but if you're training another type of NLP network, it's almost always worth
|
||||
adding word vectors to your model. As well as improving your final accuracy,
|
||||
word vectors often make experiments more consistent, as the accuracy you reach
|
||||
will be less sensitive to how the network is randomly initialized. High variance
|
||||
due to random chance can slow down your progress significantly, as you need to
|
||||
run many experiments to filter the signal from the noise.
|
||||
|
||||
Word vector features need to be enabled prior to training, and the same word
|
||||
vectors table will need to be available at runtime as well. You cannot add word
|
||||
vector features once the model has already been trained, and you usually cannot
|
||||
replace one word vectors table with another without causing a significant loss
|
||||
of performance.
|
||||
|
||||
</Accordion>
|
||||
|
||||
## Shared embedding layers {id="embedding-layers"}
|
||||
|
||||
spaCy lets you share a single transformer or other token-to-vector ("tok2vec")
|
||||
embedding layer between multiple components. You can even update the shared
|
||||
layer, performing **multi-task learning**. Reusing the tok2vec layer between
|
||||
components can make your pipeline run a lot faster and result in much smaller
|
||||
models. However, it can make the pipeline less modular and make it more
|
||||
difficult to swap components or retrain parts of the pipeline. Multi-task
|
||||
learning can affect your accuracy (either positively or negatively), and may
|
||||
require some retuning of your hyper-parameters.
|
||||
|
||||

|
||||
|
||||
| Shared | Independent |
|
||||
| ------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------- |
|
||||
| ✅ **smaller:** models only need to include a single copy of the embeddings | ❌ **larger:** models need to include the embeddings for each component |
|
||||
| ✅ **faster:** embed the documents once for your whole pipeline | ❌ **slower:** rerun the embedding for each component |
|
||||
| ❌ **less composable:** all components require the same embedding component in the pipeline | ✅ **modular:** components can be moved and swapped freely |
|
||||
|
||||
You can share a single transformer or other tok2vec model between multiple
|
||||
components by adding a [`Transformer`](/api/transformer) or
|
||||
[`Tok2Vec`](/api/tok2vec) component near the start of your pipeline. Components
|
||||
later in the pipeline can "connect" to it by including a **listener layer** like
|
||||
[Tok2VecListener](/api/architectures#Tok2VecListener) within their model.
|
||||
|
||||

|
||||
|
||||
At the beginning of training, the [`Tok2Vec`](/api/tok2vec) component will grab
|
||||
a reference to the relevant listener layers in the rest of your pipeline. When
|
||||
it processes a batch of documents, it will pass forward its predictions to the
|
||||
listeners, allowing the listeners to **reuse the predictions** when they are
|
||||
eventually called. A similar mechanism is used to pass gradients from the
|
||||
listeners back to the model. The [`Transformer`](/api/transformer) component and
|
||||
[TransformerListener](/api/architectures#TransformerListener) layer do the same
|
||||
thing for transformer models, but the `Transformer` component will also save the
|
||||
transformer outputs to the
|
||||
[`Doc._.trf_data`](/api/transformer#custom_attributes) extension attribute,
|
||||
giving you access to them after the pipeline has finished running.
|
||||
|
||||
### Example: Shared vs. independent config {id="embedding-layers-config"}
|
||||
|
||||
The [config system](/usage/training#config) lets you express model configuration
|
||||
for both shared and independent embedding layers. The shared setup uses a single
|
||||
[`Tok2Vec`](/api/tok2vec) component with the
|
||||
[Tok2Vec](/api/architectures#Tok2Vec) architecture. All other components, like
|
||||
the entity recognizer, use a
|
||||
[Tok2VecListener](/api/architectures#Tok2VecListener) layer as their model's
|
||||
`tok2vec` argument, which connects to the `tok2vec` component model.
|
||||
|
||||
```ini {title="Shared",highlight="1-2,4-5,19-20"}
|
||||
[components.tok2vec]
|
||||
factory = "tok2vec"
|
||||
|
||||
[components.tok2vec.model]
|
||||
@architectures = "spacy.Tok2Vec.v2"
|
||||
|
||||
[components.tok2vec.model.embed]
|
||||
@architectures = "spacy.MultiHashEmbed.v2"
|
||||
|
||||
[components.tok2vec.model.encode]
|
||||
@architectures = "spacy.MaxoutWindowEncoder.v2"
|
||||
|
||||
[components.ner]
|
||||
factory = "ner"
|
||||
|
||||
[components.ner.model]
|
||||
@architectures = "spacy.TransitionBasedParser.v1"
|
||||
|
||||
[components.ner.model.tok2vec]
|
||||
@architectures = "spacy.Tok2VecListener.v1"
|
||||
```
|
||||
|
||||
In the independent setup, the entity recognizer component defines its own
|
||||
[Tok2Vec](/api/architectures#Tok2Vec) instance. Other components will do the
|
||||
same. This makes them fully independent and doesn't require an upstream
|
||||
[`Tok2Vec`](/api/tok2vec) component to be present in the pipeline.
|
||||
|
||||
```ini {title="Independent", highlight="7-8"}
|
||||
[components.ner]
|
||||
factory = "ner"
|
||||
|
||||
[components.ner.model]
|
||||
@architectures = "spacy.TransitionBasedParser.v1"
|
||||
|
||||
[components.ner.model.tok2vec]
|
||||
@architectures = "spacy.Tok2Vec.v2"
|
||||
|
||||
[components.ner.model.tok2vec.embed]
|
||||
@architectures = "spacy.MultiHashEmbed.v2"
|
||||
|
||||
[components.ner.model.tok2vec.encode]
|
||||
@architectures = "spacy.MaxoutWindowEncoder.v2"
|
||||
```
|
||||
|
||||
{/* TODO: Once rehearsal is tested, mention it here. */}
|
||||
|
||||
## Using transformer models {id="transformers"}
|
||||
|
||||
Transformers are a family of neural network architectures that compute **dense,
|
||||
context-sensitive representations** for the tokens in your documents. Downstream
|
||||
models in your pipeline can then use these representations as input features to
|
||||
**improve their predictions**. You can connect multiple components to a single
|
||||
transformer model, with any or all of those components giving feedback to the
|
||||
transformer to fine-tune it to your tasks. spaCy's transformer support
|
||||
interoperates with [PyTorch](https://pytorch.org) and the
|
||||
[HuggingFace `transformers`](https://huggingface.co/transformers/) library,
|
||||
giving you access to thousands of pretrained models for your pipelines. There
|
||||
are many [great guides](http://jalammar.github.io/illustrated-transformer/) to
|
||||
transformer models, but for practical purposes, you can simply think of them as
|
||||
drop-in replacements that let you achieve **higher accuracy** in exchange for
|
||||
**higher training and runtime costs**.
|
||||
|
||||
### Setup and installation {id="transformers-installation"}
|
||||
|
||||
> #### System requirements
|
||||
>
|
||||
> We recommend an NVIDIA **GPU** with at least **10GB of memory** in order to
|
||||
> work with transformer models. Make sure your GPU drivers are up to date and
|
||||
> you have **CUDA v9+** installed.
|
||||
|
||||
> The exact requirements will depend on the transformer model. Training a
|
||||
> transformer-based model without a GPU will be too slow for most practical
|
||||
> purposes.
|
||||
>
|
||||
> Provisioning a new machine will require about **5GB** of data to be
|
||||
> downloaded: 3GB CUDA runtime, 800MB PyTorch, 400MB CuPy, 500MB weights, 200MB
|
||||
> spaCy and dependencies.
|
||||
|
||||
Once you have CUDA installed, we recommend installing PyTorch following the
|
||||
[PyTorch installation guidelines](https://pytorch.org/get-started/locally/) for
|
||||
your package manager and CUDA version. If you skip this step, pip will install
|
||||
PyTorch as a dependency below, but it may not find the best version for your
|
||||
setup.
|
||||
|
||||
```bash {title="Example: Install PyTorch 1.11.0 for CUDA 11.3 with pip"}
|
||||
# See: https://pytorch.org/get-started/locally/
|
||||
$ pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
|
||||
```
|
||||
|
||||
Next, install spaCy with the extras for your CUDA version and transformers. The
|
||||
CUDA extra (e.g., `cuda102`, `cuda113`) installs the correct version of
|
||||
[`cupy`](https://docs.cupy.dev/en/stable/install.html#installing-cupy), which is
|
||||
just like `numpy`, but for GPU. You may also need to set the `CUDA_PATH`
|
||||
environment variable if your CUDA runtime is installed in a non-standard
|
||||
location. Putting it all together, if you had installed CUDA 11.3 in
|
||||
`/opt/nvidia/cuda`, you would run:
|
||||
|
||||
```bash {title="Installation with CUDA"}
|
||||
$ export CUDA_PATH="/opt/nvidia/cuda"
|
||||
$ pip install -U %%SPACY_PKG_NAME[cuda113,transformers]%%SPACY_PKG_FLAGS
|
||||
```
|
||||
|
||||
For [`transformers`](https://huggingface.co/transformers/) v4.0.0+ and models
|
||||
that require [`SentencePiece`](https://github.com/google/sentencepiece) (e.g.,
|
||||
ALBERT, CamemBERT, XLNet, Marian, and T5), install the additional dependencies
|
||||
with:
|
||||
|
||||
```bash {title="Install sentencepiece"}
|
||||
$ pip install transformers[sentencepiece]
|
||||
```
|
||||
|
||||
### Runtime usage {id="transformers-runtime"}
|
||||
|
||||
Transformer models can be used as **drop-in replacements** for other types of
|
||||
neural networks, so your spaCy pipeline can include them in a way that's
|
||||
completely invisible to the user. Users will download, load and use the model in
|
||||
the standard way, like any other spaCy pipeline. Instead of using the
|
||||
transformers as subnetworks directly, you can also use them via the
|
||||
[`Transformer`](/api/transformer) pipeline component.
|
||||
|
||||

|
||||
|
||||
The `Transformer` component sets the
|
||||
[`Doc._.trf_data`](/api/transformer#custom_attributes) extension attribute,
|
||||
which lets you access the transformers outputs at runtime. The trained
|
||||
transformer-based [pipelines](/models) provided by spaCy end on `_trf`, e.g.
|
||||
[`en_core_web_trf`](/models/en#en_core_web_trf).
|
||||
|
||||
```bash
|
||||
$ python -m spacy download en_core_web_trf
|
||||
```
|
||||
|
||||
```python {title="Example"}
|
||||
import spacy
|
||||
from thinc.api import set_gpu_allocator, require_gpu
|
||||
|
||||
# Use the GPU, with memory allocations directed via PyTorch.
|
||||
# This prevents out-of-memory errors that would otherwise occur from competing
|
||||
# memory pools.
|
||||
set_gpu_allocator("pytorch")
|
||||
require_gpu(0)
|
||||
|
||||
nlp = spacy.load("en_core_web_trf")
|
||||
for doc in nlp.pipe(["some text", "some other text"]):
|
||||
# For spaCy v3.0-v3.6, trf pipelines use spacy-transformers and the transformer output in doc._.trf_data is a TransformerData object.
|
||||
try:
|
||||
tokvecs = doc._.trf_data.tensors[-1]
|
||||
# For spaCy v3.7+, trf pipelines use spacy-curated-transformers and doc._.trf_data is a DocTransformerOutput object.
|
||||
except AttributeError as e:
|
||||
tokvecs = doc._.trf_data.last_hidden_layer_state # or doc._.trf_data.all_outputs[-1]
|
||||
```
|
||||
|
||||
You can also customize how the [`Transformer`](/api/transformer) component sets
|
||||
annotations onto the [`Doc`](/api/doc) by specifying a custom
|
||||
`set_extra_annotations` function. This callback will be called with the raw
|
||||
input and output data for the whole batch, along with the batch of `Doc`
|
||||
objects, allowing you to implement whatever you need. The annotation setter is
|
||||
called with a batch of [`Doc`](/api/doc) objects and a
|
||||
[`FullTransformerBatch`](/api/transformer#fulltransformerbatch) containing the
|
||||
transformers data for the batch.
|
||||
|
||||
```python
|
||||
def custom_annotation_setter(docs, trf_data):
|
||||
doc_data = list(trf_data.doc_data)
|
||||
for doc, data in zip(docs, doc_data):
|
||||
doc._.custom_attr = data
|
||||
|
||||
nlp = spacy.load("en_core_web_trf")
|
||||
nlp.get_pipe("transformer").set_extra_annotations = custom_annotation_setter
|
||||
doc = nlp("This is a text")
|
||||
assert isinstance(doc._.custom_attr, TransformerData)
|
||||
print(doc._.custom_attr.tensors)
|
||||
```
|
||||
|
||||
### Training usage {id="transformers-training"}
|
||||
|
||||
The recommended workflow for training is to use spaCy's
|
||||
[config system](/usage/training#config), usually via the
|
||||
[`spacy train`](/api/cli#train) command. The training config defines all
|
||||
component settings and hyperparameters in one place and lets you describe a tree
|
||||
of objects by referring to creation functions, including functions you register
|
||||
yourself. For details on how to get started with training your own model, check
|
||||
out the [training quickstart](/usage/training#quickstart).
|
||||
|
||||
{/* TODO: <Project id="pipelines/transformers"> */}
|
||||
|
||||
{/* The easiest way to get started is to clone a transformers-based project */}
|
||||
{/* template. Swap in your data, edit the settings and hyperparameters and train, */}
|
||||
{/* evaluate, package and visualize your model. */}
|
||||
|
||||
{/* </Project> */}
|
||||
|
||||
The `[components]` section in the [`config.cfg`](/api/data-formats#config)
|
||||
describes the pipeline components and the settings used to construct them,
|
||||
including their model implementation. Here's a config snippet for the
|
||||
[`Transformer`](/api/transformer) component, along with matching Python code. In
|
||||
this case, the `[components.transformer]` block describes the `transformer`
|
||||
component:
|
||||
|
||||
> #### Python equivalent
|
||||
>
|
||||
> ```python
|
||||
> from spacy_transformers import Transformer, TransformerModel
|
||||
> from spacy_transformers.annotation_setters import null_annotation_setter
|
||||
> from spacy_transformers.span_getters import get_doc_spans
|
||||
>
|
||||
> trf = Transformer(
|
||||
> nlp.vocab,
|
||||
> TransformerModel(
|
||||
> "bert-base-cased",
|
||||
> get_spans=get_doc_spans,
|
||||
> tokenizer_config={"use_fast": True},
|
||||
> ),
|
||||
> set_extra_annotations=null_annotation_setter,
|
||||
> max_batch_items=4096,
|
||||
> )
|
||||
> ```
|
||||
|
||||
```ini {title="config.cfg",excerpt="true"}
|
||||
[components.transformer]
|
||||
factory = "transformer"
|
||||
max_batch_items = 4096
|
||||
|
||||
[components.transformer.model]
|
||||
@architectures = "spacy-transformers.TransformerModel.v3"
|
||||
name = "bert-base-cased"
|
||||
tokenizer_config = {"use_fast": true}
|
||||
|
||||
[components.transformer.model.get_spans]
|
||||
@span_getters = "spacy-transformers.doc_spans.v1"
|
||||
|
||||
[components.transformer.set_extra_annotations]
|
||||
@annotation_setters = "spacy-transformers.null_annotation_setter.v1"
|
||||
|
||||
```
|
||||
|
||||
The `[components.transformer.model]` block describes the `model` argument passed
|
||||
to the transformer component. It's a Thinc
|
||||
[`Model`](https://thinc.ai/docs/api-model) object that will be passed into the
|
||||
component. Here, it references the function
|
||||
[spacy-transformers.TransformerModel.v3](/api/architectures#TransformerModel)
|
||||
registered in the [`architectures` registry](/api/top-level#registry). If a key
|
||||
in a block starts with `@`, it's **resolved to a function** and all other
|
||||
settings are passed to the function as arguments. In this case, `name`,
|
||||
`tokenizer_config` and `get_spans`.
|
||||
|
||||
`get_spans` is a function that takes a batch of `Doc` objects and returns lists
|
||||
of potentially overlapping `Span` objects to process by the transformer. Several
|
||||
[built-in functions](/api/transformer#span_getters) are available – for example,
|
||||
to process the whole document or individual sentences. When the config is
|
||||
resolved, the function is created and passed into the model as an argument.
|
||||
|
||||
The `name` value is the name of any [HuggingFace model](huggingface-models),
|
||||
which will be downloaded automatically the first time it's used. You can also
|
||||
use a local file path. For full details, see the
|
||||
[`TransformerModel` docs](/api/architectures#TransformerModel).
|
||||
|
||||
[huggingface-models]:
|
||||
https://huggingface.co/models?library=pytorch&sort=downloads
|
||||
|
||||
A wide variety of PyTorch models are supported, but some might not work. If a
|
||||
model doesn't seem to work feel free to open an
|
||||
[issue](https://github.com/explosion/spacy/issues). Additionally note that
|
||||
Transformers loaded in spaCy can only be used for tensors, and pretrained
|
||||
task-specific heads or text generation features cannot be used as part of the
|
||||
`transformer` pipeline component.
|
||||
|
||||
<Infobox variant="warning">
|
||||
|
||||
Remember that the `config.cfg` used for training should contain **no missing
|
||||
values** and requires all settings to be defined. You don't want any hidden
|
||||
defaults creeping in and changing your results! spaCy will tell you if settings
|
||||
are missing, and you can run
|
||||
[`spacy init fill-config`](/api/cli#init-fill-config) to automatically fill in
|
||||
all defaults.
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Customizing the settings {id="transformers-training-custom-settings"}
|
||||
|
||||
To change any of the settings, you can edit the `config.cfg` and re-run the
|
||||
training. To change any of the functions, like the span getter, you can replace
|
||||
the name of the referenced function – e.g.
|
||||
`@span_getters = "spacy-transformers.sent_spans.v1"` to process sentences. You
|
||||
can also register your own functions using the
|
||||
[`span_getters` registry](/api/top-level#registry). For instance, the following
|
||||
custom function returns [`Span`](/api/span) objects following sentence
|
||||
boundaries, unless a sentence succeeds a certain amount of tokens, in which case
|
||||
subsentences of at most `max_length` tokens are returned.
|
||||
|
||||
> #### config.cfg
|
||||
>
|
||||
> ```ini
|
||||
> [components.transformer.model.get_spans]
|
||||
> @span_getters = "custom_sent_spans"
|
||||
> max_length = 25
|
||||
> ```
|
||||
|
||||
```python {title="code.py"}
|
||||
import spacy_transformers
|
||||
|
||||
@spacy_transformers.registry.span_getters("custom_sent_spans")
|
||||
def configure_custom_sent_spans(max_length: int):
|
||||
def get_custom_sent_spans(docs):
|
||||
spans = []
|
||||
for doc in docs:
|
||||
spans.append([])
|
||||
for sent in doc.sents:
|
||||
start = 0
|
||||
end = max_length
|
||||
while end <= len(sent):
|
||||
spans[-1].append(sent[start:end])
|
||||
start += max_length
|
||||
end += max_length
|
||||
if start < len(sent):
|
||||
spans[-1].append(sent[start:len(sent)])
|
||||
return spans
|
||||
|
||||
return get_custom_sent_spans
|
||||
```
|
||||
|
||||
To resolve the config during training, spaCy needs to know about your custom
|
||||
function. You can make it available via the `--code` argument that can point to
|
||||
a Python file. For more details on training with custom code, see the
|
||||
[training documentation](/usage/training#custom-functions).
|
||||
|
||||
```bash
|
||||
python -m spacy train ./config.cfg --code ./code.py
|
||||
```
|
||||
|
||||
### Customizing the model implementations {id="training-custom-model"}
|
||||
|
||||
The [`Transformer`](/api/transformer) component expects a Thinc
|
||||
[`Model`](https://thinc.ai/docs/api-model) object to be passed in as its `model`
|
||||
argument. You're not limited to the implementation provided by
|
||||
`spacy-transformers` – the only requirement is that your registered function
|
||||
must return an object of type ~~Model[List[Doc], FullTransformerBatch]~~: that
|
||||
is, a Thinc model that takes a list of [`Doc`](/api/doc) objects, and returns a
|
||||
[`FullTransformerBatch`](/api/transformer#fulltransformerbatch) object with the
|
||||
transformer data.
|
||||
|
||||
The same idea applies to task models that power the **downstream components**.
|
||||
Most of spaCy's built-in model creation functions support a `tok2vec` argument,
|
||||
which should be a Thinc layer of type ~~Model[List[Doc], List[Floats2d]]~~. This
|
||||
is where we'll plug in our transformer model, using the
|
||||
[TransformerListener](/api/architectures#TransformerListener) layer, which
|
||||
sneakily delegates to the `Transformer` pipeline component.
|
||||
|
||||
```ini {title="config.cfg (excerpt)",highlight="12"}
|
||||
[components.ner]
|
||||
factory = "ner"
|
||||
|
||||
[nlp.pipeline.ner.model]
|
||||
@architectures = "spacy.TransitionBasedParser.v1"
|
||||
state_type = "ner"
|
||||
extra_state_tokens = false
|
||||
hidden_width = 128
|
||||
maxout_pieces = 3
|
||||
use_upper = false
|
||||
|
||||
[nlp.pipeline.ner.model.tok2vec]
|
||||
@architectures = "spacy-transformers.TransformerListener.v1"
|
||||
grad_factor = 1.0
|
||||
|
||||
[nlp.pipeline.ner.model.tok2vec.pooling]
|
||||
@layers = "reduce_mean.v1"
|
||||
```
|
||||
|
||||
The [TransformerListener](/api/architectures#TransformerListener) layer expects
|
||||
a [pooling layer](https://thinc.ai/docs/api-layers#reduction-ops) as the
|
||||
argument `pooling`, which needs to be of type ~~Model[Ragged, Floats2d]~~. This
|
||||
layer determines how the vector for each spaCy token will be computed from the
|
||||
zero or more source rows the token is aligned against. Here we use the
|
||||
[`reduce_mean`](https://thinc.ai/docs/api-layers#reduce_mean) layer, which
|
||||
averages the wordpiece rows. We could instead use
|
||||
[`reduce_max`](https://thinc.ai/docs/api-layers#reduce_max), or a custom
|
||||
function you write yourself.
|
||||
|
||||
You can have multiple components all listening to the same transformer model,
|
||||
and all passing gradients back to it. By default, all of the gradients will be
|
||||
**equally weighted**. You can control this with the `grad_factor` setting, which
|
||||
lets you reweight the gradients from the different listeners. For instance,
|
||||
setting `grad_factor = 0` would disable gradients from one of the listeners,
|
||||
while `grad_factor = 2.0` would multiply them by 2. This is similar to having a
|
||||
custom learning rate for each component. Instead of a constant, you can also
|
||||
provide a schedule, allowing you to freeze the shared parameters at the start of
|
||||
training.
|
||||
|
||||
## Static vectors {id="static-vectors"}
|
||||
|
||||
If your pipeline includes a **word vectors table**, you'll be able to use the
|
||||
`.similarity()` method on the [`Doc`](/api/doc), [`Span`](/api/span),
|
||||
[`Token`](/api/token) and [`Lexeme`](/api/lexeme) objects. You'll also be able
|
||||
to access the vectors using the `.vector` attribute, or you can look up one or
|
||||
more vectors directly using the [`Vocab`](/api/vocab) object. Pipelines with
|
||||
word vectors can also **use the vectors as features** for the statistical
|
||||
models, which can **improve the accuracy** of your components.
|
||||
|
||||
Word vectors in spaCy are "static" in the sense that they are not learned
|
||||
parameters of the statistical models, and spaCy itself does not feature any
|
||||
algorithms for learning word vector tables. You can train a word vectors table
|
||||
using tools such as [floret](https://github.com/explosion/floret),
|
||||
[Gensim](https://radimrehurek.com/gensim/), [FastText](https://fasttext.cc/) or
|
||||
[GloVe](https://nlp.stanford.edu/projects/glove/), or download existing
|
||||
pretrained vectors. The [`init vectors`](/api/cli#init-vectors) command lets you
|
||||
convert vectors for use with spaCy and will give you a directory you can load or
|
||||
refer to in your [training configs](/usage/training#config).
|
||||
|
||||
<Infobox title="Word vectors and similarity" emoji="📖">
|
||||
|
||||
For more details on loading word vectors into spaCy, using them for similarity
|
||||
and improving word vector coverage by truncating and pruning the vectors, see
|
||||
the usage guide on
|
||||
[word vectors and similarity](/usage/linguistic-features#vectors-similarity).
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Using word vectors in your models {id="word-vectors-models"}
|
||||
|
||||
Many neural network models are able to use word vector tables as additional
|
||||
features, which sometimes results in significant improvements in accuracy.
|
||||
spaCy's built-in embedding layer,
|
||||
[MultiHashEmbed](/api/architectures#MultiHashEmbed), can be configured to use
|
||||
word vector tables using the `include_static_vectors` flag.
|
||||
|
||||
```ini
|
||||
[tagger.model.tok2vec.embed]
|
||||
@architectures = "spacy.MultiHashEmbed.v2"
|
||||
width = 128
|
||||
attrs = ["LOWER","PREFIX","SUFFIX","SHAPE"]
|
||||
rows = [5000,2500,2500,2500]
|
||||
include_static_vectors = true
|
||||
```
|
||||
|
||||
<Infobox title="How it works" emoji="💡">
|
||||
|
||||
The configuration system will look up the string `"spacy.MultiHashEmbed.v2"` in
|
||||
the `architectures` [registry](/api/top-level#registry), and call the returned
|
||||
object with the rest of the arguments from the block. This will result in a call
|
||||
to the
|
||||
[`MultiHashEmbed`](https://github.com/explosion/spacy/tree/develop/spacy/ml/models/tok2vec.py)
|
||||
function, which will return a [Thinc](https://thinc.ai) model object with the
|
||||
type signature ~~Model[List[Doc], List[Floats2d]]~~. Because the embedding layer
|
||||
takes a list of `Doc` objects as input, it does not need to store a copy of the
|
||||
vectors table. The vectors will be retrieved from the `Doc` objects that are
|
||||
passed in, via the `doc.vocab.vectors` attribute. This part of the process is
|
||||
handled by the [StaticVectors](/api/architectures#StaticVectors) layer.
|
||||
|
||||
</Infobox>
|
||||
|
||||
#### Creating a custom embedding layer {id="custom-embedding-layer"}
|
||||
|
||||
The [MultiHashEmbed](/api/architectures#StaticVectors) layer is spaCy's
|
||||
recommended strategy for constructing initial word representations for your
|
||||
neural network models, but you can also implement your own. You can register any
|
||||
function to a string name, and then reference that function within your config
|
||||
(see the [training docs](/usage/training) for more details). To try this out,
|
||||
you can save the following little example to a new Python file:
|
||||
|
||||
```python
|
||||
from spacy.ml.staticvectors import StaticVectors
|
||||
from spacy.util import registry
|
||||
|
||||
print("I was imported!")
|
||||
|
||||
@registry.architectures("my_example.MyEmbedding.v1")
|
||||
def MyEmbedding(output_width: int) -> Model[List[Doc], List[Floats2d]]:
|
||||
print("I was called!")
|
||||
return StaticVectors(nO=output_width)
|
||||
```
|
||||
|
||||
If you pass the path to your file to the [`spacy train`](/api/cli#train) command
|
||||
using the `--code` argument, your file will be imported, which means the
|
||||
decorator registering the function will be run. Your function is now on equal
|
||||
footing with any of spaCy's built-ins, so you can drop it in instead of any
|
||||
other model with the same input and output signature. For instance, you could
|
||||
use it in the tagger model as follows:
|
||||
|
||||
```ini
|
||||
[tagger.model.tok2vec.embed]
|
||||
@architectures = "my_example.MyEmbedding.v1"
|
||||
output_width = 128
|
||||
```
|
||||
|
||||
Now that you have a custom function wired into the network, you can start
|
||||
implementing the logic you're interested in. For example, let's say you want to
|
||||
try a relatively simple embedding strategy that makes use of static word
|
||||
vectors, but combines them via summation with a smaller table of learned
|
||||
embeddings.
|
||||
|
||||
```python
|
||||
from thinc.api import add, chain, remap_ids, Embed
|
||||
from spacy.ml.staticvectors import StaticVectors
|
||||
from spacy.ml.featureextractor import FeatureExtractor
|
||||
from spacy.util import registry
|
||||
|
||||
@registry.architectures("my_example.MyEmbedding.v1")
|
||||
def MyCustomVectors(
|
||||
output_width: int,
|
||||
vector_width: int,
|
||||
embed_rows: int,
|
||||
key2row: Dict[int, int]
|
||||
) -> Model[List[Doc], List[Floats2d]]:
|
||||
return add(
|
||||
StaticVectors(nO=output_width),
|
||||
chain(
|
||||
FeatureExtractor(["ORTH"]),
|
||||
remap_ids(key2row),
|
||||
Embed(nO=output_width, nV=embed_rows)
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
#### Creating a custom vectors implementation {id="custom-vectors",version="3.7"}
|
||||
|
||||
You can specify a custom registered vectors class under `[nlp.vectors]` in order
|
||||
to use static vectors in formats other than the ones supported by
|
||||
[`Vectors`](/api/vectors). Extend the abstract [`BaseVectors`](/api/basevectors)
|
||||
class to implement your custom vectors.
|
||||
|
||||
As an example, the following `BPEmbVectors` class implements support for
|
||||
[BPEmb subword embeddings](https://bpemb.h-its.org/):
|
||||
|
||||
```python
|
||||
# requires: pip install bpemb
|
||||
import warnings
|
||||
from pathlib import Path
|
||||
from typing import Callable, Optional, cast
|
||||
|
||||
from bpemb import BPEmb
|
||||
from thinc.api import Ops, get_current_ops
|
||||
from thinc.backends import get_array_ops
|
||||
from thinc.types import Floats2d
|
||||
|
||||
from spacy.strings import StringStore
|
||||
from spacy.util import registry
|
||||
from spacy.vectors import BaseVectors
|
||||
from spacy.vocab import Vocab
|
||||
|
||||
|
||||
class BPEmbVectors(BaseVectors):
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
strings: Optional[StringStore] = None,
|
||||
lang: Optional[str] = None,
|
||||
vs: Optional[int] = None,
|
||||
dim: Optional[int] = None,
|
||||
cache_dir: Optional[Path] = None,
|
||||
encode_extra_options: Optional[str] = None,
|
||||
model_file: Optional[Path] = None,
|
||||
emb_file: Optional[Path] = None,
|
||||
):
|
||||
kwargs = {}
|
||||
if lang is not None:
|
||||
kwargs["lang"] = lang
|
||||
if vs is not None:
|
||||
kwargs["vs"] = vs
|
||||
if dim is not None:
|
||||
kwargs["dim"] = dim
|
||||
if cache_dir is not None:
|
||||
kwargs["cache_dir"] = cache_dir
|
||||
if encode_extra_options is not None:
|
||||
kwargs["encode_extra_options"] = encode_extra_options
|
||||
if model_file is not None:
|
||||
kwargs["model_file"] = model_file
|
||||
if emb_file is not None:
|
||||
kwargs["emb_file"] = emb_file
|
||||
self.bpemb = BPEmb(**kwargs)
|
||||
self.strings = strings
|
||||
self.name = repr(self.bpemb)
|
||||
self.n_keys = -1
|
||||
self.mode = "BPEmb"
|
||||
self.to_ops(get_current_ops())
|
||||
|
||||
def __contains__(self, key):
|
||||
return True
|
||||
|
||||
def is_full(self):
|
||||
return True
|
||||
|
||||
def add(self, key, *, vector=None, row=None):
|
||||
warnings.warn(
|
||||
(
|
||||
"Skipping BPEmbVectors.add: the bpemb vector table cannot be "
|
||||
"modified. Vectors are calculated from bytepieces."
|
||||
)
|
||||
)
|
||||
return -1
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.get_batch([key])[0]
|
||||
|
||||
def get_batch(self, keys):
|
||||
keys = [self.strings.as_string(key) for key in keys]
|
||||
bp_ids = self.bpemb.encode_ids(keys)
|
||||
ops = get_array_ops(self.bpemb.emb.vectors)
|
||||
indices = ops.asarray(ops.xp.hstack(bp_ids), dtype="int32")
|
||||
lengths = ops.asarray([len(x) for x in bp_ids], dtype="int32")
|
||||
vecs = ops.reduce_mean(cast(Floats2d, self.bpemb.emb.vectors[indices]), lengths)
|
||||
return vecs
|
||||
|
||||
@property
|
||||
def shape(self):
|
||||
return self.bpemb.vectors.shape
|
||||
|
||||
def __len__(self):
|
||||
return self.shape[0]
|
||||
|
||||
@property
|
||||
def vectors_length(self):
|
||||
return self.shape[1]
|
||||
|
||||
@property
|
||||
def size(self):
|
||||
return self.bpemb.vectors.size
|
||||
|
||||
def to_ops(self, ops: Ops):
|
||||
self.bpemb.emb.vectors = ops.asarray(self.bpemb.emb.vectors)
|
||||
|
||||
|
||||
@registry.vectors("BPEmbVectors.v1")
|
||||
def create_bpemb_vectors(
|
||||
lang: Optional[str] = "multi",
|
||||
vs: Optional[int] = None,
|
||||
dim: Optional[int] = None,
|
||||
cache_dir: Optional[Path] = None,
|
||||
encode_extra_options: Optional[str] = None,
|
||||
model_file: Optional[Path] = None,
|
||||
emb_file: Optional[Path] = None,
|
||||
) -> Callable[[Vocab], BPEmbVectors]:
|
||||
def bpemb_vectors_factory(vocab: Vocab) -> BPEmbVectors:
|
||||
return BPEmbVectors(
|
||||
strings=vocab.strings,
|
||||
lang=lang,
|
||||
vs=vs,
|
||||
dim=dim,
|
||||
cache_dir=cache_dir,
|
||||
encode_extra_options=encode_extra_options,
|
||||
model_file=model_file,
|
||||
emb_file=emb_file,
|
||||
)
|
||||
|
||||
return bpemb_vectors_factory
|
||||
```
|
||||
|
||||
<Infobox variant="warning">
|
||||
|
||||
Note that the serialization methods are not implemented, so the embeddings are
|
||||
loaded from your local cache or downloaded by `BPEmb` each time the pipeline is
|
||||
loaded.
|
||||
|
||||
</Infobox>
|
||||
|
||||
To use this in your pipeline, specify this registered function under
|
||||
`[nlp.vectors]` in your config:
|
||||
|
||||
```ini
|
||||
[nlp.vectors]
|
||||
@vectors = "BPEmbVectors.v1"
|
||||
lang = "en"
|
||||
```
|
||||
|
||||
Or specify it when creating a blank pipeline:
|
||||
|
||||
```python
|
||||
nlp = spacy.blank("en", config={"nlp.vectors": {"@vectors": "BPEmbVectors.v1", "lang": "en"}})
|
||||
```
|
||||
|
||||
Remember to include this code with `--code` when using
|
||||
[`spacy train`](/api/cli#train) and [`spacy package`](/api/cli#package).
|
||||
|
||||
## Pretraining {id="pretraining"}
|
||||
|
||||
The [`spacy pretrain`](/api/cli#pretrain) command lets you initialize your
|
||||
models with **information from raw text**. Without pretraining, the models for
|
||||
your components will usually be initialized randomly. The idea behind
|
||||
pretraining is simple: random probably isn't optimal, so if we have some text to
|
||||
learn from, we can probably find a way to get the model off to a better start.
|
||||
|
||||
Pretraining uses the same [`config.cfg`](/usage/training#config) file as the
|
||||
regular training, which helps keep the settings and hyperparameters consistent.
|
||||
The additional `[pretraining]` section has several configuration subsections
|
||||
that are familiar from the training block: the `[pretraining.batcher]`,
|
||||
`[pretraining.optimizer]` and `[pretraining.corpus]` all work the same way and
|
||||
expect the same types of objects, although for pretraining your corpus does not
|
||||
need to have any annotations, so you will often use a different reader, such as
|
||||
the [`JsonlCorpus`](/api/top-level#jsonlcorpus).
|
||||
|
||||
> #### Raw text format
|
||||
>
|
||||
> The raw text can be provided in spaCy's
|
||||
> [binary `.spacy` format](/api/data-formats#training) consisting of serialized
|
||||
> `Doc` objects or as a JSONL (newline-delimited JSON) with a key `"text"` per
|
||||
> entry. This allows the data to be read in line by line, while also allowing
|
||||
> you to include newlines in the texts.
|
||||
>
|
||||
> ```json
|
||||
> {"text": "Can I ask where you work now and what you do, and if you enjoy it?"}
|
||||
> {"text": "They may just pull out of the Seattle market completely, at least until they have autonomous vehicles."}
|
||||
> ```
|
||||
>
|
||||
> You can also use your own custom corpus loader instead.
|
||||
|
||||
You can add a `[pretraining]` block to your config by setting the
|
||||
`--pretraining` flag on [`init config`](/api/cli#init-config) or
|
||||
[`init fill-config`](/api/cli#init-fill-config):
|
||||
|
||||
```bash
|
||||
$ python -m spacy init fill-config config.cfg config_pretrain.cfg --pretraining
|
||||
```
|
||||
|
||||
You can then run [`spacy pretrain`](/api/cli#pretrain) with the updated config
|
||||
and pass in optional config overrides, like the path to the raw text file:
|
||||
|
||||
```bash
|
||||
$ python -m spacy pretrain config_pretrain.cfg ./output --paths.raw_text text.jsonl
|
||||
```
|
||||
|
||||
The following defaults are used for the `[pretraining]` block and merged into
|
||||
your existing config when you run [`init config`](/api/cli#init-config) or
|
||||
[`init fill-config`](/api/cli#init-fill-config) with `--pretraining`. If needed,
|
||||
you can [configure](#pretraining-configure) the settings and hyperparameters or
|
||||
change the [objective](#pretraining-objectives).
|
||||
|
||||
```ini
|
||||
%%GITHUB_SPACY/spacy/default_config_pretraining.cfg
|
||||
```
|
||||
|
||||
### How pretraining works {id="pretraining-details"}
|
||||
|
||||
The impact of [`spacy pretrain`](/api/cli#pretrain) varies, but it will usually
|
||||
be worth trying if you're **not using a transformer** model and you have
|
||||
**relatively little training data** (for instance, fewer than 5,000 sentences).
|
||||
A good rule of thumb is that pretraining will generally give you a similar
|
||||
accuracy improvement to using word vectors in your model. If word vectors have
|
||||
given you a 10% error reduction, pretraining with spaCy might give you another
|
||||
10%, for a 20% error reduction in total.
|
||||
|
||||
The [`spacy pretrain`](/api/cli#pretrain) command will take a **specific
|
||||
subnetwork** within one of your components, and add additional layers to build a
|
||||
network for a temporary task that forces the model to learn something about
|
||||
sentence structure and word cooccurrence statistics.
|
||||
|
||||
Pretraining produces a **binary weights file** that can be loaded back in at the
|
||||
start of training, using the configuration option `initialize.init_tok2vec`. The
|
||||
weights file specifies an initial set of weights. Training then proceeds as
|
||||
normal.
|
||||
|
||||
You can only pretrain one subnetwork from your pipeline at a time, and the
|
||||
subnetwork must be typed ~~Model[List[Doc], List[Floats2d]]~~ (i.e. it has to be
|
||||
a "tok2vec" layer). The most common workflow is to use the
|
||||
[`Tok2Vec`](/api/tok2vec) component to create a shared token-to-vector layer for
|
||||
several components of your pipeline, and apply pretraining to its whole model.
|
||||
|
||||
#### Configuring the pretraining {id="pretraining-configure"}
|
||||
|
||||
The [`spacy pretrain`](/api/cli#pretrain) command is configured using the
|
||||
`[pretraining]` section of your [config file](/usage/training#config). The
|
||||
`component` and `layer` settings tell spaCy how to **find the subnetwork** to
|
||||
pretrain. The `layer` setting should be either the empty string (to use the
|
||||
whole model), or a
|
||||
[node reference](https://thinc.ai/docs/usage-models#model-state). Most of
|
||||
spaCy's built-in model architectures have a reference named `"tok2vec"` that
|
||||
will refer to the right layer.
|
||||
|
||||
```ini {title="config.cfg"}
|
||||
# 1. Use the whole model of the "tok2vec" component
|
||||
[pretraining]
|
||||
component = "tok2vec"
|
||||
layer = ""
|
||||
|
||||
# 2. Pretrain the "tok2vec" node of the "textcat" component
|
||||
[pretraining]
|
||||
component = "textcat"
|
||||
layer = "tok2vec"
|
||||
```
|
||||
|
||||
#### Connecting pretraining to training {id="pretraining-training"}
|
||||
|
||||
To benefit from pretraining, your training step needs to know to initialize its
|
||||
`tok2vec` component with the weights learned from the pretraining step. You do
|
||||
this by setting `initialize.init_tok2vec` to the filename of the `.bin` file
|
||||
that you want to use from pretraining.
|
||||
|
||||
A pretraining step that runs for 5 epochs with an output path of `pretrain/`, as
|
||||
an example, produces `pretrain/model0.bin` through `pretrain/model4.bin` plus a
|
||||
copy of the last iteration as `pretrain/model-last.bin`. Additionally, you can
|
||||
configure `n_save_epoch` to tell pretraining in which epoch interval it should
|
||||
save the current training progress. To use the final output to initialize your
|
||||
`tok2vec` layer, you could fill in this value in your config file:
|
||||
|
||||
```ini {title="config.cfg"}
|
||||
|
||||
[paths]
|
||||
init_tok2vec = "pretrain/model-last.bin"
|
||||
|
||||
[initialize]
|
||||
init_tok2vec = ${paths.init_tok2vec}
|
||||
```
|
||||
|
||||
<Infobox variant="warning">
|
||||
|
||||
The outputs of `spacy pretrain` are not the same data format as the pre-packaged
|
||||
static word vectors that would go into
|
||||
[`initialize.vectors`](/api/data-formats#config-initialize). The pretraining
|
||||
output consists of the weights that the `tok2vec` component should start with in
|
||||
an existing pipeline, so it goes in `initialize.init_tok2vec`.
|
||||
|
||||
</Infobox>
|
||||
|
||||
#### Pretraining objectives {id="pretraining-objectives"}
|
||||
|
||||
> ```ini
|
||||
> ### Characters objective
|
||||
> [pretraining.objective]
|
||||
> @architectures = "spacy.PretrainCharacters.v1"
|
||||
> maxout_pieces = 3
|
||||
> hidden_size = 300
|
||||
> n_characters = 4
|
||||
> ```
|
||||
>
|
||||
> ```ini
|
||||
> ### Vectors objective
|
||||
> [pretraining.objective]
|
||||
> @architectures = "spacy.PretrainVectors.v1"
|
||||
> maxout_pieces = 3
|
||||
> hidden_size = 300
|
||||
> loss = "cosine"
|
||||
> ```
|
||||
|
||||
Two pretraining objectives are available, both of which are variants of the
|
||||
cloze task [Devlin et al. (2018)](https://arxiv.org/abs/1810.04805) introduced
|
||||
for BERT. The objective can be defined and configured via the
|
||||
`[pretraining.objective]` config block.
|
||||
|
||||
- [`PretrainCharacters`](/api/architectures#pretrain_chars): The `"characters"`
|
||||
objective asks the model to predict some number of leading and trailing UTF-8
|
||||
bytes for the words. For instance, setting `n_characters = 2`, the model will
|
||||
try to predict the first two and last two characters of the word.
|
||||
|
||||
- [`PretrainVectors`](/api/architectures#pretrain_vectors): The `"vectors"`
|
||||
objective asks the model to predict the word's vector, from a static
|
||||
embeddings table. This requires a word vectors model to be trained and loaded.
|
||||
The vectors objective can optimize either a cosine or an L2 loss. We've
|
||||
generally found cosine loss to perform better.
|
||||
|
||||
These pretraining objectives use a trick that we term **language modelling with
|
||||
approximate outputs (LMAO)**. The motivation for the trick is that predicting an
|
||||
exact word ID introduces a lot of incidental complexity. You need a large output
|
||||
layer, and even then, the vocabulary is too large, which motivates tokenization
|
||||
schemes that do not align to actual word boundaries. At the end of training, the
|
||||
output layer will be thrown away regardless: we just want a task that forces the
|
||||
network to model something about word cooccurrence statistics. Predicting
|
||||
leading and trailing characters does that more than adequately, as the exact
|
||||
word sequence could be recovered with high accuracy if the initial and trailing
|
||||
characters are predicted accurately. With the vectors objective, the pretraining
|
||||
uses the embedding space learned by an algorithm such as
|
||||
[GloVe](https://nlp.stanford.edu/projects/glove/) or
|
||||
[Word2vec](https://code.google.com/archive/p/word2vec/), allowing the model to
|
||||
focus on the contextual modelling we actual care about.
|
||||
@@ -0,0 +1,116 @@
|
||||
---
|
||||
title: Facts & Figures
|
||||
teaser: The hard numbers for spaCy and how it compares to other tools
|
||||
next: /usage/spacy-101
|
||||
menu:
|
||||
- ['Feature Comparison', 'comparison']
|
||||
- ['Benchmarks', 'benchmarks']
|
||||
# TODO: - ['Citing spaCy', 'citation']
|
||||
---
|
||||
|
||||
## Comparison {id="comparison",hidden="true"}
|
||||
|
||||
spaCy is a **free, open-source library** for advanced **Natural Language
|
||||
Processing** (NLP) in Python. It's designed specifically for **production use**
|
||||
and helps you build applications that process and "understand" large volumes of
|
||||
text. It can be used to build information extraction or natural language
|
||||
understanding systems.
|
||||
|
||||
### Feature overview {id="comparison-features"}
|
||||
|
||||
<Features />
|
||||
|
||||
### When should I use spaCy? {id="comparison-usage"}
|
||||
|
||||
- ✅ **I'm a beginner and just getting started with NLP.** – spaCy makes it easy
|
||||
to get started and comes with extensive documentation, including a
|
||||
beginner-friendly [101 guide](/usage/spacy-101), a free interactive
|
||||
[online course](https://course.spacy.io) and a range of
|
||||
[video tutorials](https://www.youtube.com/c/ExplosionAI).
|
||||
- ✅ **I want to build an end-to-end production application.** – spaCy is
|
||||
specifically designed for production use and lets you build and train powerful
|
||||
NLP pipelines and package them for easy deployment.
|
||||
- ✅ **I want my application to be efficient on GPU _and_ CPU.** – While spaCy
|
||||
lets you train modern NLP models that are best run on GPU, it also offers
|
||||
CPU-optimized pipelines, which are less accurate but much cheaper to run.
|
||||
- ✅ **I want to try out different neural network architectures for NLP.** –
|
||||
spaCy lets you customize and swap out the model architectures powering its
|
||||
components, and implement your own using a framework like PyTorch or
|
||||
TensorFlow. The declarative configuration system makes it easy to mix and
|
||||
match functions and keep track of your hyperparameters to make sure your
|
||||
experiments are reproducible.
|
||||
- ❌ **I want to build a language generation application.** – spaCy's focus is
|
||||
natural language _processing_ and extracting information from large volumes of
|
||||
text. While you can use it to help you re-write existing text, it doesn't
|
||||
include any specific functionality for language generation tasks.
|
||||
- ❌ **I want to research machine learning algorithms.** spaCy is built on the
|
||||
latest research, but it's not a research library. If your goal is to write
|
||||
papers and run benchmarks, spaCy is probably not a good choice. However, you
|
||||
can use it to make the results of your research easily available for others to
|
||||
use, e.g. via a custom spaCy component.
|
||||
|
||||
## Benchmarks {id="benchmarks"}
|
||||
|
||||
spaCy v3.0 introduces transformer-based pipelines that bring spaCy's accuracy
|
||||
right up to **current state-of-the-art**. You can also use a CPU-optimized
|
||||
pipeline, which is less accurate but much cheaper to run.
|
||||
|
||||
{/* TODO: update benchmarks and intro */}
|
||||
|
||||
> #### Evaluation details
|
||||
>
|
||||
> - **OntoNotes 5.0:** spaCy's English models are trained on this corpus, as
|
||||
> it's several times larger than other English treebanks. However, most
|
||||
> systems do not report accuracies on it.
|
||||
> - **Penn Treebank:** The "classic" parsing evaluation for research. However,
|
||||
> it's quite far removed from actual usage: it uses sentences with
|
||||
> gold-standard segmentation and tokenization, from a pretty specific type of
|
||||
> text (articles from a single newspaper, 1984-1989).
|
||||
|
||||
<Benchmarks />
|
||||
|
||||
<figure>
|
||||
|
||||
| Dependency Parsing System | UAS | LAS |
|
||||
| ------------------------------------------------------------------------------ | ---: | ---: |
|
||||
| spaCy RoBERTa (2020) | 95.1 | 93.7 |
|
||||
| [Mrini et al.](https://khalilmrini.github.io/Label_Attention_Layer.pdf) (2019) | 97.4 | 96.3 |
|
||||
| [Zhou and Zhao](https://www.aclweb.org/anthology/P19-1230/) (2019) | 97.2 | 95.7 |
|
||||
|
||||
<figcaption className="caption">
|
||||
|
||||
**Dependency parsing accuracy** on the Penn Treebank. See
|
||||
[NLP-progress](http://nlpprogress.com/english/dependency_parsing.html) for more
|
||||
results. Project template:
|
||||
[`benchmarks/parsing_penn_treebank`](%%GITHUB_PROJECTS/benchmarks/parsing_penn_treebank).
|
||||
|
||||
</figcaption>
|
||||
|
||||
</figure>
|
||||
|
||||
### Speed comparison {id="benchmarks-speed"}
|
||||
|
||||
We compare the speed of different NLP libraries, measured in words per second
|
||||
(WPS) - higher is better. The evaluation was performed on 10,000 Reddit
|
||||
comments.
|
||||
|
||||
<figure>
|
||||
|
||||
| Library | Pipeline | WPS CPU <Help>words per second on CPU, higher is better</Help> | WPS GPU <Help>words per second on GPU, higher is better</Help> |
|
||||
| ------- | ----------------------------------------------- | -------------------------------------------------------------: | -------------------------------------------------------------: |
|
||||
| spaCy | [`en_core_web_lg`](/models/en#en_core_web_lg) | 10,014 | 14,954 |
|
||||
| spaCy | [`en_core_web_trf`](/models/en#en_core_web_trf) | 684 | 3,768 |
|
||||
| Stanza | `en_ewt` | 878 | 2,180 |
|
||||
| Flair | `pos`(`-fast`) & `ner`(`-fast`) | 323 | 1,184 |
|
||||
| UDPipe | `english-ewt-ud-2.5` | 1,101 | _n/a_ |
|
||||
|
||||
<figcaption className="caption">
|
||||
|
||||
**End-to-end processing speed** on raw unannotated text. Project template:
|
||||
[`benchmarks/speed`](%%GITHUB_PROJECTS/benchmarks/speed).
|
||||
|
||||
</figcaption>
|
||||
|
||||
</figure>
|
||||
|
||||
{/* TODO: ## Citing spaCy {id="citation"} */}
|
||||
@@ -0,0 +1,470 @@
|
||||
---
|
||||
title: Install spaCy
|
||||
next: /usage/models
|
||||
menu:
|
||||
- ['Quickstart', 'quickstart']
|
||||
- ['Instructions', 'installation']
|
||||
- ['Troubleshooting', 'troubleshooting']
|
||||
- ['Changelog', 'changelog']
|
||||
---
|
||||
|
||||
## Quickstart {hidden="true"}
|
||||
|
||||
> #### 📖 Looking for the old docs?
|
||||
>
|
||||
> To help you make the transition from v2.x to v3.0, we've uploaded the old
|
||||
> website to [**v2.spacy.io**](https://v2.spacy.io/docs). To see what's changed
|
||||
> and how to migrate, see the [v3.0 guide](/usage/v3).
|
||||
|
||||
<QuickstartInstall id="quickstart" />
|
||||
|
||||
## Installation instructions {id="installation"}
|
||||
|
||||
spaCy is compatible with **64-bit CPython 3.7+** and runs on **Unix/Linux**,
|
||||
**macOS/OS X** and **Windows**. The latest spaCy releases are available over
|
||||
[pip](https://pypi.python.org/pypi/spacy) and
|
||||
[conda](https://anaconda.org/conda-forge/spacy).
|
||||
|
||||
### pip {id="pip"}
|
||||
|
||||
Using pip, spaCy releases are available as source packages and binary wheels.
|
||||
Before you install spaCy and its dependencies, make sure that your `pip`,
|
||||
`setuptools` and `wheel` are up to date.
|
||||
|
||||
> #### Download pipelines
|
||||
>
|
||||
> After installation you typically want to download a trained pipeline. For more
|
||||
> info and available packages, see the [models directory](/models).
|
||||
>
|
||||
> ```bash
|
||||
> $ python -m spacy download en_core_web_sm
|
||||
>
|
||||
> >>> import spacy
|
||||
> >>> nlp = spacy.load("en_core_web_sm")
|
||||
> ```
|
||||
|
||||
```bash
|
||||
$ pip install -U pip setuptools wheel
|
||||
$ pip install -U %%SPACY_PKG_NAME%%SPACY_PKG_FLAGS
|
||||
```
|
||||
|
||||
When using pip it is generally recommended to install packages in a virtual
|
||||
environment to avoid modifying system state:
|
||||
|
||||
```bash
|
||||
$ python -m venv .env
|
||||
$ source .env/bin/activate
|
||||
$ pip install -U pip setuptools wheel
|
||||
$ pip install -U %%SPACY_PKG_NAME%%SPACY_PKG_FLAGS
|
||||
```
|
||||
|
||||
spaCy also lets you install extra dependencies by specifying the following
|
||||
keywords in brackets, e.g. `spacy[ja]` or `spacy[lookups,transformers]` (with
|
||||
multiple comma-separated extras). See the `[options.extras_require]` section in
|
||||
spaCy's [`setup.cfg`](%%GITHUB_SPACY/setup.cfg) for details on what's included.
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```bash
|
||||
> $ pip install %%SPACY_PKG_NAME[lookups,transformers]%%SPACY_PKG_FLAGS
|
||||
> ```
|
||||
|
||||
| Name | Description |
|
||||
| ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `lookups` | Install [`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data) for data tables for lemmatization and lexeme normalization. The data is serialized with trained pipelines, so you only need this package if you want to train your own models. |
|
||||
| `transformers` | Install [`spacy-transformers`](https://github.com/explosion/spacy-transformers). The package will be installed automatically when you install a transformer-based pipeline. |
|
||||
| `cuda`, ... | Install spaCy with GPU support provided by [CuPy](https://cupy.chainer.org) for your given CUDA version. See the GPU [installation instructions](#gpu) for details and options. |
|
||||
| `apple` | Install [`thinc-apple-ops`](https://github.com/explosion/thinc-apple-ops) to improve performance on an Apple M1. |
|
||||
| `ja`, `ko`, `th` | Install additional dependencies required for tokenization for the [languages](/usage/models#languages). |
|
||||
|
||||
### conda {id="conda"}
|
||||
|
||||
Thanks to our great community, we've been able to re-add conda support. You can
|
||||
also install spaCy via `conda-forge`:
|
||||
|
||||
```bash
|
||||
$ conda install -c conda-forge spacy
|
||||
```
|
||||
|
||||
For the feedstock including the build recipe and configuration, check out
|
||||
[this repository](https://github.com/conda-forge/spacy-feedstock). Note that we
|
||||
currently don't publish any [pre-releases](#changelog-pre) on conda.
|
||||
|
||||
### Upgrading spaCy {id="upgrading"}
|
||||
|
||||
> #### Upgrading from v2 to v3
|
||||
>
|
||||
> Although we've tried to keep breaking changes to a minimum, upgrading from
|
||||
> spaCy v2.x to v3.x may still require some changes to your code base. For
|
||||
> details see the sections on [backwards incompatibilities](/usage/v3#incompat)
|
||||
> and [migrating](/usage/v3#migrating). Also remember to download the new
|
||||
> trained pipelines, and retrain your own pipelines.
|
||||
|
||||
When updating to a newer version of spaCy, it's generally recommended to start
|
||||
with a clean virtual environment. If you're upgrading to a new major version,
|
||||
make sure you have the latest **compatible trained pipelines** installed, and
|
||||
that there are no old and incompatible packages left over in your environment,
|
||||
as this can often lead to unexpected results and errors. If you've trained your
|
||||
own models, keep in mind that your train and runtime inputs must match. This
|
||||
means you'll have to **retrain your pipelines** with the new version.
|
||||
|
||||
spaCy also provides a [`validate`](/api/cli#validate) command, which lets you
|
||||
verify that all installed pipeline packages are compatible with your spaCy
|
||||
version. If incompatible packages are found, tips and installation instructions
|
||||
are printed. It's recommended to run the command with `python -m` to make sure
|
||||
you're executing the correct version of spaCy.
|
||||
|
||||
```bash
|
||||
$ pip install -U %%SPACY_PKG_NAME%%SPACY_PKG_FLAGS
|
||||
$ python -m spacy validate
|
||||
```
|
||||
|
||||
### Run spaCy with GPU {id="gpu",version="2.0.14"}
|
||||
|
||||
As of v2.0, spaCy comes with neural network models that are implemented in our
|
||||
machine learning library, [Thinc](https://thinc.ai). For GPU support, we've been
|
||||
grateful to use the work of Chainer's [CuPy](https://cupy.chainer.org) module,
|
||||
which provides a numpy-compatible interface for GPU arrays.
|
||||
|
||||
spaCy can be installed for a CUDA-compatible GPU by specifying `spacy[cuda]`,
|
||||
`spacy[cuda102]`, `spacy[cuda112]`, `spacy[cuda113]`, etc. If you know your CUDA
|
||||
version, using the more explicit specifier allows CuPy to be installed via
|
||||
wheel, saving some compilation time. The specifiers should install
|
||||
[`cupy`](https://cupy.chainer.org).
|
||||
|
||||
```bash
|
||||
$ pip install -U %%SPACY_PKG_NAME[cuda113]%%SPACY_PKG_FLAGS
|
||||
```
|
||||
|
||||
Once you have a GPU-enabled installation, the best way to activate it is to call
|
||||
[`spacy.prefer_gpu`](/api/top-level#spacy.prefer_gpu) or
|
||||
[`spacy.require_gpu()`](/api/top-level#spacy.require_gpu) somewhere in your
|
||||
script before any pipelines have been loaded. `require_gpu` will raise an error
|
||||
if no GPU is available.
|
||||
|
||||
```python
|
||||
import spacy
|
||||
|
||||
spacy.prefer_gpu()
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
```
|
||||
|
||||
### Compile from source {id="source"}
|
||||
|
||||
The other way to install spaCy is to clone its
|
||||
[GitHub repository](https://github.com/explosion/spaCy) and build it from
|
||||
source. That is the common way if you want to make changes to the code base.
|
||||
You'll need to make sure that you have a development environment consisting of a
|
||||
Python distribution including header files, a compiler,
|
||||
[pip](https://pip.pypa.io/en/stable/) and [git](https://git-scm.com) installed.
|
||||
The compiler part is the trickiest. How to do that depends on your system. See
|
||||
notes on [Ubuntu](#source-ubuntu), [macOS / OS X](#source-osx) and
|
||||
[Windows](#source-windows) for details.
|
||||
|
||||
```bash
|
||||
$ python -m pip install -U pip setuptools wheel # install/update build tools
|
||||
$ git clone https://github.com/explosion/spaCy # clone spaCy
|
||||
$ cd spaCy # navigate into dir
|
||||
$ python -m venv .env # create environment in .env
|
||||
$ source .env/bin/activate # activate virtual env
|
||||
$ pip install -r requirements.txt # install requirements
|
||||
$ pip install --no-build-isolation --editable . # compile and install spaCy
|
||||
```
|
||||
|
||||
To install with extras:
|
||||
|
||||
```bash
|
||||
$ pip install --no-build-isolation --editable .[lookups,cuda102]
|
||||
```
|
||||
|
||||
How to install compilers and related build tools:
|
||||
|
||||
- <strong id="source-ubuntu">Ubuntu:</strong> Install system-level dependencies via
|
||||
`apt-get`: `sudo apt-get install build-essential python-dev git`
|
||||
- <strong id="source-osx">macOS / OS X:</strong> Install a recent version of [XCode](https://developer.apple.com/xcode/),
|
||||
including the so-called "Command Line Tools". macOS and OS X ship with Python and
|
||||
Git preinstalled.
|
||||
- <strong id="source-windows">Windows:</strong> Install a version of the [Visual
|
||||
C++ Build Tools](https://visualstudio.microsoft.com/visual-cpp-build-tools/) or
|
||||
[Visual Studio Express](https://www.visualstudio.com/vs/visual-studio-express/)
|
||||
that matches the version that was used to compile your Python interpreter.
|
||||
|
||||
#### Using build constraints when compiling from source
|
||||
|
||||
If you install spaCy from source or with `pip` for platforms where there are not
|
||||
binary wheels on PyPI, you may need to use build constraints if any package in
|
||||
your environment requires an older version of `numpy`.
|
||||
|
||||
If `numpy` gets downgraded from the most recent release at any point after
|
||||
you've compiled `spacy`, you might see an error that looks like this:
|
||||
|
||||
```
|
||||
numpy.ndarray size changed, may indicate binary incompatibility.
|
||||
```
|
||||
|
||||
To fix this, create a new virtual environment and install `spacy` and all of its
|
||||
dependencies using build constraints.
|
||||
[Build constraints](https://pip.pypa.io/en/stable/user_guide/#constraints-files)
|
||||
specify an older version of `numpy` that is only used while compiling `spacy`,
|
||||
and then your runtime environment can use any newer version of `numpy` and still
|
||||
be compatible. In addition, use `--no-cache-dir` to ignore any previously cached
|
||||
wheels so that all relevant packages are recompiled from scratch:
|
||||
|
||||
```shell
|
||||
PIP_CONSTRAINT=https://raw.githubusercontent.com/explosion/spacy/master/build-constraints.txt \
|
||||
pip install spacy --no-cache-dir
|
||||
```
|
||||
|
||||
Our build constraints currently specify the oldest supported `numpy` available
|
||||
on PyPI for `x86_64` and `aarch64`. Depending on your platform and environment,
|
||||
you may want to customize the specific versions of `numpy`. For other platforms,
|
||||
you can have a look at SciPy's
|
||||
[`oldest-supported-numpy`](https://github.com/scipy/oldest-supported-numpy/blob/main/setup.cfg)
|
||||
package to see what the oldest recommended versions of `numpy` are.
|
||||
|
||||
(_Warning_: don't use `pip install -c constraints.txt` instead of
|
||||
`PIP_CONSTRAINT`, since this isn't applied to the isolated build environments.)
|
||||
|
||||
#### Additional options for developers {id="source-developers"}
|
||||
|
||||
Some additional options may be useful for spaCy developers who are editing the
|
||||
source code and recompiling frequently.
|
||||
|
||||
- Install in editable mode. Changes to `.py` files will be reflected as soon as
|
||||
the files are saved, but edits to Cython files (`.pxd`, `.pyx`) will require
|
||||
the `pip install` command below to be run again. Before installing in editable
|
||||
mode, be sure you have removed any previous installs with
|
||||
`pip uninstall spacy`, which you may need to run multiple times to remove all
|
||||
traces of earlier installs.
|
||||
|
||||
```bash
|
||||
$ pip install -r requirements.txt
|
||||
$ pip install --no-build-isolation --editable .
|
||||
```
|
||||
|
||||
- Build in parallel. Starting in v3.4.0, you can specify the number of build
|
||||
jobs with the environment variable `SPACY_NUM_BUILD_JOBS`:
|
||||
|
||||
```bash
|
||||
$ pip install -r requirements.txt
|
||||
$ SPACY_NUM_BUILD_JOBS=4 pip install --no-build-isolation --editable .
|
||||
```
|
||||
|
||||
- For editable mode and parallel builds with `python setup.py` instead of `pip`
|
||||
(no longer recommended):
|
||||
|
||||
```bash
|
||||
$ pip install -r requirements.txt
|
||||
$ python setup.py build_ext --inplace -j 4
|
||||
$ python setup.py develop
|
||||
```
|
||||
|
||||
#### Visual Studio Code extension
|
||||
|
||||

|
||||
|
||||
The [spaCy VSCode Extension](https://github.com/explosion/spacy-vscode) provides
|
||||
additional tooling and features for working with spaCy's config files. Version
|
||||
1.0.0 includes hover descriptions for registry functions, variables, and section
|
||||
names within the config as an installable extension.
|
||||
|
||||
1. Install a supported version of Python on your system (`>=3.7`)
|
||||
2. Install the
|
||||
[Python Extension for Visual Studio Code](https://code.visualstudio.com/docs/python/python-tutorial)
|
||||
3. Create a
|
||||
[virtual python environment](https://docs.python.org/3/library/venv.html)
|
||||
4. Install all python requirements (`spaCy >= 3.4.0` & `pygls >= 1.0.0`)
|
||||
5. Install
|
||||
[spaCy extension for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=Explosion.spacy-extension)
|
||||
6. Select your python environment
|
||||
7. You are ready to work with `.cfg` files in spaCy!
|
||||
|
||||
### Building an executable {id="executable"}
|
||||
|
||||
The spaCy repository includes a [`Makefile`](%%GITHUB_SPACY/Makefile) that
|
||||
builds an executable zip file using [`pex`](https://github.com/pantsbuild/pex)
|
||||
(**P**ython **Ex**ecutable). The executable includes spaCy and all its package
|
||||
dependencies and only requires the system Python at runtime. Building an
|
||||
executable `.pex` file is often the most convenient way to deploy spaCy, as it
|
||||
lets you separate the build from the deployment process.
|
||||
|
||||
> #### Usage
|
||||
>
|
||||
> To use a `.pex` file, just replace `python` with the path to the file when you
|
||||
> execute your code or CLI commands. This is equivalent to running Python in a
|
||||
> virtual environment with spaCy installed.
|
||||
>
|
||||
> ```bash
|
||||
> $ ./spacy.pex my_script.py
|
||||
> $ ./spacy.pex -m spacy info
|
||||
> ```
|
||||
|
||||
```bash
|
||||
$ git clone https://github.com/explosion/spaCy
|
||||
$ cd spaCy
|
||||
$ make
|
||||
```
|
||||
|
||||
You can configure the build process with the following environment variables:
|
||||
|
||||
| Variable | Description |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `SPACY_EXTRAS` | Additional Python packages to install alongside spaCy with optional version specifications. Should be a string that can be passed to `pip install`. See [`Makefile`](%%GITHUB_SPACY/Makefile) for defaults. |
|
||||
| `PYVER` | The Python version to build against. This version needs to be available on your build and runtime machines. Defaults to `3.8`. |
|
||||
| `WHEELHOUSE` | Directory to store the wheel files during compilation. Defaults to `./wheelhouse`. |
|
||||
|
||||
### Run tests {id="run-tests"}
|
||||
|
||||
spaCy comes with an [extensive test suite](%%GITHUB_SPACY/spacy/tests). In order
|
||||
to run the tests, you'll usually want to clone the [repository](%%GITHUB_SPACY)
|
||||
and [build spaCy from source](#source). This will also install the required
|
||||
development dependencies and test utilities defined in the `requirements.txt`.
|
||||
|
||||
Alternatively, you can find out where spaCy is installed and run `pytest` on
|
||||
that directory. Don't forget to also install the test utilities via spaCy's
|
||||
[`requirements.txt`](%%GITHUB_SPACY/requirements.txt):
|
||||
|
||||
```bash
|
||||
$ python -c "import os; import spacy; print(os.path.dirname(spacy.__file__))"
|
||||
$ pip install -r path/to/requirements.txt
|
||||
$ python -m pytest --pyargs %%SPACY_PKG_NAME
|
||||
```
|
||||
|
||||
Calling `pytest` on the spaCy directory will run only the basic tests. The flag
|
||||
`--slow` is optional and enables additional tests that take longer.
|
||||
|
||||
```bash
|
||||
$ python -m pip install -U pytest # update pytest
|
||||
$ python -m pytest --pyargs %%SPACY_PKG_NAME # basic tests
|
||||
$ python -m pytest --pyargs %%SPACY_PKG_NAME --slow # basic and slow tests
|
||||
```
|
||||
|
||||
## Troubleshooting guide {id="troubleshooting"}
|
||||
|
||||
This section collects some of the most common errors you may come across when
|
||||
installing, loading and using spaCy, as well as their solutions. Also see the
|
||||
[Discussions FAQ Thread](https://github.com/explosion/spaCy/discussions/8226),
|
||||
which is updated more frequently and covers more transitory issues.
|
||||
|
||||
> #### Help us improve this guide
|
||||
>
|
||||
> Did you come across a problem like the ones listed here and want to share the
|
||||
> solution? You can find the "Suggest edits" button at the bottom of this page
|
||||
> that points you to the source. We always appreciate
|
||||
> [pull requests](https://github.com/explosion/spaCy/pulls)!
|
||||
|
||||
<Accordion title="No compatible model found" id="compatible-model">
|
||||
|
||||
```
|
||||
No compatible package found for [lang] (spaCy vX.X.X).
|
||||
```
|
||||
|
||||
This usually means that the trained pipeline you're trying to download does not
|
||||
exist, or isn't available for your version of spaCy. Check the
|
||||
[compatibility table](https://github.com/explosion/spacy-models/tree/master/compatibility.json)
|
||||
to see which packages are available for your spaCy version. If you're using an
|
||||
old version, consider upgrading to the latest release. Note that while spaCy
|
||||
supports tokenization for [a variety of languages](/usage/models#languages), not
|
||||
all of them come with trained pipelines. To only use the tokenizer, import the
|
||||
language's `Language` class instead, for example
|
||||
`from spacy.lang.fr import French`.
|
||||
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="Import error: No module named spacy" id="import-error">
|
||||
|
||||
```
|
||||
Import Error: No module named spacy
|
||||
```
|
||||
|
||||
This error means that the spaCy module can't be located on your system, or in
|
||||
your environment. Make sure you have spaCy installed. If you're using a virtual
|
||||
environment, make sure it's activated and check that spaCy is installed in that
|
||||
environment – otherwise, you're trying to load a system installation. You can
|
||||
also run `which python` to find out where your Python executable is located.
|
||||
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="Import error: No module named [name]" id="import-error-models">
|
||||
|
||||
```
|
||||
ImportError: No module named 'en_core_web_sm'
|
||||
```
|
||||
|
||||
As of spaCy v1.7, all trained pipelines can be installed as Python packages.
|
||||
This means that they'll become importable modules of your application. If this
|
||||
fails, it's usually a sign that the package is not installed in the current
|
||||
environment. Run `pip list` or `pip freeze` to check which pipeline packages you
|
||||
have installed, and install the [correct package](/models) if necessary. If
|
||||
you're importing a package manually at the top of a file, make sure to use the
|
||||
full name of the package.
|
||||
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="Command not found: spacy" id="command-not-found">
|
||||
|
||||
```
|
||||
command not found: spacy
|
||||
```
|
||||
|
||||
This error may occur when running the `spacy` command from the command line.
|
||||
spaCy does not currently add an entry to your `PATH` environment variable, as
|
||||
this can lead to unexpected results, especially when using a virtual
|
||||
environment. Instead, spaCy adds an auto-alias that maps `spacy` to
|
||||
`python -m spacy`. If this is not working as expected, run the command with
|
||||
`python -m`, yourself – for example `python -m spacy download en_core_web_sm`.
|
||||
For more info on this, see the [`download`](/api/cli#download) command.
|
||||
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="'module' object has no attribute 'load'" id="module-load">
|
||||
|
||||
```
|
||||
AttributeError: 'module' object has no attribute 'load'
|
||||
```
|
||||
|
||||
While this could technically have many causes, including spaCy being broken, the
|
||||
most likely one is that your script's file or directory name is "shadowing" the
|
||||
module – e.g. your file is called `spacy.py`, or a directory you're importing
|
||||
from is called `spacy`. So, when using spaCy, never call anything else `spacy`.
|
||||
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="NER model doesn't recognize other entities anymore after training" id="catastrophic-forgetting">
|
||||
|
||||
If your training data only contained new entities and you didn't mix in any
|
||||
examples the model previously recognized, it can cause the model to "forget"
|
||||
what it had previously learned. This is also referred to as the "catastrophic
|
||||
forgetting problem". A solution is to pre-label some text, and mix it with the
|
||||
new text in your updates. You can also do this by running spaCy over some text,
|
||||
extracting a bunch of entities the model previously recognized correctly, and
|
||||
adding them to your training examples.
|
||||
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="Unhashable type: 'list'" id="unhashable-list">
|
||||
|
||||
```
|
||||
TypeError: unhashable type: 'list'
|
||||
```
|
||||
|
||||
If you're training models, writing them to disk, and versioning them with git,
|
||||
you might encounter this error when trying to load them in a Windows
|
||||
environment. This happens because a default install of Git for Windows is
|
||||
configured to automatically convert Unix-style end-of-line characters (LF) to
|
||||
Windows-style ones (CRLF) during file checkout (and the reverse when
|
||||
committing). While that's mostly fine for text files, a trained model written to
|
||||
disk has some binary files that should not go through this conversion. When they
|
||||
do, you get the error above. You can fix it by either changing your
|
||||
[`core.autocrlf`](https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration)
|
||||
setting to `"false"`, or by committing a
|
||||
[`.gitattributes`](https://git-scm.com/docs/gitattributes) file to your
|
||||
repository to tell Git on which files or folders it shouldn't do LF-to-CRLF
|
||||
conversion, with an entry like `path/to/spacy/model/** -text`. After you've done
|
||||
either of these, clone your repository again.
|
||||
|
||||
</Accordion>
|
||||
|
||||
## Changelog {id="changelog"}
|
||||
|
||||
<Changelog />
|
||||
@@ -0,0 +1,534 @@
|
||||
---
|
||||
title: Large Language Models
|
||||
teaser: Integrating LLMs into structured NLP pipelines
|
||||
menu:
|
||||
- ['Motivation', 'motivation']
|
||||
- ['Install', 'install']
|
||||
- ['Usage', 'usage']
|
||||
- ['Logging', 'logging']
|
||||
- ['API', 'api']
|
||||
- ['Tasks', 'tasks']
|
||||
- ['Models', 'models']
|
||||
---
|
||||
|
||||
[The spacy-llm package](https://github.com/explosion/spacy-llm) integrates Large
|
||||
Language Models (LLMs) into spaCy pipelines, featuring a modular system for
|
||||
**fast prototyping** and **prompting**, and turning unstructured responses into
|
||||
**robust outputs** for various NLP tasks, **no training data** required.
|
||||
|
||||
- Serializable `llm` **component** to integrate prompts into your pipeline
|
||||
- **Modular functions** to define the [**task**](#tasks) (prompting and parsing)
|
||||
and [**model**](#models) (model to use)
|
||||
- Support for **hosted APIs** and self-hosted **open-source models**
|
||||
- Integration with [`LangChain`](https://github.com/hwchase17/langchain)
|
||||
- Access to
|
||||
**[OpenAI API](https://platform.openai.com/docs/api-reference/introduction)**,
|
||||
including GPT-4 and various GPT-3 models
|
||||
- Built-in support for various **open-source** models hosted on
|
||||
[Hugging Face](https://huggingface.co/)
|
||||
- Usage examples for standard NLP tasks such as **Named Entity Recognition** and
|
||||
**Text Classification**
|
||||
- Easy implementation of **your own functions** via the
|
||||
[registry](/api/top-level#registry) for custom prompting, parsing and model
|
||||
integrations
|
||||
|
||||
## Motivation {id="motivation"}
|
||||
|
||||
Large Language Models (LLMs) feature powerful natural language understanding
|
||||
capabilities. With only a few (and sometimes no) examples, an LLM can be
|
||||
prompted to perform custom NLP tasks such as text categorization, named entity
|
||||
recognition, coreference resolution, information extraction and more.
|
||||
|
||||
Supervised learning is much worse than LLM prompting for prototyping, but for
|
||||
many tasks it's much better for production. A transformer model that runs
|
||||
comfortably on a single GPU is extremely powerful, and it's likely to be a
|
||||
better choice for any task for which you have a well-defined output. You train
|
||||
the model with anything from a few hundred to a few thousand labelled examples,
|
||||
and it will learn to do exactly that. Efficiency, reliability and control are
|
||||
all better with supervised learning, and accuracy will generally be higher than
|
||||
LLM prompting as well.
|
||||
|
||||
`spacy-llm` lets you have **the best of both worlds**. You can quickly
|
||||
initialize a pipeline with components powered by LLM prompts, and freely mix in
|
||||
components powered by other approaches. As your project progresses, you can look
|
||||
at replacing some or all of the LLM-powered components as you require.
|
||||
|
||||
Of course, there can be components in your system for which the power of an LLM
|
||||
is fully justified. If you want a system that can synthesize information from
|
||||
multiple documents in subtle ways and generate a nuanced summary for you, bigger
|
||||
is better. However, even if your production system needs an LLM for some of the
|
||||
task, that doesn't mean you need an LLM for all of it. Maybe you want to use a
|
||||
cheap text classification model to help you find the texts to summarize, or
|
||||
maybe you want to add a rule-based system to sanity check the output of the
|
||||
summary. These before-and-after tasks are much easier with a mature and
|
||||
well-thought-out library, which is exactly what spaCy provides.
|
||||
|
||||
## Install {id="install"}
|
||||
|
||||
`spacy-llm` will be installed automatically in future spaCy versions. For now,
|
||||
you can run the following in the same virtual environment where you already have
|
||||
`spacy` [installed](/usage).
|
||||
|
||||
> ⚠️ This package is still experimental and it is possible that changes made to
|
||||
> the interface will be breaking in minor version updates.
|
||||
|
||||
```bash
|
||||
python -m pip install spacy-llm
|
||||
```
|
||||
|
||||
## Usage {id="usage"}
|
||||
|
||||
The task and the model have to be supplied to the `llm` pipeline component using
|
||||
the [config system](/api/data-formats#config). This package provides various
|
||||
built-in functionality, as detailed in the [API](#-api) documentation.
|
||||
|
||||
### Example 1: Add a text classifier using a GPT-3 model from OpenAI {id="example-1"}
|
||||
|
||||
Create a new API key from openai.com or fetch an existing one, and ensure the
|
||||
keys are set as environmental variables. For more background information, see
|
||||
the [OpenAI](/api/large-language-models#gpt-3-5) section.
|
||||
|
||||
Create a config file `config.cfg` containing at least the following (or see the
|
||||
full example
|
||||
[here](https://github.com/explosion/spacy-llm/tree/main/usage_examples/textcat_openai)):
|
||||
|
||||
```ini
|
||||
[nlp]
|
||||
lang = "en"
|
||||
pipeline = ["llm"]
|
||||
|
||||
[components]
|
||||
|
||||
[components.llm]
|
||||
factory = "llm"
|
||||
|
||||
[components.llm.task]
|
||||
@llm_tasks = "spacy.TextCat.v2"
|
||||
labels = ["COMPLIMENT", "INSULT"]
|
||||
|
||||
[components.llm.model]
|
||||
@llm_models = "spacy.GPT-3-5.v1"
|
||||
config = {"temperature": 0.0}
|
||||
```
|
||||
|
||||
Now run:
|
||||
|
||||
```python
|
||||
from spacy_llm.util import assemble
|
||||
|
||||
nlp = assemble("config.cfg")
|
||||
doc = nlp("You look gorgeous!")
|
||||
print(doc.cats)
|
||||
```
|
||||
|
||||
### Example 2: Add NER using an open-source model through Hugging Face {id="example-2"}
|
||||
|
||||
To run this example, ensure that you have a GPU enabled, and `transformers`,
|
||||
`torch` and CUDA installed. For more background information, see the
|
||||
[DollyHF](/api/large-language-models#dolly) section.
|
||||
|
||||
Create a config file `config.cfg` containing at least the following (or see the
|
||||
full example
|
||||
[here](https://github.com/explosion/spacy-llm/tree/main/usage_examples/ner_dolly)):
|
||||
|
||||
```ini
|
||||
[nlp]
|
||||
lang = "en"
|
||||
pipeline = ["llm"]
|
||||
|
||||
[components]
|
||||
|
||||
[components.llm]
|
||||
factory = "llm"
|
||||
|
||||
[components.llm.task]
|
||||
@llm_tasks = "spacy.NER.v3"
|
||||
labels = ["PERSON", "ORGANISATION", "LOCATION"]
|
||||
|
||||
[components.llm.model]
|
||||
@llm_models = "spacy.Dolly.v1"
|
||||
# For better performance, use dolly-v2-12b instead
|
||||
name = "dolly-v2-3b"
|
||||
```
|
||||
|
||||
Now run:
|
||||
|
||||
```python
|
||||
from spacy_llm.util import assemble
|
||||
|
||||
nlp = assemble("config.cfg")
|
||||
doc = nlp("Jack and Jill rode up the hill in Les Deux Alpes")
|
||||
print([(ent.text, ent.label_) for ent in doc.ents])
|
||||
```
|
||||
|
||||
Note that Hugging Face will download the `"databricks/dolly-v2-3b"` model the
|
||||
first time you use it. You can
|
||||
[define the cached directory](https://huggingface.co/docs/huggingface_hub/main/en/guides/manage-cache)
|
||||
by setting the environmental variable `HF_HOME`. Also, you can upgrade the model
|
||||
to be `"databricks/dolly-v2-12b"` for better performance.
|
||||
|
||||
### Example 3: Create the component directly in Python {id="example-3"}
|
||||
|
||||
The `llm` component behaves as any other component does, and there are
|
||||
[task-specific components](/api/large-language-models#config) defined to help
|
||||
you hit the ground running with a reasonable built-in task implementation.
|
||||
|
||||
```python
|
||||
import spacy
|
||||
|
||||
nlp = spacy.blank("en")
|
||||
llm_ner = nlp.add_pipe("llm_ner")
|
||||
llm_ner.add_label("PERSON")
|
||||
llm_ner.add_label("LOCATION")
|
||||
nlp.initialize()
|
||||
doc = nlp("Jack and Jill rode up the hill in Les Deux Alpes")
|
||||
print([(ent.text, ent.label_) for ent in doc.ents])
|
||||
```
|
||||
|
||||
Note that for efficient usage of resources, typically you would use
|
||||
[`nlp.pipe(docs)`](/api/language#pipe) with a batch, instead of calling
|
||||
`nlp(doc)` with a single document.
|
||||
|
||||
### Example 4: Implement your own custom task {id="example-4"}
|
||||
|
||||
To write a [`task`](#tasks), you need to implement two functions:
|
||||
`generate_prompts` that takes a list of [`Doc`](/api/doc) objects and transforms
|
||||
them into a list of prompts, and `parse_responses` that transforms the LLM
|
||||
outputs into annotations on the [`Doc`](/api/doc), e.g. entity spans, text
|
||||
categories and more.
|
||||
|
||||
To register your custom task, decorate a factory function using the
|
||||
`spacy_llm.registry.llm_tasks` decorator with a custom name that you can refer
|
||||
to in your config.
|
||||
|
||||
> 📖 For more details, see the
|
||||
> [**usage example on writing your own task**](https://github.com/explosion/spacy-llm/tree/main/usage_examples#writing-your-own-task)
|
||||
|
||||
```python
|
||||
from typing import Iterable, List
|
||||
from spacy.tokens import Doc
|
||||
from spacy_llm.registry import registry
|
||||
from spacy_llm.util import split_labels
|
||||
|
||||
|
||||
@registry.llm_tasks("my_namespace.MyTask.v1")
|
||||
def make_my_task(labels: str, my_other_config_val: float) -> "MyTask":
|
||||
labels_list = split_labels(labels)
|
||||
return MyTask(labels=labels_list, my_other_config_val=my_other_config_val)
|
||||
|
||||
|
||||
class MyTask:
|
||||
def __init__(self, labels: List[str], my_other_config_val: float):
|
||||
...
|
||||
|
||||
def generate_prompts(self, docs: Iterable[Doc]) -> Iterable[str]:
|
||||
...
|
||||
|
||||
def parse_responses(
|
||||
self, docs: Iterable[Doc], responses: Iterable[str]
|
||||
) -> Iterable[Doc]:
|
||||
...
|
||||
```
|
||||
|
||||
```ini
|
||||
# config.cfg (excerpt)
|
||||
[components.llm.task]
|
||||
@llm_tasks = "my_namespace.MyTask.v1"
|
||||
labels = LABEL1,LABEL2,LABEL3
|
||||
my_other_config_val = 0.3
|
||||
```
|
||||
|
||||
## Logging {id="logging"}
|
||||
|
||||
spacy-llm has a built-in logger that can log the prompt sent to the LLM as well
|
||||
as its raw response. This logger uses the debug level and by default has a
|
||||
`logging.NullHandler()` configured.
|
||||
|
||||
In order to use this logger, you can setup a simple handler like this:
|
||||
|
||||
```python
|
||||
import logging
|
||||
import spacy_llm
|
||||
|
||||
|
||||
spacy_llm.logger.addHandler(logging.StreamHandler())
|
||||
spacy_llm.logger.setLevel(logging.DEBUG)
|
||||
```
|
||||
|
||||
> NOTE: Any `logging` handler will work here so you probably want to use some
|
||||
> sort of rotating `FileHandler` as the generated prompts can be quite long,
|
||||
> especially for tasks with few-shot examples.
|
||||
|
||||
Then when using the pipeline you'll be able to view the prompt and response.
|
||||
|
||||
E.g. with the config and code from [Example 1](#example-1) above:
|
||||
|
||||
```python
|
||||
from spacy_llm.util import assemble
|
||||
|
||||
|
||||
nlp = assemble("config.cfg")
|
||||
doc = nlp("You look gorgeous!")
|
||||
print(doc.cats)
|
||||
```
|
||||
|
||||
You will see `logging` output similar to:
|
||||
|
||||
```
|
||||
Generated prompt for doc: You look gorgeous!
|
||||
|
||||
You are an expert Text Classification system. Your task is to accept Text as input
|
||||
and provide a category for the text based on the predefined labels.
|
||||
|
||||
Classify the text below to any of the following labels: COMPLIMENT, INSULT
|
||||
The task is non-exclusive, so you can provide more than one label as long as
|
||||
they're comma-delimited. For example: Label1, Label2, Label3.
|
||||
Do not put any other text in your answer, only one or more of the provided labels with nothing before or after.
|
||||
If the text cannot be classified into any of the provided labels, answer `==NONE==`.
|
||||
|
||||
Here is the text that needs classification
|
||||
|
||||
|
||||
Text:
|
||||
'''
|
||||
You look gorgeous!
|
||||
'''
|
||||
|
||||
Model response for doc: You look gorgeous!
|
||||
COMPLIMENT
|
||||
```
|
||||
|
||||
`print(doc.cats)` to standard output should look like:
|
||||
|
||||
```
|
||||
{'COMPLIMENT': 1.0, 'INSULT': 0.0}
|
||||
```
|
||||
|
||||
## API {id="api"}
|
||||
|
||||
`spacy-llm` exposes an `llm` factory with
|
||||
[configurable settings](/api/large-language-models#config).
|
||||
|
||||
An `llm` component is defined by two main settings:
|
||||
|
||||
- A [**task**](#tasks), defining the prompt to send to the LLM as well as the
|
||||
functionality to parse the resulting response back into structured fields on
|
||||
the [Doc](/api/doc) objects.
|
||||
- A [**model**](#models) defining the model to use and how to connect to it.
|
||||
Note that `spacy-llm` supports both access to external APIs (such as OpenAI)
|
||||
as well as access to self-hosted open-source LLMs (such as using Dolly through
|
||||
Hugging Face).
|
||||
|
||||
Moreover, `spacy-llm` exposes a customizable [**caching**](#cache) functionality
|
||||
to avoid running the same document through an LLM service (be it local or
|
||||
through a REST API) more than once.
|
||||
|
||||
Finally, you can choose to save a stringified version of LLM prompts/responses
|
||||
within the `Doc.user_data["llm_io"]` attribute by setting `save_io` to `True`.
|
||||
`Doc.user_data["llm_io"]` is a dictionary containing one entry for every LLM
|
||||
component within the `nlp` pipeline. Each entry is itself a dictionary, with two
|
||||
keys: `prompt` and `response`.
|
||||
|
||||
A note on `validate_types`: by default, `spacy-llm` checks whether the
|
||||
signatures of the `model` and `task` callables are consistent with each other
|
||||
and emits a warning if they don't. `validate_types` can be set to `False` if you
|
||||
want to disable this behavior.
|
||||
|
||||
### Tasks {id="tasks"}
|
||||
|
||||
A _task_ defines an NLP problem or question, that will be sent to the LLM via a
|
||||
prompt. Further, the task defines how to parse the LLM's responses back into
|
||||
structured information. All tasks are registered in the `llm_tasks` registry.
|
||||
|
||||
Practically speaking, a task should adhere to the `Protocol` named `LLMTask`
|
||||
defined in
|
||||
[`ty.py`](https://github.com/explosion/spacy-llm/blob/main/spacy_llm/ty.py). It
|
||||
needs to define a `generate_prompts` function and a `parse_responses` function.
|
||||
|
||||
Tasks may support prompt sharding (for more info see the API docs on
|
||||
[sharding](/api/large-language-models#task-sharding) and
|
||||
[non-sharding](/api/large-language-models#task-nonsharding) tasks). The function
|
||||
signatures for `generate_prompts` and `parse_responses` depend on whether they
|
||||
do.
|
||||
|
||||
For tasks **not supporting** sharding:
|
||||
|
||||
| Task | Description | |
|
||||
| --------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | --- |
|
||||
| [`task.generate_prompts`](/api/large-language-models#task-nonsharding-generate-prompts) | Takes a collection of documents, and returns a collection of prompts, which can be of type `Any`. |
|
||||
| [`task.parse_responses`](/api/large-language-models#task-nonsharding-parse-responses) | Takes a collection of LLM responses and the original documents, parses the responses into structured information, and sets the annotations on the documents. |
|
||||
|
||||
For tasks **supporting** sharding:
|
||||
|
||||
| Task | Description | |
|
||||
| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --- |
|
||||
| [`task.generate_prompts`](/api/large-language-models#task-sharding-generate-prompts) | Takes a collection of documents, and returns a collection of collections of prompt shards, which can be of type `Any`. |
|
||||
| [`task.parse_responses`](/api/large-language-models#task-sharding-parse-responses) | Takes a collection of collections of LLM responses (one per prompt shard) and the original documents, parses the responses into structured information, sets the annotations on the doc shards, and merges those doc shards back into a single doc instance. |
|
||||
|
||||
Moreover, the task may define an optional [`scorer` method](/api/scorer#score).
|
||||
It should accept an iterable of `Example` objects as input and return a score
|
||||
dictionary. If the `scorer` method is defined, `spacy-llm` will call it to
|
||||
evaluate the component.
|
||||
|
||||
| Component | Description |
|
||||
| ----------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
|
||||
| [`spacy.EntityLinker.v1`](/api/large-language-models#el-v1) | The entity linking task prompts the model to link all entities in a given text to entries in a knowledge base. |
|
||||
| [`spacy.Summarization.v1`](/api/large-language-models#summarization-v1) | The summarization task prompts the model for a concise summary of the provided text. |
|
||||
| [`spacy.NER.v3`](/api/large-language-models#ner-v3) | Implements Chain-of-Thought reasoning for NER extraction - obtains higher accuracy than v1 or v2. |
|
||||
| [`spacy.NER.v2`](/api/large-language-models#ner-v2) | Builds on v1 and additionally supports defining the provided labels with explicit descriptions. |
|
||||
| [`spacy.NER.v1`](/api/large-language-models#ner-v1) | The original version of the built-in NER task supports both zero-shot and few-shot prompting. |
|
||||
| [`spacy.SpanCat.v3`](/api/large-language-models#spancat-v3) | Adaptation of the v3 NER task to support overlapping entities and store its annotations in `doc.spans`. |
|
||||
| [`spacy.SpanCat.v2`](/api/large-language-models#spancat-v2) | Adaptation of the v2 NER task to support overlapping entities and store its annotations in `doc.spans`. |
|
||||
| [`spacy.SpanCat.v1`](/api/large-language-models#spancat-v1) | Adaptation of the v1 NER task to support overlapping entities and store its annotations in `doc.spans`. |
|
||||
| [`spacy.REL.v1`](/api/large-language-models#rel-v1) | Relation Extraction task supporting both zero-shot and few-shot prompting. |
|
||||
| [`spacy.TextCat.v3`](/api/large-language-models#textcat-v3) | Version 3 builds on v2 and allows setting definitions of labels. |
|
||||
| [`spacy.TextCat.v2`](/api/large-language-models#textcat-v2) | Version 2 builds on v1 and includes an improved prompt template. |
|
||||
| [`spacy.TextCat.v1`](/api/large-language-models#textcat-v1) | Version 1 of the built-in TextCat task supports both zero-shot and few-shot prompting. |
|
||||
| [`spacy.Lemma.v1`](/api/large-language-models#lemma-v1) | Lemmatizes the provided text and updates the `lemma_` attribute of the tokens accordingly. |
|
||||
| [`spacy.Raw.v1`](/api/large-language-models#raw-v1) | Executes raw doc content as prompt to LLM. |
|
||||
| [`spacy.Sentiment.v1`](/api/large-language-models#sentiment-v1) | Performs sentiment analysis on provided texts. |
|
||||
| [`spacy.Translation.v1`](/api/large-language-models#translation-v1) | Translates doc content into the specified target language. |
|
||||
| [`spacy.NoOp.v1`](/api/large-language-models#noop-v1) | This task is only useful for testing - it tells the LLM to do nothing, and does not set any fields on the `docs`. |
|
||||
|
||||
#### Providing examples for few-shot prompts {id="few-shot-prompts"}
|
||||
|
||||
All built-in tasks support few-shot prompts, i. e. including examples in a
|
||||
prompt. Examples can be supplied in two ways: (1) as a separate file containing
|
||||
only examples or (2) by initializing `llm` with a `get_examples()` callback
|
||||
(like any other pipeline component).
|
||||
|
||||
##### (1) Few-shot example file
|
||||
|
||||
A file containing examples for few-shot prompting can be configured like this:
|
||||
|
||||
```ini
|
||||
[components.llm.task]
|
||||
@llm_tasks = "spacy.NER.v2"
|
||||
labels = PERSON,ORGANISATION,LOCATION
|
||||
[components.llm.task.examples]
|
||||
@misc = "spacy.FewShotReader.v1"
|
||||
path = "ner_examples.yml"
|
||||
```
|
||||
|
||||
The supplied file has to conform to the format expected by the required task
|
||||
(see the task documentation further down).
|
||||
|
||||
##### (2) Initializing the `llm` component with a `get_examples()` callback
|
||||
|
||||
Alternatively, you can initialize your `nlp` pipeline by providing a
|
||||
`get_examples` callback for [`nlp.initialize`](/api/language#initialize) and
|
||||
setting `n_prompt_examples` to a positive number to automatically fetch a few
|
||||
examples for few-shot learning. Set `n_prompt_examples` to `-1` to use all
|
||||
examples as part of the few-shot learning prompt.
|
||||
|
||||
```ini
|
||||
[initialize.components.llm]
|
||||
n_prompt_examples = 3
|
||||
```
|
||||
|
||||
### Model {id="models"}
|
||||
|
||||
A _model_ defines which LLM model to query, and how to query it. It can be a
|
||||
simple function taking a collection of prompts (consistent with the output type
|
||||
of `task.generate_prompts()`) and returning a collection of responses
|
||||
(consistent with the expected input of `parse_responses`). Generally speaking,
|
||||
it's a function of type `Callable[[Iterable[Any]], Iterable[Any]]`, but specific
|
||||
implementations can have other signatures, like
|
||||
`Callable[[Iterable[str]], Iterable[str]]`.
|
||||
|
||||
All built-in models are registered in `llm_models`. If no model is specified,
|
||||
the repo currently connects to the `OpenAI` API by default using REST, and
|
||||
accesses the `"gpt-3.5-turbo"` model.
|
||||
|
||||
Currently three different approaches to use LLMs are supported:
|
||||
|
||||
1. `spacy-llm`s native REST interface. This is the default for all hosted models
|
||||
(e. g. OpenAI, Cohere, Anthropic, ...).
|
||||
2. A HuggingFace integration that allows to run a limited set of HF models
|
||||
locally.
|
||||
3. A LangChain integration that allows to run any model supported by LangChain
|
||||
(hosted or locally).
|
||||
|
||||
Approaches 1. and 2 are the default for hosted model and local models,
|
||||
respectively. Alternatively you can use LangChain to access hosted or local
|
||||
models by specifying one of the models registered with the `langchain.` prefix.
|
||||
|
||||
<Infobox>
|
||||
_Why LangChain if there are also are native REST and HuggingFace interfaces? When should I use what?_
|
||||
|
||||
Third-party libraries like `langchain` focus on prompt management, integration
|
||||
of many different LLM APIs, and other related features such as conversational
|
||||
memory or agents. `spacy-llm` on the other hand emphasizes features we consider
|
||||
useful in the context of NLP pipelines utilizing LLMs to process documents
|
||||
(mostly) independent from each other. It makes sense that the feature sets of
|
||||
such third-party libraries and `spacy-llm` aren't identical - and users might
|
||||
want to take advantage of features not available in `spacy-llm`.
|
||||
|
||||
The advantage of implementing our own REST and HuggingFace integrations is that
|
||||
we can ensure a larger degree of stability and robustness, as we can guarantee
|
||||
backwards-compatibility and more smoothly integrated error handling.
|
||||
|
||||
If however there are features or APIs not natively covered by `spacy-llm`, it's
|
||||
trivial to utilize LangChain to cover this - and easy to customize the prompting
|
||||
mechanism, if so required.
|
||||
|
||||
</Infobox>
|
||||
|
||||
<Infobox variant="warning">
|
||||
Note that when using hosted services, you have to ensure that the [proper API
|
||||
keys](/api/large-language-models#api-keys) are set as environment variables as described by the corresponding
|
||||
provider's documentation.
|
||||
|
||||
</Infobox>
|
||||
|
||||
| Model | Description |
|
||||
| ----------------------------------------------------------------------- | ---------------------------------------------- |
|
||||
| [`spacy.GPT-4.v2`](/api/large-language-models#models-rest) | OpenAI’s `gpt-4` model family. |
|
||||
| [`spacy.GPT-3-5.v2`](/api/large-language-models#models-rest) | OpenAI’s `gpt-3-5` model family. |
|
||||
| [`spacy.Text-Davinci.v2`](/api/large-language-models#models-rest) | OpenAI’s `text-davinci` model family. |
|
||||
| [`spacy.Code-Davinci.v2`](/api/large-language-models#models-rest) | OpenAI’s `code-davinci` model family. |
|
||||
| [`spacy.Text-Curie.v2`](/api/large-language-models#models-rest) | OpenAI’s `text-curie` model family. |
|
||||
| [`spacy.Text-Babbage.v2`](/api/large-language-models#models-rest) | OpenAI’s `text-babbage` model family. |
|
||||
| [`spacy.Text-Ada.v2`](/api/large-language-models#models-rest) | OpenAI’s `text-ada` model family. |
|
||||
| [`spacy.Davinci.v2`](/api/large-language-models#models-rest) | OpenAI’s `davinci` model family. |
|
||||
| [`spacy.Curie.v2`](/api/large-language-models#models-rest) | OpenAI’s `curie` model family. |
|
||||
| [`spacy.Babbage.v2`](/api/large-language-models#models-rest) | OpenAI’s `babbage` model family. |
|
||||
| [`spacy.Ada.v2`](/api/large-language-models#models-rest) | OpenAI’s `ada` model family. |
|
||||
| [`spacy.Azure.v1`](/api/large-language-models#models-rest) | Azure's OpenAI models. |
|
||||
| [`spacy.Command.v1`](/api/large-language-models#models-rest) | Cohere’s `command` model family. |
|
||||
| [`spacy.Claude-2.v1`](/api/large-language-models#models-rest) | Anthropic’s `claude-2` model family. |
|
||||
| [`spacy.Claude-1.v1`](/api/large-language-models#models-rest) | Anthropic’s `claude-1` model family. |
|
||||
| [`spacy.Claude-instant-1.v1`](/api/large-language-models#models-rest) | Anthropic’s `claude-instant-1` model family. |
|
||||
| [`spacy.Claude-instant-1-1.v1`](/api/large-language-models#models-rest) | Anthropic’s `claude-instant-1.1` model family. |
|
||||
| [`spacy.Claude-1-0.v1`](/api/large-language-models#models-rest) | Anthropic’s `claude-1.0` model family. |
|
||||
| [`spacy.Claude-1-2.v1`](/api/large-language-models#models-rest) | Anthropic’s `claude-1.2` model family. |
|
||||
| [`spacy.Claude-1-3.v1`](/api/large-language-models#models-rest) | Anthropic’s `claude-1.3` model family. |
|
||||
| [`spacy.PaLM.v1`](/api/large-language-models#models-rest) | Google’s `PaLM` model family. |
|
||||
| [`spacy.Dolly.v1`](/api/large-language-models#models-hf) | Dolly models through HuggingFace. |
|
||||
| [`spacy.Falcon.v1`](/api/large-language-models#models-hf) | Falcon models through HuggingFace. |
|
||||
| [`spacy.Mistral.v1`](/api/large-language-models#models-hf) | Mistral models through HuggingFace. |
|
||||
| [`spacy.Llama2.v1`](/api/large-language-models#models-hf) | Llama2 models through HuggingFace. |
|
||||
| [`spacy.StableLM.v1`](/api/large-language-models#models-hf) | StableLM models through HuggingFace. |
|
||||
| [`spacy.OpenLLaMA.v1`](/api/large-language-models#models-hf) | OpenLLaMA models through HuggingFace. |
|
||||
| [LangChain models](/api/large-language-models#langchain-models) | LangChain models for API retrieval. |
|
||||
|
||||
Note that the chat models variants of Llama 2 are currently not supported. This
|
||||
is because they need a particular prompting setup and don't add any discernible
|
||||
benefits in the use case of `spacy-llm` (i. e. no interactive chat) compared to
|
||||
the completion model variants.
|
||||
|
||||
### Cache {id="cache"}
|
||||
|
||||
Interacting with LLMs, either through an external API or a local instance, is
|
||||
costly. Since developing an NLP pipeline generally means a lot of exploration
|
||||
and prototyping, `spacy-llm` implements a built-in
|
||||
[cache](/api/large-language-models#cache) to avoid reprocessing the same
|
||||
documents at each run that keeps batches of documents stored on disk.
|
||||
|
||||
### Various functions {id="various-functions"}
|
||||
|
||||
| Function | Description |
|
||||
| ----------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| [`spacy.FewShotReader.v1`](/api/large-language-models#fewshotreader-v1) | This function is registered in spaCy's `misc` registry, and reads in examples from a `.yml`, `.yaml`, `.json` or `.jsonl` file. It uses [`srsly`](https://github.com/explosion/srsly) to read in these files and parses them depending on the file extension. |
|
||||
| [`spacy.FileReader.v1`](/api/large-language-models#filereader-v1) | This function is registered in spaCy's `misc` registry, and reads a file provided to the `path` to return a `str` representation of its contents. This function is typically used to read [Jinja](https://jinja.palletsprojects.com/en/3.1.x/) files containing the prompt template. |
|
||||
| [Normalizer functions](/api/large-language-models#normalizer-functions) | These functions provide simple normalizations for string comparisons, e.g. between a list of specified labels and a label given in the raw text of the LLM response. |
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,131 @@
|
||||
---
|
||||
title: Memory Management
|
||||
teaser: Managing Memory for persistent services
|
||||
version: 3.8
|
||||
menu:
|
||||
- ['Memory Zones', 'memoryzones']
|
||||
- ['Clearing Doc attributes', 'doc-attrs']
|
||||
---
|
||||
|
||||
spaCy maintains a few internal caches that improve speed,
|
||||
but cause memory to increase slightly over time. If you're
|
||||
running a batch process that you don't need to be long-lived,
|
||||
the increase in memory usage generally isn't a problem.
|
||||
However, if you're running spaCy inside a web service, you'll
|
||||
often want spaCy's memory usage to stay consistent. Transformer
|
||||
models can also run into memory problems sometimes, especially when
|
||||
used on a GPU.
|
||||
|
||||
## Memory zones {id="memoryzones"}
|
||||
|
||||
You can tell spaCy to free data from its internal caches (especially the
|
||||
[`Vocab`](/api/vocab)) using the [`Language.memory_zone`](/api/language#memory_zone) context manager. Enter
|
||||
the contextmanager and process your text within it, and spaCy will
|
||||
**reset its internal caches** (freeing up the associated memory) at the
|
||||
end of the block. spaCy objects created inside the memory zone must
|
||||
not be accessed once the memory zone is finished.
|
||||
|
||||
```python
|
||||
### Using memory zones
|
||||
from collections import Counter
|
||||
|
||||
def count_words(nlp, texts):
|
||||
counts = Counter()
|
||||
with nlp.memory_zone():
|
||||
for doc in nlp.pipe(texts):
|
||||
for token in doc:
|
||||
counts[token.text] += 1
|
||||
return counts
|
||||
```
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
Exiting the memory-zone invalidates all `Doc`, `Token`, `Span` and `Lexeme`
|
||||
objects that were created within it. If you access these objects
|
||||
after the memory zone exits, you may encounter a segmentation fault
|
||||
due to invalid memory access.
|
||||
|
||||
</Infobox>
|
||||
|
||||
spaCy needs the memory zone contextmanager because the processing pipeline
|
||||
can't keep track of which [`Doc`](/api/doc) objects are referring to data in the shared
|
||||
[`Vocab`](/api/vocab) cache. For instance, when spaCy encounters a new word, a new [`Lexeme`](/api/lexeme)
|
||||
entry is stored in the `Vocab`, and the `Doc` object points to this shared
|
||||
data. When the `Doc` goes out of scope, the `Vocab` has no way of knowing that
|
||||
this `Lexeme` is no longer in use.
|
||||
|
||||
The memory zone solves this problem by
|
||||
allowing you to tell the processing pipeline that all data created
|
||||
between two points is no longer in use. It is up to the you to honor
|
||||
this agreement. If you access objects that are supposed to no longer be in
|
||||
use, you may encounter a segmentation fault due to invalid memory access.
|
||||
|
||||
A common use case for memory zones will be **within a web service**. The processing
|
||||
pipeline can be loaded once, either as a context variable or a global, and each
|
||||
request can be handled within a memory zone:
|
||||
|
||||
```python
|
||||
### Memory zones with FastAPI {highlight="10,23"}
|
||||
from fastapi import FastAPI, APIRouter, Depends, Request
|
||||
import spacy
|
||||
from spacy.language import Language
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def make_app():
|
||||
app = FastAPI()
|
||||
app.state.NLP = spacy.load("en_core_web_sm")
|
||||
app.include_router(router)
|
||||
return app
|
||||
|
||||
|
||||
def get_nlp(request: Request) -> Language:
|
||||
return request.app.state.NLP
|
||||
|
||||
|
||||
@router.post("/parse")
|
||||
def parse_texts(
|
||||
*, text_batch: list[str], nlp: Language = Depends(get_nlp)
|
||||
) -> list[dict]:
|
||||
with nlp.memory_zone():
|
||||
# Put the spaCy call within a separate function, so we can't
|
||||
# leak the Doc objects outside the scope of the memory zone.
|
||||
output = _process_text(nlp, text_batch)
|
||||
return output
|
||||
|
||||
|
||||
def _process_text(nlp: Language, texts: list[str]) -> list[dict]:
|
||||
# Call spaCy, and transform the output into our own data
|
||||
# structures. This function is called from inside a memory
|
||||
# zone, so must not return the spaCy objects.
|
||||
docs = list(nlp.pipe(texts))
|
||||
return [
|
||||
{
|
||||
"tokens": [{"text": t.text} for t in doc],
|
||||
"entities": [
|
||||
{"start": e.start, "end": e.end, "label": e.label_} for e in doc.ents
|
||||
],
|
||||
}
|
||||
for doc in docs
|
||||
]
|
||||
|
||||
|
||||
app = make_app()
|
||||
```
|
||||
|
||||
## Clearing transformer tensors and other Doc attributes {id="doc-attrs"}
|
||||
|
||||
The [`Transformer`](/api/transformer) and [`Tok2Vec`](/api/tok2vec) components set intermediate values onto the `Doc`
|
||||
object during parsing. This can cause GPU memory to be exhausted if many `Doc`
|
||||
objects are kept in memory together.
|
||||
|
||||
To resolve this, you can add the [`doc_cleaner`](/api/pipeline-functions#doc_cleaner) component to your pipeline. By default
|
||||
this will clean up the [`Doc._.trf_data`](/api/transformer#custom_attributes) extension attribute and the [`Doc.tensor`](/api/doc#attributes) attribute.
|
||||
You can have it clean up other intermediate extension attributes you use in custom
|
||||
pipeline components as well.
|
||||
|
||||
```python
|
||||
### Adding the doc_cleaner
|
||||
nlp.add_pipe("doc_cleaner", config={"attrs": {"tensor": None}})
|
||||
```
|
||||
@@ -0,0 +1,570 @@
|
||||
---
|
||||
title: Models & Languages
|
||||
next: usage/facts-figures
|
||||
menu:
|
||||
- ['Quickstart', 'quickstart']
|
||||
- ['Language Support', 'languages']
|
||||
- ['Installation & Usage', 'download']
|
||||
- ['Production Use', 'production']
|
||||
---
|
||||
|
||||
spaCy's trained pipelines can be installed as **Python packages**. This means
|
||||
that they're a component of your application, just like any other module.
|
||||
They're versioned and can be defined as a dependency in your `requirements.txt`.
|
||||
Trained pipelines can be installed from a download URL or a local directory,
|
||||
manually or via [pip](https://pypi.python.org/pypi/pip). Their data can be
|
||||
located anywhere on your file system.
|
||||
|
||||
> #### Important note
|
||||
>
|
||||
> If you're upgrading to spaCy v3.x, you need to **download the new pipeline
|
||||
> packages**. If you've trained your own pipelines, you need to **retrain** them
|
||||
> after updating spaCy.
|
||||
|
||||
## Quickstart {hidden="true"}
|
||||
|
||||
<QuickstartModels
|
||||
title="Quickstart"
|
||||
id="quickstart"
|
||||
description="Install a default trained pipeline package, get the code to load it from within spaCy and an example to test it. For more options, see the section on available packages below."
|
||||
/>
|
||||
|
||||
### Usage note
|
||||
|
||||
> If lemmatization rules are available for your language, make sure to install
|
||||
> spaCy with the `lookups` option, or install
|
||||
> [`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data)
|
||||
> separately in the same environment:
|
||||
>
|
||||
> ```bash
|
||||
> $ pip install -U %%SPACY_PKG_NAME[lookups]%%SPACY_PKG_FLAGS
|
||||
> ```
|
||||
|
||||
If a trained pipeline is available for a language, you can download it using the
|
||||
[`spacy download`](/api/cli#download) command as shown above. In order to use
|
||||
languages that don't yet come with a trained pipeline, you have to import them
|
||||
directly, or use [`spacy.blank`](/api/top-level#spacy.blank):
|
||||
|
||||
```python
|
||||
from spacy.lang.yo import Yoruba
|
||||
nlp = Yoruba() # use directly
|
||||
nlp = spacy.blank("yo") # blank instance
|
||||
```
|
||||
|
||||
A blank pipeline is typically just a tokenizer. You might want to create a blank
|
||||
pipeline when you only need a tokenizer, when you want to add more components
|
||||
from scratch, or for testing purposes. Initializing the language object directly
|
||||
yields the same result as generating it using `spacy.blank()`. In both cases the
|
||||
default configuration for the chosen language is loaded, and no pretrained
|
||||
components will be available.
|
||||
|
||||
## Language support {id="languages"}
|
||||
|
||||
spaCy currently provides support for the following languages. You can help by
|
||||
improving the existing [language data](/usage/linguistic-features#language-data)
|
||||
and extending the tokenization patterns.
|
||||
[See here](https://github.com/explosion/spaCy/issues/3056) for details on how to
|
||||
contribute to development. Also see the
|
||||
[training documentation](/usage/training) for how to train your own pipelines on
|
||||
your data.
|
||||
|
||||
<Languages />
|
||||
|
||||
### Multi-language support {id="multi-language",version="2"}
|
||||
|
||||
> ```python
|
||||
> # Standard import
|
||||
> from spacy.lang.xx import MultiLanguage
|
||||
> nlp = MultiLanguage()
|
||||
>
|
||||
> # With lazy-loading
|
||||
> nlp = spacy.blank("xx")
|
||||
> ```
|
||||
|
||||
spaCy also supports pipelines trained on more than one language. This is
|
||||
especially useful for named entity recognition. The language ID used for
|
||||
multi-language or language-neutral pipelines is `xx`. The language class, a
|
||||
generic subclass containing only the base language data, can be found in
|
||||
[`lang/xx`](%%GITHUB_SPACY/spacy/lang/xx).
|
||||
|
||||
To train a pipeline using the neutral multi-language class, you can set
|
||||
`lang = "xx"` in your [training config](/usage/training#config). You can also
|
||||
\import the `MultiLanguage` class directly, or call
|
||||
[`spacy.blank("xx")`](/api/top-level#spacy.blank) for lazy-loading.
|
||||
|
||||
### Chinese language support {id="chinese",version="2.3"}
|
||||
|
||||
The Chinese language class supports three word segmentation options, `char`,
|
||||
`jieba` and `pkuseg`.
|
||||
|
||||
> #### Manual setup
|
||||
>
|
||||
> ```python
|
||||
> from spacy.lang.zh import Chinese
|
||||
>
|
||||
> # Character segmentation (default)
|
||||
> nlp = Chinese()
|
||||
> # Jieba
|
||||
> cfg = {"segmenter": "jieba"}
|
||||
> nlp = Chinese.from_config({"nlp": {"tokenizer": cfg}})
|
||||
> # PKUSeg with "mixed" model provided by pkuseg
|
||||
> cfg = {"segmenter": "pkuseg"}
|
||||
> nlp = Chinese.from_config({"nlp": {"tokenizer": cfg}})
|
||||
> nlp.tokenizer.initialize(pkuseg_model="mixed")
|
||||
> ```
|
||||
|
||||
```ini {title="config.cfg"}
|
||||
[nlp.tokenizer]
|
||||
@tokenizers = "spacy.zh.ChineseTokenizer"
|
||||
segmenter = "char"
|
||||
```
|
||||
|
||||
| Segmenter | Description |
|
||||
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `char` | **Character segmentation:** Character segmentation is the default segmentation option. It's enabled when you create a new `Chinese` language class or call `spacy.blank("zh")`. |
|
||||
| `jieba` | **Jieba:** to use [Jieba](https://github.com/fxsjy/jieba) for word segmentation, you can set the option `segmenter` to `"jieba"`. |
|
||||
| `pkuseg` | **PKUSeg**: As of spaCy v2.3.0, support for [PKUSeg](https://github.com/explosion/spacy-pkuseg) has been added to support better segmentation for Chinese OntoNotes and the provided [Chinese pipelines](/models/zh). Enable PKUSeg by setting tokenizer option `segmenter` to `"pkuseg"`. |
|
||||
|
||||
<Infobox title="Changed in v3.0" variant="warning">
|
||||
|
||||
In v3.0, the default word segmenter has switched from Jieba to character
|
||||
segmentation. Because the `pkuseg` segmenter depends on a model that can be
|
||||
loaded from a file, the model is loaded on
|
||||
[initialization](/usage/training#config-lifecycle) (typically before training).
|
||||
This ensures that your packaged Chinese model doesn't depend on a local path at
|
||||
runtime.
|
||||
|
||||
</Infobox>
|
||||
|
||||
<Accordion title="Details on spaCy's Chinese API">
|
||||
|
||||
The `initialize` method for the Chinese tokenizer class supports the following
|
||||
config settings for loading `pkuseg` models:
|
||||
|
||||
| Name | Description |
|
||||
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `pkuseg_model` | Name of a model provided by `spacy-pkuseg` or the path to a local model directory. ~~str~~ |
|
||||
| `pkuseg_user_dict` | Optional path to a file with one word per line which overrides the default `pkuseg` user dictionary. Defaults to `"default"`, the default provided dictionary. ~~str~~ |
|
||||
|
||||
The initialization settings are typically provided in the
|
||||
[training config](/usage/training#config) and the data is loaded in before
|
||||
training and serialized with the model. This allows you to load the data from a
|
||||
local path and save out your pipeline and config, without requiring the same
|
||||
local path at runtime. See the usage guide on the
|
||||
[config lifecycle](/usage/training#config-lifecycle) for more background on
|
||||
this.
|
||||
|
||||
```ini {title="config.cfg"}
|
||||
[initialize]
|
||||
|
||||
[initialize.tokenizer]
|
||||
pkuseg_model = "/path/to/model"
|
||||
pkuseg_user_dict = "default"
|
||||
```
|
||||
|
||||
You can also initialize the tokenizer for a blank language class by calling its
|
||||
`initialize` method:
|
||||
|
||||
```python {title="Examples"}
|
||||
# Initialize the pkuseg tokenizer
|
||||
cfg = {"segmenter": "pkuseg"}
|
||||
nlp = Chinese.from_config({"nlp": {"tokenizer": cfg}})
|
||||
|
||||
# Load spaCy's OntoNotes model
|
||||
nlp.tokenizer.initialize(pkuseg_model="spacy_ontonotes")
|
||||
|
||||
# Load pkuseg's "news" model
|
||||
nlp.tokenizer.initialize(pkuseg_model="news")
|
||||
|
||||
# Load local model
|
||||
nlp.tokenizer.initialize(pkuseg_model="/path/to/pkuseg_model")
|
||||
|
||||
# Override the user directory
|
||||
nlp.tokenizer.initialize(pkuseg_model="spacy_ontonotes", pkuseg_user_dict="/path/to/user_dict")
|
||||
```
|
||||
|
||||
You can also modify the user dictionary on-the-fly:
|
||||
|
||||
```python
|
||||
# Append words to user dict
|
||||
nlp.tokenizer.pkuseg_update_user_dict(["中国", "ABC"])
|
||||
|
||||
# Remove all words from user dict and replace with new words
|
||||
nlp.tokenizer.pkuseg_update_user_dict(["中国"], reset=True)
|
||||
|
||||
# Remove all words from user dict
|
||||
nlp.tokenizer.pkuseg_update_user_dict([], reset=True)
|
||||
```
|
||||
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="Details on trained and custom Chinese pipelines" spaced>
|
||||
|
||||
The [Chinese pipelines](/models/zh) provided by spaCy include a custom `pkuseg`
|
||||
model trained only on
|
||||
[Chinese OntoNotes 5.0](https://catalog.ldc.upenn.edu/LDC2013T19), since the
|
||||
models provided by `pkuseg` include data restricted to research use. For
|
||||
research use, `pkuseg` provides models for several different domains (`"mixed"`
|
||||
(equivalent to `"default"` from `pkuseg` packages), `"news"` `"web"`,
|
||||
`"medicine"`, `"tourism"`) and for other uses, `pkuseg` provides a simple
|
||||
[training API](https://github.com/explosion/spacy-pkuseg/blob/master/readme/readme_english.md#usage):
|
||||
|
||||
```python
|
||||
import spacy_pkuseg as pkuseg
|
||||
from spacy.lang.zh import Chinese
|
||||
|
||||
# Train pkuseg model
|
||||
pkuseg.train("train.utf8", "test.utf8", "/path/to/pkuseg_model")
|
||||
|
||||
# Load pkuseg model in spaCy Chinese tokenizer
|
||||
cfg = {"segmenter": "pkuseg"}
|
||||
nlp = Chinese.from_config({"nlp": {"tokenizer": cfg}})
|
||||
nlp.tokenizer.initialize(pkuseg_model="/path/to/pkuseg_model")
|
||||
```
|
||||
|
||||
</Accordion>
|
||||
|
||||
### Japanese language support {id="japanese",version="2.3"}
|
||||
|
||||
> #### Manual setup
|
||||
>
|
||||
> ```python
|
||||
> from spacy.lang.ja import Japanese
|
||||
>
|
||||
> # Load SudachiPy with split mode A (default)
|
||||
> nlp = Japanese()
|
||||
> # Load SudachiPy with split mode B
|
||||
> cfg = {"split_mode": "B"}
|
||||
> nlp = Japanese.from_config({"nlp": {"tokenizer": cfg}})
|
||||
> ```
|
||||
|
||||
The Japanese language class uses
|
||||
[SudachiPy](https://github.com/WorksApplications/SudachiPy) for word
|
||||
segmentation and part-of-speech tagging. The default Japanese language class and
|
||||
the provided Japanese pipelines use SudachiPy split mode `A`. The tokenizer
|
||||
config can be used to configure the split mode to `A`, `B` or `C`.
|
||||
|
||||
```ini {title="config.cfg"}
|
||||
[nlp.tokenizer]
|
||||
@tokenizers = "spacy.ja.JapaneseTokenizer"
|
||||
split_mode = "A"
|
||||
```
|
||||
|
||||
Extra information, such as reading, inflection form, and the SudachiPy
|
||||
normalized form, is available in `Token.morph`. For `B` or `C` split modes,
|
||||
subtokens are stored in `Doc.user_data["sub_tokens"]`.
|
||||
|
||||
<Infobox variant="warning">
|
||||
|
||||
If you run into errors related to `sudachipy`, which is currently under active
|
||||
development, we suggest downgrading to `sudachipy==0.4.9`, which is the version
|
||||
used for training the current [Japanese pipelines](/models/ja).
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Korean language support {id="korean"}
|
||||
|
||||
> #### mecab-ko tokenizer
|
||||
>
|
||||
> ```python
|
||||
> nlp = spacy.blank("ko")
|
||||
> ```
|
||||
|
||||
The default MeCab-based Korean tokenizer requires:
|
||||
|
||||
- [mecab-ko](https://bitbucket.org/eunjeon/mecab-ko/src/master/README.md)
|
||||
- [mecab-ko-dic](https://bitbucket.org/eunjeon/mecab-ko-dic)
|
||||
- [natto-py](https://github.com/buruzaemon/natto-py)
|
||||
|
||||
For some Korean datasets and tasks, the
|
||||
[rule-based tokenizer](/usage/linguistic-features#tokenization) is better-suited
|
||||
than MeCab. To configure a Korean pipeline with the rule-based tokenizer:
|
||||
|
||||
> #### Rule-based tokenizer
|
||||
>
|
||||
> ```python
|
||||
> config = {"nlp": {"tokenizer": {"@tokenizers": "spacy.Tokenizer.v1"}}}
|
||||
> nlp = spacy.blank("ko", config=config)
|
||||
> ```
|
||||
|
||||
```ini {title="config.cfg"}
|
||||
[nlp]
|
||||
lang = "ko"
|
||||
tokenizer = {"@tokenizers" = "spacy.Tokenizer.v1"}
|
||||
```
|
||||
|
||||
<Infobox>
|
||||
|
||||
The [Korean trained pipelines](/models/ko) use the rule-based tokenizer, so no
|
||||
additional dependencies are required.
|
||||
|
||||
</Infobox>
|
||||
|
||||
## Installing and using trained pipelines {id="download"}
|
||||
|
||||
The easiest way to download a trained pipeline is via spaCy's
|
||||
[`download`](/api/cli#download) command. It takes care of finding the
|
||||
best-matching package compatible with your spaCy installation.
|
||||
|
||||
> #### Important note for v3.0
|
||||
>
|
||||
> Note that as of spaCy v3.0, shortcut links like `en` that create (potentially
|
||||
> brittle) symlinks in your spaCy installation are **deprecated**. To download
|
||||
> and load an installed pipeline package, use its full name:
|
||||
>
|
||||
> ```diff
|
||||
> - python -m spacy download en
|
||||
> + python -m spacy download en_core_web_sm
|
||||
> ```
|
||||
>
|
||||
> ```diff
|
||||
> - nlp = spacy.load("en")
|
||||
> + nlp = spacy.load("en_core_web_sm")
|
||||
> ```
|
||||
|
||||
```bash
|
||||
# Download best-matching version of a package for your spaCy installation
|
||||
$ python -m spacy download en_core_web_sm
|
||||
|
||||
# Download exact package version
|
||||
$ python -m spacy download en_core_web_sm-3.0.0 --direct
|
||||
```
|
||||
|
||||
The download command will [install the package](/usage/models#download-pip) via
|
||||
pip and place the package in your `site-packages` directory.
|
||||
|
||||
```bash
|
||||
$ pip install -U %%SPACY_PKG_NAME%%SPACY_PKG_FLAGS
|
||||
$ python -m spacy download en_core_web_sm
|
||||
```
|
||||
|
||||
```python
|
||||
import spacy
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
doc = nlp("This is a sentence.")
|
||||
```
|
||||
|
||||
If you're in a **Jupyter notebook** or similar environment, you can use the `!`
|
||||
prefix to
|
||||
[execute commands](https://ipython.org/ipython-doc/3/interactive/tutorial.html#system-shell-commands).
|
||||
Make sure to **restart your kernel** or runtime after installation (just like
|
||||
you would when installing other Python packages) to make sure that the installed
|
||||
pipeline package can be found.
|
||||
|
||||
```bash
|
||||
!python -m spacy download en_core_web_sm
|
||||
```
|
||||
|
||||
### Installation via pip {id="download-pip"}
|
||||
|
||||
To download a trained pipeline directly using
|
||||
[pip](https://pypi.python.org/pypi/pip), point `pip install` to the URL or local
|
||||
path of the wheel file or archive. Installing the wheel is usually more
|
||||
efficient.
|
||||
|
||||
> #### Pipeline Package URLs {id="pipeline-urls"}
|
||||
>
|
||||
> Pretrained pipeline distributions are hosted on
|
||||
> [Github Releases](https://github.com/explosion/spacy-models/releases), and you
|
||||
> can find download links there, as well as on the model page. You can also get
|
||||
> URLs directly from the command line by using `spacy info` with the `--url`
|
||||
> flag, which may be useful for automation.
|
||||
>
|
||||
> ```bash
|
||||
> spacy info en_core_web_sm --url
|
||||
> ```
|
||||
>
|
||||
> This command will print the URL for the latest version of a pipeline
|
||||
> compatible with the version of spaCy you're using. Note that in order to look
|
||||
> up the compatibility information an internet connection is required.
|
||||
|
||||
```bash
|
||||
# With external URL
|
||||
$ pip install https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.0.0/en_core_web_sm-3.0.0-py3-none-any.whl
|
||||
$ pip install https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.0.0/en_core_web_sm-3.0.0.tar.gz
|
||||
|
||||
# Using spacy info to get the external URL
|
||||
$ pip install $(spacy info en_core_web_sm --url)
|
||||
|
||||
# With local file
|
||||
$ pip install /Users/you/en_core_web_sm-3.0.0-py3-none-any.whl
|
||||
$ pip install /Users/you/en_core_web_sm-3.0.0.tar.gz
|
||||
```
|
||||
|
||||
By default, this will install the pipeline package into your `site-packages`
|
||||
directory. You can then use `spacy.load` to load it via its package name or
|
||||
[import it](#usage-import) explicitly as a module. If you need to download
|
||||
pipeline packages as part of an automated process, we recommend using pip with a
|
||||
direct link, instead of relying on spaCy's [`download`](/api/cli#download)
|
||||
command.
|
||||
|
||||
You can also add the direct download link to your application's
|
||||
`requirements.txt`. For more details, see the section on
|
||||
[working with pipeline packages in production](#production).
|
||||
|
||||
### Manual download and installation {id="download-manual"}
|
||||
|
||||
In some cases, you might prefer downloading the data manually, for example to
|
||||
place it into a custom directory. You can download the package via your browser
|
||||
from the [latest releases](https://github.com/explosion/spacy-models/releases),
|
||||
or configure your own download script using the URL of the archive file. The
|
||||
archive consists of a package directory that contains another directory with the
|
||||
pipeline data.
|
||||
|
||||
```yaml {title="Directory structure",highlight="6"}
|
||||
└── en_core_web_md-3.0.0.tar.gz # downloaded archive
|
||||
├── setup.py # setup file for pip installation
|
||||
├── meta.json # copy of pipeline meta
|
||||
└── en_core_web_md # 📦 pipeline package
|
||||
├── __init__.py # init for pip installation
|
||||
└── en_core_web_md-3.0.0 # pipeline data
|
||||
├── config.cfg # pipeline config
|
||||
├── meta.json # pipeline meta
|
||||
└── ... # directories with component data
|
||||
```
|
||||
|
||||
You can place the **pipeline package directory** anywhere on your local file
|
||||
system.
|
||||
|
||||
### Installation from Python {id="download-python"}
|
||||
|
||||
Since the [`spacy download`](/api/cli#download) command installs the pipeline as
|
||||
a **Python package**, we always recommend running it from the command line, just
|
||||
like you install other Python packages with `pip install`. However, if you need
|
||||
to, or if you want to integrate the download process into another CLI command,
|
||||
you can also import and call the `download` function used by the CLI via Python.
|
||||
|
||||
<Infobox variant="warning">
|
||||
|
||||
Keep in mind that the `download` command installs a Python package into your
|
||||
environment. In order for it to be found after installation, you will need to
|
||||
**restart or reload** your Python process so that new packages are recognized.
|
||||
|
||||
</Infobox>
|
||||
|
||||
```python
|
||||
import spacy
|
||||
spacy.cli.download("en_core_web_sm")
|
||||
```
|
||||
|
||||
### Using trained pipelines with spaCy {id="usage"}
|
||||
|
||||
To load a pipeline package, use [`spacy.load`](/api/top-level#spacy.load) with
|
||||
the package name or a path to the data directory:
|
||||
|
||||
> #### Important note for v3.0
|
||||
>
|
||||
> Note that as of spaCy v3.0, shortcut links like `en` that create (potentially
|
||||
> brittle) symlinks in your spaCy installation are **deprecated**. To download
|
||||
> and load an installed pipeline package, use its full name:
|
||||
>
|
||||
> ```diff
|
||||
> - python -m spacy download en
|
||||
> + python -m spacy download en_core_web_sm
|
||||
> ```
|
||||
|
||||
```python
|
||||
import spacy
|
||||
nlp = spacy.load("en_core_web_sm") # load package "en_core_web_sm"
|
||||
nlp = spacy.load("/path/to/en_core_web_sm") # load package from a directory
|
||||
|
||||
doc = nlp("This is a sentence.")
|
||||
```
|
||||
|
||||
<Infobox title="Tip: Preview model info" emoji="💡">
|
||||
|
||||
You can use the [`info`](/api/cli#info) command or
|
||||
[`spacy.info()`](/api/top-level#spacy.info) method to print a pipeline package's
|
||||
meta data before loading it. Each `Language` object with a loaded pipeline also
|
||||
exposes the pipeline's meta data as the attribute `meta`. For example,
|
||||
`nlp.meta['version']` will return the package version.
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Importing pipeline packages as modules {id="usage-import"}
|
||||
|
||||
If you've installed a trained pipeline via [`spacy download`](/api/cli#download)
|
||||
or directly via pip, you can also `import` it and then call its `load()` method
|
||||
with no arguments:
|
||||
|
||||
```python {executable="true"}
|
||||
import en_core_web_sm
|
||||
|
||||
nlp = en_core_web_sm.load()
|
||||
doc = nlp("This is a sentence.")
|
||||
```
|
||||
|
||||
How you choose to load your trained pipelines ultimately depends on personal
|
||||
preference. However, **for larger code bases**, we usually recommend native
|
||||
imports, as this will make it easier to integrate pipeline packages with your
|
||||
existing build process, continuous integration workflow and testing framework.
|
||||
It'll also prevent you from ever trying to load a package that is not installed,
|
||||
as your code will raise an `ImportError` immediately, instead of failing
|
||||
somewhere down the line when calling `spacy.load()`. For more details, see the
|
||||
section on [working with pipeline packages in production](#production).
|
||||
|
||||
## Using trained pipelines in production {id="production"}
|
||||
|
||||
If your application depends on one or more trained pipeline packages, you'll
|
||||
usually want to integrate them into your continuous integration workflow and
|
||||
build process. While spaCy provides a range of useful helpers for downloading
|
||||
and loading pipeline packages, the underlying functionality is entirely based on
|
||||
native Python packaging. This allows your application to handle a spaCy pipeline
|
||||
like any other package dependency.
|
||||
|
||||
### Downloading and requiring package dependencies {id="models-download"}
|
||||
|
||||
spaCy's built-in [`download`](/api/cli#download) command is mostly intended as a
|
||||
convenient, interactive wrapper. It performs compatibility checks and prints
|
||||
detailed error messages and warnings. However, if you're downloading pipeline
|
||||
packages as part of an automated build process, this only adds an unnecessary
|
||||
layer of complexity. If you know which packages your application needs, you
|
||||
should be specifying them directly.
|
||||
|
||||
Because pipeline packages are valid Python packages, you can add them to your
|
||||
application's `requirements.txt`. If you're running your own internal PyPi
|
||||
installation, you can upload the pipeline packages there. pip's
|
||||
[requirements file format](https://pip.pypa.io/en/latest/reference/requirements-file-format/)
|
||||
supports both package names to download via a PyPi server, as well as
|
||||
[direct URLs](#pipeline-urls). For instance, you can specify the
|
||||
`en_core_web_sm` model for spaCy 3.7.x as follows:
|
||||
|
||||
```text {title="requirements.txt"}
|
||||
spacy>=3.0.0,<4.0.0
|
||||
en_core_web_sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.1/en_core_web_sm-3.7.1-py3-none-any.whl
|
||||
```
|
||||
|
||||
See the [list of models](https://spacy.io/models) for model download links for
|
||||
the current spaCy version.
|
||||
|
||||
All pipeline packages are versioned and specify their spaCy dependency. This
|
||||
ensures cross-compatibility and lets you specify exact version requirements for
|
||||
each pipeline. If you've [trained](/usage/training) your own pipeline, you can
|
||||
use the [`spacy package`](/api/cli#package) command to generate the required
|
||||
meta data and turn it into a loadable package.
|
||||
|
||||
### Loading and testing pipeline packages {id="models-loading"}
|
||||
|
||||
Pipeline packages are regular Python packages, so you can also import them as a
|
||||
package using Python's native `import` syntax, and then call the `load` method
|
||||
to load the data and return an `nlp` object:
|
||||
|
||||
```python
|
||||
import en_core_web_sm
|
||||
nlp = en_core_web_sm.load()
|
||||
```
|
||||
|
||||
In general, this approach is recommended for larger code bases, as it's more
|
||||
"native", and doesn't rely on spaCy's loader to resolve string names to
|
||||
packages. If a package can't be imported, Python will raise an `ImportError`
|
||||
immediately. And if a package is imported but not used, any linter will catch
|
||||
that.
|
||||
|
||||
Similarly, it'll give you more flexibility when writing tests that require
|
||||
loading pipelines. For example, instead of writing your own `try` and `except`
|
||||
logic around spaCy's loader, you can use
|
||||
[pytest](http://pytest.readthedocs.io/en/latest/)'s
|
||||
[`importorskip()`](https://docs.pytest.org/en/latest/builtin.html#_pytest.outcomes.importorskip)
|
||||
method to only run a test if a specific pipeline package or version is
|
||||
installed. Each pipeline package exposes a `__version__` attribute which you can
|
||||
also use to perform your own version compatibility checks before loading it.
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,777 @@
|
||||
---
|
||||
title: Saving and Loading
|
||||
menu:
|
||||
- ['Basics', 'basics']
|
||||
- ['Serializing Docs', 'docs']
|
||||
- ['Serialization Methods', 'serialization-methods']
|
||||
- ['Entry Points', 'entry-points']
|
||||
- ['Trained Pipelines', 'models']
|
||||
---
|
||||
|
||||
## Basics {id="basics",hidden="true"}
|
||||
|
||||
<Serialization101 />
|
||||
|
||||
### Serializing the pipeline {id="pipeline"}
|
||||
|
||||
When serializing the pipeline, keep in mind that this will only save out the
|
||||
**binary data for the individual components** to allow spaCy to restore them –
|
||||
not the entire objects. This is a good thing, because it makes serialization
|
||||
safe. But it also means that you have to take care of storing the config, which
|
||||
contains the pipeline configuration and all the relevant settings.
|
||||
|
||||
> #### Saving the meta and config
|
||||
>
|
||||
> The [`nlp.meta`](/api/language#meta) attribute is a JSON-serializable
|
||||
> dictionary and contains all pipeline meta information like the author and
|
||||
> license information. The [`nlp.config`](/api/language#config) attribute is a
|
||||
> dictionary containing the training configuration, pipeline component factories
|
||||
> and other settings. It is saved out with a pipeline as the `config.cfg`.
|
||||
|
||||
```python {title="Serialize"}
|
||||
config = nlp.config
|
||||
bytes_data = nlp.to_bytes()
|
||||
```
|
||||
|
||||
```python {title="Deserialize"}
|
||||
lang_cls = spacy.util.get_lang_class(config["nlp"]["lang"])
|
||||
nlp = lang_cls.from_config(config)
|
||||
nlp.from_bytes(bytes_data)
|
||||
```
|
||||
|
||||
This is also how spaCy does it under the hood when loading a pipeline: it loads
|
||||
the `config.cfg` containing the language and pipeline information, initializes
|
||||
the language class, creates and adds the pipeline components based on the config
|
||||
and _then_ loads in the binary data. You can read more about this process
|
||||
[here](/usage/processing-pipelines#pipelines).
|
||||
|
||||
## Serializing Doc objects efficiently {id="docs",version="2.2"}
|
||||
|
||||
If you're working with lots of data, you'll probably need to pass analyses
|
||||
between machines, either to use something like [Dask](https://dask.org) or
|
||||
[Spark](https://spark.apache.org), or even just to save out work to disk. Often
|
||||
it's sufficient to use the [`Doc.to_array`](/api/doc#to_array) functionality for
|
||||
this, and just serialize the numpy arrays – but other times you want a more
|
||||
general way to save and restore `Doc` objects.
|
||||
|
||||
The [`DocBin`](/api/docbin) class makes it easy to serialize and deserialize a
|
||||
collection of `Doc` objects together, and is much more efficient than calling
|
||||
[`Doc.to_bytes`](/api/doc#to_bytes) on each individual `Doc` object. You can
|
||||
also control what data gets saved, and you can merge pallets together for easy
|
||||
map/reduce-style processing.
|
||||
|
||||
```python {highlight="4,8,9,13,14"}
|
||||
import spacy
|
||||
from spacy.tokens import DocBin
|
||||
|
||||
doc_bin = DocBin(attrs=["LEMMA", "ENT_IOB", "ENT_TYPE"], store_user_data=True)
|
||||
texts = ["Some text", "Lots of texts...", "..."]
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
for doc in nlp.pipe(texts):
|
||||
doc_bin.add(doc)
|
||||
bytes_data = doc_bin.to_bytes()
|
||||
|
||||
# Deserialize later, e.g. in a new process
|
||||
nlp = spacy.blank("en")
|
||||
doc_bin = DocBin().from_bytes(bytes_data)
|
||||
docs = list(doc_bin.get_docs(nlp.vocab))
|
||||
```
|
||||
|
||||
If `store_user_data` is set to `True`, the `Doc.user_data` will be serialized as
|
||||
well, which includes the values of
|
||||
[extension attributes](/usage/processing-pipelines#custom-components-attributes)
|
||||
(if they're serializable with msgpack).
|
||||
|
||||
<Infobox title="Important note on serializing extension attributes" variant="warning">
|
||||
|
||||
Including the `Doc.user_data` and extension attributes will only serialize the
|
||||
**values** of the attributes. To restore the values and access them via the
|
||||
`doc._.` property, you need to register the global attribute on the `Doc` again.
|
||||
|
||||
```python
|
||||
docs = list(doc_bin.get_docs(nlp.vocab))
|
||||
Doc.set_extension("my_custom_attr", default=None)
|
||||
print([doc._.my_custom_attr for doc in docs])
|
||||
```
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Using Pickle {id="pickle"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("This is a text.")
|
||||
> data = pickle.dumps(doc)
|
||||
> ```
|
||||
|
||||
When pickling spaCy's objects like the [`Doc`](/api/doc) or the
|
||||
[`EntityRecognizer`](/api/entityrecognizer), keep in mind that they all require
|
||||
the shared [`Vocab`](/api/vocab) (which includes the string to hash mappings,
|
||||
label schemes and optional vectors). This means that their pickled
|
||||
representations can become very large, especially if you have word vectors
|
||||
loaded, because it won't only include the object itself, but also the entire
|
||||
shared vocab it depends on.
|
||||
|
||||
If you need to pickle multiple objects, try to pickle them **together** instead
|
||||
of separately. For instance, instead of pickling all pipeline components, pickle
|
||||
the entire pipeline once. And instead of pickling several `Doc` objects
|
||||
separately, pickle a list of `Doc` objects. Since they all share a reference to
|
||||
the _same_ `Vocab` object, it will only be included once.
|
||||
|
||||
```python {title="Pickling objects with shared data",highlight="8-9"}
|
||||
doc1 = nlp("Hello world")
|
||||
doc2 = nlp("This is a test")
|
||||
|
||||
doc1_data = pickle.dumps(doc1)
|
||||
doc2_data = pickle.dumps(doc2)
|
||||
print(len(doc1_data) + len(doc2_data)) # 6636116 😞
|
||||
|
||||
doc_data = pickle.dumps([doc1, doc2])
|
||||
print(len(doc_data)) # 3319761 😃
|
||||
```
|
||||
|
||||
<Infobox title="Pickling spans and tokens" variant="warning">
|
||||
|
||||
Pickling `Token` and `Span` objects isn't supported. They're only views of the
|
||||
`Doc` and can't exist on their own. Pickling them would always mean pulling in
|
||||
the parent document and its vocabulary, which has practically no advantage over
|
||||
pickling the parent `Doc`.
|
||||
|
||||
```diff
|
||||
- data = pickle.dumps(doc[10:20])
|
||||
+ data = pickle.dumps(doc)
|
||||
```
|
||||
|
||||
If you really only need a span – for example, a particular sentence – you can
|
||||
use [`Span.as_doc`](/api/span#as_doc) to make a copy of it and convert it to a
|
||||
`Doc` object. However, note that this will not let you recover contextual
|
||||
information from _outside_ the span.
|
||||
|
||||
```diff
|
||||
+ span_doc = doc[10:20].as_doc()
|
||||
data = pickle.dumps(span_doc)
|
||||
```
|
||||
|
||||
</Infobox>
|
||||
|
||||
## Implementing serialization methods {id="serialization-methods"}
|
||||
|
||||
When you call [`nlp.to_disk`](/api/language#to_disk),
|
||||
[`nlp.from_disk`](/api/language#from_disk) or load a pipeline package, spaCy
|
||||
will iterate over the components in the pipeline, check if they expose a
|
||||
`to_disk` or `from_disk` method and if so, call it with the path to the pipeline
|
||||
directory plus the string name of the component. For example, if you're calling
|
||||
`nlp.to_disk("/path")`, the data for the named entity recognizer will be saved
|
||||
in `/path/ner`.
|
||||
|
||||
If you're using custom pipeline components that depend on external data – for
|
||||
example, model weights or terminology lists – you can take advantage of spaCy's
|
||||
built-in component serialization by making your custom component expose its own
|
||||
`to_disk` and `from_disk` or `to_bytes` and `from_bytes` methods. When an `nlp`
|
||||
object with the component in its pipeline is saved or loaded, the component will
|
||||
then be able to serialize and deserialize itself.
|
||||
|
||||
<Infobox title="Custom components and data" emoji="📖">
|
||||
|
||||
For more details on how to work with pipeline components that depend on data
|
||||
resources and manage data loading and initialization at training and runtime,
|
||||
see the usage guide on initializing and serializing
|
||||
[component data](/usage/processing-pipelines#component-data).
|
||||
|
||||
</Infobox>
|
||||
|
||||
The following example shows a custom component that keeps arbitrary
|
||||
JSON-serializable data, allows the user to add to that data and saves and loads
|
||||
the data to and from a JSON file.
|
||||
|
||||
> #### Real-world example
|
||||
>
|
||||
> To see custom serialization methods in action, check out the new
|
||||
> [`EntityRuler`](/api/entityruler) component and its
|
||||
> [source](%%GITHUB_SPACY/spacy/pipeline/entityruler.py). Patterns added to the
|
||||
> component will be saved to a `.jsonl` file if the pipeline is serialized to
|
||||
> disk, and to a bytestring if the pipeline is serialized to bytes. This allows
|
||||
> saving out a pipeline with a rule-based entity recognizer and including all
|
||||
> rules _with_ the component data.
|
||||
|
||||
```python {highlight="16-23,25-30"}
|
||||
import json
|
||||
from spacy import Language
|
||||
from spacy.util import ensure_path
|
||||
|
||||
@Language.factory("my_component")
|
||||
class CustomComponent:
|
||||
def __init__(self, nlp: Language, name: str = "my_component"):
|
||||
self.name = name
|
||||
self.data = []
|
||||
|
||||
def __call__(self, doc):
|
||||
# Do something to the doc here
|
||||
return doc
|
||||
|
||||
def add(self, data):
|
||||
# Add something to the component's data
|
||||
self.data.append(data)
|
||||
|
||||
def to_disk(self, path, exclude=tuple()):
|
||||
# This will receive the directory path + /my_component
|
||||
path = ensure_path(path)
|
||||
if not path.exists():
|
||||
path.mkdir()
|
||||
data_path = path / "data.json"
|
||||
with data_path.open("w", encoding="utf8") as f:
|
||||
f.write(json.dumps(self.data))
|
||||
|
||||
def from_disk(self, path, exclude=tuple()):
|
||||
# This will receive the directory path + /my_component
|
||||
data_path = path / "data.json"
|
||||
with data_path.open("r", encoding="utf8") as f:
|
||||
self.data = json.load(f)
|
||||
return self
|
||||
```
|
||||
|
||||
After adding the component to the pipeline and adding some data to it, we can
|
||||
serialize the `nlp` object to a directory, which will call the custom
|
||||
component's `to_disk` method.
|
||||
|
||||
```python {highlight="2-4"}
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
my_component = nlp.add_pipe("my_component")
|
||||
my_component.add({"hello": "world"})
|
||||
nlp.to_disk("/path/to/pipeline")
|
||||
```
|
||||
|
||||
The contents of the directory would then look like this.
|
||||
`CustomComponent.to_disk` converted the data to a JSON string and saved it to a
|
||||
file `data.json` in its subdirectory:
|
||||
|
||||
```yaml {title="Directory structure",highlight="2-3"}
|
||||
└── /path/to/pipeline
|
||||
├── my_component # data serialized by "my_component"
|
||||
│ └── data.json
|
||||
├── ner # data for "ner" component
|
||||
├── parser # data for "parser" component
|
||||
├── tagger # data for "tagger" component
|
||||
├── vocab # pipeline vocabulary
|
||||
├── meta.json # pipeline meta.json
|
||||
├── config.cfg # pipeline config
|
||||
└── tokenizer # tokenization rules
|
||||
```
|
||||
|
||||
When you load the data back in, spaCy will call the custom component's
|
||||
`from_disk` method with the given file path, and the component can then load the
|
||||
contents of `data.json`, convert them to a Python object and restore the
|
||||
component state. The same works for other types of data, of course – for
|
||||
instance, you could add a
|
||||
[wrapper for a model](/usage/layers-architectures#frameworks) trained with a
|
||||
different library like TensorFlow or PyTorch and make spaCy load its weights
|
||||
automatically when you load the pipeline package.
|
||||
|
||||
<Infobox title="Important note on loading custom components" variant="warning">
|
||||
|
||||
When you load back a pipeline with custom components, make sure that the
|
||||
components are **available** and that the
|
||||
[`@Language.component`](/api/language#component) or
|
||||
[`@Language.factory`](/api/language#factory) decorators are executed _before_
|
||||
your pipeline is loaded back. Otherwise, spaCy won't know how to resolve the
|
||||
string name of a component factory like `"my_component"` back to a function. For
|
||||
more details, see the documentation on
|
||||
[adding factories](/usage/processing-pipelines#custom-components-factories) or
|
||||
use [entry points](#entry-points) to make your extension package expose your
|
||||
custom components to spaCy automatically.
|
||||
|
||||
</Infobox>
|
||||
|
||||
{/* ## Initializing components with data {id="initialization",version="3"} */}
|
||||
|
||||
## Using entry points {id="entry-points",version="2.1"}
|
||||
|
||||
Entry points let you expose parts of a Python package you write to other Python
|
||||
packages. This lets one application easily customize the behavior of another, by
|
||||
exposing an entry point in its `setup.py`. For a quick and fun intro to entry
|
||||
points in Python, check out
|
||||
[this excellent blog post](https://amir.rachum.com/blog/2017/07/28/python-entry-points/).
|
||||
spaCy can load custom functions from several different entry points to add
|
||||
pipeline component factories, language classes and other settings. To make spaCy
|
||||
use your entry points, your package needs to expose them and it needs to be
|
||||
installed in the same environment – that's it.
|
||||
|
||||
| Entry point | Description |
|
||||
| ------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| [`spacy_factories`](#entry-points-components) | Group of entry points for pipeline component factories, keyed by component name. Can be used to expose custom components defined by another package. |
|
||||
| [`spacy_languages`](#entry-points-languages) | Group of entry points for custom [`Language` subclasses](/usage/linguistic-features#language-data), keyed by language shortcut. |
|
||||
| `spacy_lookups` | Group of entry points for custom [`Lookups`](/api/lookups), including lemmatizer data. Used by spaCy's [`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data) package. |
|
||||
| [`spacy_displacy_colors`](#entry-points-displacy) | Group of entry points of custom label colors for the [displaCy visualizer](/usage/visualizers#ent). The key name doesn't matter, but it should point to a dict of labels and color values. Useful for custom models that predict different entity types. |
|
||||
|
||||
### Loading probability tables into existing models
|
||||
|
||||
You can load a probability table from
|
||||
[spacy-lookups-data](https://github.com/explosion/spacy-lookups-data) into an
|
||||
existing spaCy model like `en_core_web_sm`.
|
||||
|
||||
```python
|
||||
# Requirements: pip install spacy-lookups-data
|
||||
import spacy
|
||||
from spacy.lookups import load_lookups
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
lookups = load_lookups("en", ["lexeme_prob"])
|
||||
nlp.vocab.lookups.add_table("lexeme_prob", lookups.get_table("lexeme_prob"))
|
||||
```
|
||||
|
||||
When training a model from scratch you can also specify probability tables in
|
||||
the `config.cfg`.
|
||||
|
||||
```ini {title="config.cfg (excerpt)"}
|
||||
[initialize.lookups]
|
||||
@misc = "spacy.LookupsDataLoader.v1"
|
||||
lang = ${nlp.lang}
|
||||
tables = ["lexeme_prob"]
|
||||
```
|
||||
|
||||
### Custom components via entry points {id="entry-points-components"}
|
||||
|
||||
When you load a pipeline, spaCy will generally use its `config.cfg` to set up
|
||||
the language class and construct the pipeline. The pipeline is specified as a
|
||||
list of strings, e.g. `pipeline = ["tagger", "parser", "ner"]`. For each of
|
||||
those strings, spaCy will call `nlp.add_pipe` and look up the name in all
|
||||
factories defined by the decorators
|
||||
[`@Language.component`](/api/language#component) and
|
||||
[`@Language.factory`](/api/language#factory). This means that you have to import
|
||||
your custom components _before_ loading the pipeline.
|
||||
|
||||
Using entry points, pipeline packages and extension packages can define their
|
||||
own `"spacy_factories"`, which will be loaded automatically in the background
|
||||
when the `Language` class is initialized. So if a user has your package
|
||||
installed, they'll be able to use your components – even if they **don't import
|
||||
them**!
|
||||
|
||||
To stick with the theme of
|
||||
[this entry points blog post](https://amir.rachum.com/blog/2017/07/28/python-entry-points/),
|
||||
consider the following custom spaCy
|
||||
[pipeline component](/usage/processing-pipelines#custom-components) that prints
|
||||
a snake when it's called:
|
||||
|
||||
> #### Package directory structure
|
||||
>
|
||||
> ```yaml
|
||||
> ├── snek.py # the extension code
|
||||
> └── setup.py # setup file for pip installation
|
||||
> ```
|
||||
|
||||
```python {title="snek.py"}
|
||||
from spacy.language import Language
|
||||
|
||||
snek = """
|
||||
--..,_ _,.--.
|
||||
`'.'. .'`__ o `;__. {text}
|
||||
'.'. .'.'` '---'` `
|
||||
'.`'--....--'`.'
|
||||
`'--....--'`
|
||||
"""
|
||||
|
||||
@Language.component("snek")
|
||||
def snek_component(doc):
|
||||
print(snek.format(text=doc.text))
|
||||
return doc
|
||||
```
|
||||
|
||||
Since it's a very complex and sophisticated module, you want to split it off
|
||||
into its own package so you can version it and upload it to PyPi. You also want
|
||||
your custom package to be able to define `pipeline = ["snek"]` in its
|
||||
`config.cfg`. For that, you need to be able to tell spaCy where to find the
|
||||
component `"snek"`. If you don't do this, spaCy will raise an error when you try
|
||||
to load the pipeline because there's no built-in `"snek"` component. To add an
|
||||
entry to the factories, you can now expose it in your `setup.py` via the
|
||||
`entry_points` dictionary:
|
||||
|
||||
> #### Entry point syntax
|
||||
>
|
||||
> Python entry points for a group are formatted as a **list of strings**, with
|
||||
> each string following the syntax of `name = module:object`. In this example,
|
||||
> the created entry point is named `snek` and points to the function
|
||||
> `snek_component` in the module `snek`, i.e. `snek.py`.
|
||||
|
||||
```python {title="setup.py",highlight="5-7"}
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name="snek",
|
||||
entry_points={
|
||||
"spacy_factories": ["snek = snek:snek_component"]
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
The same package can expose multiple entry points, by the way. To make them
|
||||
available to spaCy, all you need to do is install the package in your
|
||||
environment:
|
||||
|
||||
```bash
|
||||
$ python -m pip install .
|
||||
```
|
||||
|
||||
spaCy is now able to create the pipeline component `"snek"` – even though you
|
||||
never imported `snek_component`. When you save the
|
||||
[`nlp.config`](/api/language#config) to disk, it includes an entry for your
|
||||
`"snek"` component and any pipeline you train with this config will include the
|
||||
component and know how to load it – if your `snek` package is installed.
|
||||
|
||||
> #### config.cfg (excerpt)
|
||||
>
|
||||
> ```diff
|
||||
> [nlp]
|
||||
> lang = "en"
|
||||
> + pipeline = ["snek"]
|
||||
>
|
||||
> [components]
|
||||
>
|
||||
> + [components.snek]
|
||||
> + factory = "snek"
|
||||
> ```
|
||||
|
||||
```
|
||||
>>> from spacy.lang.en import English
|
||||
>>> nlp = English()
|
||||
>>> nlp.add_pipe("snek") # this now works! 🐍🎉
|
||||
>>> doc = nlp("I am snek")
|
||||
--..,_ _,.--.
|
||||
`'.'. .'`__ o `;__. I am snek
|
||||
'.'. .'.'` '---'` `
|
||||
'.`'--....--'`.'
|
||||
`'--....--'`
|
||||
```
|
||||
|
||||
Instead of making your snek component a simple
|
||||
[stateless component](/usage/processing-pipelines#custom-components-simple), you
|
||||
could also make it a
|
||||
[factory](/usage/processing-pipelines#custom-components-factories) that takes
|
||||
settings. Your users can then pass in an optional `config` when they add your
|
||||
component to the pipeline and customize its appearance – for example, the
|
||||
`snek_style`.
|
||||
|
||||
> #### config.cfg (excerpt)
|
||||
>
|
||||
> ```diff
|
||||
> [components.snek]
|
||||
> factory = "snek"
|
||||
> + snek_style = "basic"
|
||||
> ```
|
||||
|
||||
```python
|
||||
SNEKS = {"basic": snek, "cute": cute_snek} # collection of sneks
|
||||
|
||||
@Language.factory("snek", default_config={"snek_style": "basic"})
|
||||
class SnekFactory:
|
||||
def __init__(self, nlp: Language, name: str, snek_style: str):
|
||||
self.nlp = nlp
|
||||
self.snek_style = snek_style
|
||||
self.snek = SNEKS[self.snek_style]
|
||||
|
||||
def __call__(self, doc):
|
||||
print(self.snek)
|
||||
return doc
|
||||
```
|
||||
|
||||
```diff {title="setup.py"}
|
||||
entry_points={
|
||||
- "spacy_factories": ["snek = snek:snek_component"]
|
||||
+ "spacy_factories": ["snek = snek:SnekFactory"]
|
||||
}
|
||||
```
|
||||
|
||||
The factory can also implement other pipeline component methods like `to_disk`
|
||||
and `from_disk` for serialization, or even `update` to make the component
|
||||
trainable. If a component exposes a `from_disk` method and is included in a
|
||||
pipeline, spaCy will call it on load. This lets you ship custom data with your
|
||||
pipeline package. When you save out a pipeline using `nlp.to_disk` and the
|
||||
component exposes a `to_disk` method, it will be called with the disk path.
|
||||
|
||||
```python
|
||||
from spacy.util import ensure_path
|
||||
|
||||
def to_disk(self, path, exclude=tuple()):
|
||||
path = ensure_path(path)
|
||||
if not path.exists():
|
||||
path.mkdir()
|
||||
snek_path = path / "snek.txt"
|
||||
with snek_path.open("w", encoding="utf8") as snek_file:
|
||||
snek_file.write(self.snek)
|
||||
|
||||
def from_disk(self, path, exclude=tuple()):
|
||||
snek_path = path / "snek.txt"
|
||||
with snek_path.open("r", encoding="utf8") as snek_file:
|
||||
self.snek = snek_file.read()
|
||||
return self
|
||||
```
|
||||
|
||||
The above example will serialize the current snake in a `snek.txt` in the data
|
||||
directory. When a pipeline using the `snek` component is loaded, it will open
|
||||
the `snek.txt` and make it available to the component.
|
||||
|
||||
### Custom language classes via entry points {id="entry-points-languages"}
|
||||
|
||||
To stay with the theme of the previous example and
|
||||
[this blog post on entry points](https://amir.rachum.com/blog/2017/07/28/python-entry-points/),
|
||||
let's imagine you wanted to implement your own `SnekLanguage` class for your
|
||||
custom pipeline – but you don't necessarily want to modify spaCy's code to add a
|
||||
language. In your package, you could then implement the following
|
||||
[custom language subclass](/usage/linguistic-features#language-subclass):
|
||||
|
||||
```python {title="snek.py"}
|
||||
from spacy.language import Language
|
||||
|
||||
class SnekDefaults(Language.Defaults):
|
||||
stop_words = set(["sss", "hiss"])
|
||||
|
||||
class SnekLanguage(Language):
|
||||
lang = "snk"
|
||||
Defaults = SnekDefaults
|
||||
```
|
||||
|
||||
Alongside the `spacy_factories`, there's also an entry point option for
|
||||
`spacy_languages`, which maps language codes to language-specific `Language`
|
||||
subclasses:
|
||||
|
||||
```diff {title="setup.py"}
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name="snek",
|
||||
entry_points={
|
||||
"spacy_factories": ["snek = snek:SnekFactory"],
|
||||
+ "spacy_languages": ["snk = snek:SnekLanguage"]
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
In spaCy, you can then load the custom `snk` language and it will be resolved to
|
||||
`SnekLanguage` via the custom entry point. This is especially relevant for
|
||||
pipeline packages you [train](/usage/training), which could then specify
|
||||
`lang = snk` in their `config.cfg` without spaCy raising an error because the
|
||||
language is not available in the core library.
|
||||
|
||||
### Custom displaCy colors via entry points {id="entry-points-displacy",version="2.2"}
|
||||
|
||||
If you're training a named entity recognition model for a custom domain, you may
|
||||
end up training different labels that don't have pre-defined colors in the
|
||||
[`displacy` visualizer](/usage/visualizers#ent). The `spacy_displacy_colors`
|
||||
entry point lets you define a dictionary of entity labels mapped to their color
|
||||
values. It's added to the pre-defined colors and can also overwrite existing
|
||||
values.
|
||||
|
||||
> #### Domain-specific NER labels
|
||||
>
|
||||
> Good examples of pipelines with domain-specific label schemes are
|
||||
> [scispaCy](/universe/project/scispacy) and
|
||||
> [Blackstone](/universe/project/blackstone).
|
||||
|
||||
```python {title="snek.py"}
|
||||
displacy_colors = {"SNEK": "#3dff74", "HUMAN": "#cfc5ff"}
|
||||
```
|
||||
|
||||
Given the above colors, the entry point can be defined as follows. Entry points
|
||||
need to have a name, so we use the key `colors`. However, the name doesn't
|
||||
matter and whatever is defined in the entry point group will be used.
|
||||
|
||||
```diff {title="setup.py"}
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name="snek",
|
||||
entry_points={
|
||||
+ "spacy_displacy_colors": ["colors = snek:displacy_colors"]
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
After installing the package, the custom colors will be used when visualizing
|
||||
text with `displacy`. Whenever the label `SNEK` is assigned, it will be
|
||||
displayed in `#3dff74`.
|
||||
|
||||
<Standalone height={100}>
|
||||
<div style={{lineHeight: 2.5, fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'", fontSize: 18}}>🌱🌿 <mark style={{ background: '#3dff74', padding: '0.45em 0.6em', margin: '0 0.25em', lineHeight: 1, borderRadius: '0.35em'}}>🐍 <span style={{ fontSize: '0.8em', fontWeight: 'bold', lineHeight: 1, borderRadius: '0.35em', marginLeft: '0.5rem'}}>SNEK</span></mark> ____ 🌳🌲 ____ <mark style={{ background: '#cfc5ff', padding: '0.45em 0.6em', margin: '0 0.25em', lineHeight: 1, borderRadius: '0.35em'}}>👨🌾 <span style={{ fontSize: '0.8em', fontWeight: 'bold', lineHeight: 1, borderRadius: '0.35em', marginLeft: '0.5rem'}}>HUMAN</span></mark> 🏘️</div>
|
||||
</Standalone>
|
||||
|
||||
## Saving, loading and distributing trained pipelines {id="models"}
|
||||
|
||||
After training your pipeline, you'll usually want to save its state, and load it
|
||||
back later. You can do this with the [`Language.to_disk`](/api/language#to_disk)
|
||||
method:
|
||||
|
||||
```python
|
||||
nlp.to_disk("./en_example_pipeline")
|
||||
```
|
||||
|
||||
The directory will be created if it doesn't exist, and the whole pipeline data,
|
||||
meta and configuration will be written out. To make the pipeline more convenient
|
||||
to deploy, we recommend wrapping it as a [Python package](/api/cli#package).
|
||||
|
||||
<Accordion title="What’s the difference between the config.cfg and meta.json?" spaced id="models-meta-vs-config" spaced>
|
||||
|
||||
When you save a pipeline in spaCy v3.0+, two files will be exported: a
|
||||
[`config.cfg`](/api/data-formats#config) based on
|
||||
[`nlp.config`](/api/language#config) and a [`meta.json`](/api/data-formats#meta)
|
||||
based on [`nlp.meta`](/api/language#meta).
|
||||
|
||||
- **config**: Configuration used to create the current `nlp` object, its
|
||||
pipeline components and models, as well as training settings and
|
||||
hyperparameters. Can include references to registered functions like
|
||||
[pipeline components](/usage/processing-pipelines#custom-components) or
|
||||
[model architectures](/api/architectures). Given a config, spaCy is able
|
||||
reconstruct the whole tree of objects and the `nlp` object. An exported config
|
||||
can also be used to [train a pipeline](/usage/training#config) with the same
|
||||
settings.
|
||||
- **meta**: Meta information about the pipeline and the Python package, such as
|
||||
the author information, license, version, data sources and label scheme. This
|
||||
is mostly used for documentation purposes and for packaging pipelines. It has
|
||||
no impact on the functionality of the `nlp` object.
|
||||
|
||||
</Accordion>
|
||||
|
||||
<Project id="pipelines/tagger_parser_ud">
|
||||
|
||||
The easiest way to get started with an end-to-end workflow is to clone a
|
||||
[project template](/usage/projects) and run it – for example, this template that
|
||||
lets you train a **part-of-speech tagger** and **dependency parser** on a
|
||||
Universal Dependencies treebank and generates an installable Python package.
|
||||
|
||||
</Project>
|
||||
|
||||
### Generating a pipeline package {id="models-generating"}
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
Pipeline packages are typically **not suitable** for the public
|
||||
[pypi.python.org](https://pypi.python.org) directory, which is not designed for
|
||||
binary data and files over 50 MB. However, if your company is running an
|
||||
**internal installation** of PyPi, publishing your pipeline packages on there
|
||||
can be a convenient way to share them with your team.
|
||||
|
||||
</Infobox>
|
||||
|
||||
spaCy comes with a handy CLI command that will create all required files, and
|
||||
walk you through generating the meta data. You can also create the
|
||||
[`meta.json`](/api/data-formats#meta) manually and place it in the data
|
||||
directory, or supply a path to it using the `--meta` flag. For more info on
|
||||
this, see the [`package`](/api/cli#package) docs.
|
||||
|
||||
> #### meta.json (example)
|
||||
>
|
||||
> ```json
|
||||
> {
|
||||
> "name": "example_pipeline",
|
||||
> "lang": "en",
|
||||
> "version": "1.0.0",
|
||||
> "spacy_version": ">=2.0.0,<3.0.0",
|
||||
> "description": "Example pipeline for spaCy",
|
||||
> "author": "You",
|
||||
> "email": "you@example.com",
|
||||
> "license": "CC BY-SA 3.0"
|
||||
> }
|
||||
> ```
|
||||
|
||||
```bash
|
||||
$ python -m spacy package ./en_example_pipeline ./packages
|
||||
```
|
||||
|
||||
This command will create a pipeline package directory and will run
|
||||
`python -m build` in that directory to create a binary `.whl` file or
|
||||
`.tar.gz` archive of your package that can be installed using `pip install`.
|
||||
Installing the binary wheel is usually more efficient.
|
||||
|
||||
```yaml {title="Directory structure"}
|
||||
└── /
|
||||
├── MANIFEST.in # to include meta.json
|
||||
├── meta.json # pipeline meta data
|
||||
├── setup.py # setup file for pip installation
|
||||
├── en_example_pipeline # pipeline directory
|
||||
│ ├── __init__.py # init for pip installation
|
||||
│ └── en_example_pipeline-1.0.0 # pipeline data
|
||||
│ ├── config.cfg # pipeline config
|
||||
│ ├── meta.json # pipeline meta
|
||||
│ └── ... # directories with component data
|
||||
└── dist
|
||||
└── en_example_pipeline-1.0.0.tar.gz # installable package
|
||||
```
|
||||
|
||||
You can also find templates for all files in the
|
||||
[`cli/package.py` source](https://github.com/explosion/spacy/tree/master/spacy/cli/package.py).
|
||||
If you're creating the package manually, keep in mind that the directories need
|
||||
to be named according to the naming conventions of `lang_name` and
|
||||
`lang_name-version`.
|
||||
|
||||
### Including custom functions and components {id="models-custom"}
|
||||
|
||||
If your pipeline includes
|
||||
[custom components](/usage/processing-pipelines#custom-components), model
|
||||
architectures or other [code](/usage/training#custom-code), those functions need
|
||||
to be registered **before** your pipeline is loaded. Otherwise, spaCy won't know
|
||||
how to create the objects referenced in the config. If you're loading your own
|
||||
pipeline in Python, you can make custom components available just by importing
|
||||
the code that defines them before calling
|
||||
[`spacy.load`](/api/top-level#spacy.load). This is also how the `--code`
|
||||
argument to CLI commands works.
|
||||
|
||||
With the [`spacy package`](/api/cli#package) command, you can provide one or
|
||||
more paths to Python files containing custom registered functions using the
|
||||
`--code` argument.
|
||||
|
||||
> #### \_\_init\_\_.py (excerpt)
|
||||
>
|
||||
> ```python
|
||||
> from . import functions
|
||||
>
|
||||
> def load(**overrides):
|
||||
> ...
|
||||
> ```
|
||||
|
||||
```bash
|
||||
$ python -m spacy package ./en_example_pipeline ./packages --code functions.py
|
||||
```
|
||||
|
||||
The Python files will be copied over into the root of the package, and the
|
||||
package's `__init__.py` will import them as modules. This ensures that functions
|
||||
are registered when the pipeline is imported, e.g. when you call `spacy.load`. A
|
||||
simple import is all that's needed to make registered functions available.
|
||||
|
||||
Make sure to include **all Python files** that are referenced in your custom
|
||||
code, including modules imported by others. If your custom code depends on
|
||||
**external packages**, make sure they're listed in the list of `"requirements"`
|
||||
in your [`meta.json`](/api/data-formats#meta). For the majority of use cases,
|
||||
registered functions should provide you with all customizations you need, from
|
||||
custom components to custom model architectures and lifecycle hooks. However, if
|
||||
you do want to customize the setup in more detail, you can edit the package's
|
||||
`__init__.py` and the package's `load` function that's called by
|
||||
[`spacy.load`](/api/top-level#spacy.load).
|
||||
|
||||
<Infobox variant="warning" title="Important note on making manual edits">
|
||||
|
||||
While it's no problem to edit the package code or meta information, avoid making
|
||||
edits to the `config.cfg` **after** training, as this can easily lead to data
|
||||
incompatibility. For instance, changing an architecture or hyperparameter can
|
||||
mean that the trained weights are now incompatible. If you want to make
|
||||
adjustments, you can do so before training. Otherwise, you should always trust
|
||||
spaCy to export the current state of its `nlp` objects via
|
||||
[`nlp.config`](/api/language#config).
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Loading a custom pipeline package {id="loading"}
|
||||
|
||||
To load a pipeline from a data directory, you can use
|
||||
[`spacy.load()`](/api/top-level#spacy.load) with the local path. This will look
|
||||
for a `config.cfg` in the directory and use the `lang` and `pipeline` settings
|
||||
to initialize a `Language` class with a processing pipeline and load in the
|
||||
model data.
|
||||
|
||||
```python
|
||||
nlp = spacy.load("/path/to/pipeline")
|
||||
```
|
||||
|
||||
If you want to **load only the binary data**, you'll have to create a `Language`
|
||||
class and call [`from_disk`](/api/language#from_disk) instead.
|
||||
|
||||
```python
|
||||
nlp = spacy.blank("en").from_disk("/path/to/data")
|
||||
```
|
||||
@@ -0,0 +1,584 @@
|
||||
---
|
||||
title: 'spaCy 101: Everything you need to know'
|
||||
teaser: The most important concepts, explained in simple terms
|
||||
menu:
|
||||
- ["What's spaCy?", 'whats-spacy']
|
||||
- ['Features', 'features']
|
||||
- ['Linguistic Annotations', 'annotations']
|
||||
- ['Pipelines', 'pipelines']
|
||||
- ['Architecture', 'architecture']
|
||||
- ['Vocab', 'vocab']
|
||||
- ['Serialization', 'serialization']
|
||||
- ['Training', 'training']
|
||||
- ['Language Data', 'language-data']
|
||||
- ['Community & FAQ', 'community-faq']
|
||||
---
|
||||
|
||||
Whether you're new to spaCy, or just want to brush up on some NLP basics and
|
||||
implementation details – this page should have you covered. Each section will
|
||||
explain one of spaCy's features in simple terms and with examples or
|
||||
illustrations. Some sections will also reappear across the usage guides as a
|
||||
quick introduction.
|
||||
|
||||
> #### Help us improve the docs
|
||||
>
|
||||
> Did you spot a mistake or come across explanations that are unclear? We always
|
||||
> appreciate improvement
|
||||
> [suggestions](https://github.com/explosion/spaCy/issues) or
|
||||
> [pull requests](https://github.com/explosion/spaCy/pulls). You can find a
|
||||
> "Suggest edits" link at the bottom of each page that points you to the source.
|
||||
|
||||
<Infobox title="Take the free interactive course">
|
||||
|
||||
<Image
|
||||
src="/images/course.jpg"
|
||||
href="https://course.spacy.io"
|
||||
alt="Advanced NLP with spaCy"
|
||||
/>
|
||||
|
||||
In this course you'll learn how to use spaCy to build advanced natural language
|
||||
understanding systems, using both rule-based and machine learning approaches. It
|
||||
includes 55 exercises featuring interactive coding practice, multiple-choice
|
||||
questions and slide decks.
|
||||
|
||||
<Button to="https://course.spacy.io" variant="primary">
|
||||
{'Start the course'}
|
||||
</Button>
|
||||
|
||||
</Infobox>
|
||||
|
||||
## What's spaCy? {id="whats-spacy"}
|
||||
|
||||
<Grid cols={2}>
|
||||
|
||||
<div>
|
||||
|
||||
spaCy is a **free, open-source library** for advanced **Natural Language
|
||||
Processing** (NLP) in Python.
|
||||
|
||||
If you're working with a lot of text, you'll eventually want to know more about
|
||||
it. For example, what's it about? What do the words mean in context? Who is
|
||||
doing what to whom? What companies and products are mentioned? Which texts are
|
||||
similar to each other?
|
||||
|
||||
spaCy is designed specifically for **production use** and helps you build
|
||||
applications that process and "understand" large volumes of text. It can be used
|
||||
to build **information extraction** or **natural language understanding**
|
||||
systems, or to pre-process text for **deep learning**.
|
||||
|
||||
</div>
|
||||
|
||||
<Infobox title="Table of contents" id="toc">
|
||||
|
||||
- [Features](#features)
|
||||
- [Linguistic annotations](#annotations)
|
||||
- [Tokenization](#annotations-token)
|
||||
- [POS tags and dependencies](#annotations-pos-deps)
|
||||
- [Named entities](#annotations-ner)
|
||||
- [Word vectors and similarity](#vectors-similarity)
|
||||
- [Pipelines](#pipelines)
|
||||
- [Library architecture](#architecture)
|
||||
- [Vocab, hashes and lexemes](#vocab)
|
||||
- [Serialization](#serialization)
|
||||
- [Training](#training)
|
||||
- [Language data](#language-data)
|
||||
- [Community & FAQ](#community)
|
||||
|
||||
</Infobox>
|
||||
|
||||
</Grid>
|
||||
|
||||
### What spaCy isn't {id="what-spacy-isnt"}
|
||||
|
||||
- ❌ **spaCy is not a platform or "an API"**. Unlike a platform, spaCy does not
|
||||
provide a software as a service, or a web application. It's an open-source
|
||||
library designed to help you build NLP applications, not a consumable service.
|
||||
- ❌ **spaCy is not an out-of-the-box chat bot engine**. While spaCy can be used
|
||||
to power conversational applications, it's not designed specifically for chat
|
||||
bots, and only provides the underlying text processing capabilities.
|
||||
- ❌**spaCy is not research software**. It's built on the latest research, but
|
||||
it's designed to get things done. This leads to fairly different design
|
||||
decisions than [NLTK](https://github.com/nltk/nltk) or
|
||||
[CoreNLP](https://stanfordnlp.github.io/CoreNLP/), which were created as
|
||||
platforms for teaching and research. The main difference is that spaCy is
|
||||
integrated and opinionated. spaCy tries to avoid asking the user to choose
|
||||
between multiple algorithms that deliver equivalent functionality. Keeping the
|
||||
menu small lets spaCy deliver generally better performance and developer
|
||||
experience.
|
||||
- ❌ **spaCy is not a company**. It's an open-source library. Our company
|
||||
publishing spaCy and other software is called
|
||||
[Explosion](https://explosion.ai).
|
||||
|
||||
## Features {id="features"}
|
||||
|
||||
In the documentation, you'll come across mentions of spaCy's features and
|
||||
capabilities. Some of them refer to linguistic concepts, while others are
|
||||
related to more general machine learning functionality.
|
||||
|
||||
| Name | Description |
|
||||
| ------------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
|
||||
| **Tokenization** | Segmenting text into words, punctuations marks etc. |
|
||||
| **Part-of-speech** (POS) **Tagging** | Assigning word types to tokens, like verb or noun. |
|
||||
| **Dependency Parsing** | Assigning syntactic dependency labels, describing the relations between individual tokens, like subject or object. |
|
||||
| **Lemmatization** | Assigning the base forms of words. For example, the lemma of "was" is "be", and the lemma of "rats" is "rat". |
|
||||
| **Sentence Boundary Detection** (SBD) | Finding and segmenting individual sentences. |
|
||||
| **Named Entity Recognition** (NER) | Labelling named "real-world" objects, like persons, companies or locations. |
|
||||
| **Entity Linking** (EL) | Disambiguating textual entities to unique identifiers in a knowledge base. |
|
||||
| **Similarity** | Comparing words, text spans and documents and how similar they are to each other. |
|
||||
| **Text Classification** | Assigning categories or labels to a whole document, or parts of a document. |
|
||||
| **Rule-based Matching** | Finding sequences of tokens based on their texts and linguistic annotations, similar to regular expressions. |
|
||||
| **Training** | Updating and improving a statistical model's predictions. |
|
||||
| **Serialization** | Saving objects to files or byte strings. |
|
||||
|
||||
### Statistical models {id="statistical-models"}
|
||||
|
||||
While some of spaCy's features work independently, others require
|
||||
[trained pipelines](/models) to be loaded, which enable spaCy to **predict**
|
||||
linguistic annotations – for example, whether a word is a verb or a noun. A
|
||||
trained pipeline can consist of multiple components that use a statistical model
|
||||
trained on labeled data. spaCy currently offers trained pipelines for a variety
|
||||
of languages, which can be installed as individual Python modules. Pipeline
|
||||
packages can differ in size, speed, memory usage, accuracy and the data they
|
||||
include. The package you choose always depends on your use case and the texts
|
||||
you're working with. For a general-purpose use case, the small, default packages
|
||||
are always a good start. They typically include the following components:
|
||||
|
||||
- **Binary weights** for the part-of-speech tagger, dependency parser and named
|
||||
entity recognizer to predict those annotations in context.
|
||||
- **Lexical entries** in the vocabulary, i.e. words and their
|
||||
context-independent attributes like the shape or spelling.
|
||||
- **Data files** like lemmatization rules and lookup tables.
|
||||
- **Word vectors**, i.e. multi-dimensional meaning representations of words that
|
||||
let you determine how similar they are to each other.
|
||||
- **Configuration** options, like the language and processing pipeline settings
|
||||
and model implementations to use, to put spaCy in the correct state when you
|
||||
load the pipeline.
|
||||
|
||||
## Linguistic annotations {id="annotations"}
|
||||
|
||||
spaCy provides a variety of linguistic annotations to give you **insights into a
|
||||
text's grammatical structure**. This includes the word types, like the parts of
|
||||
speech, and how the words are related to each other. For example, if you're
|
||||
analyzing text, it makes a huge difference whether a noun is the subject of a
|
||||
sentence, or the object – or whether "google" is used as a verb, or refers to
|
||||
the website or company in a specific context.
|
||||
|
||||
> #### Loading pipelines
|
||||
>
|
||||
> ```bash
|
||||
> $ python -m spacy download en_core_web_sm
|
||||
>
|
||||
> >>> import spacy
|
||||
> >>> nlp = spacy.load("en_core_web_sm")
|
||||
> ```
|
||||
|
||||
Once you've [downloaded and installed](/usage/models) a trained pipeline, you
|
||||
can load it via [`spacy.load`](/api/top-level#spacy.load). This will return a
|
||||
`Language` object containing all components and data needed to process text. We
|
||||
usually call it `nlp`. Calling the `nlp` object on a string of text will return
|
||||
a processed `Doc`:
|
||||
|
||||
```python {executable="true"}
|
||||
import spacy
|
||||
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
doc = nlp("Apple is looking at buying U.K. startup for $1 billion")
|
||||
for token in doc:
|
||||
print(token.text, token.pos_, token.dep_)
|
||||
```
|
||||
|
||||
Even though a `Doc` is processed – e.g. split into individual words and
|
||||
annotated – it still holds **all information of the original text**, like
|
||||
whitespace characters. You can always get the offset of a token into the
|
||||
original string, or reconstruct the original by joining the tokens and their
|
||||
trailing whitespace. This way, you'll never lose any information when processing
|
||||
text with spaCy.
|
||||
|
||||
### Tokenization {id="annotations-token"}
|
||||
|
||||
<Tokenization101 />
|
||||
|
||||
<Infobox title="Tokenization rules" emoji="📖">
|
||||
|
||||
To learn more about how spaCy's tokenization rules work in detail, how to
|
||||
**customize and replace** the default tokenizer and how to **add
|
||||
language-specific data**, see the usage guides on
|
||||
[language data](/usage/linguistic-features#language-data) and
|
||||
[customizing the tokenizer](/usage/linguistic-features#tokenization).
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Part-of-speech tags and dependencies {id="annotations-pos-deps",model="parser"}
|
||||
|
||||
<PosDeps101 />
|
||||
|
||||
<Infobox title="Part-of-speech tagging and morphology" emoji="📖">
|
||||
|
||||
To learn more about **part-of-speech tagging** and rule-based morphology, and
|
||||
how to **navigate and use the parse tree** effectively, see the usage guides on
|
||||
[part-of-speech tagging](/usage/linguistic-features#pos-tagging) and
|
||||
[using the dependency parse](/usage/linguistic-features#dependency-parse).
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Named Entities {id="annotations-ner",model="ner"}
|
||||
|
||||
<NER101 />
|
||||
|
||||
<Infobox title="Named Entity Recognition" emoji="📖">
|
||||
|
||||
To learn more about entity recognition in spaCy, how to **add your own
|
||||
entities** to a document and how to **train and update** the entity predictions
|
||||
of a model, see the usage guides on
|
||||
[named entity recognition](/usage/linguistic-features#named-entities) and
|
||||
[training pipelines](/usage/training).
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Word vectors and similarity {id="vectors-similarity",model="vectors"}
|
||||
|
||||
<Vectors101 />
|
||||
|
||||
<Infobox title="Word vectors" emoji="📖">
|
||||
|
||||
To learn more about word vectors, how to **customize them** and how to load
|
||||
**your own vectors** into spaCy, see the usage guide on
|
||||
[using word vectors and semantic similarities](/usage/linguistic-features#vectors-similarity).
|
||||
|
||||
</Infobox>
|
||||
|
||||
## Pipelines {id="pipelines"}
|
||||
|
||||
<Pipelines101 />
|
||||
|
||||
<Infobox title="Processing pipelines" emoji="📖">
|
||||
|
||||
To learn more about **how processing pipelines work** in detail, how to enable
|
||||
and disable their components, and how to **create your own**, see the usage
|
||||
guide on [language processing pipelines](/usage/processing-pipelines).
|
||||
|
||||
</Infobox>
|
||||
|
||||
## Architecture {id="architecture"}
|
||||
|
||||
<Architecture101 />
|
||||
|
||||
## Vocab, hashes and lexemes {id="vocab"}
|
||||
|
||||
Whenever possible, spaCy tries to store data in a vocabulary, the
|
||||
[`Vocab`](/api/vocab), that will be **shared by multiple documents**. To save
|
||||
memory, spaCy also encodes all strings to **hash values** – in this case for
|
||||
example, "coffee" has the hash `3197928453018144401`. Entity labels like "ORG"
|
||||
and part-of-speech tags like "VERB" are also encoded. Internally, spaCy only
|
||||
"speaks" in hash values.
|
||||
|
||||
> - **Token**: A word, punctuation mark etc. _in context_, including its
|
||||
> attributes, tags and dependencies.
|
||||
> - **Lexeme**: A "word type" with no context. Includes the word shape and
|
||||
> flags, e.g. if it's lowercase, a digit or punctuation.
|
||||
> - **Doc**: A processed container of tokens in context.
|
||||
> - **Vocab**: The collection of lexemes.
|
||||
> - **StringStore**: The dictionary mapping hash values to strings, for example
|
||||
> `3197928453018144401` → "coffee".
|
||||
|
||||

|
||||
|
||||
If you process lots of documents containing the word "coffee" in all kinds of
|
||||
different contexts, storing the exact string "coffee" every time would take up
|
||||
way too much space. So instead, spaCy hashes the string and stores it in the
|
||||
[`StringStore`](/api/stringstore). You can think of the `StringStore` as a
|
||||
**lookup table that works in both directions** – you can look up a string to get
|
||||
its hash, or a hash to get its string:
|
||||
|
||||
```python {executable="true"}
|
||||
import spacy
|
||||
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
doc = nlp("I love coffee")
|
||||
print(doc.vocab.strings["coffee"]) # 3197928453018144401
|
||||
print(doc.vocab.strings[3197928453018144401]) # 'coffee'
|
||||
```
|
||||
|
||||
Now that all strings are encoded, the entries in the vocabulary **don't need to
|
||||
include the word text** themselves. Instead, they can look it up in the
|
||||
`StringStore` via its hash value. Each entry in the vocabulary, also called
|
||||
[`Lexeme`](/api/lexeme), contains the **context-independent** information about
|
||||
a word. For example, no matter if "love" is used as a verb or a noun in some
|
||||
context, its spelling and whether it consists of alphabetic characters won't
|
||||
ever change. Its hash value will also always be the same.
|
||||
|
||||
```python {executable="true"}
|
||||
import spacy
|
||||
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
doc = nlp("I love coffee")
|
||||
for word in doc:
|
||||
lexeme = doc.vocab[word.text]
|
||||
print(lexeme.text, lexeme.orth, lexeme.shape_, lexeme.prefix_, lexeme.suffix_,
|
||||
lexeme.is_alpha, lexeme.is_digit, lexeme.is_title, lexeme.lang_)
|
||||
```
|
||||
|
||||
> - **Text**: The original text of the lexeme.
|
||||
> - **Orth**: The hash value of the lexeme.
|
||||
> - **Shape**: The abstract word shape of the lexeme.
|
||||
> - **Prefix**: By default, the first letter of the word string.
|
||||
> - **Suffix**: By default, the last three letters of the word string.
|
||||
> - **is alpha**: Does the lexeme consist of alphabetic characters?
|
||||
> - **is digit**: Does the lexeme consist of digits?
|
||||
|
||||
| Text | Orth | Shape | Prefix | Suffix | is_alpha | is_digit |
|
||||
| ------ | --------------------- | ------ | ------ | ------ | -------- | -------- |
|
||||
| I | `4690420944186131903` | `X` | I | I | `True` | `False` |
|
||||
| love | `3702023516439754181` | `xxxx` | l | ove | `True` | `False` |
|
||||
| coffee | `3197928453018144401` | `xxxx` | c | fee | `True` | `False` |
|
||||
|
||||
The mapping of words to hashes doesn't depend on any state. To make sure each
|
||||
value is unique, spaCy uses a
|
||||
[hash function](https://en.wikipedia.org/wiki/Hash_function) to calculate the
|
||||
hash **based on the word string**. This also means that the hash for "coffee"
|
||||
will always be the same, no matter which pipeline you're using or how you've
|
||||
configured spaCy.
|
||||
|
||||
However, hashes **cannot be reversed** and there's no way to resolve
|
||||
`3197928453018144401` back to "coffee". All spaCy can do is look it up in the
|
||||
vocabulary. That's why you always need to make sure all objects you create have
|
||||
access to the same vocabulary. If they don't, spaCy might not be able to find
|
||||
the strings it needs.
|
||||
|
||||
```python {executable="true"}
|
||||
import spacy
|
||||
from spacy.tokens import Doc
|
||||
from spacy.vocab import Vocab
|
||||
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
doc = nlp("I love coffee") # Original Doc
|
||||
print(doc.vocab.strings["coffee"]) # 3197928453018144401
|
||||
print(doc.vocab.strings[3197928453018144401]) # 'coffee' 👍
|
||||
|
||||
empty_doc = Doc(Vocab()) # New Doc with empty Vocab
|
||||
# empty_doc.vocab.strings[3197928453018144401] will raise an error :(
|
||||
|
||||
empty_doc.vocab.strings.add("coffee") # Add "coffee" and generate hash
|
||||
print(empty_doc.vocab.strings[3197928453018144401]) # 'coffee' 👍
|
||||
|
||||
new_doc = Doc(doc.vocab) # Create new doc with first doc's vocab
|
||||
print(new_doc.vocab.strings[3197928453018144401]) # 'coffee' 👍
|
||||
```
|
||||
|
||||
If the vocabulary doesn't contain a string for `3197928453018144401`, spaCy will
|
||||
raise an error. You can re-add "coffee" manually, but this only works if you
|
||||
actually _know_ that the document contains that word. To prevent this problem,
|
||||
spaCy will also export the `Vocab` when you save a `Doc` or `nlp` object. This
|
||||
will give you the object and its encoded annotations, plus the "key" to decode
|
||||
it.
|
||||
|
||||
## Serialization {id="serialization"}
|
||||
|
||||
<Serialization101 />
|
||||
|
||||
<Infobox title="Saving and loading" emoji="📖">
|
||||
|
||||
To learn more about how to **save and load your own pipelines**, see the usage
|
||||
guide on [saving and loading](/usage/saving-loading#models).
|
||||
|
||||
</Infobox>
|
||||
|
||||
## Training {id="training"}
|
||||
|
||||
<Training101 />
|
||||
|
||||
<Infobox title="Training pipelines and models" emoji="📖">
|
||||
|
||||
To learn more about **training and updating** pipelines, how to create training
|
||||
data and how to improve spaCy's named models, see the usage guides on
|
||||
[training](/usage/training).
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Training config and lifecycle {id="training-config"}
|
||||
|
||||
Training config files include all **settings and hyperparameters** for training
|
||||
your pipeline. Instead of providing lots of arguments on the command line, you
|
||||
only need to pass your `config.cfg` file to [`spacy train`](/api/cli#train).
|
||||
This also makes it easy to integrate custom models and architectures, written in
|
||||
your framework of choice. A pipeline's `config.cfg` is considered the "single
|
||||
source of truth", both at **training** and **runtime**.
|
||||
|
||||
> ```ini
|
||||
> ### config.cfg (excerpt)
|
||||
> [training]
|
||||
> accumulate_gradient = 3
|
||||
>
|
||||
> [training.optimizer]
|
||||
> @optimizers = "Adam.v1"
|
||||
>
|
||||
> [training.optimizer.learn_rate]
|
||||
> @schedules = "warmup_linear.v1"
|
||||
> warmup_steps = 250
|
||||
> total_steps = 20000
|
||||
> initial_rate = 0.01
|
||||
> ```
|
||||
|
||||

|
||||
|
||||
<Infobox title="Training configuration system" emoji="📖">
|
||||
|
||||
For more details on spaCy's **configuration system** and how to use it to
|
||||
customize your pipeline components, component models, training settings and
|
||||
hyperparameters, see the [training config](/usage/training#config) usage guide.
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Trainable components {id="training-components"}
|
||||
|
||||
spaCy's [`Pipe`](/api/pipe) class helps you implement your own trainable
|
||||
components that have their own model instance, make predictions over `Doc`
|
||||
objects and can be updated using [`spacy train`](/api/cli#train). This lets you
|
||||
plug fully custom machine learning components into your pipeline that can be
|
||||
configured via a single training config.
|
||||
|
||||
> #### config.cfg (excerpt)
|
||||
>
|
||||
> ```ini
|
||||
> [components.my_component]
|
||||
> factory = "my_component"
|
||||
>
|
||||
> [components.my_component.model]
|
||||
> @architectures = "my_model.v1"
|
||||
> width = 128
|
||||
> ```
|
||||
|
||||

|
||||
|
||||
<Infobox title="Custom trainable components" emoji="📖">
|
||||
|
||||
To learn more about how to implement your own **model architectures** and use
|
||||
them to power custom **trainable components**, see the usage guides on the
|
||||
[trainable component API](/usage/processing-pipelines#trainable-components) and
|
||||
implementing [layers and architectures](/usage/layers-architectures#components)
|
||||
for trainable components.
|
||||
|
||||
</Infobox>
|
||||
|
||||
## Language data {id="language-data"}
|
||||
|
||||
<LanguageData101 />
|
||||
|
||||
## Community & FAQ {id="community-faq"}
|
||||
|
||||
We're very happy to see the spaCy community grow and include a mix of people
|
||||
from all kinds of different backgrounds – computational linguistics, data
|
||||
science, deep learning, research and more. If you'd like to get involved, below
|
||||
are some answers to the most important questions and resources for further
|
||||
reading.
|
||||
|
||||
### Help, my code isn't working! {id="faq-help-code"}
|
||||
|
||||
Bugs suck, and we're doing our best to continuously improve the tests and fix
|
||||
bugs as soon as possible. Before you submit an issue, do a quick search and
|
||||
check if the problem has already been reported. If you're having installation or
|
||||
loading problems, make sure to also check out the
|
||||
[troubleshooting guide](/usage/#troubleshooting). Help with spaCy is available
|
||||
via the following platforms:
|
||||
|
||||
> #### How do I know if something is a bug?
|
||||
>
|
||||
> Of course, it's always hard to know for sure, so don't worry – we're not going
|
||||
> to be mad if a bug report turns out to be a typo in your code. As a simple
|
||||
> rule, any C-level error without a Python traceback, like a **segmentation
|
||||
> fault** or **memory error**, is **always** a spaCy bug.
|
||||
>
|
||||
> Because models are statistical, their performance will never be _perfect_.
|
||||
> However, if you come across **patterns that might indicate an underlying
|
||||
> issue**, please do file a report. Similarly, we also care about behaviors that
|
||||
> **contradict our docs**.
|
||||
|
||||
- [Stack Overflow](https://stackoverflow.com/questions/tagged/spacy): **Usage
|
||||
questions** and everything related to problems with your specific code. The
|
||||
Stack Overflow community is much larger than ours, so if your problem can be
|
||||
solved by others, you'll receive help much quicker.
|
||||
- [GitHub discussions](https://github.com/explosion/spaCy/discussions):
|
||||
**General discussion**, **project ideas** and **usage questions**. Meet other
|
||||
community members to get help with a specific code implementation, discuss
|
||||
ideas for new projects/plugins, support more languages, and share best
|
||||
practices.
|
||||
- [GitHub issue tracker](https://github.com/explosion/spaCy/issues): **Bug
|
||||
reports** and **improvement suggestions**, i.e. everything that's likely
|
||||
spaCy's fault. This also includes problems with the trained pipelines beyond
|
||||
statistical imprecisions, like patterns that point to a bug.
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
Please understand that we won't be able to provide individual support via email.
|
||||
We also believe that help is much more valuable if it's shared publicly, so that
|
||||
**more people can benefit from it**. If you come across an issue and you think
|
||||
you might be able to help, consider posting a quick update with your solution.
|
||||
No matter how simple, it can easily save someone a lot of time and headache –
|
||||
and the next time you need help, they might repay the favor.
|
||||
|
||||
</Infobox>
|
||||
|
||||
### How can I contribute to spaCy? {id="faq-contributing"}
|
||||
|
||||
You don't have to be an NLP expert or Python pro to contribute, and we're happy
|
||||
to help you get started. If you're new to spaCy, a good place to start is the
|
||||
[`help wanted (easy)` label](https://github.com/explosion/spaCy/issues?q=is%3Aissue+is%3Aopen+label%3A"help+wanted+%28easy%29")
|
||||
on GitHub, which we use to tag bugs and feature requests that are easy and
|
||||
self-contained. We also appreciate contributions to the docs – whether it's
|
||||
fixing a typo, improving an example or adding additional explanations. You'll
|
||||
find a "Suggest edits" link at the bottom of each page that points you to the
|
||||
source.
|
||||
|
||||
Another way of getting involved is to help us improve the
|
||||
[language data](/usage/linguistic-features#language-data) – especially if you
|
||||
happen to speak one of the languages currently in
|
||||
[alpha support](/usage/models#languages). Even adding simple tokenizer
|
||||
exceptions, stop words or lemmatizer data can make a big difference. It will
|
||||
also make it easier for us to provide a trained pipeline for the language in the
|
||||
future. Submitting a test that documents a bug or performance issue, or covers
|
||||
functionality that's especially important for your application is also very
|
||||
helpful. This way, you'll also make sure we never accidentally introduce
|
||||
regressions to the parts of the library that you care about the most.
|
||||
|
||||
**For more details on the types of contributions we're looking for, the code
|
||||
conventions and other useful tips, make sure to check out the
|
||||
[contributing guidelines](%%GITHUB_SPACY/CONTRIBUTING.md).**
|
||||
|
||||
<Infobox title="Code of Conduct" variant="warning">
|
||||
|
||||
spaCy adheres to the
|
||||
[Contributor Covenant Code of Conduct](http://contributor-covenant.org/version/1/4/).
|
||||
By participating, you are expected to uphold this code.
|
||||
|
||||
</Infobox>
|
||||
|
||||
### I've built something cool with spaCy – how can I get the word out? {id="faq-project-with-spacy"}
|
||||
|
||||
First, congrats – we'd love to check it out! If you think your project would be a good fit for the
|
||||
[spaCy Universe](/universe), **feel free to submit it!** Tutorials are also
|
||||
incredibly valuable to other users and a great way to get exposure. So we
|
||||
strongly encourage **writing up your experiences**, or sharing your code and
|
||||
some tips and tricks on your blog. Since our website is open-source, you can add
|
||||
your project or tutorial by making a pull request on GitHub.
|
||||
|
||||
If you would like to use the spaCy logo on your site, please get in touch and
|
||||
ask us first. However, if you want to show support and tell others that your
|
||||
project is using spaCy, you can grab one of our **spaCy badges** here:
|
||||
|
||||
<img
|
||||
src={`https://img.shields.io/badge/built%20with-spaCy-09a3d5.svg`}
|
||||
alt="Built with spaCy"
|
||||
/>
|
||||
|
||||
```markdown
|
||||
[](https://spacy.io)
|
||||
```
|
||||
|
||||
<img
|
||||
src={`https://img.shields.io/badge/made%20with%20❤%20and-spaCy-09a3d5.svg`}
|
||||
alt="Made with love and spaCy"
|
||||
/>
|
||||
|
||||
```markdown
|
||||
[](https://spacy.io)
|
||||
```
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,309 @@
|
||||
---
|
||||
title: What's New in v2.1
|
||||
teaser: New features, backwards incompatibilities and migration guide
|
||||
menu:
|
||||
- ['New Features', 'features']
|
||||
- ['Backwards Incompatibilities', 'incompat']
|
||||
---
|
||||
|
||||
## New Features {id="features",hidden="true"}
|
||||
|
||||
spaCy v2.1 has focussed primarily on stability and performance, solidifying the
|
||||
design changes introduced in [v2.0](/usage/v2). As well as smaller models,
|
||||
faster runtime, and many bug fixes, v2.1 also introduces experimental support
|
||||
for some exciting new NLP innovations. For the full changelog, see the
|
||||
[release notes on GitHub](https://github.com/explosion/spaCy/releases/tag/v2.1.0).
|
||||
For more details and a behind-the-scenes look at the new release,
|
||||
[see our blog post](https://explosion.ai/blog/spacy-v2-1).
|
||||
|
||||
### BERT/ULMFit/Elmo-style pre-training {id="pretraining",tag="experimental"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```bash
|
||||
> $ python -m spacy pretrain ./raw_text.jsonl
|
||||
> en_core_web_lg ./pretrained-model
|
||||
> ```
|
||||
|
||||
spaCy v2.1 introduces a new CLI command, `spacy pretrain`, that can make your
|
||||
models much more accurate. It's especially useful when you have **limited
|
||||
training data**. The `spacy pretrain` command lets you use transfer learning to
|
||||
initialize your models with information from raw text, using a language model
|
||||
objective similar to the one used in Google's BERT system. We've taken
|
||||
particular care to ensure that pretraining works well even with spaCy's small
|
||||
default architecture sizes, so you don't have to compromise on efficiency to use
|
||||
it.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`spacy pretrain`](/api/cli#pretrain) **Usage: **
|
||||
[Improving accuracy with transfer learning](/usage/training#transfer-learning)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Extended match pattern API {id="matcher-api"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Matches "love cats" or "likes flowers"
|
||||
> pattern1 = [{"LEMMA": {"IN": ["like", "love"]}}, {"POS": "NOUN"}]
|
||||
> # Matches tokens of length >= 10
|
||||
> pattern2 = [{"LENGTH": {">=": 10}}]
|
||||
> # Matches custom attribute with regex
|
||||
> pattern3 = [{"_": {"country": {"REGEX": "^([Uu](\\.?|nited) ?[Ss](\\.?|tates)"}}}]
|
||||
> ```
|
||||
|
||||
Instead of mapping to a single value, token patterns can now also map to a
|
||||
**dictionary of properties**. For example, to specify that the value of a lemma
|
||||
should be part of a list of values, or to set a minimum character length. It now
|
||||
also supports a `REGEX` property, as well as set membership via `IN` and
|
||||
`NOT_IN`, custom extension attributes via `_` and rich comparison for numeric
|
||||
values.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`Matcher`](/api/matcher) **Usage: **
|
||||
[Extended pattern syntax and attributes](/usage/rule-based-matching#adding-patterns-attributes-extended),
|
||||
[Regular expressions](/usage/rule-based-matching#regex)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Easy rule-based entity recognition {id="entity-ruler"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.pipeline import EntityRuler
|
||||
> ruler = EntityRuler(nlp)
|
||||
> ruler.add_patterns([{"label": "ORG", "pattern": "Apple"}])
|
||||
> nlp.add_pipe(ruler, before="ner")
|
||||
> ```
|
||||
|
||||
The `EntityRuler` is an exciting new component that lets you add named entities
|
||||
based on pattern dictionaries, and makes it easy to combine rule-based and
|
||||
statistical named entity recognition for even more powerful models. Entity rules
|
||||
can be phrase patterns for exact string matches, or token patterns for full
|
||||
flexibility.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`EntityRuler`](/api/entityruler) **Usage: **
|
||||
[Rule-based entity recognition](/usage/rule-based-matching#entityruler)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Phrase matching with other attributes {id="phrasematcher"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> matcher = PhraseMatcher(nlp.vocab, attr="POS")
|
||||
> matcher.add("PATTERN", None, nlp("I love cats"))
|
||||
> doc = nlp("You like dogs")
|
||||
> matches = matcher(doc)
|
||||
> ```
|
||||
|
||||
By default, the `PhraseMatcher` will match on the verbatim token text, e.g.
|
||||
`Token.text`. By setting the `attr` argument on initialization, you can change
|
||||
**which token attribute the matcher should use** when comparing the phrase
|
||||
pattern to the matched `Doc`. For example, `LOWER` for case-insensitive matches
|
||||
or `POS` for finding sequences of the same part-of-speech tags.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`PhraseMatcher`](/api/phrasematcher) **Usage: **
|
||||
[Matching on other token attributes](/usage/rule-based-matching#phrasematcher-attrs)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Retokenizer for merging and splitting {id="retokenizer"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I like David Bowie")
|
||||
> with doc.retokenize() as retokenizer:
|
||||
> attrs = {"LEMMA": "David Bowie"}
|
||||
> retokenizer.merge(doc[2:4], attrs=attrs)
|
||||
> ```
|
||||
|
||||
The new `Doc.retokenize` context manager allows merging spans of multiple tokens
|
||||
into one single token, and splitting single tokens into multiple tokens.
|
||||
Modifications to the `Doc`'s tokenization are stored, and then made all at once
|
||||
when the context manager exits. This is much more efficient, and less
|
||||
error-prone. `Doc.merge` and `Span.merge` still work, but they're considered
|
||||
deprecated.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`Doc.retokenize`](/api/doc#retokenize),
|
||||
[`Retokenizer.merge`](/api/doc#retokenizer.merge),
|
||||
[`Retokenizer.split`](/api/doc#retokenizer.split)<br />**Usage:
|
||||
**[Merging and splitting](/usage/linguistic-features#retokenization)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Components and languages via entry points {id="entry-points"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from setuptools import setup
|
||||
> setup(
|
||||
> name="custom_extension_package",
|
||||
> entry_points={
|
||||
> "spacy_factories": ["your_component = component:ComponentFactory"]
|
||||
> "spacy_languages": ["xyz = language:XYZLanguage"]
|
||||
> }
|
||||
> )
|
||||
> ```
|
||||
|
||||
Using entry points, model packages and extension packages can now define their
|
||||
own `"spacy_factories"` and `"spacy_languages"`, which will be added to the
|
||||
built-in factories and languages. If a package in the same environment exposes
|
||||
spaCy entry points, all of this happens automatically and no further user action
|
||||
is required.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**Usage:** [Using entry points](/usage/saving-loading#entry-points)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Improved documentation {id="docs"}
|
||||
|
||||
Although it looks pretty much the same, we've rebuilt the entire documentation
|
||||
using [Gatsby](https://www.gatsbyjs.org/) and [MDX](https://mdxjs.com/). It's
|
||||
now an even faster progressive web app and allows us to write all content
|
||||
entirely **in Markdown**, without having to compromise on easy-to-use custom UI
|
||||
components. We're hoping that the Markdown source will make it even easier to
|
||||
contribute to the documentation. For more details, check out the
|
||||
[styleguide](/styleguide) and
|
||||
[source](https://github.com/explosion/spacy/tree/v2.x/website). While converting
|
||||
the pages to Markdown, we've also fixed a bunch of typos, improved the existing
|
||||
pages and added some new content:
|
||||
|
||||
- **Usage Guide:** [Rule-based Matching](/usage/rule-based-matching)<br/>How to
|
||||
use the `Matcher`, `PhraseMatcher` and the new `EntityRuler`, and write
|
||||
powerful components to combine statistical models and rules.
|
||||
- **Usage Guide:** [Saving and Loading](/usage/saving-loading)<br/>Everything
|
||||
you need to know about serialization, and how to save and load pipeline
|
||||
components, package your spaCy models as Python modules and use entry points.
|
||||
- **Usage Guide: **
|
||||
[Merging and Splitting](/usage/linguistic-features#retokenization)<br />How to
|
||||
retokenize a `Doc` using the new `retokenize` context manager and merge spans
|
||||
into single tokens and split single tokens into multiple.
|
||||
- **Universe:** [Videos](/universe/category/videos) and
|
||||
[Podcasts](/universe/category/podcasts)
|
||||
- **API:** [`EntityRuler`](/api/entityruler)
|
||||
- **API:** [`Sentencizer`](/api/sentencizer)
|
||||
- **API:** [Pipeline functions](/api/pipeline-functions)
|
||||
|
||||
## Backwards incompatibilities {id="incompat"}
|
||||
|
||||
<Infobox title="Important note on models" variant="warning">
|
||||
|
||||
If you've been training **your own models**, you'll need to **retrain** them
|
||||
with the new version. Also don't forget to upgrade all models to the latest
|
||||
versions. Models for v2.0.x aren't compatible with models for v2.1.x. To check
|
||||
if all of your models are up to date, you can run the
|
||||
[`spacy validate`](/api/cli#validate) command.
|
||||
|
||||
</Infobox>
|
||||
|
||||
- Due to difficulties linking our new
|
||||
[`blis`](https://github.com/explosion/cython-blis) for faster
|
||||
platform-independent matrix multiplication, this release currently **doesn't
|
||||
work on Python 2.7 on Windows**. We expect this to be corrected in the future.
|
||||
|
||||
- While the [`Matcher`](/api/matcher) API is fully backwards compatible, its
|
||||
algorithm has changed to fix a number of bugs and performance issues. This
|
||||
means that the `Matcher` in v2.1.x may produce different results compared to
|
||||
the `Matcher` in v2.0.x.
|
||||
|
||||
- The deprecated [`Doc.merge`](/api/doc#merge) and
|
||||
[`Span.merge`](/api/span#merge) methods still work, but you may notice that
|
||||
they now run slower when merging many objects in a row. That's because the
|
||||
merging engine was rewritten to be more reliable and to support more efficient
|
||||
merging **in bulk**. To take advantage of this, you should rewrite your logic
|
||||
to use the [`Doc.retokenize`](/api/doc#retokenize) context manager and perform
|
||||
as many merges as possible together in the `with` block.
|
||||
|
||||
```diff
|
||||
- doc[1:5].merge()
|
||||
- doc[6:8].merge()
|
||||
+ with doc.retokenize() as retokenizer:
|
||||
+ retokenizer.merge(doc[1:5])
|
||||
+ retokenizer.merge(doc[6:8])
|
||||
```
|
||||
|
||||
- The serialization methods `to_disk`, `from_disk`, `to_bytes` and `from_bytes`
|
||||
now support a single `exclude` argument to provide a list of string names to
|
||||
exclude. The docs have been updated to list the available serialization fields
|
||||
for each class. The `disable` argument on the [`Language`](/api/language)
|
||||
serialization methods has been renamed to `exclude` for consistency.
|
||||
|
||||
```diff
|
||||
- nlp.to_disk("/path", disable=["parser", "ner"])
|
||||
+ nlp.to_disk("/path", exclude=["parser", "ner"])
|
||||
- data = nlp.tokenizer.to_bytes(vocab=False)
|
||||
+ data = nlp.tokenizer.to_bytes(exclude=["vocab"])
|
||||
```
|
||||
|
||||
- The .pos value for several common English words has changed, due to
|
||||
corrections to long-standing mistakes in the English tag map (see
|
||||
[issue #593](https://github.com/explosion/spaCy/issues/593) and
|
||||
[issue #3311](https://github.com/explosion/spaCy/issues/3311) for details).
|
||||
|
||||
- For better compatibility with the Universal Dependencies data, the lemmatizer
|
||||
now preserves capitalization, e.g. for proper nouns. See
|
||||
[issue #3256](https://github.com/explosion/spaCy/issues/3256) for details.
|
||||
|
||||
- The built-in rule-based sentence boundary detector is now only called
|
||||
`"sentencizer"` – the name `"sbd"` is deprecated.
|
||||
|
||||
```diff
|
||||
- sentence_splitter = nlp.create_pipe("sbd")
|
||||
+ sentence_splitter = nlp.create_pipe("sentencizer")
|
||||
```
|
||||
|
||||
- The `is_sent_start` attribute of the first token in a `Doc` now correctly
|
||||
defaults to `True`. It previously defaulted to `None`.
|
||||
|
||||
- The keyword argument `n_threads` on the `.pipe` methods is now deprecated, as
|
||||
the v2.x models cannot release the global interpreter lock. (Future versions
|
||||
may introduce a `n_process` argument for parallel inference via
|
||||
multiprocessing.)
|
||||
|
||||
- The `Doc.print_tree` method is now deprecated. If you need a custom nested
|
||||
JSON representation of a `Doc` object, you might want to write your own helper
|
||||
function. For a simple and consistent JSON representation of the `Doc` object
|
||||
and its annotations, you can now use the [`Doc.to_json`](/api/doc#to_json)
|
||||
method. Going forward, this method will output the same format as the JSON
|
||||
training data expected by [`spacy train`](/api/cli#train).
|
||||
|
||||
- The [`spacy train`](/api/cli#train) command now lets you specify a
|
||||
comma-separated list of pipeline component names, instead of separate flags
|
||||
like `--no-parser` to disable components. This is more flexible and also
|
||||
handles custom components out-of-the-box.
|
||||
|
||||
```diff
|
||||
- $ spacy train en /output train_data.json dev_data.json --no-parser
|
||||
+ $ spacy train en /output train_data.json dev_data.json --pipeline tagger,ner
|
||||
```
|
||||
|
||||
- The [`spacy init-model`](/api/cli#init-model) command now uses a `--jsonl-loc`
|
||||
argument to pass in a a newline-delimited JSON (JSONL) file containing one
|
||||
lexical entry per line instead of a separate `--freqs-loc` and
|
||||
`--clusters-loc`.
|
||||
|
||||
```diff
|
||||
- $ spacy init-model en ./model --freqs-loc ./freqs.txt --clusters-loc ./clusters.txt
|
||||
+ $ spacy init-model en ./model --jsonl-loc ./vocab.jsonl
|
||||
```
|
||||
|
||||
- Also note that some of the model licenses have changed:
|
||||
[`it_core_news_sm`](/models/it#it_core_news_sm) is now correctly licensed
|
||||
under CC BY-NC-SA 3.0, and all [English](/models/en) and [German](/models/de)
|
||||
models are now published under the MIT license.
|
||||
@@ -0,0 +1,455 @@
|
||||
---
|
||||
title: What's New in v2.2
|
||||
teaser: New features, backwards incompatibilities and migration guide
|
||||
menu:
|
||||
- ['New Features', 'features']
|
||||
- ['Backwards Incompatibilities', 'incompat']
|
||||
- ['Migrating from v2.1', 'migrating']
|
||||
---
|
||||
|
||||
## New Features {id="features",hidden="true"}
|
||||
|
||||
spaCy v2.2 features improved statistical models, new pretrained models for
|
||||
Norwegian and Lithuanian, better Dutch NER, as well as a new mechanism for
|
||||
storing language data that makes the installation about **5-10× smaller**
|
||||
on disk. We've also added a new class to efficiently **serialize annotations**,
|
||||
an improved and **10× faster** phrase matching engine, built-in scoring
|
||||
and **CLI training for text classification**, a new command to analyze and
|
||||
**debug training data**, data augmentation during training and more. For the
|
||||
full changelog, see the
|
||||
[release notes on GitHub](https://github.com/explosion/spaCy/releases/tag/v2.2.0).
|
||||
|
||||
For more details and a behind-the-scenes look at the new release,
|
||||
[see our blog post](https://explosion.ai/blog/spacy-v2-2).
|
||||
|
||||
### Better pretrained models and more languages {id="models"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```bash
|
||||
> python -m spacy download nl_core_news_sm
|
||||
> python -m spacy download nb_core_news_sm
|
||||
> python -m spacy download lt_core_news_sm
|
||||
> ```
|
||||
|
||||
The new version also features new and re-trained models for all languages and
|
||||
resolves a number of data bugs. The [Dutch model](/models/nl) has been retrained
|
||||
with a new and custom-labelled NER corpus using the same extended label scheme
|
||||
as the English models. It should now produce significantly better NER results
|
||||
overall. We've also added new core models for [Norwegian](/models/nb) (MIT) and
|
||||
[Lithuanian](/models/lt) (CC BY-SA).
|
||||
|
||||
<Infobox>
|
||||
|
||||
**Usage:** [Models directory](/models) **Benchmarks: **
|
||||
[Release notes](https://github.com/explosion/spaCy/releases/tag/v2.2.0)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Text classification scores and CLI training {id="train-textcat-cli"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```bash
|
||||
> $ python -m spacy train en /output /train /dev \\
|
||||
> --pipeline textcat --textcat-arch simple_cnn \\
|
||||
> --textcat-multilabel
|
||||
> ```
|
||||
|
||||
When training your models using the `spacy train` command, you can now also
|
||||
include text categories in the JSON-formatted training data. The `Scorer` and
|
||||
`nlp.evaluate` now report the text classification scores, calculated as the
|
||||
F-score on positive label for binary exclusive tasks, the macro-averaged F-score
|
||||
for 3+ exclusive labels or the macro-averaged AUC ROC score for multilabel
|
||||
classification.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`spacy train`](/api/cli#train), [`Scorer`](/api/scorer),
|
||||
[`Language.evaluate`](/api/language#evaluate)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### New DocBin class to efficiently serialize Doc collections
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.tokens import DocBin
|
||||
> doc_bin = DocBin(attrs=["LEMMA", "ENT_IOB", "ENT_TYPE"], store_user_data=True)
|
||||
> for doc in nlp.pipe(texts):
|
||||
> doc_bin.add(doc)
|
||||
> bytes_data = doc_bin.to_bytes()
|
||||
> # Deserialize later, e.g. in a new process
|
||||
> nlp = spacy.blank("en")
|
||||
> doc_bin = DocBin().from_bytes(bytes_data)
|
||||
> docs = list(doc_bin.get_docs(nlp.vocab))
|
||||
> ```
|
||||
|
||||
If you're working with lots of data, you'll probably need to pass analyses
|
||||
between machines, either to use something like [Dask](https://dask.org) or
|
||||
[Spark](https://spark.apache.org), or even just to save out work to disk. Often
|
||||
it's sufficient to use the `Doc.to_array` functionality for this, and just
|
||||
serialize the numpy arrays – but other times you want a more general way to save
|
||||
and restore `Doc` objects.
|
||||
|
||||
The new `DocBin` class makes it easy to serialize and deserialize a collection
|
||||
of `Doc` objects together, and is much more efficient than calling
|
||||
`Doc.to_bytes` on each individual `Doc` object. You can also control what data
|
||||
gets saved, and you can merge pallets together for easy map/reduce-style
|
||||
processing.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`DocBin`](/api/docbin) **Usage: **
|
||||
[Serializing Doc objects](/usage/saving-loading#docs)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Serializable lookup tables and smaller installation {id="lookups"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> data = {"foo": "bar"}
|
||||
> nlp.vocab.lookups.add_table("my_dict", data)
|
||||
>
|
||||
> def custom_component(doc):
|
||||
> table = doc.vocab.lookups.get_table("my_dict")
|
||||
> print(table.get("foo")) # look something up
|
||||
> return doc
|
||||
> ```
|
||||
|
||||
The new `Lookups` API lets you add large dictionaries and lookup tables to the
|
||||
`Vocab` and access them from the tokenizer or custom components and extension
|
||||
attributes. Internally, the tables use Bloom filters for efficient lookup
|
||||
checks. They're also fully serializable out-of-the-box. All large data resources
|
||||
like lemmatization tables have been moved to a separate package,
|
||||
[`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data) that can
|
||||
be installed alongside the core library. This allowed us to make the spaCy
|
||||
installation **5-10× smaller on disk** (depending on your platform).
|
||||
[Pretrained models](/models) now include their data files, so you only need to
|
||||
install the lookups if you want to build blank models or use lemmatization with
|
||||
languages that don't yet ship with pretrained models.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`Lookups`](/api/lookups),
|
||||
[`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data) **Usage:
|
||||
** [Adding languages: Lemmatizer](/usage/adding-languages#lemmatizer)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### CLI command to debug and validate training data {id="debug-data"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```bash
|
||||
> $ python -m spacy debug-data en train.json dev.json
|
||||
> ```
|
||||
|
||||
The new `debug-data` command lets you analyze and validate your training and
|
||||
development data, get useful stats, and find problems like invalid entity
|
||||
annotations, cyclic dependencies, low data labels and more. If you're training a
|
||||
model with `spacy train` and the results seem surprising or confusing,
|
||||
`debug-data` may help you track down the problems and improve your training
|
||||
data.
|
||||
|
||||
<Accordion title="Example output">
|
||||
|
||||
```
|
||||
=========================== Data format validation ===========================
|
||||
✔ Corpus is loadable
|
||||
|
||||
=============================== Training stats ===============================
|
||||
Training pipeline: tagger, parser, ner
|
||||
Starting with blank model 'en'
|
||||
18127 training docs
|
||||
2939 evaluation docs
|
||||
⚠ 34 training examples also in evaluation data
|
||||
|
||||
============================== Vocab & Vectors ==============================
|
||||
ℹ 2083156 total words in the data (56962 unique)
|
||||
⚠ 13020 misaligned tokens in the training data
|
||||
⚠ 2423 misaligned tokens in the dev data
|
||||
10 most common words: 'the' (98429), ',' (91756), '.' (87073), 'to' (50058),
|
||||
'of' (49559), 'and' (44416), 'a' (34010), 'in' (31424), 'that' (22792), 'is'
|
||||
(18952)
|
||||
ℹ No word vectors present in the model
|
||||
|
||||
========================== Named Entity Recognition ==========================
|
||||
ℹ 18 new labels, 0 existing labels
|
||||
528978 missing values (tokens with '-' label)
|
||||
New: 'ORG' (23860), 'PERSON' (21395), 'GPE' (21193), 'DATE' (18080), 'CARDINAL'
|
||||
(10490), 'NORP' (9033), 'MONEY' (5164), 'PERCENT' (3761), 'ORDINAL' (2122),
|
||||
'LOC' (2113), 'TIME' (1616), 'WORK_OF_ART' (1229), 'QUANTITY' (1150), 'FAC'
|
||||
(1134), 'EVENT' (974), 'PRODUCT' (935), 'LAW' (444), 'LANGUAGE' (338)
|
||||
✔ Good amount of examples for all labels
|
||||
✔ Examples without occurrences available for all labels
|
||||
✔ No entities consisting of or starting/ending with whitespace
|
||||
|
||||
=========================== Part-of-speech Tagging ===========================
|
||||
ℹ 49 labels in data (57 labels in tag map)
|
||||
'NN' (266331), 'IN' (227365), 'DT' (185600), 'NNP' (164404), 'JJ' (119830),
|
||||
'NNS' (110957), '.' (101482), ',' (92476), 'RB' (90090), 'PRP' (90081), 'VB'
|
||||
(74538), 'VBD' (68199), 'CC' (62862), 'VBZ' (50712), 'VBP' (43420), 'VBN'
|
||||
(42193), 'CD' (40326), 'VBG' (34764), 'TO' (31085), 'MD' (25863), 'PRP$'
|
||||
(23335), 'HYPH' (13833), 'POS' (13427), 'UH' (13322), 'WP' (10423), 'WDT'
|
||||
(9850), 'RP' (8230), 'WRB' (8201), ':' (8168), '''' (7392), '``' (6984), 'NNPS'
|
||||
(5817), 'JJR' (5689), '$' (3710), 'EX' (3465), 'JJS' (3118), 'RBR' (2872),
|
||||
'-RRB-' (2825), '-LRB-' (2788), 'PDT' (2078), 'XX' (1316), 'RBS' (1142), 'FW'
|
||||
(794), 'NFP' (557), 'SYM' (440), 'WP$' (294), 'LS' (293), 'ADD' (191), 'AFX'
|
||||
(24)
|
||||
✔ All labels present in tag map for language 'en'
|
||||
|
||||
============================= Dependency Parsing =============================
|
||||
ℹ Found 111703 sentences with an average length of 18.6 words.
|
||||
ℹ Found 2251 nonprojective train sentences
|
||||
ℹ Found 303 nonprojective dev sentences
|
||||
ℹ 47 labels in train data
|
||||
ℹ 211 labels in projectivized train data
|
||||
'punct' (236796), 'prep' (188853), 'pobj' (182533), 'det' (172674), 'nsubj'
|
||||
(169481), 'compound' (116142), 'ROOT' (111697), 'amod' (107945), 'dobj' (93540),
|
||||
'aux' (86802), 'advmod' (86197), 'cc' (62679), 'conj' (59575), 'poss' (36449),
|
||||
'ccomp' (36343), 'advcl' (29017), 'mark' (27990), 'nummod' (24582), 'relcl'
|
||||
(21359), 'xcomp' (21081), 'attr' (18347), 'npadvmod' (17740), 'acomp' (17204),
|
||||
'auxpass' (15639), 'appos' (15368), 'neg' (15266), 'nsubjpass' (13922), 'case'
|
||||
(13408), 'acl' (12574), 'pcomp' (10340), 'nmod' (9736), 'intj' (9285), 'prt'
|
||||
(8196), 'quantmod' (7403), 'dep' (4300), 'dative' (4091), 'agent' (3908), 'expl'
|
||||
(3456), 'parataxis' (3099), 'oprd' (2326), 'predet' (1946), 'csubj' (1494),
|
||||
'subtok' (1147), 'preconj' (692), 'meta' (469), 'csubjpass' (64), 'iobj' (1)
|
||||
⚠ Low number of examples for label 'iobj' (1)
|
||||
⚠ Low number of examples for 130 labels in the projectivized dependency
|
||||
trees used for training. You may want to projectivize labels such as punct
|
||||
before training in order to improve parser performance.
|
||||
⚠ Projectivized labels with low numbers of examples: appos||attr: 12
|
||||
advmod||dobj: 13 prep||ccomp: 12 nsubjpass||ccomp: 15 pcomp||prep: 14
|
||||
amod||dobj: 9 attr||xcomp: 14 nmod||nsubj: 17 prep||advcl: 2 prep||prep: 5
|
||||
nsubj||conj: 12 advcl||advmod: 18 ccomp||advmod: 11 ccomp||pcomp: 5 acl||pobj:
|
||||
10 npadvmod||acomp: 7 dobj||pcomp: 14 nsubjpass||pcomp: 1 nmod||pobj: 8
|
||||
amod||attr: 6 nmod||dobj: 12 aux||conj: 1 neg||conj: 1 dative||xcomp: 11
|
||||
pobj||dative: 3 xcomp||acomp: 19 advcl||pobj: 2 nsubj||advcl: 2 csubj||ccomp: 1
|
||||
advcl||acl: 1 relcl||nmod: 2 dobj||advcl: 10 advmod||advcl: 3 nmod||nsubjpass: 6
|
||||
amod||pobj: 5 cc||neg: 1 attr||ccomp: 16 advcl||xcomp: 3 nmod||attr: 4
|
||||
advcl||nsubjpass: 5 advcl||ccomp: 4 ccomp||conj: 1 punct||acl: 1 meta||acl: 1
|
||||
parataxis||acl: 1 prep||acl: 1 amod||nsubj: 7 ccomp||ccomp: 3 acomp||xcomp: 5
|
||||
dobj||acl: 5 prep||oprd: 6 advmod||acl: 2 dative||advcl: 1 pobj||agent: 5
|
||||
xcomp||amod: 1 dep||advcl: 1 prep||amod: 8 relcl||compound: 1 advcl||csubj: 3
|
||||
npadvmod||conj: 2 npadvmod||xcomp: 4 advmod||nsubj: 3 ccomp||amod: 7
|
||||
advcl||conj: 1 nmod||conj: 2 advmod||nsubjpass: 2 dep||xcomp: 2 appos||ccomp: 1
|
||||
advmod||dep: 1 advmod||advmod: 5 aux||xcomp: 8 dep||advmod: 1 dative||ccomp: 2
|
||||
prep||dep: 1 conj||conj: 1 dep||ccomp: 4 cc||ROOT: 1 prep||ROOT: 1 nsubj||pcomp:
|
||||
3 advmod||prep: 2 relcl||dative: 1 acl||conj: 1 advcl||attr: 4 prep||npadvmod: 1
|
||||
nsubjpass||xcomp: 1 neg||advmod: 1 xcomp||oprd: 1 advcl||advcl: 1 dobj||dep: 3
|
||||
nsubjpass||parataxis: 1 attr||pcomp: 1 ccomp||parataxis: 1 advmod||attr: 1
|
||||
nmod||oprd: 1 appos||nmod: 2 advmod||relcl: 1 appos||npadvmod: 1 appos||conj: 1
|
||||
prep||expl: 1 nsubjpass||conj: 1 punct||pobj: 1 cc||pobj: 1 conj||pobj: 1
|
||||
punct||conj: 1 ccomp||dep: 1 oprd||xcomp: 3 ccomp||xcomp: 1 ccomp||nsubj: 1
|
||||
nmod||dep: 1 xcomp||ccomp: 1 acomp||advcl: 1 intj||advmod: 1 advmod||acomp: 2
|
||||
relcl||oprd: 1 advmod||prt: 1 advmod||pobj: 1 appos||nummod: 1 relcl||npadvmod:
|
||||
3 mark||advcl: 1 aux||ccomp: 1 amod||nsubjpass: 1 npadvmod||advmod: 1 conj||dep:
|
||||
1 nummod||pobj: 1 amod||npadvmod: 1 intj||pobj: 1 nummod||npadvmod: 1
|
||||
xcomp||xcomp: 1 aux||dep: 1 advcl||relcl: 1
|
||||
⚠ The following labels were found only in the train data: xcomp||amod,
|
||||
advcl||relcl, prep||nsubjpass, acl||nsubj, nsubjpass||conj, xcomp||oprd,
|
||||
advmod||conj, advmod||advmod, iobj, advmod||nsubjpass, dobj||conj, ccomp||amod,
|
||||
meta||acl, xcomp||xcomp, prep||attr, prep||ccomp, advcl||acomp, acl||dobj,
|
||||
advcl||advcl, pobj||agent, prep||advcl, nsubjpass||xcomp, prep||dep,
|
||||
acomp||xcomp, aux||ccomp, ccomp||dep, conj||dep, relcl||compound,
|
||||
nsubjpass||ccomp, nmod||dobj, advmod||advcl, advmod||acl, dobj||advcl,
|
||||
dative||xcomp, prep||nsubj, ccomp||ccomp, nsubj||ccomp, xcomp||acomp,
|
||||
prep||acomp, dep||advmod, acl||pobj, appos||dobj, npadvmod||acomp, cc||ROOT,
|
||||
relcl||nsubj, nmod||pobj, acl||nsubjpass, ccomp||advmod, pcomp||prep,
|
||||
amod||dobj, advmod||attr, advcl||csubj, appos||attr, dobj||pcomp, prep||ROOT,
|
||||
relcl||pobj, advmod||pobj, amod||nsubj, ccomp||xcomp, prep||oprd,
|
||||
npadvmod||advmod, appos||nummod, advcl||pobj, neg||advmod, acl||attr,
|
||||
appos||nsubjpass, csubj||ccomp, amod||nsubjpass, intj||pobj, dep||advcl,
|
||||
cc||neg, xcomp||ccomp, dative||ccomp, nmod||oprd, pobj||dative, prep||dobj,
|
||||
dep||ccomp, relcl||attr, ccomp||nsubj, advcl||xcomp, nmod||dep, advcl||advmod,
|
||||
ccomp||conj, pobj||prep, advmod||acomp, advmod||relcl, attr||pcomp,
|
||||
ccomp||parataxis, oprd||xcomp, intj||advmod, nmod||nsubjpass, prep||npadvmod,
|
||||
parataxis||acl, prep||pobj, advcl||dobj, amod||pobj, prep||acl, conj||pobj,
|
||||
advmod||dep, punct||pobj, ccomp||acomp, acomp||advcl, nummod||npadvmod,
|
||||
dobj||dep, npadvmod||xcomp, advcl||conj, relcl||npadvmod, punct||acl,
|
||||
relcl||dobj, dobj||xcomp, nsubjpass||parataxis, dative||advcl, relcl||nmod,
|
||||
advcl||ccomp, appos||npadvmod, ccomp||pcomp, prep||amod, mark||advcl,
|
||||
prep||advmod, prep||xcomp, appos||nsubj, attr||ccomp, advmod||prt, dobj||ccomp,
|
||||
aux||conj, advcl||nsubj, conj||conj, advmod||ccomp, advcl||nsubjpass,
|
||||
attr||xcomp, nmod||conj, npadvmod||conj, relcl||dative, prep||expl,
|
||||
nsubjpass||pcomp, advmod||xcomp, advmod||dobj, appos||pobj, nsubj||conj,
|
||||
relcl||nsubjpass, advcl||attr, appos||ccomp, advmod||prep, prep||conj,
|
||||
nmod||attr, punct||conj, neg||conj, dep||xcomp, aux||xcomp, dobj||acl,
|
||||
nummod||pobj, amod||npadvmod, nsubj||pcomp, advcl||acl, appos||nmod,
|
||||
relcl||oprd, prep||prep, cc||pobj, nmod||nsubj, amod||attr, aux||dep,
|
||||
appos||conj, advmod||nsubj, nsubj||advcl, acl||conj
|
||||
To train a parser, your data should include at least 20 instances of each label.
|
||||
⚠ Multiple root labels (ROOT, nsubj, aux, npadvmod, prep) found in
|
||||
training data. spaCy's parser uses a single root label ROOT so this distinction
|
||||
will not be available.
|
||||
|
||||
================================== Summary ==================================
|
||||
✔ 5 checks passed
|
||||
⚠ 8 warnings
|
||||
```
|
||||
|
||||
</Accordion>
|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`spacy debug-data`](/api/cli#debug-data)
|
||||
|
||||
</Infobox>
|
||||
|
||||
## Backwards incompatibilities {id="incompat"}
|
||||
|
||||
<Infobox title="Important note on models" variant="warning">
|
||||
|
||||
If you've been training **your own models**, you'll need to **retrain** them
|
||||
with the new version. Also don't forget to upgrade all models to the latest
|
||||
versions. Models for v2.0 or v2.1 aren't compatible with models for v2.2. To
|
||||
check if all of your models are up to date, you can run the
|
||||
[`spacy validate`](/api/cli#validate) command.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Install with lookups data
|
||||
>
|
||||
> ```bash
|
||||
> $ pip install spacy[lookups]
|
||||
> ```
|
||||
>
|
||||
> You can also install
|
||||
> [`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data)
|
||||
> directly.
|
||||
|
||||
- The lemmatization tables have been moved to their own package,
|
||||
[`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data), which
|
||||
is not installed by default. If you're using pretrained models, **nothing
|
||||
changes**, because the tables are now included in the model packages. If you
|
||||
want to use the lemmatizer for other languages that don't yet have pretrained
|
||||
models (e.g. Turkish or Croatian) or start off with a blank model that
|
||||
contains lookup data (e.g. `spacy.blank("en")`), you'll need to **explicitly
|
||||
install spaCy plus data** via `pip install spacy[lookups]`.
|
||||
- Lemmatization tables (rules, exceptions, index and lookups) are now part of
|
||||
the `Vocab` and serialized with it. This means that serialized objects (`nlp`,
|
||||
pipeline components, vocab) will now include additional data, and models
|
||||
written to disk will include additional files.
|
||||
- The [`Lemmatizer`](/api/lemmatizer) class is now initialized with an instance
|
||||
of [`Lookups`](/api/lookups) containing the rules and tables, instead of dicts
|
||||
as separate arguments. This makes it easier to share data tables and modify
|
||||
them at runtime. This is mostly internals, but if you've been implementing a
|
||||
custom `Lemmatizer`, you'll need to update your code.
|
||||
- The [Dutch model](/models/nl) has been trained on a new NER corpus (custom
|
||||
labelled UD instead of WikiNER), so their predictions may be very different
|
||||
compared to the previous version. The results should be significantly better
|
||||
and more generalizable, though.
|
||||
- The [`spacy download`](/api/cli#download) command does **not** set the
|
||||
`--no-deps` pip argument anymore by default, meaning that model package
|
||||
dependencies (if available) will now be also downloaded and installed. If
|
||||
spaCy (which is also a model dependency) is not installed in the current
|
||||
environment, e.g. if a user has built from source, `--no-deps` is added back
|
||||
automatically to prevent spaCy from being downloaded and installed again from
|
||||
pip.
|
||||
- The built-in
|
||||
[`biluo_tags_from_offsets`](/api/top-level#biluo_tags_from_offsets) converter
|
||||
is now stricter and will raise an error if entities are overlapping (instead
|
||||
of silently skipping them). If your data contains invalid entity annotations,
|
||||
make sure to clean it and resolve conflicts. You can now also use the new
|
||||
`debug-data` command to find problems in your data.
|
||||
- Pipeline components can now overwrite IOB tags of tokens that are not yet part
|
||||
of an entity. Once a token has an `ent_iob` value set, it won't be reset to an
|
||||
"unset" state and will always have at least `O` assigned. `list(doc.ents)` now
|
||||
actually keeps the annotations on the token level consistent, instead of
|
||||
resetting `O` to an empty string.
|
||||
- The default punctuation in the [`Sentencizer`](/api/sentencizer) has been
|
||||
extended and now includes more characters common in various languages. This
|
||||
also means that the results it produces may change, depending on your text. If
|
||||
you want the previous behavior with limited characters, set
|
||||
`punct_chars=[".", "!", "?"]` on initialization.
|
||||
- The [`PhraseMatcher`](/api/phrasematcher) algorithm was rewritten from scratch
|
||||
and it's now 10× faster. The rewrite also resolved a few subtle bugs
|
||||
with very large terminology lists. So if you were matching large lists, you
|
||||
may see slightly different results – however, the results should now be fully
|
||||
correct. See [this PR](https://github.com/explosion/spaCy/pull/4309) for more
|
||||
details.
|
||||
- The `Serbian` language class (introduced in v2.1.8) incorrectly used the
|
||||
language code `rs` instead of `sr`. This has now been fixed, so `Serbian` is
|
||||
now available via `spacy.lang.sr`.
|
||||
- The `"sources"` in the `meta.json` have changed from a list of strings to a
|
||||
list of dicts. This is mostly internals, but if your code used
|
||||
`nlp.meta["sources"]`, you might have to update it.
|
||||
|
||||
### Migrating from spaCy 2.1 {id="migrating"}
|
||||
|
||||
#### Lemmatization data and lookup tables
|
||||
|
||||
If you application needs lemmatization for [languages](/usage/models#languages)
|
||||
with only tokenizers, you now need to install that data explicitly via
|
||||
`pip install spacy[lookups]` or `pip install spacy-lookups-data`. No additional
|
||||
setup is required – the package just needs to be installed in the same
|
||||
environment as spaCy.
|
||||
|
||||
```python {highlight="3-4"}
|
||||
nlp = Turkish()
|
||||
doc = nlp("Bu bir cümledir.")
|
||||
# 🚨 This now requires the lookups data to be installed explicitly
|
||||
print([token.lemma_ for token in doc])
|
||||
```
|
||||
|
||||
The same applies to blank models that you want to update and train – for
|
||||
instance, you might use [`spacy.blank`](/api/top-level#spacy.blank) to create a
|
||||
blank English model and then train your own part-of-speech tagger on top. If you
|
||||
don't explicitly install the lookups data, that `nlp` object won't have any
|
||||
lemmatization rules available. spaCy will now show you a warning when you train
|
||||
a new part-of-speech tagger and the vocab has no lookups available.
|
||||
|
||||
#### Lemmatizer initialization
|
||||
|
||||
This is mainly internals and should hopefully not affect your code. But if
|
||||
you've been creating custom [`Lemmatizers`](/api/lemmatizer), you'll need to
|
||||
update how they're initialized and pass in an instance of
|
||||
[`Lookups`](/api/lookups) with the (optional) tables `lemma_index`, `lemma_exc`,
|
||||
`lemma_rules` and `lemma_lookup`.
|
||||
|
||||
```diff
|
||||
from spacy.lemmatizer import Lemmatizer
|
||||
+ from spacy.lookups import Lookups
|
||||
|
||||
lemma_index = {"verb": ("cope", "cop")}
|
||||
lemma_exc = {"verb": {"coping": ("cope",)}}
|
||||
lemma_rules = {"verb": [["ing", ""]]}
|
||||
- lemmatizer = Lemmatizer(lemma_index, lemma_exc, lemma_rules)
|
||||
+ lookups = Lookups()
|
||||
+ lookups.add_table("lemma_index", lemma_index)
|
||||
+ lookups.add_table("lemma_exc", lemma_exc)
|
||||
+ lookups.add_table("lemma_rules", lemma_rules)
|
||||
+ lemmatizer = Lemmatizer(lookups)
|
||||
```
|
||||
|
||||
#### Converting entity offsets to BILUO tags
|
||||
|
||||
If you've been using the
|
||||
[`biluo_tags_from_offsets`](/api/top-level#biluo_tags_from_offsets) helper to
|
||||
convert character offsets into token-based BILUO tags, you may now see an error
|
||||
if the offsets contain overlapping tokens and make it impossible to create a
|
||||
valid BILUO sequence. This is helpful, because it lets you spot potential
|
||||
problems in your data that can lead to inconsistent results later on. But it
|
||||
also means that you need to adjust and clean up the offsets before converting
|
||||
them:
|
||||
|
||||
```diff
|
||||
doc = nlp("I live in Berlin Kreuzberg")
|
||||
- entities = [(10, 26, "LOC"), (10, 16, "GPE"), (17, 26, "LOC")]
|
||||
+ entities = [(10, 16, "GPE"), (17, 26, "LOC")]
|
||||
tags = get_biluo_tags_from_offsets(doc, entities)
|
||||
```
|
||||
|
||||
#### Serbian language data
|
||||
|
||||
If you've been working with `Serbian` (introduced in v2.1.8), you'll need to
|
||||
change the language code from `rs` to the correct `sr`:
|
||||
|
||||
```diff
|
||||
- from spacy.lang.rs import Serbian
|
||||
+ from spacy.lang.sr import Serbian
|
||||
```
|
||||
@@ -0,0 +1,344 @@
|
||||
---
|
||||
title: What's New in v2.3
|
||||
teaser: New features, backwards incompatibilities and migration guide
|
||||
menu:
|
||||
- ['New Features', 'features']
|
||||
- ['Backwards Incompatibilities', 'incompat']
|
||||
- ['Migrating from v2.2', 'migrating']
|
||||
---
|
||||
|
||||
## New Features {id="features",hidden="true"}
|
||||
|
||||
spaCy v2.3 features new pretrained models for five languages, word vectors for
|
||||
all language models, and decreased model size and loading times for models with
|
||||
vectors. We've added pretrained models for **Chinese, Danish, Japanese, Polish
|
||||
and Romanian** and updated the training data and vectors for most languages.
|
||||
Model packages with vectors are about **2×** smaller on disk and load
|
||||
**2-4×** faster. For the full changelog, see the
|
||||
[release notes on GitHub](https://github.com/explosion/spaCy/releases/tag/v2.3.0).
|
||||
For more details and a behind-the-scenes look at the new release,
|
||||
[see our blog post](https://explosion.ai/blog/spacy-v2-3).
|
||||
|
||||
### Expanded model families with vectors {id="models"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```bash
|
||||
> python -m spacy download da_core_news_sm
|
||||
> python -m spacy download ja_core_news_sm
|
||||
> python -m spacy download pl_core_news_sm
|
||||
> python -m spacy download ro_core_news_sm
|
||||
> python -m spacy download zh_core_web_sm
|
||||
> ```
|
||||
|
||||
With new model families for Chinese, Danish, Polish, Romanian and Chinese plus
|
||||
`md` and `lg` models with word vectors for all languages, this release provides
|
||||
a total of 46 model packages. For models trained using
|
||||
[Universal Dependencies](https://universaldependencies.org) corpora, the
|
||||
training data has been updated to UD v2.5 (v2.6 for Japanese, v2.3 for Polish)
|
||||
and Dutch has been extended to include both UD Dutch Alpino and LassySmall.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**Models:** [Models directory](/models) **Benchmarks: **
|
||||
[Release notes](https://github.com/explosion/spaCy/releases/tag/v2.3.0)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Chinese {id="chinese"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.lang.zh import Chinese
|
||||
>
|
||||
> # Load with "default" model provided by pkuseg
|
||||
> cfg = {"pkuseg_model": "default", "require_pkuseg": True}
|
||||
> nlp = Chinese(meta={"tokenizer": {"config": cfg}})
|
||||
>
|
||||
> # Append words to user dict
|
||||
> nlp.tokenizer.pkuseg_update_user_dict(["中国", "ABC"])
|
||||
> ```
|
||||
|
||||
This release adds support for
|
||||
[`pkuseg`](https://github.com/lancopku/pkuseg-python) for word segmentation and
|
||||
the new Chinese models ship with a custom pkuseg model trained on OntoNotes. The
|
||||
Chinese tokenizer can be initialized with both `pkuseg` and custom models and
|
||||
the `pkuseg` user dictionary is easy to customize. Note that
|
||||
[`pkuseg`](https://github.com/lancopku/pkuseg-python) doesn't yet ship with
|
||||
pre-compiled wheels for Python 3.8. See the
|
||||
[usage documentation](/usage/models#chinese) for details on how to install it on
|
||||
Python 3.8.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**Models:** [Chinese models](/models/zh) **Usage: **
|
||||
[Chinese tokenizer usage](/usage/models#chinese)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Japanese {id="japanese"}
|
||||
|
||||
The updated Japanese language class switches to
|
||||
[`SudachiPy`](https://github.com/WorksApplications/SudachiPy) for word
|
||||
segmentation and part-of-speech tagging. Using `SudachiPy` greatly simplifies
|
||||
installing spaCy for Japanese, which is now possible with a single command:
|
||||
`pip install spacy[ja]`.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**Models:** [Japanese models](/models/ja) **Usage:**
|
||||
[Japanese tokenizer usage](/usage/models#japanese)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Small CLI updates
|
||||
|
||||
- [`spacy debug-data`](/api/cli#debug-data) provides the coverage of the vectors
|
||||
in a base model with `spacy debug-data lang train dev -b base_model`
|
||||
- [`spacy evaluate`](/api/cli#evaluate) supports `blank:lg` (e.g.
|
||||
`spacy evaluate blank:en dev.json`) to evaluate the tokenization accuracy
|
||||
without loading a model
|
||||
- [`spacy train`](/api/cli#train) on GPU restricts the CPU timing evaluation to
|
||||
the first iteration
|
||||
|
||||
## Backwards incompatibilities {id="incompat"}
|
||||
|
||||
<Infobox title="Important note on models" variant="warning">
|
||||
|
||||
If you've been training **your own models**, you'll need to **retrain** them
|
||||
with the new version. Also don't forget to upgrade all models to the latest
|
||||
versions. Models for earlier v2 releases (v2.0, v2.1, v2.2) aren't compatible
|
||||
with models for v2.3. To check if all of your models are up to date, you can run
|
||||
the [`spacy validate`](/api/cli#validate) command.
|
||||
|
||||
</Infobox>
|
||||
|
||||
> #### Install with lookups data
|
||||
>
|
||||
> ```bash
|
||||
> $ pip install spacy[lookups]
|
||||
> ```
|
||||
>
|
||||
> You can also install
|
||||
> [`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data)
|
||||
> directly.
|
||||
|
||||
- If you're training new models, you'll want to install the package
|
||||
[`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data), which
|
||||
now includes both the lemmatization tables (as in v2.2) and the normalization
|
||||
tables (new in v2.3). If you're using pretrained models, **nothing changes**,
|
||||
because the relevant tables are included in the model packages.
|
||||
- Due to the updated Universal Dependencies training data, the fine-grained
|
||||
part-of-speech tags will change for many provided language models. The
|
||||
coarse-grained part-of-speech tagset remains the same, but the mapping from
|
||||
particular fine-grained to coarse-grained tags may show minor differences.
|
||||
- For French, Italian, Portuguese and Spanish, the fine-grained part-of-speech
|
||||
tagsets contain new merged tags related to contracted forms, such as `ADP_DET`
|
||||
for French `"au"`, which maps to UPOS `ADP` based on the head `"à"`. This
|
||||
increases the accuracy of the models by improving the alignment between
|
||||
spaCy's tokenization and Universal Dependencies multi-word tokens used for
|
||||
contractions.
|
||||
|
||||
### Migrating from spaCy 2.2 {id="migrating"}
|
||||
|
||||
#### Tokenizer settings
|
||||
|
||||
In spaCy v2.2.2-v2.2.4, there was a change to the precedence of `token_match`
|
||||
that gave prefixes and suffixes priority over `token_match`, which caused
|
||||
problems for many custom tokenizer configurations. This has been reverted in
|
||||
v2.3 so that `token_match` has priority over prefixes and suffixes as in v2.2.1
|
||||
and earlier versions.
|
||||
|
||||
A new tokenizer setting `url_match` has been introduced in v2.3.0 to handle
|
||||
cases like URLs where the tokenizer should remove prefixes and suffixes (e.g., a
|
||||
comma at the end of a URL) before applying the match. See the full
|
||||
[tokenizer documentation](/usage/linguistic-features#tokenization) and try out
|
||||
[`nlp.tokenizer.explain()`](/usage/linguistic-features#tokenizer-debug) when
|
||||
debugging your tokenizer configuration.
|
||||
|
||||
#### Warnings configuration
|
||||
|
||||
spaCy's custom warnings have been replaced with native Python
|
||||
[`warnings`](https://docs.python.org/3/library/warnings.html). Instead of
|
||||
setting `SPACY_WARNING_IGNORE`, use the
|
||||
[`warnings` filters](https://docs.python.org/3/library/warnings.html#the-warnings-filter)
|
||||
to manage warnings.
|
||||
|
||||
```diff
|
||||
import spacy
|
||||
+ import warnings
|
||||
|
||||
- spacy.errors.SPACY_WARNING_IGNORE.append('W007')
|
||||
+ warnings.filterwarnings("ignore", message=r"\\[W007\\]", category=UserWarning)
|
||||
```
|
||||
|
||||
#### Normalization tables
|
||||
|
||||
The normalization tables have moved from the language data in
|
||||
[`spacy/lang`](https://github.com/explosion/spacy/tree/v2.x/spacy/lang) to the
|
||||
package [`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data).
|
||||
If you're adding data for a new language, the normalization table should be
|
||||
added to `spacy-lookups-data`. See
|
||||
[adding norm exceptions](/usage/adding-languages#norm-exceptions).
|
||||
|
||||
#### No preloaded vocab for models with vectors
|
||||
|
||||
To reduce the initial loading time, the lexemes in `nlp.vocab` are no longer
|
||||
loaded on initialization for models with vectors. As you process texts, the
|
||||
lexemes will be added to the vocab automatically, just as in small models
|
||||
without vectors.
|
||||
|
||||
To see the number of unique vectors and number of words with vectors, see
|
||||
`nlp.meta['vectors']`, for example for `en_core_web_md` there are `20000` unique
|
||||
vectors and `684830` words with vectors:
|
||||
|
||||
```python
|
||||
{
|
||||
'width': 300,
|
||||
'vectors': 20000,
|
||||
'keys': 684830,
|
||||
'name': 'en_core_web_md.vectors'
|
||||
}
|
||||
```
|
||||
|
||||
If required, for instance if you are working directly with word vectors rather
|
||||
than processing texts, you can load all lexemes for words with vectors at once:
|
||||
|
||||
```python
|
||||
for orth in nlp.vocab.vectors:
|
||||
_ = nlp.vocab[orth]
|
||||
```
|
||||
|
||||
If your workflow previously iterated over `nlp.vocab`, a similar alternative is
|
||||
to iterate over words with vectors instead:
|
||||
|
||||
```diff
|
||||
- lexemes = [w for w in nlp.vocab]
|
||||
+ lexemes = [nlp.vocab[orth] for orth in nlp.vocab.vectors]
|
||||
```
|
||||
|
||||
Be aware that the set of preloaded lexemes in a v2.2 model is not equivalent to
|
||||
the set of words with vectors. For English, v2.2 `md/lg` models have 1.3M
|
||||
provided lexemes but only 685K words with vectors. The vectors have been updated
|
||||
for most languages in v2.2, but the English models contain the same vectors for
|
||||
both v2.2 and v2.3.
|
||||
|
||||
#### Lexeme.is_oov and Token.is_oov
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
Due to a bug, the values for `is_oov` are reversed in v2.3.0, but this will be
|
||||
fixed in the next patch release v2.3.1.
|
||||
|
||||
</Infobox>
|
||||
|
||||
In v2.3, `Lexeme.is_oov` and `Token.is_oov` are `True` if the lexeme does not
|
||||
have a word vector. This is equivalent to `token.orth not in nlp.vocab.vectors`.
|
||||
|
||||
Previously in v2.2, `is_oov` corresponded to whether a lexeme had stored
|
||||
probability and cluster features. The probability and cluster features are no
|
||||
longer included in the provided medium and large models (see the next section).
|
||||
|
||||
#### Probability and cluster features
|
||||
|
||||
> #### Load and save extra prob lookups table
|
||||
>
|
||||
> ```python
|
||||
> from spacy.lang.en import English
|
||||
> nlp = English()
|
||||
> doc = nlp("the")
|
||||
> print(doc[0].prob) # lazily loads extra prob table
|
||||
> nlp.to_disk("/path/to/model") # includes prob table
|
||||
> ```
|
||||
|
||||
The `Token.prob` and `Token.cluster` features, which are no longer used by the
|
||||
core pipeline components as of spaCy v2, are no longer provided in the
|
||||
pretrained models to reduce the model size. To keep these features available for
|
||||
users relying on them, the `prob` and `cluster` features for the most frequent
|
||||
1M tokens have been moved to
|
||||
[`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data) as
|
||||
`extra` features for the relevant languages (English, German, Greek and
|
||||
Spanish).
|
||||
|
||||
The extra tables are loaded lazily, so if you have `spacy-lookups-data`
|
||||
installed and your code accesses `Token.prob`, the full table is loaded into the
|
||||
model vocab, which will take a few seconds on initial loading. When you save
|
||||
this model after loading the `prob` table, the full `prob` table will be saved
|
||||
as part of the model vocab.
|
||||
|
||||
To load the probability table into a provided model, first make sure you have
|
||||
`spacy-lookups-data` installed. To load the table, remove the empty provided
|
||||
`lexeme_prob` table and then access `Lexeme.prob` for any word to load the table
|
||||
from `spacy-lookups-data`:
|
||||
|
||||
```diff
|
||||
+ # prerequisite: pip install spacy-lookups-data
|
||||
import spacy
|
||||
|
||||
nlp = spacy.load("en_core_web_md")
|
||||
|
||||
# remove the empty placeholder prob table
|
||||
+ if nlp.vocab.lookups_extra.has_table("lexeme_prob"):
|
||||
+ nlp.vocab.lookups_extra.remove_table("lexeme_prob")
|
||||
|
||||
# access any `.prob` to load the full table into the model
|
||||
assert nlp.vocab["a"].prob == -3.9297883511
|
||||
|
||||
# if desired, save this model with the probability table included
|
||||
nlp.to_disk("/path/to/model")
|
||||
```
|
||||
|
||||
If you'd like to include custom `cluster`, `prob`, or `sentiment` tables as part
|
||||
of a new model, add the data to
|
||||
[`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data) under
|
||||
the entry point `lg_extra`, e.g. `en_extra` for English. Alternatively, you can
|
||||
initialize your [`Vocab`](/api/vocab) with the `lookups_extra` argument with a
|
||||
[`Lookups`](/api/lookups) object that includes the tables `lexeme_cluster`,
|
||||
`lexeme_prob`, `lexeme_sentiment` or `lexeme_settings`. `lexeme_settings` is
|
||||
currently only used to provide a custom `oov_prob`. See examples in the
|
||||
[`data` directory](https://github.com/explosion/spacy-lookups-data/tree/master/spacy_lookups_data/data)
|
||||
in `spacy-lookups-data`.
|
||||
|
||||
#### Initializing new models without extra lookups tables
|
||||
|
||||
When you initialize a new model with [`spacy init-model`](/api/cli#init-model),
|
||||
the `prob` table from `spacy-lookups-data` may be loaded as part of the
|
||||
initialization. If you'd like to omit this extra data as in spaCy's provided
|
||||
v2.3 models, use the new flag `--omit-extra-lookups`.
|
||||
|
||||
#### Tag maps in provided models vs. blank models
|
||||
|
||||
The tag maps in the provided models may differ from the tag maps in the spaCy
|
||||
library. You can access the tag map in a loaded model under
|
||||
`nlp.vocab.morphology.tag_map`.
|
||||
|
||||
The tag map from `spacy.lang.lg.tag_map` is still used when a blank model is
|
||||
initialized. If you want to provide an alternate tag map, update
|
||||
`nlp.vocab.morphology.tag_map` after initializing the model or if you're using
|
||||
the [train CLI](/api/cli#train), you can use the new `--tag-map-path` option to
|
||||
provide in the tag map as a JSON dict.
|
||||
|
||||
If you want to export a tag map from a provided model for use with the train
|
||||
CLI, you can save it as a JSON dict. To only use string keys as required by JSON
|
||||
and to make it easier to read and edit, any internal integer IDs need to be
|
||||
converted back to strings:
|
||||
|
||||
```python
|
||||
import spacy
|
||||
import srsly
|
||||
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
tag_map = {}
|
||||
|
||||
# convert any integer IDs to strings for JSON
|
||||
for tag, morph in nlp.vocab.morphology.tag_map.items():
|
||||
tag_map[tag] = {}
|
||||
for feat, val in morph.items():
|
||||
feat = nlp.vocab.strings.as_string(feat)
|
||||
if not isinstance(val, bool):
|
||||
val = nlp.vocab.strings.as_string(val)
|
||||
tag_map[tag][feat] = val
|
||||
|
||||
srsly.write_json("tag_map.json", tag_map)
|
||||
```
|
||||
@@ -0,0 +1,613 @@
|
||||
---
|
||||
title: What's New in v2.0
|
||||
teaser: New features, backwards incompatibilities and migration guide
|
||||
menu:
|
||||
- ['Summary', 'summary']
|
||||
- ['New Features', 'features']
|
||||
- ['Backwards Incompatibilities', 'incompat']
|
||||
- ['Migrating from v1.x', 'migrating']
|
||||
---
|
||||
|
||||
We're very excited to finally introduce spaCy v2.0! On this page, you'll find a
|
||||
summary of the new features, information on the backwards incompatibilities,
|
||||
including a handy overview of what's been renamed or deprecated. To help you
|
||||
make the most of v2.0, we also **re-wrote almost all of the usage guides and API
|
||||
docs**, and added more [real-world examples](/usage/examples). If you're new to
|
||||
spaCy, or just want to brush up on some NLP basics and the details of the
|
||||
library, check out the [spaCy 101 guide](/usage/spacy-101) that explains the
|
||||
most important concepts with examples and illustrations.
|
||||
|
||||
## Summary {id="summary"}
|
||||
|
||||
<Grid cols={2}>
|
||||
|
||||
<div>
|
||||
|
||||
This release features entirely new **deep learning-powered models** for spaCy's
|
||||
tagger, parser and entity recognizer. The new models are **10× smaller**, **20%
|
||||
more accurate** and **even cheaper to run** than the previous generation.
|
||||
|
||||
We've also made several usability improvements that are particularly helpful for
|
||||
**production deployments**. spaCy v2 now fully supports the Pickle protocol,
|
||||
making it easy to use spaCy with [Apache Spark](https://spark.apache.org/). The
|
||||
string-to-integer mapping is **no longer stateful**, making it easy to reconcile
|
||||
annotations made in different processes. Models are smaller and use less memory,
|
||||
and the APIs for serialization are now much more consistent. Custom pipeline
|
||||
components let you modify the `Doc` at any stage in the pipeline. You can now
|
||||
also add your own custom attributes, properties and methods to the `Doc`,
|
||||
`Token` and `Span`.
|
||||
|
||||
</div>
|
||||
|
||||
<Infobox title="Table of Contents" id="toc">
|
||||
|
||||
- [Summary](#summary)
|
||||
- [New features](#features)
|
||||
- [Neural network models](#features-models)
|
||||
- [Improved processing pipelines](#features-pipelines)
|
||||
- [Text classification](#features-text-classification)
|
||||
- [Hash values as IDs](#features-hash-ids)
|
||||
- [Improved word vectors support](#features-vectors)
|
||||
- [Saving, loading and serialization](#features-serializer)
|
||||
- [displaCy visualizer](#features-displacy)
|
||||
- [Language data and lazy loading](#features-language)
|
||||
- [Revised matcher API and phrase matcher](#features-matcher)
|
||||
- [Backwards incompatibilities](#incompat)
|
||||
- [Migrating from spaCy v1.x](#migrating)
|
||||
|
||||
</Infobox>
|
||||
|
||||
</Grid>
|
||||
|
||||
The main usability improvements you'll notice in spaCy v2.0 are around
|
||||
**defining, training and loading your own models** and components. The new
|
||||
neural network models make it much easier to train a model from scratch, or
|
||||
update an existing model with a few examples. In v1.x, the statistical models
|
||||
depended on the state of the `Vocab`. If you taught the model a new word, you
|
||||
would have to save and load a lot of data — otherwise the model wouldn't
|
||||
correctly recall the features of your new example. That's no longer the case.
|
||||
|
||||
Due to some clever use of hashing, the statistical models **never change size**,
|
||||
even as they learn new vocabulary items. The whole pipeline is also now fully
|
||||
differentiable. Even if you don't have explicitly annotated data, you can update
|
||||
spaCy using all the **latest deep learning tricks** like adversarial training,
|
||||
noise contrastive estimation or reinforcement learning.
|
||||
|
||||
## New features {id="features"}
|
||||
|
||||
This section contains an overview of the most important **new features and
|
||||
improvements**. The [API docs](/api) include additional deprecation notes.
|
||||
|
||||
### Convolutional neural network models {id="features-models"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```bash
|
||||
> python -m spacy download en_core_web_sm
|
||||
> python -m spacy download de_core_news_sm
|
||||
> python -m spacy download xx_ent_wiki_sm
|
||||
> ```
|
||||
|
||||
spaCy v2.0 features new neural models for tagging, parsing and entity
|
||||
recognition. The models have been designed and implemented from scratch
|
||||
specifically for spaCy, to give you an unmatched balance of speed, size and
|
||||
accuracy. The new models are **10× smaller**, **20% more accurate**, and **even
|
||||
cheaper to run** than the previous generation.
|
||||
|
||||
spaCy v2.0's new neural network models bring significant improvements in
|
||||
accuracy, especially for English Named Entity Recognition. The new
|
||||
[`en_core_web_lg`](/models/en#en_core_web_lg) model makes about **25% fewer
|
||||
mistakes** than the corresponding v1.x model and is within **1% of the current
|
||||
state-of-the-art**
|
||||
([Strubell et al., 2017](https://arxiv.org/pdf/1702.02098.pdf)). The v2.0 models
|
||||
are also cheaper to run at scale, as they require **under 1 GB of memory** per
|
||||
process.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**Usage:** [Models directory](/models)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Improved processing pipelines {id="features-pipelines"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> # Set custom attributes
|
||||
> Doc.set_extension("my_attr", default=False)
|
||||
> Token.set_extension("my_attr", getter=my_token_getter)
|
||||
> assert doc._.my_attr, token._.my_attr
|
||||
>
|
||||
> # Add components to the pipeline
|
||||
> my_component = lambda doc: doc
|
||||
> nlp.add_pipe(my_component)
|
||||
> ```
|
||||
|
||||
It's now much easier to **customize the pipeline** with your own components:
|
||||
functions that receive a `Doc` object, modify and return it. Extensions let you
|
||||
write any **attributes, properties and methods** to the `Doc`, `Token` and
|
||||
`Span`. You can add data, implement new features, integrate other libraries with
|
||||
spaCy or plug in your own machine learning models.
|
||||
|
||||

|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`Language`](/api/language),
|
||||
[`Doc.set_extension`](/api/doc#set_extension),
|
||||
[`Span.set_extension`](/api/span#set_extension),
|
||||
[`Token.set_extension`](/api/token#set_extension) **Usage:**
|
||||
[Processing pipelines](/usage/processing-pipelines) **Code:**
|
||||
[Pipeline examples](/usage/examples#section-pipeline)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Text classification {id="features-text-classification"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> textcat = nlp.create_pipe("textcat")
|
||||
> nlp.add_pipe(textcat, last=True)
|
||||
> nlp.begin_training()
|
||||
> for itn in range(100):
|
||||
> for doc, gold in train_data:
|
||||
> nlp.update([doc], [gold])
|
||||
> doc = nlp("This is a text.")
|
||||
> print(doc.cats)
|
||||
> ```
|
||||
|
||||
spaCy v2.0 lets you add text categorization models to spaCy pipelines. The model
|
||||
supports classification with multiple, non-mutually exclusive labels – so
|
||||
multiple labels can apply at once. You can change the model architecture rather
|
||||
easily, but by default, the `TextCategorizer` class uses a convolutional neural
|
||||
network to assign position-sensitive vectors to each word in the document.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`TextCategorizer`](/api/textcategorizer),
|
||||
[`Doc.cats`](/api/doc#attributes), `GoldParse.cats` **Usage:**
|
||||
[Training a text classification model](/usage/training#textcat)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Hash values instead of integer IDs {id="features-hash-ids"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("I love coffee")
|
||||
> assert doc.vocab.strings["coffee"] == 3197928453018144401
|
||||
> assert doc.vocab.strings[3197928453018144401] == "coffee"
|
||||
>
|
||||
> beer_hash = doc.vocab.strings.add("beer")
|
||||
> assert doc.vocab.strings["beer"] == beer_hash
|
||||
> assert doc.vocab.strings[beer_hash] == "beer"
|
||||
> ```
|
||||
|
||||
The [`StringStore`](/api/stringstore) now resolves all strings to hash values
|
||||
instead of integer IDs. This means that the string-to-int mapping **no longer
|
||||
depends on the vocabulary state**, making a lot of workflows much simpler,
|
||||
especially during training. Unlike integer IDs in spaCy v1.x, hash values will
|
||||
**always match** – even across models. Strings can now be added explicitly using
|
||||
the new [`Stringstore.add`](/api/stringstore#add) method. A token's hash is
|
||||
available via `token.orth`.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`StringStore`](/api/stringstore) **Usage:**
|
||||
[Vocab, hashes and lexemes 101](/usage/spacy-101#vocab)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Improved word vectors support {id="features-vectors"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> for word, vector in vector_data:
|
||||
> nlp.vocab.set_vector(word, vector)
|
||||
> nlp.vocab.vectors.from_glove("/path/to/vectors")
|
||||
> # Keep 10000 unique vectors and remap the rest
|
||||
> nlp.vocab.prune_vectors(10000)
|
||||
> nlp.to_disk("/model")
|
||||
> ```
|
||||
|
||||
The new [`Vectors`](/api/vectors) class helps the `Vocab` manage the vectors
|
||||
assigned to strings, and lets you assign vectors individually, or
|
||||
[load in GloVe vectors](/usage/linguistic-features#adding-vectors) from a
|
||||
directory. To help you strike a good balance between coverage and memory usage,
|
||||
the `Vectors` class lets you map **multiple keys** to the **same row** of the
|
||||
table. If you're using the [`spacy init-model`](/api/cli#init-model) command to
|
||||
create a vocabulary, pruning the vectors will be taken care of automatically if
|
||||
you set the `--prune-vectors` flag. Otherwise, you can use the new
|
||||
[`Vocab.prune_vectors`](/api/vocab#prune_vectors).
|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`Vectors`](/api/vectors), [`Vocab`](/api/vocab) **Usage:**
|
||||
[Word vectors and semantic similarity](/usage/vectors-similarity)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Saving, loading and serialization {id="features-serializer"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> nlp = spacy.load("en") # shortcut link
|
||||
> nlp = spacy.load("en_core_web_sm") # package
|
||||
> nlp = spacy.load("/path/to/en") # unicode path
|
||||
> nlp = spacy.load(Path("/path/to/en")) # pathlib Path
|
||||
>
|
||||
> nlp.to_disk("/path/to/nlp")
|
||||
> nlp = English().from_disk("/path/to/nlp")
|
||||
> ```
|
||||
|
||||
spaCy's serialization API has been made consistent across classes and objects.
|
||||
All container classes, i.e. `Language`, `Doc`, `Vocab` and `StringStore` now
|
||||
have a `to_bytes()`, `from_bytes()`, `to_disk()` and `from_disk()` method that
|
||||
supports the Pickle protocol.
|
||||
|
||||
The improved `spacy.load` makes loading models easier and more transparent. You
|
||||
can load a model by supplying its shortcut link, the name of an installed
|
||||
[model package](/models) or a path. The `Language` class to initialize will be
|
||||
determined based on the model's settings. For a blank language, you can import
|
||||
the class directly, e.g. `from spacy.lang.en import English` or use
|
||||
[`spacy.blank()`](/api/top-level#spacy.blank).
|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`spacy.load`](/api/top-level#spacy.load),
|
||||
[`Language.to_disk`](/api/language#to_disk) **Usage:**
|
||||
[Models](/usage/models#usage),
|
||||
[Saving and loading](/usage/saving-loading#models)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### displaCy visualizer with Jupyter support {id="features-displacy"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy import displacy
|
||||
> doc = nlp("This is a sentence about Facebook.")
|
||||
> displacy.serve(doc, style="dep") # run the web server
|
||||
> html = displacy.render(doc, style="ent") # generate HTML
|
||||
> ```
|
||||
|
||||
Our popular dependency and named entity visualizers are now an official part of
|
||||
the spaCy library. displaCy can run a simple web server, or generate raw HTML
|
||||
markup or SVG files to be exported. You can pass in one or more docs, and
|
||||
customize the style. displaCy also auto-detects whether you're running
|
||||
[Jupyter](https://jupyter.org) and will render the visualizations in your
|
||||
notebook.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`displacy`](/api/top-level#displacy) **Usage:**
|
||||
[Visualizing spaCy](/usage/visualizers)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Improved language data and lazy loading {id="features-language"}
|
||||
|
||||
Language-specific data now lives in its own submodule, `spacy.lang`. Languages
|
||||
are lazy-loaded, i.e. only loaded when you import a `Language` class, or load a
|
||||
model that initializes one. This allows languages to contain more custom data,
|
||||
e.g. lemmatizer lookup tables, or complex regular expressions. The language data
|
||||
has also been tidied up and simplified. spaCy now also supports simple
|
||||
lookup-based lemmatization – and **many new languages**!
|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`Language`](/api/language) **Code:**
|
||||
[`spacy/lang`](https://github.com/explosion/spacy/tree/v2.x/spacy/lang)
|
||||
**Usage:** [Adding languages](/usage/adding-languages)
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Revised matcher API and phrase matcher {id="features-matcher"}
|
||||
|
||||
> #### Example
|
||||
>
|
||||
> ```python
|
||||
> from spacy.matcher import Matcher, PhraseMatcher
|
||||
>
|
||||
> matcher = Matcher(nlp.vocab)
|
||||
> matcher.add('HEARTS', None, [{"ORTH": "❤️", "OP": '+'}])
|
||||
>
|
||||
> phrasematcher = PhraseMatcher(nlp.vocab)
|
||||
> phrasematcher.add("OBAMA", None, nlp("Barack Obama"))
|
||||
> ```
|
||||
|
||||
Patterns can now be added to the matcher by calling
|
||||
[`matcher.add()`](/api/matcher#add) with a match ID, an optional callback
|
||||
function to be invoked on each match, and one or more patterns. This allows you
|
||||
to write powerful, pattern-specific logic using only one matcher. For example,
|
||||
you might only want to merge some entity types, and set custom flags for other
|
||||
matched patterns. The new [`PhraseMatcher`](/api/phrasematcher) lets you
|
||||
efficiently match very large terminology lists using `Doc` objects as match
|
||||
patterns.
|
||||
|
||||
<Infobox>
|
||||
|
||||
**API:** [`Matcher`](/api/matcher), [`PhraseMatcher`](/api/phrasematcher)
|
||||
**Usage:** [Rule-based matching](/usage/rule-based-matching)
|
||||
|
||||
</Infobox>
|
||||
|
||||
## Backwards incompatibilities {id="incompat"}
|
||||
|
||||
The following modules, classes and methods have changed between v1.x and v2.0.
|
||||
|
||||
| Old | New |
|
||||
| ------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `spacy.download.en`, `spacy.download.de` | [`cli.download`](/api/cli#download) |
|
||||
| `spacy.en` etc. | `spacy.lang.en` etc. |
|
||||
| `spacy.en.word_sets` | `spacy.lang.en.stop_words` |
|
||||
| `spacy.orth` | `spacy.lang.xx.lex_attrs` |
|
||||
| `spacy.syntax.iterators` | `spacy.lang.xx.syntax_iterators` |
|
||||
| `spacy.tagger.Tagger` | `spacy.pipeline.Tagger` |
|
||||
| `spacy.cli.model` | [`spacy.cli.vocab`](/api/cli#vocab) |
|
||||
| `Language.save_to_directory` | [`Language.to_disk`](/api/language#to_disk) |
|
||||
| `Language.end_training` | [`Language.begin_training`](/api/language#begin_training) |
|
||||
| `Language.create_make_doc` | [`Language.tokenizer`](/api/language#attributes) |
|
||||
| `Vocab.resize_vectors` | [`Vectors.resize`](/api/vectors#resize) |
|
||||
| `Vocab.load` `Vocab.load_lexemes` | [`Vocab.from_disk`](/api/vocab#from_disk) [`Vocab.from_bytes`](/api/vocab#from_bytes) |
|
||||
| `Vocab.dump` | [`Vocab.to_disk`](/api/vocab#to_disk) [`Vocab.to_bytes`](/api/vocab#to_bytes) |
|
||||
| `Vocab.load_vectors` `Vocab.load_vectors_from_bin_loc` | [`Vectors.from_disk`](/api/vectors#from_disk) [`Vectors.from_bytes`](/api/vectors#from_bytes) [`Vectors.from_glove`](/api/vectors#from_glove) |
|
||||
| `Vocab.dump_vectors` | [`Vectors.to_disk`](/api/vectors#to_disk) [`Vectors.to_bytes`](/api/vectors#to_bytes) |
|
||||
| `StringStore.load` | [`StringStore.from_disk`](/api/stringstore#from_disk) [`StringStore.from_bytes`](/api/stringstore#from_bytes) |
|
||||
| `StringStore.dump` | [`StringStore.to_disk`](/api/stringstore#to_disk) [`StringStore.to_bytes`](/api/stringstore#to_bytes) |
|
||||
| `Tokenizer.load` | [`Tokenizer.from_disk`](/api/tokenizer#from_disk) [`Tokenizer.from_bytes`](/api/tokenizer#from_bytes) |
|
||||
| `Tagger.load` | [`Tagger.from_disk`](/api/tagger#from_disk) [`Tagger.from_bytes`](/api/tagger#from_bytes) |
|
||||
| `Tagger.tag_names` | `Tagger.labels` |
|
||||
| `DependencyParser.load` | [`DependencyParser.from_disk`](/api/dependencyparser#from_disk) [`DependencyParser.from_bytes`](/api/dependencyparser#from_bytes) |
|
||||
| `EntityRecognizer.load` | [`EntityRecognizer.from_disk`](/api/entityrecognizer#from_disk) [`EntityRecognizer.from_bytes`](/api/entityrecognizer#from_bytes) |
|
||||
| `Matcher.load` | - |
|
||||
| `Matcher.add_pattern` `Matcher.add_entity` | [`Matcher.add`](/api/matcher#add) [`PhraseMatcher.add`](/api/phrasematcher#add) |
|
||||
| `Matcher.get_entity` | [`Matcher.get`](/api/matcher#get) |
|
||||
| `Matcher.has_entity` | [`Matcher.has_key`](/api/matcher#has_key) |
|
||||
| `Doc.read_bytes` | [`Doc.to_bytes`](/api/doc#to_bytes) [`Doc.from_bytes`](/api/doc#from_bytes) [`Doc.to_disk`](/api/doc#to_disk) [`Doc.from_disk`](/api/doc#from_disk) |
|
||||
| `Token.is_ancestor_of` | [`Token.is_ancestor`](/api/token#is_ancestor) |
|
||||
|
||||
### Deprecated {id="deprecated"}
|
||||
|
||||
The following methods are deprecated. They can still be used, but should be
|
||||
replaced.
|
||||
|
||||
| Old | New |
|
||||
| ---------------------------- | ----------------------------------------------- |
|
||||
| `Tokenizer.tokens_from_list` | [`Doc`](/api/doc) |
|
||||
| `Span.sent_start` | [`Span.is_sent_start`](/api/span#is_sent_start) |
|
||||
|
||||
## Migrating from spaCy 1.x {id="migrating"}
|
||||
|
||||
Because we'e made so many architectural changes to the library, we've tried to
|
||||
**keep breaking changes to a minimum**. A lot of projects follow the philosophy
|
||||
that if you're going to break anything, you may as well break everything. We
|
||||
think migration is easier if there's a logic to what has changed. We've
|
||||
therefore followed a policy of avoiding breaking changes to the `Doc`, `Span`
|
||||
and `Token` objects. This way, you can focus on only migrating the code that
|
||||
does training, loading and serialization — in other words, code that works with
|
||||
the `nlp` object directly. Code that uses the annotations should continue to
|
||||
work.
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
If you've trained your own models, keep in mind that your train and runtime
|
||||
inputs must match. This means you'll have to **retrain your models** with spaCy
|
||||
v2.0.
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Document processing {id="migrating-document-processing"}
|
||||
|
||||
The [`Language.pipe`](/api/language#pipe) method allows spaCy to batch
|
||||
documents, which brings a **significant performance advantage** in v2.0. The new
|
||||
neural networks introduce some overhead per batch, so if you're processing a
|
||||
number of documents in a row, you should use `nlp.pipe` and process the texts as
|
||||
a stream.
|
||||
|
||||
```diff
|
||||
- docs = (nlp(text) for text in texts)
|
||||
|
||||
+ docs = nlp.pipe(texts)
|
||||
```
|
||||
|
||||
To make usage easier, there's now a boolean `as_tuples` keyword argument, that
|
||||
lets you pass in an iterator of `(text, context)` pairs, so you can get back an
|
||||
iterator of `(doc, context)` tuples.
|
||||
|
||||
### Saving, loading and serialization {id="migrating-saving-loading"}
|
||||
|
||||
Double-check all calls to `spacy.load()` and make sure they don't use the `path`
|
||||
keyword argument. If you're only loading in binary data and not a model package
|
||||
that can construct its own `Language` class and pipeline, you should now use the
|
||||
[`Language.from_disk`](/api/language#from_disk) method.
|
||||
|
||||
```diff
|
||||
- nlp = spacy.load("en", path="/model")
|
||||
|
||||
+ nlp = spacy.load("/model")
|
||||
+ nlp = spacy.blank("en").from_disk("/model/data")
|
||||
```
|
||||
|
||||
Review all other code that writes state to disk or bytes. All containers, now
|
||||
share the same, consistent API for saving and loading. Replace saving with
|
||||
`to_disk()` or `to_bytes()`, and loading with `from_disk()` and `from_bytes()`.
|
||||
|
||||
```diff
|
||||
- nlp.save_to_directory("/model")
|
||||
- nlp.vocab.dump("/vocab")
|
||||
|
||||
+ nlp.to_disk("/model")
|
||||
+ nlp.vocab.to_disk("/vocab")
|
||||
```
|
||||
|
||||
If you've trained models with input from v1.x, you'll need to **retrain them**
|
||||
with spaCy v2.0. All previous models will not be compatible with the new
|
||||
version.
|
||||
|
||||
### Processing pipelines and language data {id="migrating-languages"}
|
||||
|
||||
If you're importing language data or `Language` classes, make sure to change
|
||||
your import statements to import from `spacy.lang`. If you've added your own
|
||||
custom language, it needs to be moved to `spacy/lang/xx` and adjusted
|
||||
accordingly.
|
||||
|
||||
```diff
|
||||
- from spacy.en import English
|
||||
|
||||
+ from spacy.lang.en import English
|
||||
```
|
||||
|
||||
If you've been using custom pipeline components, check out the new guide on
|
||||
[processing pipelines](/usage/processing-pipelines). Pipeline components are now
|
||||
`(name, func)` tuples. Appending them to the pipeline still works – but the
|
||||
[`add_pipe`](/api/language#add_pipe) method now makes this much more convenient.
|
||||
Methods for removing, renaming, replacing and retrieving components have been
|
||||
added as well. Components can now be disabled by passing a list of their names
|
||||
to the `disable` keyword argument on load, or by using
|
||||
[`disable_pipes`](/api/language#disable_pipes) as a method or context manager:
|
||||
|
||||
```diff
|
||||
- nlp = spacy.load("en_core_web_sm", tagger=False, entity=False)
|
||||
- doc = nlp("I don't want parsed", parse=False)
|
||||
|
||||
+ nlp = spacy.load("en_core_web_sm", disable=["tagger", "ner"])
|
||||
+ with nlp.disable_pipes("parser"):
|
||||
+ doc = nlp("I don't want parsed")
|
||||
```
|
||||
|
||||
To add spaCy's built-in pipeline components to your pipeline, you can still
|
||||
import and instantiate them directly – but it's more convenient to use the new
|
||||
[`create_pipe`](/api/language#create_pipe) method with the component name, i.e.
|
||||
`'tagger'`, `'parser'`, `'ner'` or `'textcat'`.
|
||||
|
||||
```diff
|
||||
- from spacy.pipeline import Tagger
|
||||
- tagger = Tagger(nlp.vocab)
|
||||
- nlp.pipeline.insert(0, tagger)
|
||||
|
||||
+ tagger = nlp.create_pipe("tagger")
|
||||
+ nlp.add_pipe(tagger, first=True)
|
||||
```
|
||||
|
||||
### Training {id="migrating-training"}
|
||||
|
||||
All built-in pipeline components are now subclasses of [`Pipe`](/api/pipe),
|
||||
fully trainable and serializable, and follow the same API. Instead of updating
|
||||
the model and telling spaCy when to _stop_, you can now explicitly call
|
||||
[`begin_training`](/api/language#begin_training), which returns an optimizer you
|
||||
can pass into the [`update`](/api/language#update) function. While `update`
|
||||
still accepts sequences of `Doc` and `GoldParse` objects, you can now also pass
|
||||
in a list of strings and dictionaries describing the annotations. We call this
|
||||
the ["simple training style"](/usage/training#training-simple-style). This is
|
||||
also the recommended usage, as it removes one layer of abstraction from the
|
||||
training.
|
||||
|
||||
```diff
|
||||
- for itn in range(1000):
|
||||
- for text, entities in train_data:
|
||||
- doc = Doc(text)
|
||||
- gold = GoldParse(doc, entities=entities)
|
||||
- nlp.update(doc, gold)
|
||||
- nlp.end_training()
|
||||
- nlp.save_to_directory("/model")
|
||||
|
||||
+ nlp.begin_training()
|
||||
+ for itn in range(1000):
|
||||
+ for texts, annotations in train_data:
|
||||
+ nlp.update(texts, annotations)
|
||||
+ nlp.to_disk("/model")
|
||||
```
|
||||
|
||||
### Attaching custom data to the Doc {id="migrating-doc"}
|
||||
|
||||
Previously, you had to create a new container in order to attach custom data to
|
||||
a `Doc` object. This often required converting the `Doc` objects to and from
|
||||
arrays. In spaCy v2.0, you can set your own attributes, properties and methods
|
||||
on the `Doc`, `Token` and `Span` via
|
||||
[custom extensions](/usage/processing-pipelines#custom-components-attributes).
|
||||
This means that your application can – and should – only pass around `Doc`
|
||||
objects and refer to them as the single source of truth.
|
||||
|
||||
```diff
|
||||
- doc = nlp("This is a regular doc")
|
||||
- doc_array = doc.to_array(["ORTH", "POS"])
|
||||
- doc_with_meta = {"doc_array": doc_array, "meta": get_doc_meta(doc_array)}
|
||||
|
||||
+ Doc.set_extension("meta", getter=get_doc_meta)
|
||||
+ doc_with_meta = nlp(u'This is a doc with meta data')
|
||||
+ meta = doc._.meta
|
||||
```
|
||||
|
||||
If you wrap your extension attributes in a
|
||||
[custom pipeline component](/usage/processing-pipelines#custom-components), they
|
||||
will be assigned automatically when you call `nlp` on a text. If your
|
||||
application assigns custom data to spaCy's container objects, or includes other
|
||||
utilities that interact with the pipeline, consider moving this logic into its
|
||||
own extension module.
|
||||
|
||||
```diff
|
||||
- doc = nlp("Doc with a standard pipeline")
|
||||
- meta = get_meta(doc)
|
||||
|
||||
+ nlp.add_pipe(meta_component)
|
||||
+ doc = nlp("Doc with a custom pipeline that assigns meta")
|
||||
+ meta = doc._.meta
|
||||
```
|
||||
|
||||
### Strings and hash values {id="migrating-strings"}
|
||||
|
||||
The change from integer IDs to hash values may not actually affect your code
|
||||
very much. However, if you're adding strings to the vocab manually, you now need
|
||||
to call [`StringStore.add`](/api/stringstore#add) explicitly. You can also now
|
||||
be sure that the string-to-hash mapping will always match across vocabularies.
|
||||
|
||||
```diff
|
||||
- nlp.vocab.strings["coffee"] # 3672
|
||||
- other_nlp.vocab.strings["coffee"] # 40259
|
||||
|
||||
+ nlp.vocab.strings.add("coffee")
|
||||
+ nlp.vocab.strings["coffee"] # 3197928453018144401
|
||||
+ other_nlp.vocab.strings["coffee"] # 3197928453018144401
|
||||
```
|
||||
|
||||
### Adding patterns and callbacks to the matcher {id="migrating-matcher"}
|
||||
|
||||
If you're using the matcher, you can now add patterns in one step. This should
|
||||
be easy to update – simply merge the ID, callback and patterns into one call to
|
||||
[`Matcher.add()`](/api/matcher#add). The matcher now also supports string keys,
|
||||
which saves you an extra import. If you've been using **acceptor functions**,
|
||||
you'll need to move this logic into the
|
||||
[`on_match` callbacks](/usage/linguistic-features#on_match). The callback
|
||||
function is invoked on every match and will give you access to the doc, the
|
||||
index of the current match and all total matches. This lets you both accept or
|
||||
reject the match, and define the actions to be triggered.
|
||||
|
||||
```diff
|
||||
- matcher.add_entity("GoogleNow", on_match=merge_phrases)
|
||||
- matcher.add_pattern("GoogleNow", [{ORTH: "Google"}, {ORTH: "Now"}])
|
||||
|
||||
+ matcher.add("GoogleNow", merge_phrases, [{"ORTH": "Google"}, {"ORTH": "Now"}])
|
||||
```
|
||||
|
||||
If you need to match large terminology lists, you can now also use the
|
||||
[`PhraseMatcher`](/api/phrasematcher), which accepts `Doc` objects as match
|
||||
patterns and is more efficient than the regular, rule-based matcher.
|
||||
|
||||
```diff
|
||||
- matcher = Matcher(nlp.vocab)
|
||||
- matcher.add_entity("PRODUCT")
|
||||
- for text in large_terminology_list
|
||||
- matcher.add_pattern("PRODUCT", [{ORTH: text}])
|
||||
|
||||
+ from spacy.matcher import PhraseMatcher
|
||||
+ matcher = PhraseMatcher(nlp.vocab)
|
||||
+ patterns = [nlp.make_doc(text) for text in large_terminology_list]
|
||||
+ matcher.add("PRODUCT", None, *patterns)
|
||||
```
|
||||
@@ -0,0 +1,322 @@
|
||||
---
|
||||
title: What's New in v3.1
|
||||
teaser: New features and how to upgrade
|
||||
menu:
|
||||
- ['New Features', 'features']
|
||||
- ['Upgrading Notes', 'upgrading']
|
||||
---
|
||||
|
||||
## New Features {id="features",hidden="true"}
|
||||
|
||||
It's been great to see the adoption of the new spaCy v3, which introduced
|
||||
[transformer-based](/usage/embeddings-transformers) pipelines, a new
|
||||
[config and training system](/usage/training) for reproducible experiments,
|
||||
[projects](/usage/projects) for end-to-end workflows, and many
|
||||
[other features](/usage/v3). Version 3.1 adds more on top of it, including the
|
||||
ability to use predicted annotations during training, a new `SpanCategorizer`
|
||||
component for predicting arbitrary and potentially overlapping spans, support
|
||||
for partial incorrect annotations in the entity recognizer, new trained
|
||||
pipelines for Catalan and Danish, as well as many bug fixes and improvements.
|
||||
|
||||
### Using predicted annotations during training {id="predicted-annotations-training"}
|
||||
|
||||
By default, components are updated in isolation during training, which means
|
||||
that they don't see the predictions of any earlier components in the pipeline.
|
||||
The new
|
||||
[`[training.annotating_components]`](/usage/training#annotating-components)
|
||||
config setting lets you specify pipeline components that should set annotations
|
||||
on the predicted docs during training. This makes it easy to use the predictions
|
||||
of a previous component in the pipeline as features for a subsequent component,
|
||||
e.g. the dependency labels in the tagger:
|
||||
|
||||
```ini {title="config.cfg (excerpt)",highlight="7,12"}
|
||||
[nlp]
|
||||
pipeline = ["parser", "tagger"]
|
||||
|
||||
[components.tagger.model.tok2vec.embed]
|
||||
@architectures = "spacy.MultiHashEmbed.v1"
|
||||
width = ${components.tagger.model.tok2vec.encode.width}
|
||||
attrs = ["NORM","DEP"]
|
||||
rows = [5000,2500]
|
||||
include_static_vectors = false
|
||||
|
||||
[training]
|
||||
annotating_components = ["parser"]
|
||||
```
|
||||
|
||||
<Project id="pipelines/tagger_parser_predicted_annotations">
|
||||
|
||||
This project shows how to use the `token.dep` attribute predicted by the parser
|
||||
as a feature for a subsequent tagger component in the pipeline.
|
||||
|
||||
</Project>
|
||||
|
||||
### SpanCategorizer for predicting arbitrary and overlapping spans {id="spancategorizer",tag="experimental"}
|
||||
|
||||
A common task in applied NLP is extracting spans of texts from documents,
|
||||
including longer phrases or nested expressions. Named entity recognition isn't
|
||||
the right tool for this problem, since an entity recognizer typically predicts
|
||||
single token-based tags that are very sensitive to boundaries. This is effective
|
||||
for proper nouns and self-contained expressions, but less useful for other types
|
||||
of phrases or overlapping spans. The new
|
||||
[`SpanCategorizer`](/api/spancategorizer) component and
|
||||
[SpanCategorizer](/api/architectures#spancategorizer) architecture let you label
|
||||
arbitrary and potentially overlapping spans of texts. A span categorizer
|
||||
consists of two parts: a [suggester function](/api/spancategorizer#suggesters)
|
||||
that proposes candidate spans, which may or may not overlap, and a labeler model
|
||||
that predicts zero or more labels for each candidate. The predicted spans are
|
||||
available via the [`Doc.spans`](/api/doc#spans) container.
|
||||
|
||||
<Project id="experimental/ner_spancat">
|
||||
|
||||
This project trains a span categorizer for Indonesian NER.
|
||||
|
||||
</Project>
|
||||
|
||||
<Infobox title="Tip: Create data with Prodigy's new span annotation UI">
|
||||
|
||||
<Image
|
||||
src="/images/prodigy_spans-manual.jpg"
|
||||
href="https://support.prodi.gy/t/3861"
|
||||
alt="Prodigy: example of the new manual spans UI"
|
||||
/>
|
||||
|
||||
The upcoming version of our annotation tool [Prodigy](https://prodi.gy)
|
||||
(currently available as a [pre-release](https://support.prodi.gy/t/3861) for all
|
||||
users) features a [new workflow and UI](https://support.prodi.gy/t/3861) for
|
||||
annotating overlapping and nested spans. You can use it to create training data
|
||||
for spaCy's `SpanCategorizer` component.
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Update the entity recognizer with partial incorrect annotations {id="negative-samples"}
|
||||
|
||||
> #### config.cfg (excerpt)
|
||||
>
|
||||
> ```ini
|
||||
> [components.ner]
|
||||
> factory = "ner"
|
||||
> incorrect_spans_key = "incorrect_spans"
|
||||
> moves = null
|
||||
> update_with_oracle_cut_size = 100
|
||||
> ```
|
||||
|
||||
The [`EntityRecognizer`](/api/entityrecognizer) can now be updated with known
|
||||
incorrect annotations, which lets you take advantage of partial and sparse data.
|
||||
For example, you'll be able to use the information that certain spans of text
|
||||
are definitely **not** `PERSON` entities, without having to provide the complete
|
||||
gold-standard annotations for the given example. The incorrect span annotations
|
||||
can be added via the [`Doc.spans`](/api/doc#spans) in the training data under
|
||||
the key defined as [`incorrect_spans_key`](/api/entityrecognizer#init) in the
|
||||
component config.
|
||||
|
||||
```python
|
||||
train_doc = nlp.make_doc("Barack Obama was born in Hawaii.")
|
||||
# The doc.spans key can be defined in the config
|
||||
train_doc.spans["incorrect_spans"] = [
|
||||
Span(doc, 0, 2, label="ORG"),
|
||||
Span(doc, 5, 6, label="PRODUCT")
|
||||
]
|
||||
```
|
||||
|
||||
{/* TODO: more details and/or example project? */}
|
||||
|
||||
### New pipeline packages for Catalan and Danish {id="pipeline-packages"}
|
||||
|
||||
spaCy v3.1 adds 5 new pipeline packages, including a new core family for Catalan
|
||||
and a new transformer-based pipeline for Danish using the
|
||||
[`danish-bert-botxo`](http://huggingface.co/Maltehb/danish-bert-botxo) weights.
|
||||
See the [models directory](/models) for an overview of all available trained
|
||||
pipelines and the [training guide](/usage/training) for details on how to train
|
||||
your own.
|
||||
|
||||
> Thanks to Carlos Rodríguez Penagos and the
|
||||
> [Barcelona Supercomputing Center](https://temu.bsc.es/) for their
|
||||
> contributions for Catalan and to Kenneth Enevoldsen for Danish. For additional
|
||||
> Danish pipelines, check out [DaCy](https://github.com/KennethEnevoldsen/DaCy).
|
||||
|
||||
| Package | Language | UPOS | Parser LAS | NER F |
|
||||
| ------------------------------------------------- | -------- | ---: | ---------: | ----: |
|
||||
| [`ca_core_news_sm`](/models/ca#ca_core_news_sm) | Catalan | 98.2 | 87.4 | 79.8 |
|
||||
| [`ca_core_news_md`](/models/ca#ca_core_news_md) | Catalan | 98.3 | 88.2 | 84.0 |
|
||||
| [`ca_core_news_lg`](/models/ca#ca_core_news_lg) | Catalan | 98.5 | 88.4 | 84.2 |
|
||||
| [`ca_core_news_trf`](/models/ca#ca_core_news_trf) | Catalan | 98.9 | 93.0 | 91.2 |
|
||||
| [`da_core_news_trf`](/models/da#da_core_news_trf) | Danish | 98.0 | 85.0 | 82.9 |
|
||||
|
||||
### Resizable text classification architectures {id="resizable-textcat"}
|
||||
|
||||
Previously, the [`TextCategorizer`](/api/textcategorizer) architectures could
|
||||
not be resized, meaning that you couldn't add new labels to an already trained
|
||||
model. In spaCy v3.1, the [TextCatCNN](/api/architectures#TextCatCNN) and
|
||||
[TextCatBOW](/api/architectures#TextCatBOW) architectures are now resizable,
|
||||
while ensuring that the predictions for the old labels remain the same.
|
||||
|
||||
### CLI command to assemble pipeline from config {id="assemble"}
|
||||
|
||||
The [`spacy assemble`](/api/cli#assemble) command lets you assemble a pipeline
|
||||
from a config file without additional training. It can be especially useful for
|
||||
creating a blank pipeline with a custom tokenizer, rule-based components or word
|
||||
vectors.
|
||||
|
||||
```bash
|
||||
$ python -m spacy assemble config.cfg ./output
|
||||
```
|
||||
|
||||
### Pretty pipeline package READMEs {id="package-readme"}
|
||||
|
||||
The [`spacy package`](/api/cli#package) command now auto-generates a pretty
|
||||
`README.md` based on the pipeline information defined in the `meta.json`. This
|
||||
includes a table with a general overview, as well as the label scheme and
|
||||
accuracy figures, if available. For an example, see the
|
||||
[model releases](https://github.com/explosion/spacy-models/releases).
|
||||
|
||||
### Support for streaming large or infinite corpora {id="streaming-corpora"}
|
||||
|
||||
> #### config.cfg (excerpt)
|
||||
>
|
||||
> ```ini
|
||||
> [training]
|
||||
> max_epochs = -1
|
||||
> ```
|
||||
|
||||
The training process now supports streaming large or infinite corpora
|
||||
out-of-the-box, which can be controlled via the
|
||||
[`[training.max_epochs]`](/api/data-formats#training) config setting. Setting it
|
||||
to `-1` means that the train corpus should be streamed rather than loaded into
|
||||
memory with no shuffling within the training loop. For details on how to
|
||||
implement a custom corpus loader, e.g. to stream in data from a remote storage,
|
||||
see the usage guide on
|
||||
[custom data reading](/usage/training#custom-code-readers-batchers).
|
||||
|
||||
When streaming a corpus, only the first 100 examples will be used for
|
||||
[initialization](/usage/training#config-lifecycle). This is no problem if you're
|
||||
training a component like the text classifier with data that specifies all
|
||||
available labels in every example. If necessary, you can use the
|
||||
[`init labels`](/api/cli#init-labels) command to pre-generate the labels for
|
||||
your components using a representative sample so the model can be initialized
|
||||
correctly before training.
|
||||
|
||||
### New lemmatizers for Catalan and Italian {id="pos-lemmatizers"}
|
||||
|
||||
The trained pipelines for [Catalan](/models/ca) and [Italian](/models/it) now
|
||||
include lemmatizers that use the predicted part-of-speech tags as part of the
|
||||
lookup lemmatization for higher lemmatization accuracy. If you're training your
|
||||
own pipelines for these languages and you want to include a lemmatizer, make
|
||||
sure you have the
|
||||
[`spacy-lookups-data`](https://github.com/explosion/spacy-lookups-data) package
|
||||
installed, which provides the relevant tables.
|
||||
|
||||
### Upload your pipelines to the Hugging Face Hub {id="huggingface-hub"}
|
||||
|
||||
The [Hugging Face Hub](https://huggingface.co/) lets you upload models and share
|
||||
them with others, and it now supports spaCy pipelines out-of-the-box. The new
|
||||
[`spacy-huggingface-hub`](https://github.com/explosion/spacy-huggingface-hub)
|
||||
package automatically adds the `huggingface-hub` command to your `spacy` CLI. It
|
||||
lets you upload any pipelines packaged with [`spacy package`](/api/cli#package)
|
||||
and `--build wheel` and takes care of auto-generating all required meta
|
||||
information.
|
||||
|
||||
After uploading, you'll get a live URL for your model page that includes all
|
||||
details, files and interactive visualizers, as well as a direct URL to the wheel
|
||||
file that you can install via `pip install`. For examples, check out the
|
||||
[spaCy pipelines](https://huggingface.co/spacy) we've uploaded.
|
||||
|
||||
```bash
|
||||
$ pip install spacy-huggingface-hub
|
||||
$ huggingface-cli login
|
||||
$ python -m spacy package ./en_ner_fashion ./output --build wheel
|
||||
$ cd ./output/en_ner_fashion-0.0.0/dist
|
||||
$ python -m spacy huggingface-hub push en_ner_fashion-0.0.0-py3-none-any.whl
|
||||
```
|
||||
|
||||
You can also integrate the upload command into your
|
||||
[project template](/usage/projects#huggingface_hub) to automatically upload your
|
||||
packaged pipelines after training.
|
||||
|
||||
<Project id="integrations/huggingface_hub">
|
||||
|
||||
Get started with uploading your models to the Hugging Face hub using our project
|
||||
template. It trains a simple pipeline, packages it and uploads it if the
|
||||
packaged model has changed. This makes it easy to deploy your models end-to-end.
|
||||
|
||||
</Project>
|
||||
|
||||
## Notes about upgrading from v3.0 {id="upgrading"}
|
||||
|
||||
### Pipeline package version compatibility {id="version-compat"}
|
||||
|
||||
> #### Using legacy implementations
|
||||
>
|
||||
> In spaCy v3, you'll still be able to load and reference legacy implementations
|
||||
> via [`spacy-legacy`](https://github.com/explosion/spacy-legacy), even if the
|
||||
> components or architectures change and newer versions are available in the
|
||||
> core library.
|
||||
|
||||
When you're loading a pipeline package trained with spaCy v3.0, you will see a
|
||||
warning telling you that the pipeline may be incompatible. This doesn't
|
||||
necessarily have to be true, but we recommend running your pipelines against
|
||||
your test suite or evaluation data to make sure there are no unexpected results.
|
||||
If you're using one of the [trained pipelines](/models) we provide, you should
|
||||
run [`spacy download`](/api/cli#download) to update to the latest version. To
|
||||
see an overview of all installed packages and their compatibility, you can run
|
||||
[`spacy validate`](/api/cli#validate).
|
||||
|
||||
If you've trained your own custom pipeline and you've confirmed that it's still
|
||||
working as expected, you can update the spaCy version requirements in the
|
||||
[`meta.json`](/api/data-formats#meta):
|
||||
|
||||
```diff
|
||||
- "spacy_version": ">=3.0.0,<3.1.0",
|
||||
+ "spacy_version": ">=3.0.0,<3.2.0",
|
||||
```
|
||||
|
||||
### Updating v3.0 configs
|
||||
|
||||
To update a config from spaCy v3.0 with the new v3.1 settings, run
|
||||
[`init fill-config`](/api/cli#init-fill-config):
|
||||
|
||||
```bash
|
||||
python -m spacy init fill-config config-v3.0.cfg config-v3.1.cfg
|
||||
```
|
||||
|
||||
In many cases (`spacy train`, `spacy.load()`), the new defaults will be filled
|
||||
in automatically, but you'll need to fill in the new settings to run
|
||||
[`debug config`](/api/cli#debug) and [`debug data`](/api/cli#debug-data).
|
||||
|
||||
### Sourcing pipeline components with vectors {id="source-vectors"}
|
||||
|
||||
If you're sourcing a pipeline component that requires static vectors (for
|
||||
example, a tagger or parser from an `md` or `lg` pretrained pipeline), be sure
|
||||
to include the source model's vectors in the setting `[initialize.vectors]`. In
|
||||
spaCy v3.0, a bug allowed vectors to be loaded implicitly through `source`,
|
||||
however in v3.1 this setting must be provided explicitly as
|
||||
`[initialize.vectors]`:
|
||||
|
||||
```ini {title="config.cfg (excerpt)"}
|
||||
[components.ner]
|
||||
source = "en_core_web_md"
|
||||
|
||||
[initialize]
|
||||
vectors = "en_core_web_md"
|
||||
```
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
Each pipeline can only store one set of static vectors, so it's not possible to
|
||||
assemble a pipeline with components that were trained on different static
|
||||
vectors.
|
||||
|
||||
</Infobox>
|
||||
|
||||
[`spacy train`](/api/cli#train) and [`spacy assemble`](/api/cli#assemble) will
|
||||
provide warnings if the source and target pipelines don't contain the same
|
||||
vectors. If you are sourcing a rule-based component like an entity ruler or
|
||||
lemmatizer that does not use the vectors as a model feature, then this warning
|
||||
can be safely ignored.
|
||||
|
||||
### Warnings {id="warnings"}
|
||||
|
||||
Logger warnings have been converted to Python warnings. Use
|
||||
[`warnings.filterwarnings`](https://docs.python.org/3/library/warnings.html#warnings.filterwarnings)
|
||||
or the new helper method `spacy.errors.filter_warning(action, error_msg='')` to
|
||||
manage warnings.
|
||||
@@ -0,0 +1,242 @@
|
||||
---
|
||||
title: What's New in v3.2
|
||||
teaser: New features and how to upgrade
|
||||
menu:
|
||||
- ['New Features', 'features']
|
||||
- ['Upgrading Notes', 'upgrading']
|
||||
---
|
||||
|
||||
## New Features {id="features",hidden="true"}
|
||||
|
||||
spaCy v3.2 adds support for [`floret`](https://github.com/explosion/floret)
|
||||
vectors, makes custom `Doc` creation and scoring easier, and includes many bug
|
||||
fixes and improvements. For the trained pipelines, there's a new transformer
|
||||
pipeline for Japanese and the Universal Dependencies training data has been
|
||||
updated across the board to the most recent release.
|
||||
|
||||
<Infobox title="Improve performance for spaCy on Apple M1 with AppleOps" variant="warning" emoji="📣">
|
||||
|
||||
spaCy is now up to **8 × faster on M1 Macs** by calling into Apple's
|
||||
native Accelerate library for matrix multiplication. For more details, see
|
||||
[`thinc-apple-ops`](https://github.com/explosion/thinc-apple-ops).
|
||||
|
||||
```bash
|
||||
$ pip install spacy[apple]
|
||||
```
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Registered scoring functions {id="registered-scoring-functions"}
|
||||
|
||||
To customize the scoring, you can specify a scoring function for each component
|
||||
in your config from the new [`scorers` registry](/api/top-level#registry):
|
||||
|
||||
```ini {title="config.cfg (excerpt)",highlight="3"}
|
||||
[components.tagger]
|
||||
factory = "tagger"
|
||||
scorer = {"@scorers":"spacy.tagger_scorer.v1"}
|
||||
```
|
||||
|
||||
### Overwrite settings {id="overwrite"}
|
||||
|
||||
Most pipeline components now include an `overwrite` setting in the config that
|
||||
determines whether existing annotation in the `Doc` is preserved or overwritten:
|
||||
|
||||
```ini {title="config.cfg (excerpt)",highlight="3"}
|
||||
[components.tagger]
|
||||
factory = "tagger"
|
||||
overwrite = false
|
||||
```
|
||||
|
||||
### Doc input for pipelines {id="doc-input"}
|
||||
|
||||
[`nlp`](/api/language#call) and [`nlp.pipe`](/api/language#pipe) accept
|
||||
[`Doc`](/api/doc) input, skipping the tokenizer if a `Doc` is provided instead
|
||||
of a string. This makes it easier to create a `Doc` with custom tokenization or
|
||||
to set custom extensions before processing:
|
||||
|
||||
```python
|
||||
doc = nlp.make_doc("This is text 500.")
|
||||
doc._.text_id = 500
|
||||
doc = nlp(doc)
|
||||
```
|
||||
|
||||
### Support for floret vectors {id="vectors"}
|
||||
|
||||
We recently published [`floret`](https://github.com/explosion/floret), an
|
||||
extended version of [fastText](https://fasttext.cc) that combines fastText's
|
||||
subwords with Bloom embeddings for compact, full-coverage vectors. The use of
|
||||
subwords means that there are no OOV words and due to Bloom embeddings, the
|
||||
vector table can be kept very small at \<100K entries. Bloom embeddings are
|
||||
already used by [HashEmbed](https://thinc.ai/docs/api-layers#hashembed) in
|
||||
[tok2vec](/api/architectures#tok2vec-arch) for compact spaCy models.
|
||||
|
||||
For easy integration, floret includes a
|
||||
[Python wrapper](https://github.com/explosion/floret/blob/main/python/README.md):
|
||||
|
||||
```bash
|
||||
$ pip install floret
|
||||
```
|
||||
|
||||
A demo project shows how to train and import floret vectors:
|
||||
|
||||
<Project id="pipelines/floret_vectors_demo">
|
||||
|
||||
Train toy English floret vectors and import them into a spaCy pipeline.
|
||||
|
||||
</Project>
|
||||
|
||||
Two additional demo projects compare standard fastText vectors with floret
|
||||
vectors for full spaCy pipelines. For agglutinative languages like Finnish or
|
||||
Korean, there are large improvements in performance due to the use of subwords
|
||||
(no OOV words!), with a vector table containing merely 50K entries.
|
||||
|
||||
<Project id="pipelines/floret_fi_core_demo">
|
||||
|
||||
Finnish UD+NER vector and pipeline training, comparing standard fasttext vs.
|
||||
floret vectors.
|
||||
|
||||
For the default project settings with 1M (2.6G) tokenized training texts and 50K
|
||||
300-dim vectors, ~300K keys for the standard vectors:
|
||||
|
||||
| Vectors | TAG | POS | DEP UAS | DEP LAS | NER F |
|
||||
| -------------------------------------------- | -------: | -------: | -------: | -------: | -------: |
|
||||
| none | 93.3 | 92.3 | 79.7 | 72.8 | 61.0 |
|
||||
| standard (pruned: 50K vectors for 300K keys) | 95.9 | 94.7 | 83.3 | 77.9 | 68.5 |
|
||||
| standard (unpruned: 300K vectors/keys) | 96.0 | 95.0 | **83.8** | 78.4 | 69.1 |
|
||||
| floret (minn 4, maxn 5; 50K vectors, no OOV) | **96.6** | **95.5** | 83.5 | **78.5** | **70.9** |
|
||||
|
||||
</Project>
|
||||
|
||||
<Project id="pipelines/floret_ko_ud_demo">
|
||||
|
||||
Korean UD vector and pipeline training, comparing standard fasttext vs. floret
|
||||
vectors.
|
||||
|
||||
For the default project settings with 1M (3.3G) tokenized training texts and 50K
|
||||
300-dim vectors, ~800K keys for the standard vectors:
|
||||
|
||||
| Vectors | TAG | POS | DEP UAS | DEP LAS |
|
||||
| -------------------------------------------- | -------: | -------: | -------: | -------: |
|
||||
| none | 72.5 | 85.0 | 73.2 | 64.3 |
|
||||
| standard (pruned: 50K vectors for 800K keys) | 77.9 | 89.4 | 78.8 | 72.8 |
|
||||
| standard (unpruned: 800K vectors/keys) | 79.0 | 90.2 | 79.2 | 73.9 |
|
||||
| floret (minn 2, maxn 3; 50K vectors, no OOV) | **82.5** | **93.8** | **83.0** | **80.1** |
|
||||
|
||||
</Project>
|
||||
|
||||
### Updates for spacy-transformers v1.1 {id="spacy-transformers"}
|
||||
|
||||
[`spacy-transformers`](https://github.com/explosion/spacy-transformers) v1.1 has
|
||||
been refactored to improve serialization and support of inline transformer
|
||||
components and replacing listeners. In addition, the transformer model output is
|
||||
provided as
|
||||
[`ModelOutput`](https://huggingface.co/transformers/main_classes/output.html?highlight=modeloutput#transformers.file_utils.ModelOutput)
|
||||
instead of tuples in
|
||||
`TransformerData.model_output and FullTransformerBatch.model_output.` For
|
||||
backwards compatibility, the tuple format remains available under
|
||||
`TransformerData.tensors` and `FullTransformerBatch.tensors`. See more details
|
||||
in the [transformer API docs](/api/architectures#TransformerModel).
|
||||
|
||||
`spacy-transformers` v1.1 also adds support for `transformer_config` settings
|
||||
such as `output_attentions`. Additional output is stored under
|
||||
`TransformerData.model_output`. More details are in the
|
||||
[TransformerModel docs](/api/architectures#TransformerModel). The training speed
|
||||
has been improved by streamlining allocations for tokenizer output and there is
|
||||
new support for [mixed-precision training](/api/architectures#TransformerModel).
|
||||
|
||||
### New transformer package for Japanese {id="pipeline-packages"}
|
||||
|
||||
spaCy v3.2 adds a new transformer pipeline package for Japanese
|
||||
[`ja_core_news_trf`](/models/ja#ja_core_news_trf), which uses the `basic`
|
||||
pretokenizer instead of `mecab` to limit the number of dependencies required for
|
||||
the pipeline. Thanks to Hiroshi Matsuda and the spaCy Japanese community for
|
||||
their contributions!
|
||||
|
||||
### Pipeline and language updates {id="pipeline-updates"}
|
||||
|
||||
- All Universal Dependencies training data has been updated to v2.8.
|
||||
- The Catalan data, tokenizer and lemmatizer have been updated, thanks to Carlos
|
||||
Rodriguez, Carme Armentano and the Barcelona Supercomputing Center!
|
||||
- The transformer pipelines are trained using spacy-transformers v1.1, with
|
||||
improved IO and more options for
|
||||
[model config and output](/api/architectures#TransformerModel).
|
||||
- Trailing whitespace has been added as a `tok2vec` feature, improving the
|
||||
performance for many components, especially fine-grained tagging and sentence
|
||||
segmentation.
|
||||
- The English attribute ruler patterns have been overhauled to improve
|
||||
`Token.pos` and `Token.morph`.
|
||||
|
||||
spaCy v3.2 also features a new Irish lemmatizer, support for `noun_chunks` in
|
||||
Portuguese, improved `noun_chunks` for Spanish and additional updates for
|
||||
Bulgarian, Catalan, Sinhala, Tagalog, Tigrinya and Vietnamese.
|
||||
|
||||
## Notes about upgrading from v3.1 {id="upgrading"}
|
||||
|
||||
### Pipeline package version compatibility {id="version-compat"}
|
||||
|
||||
> #### Using legacy implementations
|
||||
>
|
||||
> In spaCy v3, you'll still be able to load and reference legacy implementations
|
||||
> via [`spacy-legacy`](https://github.com/explosion/spacy-legacy), even if the
|
||||
> components or architectures change and newer versions are available in the
|
||||
> core library.
|
||||
|
||||
When you're loading a pipeline package trained with spaCy v3.0 or v3.1, you will
|
||||
see a warning telling you that the pipeline may be incompatible. This doesn't
|
||||
necessarily have to be true, but we recommend running your pipelines against
|
||||
your test suite or evaluation data to make sure there are no unexpected results.
|
||||
If you're using one of the [trained pipelines](/models) we provide, you should
|
||||
run [`spacy download`](/api/cli#download) to update to the latest version. To
|
||||
see an overview of all installed packages and their compatibility, you can run
|
||||
[`spacy validate`](/api/cli#validate).
|
||||
|
||||
If you've trained your own custom pipeline and you've confirmed that it's still
|
||||
working as expected, you can update the spaCy version requirements in the
|
||||
[`meta.json`](/api/data-formats#meta):
|
||||
|
||||
```diff
|
||||
- "spacy_version": ">=3.1.0,<3.2.0",
|
||||
+ "spacy_version": ">=3.2.0,<3.3.0",
|
||||
```
|
||||
|
||||
### Updating v3.1 configs
|
||||
|
||||
To update a config from spaCy v3.1 with the new v3.2 settings, run
|
||||
[`init fill-config`](/api/cli#init-fill-config):
|
||||
|
||||
```bash
|
||||
$ python -m spacy init fill-config config-v3.1.cfg config-v3.2.cfg
|
||||
```
|
||||
|
||||
In many cases ([`spacy train`](/api/cli#train),
|
||||
[`spacy.load`](/api/top-level#spacy.load)), the new defaults will be filled in
|
||||
automatically, but you'll need to fill in the new settings to run
|
||||
[`debug config`](/api/cli#debug) and [`debug data`](/api/cli#debug-data).
|
||||
|
||||
## Notes about upgrading from spacy-transformers v1.0 {id="upgrading-transformers"}
|
||||
|
||||
When you're loading a transformer pipeline package trained with
|
||||
[`spacy-transformers`](https://github.com/explosion/spacy-transformers) v1.0
|
||||
after upgrading to `spacy-transformers` v1.1, you'll see a warning telling you
|
||||
that the pipeline may be incompatible. `spacy-transformers` v1.1 should be able
|
||||
to import v1.0 `transformer` components into the new internal format with no
|
||||
change in performance, but here we'd also recommend running your test suite to
|
||||
verify that the pipeline still performs as expected.
|
||||
|
||||
If you save your pipeline with [`nlp.to_disk`](/api/language#to_disk), it will
|
||||
be saved in the new v1.1 format and should be fully compatible with
|
||||
`spacy-transformers` v1.1. Once you've confirmed the performance, you can update
|
||||
the requirements in [`meta.json`](/api/data-formats#meta):
|
||||
|
||||
```diff
|
||||
"requirements": [
|
||||
- "spacy-transformers>=1.0.3,<1.1.0"
|
||||
+ "spacy-transformers>=1.1.2,<1.2.0"
|
||||
]
|
||||
```
|
||||
|
||||
If you're using one of the [trained pipelines](/models) we provide, you should
|
||||
run [`spacy download`](/api/cli#download) to update to the latest version. To
|
||||
see an overview of all installed packages and their compatibility, you can run
|
||||
[`spacy validate`](/api/cli#validate).
|
||||
@@ -0,0 +1,247 @@
|
||||
---
|
||||
title: What's New in v3.3
|
||||
teaser: New features and how to upgrade
|
||||
menu:
|
||||
- ['New Features', 'features']
|
||||
- ['Upgrading Notes', 'upgrading']
|
||||
---
|
||||
|
||||
## New features {id="features",hidden="true"}
|
||||
|
||||
spaCy v3.3 improves the speed of core pipeline components, adds a new trainable
|
||||
lemmatizer, and introduces trained pipelines for Finnish, Korean and Swedish.
|
||||
|
||||
### Speed improvements {id="speed"}
|
||||
|
||||
v3.3 includes a slew of speed improvements:
|
||||
|
||||
- Speed up parser and NER by using constant-time head lookups.
|
||||
- Support unnormalized softmax probabilities in `spacy.Tagger.v2` to speed up
|
||||
inference for tagger, morphologizer, senter and trainable lemmatizer.
|
||||
- Speed up parser projectivization functions.
|
||||
- Replace `Ragged` with faster `AlignmentArray` in `Example` for training.
|
||||
- Improve `Matcher` speed.
|
||||
- Improve serialization speed for empty `Doc.spans`.
|
||||
|
||||
For longer texts, the trained pipeline speeds improve **15%** or more in
|
||||
prediction. We benchmarked `en_core_web_md` (same components as in v3.2) and
|
||||
`de_core_news_md` (with the new trainable lemmatizer) across a range of text
|
||||
sizes on Linux (Intel Xeon W-2265) and OS X (M1) to compare spaCy v3.2 vs. v3.3:
|
||||
|
||||
**Intel Xeon W-2265**
|
||||
|
||||
| Model | Avg. Words/Doc | v3.2 Words/Sec | v3.3 Words/Sec | Diff |
|
||||
| :----------------------------------------------- | -------------: | -------------: | -------------: | -----: |
|
||||
| [`en_core_web_md`](/models/en#en_core_web_md) | 100 | 17292 | 17441 | 0.86% |
|
||||
| (=same components) | 1000 | 15408 | 16024 | 4.00% |
|
||||
| | 10000 | 12798 | 15346 | 19.91% |
|
||||
| [`de_core_news_md`](/models/de/#de_core_news_md) | 100 | 20221 | 19321 | -4.45% |
|
||||
| (+v3.3 trainable lemmatizer) | 1000 | 17480 | 17345 | -0.77% |
|
||||
| | 10000 | 14513 | 17036 | 17.38% |
|
||||
|
||||
**Apple M1**
|
||||
|
||||
| Model | Avg. Words/Doc | v3.2 Words/Sec | v3.3 Words/Sec | Diff |
|
||||
| ------------------------------------------------ | -------------: | -------------: | -------------: | -----: |
|
||||
| [`en_core_web_md`](/models/en#en_core_web_md) | 100 | 18272 | 18408 | 0.74% |
|
||||
| (=same components) | 1000 | 18794 | 19248 | 2.42% |
|
||||
| | 10000 | 15144 | 17513 | 15.64% |
|
||||
| [`de_core_news_md`](/models/de/#de_core_news_md) | 100 | 19227 | 19591 | 1.89% |
|
||||
| (+v3.3 trainable lemmatizer) | 1000 | 20047 | 20628 | 2.90% |
|
||||
| | 10000 | 15921 | 18546 | 16.49% |
|
||||
|
||||
### Trainable lemmatizer {id="trainable-lemmatizer"}
|
||||
|
||||
The new [trainable lemmatizer](/api/edittreelemmatizer) component uses
|
||||
[edit trees](https://explosion.ai/blog/edit-tree-lemmatizer) to transform tokens
|
||||
into lemmas. Try out the trainable lemmatizer with the
|
||||
[training quickstart](/usage/training#quickstart)!
|
||||
|
||||
### displaCy support for overlapping spans and arcs {id="displacy"}
|
||||
|
||||
displaCy now supports overlapping spans with a new
|
||||
[`span`](/usage/visualizers#span) style and multiple arcs with different labels
|
||||
between the same tokens for [`dep`](/usage/visualizers#dep) visualizations.
|
||||
|
||||
Overlapping spans can be visualized for any spans key in `doc.spans`:
|
||||
|
||||
```python
|
||||
import spacy
|
||||
from spacy import displacy
|
||||
from spacy.tokens import Span
|
||||
|
||||
nlp = spacy.blank("en")
|
||||
text = "Welcome to the Bank of China."
|
||||
doc = nlp(text)
|
||||
doc.spans["custom"] = [Span(doc, 3, 6, "ORG"), Span(doc, 5, 6, "GPE")]
|
||||
displacy.serve(doc, style="span", options={"spans_key": "custom"})
|
||||
```
|
||||
|
||||
<Standalone height={100}>
|
||||
<div style={{ lineHeight: 2.5, direction: 'ltr', fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'", fontSize: 18 }}>Welcome to the <span style={{ fontWeight: 'bold', display: 'inline-block', position: 'relative'}}>Bank<span style={{ background: '#7aecec', top: 40, height: 4, left: -1, width: 'calc(100% + 2px)', position: 'absolute' }}></span><span style={{ background: '#7aecec', top: 40, height: 4, borderTopLeftRadius: 3, borderBottomLeftRadius: 3, left: -1, width: 'calc(100% + 2px)', position: 'absolute' }}><span style={{ background: '#7aecec', color: '#000', top: '-0.5em', padding: '2px 3px', position: 'absolute', fontSize: '0.6em', fontWeight: 'bold', lineHeight: 1, borderRadius: 3 }}>ORG</span></span></span> <span style={{ fontWeight: 'bold', display: 'inline-block', position: 'relative'}}>of <span style={{ background: '#7aecec', top: 40, height: 4, left: -1, width: 'calc(100% + 2px)', position: 'absolute' }}></span></span> <span style={{ fontWeight: 'bold', display: 'inline-block', position: 'relative'}}>China<span style={{ background: '#7aecec', top: 40, height: 4, left: -1, width: 'calc(100% + 2px)', position: 'absolute' }}></span><span style={{ background: '#feca74', top: 57, height: 4, left: -1, width: 'calc(100% + 2px)', position: 'absolute' }}></span><span style={{ background: '#feca74', top: 57, height: 4, borderTopLeftRadius: 3, borderBottomLeftRadius: 3, left: -1, width: 'calc(100% + 2px)', position: 'absolute' }}><span style={{ background: '#feca74', color: '#000', top: '-0.5em', padding: '2px 3px', position: 'absolute', fontSize: '0.6em', fontWeight: 'bold', lineHeight: 1, borderRadius: 3 }}>GPE</span></span></span>.</div>
|
||||
</Standalone>
|
||||
|
||||
## Additional features and improvements
|
||||
|
||||
- Config comparisons with [`spacy debug diff-config`](/api/cli#debug-diff).
|
||||
- Span suggester debugging with
|
||||
[`SpanCategorizer.set_candidates`](/api/spancategorizer#set_candidates).
|
||||
- Big endian support with
|
||||
[`thinc-bigendian-ops`](https://github.com/andrewsi-z/thinc-bigendian-ops) and
|
||||
updates to make `floret`, `murmurhash`, Thinc and spaCy endian neutral.
|
||||
- Initial support for Lower Sorbian and Upper Sorbian.
|
||||
- Language updates for English, French, Italian, Japanese, Korean, Norwegian,
|
||||
Russian, Slovenian, Spanish, Turkish, Ukrainian and Vietnamese.
|
||||
- New noun chunks for Finnish.
|
||||
|
||||
## Trained pipelines {id="pipelines"}
|
||||
|
||||
### New trained pipelines {id="new-pipelines"}
|
||||
|
||||
v3.3 introduces new CPU/CNN pipelines for Finnish, Korean and Swedish, which use
|
||||
the new trainable lemmatizer and
|
||||
[floret vectors](https://github.com/explosion/floret). Due to the use
|
||||
[Bloom embeddings](https://explosion.ai/blog/bloom-embeddings) and subwords, the
|
||||
pipelines have compact vectors with no out-of-vocabulary words.
|
||||
|
||||
| Package | Language | UPOS | Parser LAS | NER F |
|
||||
| ----------------------------------------------- | -------- | ---: | ---------: | ----: |
|
||||
| [`fi_core_news_sm`](/models/fi#fi_core_news_sm) | Finnish | 92.5 | 71.9 | 75.9 |
|
||||
| [`fi_core_news_md`](/models/fi#fi_core_news_md) | Finnish | 95.9 | 78.6 | 80.6 |
|
||||
| [`fi_core_news_lg`](/models/fi#fi_core_news_lg) | Finnish | 96.2 | 79.4 | 82.4 |
|
||||
| [`ko_core_news_sm`](/models/ko#ko_core_news_sm) | Korean | 86.1 | 65.6 | 71.3 |
|
||||
| [`ko_core_news_md`](/models/ko#ko_core_news_md) | Korean | 94.7 | 80.9 | 83.1 |
|
||||
| [`ko_core_news_lg`](/models/ko#ko_core_news_lg) | Korean | 94.7 | 81.3 | 85.3 |
|
||||
| [`sv_core_news_sm`](/models/sv#sv_core_news_sm) | Swedish | 95.0 | 75.9 | 74.7 |
|
||||
| [`sv_core_news_md`](/models/sv#sv_core_news_md) | Swedish | 96.3 | 78.5 | 79.3 |
|
||||
| [`sv_core_news_lg`](/models/sv#sv_core_news_lg) | Swedish | 96.3 | 79.1 | 81.1 |
|
||||
|
||||
### Pipeline updates {id="pipeline-updates"}
|
||||
|
||||
The following languages switch from lookup or rule-based lemmatizers to the new
|
||||
trainable lemmatizer: Danish, Dutch, German, Greek, Italian, Lithuanian,
|
||||
Norwegian, Polish, Portuguese and Romanian. The overall lemmatizer accuracy
|
||||
improves for all of these pipelines, but be aware that the types of errors may
|
||||
look quite different from the lookup-based lemmatizers. If you'd prefer to
|
||||
continue using the previous lemmatizer, you can
|
||||
[switch from the trainable lemmatizer to a non-trainable lemmatizer](/models#design-modify).
|
||||
|
||||
<figure>
|
||||
|
||||
| Model | v3.2 Lemma Acc | v3.3 Lemma Acc |
|
||||
| ----------------------------------------------- | -------------: | -------------: |
|
||||
| [`da_core_news_md`](/models/da#da_core_news_md) | 84.9 | 94.8 |
|
||||
| [`de_core_news_md`](/models/de#de_core_news_md) | 73.4 | 97.7 |
|
||||
| [`el_core_news_md`](/models/el#el_core_news_md) | 56.5 | 88.9 |
|
||||
| [`fi_core_news_md`](/models/fi#fi_core_news_md) | - | 86.2 |
|
||||
| [`it_core_news_md`](/models/it#it_core_news_md) | 86.6 | 97.2 |
|
||||
| [`ko_core_news_md`](/models/ko#ko_core_news_md) | - | 90.0 |
|
||||
| [`lt_core_news_md`](/models/lt#lt_core_news_md) | 71.1 | 84.8 |
|
||||
| [`nb_core_news_md`](/models/nb#nb_core_news_md) | 76.7 | 97.1 |
|
||||
| [`nl_core_news_md`](/models/nl#nl_core_news_md) | 81.5 | 94.0 |
|
||||
| [`pl_core_news_md`](/models/pl#pl_core_news_md) | 87.1 | 93.7 |
|
||||
| [`pt_core_news_md`](/models/pt#pt_core_news_md) | 76.7 | 96.9 |
|
||||
| [`ro_core_news_md`](/models/ro#ro_core_news_md) | 81.8 | 95.5 |
|
||||
| [`sv_core_news_md`](/models/sv#sv_core_news_md) | - | 95.5 |
|
||||
|
||||
</figure>
|
||||
|
||||
In addition, the vectors in the English pipelines are deduplicated to improve
|
||||
the pruned vectors in the `md` models and reduce the `lg` model size.
|
||||
|
||||
## Notes about upgrading from v3.2 {id="upgrading"}
|
||||
|
||||
### Span comparisons
|
||||
|
||||
Span comparisons involving ordering (`<`, `<=`, `>`, `>=`) now take all span
|
||||
attributes into account (start, end, label, and KB ID) so spans may be sorted in
|
||||
a slightly different order.
|
||||
|
||||
### Whitespace annotation
|
||||
|
||||
During training, annotation on whitespace tokens is handled in the same way as
|
||||
annotation on non-whitespace tokens in order to allow custom whitespace
|
||||
annotation.
|
||||
|
||||
### Doc.from_docs
|
||||
|
||||
[`Doc.from_docs`](/api/doc#from_docs) now includes `Doc.tensor` by default and
|
||||
supports excludes with an `exclude` argument in the same format as
|
||||
`Doc.to_bytes`. The supported exclude fields are `spans`, `tensor` and
|
||||
`user_data`.
|
||||
|
||||
Docs including `Doc.tensor` may be quite a bit larger in RAM, so to exclude
|
||||
`Doc.tensor` as in v3.2:
|
||||
|
||||
```diff
|
||||
-merged_doc = Doc.from_docs(docs)
|
||||
+merged_doc = Doc.from_docs(docs, exclude=["tensor"])
|
||||
```
|
||||
|
||||
### Using trained pipelines with floret vectors
|
||||
|
||||
If you're running a new trained pipeline for Finnish, Korean or Swedish on new
|
||||
texts and working with `Doc` objects, you shouldn't notice any difference with
|
||||
floret vectors vs. default vectors.
|
||||
|
||||
If you use vectors for similarity comparisons, there are a few differences,
|
||||
mainly because a floret pipeline doesn't include any kind of frequency-based
|
||||
word list similar to the list of in-vocabulary vector keys with default vectors.
|
||||
|
||||
- If your workflow iterates over the vector keys, you should use an external
|
||||
word list instead:
|
||||
|
||||
```diff
|
||||
- lexemes = [nlp.vocab[orth] for orth in nlp.vocab.vectors]
|
||||
+ lexemes = [nlp.vocab[word] for word in external_word_list]
|
||||
```
|
||||
|
||||
- `Vectors.most_similar` is not supported because there's no fixed list of
|
||||
vectors to compare your vectors to.
|
||||
|
||||
### Pipeline package version compatibility {id="version-compat"}
|
||||
|
||||
> #### Using legacy implementations
|
||||
>
|
||||
> In spaCy v3, you'll still be able to load and reference legacy implementations
|
||||
> via [`spacy-legacy`](https://github.com/explosion/spacy-legacy), even if the
|
||||
> components or architectures change and newer versions are available in the
|
||||
> core library.
|
||||
|
||||
When you're loading a pipeline package trained with an earlier version of spaCy
|
||||
v3, you will see a warning telling you that the pipeline may be incompatible.
|
||||
This doesn't necessarily have to be true, but we recommend running your
|
||||
pipelines against your test suite or evaluation data to make sure there are no
|
||||
unexpected results.
|
||||
|
||||
If you're using one of the [trained pipelines](/models) we provide, you should
|
||||
run [`spacy download`](/api/cli#download) to update to the latest version. To
|
||||
see an overview of all installed packages and their compatibility, you can run
|
||||
[`spacy validate`](/api/cli#validate).
|
||||
|
||||
If you've trained your own custom pipeline and you've confirmed that it's still
|
||||
working as expected, you can update the spaCy version requirements in the
|
||||
[`meta.json`](/api/data-formats#meta):
|
||||
|
||||
```diff
|
||||
- "spacy_version": ">=3.2.0,<3.3.0",
|
||||
+ "spacy_version": ">=3.2.0,<3.4.0",
|
||||
```
|
||||
|
||||
### Updating v3.2 configs
|
||||
|
||||
To update a config from spaCy v3.2 with the new v3.3 settings, run
|
||||
[`init fill-config`](/api/cli#init-fill-config):
|
||||
|
||||
```bash
|
||||
$ python -m spacy init fill-config config-v3.2.cfg config-v3.3.cfg
|
||||
```
|
||||
|
||||
In many cases ([`spacy train`](/api/cli#train),
|
||||
[`spacy.load`](/api/top-level#spacy.load)), the new defaults will be filled in
|
||||
automatically, but you'll need to fill in the new settings to run
|
||||
[`debug config`](/api/cli#debug) and [`debug data`](/api/cli#debug-data).
|
||||
|
||||
To see the speed improvements for the
|
||||
[`Tagger` architecture](/api/architectures#Tagger), edit your config to switch
|
||||
from `spacy.Tagger.v1` to `spacy.Tagger.v2` and then run `init fill-config`.
|
||||
@@ -0,0 +1,143 @@
|
||||
---
|
||||
title: What's New in v3.4
|
||||
teaser: New features and how to upgrade
|
||||
menu:
|
||||
- ['New Features', 'features']
|
||||
- ['Upgrading Notes', 'upgrading']
|
||||
---
|
||||
|
||||
## New features {id="features",hidden="true"}
|
||||
|
||||
spaCy v3.4 brings typing and speed improvements along with new vectors for
|
||||
English CNN pipelines and new trained pipelines for Croatian. This release also
|
||||
includes prebuilt linux aarch64 wheels for all spaCy dependencies distributed by
|
||||
Explosion.
|
||||
|
||||
### Typing improvements {id="typing"}
|
||||
|
||||
spaCy v3.4 supports pydantic v1.9 and mypy 0.950+ through extensive updates to
|
||||
types in Thinc v8.1.
|
||||
|
||||
### Speed improvements {id="speed"}
|
||||
|
||||
- For the parser, use C `saxpy`/`sgemm` provided by the `Ops` implementation in
|
||||
order to use Accelerate through `thinc-apple-ops`.
|
||||
- Improved speed of vector lookups.
|
||||
- Improved speed for `Example.get_aligned_parse` and `Example.get_aligned`.
|
||||
|
||||
## Additional features and improvements
|
||||
|
||||
- Min/max `{n,m}` operator for `Matcher` patterns.
|
||||
- Language updates:
|
||||
- Improve tokenization for Cyrillic combining diacritics.
|
||||
- Improve English tokenizer exceptions for contractions with
|
||||
this/that/these/those.
|
||||
- Updated `spacy project clone` to try both `main` and `master` branches by
|
||||
default.
|
||||
- Added confidence threshold for named entity linker.
|
||||
- Improved handling of Typer optional default values for `init_config_cli`.
|
||||
- Added cycle detection in parser projectivization methods.
|
||||
- Added counts for NER labels in `debug data`.
|
||||
- Support for adding NVTX ranges to `TrainablePipe` components.
|
||||
- Support env variable `SPACY_NUM_BUILD_JOBS` to specify the number of build
|
||||
jobs to run in parallel with `pip`.
|
||||
|
||||
## Trained pipelines {id="pipelines"}
|
||||
|
||||
### New trained pipelines {id="new-pipelines"}
|
||||
|
||||
v3.4 introduces new CPU/CNN pipelines for Croatian, which use the trainable
|
||||
lemmatizer and [floret vectors](https://github.com/explosion/floret). Due to the
|
||||
use of [Bloom embeddings](https://explosion.ai/blog/bloom-embeddings) and
|
||||
subwords, the pipelines have compact vectors with no out-of-vocabulary words.
|
||||
|
||||
| Package | UPOS | Parser LAS | NER F |
|
||||
| ----------------------------------------------- | ---: | ---------: | ----: |
|
||||
| [`hr_core_news_sm`](/models/hr#hr_core_news_sm) | 96.6 | 77.5 | 76.1 |
|
||||
| [`hr_core_news_md`](/models/hr#hr_core_news_md) | 97.3 | 80.1 | 81.8 |
|
||||
| [`hr_core_news_lg`](/models/hr#hr_core_news_lg) | 97.5 | 80.4 | 83.0 |
|
||||
|
||||
### Pipeline updates {id="pipeline-updates"}
|
||||
|
||||
All CNN pipelines have been extended with whitespace augmentation.
|
||||
|
||||
The English CNN pipelines have new word vectors:
|
||||
|
||||
| Package | Model Version | TAG | Parser LAS | NER F |
|
||||
| --------------------------------------------- | ------------- | ---: | ---------: | ----: |
|
||||
| [`en_core_web_md`](/models/en#en_core_web_md) | v3.3.0 | 97.3 | 90.1 | 84.6 |
|
||||
| [`en_core_web_md`](/models/en#en_core_web_md) | v3.4.0 | 97.2 | 90.3 | 85.5 |
|
||||
| [`en_core_web_lg`](/models/en#en_core_web_lg) | v3.3.0 | 97.4 | 90.1 | 85.3 |
|
||||
| [`en_core_web_lg`](/models/en#en_core_web_lg) | v3.4.0 | 97.3 | 90.2 | 85.6 |
|
||||
|
||||
## Notes about upgrading from v3.3 {id="upgrading"}
|
||||
|
||||
### Doc.has_vector
|
||||
|
||||
`Doc.has_vector` now matches `Token.has_vector` and `Span.has_vector`: it
|
||||
returns `True` if at least one token in the doc has a vector rather than
|
||||
checking only whether the vocab contains vectors.
|
||||
|
||||
### Using trained pipelines with floret vectors
|
||||
|
||||
If you're using a trained pipeline for Croatian, Finnish, Korean or Swedish with
|
||||
new texts and working with `Doc` objects, you shouldn't notice any difference
|
||||
between floret vectors and default vectors.
|
||||
|
||||
If you use vectors for similarity comparisons, there are a few differences,
|
||||
mainly because a floret pipeline doesn't include any kind of frequency-based
|
||||
word list similar to the list of in-vocabulary vector keys with default vectors.
|
||||
|
||||
- If your workflow iterates over the vector keys, you should use an external
|
||||
word list instead:
|
||||
|
||||
```diff
|
||||
- lexemes = [nlp.vocab[orth] for orth in nlp.vocab.vectors]
|
||||
+ lexemes = [nlp.vocab[word] for word in external_word_list]
|
||||
```
|
||||
|
||||
- `Vectors.most_similar` is not supported because there's no fixed list of
|
||||
vectors to compare your vectors to.
|
||||
|
||||
### Pipeline package version compatibility {id="version-compat"}
|
||||
|
||||
> #### Using legacy implementations
|
||||
>
|
||||
> In spaCy v3, you'll still be able to load and reference legacy implementations
|
||||
> via [`spacy-legacy`](https://github.com/explosion/spacy-legacy), even if the
|
||||
> components or architectures change and newer versions are available in the
|
||||
> core library.
|
||||
|
||||
When you're loading a pipeline package trained with an earlier version of spaCy
|
||||
v3, you will see a warning telling you that the pipeline may be incompatible.
|
||||
This doesn't necessarily have to be true, but we recommend running your
|
||||
pipelines against your test suite or evaluation data to make sure there are no
|
||||
unexpected results.
|
||||
|
||||
If you're using one of the [trained pipelines](/models) we provide, you should
|
||||
run [`spacy download`](/api/cli#download) to update to the latest version. To
|
||||
see an overview of all installed packages and their compatibility, you can run
|
||||
[`spacy validate`](/api/cli#validate).
|
||||
|
||||
If you've trained your own custom pipeline and you've confirmed that it's still
|
||||
working as expected, you can update the spaCy version requirements in the
|
||||
[`meta.json`](/api/data-formats#meta):
|
||||
|
||||
```diff
|
||||
- "spacy_version": ">=3.3.0,<3.4.0",
|
||||
+ "spacy_version": ">=3.3.0,<3.5.0",
|
||||
```
|
||||
|
||||
### Updating v3.3 configs
|
||||
|
||||
To update a config from spaCy v3.3 with the new v3.4 settings, run
|
||||
[`init fill-config`](/api/cli#init-fill-config):
|
||||
|
||||
```bash
|
||||
$ python -m spacy init fill-config config-v3.3.cfg config-v3.4.cfg
|
||||
```
|
||||
|
||||
In many cases ([`spacy train`](/api/cli#train),
|
||||
[`spacy.load`](/api/top-level#spacy.load)), the new defaults will be filled in
|
||||
automatically, but you'll need to fill in the new settings to run
|
||||
[`debug config`](/api/cli#debug) and [`debug data`](/api/cli#debug-data).
|
||||
@@ -0,0 +1,230 @@
|
||||
---
|
||||
title: What's New in v3.5
|
||||
teaser: New features and how to upgrade
|
||||
menu:
|
||||
- ['New Features', 'features']
|
||||
- ['Upgrading Notes', 'upgrading']
|
||||
---
|
||||
|
||||
## New features {id="features",hidden="true"}
|
||||
|
||||
spaCy v3.5 introduces three new CLI commands, `apply`, `benchmark` and
|
||||
`find-threshold`, adds fuzzy matching, provides improvements to our entity
|
||||
linking functionality, and includes a range of language updates and bug fixes.
|
||||
|
||||
### New CLI commands {id="cli"}
|
||||
|
||||
#### apply CLI
|
||||
|
||||
The [`apply` CLI](/api/cli#apply) can be used to apply a pipeline to one or more
|
||||
`.txt`, `.jsonl` or `.spacy` input files, saving the annotated docs in a single
|
||||
`.spacy` file.
|
||||
|
||||
```bash
|
||||
$ spacy apply en_core_web_sm my_texts/ output.spacy
|
||||
```
|
||||
|
||||
#### benchmark CLI
|
||||
|
||||
The [`benchmark` CLI](/api/cli#benchmark) has been added to extend the existing
|
||||
`evaluate` functionality with a wider range of profiling subcommands.
|
||||
|
||||
The `benchmark accuracy` CLI is introduced as an alias for `evaluate`. The new
|
||||
`benchmark speed` CLI performs warmup rounds before measuring the speed in words
|
||||
per second on batches of randomly shuffled documents from the provided data.
|
||||
|
||||
```bash
|
||||
$ spacy benchmark speed my_pipeline data.spacy
|
||||
```
|
||||
|
||||
The output is the mean performance using batches (`nlp.pipe`) with a 95%
|
||||
confidence interval, e.g., profiling `en_core_web_sm` on CPU:
|
||||
|
||||
```none
|
||||
Outliers: 2.0%, extreme outliers: 0.0%
|
||||
Mean: 18904.1 words/s (95% CI: -256.9 +244.1)
|
||||
```
|
||||
|
||||
#### find-threshold CLI
|
||||
|
||||
The [`find-threshold` CLI](/api/cli#find-threshold) runs a series of trials
|
||||
across threshold values from `0.0` to `1.0` and identifies the best threshold
|
||||
for the provided score metric.
|
||||
|
||||
The following command runs 20 trials for the `spancat` component in
|
||||
`my_pipeline`, recording the `spans_sc_f` score for each value of the threshold
|
||||
`[components.spancat.threshold]` from `0.0` to `1.0`:
|
||||
|
||||
```bash
|
||||
$ spacy find-threshold my_pipeline data.spacy spancat threshold spans_sc_f --n_trials 20
|
||||
```
|
||||
|
||||
The `find-threshold` CLI can be used with `textcat_multilabel`, `spancat` and
|
||||
custom components with thresholds that are applied while predicting or scoring.
|
||||
|
||||
### Fuzzy matching {id="fuzzy"}
|
||||
|
||||
New `FUZZY` operators support [fuzzy matching](/usage/rule-based-matching#fuzzy)
|
||||
with the `Matcher`. By default, the `FUZZY` operator allows a Levenshtein edit
|
||||
distance of 2 and up to 30% of the pattern string length. `FUZZY1`..`FUZZY9` can
|
||||
be used to specify the exact number of allowed edits.
|
||||
|
||||
```python
|
||||
# Match lowercase with fuzzy matching (allows up to 3 edits)
|
||||
pattern = [{"LOWER": {"FUZZY": "definitely"}}]
|
||||
|
||||
# Match custom attribute values with fuzzy matching (allows up to 3 edits)
|
||||
pattern = [{"_": {"country": {"FUZZY": "Kyrgyzstan"}}}]
|
||||
|
||||
# Match with exact Levenshtein edit distance limits (allows up to 4 edits)
|
||||
pattern = [{"_": {"country": {"FUZZY4": "Kyrgyzstan"}}}]
|
||||
```
|
||||
|
||||
Note that `FUZZY` uses Levenshtein edit distance rather than Damerau-Levenshtein
|
||||
edit distance, so a transposition like `teh` for `the` counts as two edits, one
|
||||
insertion and one deletion.
|
||||
|
||||
If you'd prefer an alternate fuzzy matching algorithm, you can provide your own
|
||||
custom method to the `Matcher` or as a config option for an entity ruler and
|
||||
span ruler.
|
||||
|
||||
### FUZZY and REGEX with lists {id="fuzzy-regex-lists"}
|
||||
|
||||
The `FUZZY` and `REGEX` operators are also now supported for lists with `IN` and
|
||||
`NOT_IN`:
|
||||
|
||||
```python
|
||||
pattern = [{"TEXT": {"FUZZY": {"IN": ["awesome", "cool", "wonderful"]}}}]
|
||||
pattern = [{"TEXT": {"REGEX": {"NOT_IN": ["^awe(some)?$", "^wonder(ful)?"]}}}]
|
||||
```
|
||||
|
||||
### Entity linking generalization {id="el"}
|
||||
|
||||
The knowledge base used for entity linking is now easier to customize and has a
|
||||
new default implementation [`InMemoryLookupKB`](/api/inmemorylookupkb).
|
||||
|
||||
### Additional features and improvements {id="additional-features-and-improvements"}
|
||||
|
||||
- Language updates:
|
||||
- Extended support for Slovenian
|
||||
- Fixed lookup fallback for French and Catalan lemmatizers
|
||||
- Switch Russian and Ukrainian lemmatizers to `pymorphy3`
|
||||
- Support for editorial punctuation in Ancient Greek
|
||||
- Update to Russian tokenizer exceptions
|
||||
- Small fix for Dutch stop words
|
||||
- Allow up to `typer` v0.7.x, `mypy` 0.990 and `typing_extensions` v4.4.x.
|
||||
- New `spacy.ConsoleLogger.v3` with expanded progress
|
||||
[tracking](/api/top-level#ConsoleLogger).
|
||||
- Improved scoring behavior for `textcat` with `spacy.textcat_scorer.v2` and
|
||||
`spacy.textcat_multilabel_scorer.v2`.
|
||||
- Updates so that downstream components can train properly on a frozen `tok2vec`
|
||||
or `transformer` layer.
|
||||
- Allow interpolation of variables in directory names in projects.
|
||||
- Support for local file system [remotes](/usage/projects#remote) for projects.
|
||||
- Improve UX around `displacy.serve` when the default port is in use.
|
||||
- Optional `before_update` callback that is invoked at the start of each
|
||||
[training step](/api/data-formats#config-training).
|
||||
- Improve performance of `SpanGroup` and fix typing issues for `SpanGroup` and
|
||||
`Span` objects.
|
||||
- Patch a
|
||||
[security vulnerability](https://github.com/advisories/GHSA-gw9q-c7gh-j9vm) in
|
||||
extracting tar files.
|
||||
- Add equality definition for `Vectors`.
|
||||
- Ensure `Vocab.to_disk` respects the exclude setting for `lookups` and
|
||||
`vectors`.
|
||||
- Correctly handle missing annotations in the edit tree lemmatizer.
|
||||
|
||||
### Trained pipeline updates {id="pipelines"}
|
||||
|
||||
- The CNN pipelines add `IS_SPACE` as a `tok2vec` feature for `tagger` and
|
||||
`morphologizer` components to improve tagging of non-whitespace vs. whitespace
|
||||
tokens.
|
||||
- The transformer pipelines require `spacy-transformers` v1.2, which uses the
|
||||
exact alignment from `tokenizers` for fast tokenizers instead of the heuristic
|
||||
alignment from `spacy-alignments`. For all trained pipelines except
|
||||
`ja_core_news_trf`, the alignments between spaCy tokens and transformer tokens
|
||||
may be slightly different. More details about the `spacy-transformers` changes
|
||||
in the
|
||||
[v1.2.0 release notes](https://github.com/explosion/spacy-transformers/releases/tag/v1.2.0).
|
||||
|
||||
## Notes about upgrading from v3.4 {id="upgrading"}
|
||||
|
||||
### Validation of textcat values {id="textcat-validation"}
|
||||
|
||||
An error is now raised when unsupported values are given as input to train a
|
||||
`textcat` or `textcat_multilabel` model - ensure that values are `0.0` or `1.0`
|
||||
as explained in the [docs](/api/textcategorizer#assigned-attributes).
|
||||
|
||||
### Using the default knowledge base
|
||||
|
||||
As `KnowledgeBase` is now an abstract class, you should call the constructor of
|
||||
the new `InMemoryLookupKB` instead when you want to use spaCy's default KB
|
||||
implementation:
|
||||
|
||||
```diff
|
||||
- kb = KnowledgeBase()
|
||||
+ kb = InMemoryLookupKB()
|
||||
```
|
||||
|
||||
If you've written a custom KB that inherits from `KnowledgeBase`, you'll need to
|
||||
implement its abstract methods, or alternatively inherit from `InMemoryLookupKB`
|
||||
instead.
|
||||
|
||||
### Updated scorers for tokenization and textcat {id="scores"}
|
||||
|
||||
We fixed a bug that inflated the `token_acc` scores in v3.0-v3.4. The reported
|
||||
`token_acc` will drop from v3.4 to v3.5, but if `token_p/r/f` stay the same,
|
||||
your tokenization performance has not changed from v3.4.
|
||||
|
||||
For new `textcat` or `textcat_multilabel` configs, the new default `v2` scorers:
|
||||
|
||||
- ignore `threshold` for `textcat`, so the reported `cats_p/r/f` may increase
|
||||
slightly in v3.5 even though the underlying predictions are unchanged
|
||||
- report the performance of only the **final** `textcat` or `textcat_multilabel`
|
||||
component in the pipeline by default
|
||||
- allow custom scorers to be used to score multiple `textcat` and
|
||||
`textcat_multilabel` components with `Scorer.score_cats` by restricting the
|
||||
evaluation to the component's provided labels
|
||||
|
||||
### Pipeline package version compatibility {id="version-compat"}
|
||||
|
||||
> #### Using legacy implementations
|
||||
>
|
||||
> In spaCy v3, you'll still be able to load and reference legacy implementations
|
||||
> via [`spacy-legacy`](https://github.com/explosion/spacy-legacy), even if the
|
||||
> components or architectures change and newer versions are available in the
|
||||
> core library.
|
||||
|
||||
When you're loading a pipeline package trained with an earlier version of spaCy
|
||||
v3, you will see a warning telling you that the pipeline may be incompatible.
|
||||
This doesn't necessarily have to be true, but we recommend running your
|
||||
pipelines against your test suite or evaluation data to make sure there are no
|
||||
unexpected results.
|
||||
|
||||
If you're using one of the [trained pipelines](/models) we provide, you should
|
||||
run [`spacy download`](/api/cli#download) to update to the latest version. To
|
||||
see an overview of all installed packages and their compatibility, you can run
|
||||
[`spacy validate`](/api/cli#validate).
|
||||
|
||||
If you've trained your own custom pipeline and you've confirmed that it's still
|
||||
working as expected, you can update the spaCy version requirements in the
|
||||
[`meta.json`](/api/data-formats#meta):
|
||||
|
||||
```diff
|
||||
- "spacy_version": ">=3.4.0,<3.5.0",
|
||||
+ "spacy_version": ">=3.4.0,<3.6.0",
|
||||
```
|
||||
|
||||
### Updating v3.4 configs
|
||||
|
||||
To update a config from spaCy v3.4 with the new v3.5 settings, run
|
||||
[`init fill-config`](/api/cli#init-fill-config):
|
||||
|
||||
```cli
|
||||
$ python -m spacy init fill-config config-v3.4.cfg config-v3.5.cfg
|
||||
```
|
||||
|
||||
In many cases ([`spacy train`](/api/cli#train),
|
||||
[`spacy.load`](/api/top-level#spacy.load)), the new defaults will be filled in
|
||||
automatically, but you'll need to fill in the new settings to run
|
||||
[`debug config`](/api/cli#debug) and [`debug data`](/api/cli#debug-data).
|
||||
@@ -0,0 +1,143 @@
|
||||
---
|
||||
title: What's New in v3.6
|
||||
teaser: New features and how to upgrade
|
||||
menu:
|
||||
- ['New Features', 'features']
|
||||
- ['Upgrading Notes', 'upgrading']
|
||||
---
|
||||
|
||||
## New features {id="features",hidden="true"}
|
||||
|
||||
spaCy v3.6 adds the new [`SpanFinder`](/api/spanfinder) component to the core
|
||||
spaCy library and new trained pipelines for Slovenian.
|
||||
|
||||
### SpanFinder {id="spanfinder"}
|
||||
|
||||
The [`SpanFinder`](/api/spanfinder) component identifies potentially
|
||||
overlapping, unlabeled spans by identifying span start and end tokens. It is
|
||||
intended for use in combination with a component like
|
||||
[`SpanCategorizer`](/api/spancategorizer) that may further filter or label the
|
||||
spans. See our
|
||||
[Spancat blog post](https://explosion.ai/blog/spancat#span-finder) for a more
|
||||
detailed introduction to the span finder.
|
||||
|
||||
To train a pipeline with `span_finder` + `spancat`, remember to add
|
||||
`span_finder` (and its `tok2vec` or `transformer` if required) to
|
||||
`[training.annotating_components]` so that the `spancat` component can be
|
||||
trained directly from its predictions:
|
||||
|
||||
```ini
|
||||
[nlp]
|
||||
pipeline = ["tok2vec","span_finder","spancat"]
|
||||
|
||||
[training]
|
||||
annotating_components = ["tok2vec","span_finder"]
|
||||
```
|
||||
|
||||
In practice it can be helpful to initially train the `span_finder` separately
|
||||
before [sourcing](/usage/processing-pipelines#sourced-components) it (along with
|
||||
its `tok2vec`) into the `spancat` pipeline for further training. Otherwise the
|
||||
memory usage can spike for `spancat` in the first few training steps if the
|
||||
`span_finder` makes a large number of predictions.
|
||||
|
||||
### Additional features and improvements {id="additional-features-and-improvements"}
|
||||
|
||||
- Language updates:
|
||||
- Add initial support for Malay.
|
||||
- Update Latin defaults to support noun chunks, update lexical/tokenizer
|
||||
settings and add example sentences.
|
||||
- Support `spancat_singlelabel` in `spacy debug data` CLI.
|
||||
- Add `doc.spans` rendering to `spacy evaluate` CLI displaCy output.
|
||||
- Support custom token/lexeme attribute for vectors.
|
||||
- Add option to return scores separately keyed by component name with
|
||||
`spacy evaluate --per-component`, `Language.evaluate(per_component=True)` and
|
||||
`Scorer.score(per_component=True)`. This is useful when the pipeline contains
|
||||
more than one of the same component like `textcat` that may have overlapping
|
||||
scores keys.
|
||||
- Typing updates for `PhraseMatcher` and `SpanGroup`.
|
||||
|
||||
## Trained pipelines {id="pipelines"}
|
||||
|
||||
### New trained pipelines {id="new-pipelines"}
|
||||
|
||||
v3.6 introduces new pipelines for Slovenian, which use the trainable lemmatizer
|
||||
and [floret vectors](https://github.com/explosion/floret).
|
||||
|
||||
| Package | UPOS | Parser LAS | NER F |
|
||||
| ------------------------------------------------- | ---: | ---------: | ----: |
|
||||
| [`sl_core_news_sm`](/models/sl#sl_core_news_sm) | 96.9 | 82.1 | 62.9 |
|
||||
| [`sl_core_news_md`](/models/sl#sl_core_news_md) | 97.6 | 84.3 | 73.5 |
|
||||
| [`sl_core_news_lg`](/models/sl#sl_core_news_lg) | 97.7 | 84.3 | 79.0 |
|
||||
| [`sl_core_news_trf`](/models/sl#sl_core_news_trf) | 99.0 | 91.7 | 90.0 |
|
||||
|
||||
### Pipeline updates {id="pipeline-updates"}
|
||||
|
||||
The English pipelines have been updated to improve handling of contractions with
|
||||
various apostrophes and to lemmatize "get" as a passive auxiliary.
|
||||
|
||||
The Danish pipeline `da_core_news_trf` has been updated to use
|
||||
[`vesteinn/DanskBERT`](https://huggingface.co/vesteinn/DanskBERT) with
|
||||
performance improvements across the board.
|
||||
|
||||
## Notes about upgrading from v3.5 {id="upgrading"}
|
||||
|
||||
### SpanGroup spans are now required to be from the same doc {id="spangroup-spans"}
|
||||
|
||||
When initializing a `SpanGroup`, there is a new check to verify that all added
|
||||
spans refer to the current doc. Without this check, it was possible to run into
|
||||
string store or other errors.
|
||||
|
||||
One place this may crop up is when creating `Example` objects for training with
|
||||
custom spans:
|
||||
|
||||
```diff
|
||||
doc = Doc(nlp.vocab, words=tokens) # predicted doc
|
||||
example = Example.from_dict(doc, {"ner": iob_tags})
|
||||
# use the reference doc when creating reference spans
|
||||
- span = Span(doc, 0, 5, "ORG")
|
||||
+ span = Span(example.reference, 0, 5, "ORG")
|
||||
example.reference.spans[spans_key] = [span]
|
||||
```
|
||||
|
||||
### Pipeline package version compatibility {id="version-compat"}
|
||||
|
||||
> #### Using legacy implementations
|
||||
>
|
||||
> In spaCy v3, you'll still be able to load and reference legacy implementations
|
||||
> via [`spacy-legacy`](https://github.com/explosion/spacy-legacy), even if the
|
||||
> components or architectures change and newer versions are available in the
|
||||
> core library.
|
||||
|
||||
When you're loading a pipeline package trained with an earlier version of spaCy
|
||||
v3, you will see a warning telling you that the pipeline may be incompatible.
|
||||
This doesn't necessarily have to be true, but we recommend running your
|
||||
pipelines against your test suite or evaluation data to make sure there are no
|
||||
unexpected results.
|
||||
|
||||
If you're using one of the [trained pipelines](/models) we provide, you should
|
||||
run [`spacy download`](/api/cli#download) to update to the latest version. To
|
||||
see an overview of all installed packages and their compatibility, you can run
|
||||
[`spacy validate`](/api/cli#validate).
|
||||
|
||||
If you've trained your own custom pipeline and you've confirmed that it's still
|
||||
working as expected, you can update the spaCy version requirements in the
|
||||
[`meta.json`](/api/data-formats#meta):
|
||||
|
||||
```diff
|
||||
- "spacy_version": ">=3.5.0,<3.6.0",
|
||||
+ "spacy_version": ">=3.5.0,<3.7.0",
|
||||
```
|
||||
|
||||
### Updating v3.5 configs
|
||||
|
||||
To update a config from spaCy v3.5 with the new v3.6 settings, run
|
||||
[`init fill-config`](/api/cli#init-fill-config):
|
||||
|
||||
```cli
|
||||
$ python -m spacy init fill-config config-v3.5.cfg config-v3.6.cfg
|
||||
```
|
||||
|
||||
In many cases ([`spacy train`](/api/cli#train),
|
||||
[`spacy.load`](/api/top-level#spacy.load)), the new defaults will be filled in
|
||||
automatically, but you'll need to fill in the new settings to run
|
||||
[`debug config`](/api/cli#debug) and [`debug data`](/api/cli#debug-data).
|
||||
@@ -0,0 +1,140 @@
|
||||
---
|
||||
title: What's New in v3.7
|
||||
teaser: New features and how to upgrade
|
||||
menu:
|
||||
- ['New Features', 'features']
|
||||
- ['Upgrading Notes', 'upgrading']
|
||||
---
|
||||
|
||||
## New features {id="features",hidden="true"}
|
||||
|
||||
spaCy v3.7 adds support for Python 3.12, introduces the new standalone library
|
||||
[Weasel](https://github.com/explosion/weasel) for project workflows, and updates
|
||||
the transformer-based trained pipelines to use our new
|
||||
[Curated Transformers](https://github.com/explosion/curated-transformers)
|
||||
library.
|
||||
|
||||
This release drops support for Python 3.6.
|
||||
|
||||
### Weasel {id="weasel"}
|
||||
|
||||
The [spaCy projects](/usage/projects) functionality has been moved into a new
|
||||
standalone library [Weasel](https://github.com/explosion/weasel). This brings
|
||||
minor changes to spaCy-specific settings in spaCy projects (see
|
||||
[upgrading](#upgrading) below), but also makes it possible to use the same
|
||||
workflow functionality outside of spaCy.
|
||||
|
||||
All `spacy project` commands should run as before, just now they're using Weasel
|
||||
under the hood.
|
||||
|
||||
<Infobox title="Remote storage for Python 3.12" variant="warning">
|
||||
|
||||
Remote storage for spaCy projects is not yet supported for Python 3.12. Use
|
||||
Python 3.11 or earlier for remote storage.
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Registered vectors {id="custom-vectors"}
|
||||
|
||||
You can specify a custom registered vectors class under `[nlp.vectors]` in order
|
||||
to use static vectors in formats other than the ones supported by
|
||||
[`Vectors`](/api/vectors). To implement your custom vectors, extend the abstract
|
||||
class [`BaseVectors`](/api/basevectors). See an example using
|
||||
[BPEmb subword embeddings](/usage/embeddings-transformers#custom-vectors).
|
||||
|
||||
### Additional features and improvements {id="additional-features-and-improvements"}
|
||||
|
||||
- Add support for Python 3.12.
|
||||
- Extend to Thinc v8.2.
|
||||
- Extend `transformers` extra to `spacy-transformers` v1.3.
|
||||
- Add `--spans-key` option for CLI evaluation with `spacy benchmark accuracy`.
|
||||
- Load the CLI module lazily for `spacy.info`.
|
||||
- Add type stubs for for `spacy.training.example`.
|
||||
- Warn for unsupported pattern keys in dependency matcher.
|
||||
- `Language.replace_listeners`: Pass the replaced listener and the `tok2vec`
|
||||
pipe to the callback in order to support `spacy-curated-transformers`.
|
||||
- Always use `tqdm` with `disable=None` in order to disable output in
|
||||
non-interactive environments.
|
||||
- Language updates:
|
||||
- Add left and right pointing angle brackets as punctuation to ancient Greek.
|
||||
- Update example sentences for Turkish.
|
||||
- Package setup updates:
|
||||
- Update NumPy build constraints for NumPy 1.25+. For Python 3.9+, it is no
|
||||
longer necessary to set build constraints while building binary wheels.
|
||||
- Refactor Cython profiling in order to disable profiling for Python 3.12 in
|
||||
the package setup, since Cython does not currently support profiling for
|
||||
Python 3.12.
|
||||
|
||||
## Trained pipelines {id="pipelines"}
|
||||
|
||||
### Pipeline updates {id="pipeline-updates"}
|
||||
|
||||
The transformer-based `trf` pipelines have been updated to use our new
|
||||
[Curated Transformers](https://github.com/explosion/curated-transformers)
|
||||
library using the Thinc model wrappers and pipeline component from
|
||||
[spaCy Curated Transformers](https://github.com/explosion/spacy-curated-transformers).
|
||||
|
||||
## Notes about upgrading from v3.6 {id="upgrading"}
|
||||
|
||||
This release drops support for Python 3.6, drops mypy checks for Python 3.7 and
|
||||
removes the `ray` extra. In addition there are several minor changes for spaCy
|
||||
projects described in the following section.
|
||||
|
||||
### Backwards incompatibilities for spaCy Projects {id="upgrading-projects"}
|
||||
|
||||
`spacy project` has a few backwards incompatibilities due to the transition to
|
||||
the standalone library [Weasel](https://github.com/explosion/weasel), which is
|
||||
not as tightly coupled to spaCy. Weasel produces warnings when it detects older
|
||||
spaCy-specific settings in your environment or project config.
|
||||
|
||||
- Support for the `spacy_version` configuration key has been dropped.
|
||||
- Support for the `check_requirements` configuration key has been dropped due to
|
||||
the deprecation of `pkg_resources`.
|
||||
- The `SPACY_CONFIG_OVERRIDES` environment variable is no longer checked. You
|
||||
can set configuration overrides using `WEASEL_CONFIG_OVERRIDES`.
|
||||
- Support for `SPACY_PROJECT_USE_GIT_VERSION` environment variable has been
|
||||
dropped.
|
||||
- Error codes are now Weasel-specific and do not follow spaCy error codes.
|
||||
|
||||
### Pipeline package version compatibility {id="version-compat"}
|
||||
|
||||
> #### Using legacy implementations
|
||||
>
|
||||
> In spaCy v3, you'll still be able to load and reference legacy implementations
|
||||
> via [`spacy-legacy`](https://github.com/explosion/spacy-legacy), even if the
|
||||
> components or architectures change and newer versions are available in the
|
||||
> core library.
|
||||
|
||||
When you're loading a pipeline package trained with an earlier version of spaCy
|
||||
v3, you will see a warning telling you that the pipeline may be incompatible.
|
||||
This doesn't necessarily have to be true, but we recommend running your
|
||||
pipelines against your test suite or evaluation data to make sure there are no
|
||||
unexpected results.
|
||||
|
||||
If you're using one of the [trained pipelines](/models) we provide, you should
|
||||
run [`spacy download`](/api/cli#download) to update to the latest version. To
|
||||
see an overview of all installed packages and their compatibility, you can run
|
||||
[`spacy validate`](/api/cli#validate).
|
||||
|
||||
If you've trained your own custom pipeline and you've confirmed that it's still
|
||||
working as expected, you can update the spaCy version requirements in the
|
||||
[`meta.json`](/api/data-formats#meta):
|
||||
|
||||
```diff
|
||||
- "spacy_version": ">=3.6.0,<3.7.0",
|
||||
+ "spacy_version": ">=3.6.0,<3.8.0",
|
||||
```
|
||||
|
||||
### Updating v3.6 configs
|
||||
|
||||
To update a config from spaCy v3.6 with the new v3.7 settings, run
|
||||
[`init fill-config`](/api/cli#init-fill-config):
|
||||
|
||||
```cli
|
||||
$ python -m spacy init fill-config config-v3.6.cfg config-v3.7.cfg
|
||||
```
|
||||
|
||||
In many cases ([`spacy train`](/api/cli#train),
|
||||
[`spacy.load`](/api/top-level#spacy.load)), the new defaults will be filled in
|
||||
automatically, but you'll need to fill in the new settings to run
|
||||
[`debug config`](/api/cli#debug) and [`debug data`](/api/cli#debug-data).
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,440 @@
|
||||
---
|
||||
title: Visualizers
|
||||
teaser: Visualize dependencies and entities in your browser or in a notebook
|
||||
version: 2
|
||||
menu:
|
||||
- ['Dependencies', 'dep']
|
||||
- ['Named Entities', 'ent']
|
||||
- ['Spans', 'span']
|
||||
- ['Jupyter Notebooks', 'jupyter']
|
||||
- ['Rendering HTML', 'html']
|
||||
- ['Web app usage', 'webapp']
|
||||
---
|
||||
|
||||
Visualizing a dependency parse or named entities in a text is not only a fun NLP
|
||||
demo – it can also be incredibly helpful in speeding up development and
|
||||
debugging your code and training process. That's why our popular visualizers,
|
||||
[displaCy](https://explosion.ai/demos/displacy) and
|
||||
[displaCy <sup>ENT</sup>](https://explosion.ai/demos/displacy-ent) are also an
|
||||
official part of the core library. If you're running a
|
||||
[Jupyter](https://jupyter.org) notebook, displaCy will detect this and return
|
||||
the markup in a format [ready to be rendered and exported](#jupyter).
|
||||
|
||||
The quickest way to visualize `Doc` is to use
|
||||
[`displacy.serve`](/api/top-level#displacy.serve). This will spin up a simple
|
||||
web server and let you view the result straight from your browser. displaCy can
|
||||
either take a single `Doc` or a list of `Doc` objects as its first argument.
|
||||
This lets you construct them however you like – using any pipeline or
|
||||
modifications you like. If you're using [Streamlit](https://streamlit.io), check
|
||||
out the [`spacy-streamlit`](https://github.com/explosion/spacy-streamlit)
|
||||
package that helps you integrate spaCy visualizations into your apps!
|
||||
|
||||
## Visualizing the dependency parse {id="dep"}
|
||||
|
||||
The dependency visualizer, `dep`, shows part-of-speech tags and syntactic
|
||||
dependencies.
|
||||
|
||||
```python {title="Dependency example"}
|
||||
import spacy
|
||||
from spacy import displacy
|
||||
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
doc = nlp("This is a sentence.")
|
||||
displacy.serve(doc, style="dep")
|
||||
```
|
||||
|
||||

|
||||
|
||||
The argument `options` lets you specify a dictionary of settings to customize
|
||||
the layout, for example:
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
There's currently a known issue with the `compact` mode for sentences with short
|
||||
arrows and long dependency labels, that causes labels longer than the arrow to
|
||||
wrap. So if you come across this problem, especially when using custom labels,
|
||||
you'll have to increase the `distance` setting in the `options` to allow longer
|
||||
arcs.
|
||||
|
||||
Moreover, you might need to modify the `offset_x` argument depending on the shape
|
||||
of your document. Otherwise, the left part of the document may overflow beyond the
|
||||
container's border.
|
||||
|
||||
</Infobox>
|
||||
|
||||
| Argument | Description |
|
||||
| ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `compact` | "Compact mode" with square arrows that takes up less space. Defaults to `False`. ~~bool~~ |
|
||||
| `color` | Text color. Can be provided in any CSS legal format as a string e.g.: `"#00ff00"`, `"rgb(0, 255, 0)"`, `"hsl(120, 100%, 50%)"` and `"green"` all correspond to the color green (without transparency). Defaults to `"#000000"`. ~~str~~ |
|
||||
| `bg` | Background color. Can be provided in any CSS legal format as a string e.g.: `"#00ff00"`, `"rgb(0, 255, 0)"`, `"hsl(120, 100%, 50%)"` and `"green"` all correspond to the color green (without transparency). Defaults to `"#ffffff"`. ~~str~~ |
|
||||
| `font` | Font name or font family for all text. Defaults to `"Arial"`. ~~str~~ |
|
||||
| `offset_x` | Spacing on left side of the SVG in px. You might need to tweak this setting for long texts. Defaults to `50`. ~~int~~ |
|
||||
|
||||
For a list of all available options, see the
|
||||
[`displacy` API documentation](/api/top-level#displacy_options).
|
||||
|
||||
> #### Options example
|
||||
>
|
||||
> ```python
|
||||
> options = {"compact": True, "bg": "#09a3d5",
|
||||
> "color": "white", "font": "Source Sans Pro"}
|
||||
> displacy.serve(doc, style="dep", options=options)
|
||||
> ```
|
||||
|
||||

|
||||
|
||||
### Visualizing long texts {id="dep-long-text",version="2.0.12"}
|
||||
|
||||
Long texts can become difficult to read when displayed in one row, so it's often
|
||||
better to visualize them sentence-by-sentence instead. As of v2.0.12, `displacy`
|
||||
supports rendering both [`Doc`](/api/doc) and [`Span`](/api/span) objects, as
|
||||
well as lists of `Doc`s or `Span`s. Instead of passing the full `Doc` to
|
||||
`displacy.serve`, you can also pass in a list `doc.sents`. This will create one
|
||||
visualization for each sentence.
|
||||
|
||||
```python
|
||||
import spacy
|
||||
from spacy import displacy
|
||||
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
text = """In ancient Rome, some neighbors live in three adjacent houses. In the center is the house of Senex, who lives there with wife Domina, son Hero, and several slaves, including head slave Hysterium and the musical's main character Pseudolus. A slave belonging to Hero, Pseudolus wishes to buy, win, or steal his freedom. One of the neighboring houses is owned by Marcus Lycus, who is a buyer and seller of beautiful women; the other belongs to the ancient Erronius, who is abroad searching for his long-lost children (stolen in infancy by pirates). One day, Senex and Domina go on a trip and leave Pseudolus in charge of Hero. Hero confides in Pseudolus that he is in love with the lovely Philia, one of the courtesans in the House of Lycus (albeit still a virgin)."""
|
||||
doc = nlp(text)
|
||||
sentence_spans = list(doc.sents)
|
||||
displacy.serve(sentence_spans, style="dep")
|
||||
```
|
||||
|
||||
## Visualizing the entity recognizer {id="ent"}
|
||||
|
||||
The entity visualizer, `ent`, highlights named entities and their labels in a
|
||||
text.
|
||||
|
||||
```python {title="Named Entity example"}
|
||||
import spacy
|
||||
from spacy import displacy
|
||||
|
||||
text = "When Sebastian Thrun started working on self-driving cars at Google in 2007, few people outside of the company took him seriously."
|
||||
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
doc = nlp(text)
|
||||
displacy.serve(doc, style="ent")
|
||||
```
|
||||
|
||||
<Standalone height={180}>
|
||||
<div style={{lineHeight: 2.5, fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'", fontSize: 18}}>When <mark style={{ background: '#aa9cfc', padding: '0.45em 0.6em', margin: '0 0.25em', lineHeight: 1, borderRadius: '0.35em'}}>Sebastian Thrun <span style={{ fontSize: '0.8em', fontWeight: 'bold', lineHeight: 1, borderRadius: '0.35em', marginLeft: '0.5rem'}}>PERSON</span></mark> started working on self-driving cars at <mark style={{ background: '#7aecec', padding: '0.45em 0.6em', margin: '0 0.25em', lineHeight: 1, borderRadius: '0.35em'}}>Google <span style={{ fontSize: '0.8em', fontWeight: 'bold', lineHeight: 1, borderRadius: '0.35em', marginLeft: '0.5rem'}}>ORG</span></mark> in <mark style={{ background: '#bfe1d9', padding: '0.45em 0.6em', margin: '0 0.25em', lineHeight: 1, borderRadius: '0.35em'}}>2007 <span style={{ fontSize: '0.8em', fontWeight: 'bold', lineHeight: 1, borderRadius: '0.35em', marginLeft: '0.5rem'}}>DATE</span></mark>, few people outside of the company took him seriously.</div>
|
||||
</Standalone>
|
||||
|
||||
The entity visualizer lets you customize the following `options`:
|
||||
|
||||
| Argument | Description |
|
||||
| -------- | ------------------------------------------------------------------------------------------------------------- |
|
||||
| `ents` | Entity types to highlight (`None` for all types). Defaults to `None`. ~~Optional[List[str]]~~ |
|
||||
| `colors` | Color overrides. Entity types should be mapped to color names or values. Defaults to `{}`. ~~Dict[str, str]~~ |
|
||||
|
||||
If you specify a list of `ents`, only those entity types will be rendered – for
|
||||
example, you can choose to display `PERSON` entities. Internally, the visualizer
|
||||
knows nothing about available entity types and will render whichever spans and
|
||||
labels it receives. This makes it especially easy to work with custom entity
|
||||
types. By default, displaCy comes with colors for all entity types used by
|
||||
[trained spaCy pipelines](/models). If you're using custom entity types, you can
|
||||
use the `colors` setting to add your own colors for them.
|
||||
|
||||
> #### Options example
|
||||
>
|
||||
> ```python
|
||||
> colors = {"ORG": "linear-gradient(90deg, #aa9cfc, #fc9ce7)"}
|
||||
> options = {"ents": ["ORG"], "colors": colors}
|
||||
> displacy.serve(doc, style="ent", options=options)
|
||||
> ```
|
||||
|
||||
<Standalone height={225}>
|
||||
<div style={{lineHeight: 2.5, fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'", fontSize: 18}}>But <mark style={{ background: 'linear-gradient(90deg, #aa9cfc, #fc9ce7)', padding: '0.45em 0.6em', margin: '0 0.25em', lineHeight: 1, borderRadius: '0.35em'}}>Google <span style={{ fontSize: '0.8em', fontWeight: 'bold', lineHeight: 1, borderRadius: '0.35em', marginLeft: '0.5rem'}}>ORG</span></mark> is starting from behind. The company made a late push into hardware, and <mark style={{ background: 'linear-gradient(90deg, #aa9cfc, #fc9ce7)', padding: '0.45em 0.6em', margin: '0 0.25em', lineHeight: 1, borderRadius: '0.35em'}}>Apple <span style={{ fontSize: '0.8em', fontWeight: 'bold', lineHeight: 1, borderRadius: '0.35em', marginLeft: '0.5rem'}}>ORG</span></mark>’s Siri, available on iPhones, and <mark style={{ background: 'linear-gradient(90deg, #aa9cfc, #fc9ce7)', padding: '0.45em 0.6em', margin: '0 0.25em', lineHeight: 1, borderRadius: '0.35em'}}>Amazon <span style={{ fontSize: '0.8em', fontWeight: 'bold', lineHeight: 1, borderRadius: '0.35em', marginLeft: '0.5rem'}}>ORG</span></mark>’s Alexa software, which runs on its Echo and Dot devices, have clear leads in consumer adoption.</div>
|
||||
</Standalone>
|
||||
|
||||
The above example uses a little trick: Since the background color values are
|
||||
added as the `background` style attribute, you can use any
|
||||
[valid background value](https://tympanus.net/codrops/css_reference/background/)
|
||||
or shorthand – including gradients and even images!
|
||||
|
||||
### Adding titles to documents {id="ent-titles"}
|
||||
|
||||
Rendering several large documents on one page can easily become confusing. To
|
||||
add a headline to each visualization, you can add a `title` to its `user_data`.
|
||||
User data is never touched or modified by spaCy.
|
||||
|
||||
```python
|
||||
doc = nlp("This is a sentence about Google.")
|
||||
doc.user_data["title"] = "This is a title"
|
||||
displacy.serve(doc, style="ent")
|
||||
```
|
||||
|
||||
This feature is especially handy if you're using displaCy to compare performance
|
||||
at different stages of a process, e.g. during training. Here you could use the
|
||||
title for a brief description of the text example and the number of iterations.
|
||||
|
||||
## Visualizing spans {id="span"}
|
||||
|
||||
The span visualizer, `span`, highlights overlapping spans in a text.
|
||||
|
||||
```python {title="Span example"}
|
||||
import spacy
|
||||
from spacy import displacy
|
||||
from spacy.tokens import Span
|
||||
|
||||
text = "Welcome to the Bank of China."
|
||||
|
||||
nlp = spacy.blank("en")
|
||||
doc = nlp(text)
|
||||
|
||||
doc.spans["sc"] = [
|
||||
Span(doc, 3, 6, "ORG"),
|
||||
Span(doc, 5, 6, "GPE"),
|
||||
]
|
||||
|
||||
displacy.serve(doc, style="span")
|
||||
```
|
||||
|
||||
<Standalone height={100}>
|
||||
<div style={{ lineHeight: 2.5, direction: 'ltr', fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'", fontSize: 18 }}>Welcome to the <span style={{ fontWeight: 'bold', display: 'inline-block', position: 'relative'}}>Bank<span style={{ background: '#7aecec', top: 40, height: 4, left: -1, width: 'calc(100% + 2px)', position: 'absolute' }}></span><span style={{ background: '#7aecec', top: 40, height: 4, borderTopLeftRadius: 3, borderBottomLeftRadius: 3, left: -1, width: 'calc(100% + 2px)', position: 'absolute' }}><span style={{ background: '#7aecec', color: '#000', top: '-0.5em', padding: '2px 3px', position: 'absolute', fontSize: '0.6em', fontWeight: 'bold', lineHeight: 1, borderRadius: 3 }}>ORG</span></span></span> <span style={{ fontWeight: 'bold', display: 'inline-block', position: 'relative'}}>of <span style={{ background: '#7aecec', top: 40, height: 4, left: -1, width: 'calc(100% + 2px)', position: 'absolute' }}></span></span> <span style={{ fontWeight: 'bold', display: 'inline-block', position: 'relative'}}>China<span style={{ background: '#7aecec', top: 40, height: 4, left: -1, width: 'calc(100% + 2px)', position: 'absolute' }}></span><span style={{ background: '#feca74', top: 57, height: 4, left: -1, width: 'calc(100% + 2px)', position: 'absolute' }}></span><span style={{ background: '#feca74', top: 57, height: 4, borderTopLeftRadius: 3, borderBottomLeftRadius: 3, left: -1, width: 'calc(100% + 2px)', position: 'absolute' }}><span style={{ background: '#feca74', color: '#000', top: '-0.5em', padding: '2px 3px', position: 'absolute', fontSize: '0.6em', fontWeight: 'bold', lineHeight: 1, borderRadius: 3 }}>GPE</span></span></span>.</div>
|
||||
</Standalone>
|
||||
|
||||
The span visualizer lets you customize the following `options`:
|
||||
|
||||
| Argument | Description |
|
||||
| ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `spans_key` | Which spans key to render spans from. Default is `"sc"`. ~~str~~ |
|
||||
| `templates` | Dictionary containing the keys `"span"`, `"slice"`, and `"start"`. These dictate how the overall span, a span slice, and the starting token will be rendered. ~~Optional[Dict[str, str]~~ |
|
||||
| `kb_url_template` | Optional template to construct the KB url for the entity to link to. Expects a python f-string format with single field to fill in ~~Optional[str]~~ |
|
||||
| `colors` | Color overrides. Entity types should be mapped to color names or values. ~~Dict[str, str]~~ |
|
||||
|
||||
Because spans can be stored across different keys in `doc.spans`, you need to
|
||||
specify which one displaCy should use with `spans_key` (`sc` is the default).
|
||||
|
||||
> #### Options example
|
||||
>
|
||||
> ```python
|
||||
> doc.spans["custom"] = [Span(doc, 3, 6, "BANK")]
|
||||
> options = {"spans_key": "custom"}
|
||||
> displacy.serve(doc, style="span", options=options)
|
||||
> ```
|
||||
|
||||
<Standalone height={100}>
|
||||
<div style={{ lineHeight: 2.5, direction: 'ltr', fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'", fontSize: 18 }}>Welcome to the <span style={{ fontWeight: 'bold', display: 'inline-block', position: 'relative'}}>Bank<span style={{ background: '#ddd', top: 40, height: 4, left: -1, width: 'calc(100% + 2px)', position: 'absolute' }}></span><span style={{ background: '#ddd', top: 40, height: 4, borderTopLeftRadius: 3, borderBottomLeftRadius: 3, left: -1, width: 'calc(100% + 2px)', position: 'absolute' }}><span style={{ background: '#ddd', color: '#000', top: '-0.5em', padding: '2px 3px', position: 'absolute', fontSize: '0.6em', fontWeight: 'bold', lineHeight: 1, borderRadius: 3 }}>BANK</span></span></span> <span style={{ fontWeight: 'bold', display: 'inline-block', position: 'relative'}}>of <span style={{ background: '#ddd', top: 40, height: 4, left: -1, width: 'calc(100% + 2px)', position: 'absolute' }}></span></span> <span style={{ fontWeight: 'bold', display: 'inline-block', position: 'relative'}}>China<span style={{ background: '#ddd', top: 40, height: 4, left: -1, width: 'calc(100% + 2px)', position: 'absolute' }}></span></span>.</div>
|
||||
</Standalone>
|
||||
|
||||
## Using displaCy in Jupyter notebooks {id="jupyter"}
|
||||
|
||||
displaCy is able to detect whether you're working in a
|
||||
[Jupyter](https://jupyter.org) notebook, and will return markup that can be
|
||||
rendered in a cell straight away. When you export your notebook, the
|
||||
visualizations will be included as HTML.
|
||||
|
||||
```python {title="Jupyter example"}
|
||||
# Don't forget to install a trained pipeline, e.g.: python -m spacy download en
|
||||
|
||||
# In[1]:
|
||||
import spacy
|
||||
from spacy import displacy
|
||||
|
||||
# In[2]:
|
||||
doc = nlp("Rats are various medium-sized, long-tailed rodents.")
|
||||
displacy.render(doc, style="dep")
|
||||
|
||||
# In[3]:
|
||||
doc2 = nlp(LONG_NEWS_ARTICLE)
|
||||
displacy.render(doc2, style="ent")
|
||||
```
|
||||
|
||||
<Infobox variant="warning" title="Important note">
|
||||
|
||||
To explicitly enable or disable "Jupyter mode", you can use the `jupyter`
|
||||
keyword argument – e.g. to return raw HTML in a notebook, or to force Jupyter
|
||||
rendering if auto-detection fails.
|
||||
|
||||
</Infobox>
|
||||
|
||||

|
||||
|
||||
Internally, displaCy imports `display` and `HTML` from `IPython.display`
|
||||
and returns a Jupyter HTML object. If you were doing it manually, it'd look like
|
||||
this:
|
||||
|
||||
```python
|
||||
from IPython.display import display, HTML
|
||||
|
||||
html = displacy.render(doc, style="dep")
|
||||
display(HTML(html))
|
||||
```
|
||||
|
||||
## Rendering HTML {id="html"}
|
||||
|
||||
If you don't need the web server and just want to generate the markup – for
|
||||
example, to export it to a file or serve it in a custom way – you can use
|
||||
[`displacy.render`](/api/top-level#displacy.render). It works the same way, but
|
||||
returns a string containing the markup.
|
||||
|
||||
```python {title="Example"}
|
||||
import spacy
|
||||
from spacy import displacy
|
||||
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
doc1 = nlp("This is a sentence.")
|
||||
doc2 = nlp("This is another sentence.")
|
||||
html = displacy.render([doc1, doc2], style="dep", page=True)
|
||||
```
|
||||
|
||||
`page=True` renders the markup wrapped as a full HTML page. For minified and
|
||||
more compact HTML markup, you can set `minify=True`. If you're rendering a
|
||||
dependency parse, you can also export it as an `.svg` file.
|
||||
|
||||
> #### What's SVG?
|
||||
>
|
||||
> Unlike other image formats, the SVG (Scalable Vector Graphics) uses XML markup
|
||||
> that's easy to manipulate
|
||||
> [using CSS](https://www.smashingmagazine.com/2014/11/styling-and-animating-svgs-with-css/)
|
||||
> or
|
||||
> [JavaScript](https://css-tricks.com/smil-is-dead-long-live-smil-a-guide-to-alternatives-to-smil-features/).
|
||||
> Essentially, SVG lets you design with code, which makes it a perfect fit for
|
||||
> visualizing dependency trees. SVGs can be embedded online in an `<img>` tag,
|
||||
> or inlined in an HTML document. They're also pretty easy to
|
||||
> [convert](https://convertio.co/image-converter/).
|
||||
|
||||
```python
|
||||
svg = displacy.render(doc, style="dep")
|
||||
output_path = Path("/images/sentence.svg")
|
||||
output_path.open("w", encoding="utf-8").write(svg)
|
||||
```
|
||||
|
||||
<Infobox title="Important note" variant="warning">
|
||||
|
||||
Since each visualization is generated as a separate SVG, exporting `.svg` files
|
||||
only works if you're rendering **one single doc** at a time. (This makes sense –
|
||||
after all, each visualization should be a standalone graphic.) So instead of
|
||||
rendering all `Doc`s at once, loop over them and export them separately.
|
||||
|
||||
</Infobox>
|
||||
|
||||
### Example: Export SVG graphics of dependency parses {id="examples-export-svg"}
|
||||
|
||||
```python {title="Example"}
|
||||
import spacy
|
||||
from spacy import displacy
|
||||
from pathlib import Path
|
||||
|
||||
nlp = spacy.load("en_core_web_sm")
|
||||
sentences = ["This is an example.", "This is another one."]
|
||||
for sent in sentences:
|
||||
doc = nlp(sent)
|
||||
svg = displacy.render(doc, style="dep", jupyter=False)
|
||||
file_name = '-'.join([w.text for w in doc if not w.is_punct]) + ".svg"
|
||||
output_path = Path("/images/" + file_name)
|
||||
output_path.open("w", encoding="utf-8").write(svg)
|
||||
```
|
||||
|
||||
The above code will generate the dependency visualizations as two files,
|
||||
`This-is-an-example.svg` and `This-is-another-one.svg`.
|
||||
|
||||
### Rendering data manually {id="manual-usage"}
|
||||
|
||||
You can also use displaCy to manually render data. This can be useful if you
|
||||
want to visualize output from other libraries, like [NLTK](http://www.nltk.org)
|
||||
or
|
||||
[SyntaxNet](https://github.com/tensorflow/models/tree/master/research/syntaxnet).
|
||||
If you set `manual=True` on either `render()` or `serve()`, you can pass in data
|
||||
in displaCy's format as a dictionary (instead of `Doc` objects). There are
|
||||
helper functions for converting `Doc` objects to
|
||||
[displaCy's format](/api/top-level#displacy_structures) for use with
|
||||
`manual=True`: [`displacy.parse_deps`](/api/top-level#displacy.parse_deps),
|
||||
[`displacy.parse_ents`](/api/top-level#displacy.parse_ents), and
|
||||
[`displacy.parse_spans`](/api/top-level#displacy.parse_spans).
|
||||
|
||||
> #### Example with parse function
|
||||
>
|
||||
> ```python
|
||||
> doc = nlp("But Google is starting from behind.")
|
||||
> ex = displacy.parse_ents(doc)
|
||||
> html = displacy.render(ex, style="ent", manual=True)
|
||||
> ```
|
||||
|
||||
> #### Example with raw data
|
||||
>
|
||||
> ```python
|
||||
> ex = [{"text": "But Google is starting from behind.",
|
||||
> "ents": [{"start": 4, "end": 10, "label": "ORG"}],
|
||||
> "title": None}]
|
||||
> html = displacy.render(ex, style="ent", manual=True)
|
||||
> ```
|
||||
|
||||
```python {title="DEP input"}
|
||||
{
|
||||
"words": [
|
||||
{"text": "This", "tag": "DT"},
|
||||
{"text": "is", "tag": "VBZ"},
|
||||
{"text": "a", "tag": "DT"},
|
||||
{"text": "sentence", "tag": "NN"}
|
||||
],
|
||||
"arcs": [
|
||||
{"start": 0, "end": 1, "label": "nsubj", "dir": "left"},
|
||||
{"start": 2, "end": 3, "label": "det", "dir": "left"},
|
||||
{"start": 1, "end": 3, "label": "attr", "dir": "right"}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
```python {title="ENT input"}
|
||||
{
|
||||
"text": "But Google is starting from behind.",
|
||||
"ents": [{"start": 4, "end": 10, "label": "ORG"}],
|
||||
"title": None
|
||||
}
|
||||
```
|
||||
|
||||
```python {title="ENT input with knowledge base links"}
|
||||
{
|
||||
"text": "But Google is starting from behind.",
|
||||
"ents": [{"start": 4, "end": 10, "label": "ORG", "kb_id": "Q95", "kb_url": "https://www.wikidata.org/entity/Q95"}],
|
||||
"title": None
|
||||
}
|
||||
```
|
||||
|
||||
```python {title="SPANS input"}
|
||||
{
|
||||
"text": "Welcome to the Bank of China.",
|
||||
"spans": [
|
||||
{"start_token": 3, "end_token": 6, "label": "ORG"},
|
||||
{"start_token": 5, "end_token": 6, "label": "GPE"},
|
||||
],
|
||||
"tokens": ["Welcome", "to", "the", "Bank", "of", "China", "."],
|
||||
}
|
||||
```
|
||||
|
||||
## Using displaCy in a web application {id="webapp"}
|
||||
|
||||
If you want to use the visualizers as part of a web application, for example to
|
||||
create something like our [online demo](https://explosion.ai/demos/displacy),
|
||||
it's not recommended to only wrap and serve the displaCy renderer. Instead, you
|
||||
should only rely on the server to perform spaCy's processing capabilities, and
|
||||
use a client-side implementation like
|
||||
[`displaCy.js`](https://github.com/explosion/displacy) to render the
|
||||
JSON-formatted output.
|
||||
|
||||
> #### Why not return the HTML by the server?
|
||||
>
|
||||
> It's certainly possible to just have your server return the markup. But
|
||||
> outputting raw, unsanitized HTML is risky and makes your app vulnerable to
|
||||
> [cross-site scripting](https://en.wikipedia.org/wiki/Cross-site_scripting)
|
||||
> (XSS). All your user needs to do is find a way to make spaCy return text like
|
||||
> `<script src="malicious-code.js"></script>`, which is pretty easy in NER mode.
|
||||
> Instead of relying on the server to render and sanitize HTML, you can do this
|
||||
> on the client in JavaScript. displaCy.js creates the markup as DOM nodes and
|
||||
> will never insert raw HTML.
|
||||
|
||||
<Grid cols={2}>
|
||||
|
||||
Alternatively, if you're using [Streamlit](https://streamlit.io), check out the
|
||||
[`spacy-streamlit`](https://github.com/explosion/spacy-streamlit) package that
|
||||
helps you integrate spaCy visualizations into your apps. It includes a full
|
||||
embedded visualizer, as well as individual components.
|
||||
|
||||

|
||||
|
||||
</Grid>
|
||||
Reference in New Issue
Block a user