Created
January 13, 2014 01:12
-
-
Save philchristensen/8393105 to your computer and use it in GitHub Desktop.
Generate nonsense words out of integers
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
# Port of rufus-mnemo to Python | |
# https://github.com/jmettraux/rufus-mnemo | |
# Original copyright (c) 2007-2011, John Mettraux, [email protected] | |
consonants = list('bdghjkmnprstz') | |
vowels = list('aeiou') | |
syllables = [c + v for c in consonants for v in vowels] + ['wa', 'wo', 'ya', 'yo', 'yu'] | |
negative = 'wi' | |
replacements = [ | |
[ 'hu', 'fu' ], | |
[ 'si', 'shi' ], | |
[ 'ti', 'chi' ], | |
[ 'tu', 'tsu' ], | |
[ 'zi', 'tzu' ], | |
] | |
def encode(i): | |
""" | |
Convert an integer to a base-70 gibberish string. | |
""" | |
if i == 0: | |
return '' | |
mod = abs(i) % len(syllables) | |
rest = abs(i) / len(syllables) | |
result = ''.join([ | |
['', negative][i < 0], | |
encode(rest), | |
syllables[mod], | |
]) | |
for old, new in replacements: | |
result = result.replace(old, new) | |
return result | |
def decode(s): | |
""" | |
Convert a base-70 gibberish string back to an integer. | |
""" | |
if not s: | |
return 0 | |
if s.startswith(negative): | |
return -1 * decode(s[len(negative):]) | |
for new, old in replacements: | |
s = s.replace(old, new) | |
rest = syllables.index(s[-2:]) if s[-2:] else 0 | |
return len(syllables) * decode(s[0:-2]) + rest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment