Package nltk :: Module util :: Class LazyZip
[hide private]
[frames] | no frames]

Class LazyZip

source code

          object --+        
                   |        
AbstractLazySequence --+    
                       |    
                 LazyMap --+
                           |
                          LazyZip
Known Subclasses:

A lazy sequence whose elements are tuples, each containing the i-th element from each of the argument sequences. The returned list is truncated in length to the length of the shortest argument sequence. The tuples are constructed lazily -- i.e., when you read a value from the list, LazyZip will calculate that value by forming a tuple from the i-th element of each of the argument sequences.

LazyZip is essentially a lazy version of the Python primitive function zip. In particular, the following two expressions are equivalent:

>>> zip(sequences...)
>>> list(LazyZip(sequences...))

Lazy zips can be useful for conserving memory in cases where the argument sequences are particularly long.

A typical example of a use case for this class is combining long sequences of gold standard and predicted values in a classification or tagging task in order to calculate accuracy. By constructing tuples lazily and avoiding the creation of an additional long sequence, memory usage can be significantly reduced.

Instance Methods [hide private]
 
__init__(self, *lists)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
iterate_from(self, index)
Return an iterator that generates the tokens in the corpus file underlying this corpus view, starting at the token number start.
source code
 
__len__(self)
Return the number of tokens in the corpus file underlying this corpus view.
source code

Inherited from LazyMap: __getitem__

Inherited from AbstractLazySequence: __add__, __cmp__, __contains__, __hash__, __iter__, __mul__, __radd__, __repr__, __rmul__, count, index

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

Class Variables [hide private]

Inherited from AbstractLazySequence (private): _MAX_REPR_SIZE

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, *lists)
(Constructor)

source code 

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Parameters:
  • lists (list of list) - the underlying lists
Overrides: LazyMap.__init__

iterate_from(self, index)

source code 

Return an iterator that generates the tokens in the corpus file underlying this corpus view, starting at the token number start. If start>=len(self), then this iterator will generate no tokens.

Overrides: LazyMap.iterate_from

__len__(self)
(Length operator)

source code 

Return the number of tokens in the corpus file underlying this corpus view.

Overrides: LazyMap.__len__