parlai/core/utils.py
def flatten(teacher, context_length=-1, include_labels=True):
    """Return a flattened version of a teacher's data where all episodes only
    have length one but contain the desired amount of context.

    If context_length is not None, will use only that many past utterances.
    Default is None. Setting it to one only uses the input text.

    If include_labels is True, will include a random label in past utterances.
    Default is True.
    """
    data = []
    current = []
    episode_done = False
    context_length = context_length if context_length >= 0 else None
    context = deque(maxlen=context_length)
    try:
        while not teacher.epoch_done():
            # collect examples in episode
            while not episode_done:
                action = teacher.act()
                current.append(action)
                episode_done = action['episode_done']

            # build separate episodes from each example
            for ex in current:
                context.append(ex.get('text', ''))
                if len(context) > 1:
                    ex['text'] = '\n'.join(context)
                ex['episode_done'] = True
                if include_labels:
                    # add labels to context
                    labels = ex.get('labels', ex.get('eval_labels'))
                    if labels is not None:
                        context.append(random.choice(labels))
                data.append(ex)
            # reset flags and content
            episode_done = False
            current.clear()
            context.clear()
        return data
    except MemoryError as ex:
        raise MemoryError('Ran out of memory building flattened data batches. '
                          'Try using --context-length set to a small value to '
                          'limit the length of each flattened example, '
                          'disabling batch sorting / flattening by setting '
                          '--batch-sort false, or switching to data streaming '
                          'using --datatype {type}:stream to read from disk '
                          'if it is supported for your dataset.')
Return a flattened version of a teacher's data where all episodes only
    have length one but contain the desired amount of context.

    If context_length is not None, will use only that many past utterances.
    Default is None. Setting it to one only uses the input text.

    If include_labels is True, will include a random label in past utterances.
    Default is True.
    
Return a flattened version of a teacher's data where all episodes only
have length one but contain the desired amount of context.

If context_length is not None, will use only that many past utterances.
Default is None. Setting it to one only uses the input text.

If include_labels is True, will include a random label in past utterances.
Default is True.
Return a flattened version of a teacher ' s data where all episodes only have length one but contain the desired amount of context . If context_length is not None will use only that many past utterances . Default is None . Setting it to one only uses the input text . If include_labels is True will include a random label in past utterances . Default is True .
def flatten(teacher, context_length=-1, include_labels=True):
    data = []
    current = []
    episode_done = False
    context_length = context_length if context_length >= 0 else None
    context = deque(maxlen=context_length)
    try:
        while not teacher.epoch_done():
            while not episode_done:
                action = teacher.act()
                current.append(action)
                episode_done = action['episode_done']
            for ex in current:
                context.append(ex.get('text', ''))
                if len(context) > 1:
                    ex['text'] = '\n'.join(context)
                ex['episode_done'] = True
                if include_labels:
                    labels = ex.get('labels', ex.get('eval_labels'))
                    if labels is not None:
                        context.append(random.choice(labels))
                data.append(ex)
            episode_done = False
            current.clear()
            context.clear()
        return data
    except MemoryError as ex:
        raise MemoryError('Ran out of memory building flattened data batches. '
                          'Try using --context-length set to a small value to '
                          'limit the length of each flattened example, '
                          'disabling batch sorting / flattening by setting '
                          '--batch-sort false, or switching to data streaming '
                          'using --datatype {type}:stream to read from disk '
                          'if it is supported for your dataset.')
def flatten ( teacher , context_length = - 1 , include_labels = True ) : data = [ ] current = [ ] episode_done = False context_length = context_length if context_length >= 0 else None context = deque ( maxlen = context_length ) try : while not teacher . epoch_done ( ) : while not episode_done : action = teacher . act ( ) current . append ( action ) episode_done = action [ 'episode_done' ] for ex in current : context . append ( ex . get ( 'text' , '' ) ) if len ( context ) > 1 : ex [ 'text' ] = '\n' . join ( context ) ex [ 'episode_done' ] = True if include_labels : labels = ex . get ( 'labels' , ex . get ( 'eval_labels' ) ) if labels is not None : context . append ( random . choice ( labels ) ) data . append ( ex ) episode_done = False current . clear ( ) context . clear ( ) return data except MemoryError as ex : raise MemoryError ( 'Ran out of memory building flattened data batches. ' 'Try using --context-length set to a small value to ' 'limit the length of each flattened example, ' 'disabling batch sorting / flattening by setting ' '--batch-sort false, or switching to data streaming ' 'using --datatype {type}:stream to read from disk ' 'if it is supported for your dataset.' )
Return a flattened version of a teacher's data where all episodes only
have length one but contain the desired amount of context.
Return a flattened version of a teacher ' s data where all episodes only have length one but contain the desired amount of context .
"""Return a flattened version of a teacher's data where all episodes only
    have length one but contain the desired amount of context.

    If context_length is not None, will use only that many past utterances.
    Default is None. Setting it to one only uses the input text.

    If include_labels is True, will include a random label in past utterances.
    Default is True.
    """ # collect examples in episode # build separate episodes from each example # add labels to context # reset flags and content
teacher
context_length
include_labels