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
2020-08-21 14:22:59 +03:00
this class. See [`MorphAnalysis`](/api/morphology#morphanalysis) for the
2020-08-17 17:45:24 +03:00
container storing a single morphological analysis.
2020-07-21 11:33:46 +03:00
2022-11-14 22:35:07 +03:00
## Morphology.\_\_init\_\_ {id="init",tag="method"}
2020-07-21 11:33:46 +03:00
2020-09-24 14:15:28 +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-17 17:45:24 +03:00
| Name | Description |
| --------- | --------------------------------- |
| `strings` | The string store. ~~StringStore~~ |
2020-07-21 11:33:46 +03:00
2022-11-14 22:35:07 +03:00
## Morphology.add {id="add",tag="method"}
2020-07-21 11:33:46 +03:00
2020-07-29 12:36:42 +03:00
Insert a morphological analysis in the morphology table, if not already present.
2020-08-17 17:45:24 +03:00
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.
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-08-17 17:45:24 +03:00
| Name | Description |
| ---------- | ------------------------------------------------ |
| `features` | The morphological features. ~~Union[Dict, str]~~ |
2020-07-21 11:33:46 +03:00
2022-11-14 22:35:07 +03:00
## Morphology.get {id="get",tag="method"}
2020-07-21 11:33:46 +03:00
> #### Example
>
> ```python
> feats = "Feat1=Val1|Feat2=Val2"
> hash = nlp.vocab.morphology.add(feats)
> assert nlp.vocab.morphology.get(hash) == feats
> ```
2020-08-17 17:45:24 +03:00
Get the
[FEATS](https://universaldependencies.org/format.html#morphological-annotation)
string for the hash of the morphological analysis.
2020-07-21 11:33:46 +03:00
2020-08-17 17:45:24 +03:00
| Name | Description |
| ------- | ----------------------------------------------- |
| `morph` | The hash of the morphological analysis. ~~int~~ |
2020-07-21 11:33:46 +03:00
2022-11-14 22:35:07 +03:00
## Morphology.feats_to_dict {id="feats_to_dict",tag="staticmethod"}
2020-07-21 11:33:46 +03:00
2020-08-17 17:45:24 +03:00
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.
2020-07-21 11:33:46 +03:00
> #### Example
>
> ```python
> from spacy.morphology import Morphology
> d = Morphology.feats_to_dict("Feat1=Val1|Feat2=Val2")
> assert d == {"Feat1": "Val1", "Feat2": "Val2"}
> ```
2020-08-17 17:45:24 +03:00
| 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]~~ |
2020-07-21 11:33:46 +03:00
2022-11-14 22:35:07 +03:00
## Morphology.dict_to_feats {id="dict_to_feats",tag="staticmethod"}
2020-07-21 11:33:46 +03:00
2020-08-17 17:45:24 +03:00
Convert a dictionary of features and values to a string
[FEATS](https://universaldependencies.org/format.html#morphological-annotation)
representation.
2020-07-21 11:33:46 +03:00
> #### Example
>
> ```python
> from spacy.morphology import Morphology
> f = Morphology.dict_to_feats({"Feat1": "Val1", "Feat2": "Val2"})
> assert f == "Feat1=Val1|Feat2=Val2"
> ```
2021-06-28 12:48:11 +03:00
| Name | Description |
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| `feats_dict` | The morphological features as a dictionary. ~~Dict[str, str]~~ |
2020-09-24 14:15:28 +03:00
| **RETURNS** | The morphological features in Universal Dependencies [FEATS](https://universaldependencies.org/format.html#morphological-annotation) format. ~~str~~ |
2020-07-21 11:33:46 +03:00
2022-11-14 22:35:07 +03:00
## Attributes {id="attributes"}
2020-07-21 11:33:46 +03:00
2022-12-20 19:41:38 +03:00
| 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~~ |
2020-08-17 17:45:24 +03:00
2022-11-14 22:35:07 +03:00
## MorphAnalysis {id="morphanalysis",tag="class",source="spacy/tokens/morphanalysis.pyx"}
2020-08-17 17:45:24 +03:00
Stores a single morphological analysis.
2022-11-14 22:35:07 +03:00
### MorphAnalysis.\_\_init\_\_ {id="morphanalysis-init",tag="method"}
2020-08-17 17:45:24 +03:00
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]~~ |
2022-11-14 22:35:07 +03:00
### MorphAnalysis.\_\_contains\_\_ {id="morphanalysis-contains",tag="method"}
2020-08-17 17:45:24 +03:00
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 |
| ----------- | --------------------------------------------- |
| **RETURNS** | A feature/value pair in the analysis. ~~str~~ |
2022-11-14 22:35:07 +03:00
### MorphAnalysis.\_\_iter\_\_ {id="morphanalysis-iter",tag="method"}
2020-08-17 17:45:24 +03:00
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~~ |
2022-11-14 22:35:07 +03:00
### MorphAnalysis.\_\_len\_\_ {id="morphanalysis-len",tag="method"}
2020-08-17 17:45:24 +03:00
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~~ |
2022-11-14 22:35:07 +03:00
### MorphAnalysis.\_\_str\_\_ {id="morphanalysis-str",tag="method"}
2020-08-17 17:45:24 +03:00
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~~ |
2022-11-14 22:35:07 +03:00
### MorphAnalysis.get {id="morphanalysis-get",tag="method"}
2020-08-17 17:45:24 +03:00
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~~ |
| **RETURNS** | A list of the individual features. ~~List[str]~~ |
2022-11-14 22:35:07 +03:00
### MorphAnalysis.to_dict {id="morphanalysis-to_dict",tag="method"}
2020-08-17 17:45:24 +03:00
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]~~ |
2022-11-14 22:35:07 +03:00
### MorphAnalysis.from_id {id="morphanalysis-from_id",tag="classmethod"}
2020-08-17 17:45:24 +03:00
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~~ |