website/docs/api/morphology.mdx
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 for the
container storing a single morphological analysis.
Create a Morphology object.
Example
pythonfrom spacy.morphology import Morphology morphology = Morphology(strings)
| Name | Description |
|---|---|
strings | The string store. |
Insert a morphological analysis in the morphology table, if not already present. The morphological analysis may be provided in the Universal Dependencies FEATS format as a string or in the tag map dictionary format. Returns the hash of the new analysis.
Example
pythonfeats = "Feat1=Val1|Feat2=Val2" hash = nlp.vocab.morphology.add(feats) assert hash == nlp.vocab.strings[feats]
| Name | Description |
|---|---|
features | The morphological features. |
Example
pythonfeats = "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.
| Name | Description |
|---|---|
morph | The hash of the morphological analysis. |
Convert a string FEATS representation to a dictionary of features and values in the same format as the tag map.
Example
pythonfrom 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 format. |
| RETURNS | The morphological features as a dictionary. |
Convert a dictionary of features and values to a string FEATS representation.
Example
pythonfrom 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. |
| RETURNS | The morphological features in Universal Dependencies FEATS format. |
| Name | Description |
|---|---|
FEATURE_SEP | The FEATS feature separator. Default is |. |
FIELD_SEP | The FEATS field separator. Default is =. |
VALUE_SEP | The FEATS value separator. Default is ,. |
Stores a single morphological analysis.
Initialize a MorphAnalysis object from a Universal Dependencies FEATS string or a dictionary of morphological features.
Example
pythonfrom spacy.tokens import MorphAnalysis feats = "Feat1=Val1|Feat2=Val2" m = MorphAnalysis(nlp.vocab, feats)
| Name | Description |
|---|---|
vocab | The vocab. |
features | The morphological features. |
Whether a feature/value pair is in the analysis.
Example
pythonfeats = "Feat1=Val1,Val2|Feat2=Val2" morph = MorphAnalysis(nlp.vocab, feats) assert "Feat1=Val1" in morph
| Name | Description |
|---|---|
feature | A feature/value pair. |
| RETURNS | Whether the feature/value pair is contained in the analysis. |
Iterate over the feature/value pairs in the analysis.
Example
pythonfeats = "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. |
Returns the number of features in the analysis.
Example
pythonfeats = "Feat1=Val1,Val2|Feat2=Val2" morph = MorphAnalysis(nlp.vocab, feats) assert len(morph) == 3
| Name | Description |
|---|---|
| RETURNS | The number of features in the analysis. |
Returns the morphological analysis in the Universal Dependencies FEATS string format.
Example
pythonfeats = "Feat1=Val1,Val2|Feat2=Val2" morph = MorphAnalysis(nlp.vocab, feats) assert str(morph) == feats
| Name | Description |
|---|---|
| RETURNS | The analysis in the Universal Dependencies FEATS format. |
Retrieve values for a feature by field.
Example
pythonfeats = "Feat1=Val1,Val2" morph = MorphAnalysis(nlp.vocab, feats) assert morph.get("Feat1") == ["Val1", "Val2"]
| Name | Description |
|---|---|
field | The field to retrieve. |
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 []. |
| RETURNS | A list of the individual features. |
Produce a dict representation of the analysis, in the same format as the tag map.
Example
pythonfeats = "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. |
Create a morphological analysis from a given hash ID.
Example
pythonfeats = "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. |
key | The hash of the features string. |