def brk(self, no_nodes): self.stack.append(self.current_token) tokens_to_concept = self.amr_graph.tokens_to_concepts_dict[ self.current_token] self.actions.append( act.AMRAction("BRK", tokens_to_concept[1], tokens_to_concept[0])) self.buffer_indices.pop(0) if len(self.buffer_indices) != 0: self.current_token = self.buffer_indices[0]
def shift(self): tokens_to_concept = self.amr_graph.tokens_to_concept_list_dict[ self.current_token][0] node = tokens_to_concept[0] concept = tokens_to_concept[1] node_token_pair = (node, self.current_token) self.stack.append(node_token_pair) self.actions.append(act.AMRAction("SH", concept, node)) self.buffer_indices.pop(0) if len(self.buffer_indices) != 0: self.current_token = self.buffer_indices[0]
def brk(self, no_nodes): tokens_to_concept_list = self.amr_graph.tokens_to_concept_list_dict[ self.current_token] node1 = tokens_to_concept_list[0][0] concept1 = tokens_to_concept_list[0][1] node2 = tokens_to_concept_list[1][0] concept2 = tokens_to_concept_list[1][1] node_token_pair_1 = (node1, self.current_token) node_token_pair_2 = (node2, self.current_token) self.stack.append(node_token_pair_1) self.stack.append(node_token_pair_2) self.actions.append( act.AMRAction("BRK", concept1, node1, concept2, node2)) self.buffer_indices.pop(0) if len(self.buffer_indices) != 0: self.current_token = self.buffer_indices[0]
def generate_action_sequence_impl(amr_graph, sentence, no_of_swaps, should_rotate, verbose=True): if verbose is False: logging.disable(logging.INFO) buffer = sentence.split(" ") # Top of stack is at position 0 stack = [] actions = [] current_token = 0 # last_action is: # 0 for "las action was not a swap" # 1 for "last action was a swap_1" # 2 for "last action was a swap_2" and so on last_action_swap = 0 last_rotate = False while current_token < len(buffer) or len(stack) != 1: reduce_succeeded = False if len(stack) >= 2: # try to reduce the first two terms top = len(stack) - 1 node_var_first = amr_graph.tokens_to_concepts_dict[stack[top]][0] node_var_second = amr_graph.tokens_to_concepts_dict[stack[top - 1]][0] if (node_var_first, node_var_second) in list(amr_graph.relations_dict.keys()): # first is child of second => reduce right # check if first has any children left to process if len(amr_graph.relations_dict[(node_var_first, node_var_second)][1]) == 0: reduce_right(actions, stack, top, node_var_first, node_var_second, amr_graph) reduce_succeeded = True else: if (node_var_second, node_var_first) in list( amr_graph.relations_dict.keys()): # second is child of first => reduce left # check if second has any children left to process if len(amr_graph.relations_dict[(node_var_second, node_var_first)][1]) == 0: reduce_left(actions, stack, top, node_var_first, node_var_second, amr_graph) reduce_succeeded = True if reduce_succeeded: # reset the last_action_swap to 0 to indicate that the last action was not swap last_action_swap = 0 last_rotate = False else: if last_action_swap == no_of_swaps: # try to rotate if should_rotate and (not last_rotate) and (len(stack) >= 3): # print("rotate") rotate(actions, stack, top) last_rotate = True else: if last_rotate: logging.debug( "Last rotate didn't solve the stack. Tokens left on the stack: %s. Actions %s.", stack, actions) raise RotateException( "Could not generate action sequence. Rotate not working" ) else: logging.debug( "Last swap didn't solve the stack. Tokens left on the stack: %s. Actions %s.", stack, actions) raise SwapException( "Could not generate action sequence. Swap not working" ) if current_token >= len(buffer): if (len(stack) >= (last_action_swap + 3) ) and no_of_swaps != 0 and last_action_swap < no_of_swaps: swap_n(actions, stack, top, last_action_swap + 1) last_action_swap += 1 last_rotate = False else: logging.debug("Tokens left on the stack: %s. Actions %s.", stack, actions) raise TokenOnStackException( "Could not generate action sequence. Tokens left on stack" ) else: # try to shift the current token if current_token in list( amr_graph.tokens_to_concepts_dict.keys()): stack.append(current_token) tokens_to_concept = amr_graph.tokens_to_concepts_dict[ current_token] actions.append( act.AMRAction("SH", tokens_to_concept[1], tokens_to_concept[0])) else: actions.append(act.AMRAction.build("DN")) # reset the last_action_swap to 0 to indicate that the last action was not swap last_action_swap = 0 current_token += 1 return actions