def compute_concatenated_words(words): tree = Tree() tree.add_words(words) concatenated_word_count = 0 max_concat_length = 0 second_max_concat_length = 0 max_length_concat_words = [] second_max_length_concat_words = [] for word in words: if tree.is_concatenated(word): concatenated_word_count += 1 if len(word) > max_concat_length: second_max_concat_length = max_concat_length second_max_length_concat_words = max_length_concat_words max_concat_length = len(word) max_length_concat_words = [word] elif len(word) == max_concat_length: max_length_concat_words.append(word) elif len(word) > second_max_concat_length: second_max_concat_length = len(word) second_max_length_concat_words = [word] elif len(word) == second_max_concat_length: second_max_length_concat_words.append(word) return concatenated_word_count, \ max_length_concat_words, \ second_max_length_concat_words
def compress(string): if not string or not len(string): throw tree = Tree(string) treedict = tree.as_dict() compressed = [] for c in string: compressed += treedict[c] return (tree.as_bytes(), BitArray.to_bytes(compressed))
def generate_random_tree(a, b, c): my_set = [] tree = None while len(my_set) < c: num = random.randint(a, b) if num not in my_set: my_set.append(num) if tree is None: tree = Tree(num) else: tree.insert(Tree(num)) print('list', my_set, '\n') return tree
def main(): """Main Function""" setup_logging() obj_1 = Tree('Parent') children = [Tree("child1"), Tree("child2"), Tree("child3")] obj_1.add_children(children) g_child = [Tree("newborn1"), Tree("newborn2")] children[0].add_children(g_child) g_child_1 = [Tree("newbie2")] children[1].add_children(g_child_1) LOGGER.info(obj_1)
def run_id3(database_name, num_trials): """ Loop num_trials times, building a new tree and testing it against the test set of movies. """ total = 0 for x in range(0, num_trials): mydata = init_dataset(database_name) decision_tree = Tree(id3_tree(mydata.learn_set, mydata.attribute_set)) decision_tree.insert_rules() num_correct = 0 for movie in mydata.test_set: for rule in decision_tree.rules: conditions_met = True key = '' value = '' for i in range(0, len(rule) - 1, 2): #Even index = attribute, odd index = class label key = rule[i] value = rule[i + 1] #Check if value is correct if movie[key] != value: conditions_met = False break if conditions_met: if rule[-1] == movie[TARGET]: num_correct += 1 #End for if we find a rule that correctly classifies movie break total += (num_correct / len(mydata.test_set)) * 100 print("Test #" + str(x) + ", accuracy = " + str((num_correct / len(mydata.test_set) * 100))) total /= num_trials print("Average over " + str(num_trials) + " trials: " + str(total) + "%")
def uncompress(tree, compressed): string = BitArray.from_bytes(compressed) tree = Tree(None, tree) uncompressed = '' sel = tree.tree for bit in string: if bit == 0: sel = sel.left else: sel = sel.right if (sel.key): uncompressed += sel.key sel = tree.tree return uncompressed
from classes.node import Node from classes.tree import TreeNode, Tree tree = Tree() nodes = {} # load taxonomy data to dictionary print("Loading nodes from file") with open('taxdump/nodes.dmp') as f: for line in f: formatted_input = line.split('\t|\t') nodes[formatted_input[0]] = Node(formatted_input) print("Loading completed") # function to get a node path from the dictionary def getPath(id): if id not in nodes: return [] currentNode = nodes[id] res = [currentNode.tax_id] while (currentNode.tax_id != currentNode.parent_id): res.append(currentNode.parent_id) currentNode = nodes[currentNode.parent_id] return res # function to find lca based on a dictionary list def find_lca(n1, n2): n1_path = getPath(n1) n2_path = getPath(n2)
sliders['slider_number'].set_round(0) sliders['slider_division_ratio'].set_percentage(60) sliders['slider_division_angle'].set_percentage(37) sliders['slider_number'].set_percentage(85) tree_parameters['number'] = sliders['slider_number'].get_value() tree_parameters['division_ratio'] = sliders['slider_division_ratio'].get_value() tree_parameters['division_angle'] = sliders['slider_division_angle'].get_value() tree1 = Tree(pos=tree_parameters['pos'], angle=tree_parameters['angle'], ticks=tree_parameters['tick'], vel=tree_parameters['vel'], number=tree_parameters['number'], division_ratio=tree_parameters['division_ratio'], division_angle=tree_parameters['division_angle'], divide_angles=tree_parameters['divide_angles'], divide_distance=tree_parameters['divide_distance'], fruits=tree_parameters['fruits']) mouse_pos = None clicked = False holding_click = False hidden_hud = False run = True while run: clicked = False if pygame.mouse.get_pressed()[0] == 1 and not holding_click:
from classes.tree import TreeNode, Tree tree = Tree() tree.insert(1) tree.insert(3, 1) tree.insert(2, 1) tree.insert(4, 2) tree.insert(7, 2) tree.insert(78, 7) tree.insert(10, 3) tree.insert(10, 3) print(tree.print(tree.root, '')) def find_lca(tree, n1, n2): _, n1_path = tree.search(tree.root, n1) _, n2_path = tree.search(tree.root, n2) if n1_path is None: print('Node ', n1, ' doesn\'t exist') return None if n2_path is None: print('Node ', n2, ' doesn\'t exist') return None n1_path = n1_path.split(',') n2_path = n2_path.split(',') if len(n1_path) == 0 or len(n2_path) == 0: return None
def test_delete(): my_list = [] my_list.append(2) my_list.append(1) my_list.append(7) my_list.append(4) my_list.append(8) my_list.append(3) my_list.append(6) my_list.append(5) tree = Tree(2) tree.insert(Tree(1)) tree.insert(Tree(7)) tree.insert(Tree(4)) tree.insert(Tree(8)) tree.insert(Tree(3)) tree.insert(Tree(6)) tree.insert(Tree(5)) print('list', my_list, '\n') print('delete node 7:') tree.delete(7) tree.print() print('delete node 5:') tree.delete(5) tree.print()
def test_create(): my_tree = Tree(1, None) print('Create tree object with value 1: ', my_tree.get_value())