Last active
January 9, 2019 10:26
-
-
Save Borda/64b531ea8dd7f7584ae1f6311de6ab68 to your computer and use it in GitHub Desktop.
jirkify
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 string | |
import random | |
__version__ = '0.1.1j' | |
DEFAULT = 0.5 | |
def jirkify(message, jirkaness=DEFAULT): | |
""" traslation to spoken/written form | |
:param str message: in mind | |
:param float jirkaness: mind state | |
:return str: spoken | |
Example: | |
-------- | |
>>> random.seed(0) | |
>>> jirkify("Welcome to PyJirka %s" % __version__) | |
'Weleomc tz PyairkJ 0.1.1j' | |
>>> jirkify("") | |
""" | |
new_message = [] | |
while ' ' in message: | |
message = message.replace(' ', ' ') | |
for word in message.split(' '): | |
outcome = random.random() | |
if outcome <= jirkaness: | |
ix = random.choice(range(len(word))) | |
word = ''.join([w if i != ix else random.choice(string.ascii_letters) | |
for i, w in enumerate(word)]) | |
elif len(word) > 2: | |
_rand = lambda word: random.randint(0, len(word) - 1) | |
i, j = _rand(word), _rand(word) | |
word = list(word) | |
word[i], word[j] = word[j], word[i] | |
word = ''.join(word) | |
new_message.append(word) | |
return ' '.join([w for w in new_message]) | |
if __name__ == '__main__': | |
print(jirkify("Welcome to PyJirka %s" % __version__)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment