(
    aug_toks: List[types.AugTok],
    *,
    num: int | float = 1,
    lang: Optional[str] = None,
)
return new_aug_toks
Randomly substitute a single character in randomly-selected words with another,
    up to ``num`` times or with a probability of ``num``.

    Args:
        aug_toks: Sequence of tokens to augment through character substitution.
        num: If int, maximum number of words to modify with a random character substitution;
            if float, probability that a given word will be modified.
        lang: Standard, two-letter language code corresponding to ``aug_toks``.
            Used to load a weighted distribution of language-appropriate characters
            that are randomly selected for substitution. More common characters
            are more likely to be substituted. If not specified, ascii letters and
            digits are randomly selected with equal probability.

    Returns:
        New, augmented sequence of tokens.

    Note:
        This transform requires :class:`textacy.datasets.UDHR` to be downloaded
        to work properly, since this is the data source for character weights when
        deciding which char(s) to insert.
Randomly substitute a single character in randomly-selected words with another,
    up to ``num`` times or with a probability of ``num``.
Randomly substitute a single character in randomly - selected words with another up to num times or with a probability of num .
def substitute_chars(
    aug_toks: List[types.AugTok],
    *,
    num: int | float = 1,
    lang: Optional[str] = None,
) -> List[types.AugTok]:
    """
    Randomly substitute a single character in randomly-selected words with another,
    up to ``num`` times or with a probability of ``num``.

    Args:
        aug_toks: Sequence of tokens to augment through character substitution.
        num: If int, maximum number of words to modify with a random character substitution;
            if float, probability that a given word will be modified.
        lang: Standard, two-letter language code corresponding to ``aug_toks``.
            Used to load a weighted distribution of language-appropriate characters
            that are randomly selected for substitution. More common characters
            are more likely to be substituted. If not specified, ascii letters and
            digits are randomly selected with equal probability.

    Returns:
        New, augmented sequence of tokens.

    Note:
        This transform requires :class:`textacy.datasets.UDHR` to be downloaded
        to work properly, since this is the data source for character weights when
        deciding which char(s) to insert.
    """
    _validate_aug_toks(aug_toks)
    char_weights = aug_utils.get_char_weights(lang or "xx")
    cand_idxs = [
        idx
        for idx, aug_tok in enumerate(aug_toks)
        if aug_tok.is_word and len(aug_tok.text) >= 3
    ]
    rand_idxs = set(_select_random_candidates(cand_idxs, num))
    if not rand_idxs:
        return aug_toks[:]

    rand_chars = iter(
        random.choices(
            [char for char, _ in char_weights],
            weights=[weight for _, weight in char_weights],
            k=len(rand_idxs),
        )
    )
    new_aug_toks = []
    for idx, aug_tok in enumerate(aug_toks):
        if idx in rand_idxs:
            text_list = list(aug_tok.text)
            rand_char_idx = random.choice(range(len(text_list)))
            text_list[rand_char_idx] = next(rand_chars)
            new_aug_toks.append(
                types.AugTok(
                    text="".join(text_list),
                    ws=aug_tok.ws,
                    pos=aug_tok.pos,
                    is_word=aug_tok.is_word,
                    syns=aug_tok.syns,
                )
            )
        else:
            new_aug_toks.append(aug_tok)
    return new_aug_toks
def substitute_chars ( aug_toks : List [ types . AugTok ] , * , num : int | float = 1 , lang : Optional [ str ] = None , ) -> List [ types . AugTok ] : _validate_aug_toks ( aug_toks ) char_weights = aug_utils . get_char_weights ( lang or "xx" ) cand_idxs = [ idx for idx , aug_tok in enumerate ( aug_toks ) if aug_tok . is_word and len ( aug_tok . text ) >= 3 ] rand_idxs = set ( _select_random_candidates ( cand_idxs , num ) ) if not rand_idxs : return aug_toks [ : ] rand_chars = iter ( random . choices ( [ char for char , _ in char_weights ] , weights = [ weight for _ , weight in char_weights ] , k = len ( rand_idxs ) , ) ) new_aug_toks = [ ] for idx , aug_tok in enumerate ( aug_toks ) : if idx in rand_idxs : text_list = list ( aug_tok . text ) rand_char_idx = random . choice ( range ( len ( text_list ) ) ) text_list [ rand_char_idx ] = next ( rand_chars ) new_aug_toks . append ( types . AugTok ( text = "" . join ( text_list ) , ws = aug_tok . ws , pos = aug_tok . pos , is_word = aug_tok . is_word , syns = aug_tok . syns , ) ) else : new_aug_toks . append ( aug_tok ) return new_aug_toks