Example #1
0
def complex_anagram_gen(bagnum):
    for key in ngrams:
        if bagnum % key == 0:
            main_text, main_words, main_freq = ngrams[key]
            other = simple_anagram_numeric(bagnum / key)
            if other is not None:
                other_text, other_words, other_freq = other
                yield (main_text + " " + other_text, main_words + other_words, min(main_freq, other_freq))
            else:
                garble = unbag(bagnum / key)
                # quick and dirty score: prefer common letters remaining
                score = float(main_freq) * leftover_score(garble)
                yield (main_text + "/" + garble, main_words + len(garble), score)
Example #2
0
def complex_anagram_gen(bagnum, max_partial=20):
    partial_scores = []
    for key in ngrams:
        if bagnum % key == 0:
            main_list=ngrams[key]
            for main_text, main_words, main_freq in main_list:
                other_list = ngrams.get(bagnum/key, [])
                found = False
                for other_text, other_words, other_freq in other_list:
                    found = True
                    yield (main_text+' '+other_text, main_words+other_words,
                           min(main_freq, other_freq))
                if not found:
                    garble = unbag(bagnum/key)
                    # Calculate a score for this partial anagram, and see if
                    # it's better than others.
                    score = float(main_freq) * leftover_score(garble)
                    if len(partial_scores) < max_partial or score > partial_scores[0]:
                        partial_scores.append(score)
                        partial_scores.sort()
                        partial_scores = partial_scores[-max_partial:]
                        yield (main_text+'/'+garble, main_words+len(garble),
                               score)