Skip to content

Instantly share code, notes, and snippets.

@kjaquier
Last active August 29, 2015 14:03
Show Gist options
  • Save kjaquier/965e7bd610e9b10b2d3e to your computer and use it in GitHub Desktop.
Save kjaquier/965e7bd610e9b10b2d3e to your computer and use it in GitHub Desktop.
Generate words with similar spelling and/or sounding from a list of words given in arguments.
import sys
#
# Generation config
#
PRINT_COLS = 8
LETTERS_CHANGED = 2
#
# Sounds config
#
SOUNDS = {
'a': ['e'],
'b': ['p'],
'c': ['s', 'k'],
'd': ['t'],
'e': ['i', ''],
'f': ['v'],
'g': ['j', 'k'],
'h': ['', 'w', 'y'],
'i': ['e', 'y'],
'j': ['y', 'g'],
'k': [],
'l': [],
'm': ['n'],
'n': ['m'],
'o': ['e'],
'p': ['b'],
'q': ['k'],
'r': ['l'],
's': ['z'],
't': ['s', 'd'],
'u': ['o', 'e'],
'v': ['w', 'f', 'u'],
'w': ['f', 'v', 'u', 'h', ''],
'x': ['c', 'k', 's', 'z'],
'y': ['i', 'j', 'u'],
'z': ['s', 'x'],
}
def get_sounds(letter):
related_sounds = set(SOUNDS[letter])
for sound, letters in SOUNDS.iteritems():
if sound in related_sounds:
related_sounds.update(letters)
if letter in letters:
related_sounds.add(sound)
related_sounds.update(letters)
return related_sounds
# Update every sound entry with related sounds
SOUNDS = { k : get_sounds(k) for k in SOUNDS.keys() }
#
# Generate variants of input words using SOUNDS
#
def variants(word):
yield word
for i, letter in enumerate(word):
for related_letter in SOUNDS[letter]:
yield word[:i] + related_letter + word[i+1:]
def multi_variants(word, levels = 1):
i = 0
if levels > 0:
for var in variants(word):
yield var
for v in multi_variants(var, levels-1):
yield v
i = 0
words = sys.argv[1:]
max_width = max(len(w) for w in words)
for word in words:
for var in multi_variants(word, levels = LETTERS_CHANGED):
print ("{0:%s} " % max_width).format(var),
i += 1
if i >= PRINT_COLS:
i = 0
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment