def substitute()

in augly/text/augmenters/letter_replacement.py [0:0]


    def substitute(self, data: str) -> str:
        """
        Returns a text where random letters are replaced by the specified mapping

        @param data: the text where the letter substitution will be applied on
        """
        tokens = tokenize(data)
        aug_word_cnt = self._generate_aug_cnt(
            len(tokens), self.aug_word_min, self.aug_word_max, self.aug_word_p
        )
        filtered_word_idxes = self.skip_aug(self.pre_skip_aug(tokens), tokens)
        aug_word_idxes = set(
            get_aug_idxes(self, tokens, filtered_word_idxes, aug_word_cnt, Method.WORD)
        )

        for t_i, token in enumerate(tokens):
            if t_i not in aug_word_idxes:
                continue

            chars = list(token)
            aug_char_cnt = self._generate_aug_cnt(
                len(chars), self.aug_char_min, self.aug_char_max, self.aug_char_p
            )
            aug_char_idxes = (
                None
                if len(chars) < self.min_char
                else set(
                    get_aug_idxes(
                        self, chars, list(range(len(chars))), aug_char_cnt, Method.CHAR
                    )
                )
            )

            if not aug_char_idxes:
                continue

            for c_i, char in enumerate(chars):
                if c_i in aug_char_idxes:
                    chars[c_i] = self.sample(self.letter_mapping.replace(char), 1)[0]

            tokens[t_i] = "".join(chars)

        return detokenize(tokens)