def _anagram_recursive_piece_1(alpha, wildcards, wordlist, ahash): sub_anas = [ (sub, adiff, wildcards - wildcards_used, index) for index, sub in enumerate(wordlist.find_by_anahash_raw(ahash)) for adiff, wildcards_used in [anagram_diff(alpha, alphagram(sub))] if wildcards >= wildcards_used or ( wildcards < 0 and wildcards_used == 0) ] sub_anas.sort(key=adjusted_anagram_cost) for slug1, alpha2, wildcards_remaining, index in sub_anas: alpha1 = alphagram(slug1) yield _anagram_recursive_piece_2(slug1, alpha2, wildcards_remaining, wordlist)
def anagram_single(text, wildcards=0, wordlist=WORDS, count=10, quiet=True): """ Search for anagrams that appear directly in the wordlist. """ return eval_anagrams(_anagram_single(alphagram(slugify(text)), wildcards, wordlist), wordlist, count, quiet=quiet)
def anagram_double(text, wildcards=0, wordlist=WORDS, count=100, quiet=False): """ Search for anagrams that can be made of two words or phrases from the wordlist. """ return eval_anagrams(_anagram_double(alphagram(slugify(text)), wildcards, wordlist), wordlist, count, quiet=quiet)
def build_wordplay(self): self.db.execute("DROP TABLE IF EXISTS wordplay") for statement in self.wordplay_schema: self.db.execute(statement) with self.db: for i, slug, freq, text in read_wordlist(self.name): alpha = alphagram(slug) ana = anahash(slug) cons = consonantcy(slug) self.db.execute( "INSERT INTO wordplay (slug, alphagram, anahash, consonantcy) " "VALUES (?, ?, ?, ?)", (slug, alpha, ana, cons)) if i % 10000 == 0: print("\t%s" % (text))
def anagrams(text, wildcards=0, wordlist=WORDS, count=100, quiet=False, time_limit=None): """ Search for anagrams that are made of an arbitrary number of pieces from the wordlist. """ return eval_anagrams(_anagram_recursive(alphagram(slugify(text)), wildcards, wordlist), wordlist, count, quiet=quiet, time_limit=time_limit)
def _anagram_single(alpha, wildcards, wordlist): if len(alpha) == 0: # don't make anything out of *just* wildcards return if len(alpha) == 1 and wildcards == 0: # short circuit yield alpha return if wildcards == 0: yield from wordlist.find_by_alphagram_raw(alpha) elif wildcards > 0: for seq in itertools.combinations(letters_to_try, wildcards): newalpha = alphagram(alpha + ''.join(seq)) yield from wordlist.find_by_alphagram_raw(newalpha) elif wildcards < 0: selection_size = len(alpha) + wildcards if selection_size == 0: yield '' elif selection_size >= 0: for newalpha_seq in itertools.combinations(alpha, selection_size): newalpha = ''.join(newalpha_seq) yield from _anagram_single(newalpha, 0, wordlist)
def find_by_alphagram(text): return WORDS.find_by_alphagram(alphagram(slugify(text)))