(a_string, amount_of_noise)
return a_string
Add some artificial spelling mistakes to the string
Add some artificial spelling mistakes to the string
Add some artificial spelling mistakes to the string
def add_noise_to_string(a_string, amount_of_noise):
    """Add some artificial spelling mistakes to the string"""
    if rand() < amount_of_noise * len(a_string):
        # Replace a character with a random character
        random_char_position = random_randint(len(a_string))
        a_string = a_string[:random_char_position] + random_choice(CHARS[:-1]) + a_string[random_char_position + 1:]
    if rand() < amount_of_noise * len(a_string):
        # Delete a character
        random_char_position = random_randint(len(a_string))
        a_string = a_string[:random_char_position] + a_string[random_char_position + 1:]
    if len(a_string) < CONFIG.max_input_len and rand() < amount_of_noise * len(a_string):
        # Add a random character
        random_char_position = random_randint(len(a_string))
        a_string = a_string[:random_char_position] + random_choice(CHARS[:-1]) + a_string[random_char_position:]
    if rand() < amount_of_noise * len(a_string):
        # Transpose 2 characters
        random_char_position = random_randint(len(a_string) - 1)
        a_string = (a_string[:random_char_position] + a_string[random_char_position + 1] + a_string[random_char_position] +
                    a_string[random_char_position + 2:])
    return a_string
def add_noise_to_string ( a_string , amount_of_noise ) : if rand ( ) < amount_of_noise * len ( a_string ) : # Replace a character with a random character random_char_position = random_randint ( len ( a_string ) ) a_string = a_string [ : random_char_position ] + random_choice ( CHARS [ : - 1 ] ) + a_string [ random_char_position + 1 : ] if rand ( ) < amount_of_noise * len ( a_string ) : # Delete a character random_char_position = random_randint ( len ( a_string ) ) a_string = a_string [ : random_char_position ] + a_string [ random_char_position + 1 : ] if len ( a_string ) < CONFIG . max_input_len and rand ( ) < amount_of_noise * len ( a_string ) : # Add a random character random_char_position = random_randint ( len ( a_string ) ) a_string = a_string [ : random_char_position ] + random_choice ( CHARS [ : - 1 ] ) + a_string [ random_char_position : ] if rand ( ) < amount_of_noise * len ( a_string ) : # Transpose 2 characters random_char_position = random_randint ( len ( a_string ) - 1 ) a_string = ( a_string [ : random_char_position ] + a_string [ random_char_position + 1 ] + a_string [ random_char_position ] + a_string [ random_char_position + 2 : ] ) return a_string