import itertools


def _is_single(obj):
    """Check whether `obj` is a single document or an entire corpus.

    Parameters
    ----------
    obj : object

    Return
    ------
    (bool, object)
        2-tuple ``(is_single_document, new_obj)`` tuple, where `new_obj`
        yields the same sequence as the original `obj`.

    Notes
    -----
    `obj` is a single document if it is an iterable of strings. It is a corpus if it is an iterable of documents.

    """
    obj_iter = iter(obj)
    temp_iter = obj_iter
    try:
        peek = next(obj_iter)
        obj_iter = itertools.chain([peek], obj_iter)
    except StopIteration:
        # An empty object is interpreted as a single document (not a corpus).
        return True, obj
    if isinstance(peek, str):
        # First item is a string => obj is a single document for sure.
        return True, obj_iter
    if temp_iter is obj:
        # An iterator / generator => interpret input as a corpus.
        return False, obj_iter
    # If the first item isn't a string, assume obj is an iterable corpus.
    return False, obj