nltk.featstruct.FeatStruct

class nltk.featstruct.FeatStruct[source]

Bases: SubstituteBindingsI

A mapping from feature identifiers to feature values, where each feature value is either a basic value (such as a string or an integer), or a nested feature structure. There are two types of feature structure:

  • feature dictionaries, implemented by FeatDict, act like Python dictionaries. Feature identifiers may be strings or instances of the Feature class.

  • feature lists, implemented by FeatList, act like Python lists. Feature identifiers are integers.

Feature structures may be indexed using either simple feature identifiers or ‘feature paths.’ A feature path is a sequence of feature identifiers that stand for a corresponding sequence of indexing operations. In particular, fstruct[(f1,f2,...,fn)] is equivalent to fstruct[f1][f2]...[fn].

Feature structures may contain reentrant feature structures. A “reentrant feature structure” is a single feature structure object that can be accessed via multiple feature paths. Feature structures may also be cyclic. A feature structure is “cyclic” if there is any feature path from the feature structure to itself.

Two feature structures are considered equal if they assign the same values to all features, and have the same reentrancies.

By default, feature structures are mutable. They may be made immutable with the freeze() method. Once they have been frozen, they may be hashed, and thus used as dictionary keys.

static __new__(cls, features=None, **morefeatures)[source]

Construct and return a new feature structure. If this constructor is called directly, then the returned feature structure will be an instance of either the FeatDict class or the FeatList class.

Parameters
  • features

    The initial feature values for this feature structure:

    • FeatStruct(string) -> FeatStructReader().read(string)

    • FeatStruct(mapping) -> FeatDict(mapping)

    • FeatStruct(sequence) -> FeatList(sequence)

    • FeatStruct() -> FeatDict()

  • morefeatures – If features is a mapping or None, then morefeatures provides additional features for the FeatDict constructor.

equal_values(other, check_reentrance=False)[source]

Return True if self and other assign the same value to to every feature. In particular, return true if self[p]==other[p] for every feature path p such that self[p] or other[p] is a base value (i.e., not a nested feature structure).

Parameters

check_reentrance – If True, then also return False if there is any difference between the reentrances of self and other.

Note

the == is equivalent to equal_values() with check_reentrance=True.

freeze()[source]

Make this feature structure, and any feature structures it contains, immutable. Note: this method does not attempt to ‘freeze’ any feature value that is not a FeatStruct; it is recommended that you use only immutable feature values.

frozen()[source]

Return True if this feature structure is immutable. Feature structures can be made immutable with the freeze() method. Immutable feature structures may not be made mutable again, but new mutable copies can be produced with the copy() method.

copy(deep=True)[source]

Return a new copy of self. The new copy will not be frozen.

Parameters

deep – If true, create a deep copy; if false, create a shallow copy.

cyclic()[source]

Return True if this feature structure contains itself.

walk()[source]

Return an iterator that generates this feature structure, and each feature structure it contains. Each feature structure will be generated exactly once.

substitute_bindings(bindings)[source]
See

nltk.featstruct.substitute_bindings()

retract_bindings(bindings)[source]
See

nltk.featstruct.retract_bindings()

variables()[source]
See

nltk.featstruct.find_variables()

rename_variables(vars=None, used_vars=(), new_vars=None)[source]
See

nltk.featstruct.rename_variables()

remove_variables()[source]

Return the feature structure that is obtained by deleting any feature whose value is a Variable.

Return type

FeatStruct

unify(other, bindings=None, trace=False, fail=None, rename_vars=True)[source]
subsumes(other)[source]

Return True if self subsumes other. I.e., return true If unifying self with other would result in a feature structure equal to other.