def get_decoupled()

in pytext/data/decoupled_data.py [0:0]


def get_decoupled(tokens: List[str], filter_ood_slots: bool) -> List[str]:
    """
    Convert the seqlogical form to the decoupled form
    """
    if not tokens:
        return []
    if filter_ood_slots:
        if DecoupledUtils.is_ood_token(tokens[0]):
            # Out of domain sample
            return [tokens[0], "]"]

    decoupled = []
    # stack contains mutable tuples of
    # [index_of_opening_bracket: int, has_children: bool]
    stack = []
    for i, token in enumerate(tokens):
        if DecoupledUtils.is_intent_token(token) or DecoupledUtils.is_slot_token(token):
            # everything on stack now has children
            for tup in stack:
                tup[1] = True
            # add an opening bracket to stack
            stack.append([i, False])
            # add to decoupled form
            decoupled.append(tokens[i])
        elif token == "]":
            # no bracket to end
            if len(stack) == 0:
                raise ValueError(" ".join(tokens))
            idx, has_child = stack.pop()
            # don't keep tokens if it is an intent OR it has children
            if has_child or DecoupledUtils.is_intent_token(tokens[idx]):
                decoupled.append(token)
            else:
                # leaf level slot: keep all tokens
                decoupled.extend(tokens[idx + 1 : i + 1])
        else:
            # normal token outside of a bracket
            if len(stack) == 0:
                raise ValueError(" ".join(tokens))
    if len(stack) > 0:
        raise ValueError(" ".join(tokens))
    return decoupled