def tagseq_to_entityseq(tags: list) -> list:
    """
    Convert tags format:
    [ "B-LOC", "I-LOC", "O", B-PER"] -> [(0, 2, "LOC"), (3, 4, "PER")]
    """
    entity_seq = []
    tag_name = ""
    start, end = 0, 0
    for index, tag in enumerate(tags):
        if tag.startswith("B-"):
            if tag_name != "":
                end = index
                entity_seq.append((start, end, tag_name))
            tag_name = tag[2:]
            start = index
        elif tag.startswith("I-"):
            if tag_name == "" or tag_name == tag[2:]:
                continue
            else:
                end = index
                entity_seq.append((start, end, tag_name))
                tag_name = ""
        else:  # "O"
            if tag_name == "":
                continue
            else:
                end = index
                entity_seq.append((start, end, tag_name))
                tag_name = ""
    return entity_seq