def iob_scheme(tags, iobes=True):
    """ Transform tag sequence without any scheme into IOB or IOBES scheme """
    iob = []
    tags = ["O"] + tags + ["O"]

    for i in range(1, len(tags) - 1):
        prev_tag, tag, next_tag = tags[i - 1: i + 2]

        if not tag == "O":
            if tag not in [prev_tag, next_tag]:
                if iobes:
                    iob.append("S-" + tag)
                else:
                    iob.append("B-" + tag)
            elif not tag == prev_tag:
                iob.append("B-" + tag)
            elif not tag == next_tag:
                if iobes:
                    iob.append("E-" + tag)
                else:
                    iob.append("I-" + tag)
            else:
                iob.append("I-" + tag)
        else:
            iob.append("O")

    return iob