(
    aug_toks: List[types.AugTok],
    *,
    num: int | float = 1,
    pos: Optional[str | Set[str]] = None,
)
return new_aug_toks
Randomly insert random synonyms of tokens for which synonyms are available,
    up to ``num`` times or with a probability of ``num``.

    Args:
        aug_toks: Sequence of tokens to augment through synonym insertion.
        num: If int, maximum number of words with available synonyms
            from which a random synonym is selected and randomly inserted; if float,
            probability that a given word with synonyms will provide a synonym
            to be inserted.
        pos: Part of speech tag(s) of words to be considered for augmentation.
            If None, all words with synonyms are considered.

    Returns:
        New, augmented sequence of tokens.

    Note:
        This transform requires :class:`textacy.resources.ConceptNet` to be downloaded
        to work properly, since this is the data source for word synonyms to be inserted.
Randomly insert random synonyms of tokens for which synonyms are available,
    up to ``num`` times or with a probability of ``num``.
Randomly insert random synonyms of tokens for which synonyms are available up to num times or with a probability of num .
def insert_word_synonyms(
    aug_toks: List[types.AugTok],
    *,
    num: int | float = 1,
    pos: Optional[str | Set[str]] = None,
) -> List[types.AugTok]:
    """
    Randomly insert random synonyms of tokens for which synonyms are available,
    up to ``num`` times or with a probability of ``num``.

    Args:
        aug_toks: Sequence of tokens to augment through synonym insertion.
        num: If int, maximum number of words with available synonyms
            from which a random synonym is selected and randomly inserted; if float,
            probability that a given word with synonyms will provide a synonym
            to be inserted.
        pos: Part of speech tag(s) of words to be considered for augmentation.
            If None, all words with synonyms are considered.

    Returns:
        New, augmented sequence of tokens.

    Note:
        This transform requires :class:`textacy.resources.ConceptNet` to be downloaded
        to work properly, since this is the data source for word synonyms to be inserted.
    """
    _validate_aug_toks(aug_toks)
    pos = utils.to_collection(pos, str, set)
    # bail out on very short sentences to avoid clobbering meaning
    if len(aug_toks) < 3:
        return aug_toks[:]

    cand_aug_toks = [
        aug_tok
        for aug_tok in aug_toks
        if aug_tok.syns and (pos is None or aug_tok.pos in pos)
    ]
    rand_aug_toks = _select_random_candidates(cand_aug_toks, num)
    rand_idxs = random.sample(range(len(aug_toks)), len(rand_aug_toks))
    if not rand_idxs:
        return aug_toks[:]

    rand_aug_toks = iter(rand_aug_toks)
    new_aug_toks: List[types.AugTok] = []
    # NOTE: https://github.com/python/mypy/issues/5492
    padded_pairs = itertoolz.sliding_window(2, [None] + aug_toks)  # type: ignore
    for idx, (prev_tok, curr_tok) in enumerate(padded_pairs):
        if idx in rand_idxs:
            rand_aug_tok = next(rand_aug_toks)
            if prev_tok:
                # use previous token's whitespace for inserted synonym
                new_tok_ws = prev_tok.ws
                if prev_tok.is_word and not prev_tok.ws:
                    # previous token should have whitespace, if a word
                    new_aug_toks[-1] = types.AugTok(
                        text=prev_tok.text,
                        ws=" ",
                        pos=prev_tok.pos,
                        is_word=True,
                        syns=prev_tok.syns,
                    )
            else:
                new_tok_ws = " "
            new_aug_toks.append(
                types.AugTok(
                    text=random.choice(rand_aug_tok.syns),
                    ws=new_tok_ws,
                    pos=rand_aug_tok.pos,
                    is_word=rand_aug_tok.is_word,
                    syns=rand_aug_tok.syns,  # TODO: re-fetch syns? use []?
                )
            )
        new_aug_toks.append(curr_tok)
    return new_aug_toks
def insert_word_synonyms ( aug_toks : List [ types . AugTok ] , * , num : int | float = 1 , pos : Optional [ str | Set [ str ] ] = None , ) -> List [ types . AugTok ] : _validate_aug_toks ( aug_toks ) pos = utils . to_collection ( pos , str , set ) # bail out on very short sentences to avoid clobbering meaning if len ( aug_toks ) < 3 : return aug_toks [ : ] cand_aug_toks = [ aug_tok for aug_tok in aug_toks if aug_tok . syns and ( pos is None or aug_tok . pos in pos ) ] rand_aug_toks = _select_random_candidates ( cand_aug_toks , num ) rand_idxs = random . sample ( range ( len ( aug_toks ) ) , len ( rand_aug_toks ) ) if not rand_idxs : return aug_toks [ : ] rand_aug_toks = iter ( rand_aug_toks ) new_aug_toks : List [ types . AugTok ] = [ ] # NOTE: https://github.com/python/mypy/issues/5492 padded_pairs = itertoolz . sliding_window ( 2 , [ None ] + aug_toks ) # type: ignore for idx , ( prev_tok , curr_tok ) in enumerate ( padded_pairs ) : if idx in rand_idxs : rand_aug_tok = next ( rand_aug_toks ) if prev_tok : # use previous token's whitespace for inserted synonym new_tok_ws = prev_tok . ws if prev_tok . is_word and not prev_tok . ws : # previous token should have whitespace, if a word new_aug_toks [ - 1 ] = types . AugTok ( text = prev_tok . text , ws = " " , pos = prev_tok . pos , is_word = True , syns = prev_tok . syns , ) else : new_tok_ws = " " new_aug_toks . append ( types . AugTok ( text = random . choice ( rand_aug_tok . syns ) , ws = new_tok_ws , pos = rand_aug_tok . pos , is_word = rand_aug_tok . is_word , syns = rand_aug_tok . syns , # TODO: re-fetch syns? use []? ) ) new_aug_toks . append ( curr_tok ) return new_aug_toks