2020-07-21 11:33:46 +03:00
|
|
|
---
|
|
|
|
title: Morphology
|
|
|
|
tag: class
|
|
|
|
source: spacy/morphology.pyx
|
|
|
|
---
|
|
|
|
|
2020-07-29 12:36:42 +03:00
|
|
|
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
|
2020-07-21 11:33:46 +03:00
|
|
|
morphological analysis, so queries of morphological attributes are delegated to
|
|
|
|
this class.
|
|
|
|
|
|
|
|
## Morphology.\_\_init\_\_ {#init tag="method"}
|
|
|
|
|
2020-08-07 16:27:13 +03:00
|
|
|
Create a Morphology object.
|
2020-07-21 11:33:46 +03:00
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> from spacy.morphology import Morphology
|
|
|
|
>
|
2020-08-07 16:27:13 +03:00
|
|
|
> morphology = Morphology(strings)
|
2020-07-21 11:33:46 +03:00
|
|
|
> ```
|
|
|
|
|
2020-08-07 16:27:13 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| --------- | ------------- | ----------------- |
|
|
|
|
| `strings` | `StringStore` | The string store. |
|
2020-07-21 11:33:46 +03:00
|
|
|
|
|
|
|
## Morphology.add {#add tag="method"}
|
|
|
|
|
2020-07-29 12:36:42 +03:00
|
|
|
Insert a morphological analysis in the morphology table, if not already present.
|
|
|
|
The morphological analysis may be provided in the UD FEATS format as a string or
|
|
|
|
in the tag map dictionary format. Returns the hash of the new analysis.
|
2020-07-21 11:33:46 +03:00
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> feats = "Feat1=Val1|Feat2=Val2"
|
|
|
|
> hash = nlp.vocab.morphology.add(feats)
|
|
|
|
> assert hash == nlp.vocab.strings[feats]
|
|
|
|
> ```
|
|
|
|
|
2020-07-29 12:36:42 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| ---------- | ------------------ | --------------------------- |
|
|
|
|
| `features` | `Union[Dict, str]` | The morphological features. |
|
2020-07-21 11:33:46 +03:00
|
|
|
|
|
|
|
## Morphology.get {#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 string for the hash of the morphological analysis.
|
|
|
|
|
2020-07-29 12:36:42 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| ------- | ---- | --------------------------------------- |
|
|
|
|
| `morph` | int | The hash of the morphological analysis. |
|
2020-07-21 11:33:46 +03:00
|
|
|
|
|
|
|
## Morphology.feats_to_dict {#feats_to_dict tag="staticmethod"}
|
|
|
|
|
|
|
|
Convert a string FEATS 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"}
|
|
|
|
> ```
|
|
|
|
|
2020-07-29 12:36:42 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| ----------- | ---- | ------------------------------------------------------------------ |
|
2020-07-21 11:33:46 +03:00
|
|
|
| `feats` | str | The morphological features in Universal Dependencies FEATS format. |
|
2020-07-29 12:36:42 +03:00
|
|
|
| **RETURNS** | dict | The morphological features as a dictionary. |
|
2020-07-21 11:33:46 +03:00
|
|
|
|
|
|
|
## Morphology.dict_to_feats {#dict_to_feats tag="staticmethod"}
|
|
|
|
|
|
|
|
Convert a dictionary of features and values to a string FEATS representation.
|
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> from spacy.morphology import Morphology
|
|
|
|
> f = Morphology.dict_to_feats({"Feat1": "Val1", "Feat2": "Val2"})
|
|
|
|
> assert f == "Feat1=Val1|Feat2=Val2"
|
|
|
|
> ```
|
|
|
|
|
2020-07-29 12:36:42 +03:00
|
|
|
| Name | Type | Description |
|
2020-07-21 11:33:46 +03:00
|
|
|
| ------------ | ----------------- | --------------------------------------------------------------------- |
|
|
|
|
| `feats_dict` | `Dict[str, Dict]` | The morphological features as a dictionary. |
|
|
|
|
| **RETURNS** | str | The morphological features as in Universal Dependencies FEATS format. |
|
|
|
|
|
|
|
|
## Attributes {#attributes}
|
|
|
|
|
|
|
|
| Name | Type | Description |
|
|
|
|
| ------------- | ----- | -------------------------------------------- |
|
|
|
|
| `FEATURE_SEP` | `str` | The FEATS feature separator. Default is `|`. |
|
|
|
|
| `FIELD_SEP` | `str` | The FEATS field separator. Default is `=`. |
|
|
|
|
| `VALUE_SEP` | `str` | The FEATS value separator. Default is `,`. |
|