Skip to content

Instantly share code, notes, and snippets.

@cammckinnon
Created April 15, 2012 02:14
Show Gist options
  • Save cammckinnon/2389359 to your computer and use it in GitHub Desktop.
Save cammckinnon/2389359 to your computer and use it in GitHub Desktop.
English word patterns
# word pattern => list of corresponding english words
pattern_words = defaultdict(list)
def get_pattern(word):
"""Return the pattern for the given word."""
map = {}
letters = iter(string.lowercase)
pattern = ''
for char in word:
if char not in map:
map[char] = letters.next()
pattern += map[char]
return pattern
# read in the dictionary words
with open('../dictionary/wordlist.txt') as f:
for word in f.read().split():
words.add(word)
# compute and store the pattern for each dictionary word
for word in words:
pattern = get_pattern(word)
pattern_words[pattern] += [word]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment