Created
April 15, 2012 02:14
-
-
Save cammckinnon/2389359 to your computer and use it in GitHub Desktop.
English word patterns
This file contains hidden or 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
# 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