class nltk.LazyMap(function, *lists, **config)[source]

A lazy sequence whose elements are formed by applying a given function to each element in one or more underlying lists. The function is applied lazily – i.e., when you read a value from the list, LazyMap will calculate that value by applying its function to the underlying lists’ value(s). LazyMap is essentially a lazy version of the Python primitive function map. In particular, the following two expressions are equivalent:

>>> from nltk.util import LazyMap
>>> function = str
>>> sequence = [1,2,3]
>>> map(function, sequence) 
['1', '2', '3']
>>> list(LazyMap(function, sequence))
['1', '2', '3']

Like the Python map primitive, if the source lists do not have equal size, then the value None will be supplied for the ‘missing’ elements.

Lazy maps can be useful for conserving memory, in cases where individual values take up a lot of space. This is especially true if the underlying list’s values are constructed lazily, as is the case with many corpus readers.

A typical example of a use case for this class is performing feature detection on the tokens in a corpus. Since featuresets are encoded as dictionaries, which can take up a lot of memory, using a LazyMap can significantly reduce memory usage when training and running classifiers.


__init__(function, *lists, **config)
param function:The function that should be applied to
count(value) Return the number of times this list contains value.
index(value[, start, stop]) Return the index of the first occurrence of value in this list that is greater than or equal to start and less than stop.
unicode_repr() Return a string representation for this corpus view that is similar to a list’s representation; but if it would be more than 60 characters long, it is truncated.