Created
April 6, 2012 08:18
-
-
Save juanriaza/2318128 to your computer and use it in GitHub Desktop.
angry words snippet
This file contains 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
# -*- coding: utf-8 -*- | |
from heapq import merge | |
from requests import async | |
import itertools | |
import requests | |
import json | |
possible_words = lambda y: list(itertools.chain(*[[''.join(p) for p in itertools.permutations(y, i)] for i in range(1, len(y) + 1)])) | |
merge_sort = lambda w: sorted(set(merge(*w)), key=lambda x: len(x))[::-1] | |
generate_req = lambda c: requests.get('http://apalabrados.com/api/dictionaries/ES', params={'words': ','.join(c)}, prefetch=True, return_response=False) | |
def check_valid_words(words): | |
print 'Checking %d words' % len(words) | |
valid_words = [] | |
chunks = [words[i:i + 100] for i in range(0, len(words), 100)] | |
for req in async.map([generate_req(chunk) for chunk in chunks], size=10): | |
valid_words.extend(json.loads(req.content)['ok']) | |
return merge_sort((valid_words,)) | |
def angry_words(letters, prepend='', append='', max_length=None): | |
if isinstance(prepend, tuple): | |
return merge_sort([angry_words(letters, append=append, prepend=p, max_length=max_length) for p in prepend]) | |
if isinstance(append, tuple): | |
return merge_sort([angry_words(letters, append=a, prepend=prepend, max_length=max_length) for a in append]) | |
print 'Generating words containing "%s", prepending "%s", appending "%s" with a maximum length of %s.' % (letters, prepend, append, max_length or '-') | |
words = ['%s%s%s' % (prepend, p, append) for p in possible_words(letters)] | |
if max_length: | |
words = filter(lambda e: len(e) <= max_length, words) | |
return check_valid_words(words) | |
print angry_words('gdssool', prepend=('hola', 'j', 'q', ''), append=('a', 'b', ''), max_length=6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Generating words containing "gdssool", prepending "hola", appending "a" with a maximum length of 6.
Checking 7 words
Generating words containing "gdssool", prepending "hola", appending "b" with a maximum length of 6.
Checking 7 words
Generating words containing "gdssool", prepending "hola", appending "" with a maximum length of 6.
Checking 49 words
Generating words containing "gdssool", prepending "j", appending "a" with a maximum length of 6.
Checking 1099 words
Generating words containing "gdssool", prepending "j", appending "b" with a maximum length of 6.
Checking 1099 words
Generating words containing "gdssool", prepending "j", appending "" with a maximum length of 6.
Checking 3619 words
Generating words containing "gdssool", prepending "q", appending "a" with a maximum length of 6.
Checking 1099 words
Generating words containing "gdssool", prepending "q", appending "b" with a maximum length of 6.
Checking 1099 words
Generating words containing "gdssool", prepending "q", appending "" with a maximum length of 6.
Checking 3619 words
Generating words containing "gdssool", prepending "", appending "a" with a maximum length of 6.
Checking 3619 words
Generating words containing "gdssool", prepending "", appending "b" with a maximum length of 6.
Checking 3619 words
Generating words containing "gdssool", prepending "", appending "" with a maximum length of 6.
Checking 8659 words
[u'dolosa', u'lodosa', u'golosa', u'solos', u'dolos', u'logos', u'dogos', u'ososa', u'glosa', u'gloso', u'lodos', u'solda', u'godos', u'soldo', u'loso', u'soda', u'losa', u'sola', u'lodo', u'solo', u'doga', u'jodo', u'soga', u'josa', u'dogo', u'joda', u'goda', u'soso', u'loga', u'sosa', u'godo', u'dola', u'osos', u'dolo', u'gola', u'oso', u'dos', u'gol', u'los', u'loo', u'loa', u'oda', u'sos', u'osa', u'sol', u'ola', u'os', u'so', u'oa', u'da', u'jo', u'lo', u'la', u'do']
[Finished]