def test_to_png_bbt(self, id_mock): """ Test full run of tree to png for bbt """ def side_effect(name): """ If ID is not mocked it is bases on memory location, which we cant predict. Rename it, this way we catch that edges work correctly. Had a bug where a counter was use to ID vertexes and since this method before only return the argument, we didn't catch that outside the test, edges id's were the counter number and not ID. """ return f"v{name}" id_mock.side_effect = side_effect bst = Bst() utils.list_to_bst([10, 20, 0, 33, 2, 1, 15], bst) with tempfile.TemporaryDirectory(dir="./") as tmpdirname: treevizer.to_png(bst.root, "bbt", tmpdirname + "/tree file.dot", tmpdirname + "/tree.png") # breakpoint() with open(tmpdirname + "/tree file.dot", "rb") as test_dot: with open("tests/fixtures/integration_files/bst.dot", "rb") as correct_dot: self.assertEqual(test_dot.read(), correct_dot.read())
def test_to_png_trie(self, id_mock): """ Test full run of tree to png for bbt """ def side_effect(name): """ If ID is not mocked it is bases on memory location, which we cant predict. Rename it, this way we catch that edges work correctly. Had a bug where a counter was use to ID vertexes and since this method before only return the argument, we didn't catch that outside the test, edges id's were the counter number and not ID. """ return f"v{name}" id_mock.side_effect = side_effect trie = Trie() trie.add_word("ball", 0) trie.add_word("bat", 0) trie.add_word("cat", 0) trie.add_word("a", 0) trie.add_word("he", 0) trie.add_word("heat", 0) trie.add_word("hen", 0) with tempfile.TemporaryDirectory(dir="./") as tmpdirname: treevizer.to_png(trie.root, "trie", tmpdirname + "/tree file.dot", tmpdirname + "/tree.png") with open(tmpdirname + "/tree file.dot", "rb") as test_dot: with open("tests/fixtures/integration_files/trie.dot", "rb") as correct_dot: self.assertEqual(test_dot.read(), correct_dot.read())
def insert_into_bst(tree): """ Insert a node to the tree. """ key = int(input("\nKey: ")) value = input("Value: ") tree[key] = value print(f"Node {key}: {value} has been added.") treevizer.to_png(tree.root, png_path="bst_insert.png")
def clean_bst(tree): """Generate test tree.""" for ix, key in enumerate(remove_seq): tree.remove(key) if tree.root is not None: treevizer.to_png(tree.root, png_path=f"bst_clean{ix}.png") print(f"Removed {key}") print("BST cleaned")
def remove_from_bst(tree): """ Remove a node from the tree. """ key = int(input("\nGive me a key: ")) try: print(f"{tree.remove(key)} has been removed.") except KeyError as e: print(f"Error: {e}") if tree.root is not None: treevizer.to_png(tree.root, png_path=f"bst_remove{key}.png")
def generate_test_bst(tree): """Generate test tree.""" if len(tree) > 0: print("Removing old content ...") tree = None tree = BinarySearchTree() #breakpoint() for value in test_values: tree.insert(value, str(value)) treevizer.to_png(tree.root, png_path="bst_gen.png") print("BST generated")
def remove_word(self): """ Remove word from trie data structure """ inp = input("Word: ") try: del self.trie[inp] print(f"Word '{inp}' removed from the trie") treevizer.to_png( sc.trie.root, structure_type="trie", dot_path="trie_after_remove.dot", png_path= f"{self._filename}{self.counter}_after_remove_{inp}.png") self.counter += 1 except SearchMiss as err: print(err.message) return False
node = self.trie.find_node(prefix) except SearchMiss: pass if 'node' in locals(): suffixes = self.trie.find_all_words(node) suggestions = sorted(suffixes.items(), key=lambda x: x[1]) nr_of_iterations = len(suggestions) if nr_of_iterations > 10: nr_of_iterations = 10 for index in range(nr_of_iterations): print( f"{prefix}{suggestions[index][0]}({suggestions[index][1]})" ) print(" *** Quit by typing space any word after! ***") prefix += input(f" Prefix: {prefix}") return False if __name__ == "__main__": # filename = 'tiny_frequency.txt' # filename = 'frequency.txt' filename = 'test_dict.txt' # filename = 'frequency400.txt' sc = SpellChecker(filename) treevizer.to_png(sc.trie.root, structure_type="trie", dot_path="trie.dot", png_path=f"{filename}.png") # breakpoint() sc.main()