Created
May 31, 2022 14:18
-
-
Save KeironO/449fda325974e20eab19ee6382fd1a2c to your computer and use it in GitHub Desktop.
Misspell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import string | |
def misspell(phrase, p): | |
new_phrase = [] | |
words = phrase.split(' ') | |
for word in words: | |
outcome = random.random() | |
if outcome <= p: | |
ix = random.choice(range(len(word))) | |
new_word = ''.join([word[w] if w != ix else random.choice(string.ascii_letters) for w in range(len(word))]) | |
new_phrase.append(new_word) | |
else: | |
new_phrase.append(word) | |
new_phrase = ' '.join([w for w in new_phrase]) | |
return new_phrase |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment