Back to Spacy

Morphology

website/docs/api/morphology.mdx

4.0.0.dev1010.2 KB
Original Source

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.

Morphology.__init__ {id="init",tag="method"}

Create a Morphology object.

Example

python
from spacy.morphology import Morphology

morphology = Morphology(strings)
NameDescription
stringsThe 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 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]
NameDescription
featuresThe 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 string for the hash of the morphological analysis.

NameDescription
morphThe hash of the morphological analysis. int

Morphology.feats_to_dict {id="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"}
NameDescription
featsThe morphological features in Universal Dependencies FEATS format. str
RETURNSThe 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 representation.

Example

python
from spacy.morphology import Morphology
f = Morphology.dict_to_feats({"Feat1": "Val1", "Feat2": "Val2"})
assert f == "Feat1=Val1|Feat2=Val2"
NameDescription
feats_dictThe morphological features as a dictionary. Dict[str, str]
RETURNSThe morphological features in Universal Dependencies FEATS format. str

Attributes {id="attributes"}

NameDescription
FEATURE_SEPThe FEATS feature separator. Default is |. str
FIELD_SEPThe FEATS field separator. Default is =. str
VALUE_SEPThe FEATS 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 string or a dictionary of morphological features.

Example

python
from spacy.tokens import MorphAnalysis

feats = "Feat1=Val1|Feat2=Val2"
m = MorphAnalysis(nlp.vocab, feats)
NameDescription
vocabThe vocab. Vocab
featuresThe 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
NameDescription
featureA feature/value pair. str
RETURNSWhether 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"]
NameDescription
YIELDSA 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
NameDescription
RETURNSThe number of features in the analysis. int

MorphAnalysis.__str__ {id="morphanalysis-str",tag="method"}

Returns the morphological analysis in the Universal Dependencies FEATS string format.

Example

python
feats = "Feat1=Val1,Val2|Feat2=Val2"
morph = MorphAnalysis(nlp.vocab, feats)
assert str(morph) == feats
NameDescription
RETURNSThe analysis in the Universal Dependencies FEATS 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"]
NameDescription
fieldThe 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]]
RETURNSA 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"}
NameDescription
RETURNSThe 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
NameDescription
vocabThe vocab. Vocab
keyThe hash of the features string. int