Esempio n. 1
0
def main():
    args = parse_args()

    if args.wordsearch:
        # Parse the specified wordsearch file
        board = Board([list(line.strip()) for line in args.wordsearch])
    else:
        # Use a random board
        board = random_board(args.width, args.height)

    # Pickle trie for performance
    # The cache tracks the last modified time of the dictionary.
    # If there exists a pickle cache for the dictionary's name and the file has not been
    # changed since the last cache was made, load from the pickle cache instead.
    cache_name = args.dictionary.name + '.pickle'
    cache_valid = False  #In case the cache file doesn't exist
    if os.path.exists(cache_name):
        lastmtime, rootnode = pickle.load(open(cache_name, 'rb'))
        cache_valid = lastmtime >= os.path.getmtime(args.dictionary.name)

    if not cache_valid:
        rootnode = TrieNode()
        for word in args.dictionary:
            rootnode.index(word.strip())

        pickle.dump((os.path.getmtime(args.dictionary.name), rootnode),
                    open(cache_name, 'wb'))

    for word in search_board(board, rootnode):
        if len(word) >= args.min_word_length:
            print(word)
Esempio n. 2
0
 def test_index(self):
     root = TrieNode()
     root.index('amp', 'ack', 'bus')
     self.assertTrue(recursive_equal(root, self.reference_root))