Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> from collections import defaultdict
- >>>
- >>> def read_file(f):
- ... with open(f, 'r') as f:
- ... return f.read()
- ...
- >>> def sort_string(s):
- ... return ''.join(sorted(s))
- ...
- >>>
- >>> words = read_file('./scrabble_dict.txt').lower().split()
- >>> sorted_to_words = defaultdict(list)
- >>> all_sorted_words = map(sort_string, words)
- >>>
- >>> for word in words:
- ... sorted_to_words[sort_string(word)].append(word)
- ...
- >>>
- >>> all_sorted_words = ' %s ' % ' '.join(all_sorted_words)
- >>>
- ...
- >>> def get_subwords(word, all_sorted_words=all_sorted_words, sorted_to_words=sorted_to_words):
- ... word = sort_string(word)
- ... regex = re.compile(' (%s) ' % ''.join(c + '?' for c in word))
- ... matches = regex.findall(all_sorted_words)
- ... return sorted(set(sum(map(sorted_to_words.__getitem__, matches), [])), key=len, reverse=True)
- ...
- >>>
- >>> get_subwords('elephant')
- ['elephant', 'heeltap', 'heptane', 'phenate', 'haptene', 'lateen', 'hapten', 'thenal', 'planet', 'ethane', 'peahen', 'hantle', 'platen', 'paten', 'thane', 'plena', 'pleat', 'tepal', 'anele', 'panel', 'telae', 'eaten', 'leant', 'lepta', 'elate', 'plane', 'aleph', 'neath', 'penal', 'etape', 'plate', 'palet', 'petal', 'plant', 'leapt', 'enate', 'lethe', 'laten', 'help', 'pant', 'elan', 'pane', 'hate', 'pent', 'late', 'tape', 'lath', 'then', 'halt', 'thee', 'hale', 'haen', 'teal', 'alee', 'haet', 'heel', 'pale', 'pelt', 'lean', 'leap', 'pele', 'peen', 'peel', 'plea', 'etna', 'path', 'peal', 'tepa', 'pate', 'ante', 'tela', 'nape', 'tele', 'teen', 'teel', 'lept', 'heal', 'heat', 'heap', 'thae', 'than', 'tael', 'neap', 'neat', 'epha', 'phat', 'neep', 'hent', 'leet', 'peat', 'plat', 'pean', 'plan', 'lent', 'hant', 'lane', 'tale', 'eath', 'ale', 'alt', 'alp', 'hae', 'eat', 'hap', 'hat', 'pee', 'eel', 'tel', 'ten', 'tea', 'tee', 'pet', 'nee', 'peh', 'net', 'pea', 'lea', 'lee', 'nth', 'let', 'ate', 'eth', 'apt', 'pht', 'tap', 'ape', 'het', 'hep', 'hen', 'ane', 'ant', 'tae', 'pen', 'tan', 'pat', 'nae', 'nah', 'nap', 'eta', 'pah', 'pal', 'pan', 'lat', 'lap', 'the', 'pe', 'la', 'ta', 'el', 'en', 'eh', 'et', 'pa', 'ha', 'he', 'ae', 'ah', 'al', 'an', 'at', 'na', 'ne']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement