def load(cls, prefix): trie = Trie.load(prefix + ".trie") with open(prefix + ".utrie", 'rb') as out_f: untrieable = pickle.load(out_f) obj = cls(trie, untrieable) return obj
# need fast trie (prefix tree) data structure. Choose from 3 libraries with (almost) compatible APIs. if False: from datrie import Trie # pip install datrie trie_path = 'datrie.dump' elif False: from marisa_trie import Trie # pip install marisa-trie trie_path = 'marisa_trie.dump' else: from dawg import CompletionDAWG as Trie # pip install dawg trie_path = 'dawg.dump' if os.path.exists(trie_path): # here's one i built earlier if Trie.__module__ == 'dawg': trie = Trie() trie.load(trie_path) else: trie = Trie.load(trie_path) else: dict_path = "garbled_email_dictionary.txt" if not os.path.exists(dict_path): # download code jam's dictionary dict_url = "https://code.google.com/codejam/contest/static/garbled_email_dictionary.txt" from urllib.request import urlretrieve urlretrieve(dict_url, dict_path) with open(dict_path) as f: words = [line.strip() for line in f.readlines()] if Trie.__module__ == 'datrie': trie = Trie(ascii_lowercase)