(tokens)
return named_entities
Creates a list of Entity named-tuples, storing the entity type and the start and end
    offsets of the entity.

    :param tokens: a list of tags
    :return: a list of Entity named-tuples
Creates a list of Entity named-tuples, storing the entity type and the start and end
    offsets of the entity.
Creates a list of Entity named - tuples storing the entity type and the start and end offsets of the entity .
def collect_named_entities(tokens):
    """
    Creates a list of Entity named-tuples, storing the entity type and the start and end
    offsets of the entity.

    :param tokens: a list of tags
    :return: a list of Entity named-tuples
    """

    named_entities = []
    start_offset = None
    end_offset = None
    ent_type = None

    for offset, token_tag in enumerate(tokens):

        if token_tag == 'O':
            if ent_type is not None and start_offset is not None:
                end_offset = offset - 1
                named_entities.append(Entity(ent_type, start_offset, end_offset))
                start_offset = None
                end_offset = None
                ent_type = None

        elif ent_type is None:
            ent_type = token_tag[2:]
            start_offset = offset

        elif ent_type != token_tag[2:] or (ent_type == token_tag[2:] and token_tag[:1] == 'B'):

            end_offset = offset - 1
            named_entities.append(Entity(ent_type, start_offset, end_offset))

            # start of a new entity
            ent_type = token_tag[2:]
            start_offset = offset
            end_offset = None

    # catches an entity that goes up until the last token

    if ent_type is not None and start_offset is not None and end_offset is None:
        named_entities.append(Entity(ent_type, start_offset, len(tokens)-1))

    return named_entities
def collect_named_entities ( tokens ) : named_entities = [ ] start_offset = None end_offset = None ent_type = None for offset , token_tag in enumerate ( tokens ) : if token_tag == 'O' : if ent_type is not None and start_offset is not None : end_offset = offset - 1 named_entities . append ( Entity ( ent_type , start_offset , end_offset ) ) start_offset = None end_offset = None ent_type = None elif ent_type is None : ent_type = token_tag [ 2 : ] start_offset = offset elif ent_type != token_tag [ 2 : ] or ( ent_type == token_tag [ 2 : ] and token_tag [ : 1 ] == 'B' ) : end_offset = offset - 1 named_entities . append ( Entity ( ent_type , start_offset , end_offset ) ) # start of a new entity ent_type = token_tag [ 2 : ] start_offset = offset end_offset = None # catches an entity that goes up until the last token if ent_type is not None and start_offset is not None and end_offset is None : named_entities . append ( Entity ( ent_type , start_offset , len ( tokens ) - 1 ) ) return named_entities