the_phrase = Bag(''.join(args.words)) print("Pruning dictionary. Before: {} bags ...".format( len(dict_hash_table.keys())), file=sys.stderr, end='') # Now convert the hash table to a list, longest entries first. (This # isn't necessary, but it makes the more interesting anagrams appear # first.) # While we're at it, prune the list, too. That _is_ necessary for the # program to finish before you grow old and die. the_dict_list = sorted( [[k, dict_hash_table[k]] for k in dict_hash_table.keys() if the_phrase.subtract(k)], key=len) print(" After: {} bags.".format(len(the_dict_list)), file=sys.stderr) pr = cProfile.Profile() pr.enable() result = anagrams(the_phrase, the_dict_list) pr.disable() ps = pstats.Stats(pr, stream=sys.stderr).sort_stats('cumulative') ps.print_stats() print("{} anagrams of {}".format(len(result), ' '.join(args.words)), file=sys.stderr) for a in result:
the_phrase = Bag(args[0]) print("Pruning dictionary. Before: {} bags ...".format( len(dict_hash_table.keys())), file=sys.stderr, end='') # Now convert the hash table to a list, longest entries first. (This # isn't necessary, but it makes the more interesting anagrams appear # first.) # While we're at it, prune the list, too. That _is_ necessary for the # program to finish before you grow old and die. the_dict_list = [[k, dict_hash_table[k]] for k in dict_hash_table.keys() if the_phrase.subtract(k)] # Note that sorting entries "alphabetically" only makes partial sense, # since each entry is(at least potentially) more than one word(all # the words in an entry are anagrams of each other). def biggest_first_then_alphabetically(a, b): a = a[1][0] b = b[1][0] result = cmp(len(b), len(a)) if not result: result = cmp(a, b) return result the_dict_list.sort(biggest_first_then_alphabetically) print(" After: {} bags.".format(len(the_dict_list)), file=sys.stderr)
dict_hash_table = dict.snarf_dictionary(options.dict_fn) the_phrase = Bag(args[0]) print("Pruning dictionary. Before: {} bags ...".format(len(dict_hash_table.keys())), file=sys.stderr, end='') # Now convert the hash table to a list, longest entries first. (This # isn't necessary, but it makes the more interesting anagrams appear # first.) # While we're at it, prune the list, too. That _is_ necessary for the # program to finish before you grow old and die. the_dict_list = [[k, dict_hash_table[k]] for k in dict_hash_table.keys() if the_phrase.subtract(k)] # Note that sorting entries "alphabetically" only makes partial sense, # since each entry is(at least potentially) more than one word(all # the words in an entry are anagrams of each other). def biggest_first_then_alphabetically(a, b): a = a[1][0] b = b[1][0] result = cmp(len(b), len(a)) if not result: result = cmp(a, b) return result the_dict_list.sort(biggest_first_then_alphabetically) print(" After: {} bags.".format(len(the_dict_list)),
args = parser.parse_args() dict_hash_table = dict.snarf_dictionary_from_file(args.dictionary) the_phrase = Bag("".join(args.words)) print("Pruning dictionary. Before: {} bags ...".format(len(dict_hash_table.keys())), file=sys.stderr, end="") # Now convert the hash table to a list, longest entries first. (This # isn't necessary, but it makes the more interesting anagrams appear # first.) # While we're at it, prune the list, too. That _is_ necessary for the # program to finish before you grow old and die. the_dict_list = sorted([[k, dict_hash_table[k]] for k in dict_hash_table.keys() if the_phrase.subtract(k)], key=len) print(" After: {} bags.".format(len(the_dict_list)), file=sys.stderr) pr = cProfile.Profile() pr.enable() result = anagrams(the_phrase, the_dict_list) pr.disable() ps = pstats.Stats(pr, stream=sys.stderr).sort_stats("cumulative") ps.print_stats() print("{} anagrams of {}".format(len(result), " ".join(args.words)), file=sys.stderr) for a in result: sys.stdout.write("(") for i, w in enumerate(a):