ner_utils.py 989 Bytes
from collections import defaultdict
from itertools import chain

from .constants import SEG_BEGIN, SEG_OUTSIDE
from .type_utils import TokenList, StrLabelList

type NE = list[tuple[int, str]] # (token_idx, token)

def get_nes(tokens: TokenList, ne_tags: StrLabelList) -> list[tuple[NE, str]]:
    nes = []
    curr_nes: defaultdict[str, NE] = defaultdict(list)
    for i, (token, tag) in enumerate(zip(tokens, ne_tags)):
        assert (tag is not None)
        if tag == SEG_OUTSIDE:
            continue
        iobs = tag.split('#')
        for iob in iobs:
            iob_type, ne_type = iob.split(':')
            if iob_type == SEG_BEGIN and curr_nes[ne_type]:
                nes.append((curr_nes[ne_type], ne_type))
                curr_nes[ne_type] = []
            curr_nes[ne_type].append((i, token))
    for ne_type, ne in curr_nes.items():
        if ne:
            nes.append((ne, ne_type))
    
    assert ((set(ne_tags) == {SEG_OUTSIDE}) == (not nes))
    
    return nes