Skip to content

Instantly share code, notes, and snippets.

@philchristensen
Created January 13, 2014 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philchristensen/8393105 to your computer and use it in GitHub Desktop.
Save philchristensen/8393105 to your computer and use it in GitHub Desktop.
Generate nonsense words out of integers
# Port of rufus-mnemo to Python
# https://github.com/jmettraux/rufus-mnemo
# Original copyright (c) 2007-2011, John Mettraux, jmettraux@gmail.com
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