def sanity_check_iob(naive_tokens, tag_texts):
    """
    Check if the IOB tags are valid.

    * Args:
        naive_tokens: tokens split by .split()
        tag_texts: list of tags in IOB format
    """
    def prefix(tag):
        if tag == "O":
            return tag
        return tag.split("-")[0]

    def body(tag):
        if tag == "O":
            return None
        return tag.split("-")[1]

    # same number check
    assert len(naive_tokens) == len(tag_texts), \
        f"""Number of tokens and tags doest not match.
        original tokens: {naive_tokens}
        tags: {tag_texts}"""

    # IOB format check
    prev_tag = None
    for tag_text in tag_texts:
        curr_tag = tag_text

        if prev_tag is None:  # first tag
            assert prefix(curr_tag) in ["B", "O"], \
                f"""Wrong tag: first tag starts with I.
                tag: {curr_tag}"""""

        else:  # following tags
            if prefix(prev_tag) in ["B", "I"]:
                assert (
                        (prefix(curr_tag) == "I" and body(curr_tag) == body(prev_tag))
                        or (prefix(curr_tag) == "B")
                        or (prefix(curr_tag) == "O")
                ), f"""Wrong tag: following tag mismatch.
                    previous tag: {prev_tag}
                    current tag: {curr_tag}"""

            elif prefix(prev_tag) in ["O"]:
                assert prefix(curr_tag) in ["B", "O"], \
                    f"""Wrong tag: following tag mismatch.
                    previous tag: {prev_tag}
                    current tag: {curr_tag}"""
            else:
                raise RuntimeError(f"Encountered unknown tag: {prev_tag}.")

        prev_tag = curr_tag