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 theFeature
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 tofstruct[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 theFeatList
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, thenmorefeatures
provides additional features for theFeatDict
constructor.
- equal_values(other, check_reentrance=False)[source]¶
Return True if
self
andother
assign the same value to to every feature. In particular, return true ifself[p]==other[p]
for every feature path p such thatself[p]
orother[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
andother
.- Note
the
==
is equivalent toequal_values()
withcheck_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 thecopy()
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.
- walk()[source]¶
Return an iterator that generates this feature structure, and each feature structure it contains. Each feature structure will be generated exactly once.
- rename_variables(vars=None, used_vars=(), new_vars=None)[source]¶
- See
nltk.featstruct.rename_variables()