def treeify(parsed_dict, parent): for key, values in parsed_dict.items(): key_node = pptree.Node(key, parent) if isinstance(values, str): _ = pptree.Node(values, key_node) else: treeify(values, key_node)
def generate_tree_architecture(self, root_node=None): if root_node is None: the_node = pptree.Node(str(self)) else: the_node = pptree.Node(str(self), root_node) if self.left is not None: self.left.generate_tree_architecture(the_node) if self.right is not None: self.right.generate_tree_architecture(the_node) return the_node
def inorder(node, nnode): if node.isLeaf: newnode = pptree.Node('H', nnode) wnode = pptree.Node(node.word, newnode) elif nnode is not None: newnode = pptree.Node('H', nnode) inorder(node.left, newnode) inorder(node.right, newnode) elif node.isRoot(): newnode = pptree.Node('H') inorder(node.left, newnode) inorder(node.right, newnode) return newnode return None
def out_pptree(self, parent=None): """ Recursive function Return instance of pptree module to print the tree :param parent: parent of the tree (for the recursive part of the function) :type parent: Node :return: instance of pptree.Node """ name = repr(self) if parent: current = pptree.Node(name, parent) else: current = pptree.Node(name) for c in self.children: c.out_pptree(current) return current
def pretty_print_attention_tree(attention_list, input_tree, parent, write_index, curr_index): """ Display the parts of the tree focused on by the attention while a particular node is being generated. This function was designed for the identity dataset, where the input and target trees are identical. :param attention_list: a list of indices the attention focused on (preorder traversal order) :param input_tree: subtree we're currently processing :param parent: parent of the node we're currently processing :param write_index: node currently being created during this iteration :param curr_index: index of the node we're currently processing :returns index of one past the last node in our subtree. """ # If the current node was being generated or was focused on by the attention, mark it root_val = str(int(target_tree.value)) if curr_index == write_index: root_val = "*" + root_val + "*" if curr_index in attention_list: root_val = "(" + root_val + ")" # Create new node root_node = pptree.Node(root_val, parent) curr_index += 1 # Recursively add the child subtrees to the tree. for child in target_tree.children: curr_index = pretty_print_attention_tree(attention_list, child, root_node, write_index, curr_index) if parent is None: pptree.print_tree(root_node) return curr_index
def parseHelper(b, sentence, p, LEX_k, project_rounds, verbose, debug, lexeme_dict, all_areas, explicit_areas, readout_method, readout_rules): debugger = ParserDebugger(b, all_areas, explicit_areas) sentence = sentence.split(" ") extreme_debug = False for word in sentence: lexeme = lexeme_dict[word] b.activateWord(LEX, word) if verbose: print("Activated word: " + word) print(b.areas[LEX].winners) for rule in lexeme["PRE_RULES"]: b.applyRule(rule) proj_map = b.getProjectMap() for area in proj_map: if area not in proj_map[LEX]: b.areas[area].fix_assembly() if verbose: print("FIXED assembly bc not LEX->this area in: " + area) elif area != LEX: b.areas[area].unfix_assembly() b.areas[area].winners = [] if verbose: print("ERASED assembly because LEX->this area in " + area) proj_map = b.getProjectMap() if verbose: print("Got proj_map = ") print(proj_map) for i in range(project_rounds): b.parse_project() if verbose: proj_map = b.getProjectMap() print("Got proj_map = ") print(proj_map) if extreme_debug and word == "a": print("Starting debugger after round " + str(i) + "for word" + word) debugger.run() #if verbose: # print("Done projecting for this round") # for area_name in all_areas: # print("Post proj stats for " + area_name) # print("w=" + str(b.areas[area_name].w)) # print("num_first_winners=" + str(b.areas[area_name].num_first_winners)) for rule in lexeme["POST_RULES"]: b.applyRule(rule) if debug: print("Starting debugger after the word " + word) debugger.run() # Readout # For all readout methods, unfix assemblies and remove plasticity. b.no_plasticity = True for area in all_areas: b.areas[area].unfix_assembly() dependencies = [] def read_out(area, mapping): to_areas = mapping[area] b.project({}, {area: to_areas}) this_word = b.getWord(LEX) for to_area in to_areas: if to_area == LEX: continue b.project({}, {to_area: [LEX]}) other_word = b.getWord(LEX) dependencies.append([this_word, other_word, to_area]) for to_area in to_areas: if to_area != LEX: read_out(to_area, mapping) def treeify(parsed_dict, parent): for key, values in parsed_dict.items(): key_node = pptree.Node(key, parent) if isinstance(values, str): _ = pptree.Node(values, key_node) else: treeify(values, key_node) if readout_method == ReadoutMethod.FIXED_MAP_READOUT: # Try "reading out" the parse. # To do so, start with final assembly in VERB # project VERB->SUBJ,OBJ,LEX parsed = {VERB: read_out(VERB, readout_rules)} print("Final parse dict: ") print(parsed) root = pptree.Node(VERB) treeify(parsed[VERB], root) if readout_method == ReadoutMethod.FIBER_READOUT: activated_fibers = b.getActivatedFibers() if verbose: print("Got activated fibers for readout:") print(activated_fibers) read_out(VERB, activated_fibers) print("Got dependencies: ") print(dependencies)