Package nltk :: Module featstruct :: Class FeatStruct
[hide private]
[frames] | no frames]

Class FeatStruct

source code

                   object --+    
                            |    
sem.logic.SubstituteBindingsI --+
                                |
                               FeatStruct
Known Subclasses:

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 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 reentrances.

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

Instance Methods [hide private]

Inherited from object: __delattr__, __getattribute__, __init__, __reduce__, __reduce_ex__, __setattr__, __str__

    Uniform Accessor Methods
 
_keys(self)
Return an iterable of the feature identifiers used by this FeatStruct.
source code
 
_values(self)
Return an iterable of the feature values directly defined by this FeatStruct.
source code
 
_items(self)
Return an iterable of (fid,fval) pairs, where fid is a feature identifier and fval is the corresponding feature value, for all features defined by this FeatStruct.
source code
    Equality & Hashing
 
equal_values(self, other, check_reentrance=False)
Returns: True if self and other assign the same value to to every feature.
source code
 
__eq__(self, other)
Return true if self and other are both feature structures, assign the same values to all features, and contain the same reentrances.
source code
 
__ne__(self, other)
Return true unless self and other are both feature structures, assign the same values to all features, and contain the same reentrances.
source code
 
__hash__(self)
If this feature structure is frozen, return its hash value; otherwise, raise TypeError.
source code
 
_equal(self, other, check_reentrance, visited_self, visited_other, visited_pairs)
Returns: True iff self and other have equal values.
source code
 
_hash(self, visited)
Returns: A hash value for this feature structure.
source code
    Freezing
 
freeze(self)
Make this feature structure, and any feature structures it contains, immutable.
source code
 
frozen(self)
Returns: True if this feature structure is immutable.
source code
 
_freeze(self, visited)
Make this feature structure, and any feature structure it contains, immutable.
source code
    Copying
 
copy(self, deep=True)
Return a new copy of self.
source code
 
__deepcopy__(self, memo) source code
    Structural Information
 
cyclic(self)
Returns: True if this feature structure contains itself.
source code
list of FeatStruct
reentrances(self)
Returns: A list of all feature structures that can be reached from self by multiple feature paths.
source code
 
walk(self)
Return an iterator that generates this feature structure, and each feature structure it contains.
source code
 
_walk(self, visited) source code
 
_find_reentrances(self, reentrances)
Return a dictionary that maps from the id of each feature structure contained in self (including self) to a boolean value, indicating whether it is reentrant or not.
source code
    Variables & Bindings
(any)
substitute_bindings(self, bindings)
Returns: The object that is obtained by replacing each variable bound by bindings with its values.
source code
 
retract_bindings(self, bindings) source code
 
variables(self)
Returns: A list of all variables in this object.
source code
 
rename_variables(self, vars=None, used_vars=(), new_vars=None) source code
FeatStruct
remove_variables(self)
Returns: The feature structure that is obtained by deleting all features whose values are Variables.
source code
    Unification
 
unify(self, other, bindings=None, trace=False, fail=None, rename_vars=True) source code
 
subsumes(self, other)
Returns: True if self subsumes other.
source code
    String Representations
 
__repr__(self)
Display a single-line representation of this feature structure, suitable for embedding in other representations.
source code
 
_repr(self, reentrances, reentrance_ids)
Returns: A string representation of this feature structure.
source code
Static Methods [hide private]
    Constructor
a new object with type S, a subtype of T
__new__(cls, features=None, **morefeatures)
Construct and return a new feature structure.
Class Variables [hide private]
    Freezing
  _FROZEN_ERROR = 'Frozen FeatStructs may not be modified.'
Error message used by mutating methods when called on a frozen feature structure.
Instance Variables [hide private]
  _frozen = False
A flag indicating whether this feature structure is frozen or not.
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__new__(cls, features=None, **morefeatures)
Static Method

 

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) -> FeatStructParser().parse(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.
Returns: a new object with type S, a subtype of T
Overrides: object.__new__

equal_values(self, other, check_reentrance=False)

source code 
Parameters:
  • check_reentrance - If true, then also return false if there is any difference between the reentrances of self and other.
Returns:
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).

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

__eq__(self, other)
(Equality operator)

source code 

Return true if self and other are both feature structures, assign the same values to all features, and contain the same reentrances. I.e., return self.equal_values(other, check_reentrance=True).

See Also: equal_values()

__ne__(self, other)

source code 

Return true unless self and other are both feature structures, assign the same values to all features, and contain the same reentrances. I.e., return not self.equal_values(other, check_reentrance=True).

__hash__(self)
(Hashing function)

source code 

If this feature structure is frozen, return its hash value; otherwise, raise TypeError.

Overrides: object.__hash__

_equal(self, other, check_reentrance, visited_self, visited_other, visited_pairs)

source code 
Parameters:
  • visited_self - A set containing the ids of all self feature structures we've already visited.
  • visited_other - A set containing the ids of all other feature structures we've already visited.
  • visited_pairs - A set containing (selfid, otherid) pairs for all pairs of feature structures we've already visited.
Returns:
True iff self and other have equal values.

_hash(self, visited)

source code 
Parameters:
  • visited - A set containing the ids of all feature structures we've already visited while hashing.
Returns:
A hash value for this feature structure.

Requires: self must be frozen.

freeze(self)

source code 

Make this feature structure, and any feature structures it contains, immutable. Note: this method does not attempt to 'freeze' any feature values that are not FeatStructs; it is recommended that you use only immutable feature values.

frozen(self)

source code 
Returns:
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 mutale copies can be produced with the copy() method.

_freeze(self, visited)

source code 

Make this feature structure, and any feature structure it contains, immutable.

Parameters:
  • visited - A set containing the ids of all feature structures we've already visited while freezing.

copy(self, deep=True)

source code 

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(self)

source code 
Returns:
True if this feature structure contains itself.

reentrances(self)

source code 
Returns: list of FeatStruct
A list of all feature structures that can be reached from self by multiple feature paths.

walk(self)

source code 

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

substitute_bindings(self, bindings)

source code 
Returns: (any)
The object that is obtained by replacing each variable bound by bindings with its values. Aliases are already resolved. (maybe?)
Overrides: sem.logic.SubstituteBindingsI.substitute_bindings

retract_bindings(self, bindings)

source code 

variables(self)

source code 
Returns:
A list of all variables in this object.
Overrides: sem.logic.SubstituteBindingsI.variables

rename_variables(self, vars=None, used_vars=(), new_vars=None)

source code 

remove_variables(self)

source code 
Returns: FeatStruct
The feature structure that is obtained by deleting all features whose values are Variables.

subsumes(self, other)

source code 
Returns:
True if self subsumes other. I.e., return true if unifying self with other would result in a feature structure equal to other.

__repr__(self)
(Representation operator)

source code 

Display a single-line representation of this feature structure, suitable for embedding in other representations.

Overrides: object.__repr__

_repr(self, reentrances, reentrance_ids)

source code 
Parameters:
  • reentrances - A dictionary that maps from the id of each feature value in self, indicating whether that value is reentrant or not.
  • reentrance_ids - A dictionary mapping from the ids of feature values to unique identifiers. This is modified by repr: the first time a reentrant feature value is displayed, an identifier is added to reentrance_ids for it.
Returns:
A string representation of this feature structure.

Instance Variable Details [hide private]

_frozen

A flag indicating whether this feature structure is frozen or not. Once this flag is set, it should never be un-set; and no further modification should be made to this feature structue.
Value:
False