(
    doclike: types.DocLike,
    known_acro_defs: Optional[Dict[str, str]] = None,
)
return dict(acro_defs)
Extract a collection of acronyms and their most likely definitions, if available,
    from a spacy-parsed doc. If multiple definitions are found for a given acronym,
    only the most frequently occurring definition is returned.

    Args:
        doclike
        known_acro_defs: If certain acronym/definition pairs
            are known, pass them in as {acronym (str): definition (str)};
            algorithm will not attempt to find new definitions

    Returns:
        Unique acronyms (keys) with matched definitions (values)

    References:
        Taghva, Kazem, and Jeff Gilbreth. "Recognizing acronyms and their definitions."
        International Journal on Document Analysis and Recognition 1.4 (1999): 191-198.
Extract a collection of acronyms and their most likely definitions, if available,
    from a spacy-parsed doc. If multiple definitions are found for a given acronym,
    only the most frequently occurring definition is returned.
Extract a collection of acronyms and their most likely definitions if available from a spacy - parsed doc . If multiple definitions are found for a given acronym only the most frequently occurring definition is returned .
def acronyms_and_definitions(
    doclike: types.DocLike,
    known_acro_defs: Optional[Dict[str, str]] = None,
) -> Dict[str, List[str]]:
    """
    Extract a collection of acronyms and their most likely definitions, if available,
    from a spacy-parsed doc. If multiple definitions are found for a given acronym,
    only the most frequently occurring definition is returned.

    Args:
        doclike
        known_acro_defs: If certain acronym/definition pairs
            are known, pass them in as {acronym (str): definition (str)};
            algorithm will not attempt to find new definitions

    Returns:
        Unique acronyms (keys) with matched definitions (values)

    References:
        Taghva, Kazem, and Jeff Gilbreth. "Recognizing acronyms and their definitions."
        International Journal on Document Analysis and Recognition 1.4 (1999): 191-198.
    """
    # process function arguments
    acro_defs: Dict[str, List[Tuple[str, float]]] = collections.defaultdict(list)
    if not known_acro_defs:
        known_acronyms = set()
    else:
        for acro, def_ in known_acro_defs.items():
            acro_defs[acro] = [(def_, 1.0)]
        known_acronyms = set(acro_defs.keys())

    if isinstance(doclike, Span):
        sents = [doclike]
    else:  # spacy.Doc
        sents = doclike.sents

    # iterate over sentences and their tokens
    for sent in sents:
        max_ind = len(sent) - 1

        for i, token in enumerate(sent):

            token_ = token.text
            if token_ in known_acronyms or is_acronym(token_) is False:
                continue

            # define definition search window(s)
            window_size = min(2 * len(token_), len(token_) + 5)
            windows = [
                sent[max(i - window_size, 0) : i],
                sent[min(i + 1, max_ind) : min(i + window_size + 1, max_ind)],
            ]
            # if candidate inside (X) or -X-, only look in pre-window
            if 0 < i < max_ind:
                adjacent_tokens = sent[i - 1].text + sent[i + 1].text
                if adjacent_tokens in {"()", "--", "––"}:
                    windows.pop()

            # iterate over possible windows
            # filtering for valid definition strings
            for window in windows:
                window_ = window.text
                # window text can't be all uppercase
                if window_.isupper():
                    continue
                # window can't contain separating punctuation
                if "!" in window_ or "?" in window_ or ":" in window_ or ";" in window_:
                    continue
                # acronym definition can't contain itself: no ouroboros!
                if token_ in window_:
                    continue
                # window must contain at least one character used in acronym
                if not any(char in window_ for char in token_):
                    continue
                definition, confidence = _get_acronym_definition(
                    token_, window, threshold=0.8
                )
                if definition:
                    acro_defs[token_].append((definition, confidence))

            if not acro_defs.get(token_):
                acro_defs[token_].append(("", 0.0))

    # vote by confidence score in the case of multiple definitions
    for acro, defs in acro_defs.items():
        if len(defs) == 1:
            acro_defs[acro] = defs[0][0]
        else:
            acro_defs[acro] = sorted(defs, key=itemgetter(1), reverse=True)[0][0]

    return dict(acro_defs)
def acronyms_and_definitions ( doclike : types . DocLike , known_acro_defs : Optional [ Dict [ str , str ] ] = None , ) -> Dict [ str , List [ str ] ] : # process function arguments acro_defs : Dict [ str , List [ Tuple [ str , float ] ] ] = collections . defaultdict ( list ) if not known_acro_defs : known_acronyms = set ( ) else : for acro , def_ in known_acro_defs . items ( ) : acro_defs [ acro ] = [ ( def_ , 1.0 ) ] known_acronyms = set ( acro_defs . keys ( ) ) if isinstance ( doclike , Span ) : sents = [ doclike ] else : # spacy.Doc sents = doclike . sents # iterate over sentences and their tokens for sent in sents : max_ind = len ( sent ) - 1 for i , token in enumerate ( sent ) : token_ = token . text if token_ in known_acronyms or is_acronym ( token_ ) is False : continue # define definition search window(s) window_size = min ( 2 * len ( token_ ) , len ( token_ ) + 5 ) windows = [ sent [ max ( i - window_size , 0 ) : i ] , sent [ min ( i + 1 , max_ind ) : min ( i + window_size + 1 , max_ind ) ] , ] # if candidate inside (X) or -X-, only look in pre-window if 0 < i < max_ind : adjacent_tokens = sent [ i - 1 ] . text + sent [ i + 1 ] . text if adjacent_tokens in { "()" , "--" , "––"}: windows . pop ( ) # iterate over possible windows # filtering for valid definition strings for window in windows : window_ = window . text # window text can't be all uppercase if window_ . isupper ( ) : continue # window can't contain separating punctuation if "!" in window_ or "?" in window_ or ":" in window_ or ";" in window_ : continue # acronym definition can't contain itself: no ouroboros! if token_ in window_ : continue # window must contain at least one character used in acronym if not any ( char in window_ for char in token_ ) : continue definition , confidence = _get_acronym_definition ( token_ , window , threshold = 0.8 ) if definition : acro_defs [ token_ ] . append ( ( definition , confidence ) ) if not acro_defs . get ( token_ ) : acro_defs [ token_ ] . append ( ( "" , 0.0 ) ) # vote by confidence score in the case of multiple definitions for acro , defs in acro_defs . items ( ) : if len ( defs ) == 1 : acro_defs [ acro ] = defs [ 0 ] [ 0 ] else : acro_defs [ acro ] = sorted ( defs , key = itemgetter ( 1 ) , reverse = True ) [ 0 ] [ 0 ] return dict ( acro_defs )