Skip to content

Instantly share code, notes, and snippets.

@KeironO
Created May 31, 2022 14:18
Show Gist options
  • Save KeironO/449fda325974e20eab19ee6382fd1a2c to your computer and use it in GitHub Desktop.
Save KeironO/449fda325974e20eab19ee6382fd1a2c to your computer and use it in GitHub Desktop.
Misspell
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