import torch


def tokens_to_lang(tokens, vocab, skip_tokens=None, join=True):
    """
    convert tokens into human-readable words
    """
    if skip_tokens is None:
        skip_tokens = {}

    def _tokens_to_lang(seq):
        if isinstance(seq, torch.Tensor):
            seq = seq.tolist()
        lang = [vocab.index2word(t) for t in seq if t not in skip_tokens]
        lang = " ".join(lang) if join else lang
        return lang

    if isinstance(tokens[0], int):
        # a list of ints is provided, only one sequence
        output = _tokens_to_lang(tokens)
    else:
        # a list of lists is provided, several sequences
        output = [_tokens_to_lang(seq) for seq in tokens]
    return output