def apply(self, configuration: Configuration, transition: str) -> Configuration: """ ================================================================= Implement arc standard algorithm based on Incrementality in Deterministic Dependency Parsing(Nirve, 2004): Left-reduce Right-reduce Shift ================================================================= """ # TODO(Students) Start arc = transition[2:-1] if transition.startswith("S"): configuration.shift() else: word1 = configuration.get_stack(0) word2 = configuration.get_stack(1) if transition.startswith('L'): configuration.add_arc(word1, word2, arc) configuration.remove_second_top_stack() elif transition.startswith('R'): configuration.add_arc(word2, word1, arc) configuration.remove_top_stack() else: print('Unknown case encountered') pdb.set_trace() return configuration
def apply(self, configuration: Configuration, transition: str) -> Configuration: """ ================================================================= Implement arc standard algorithm based on Incrementality in Deterministic Dependency Parsing(Nirve, 2004): Left-reduce Right-reduce Shift ================================================================= """ # TODO(Students) Start if transition.startswith("L"): # Left reduce top1 = configuration.get_stack(0) top2 = configuration.get_stack(1) configuration.remove_second_top_stack() label = transition[2:-1] configuration.add_arc(top1, top2, label) elif transition.startswith("R"): top1 = configuration.get_stack(0) top2 = configuration.get_stack(1) configuration.remove_top_stack() label = transition[2: -1] configuration.add_arc(top2, top1, label) else: # "S" configuration.shift() return configuration
def apply(self, configuration: Configuration, transition: str) -> Configuration: """ ================================================================= Implement arc standard algorithm based on Incrementality in Deterministic Dependency Parsing(Nirve, 2004): Left-reduce Right-reduce Shift ================================================================= """ # TODO(Students) Start #using get stack from configuration.py val_1, val_2 = configuration.get_stack(0), configuration.get_stack(1) #left reduce if transition[0]=='L': configuration.remove_second_top_stack() configuration.add_arc(val_1, val_2, transition[2:-1]) #right reduce elif transition[0]=='R': configuration.remove_top_stack() configuration.add_arc(val_2, val_1, transition[2:-1]) else: configuration.shift() return configuration
def apply(self, configuration: Configuration, transition: str) -> Configuration: """ ================================================================= Implement arc standard algorithm based on Incrementality in Deterministic Dependency Parsing(Nirve, 2004): Left-reduce Right-reduce Shift ================================================================= """ # TODO(Students) Start top_stack, second_top_stack = configuration.get_stack( 0), configuration.get_stack(1) label = transition[2:-1] # Get the label for the transition if transition.startswith("L"): # If transition is left-arc # Create an arc from word on top_stack to word on second_top_stack configuration.add_arc(top_stack, second_top_stack, label) # Remove word at second position from top of stack configuration.remove_second_top_stack() elif transition.startswith("R"): # If transition is right-arc # Create an arc from word on second_top_stack to word on top_stack configuration.add_arc(second_top_stack, top_stack, label) # Remove word at top of stack configuration.remove_top_stack() else: # If transition is shift configuration.shift() # Perform shift operation return configuration
def apply(self, configuration: Configuration, transition: str) -> Configuration: """ ================================================================= Implement arc standard algorithm based on Incrementality in Deterministic Dependency Parsing(Nirve, 2004): Left-reduce Right-reduce Shift ================================================================= """ # TODO(Students) Start if transition[0] == 'L': head = configuration.get_stack(0) c = configuration.get_stack(1) label = transition[2:-1] configuration.remove_second_top_stack() configuration.add_arc(head, c, label) elif transition[0] == 'R': head = configuration.get_stack(1) c = configuration.get_stack(0) label = transition[2:-1] configuration.remove_top_stack() configuration.add_arc(head, c, label) else: configuration.shift() return configuration
def apply(self, configuration: Configuration, transition: str) -> Configuration: """ ================================================================= Implement arc standard algorithm based on Incrementality in Deterministic Dependency Parsing(Nirve, 2004): Left-reduce Right-reduce Shift ================================================================= """ # TODO(Students) Start # t implies the type of transitions: LEFT-ARC, RIGHT-ARC or SHIFT (Nirve, 2004) # If t is a shift transition, call shift() defined in configuration if transition.startswith("S"): configuration.shift() # If t is a type of arc transition, extract label from it and add arc else: # Extract label from t. The format of 't' ,For example: L(root) label = transition[2:-1] # Get first two elements from stack s0, s1 = configuration.get_stack(0), configuration.get_stack(1) # If left arc, add an arc s0 -> s1 with label and remove s1 from the stack if transition.startswith("L"): configuration.add_arc(s0, s1, label) configuration.remove_second_top_stack() # If right arc, add an arc s1 -> s0 with label and remove s0 from the stack else: configuration.add_arc(s1, s0, label) configuration.remove_top_stack() return configuration
def apply(self, configuration: Configuration, transition: str) -> Configuration: """ ================================================================= Implement arc standard algorithm based on Incrementality in Deterministic Dependency Parsing(Nirve, 2004): Left-reduce Right-reduce Shift ================================================================= """ # TODO(Students) Start # Get the second top element stack1 = configuration.get_stack(1) # Get the top element stack2 = configuration.get_stack(0) # If the transition is right-arc, then add the arc from the second top element of the stack to the top element # and remove the top element of the stack. if transition.startswith("R"): configuration.add_arc(stack1, stack2, transition[2:-1]) configuration.remove_top_stack() # If the transition is left-arc, then add the arc from the top element of the stack to the second top element # and remove the second top element of the stack. elif transition.startswith("L"): configuration.add_arc(stack2, stack1, transition[2:-1]) configuration.remove_second_top_stack() # If the transition is not left-arc or right-arc, perform shift action from the input buffer to the stack. else: configuration.shift() return configuration
def apply(self, configuration: Configuration, transition: str) -> Configuration: """ ================================================================= Implement arc standard algorithm based on Incrementality in Deterministic Dependency Parsing(Nirve, 2004): Left-reduce Right-reduce Shift ================================================================= """ # TODO(Students) Start # We first get the topmost and second topmost element of the stack wi = configuration.get_stack(0) wj = configuration.get_stack(1) # If the transition is a Right-Arc, then we create an arc from second topmost to topmost element # and pop the topmost element if transition.startswith('R'): configuration.add_arc(wj, wi, transition[2:-1]) configuration.remove_top_stack() # If the transition is a Left-Arc, then we create an arc from topmost to the second topmost element # and pop the second topmost element elif transition.startswith('L'): configuration.add_arc(wi, wj, transition[2:-1]) configuration.remove_second_top_stack() # Otherwise we just move the top of the buffer to top of the stack else: configuration.shift() return configuration
def apply(self, configuration: Configuration, transition: str) -> Configuration: """ ================================================================= Implement arc standard algorithm based on Incrementality in Deterministic Dependency Parsing(Nirve, 2004): Left-reduce Right-reduce Shift ================================================================= """ # TODO(Students) Start if transition.startswith("S"): configuration.shift() else: cur_label = transition[2:-1] # Get first two elements from stack w0 = configuration.get_stack(0) w1 = configuration.get_stack(1) # If left arc, add an arc s0 -> s1 with label and remove s1 from the stack if transition.startswith("L"): configuration.add_arc(w0, w1, cur_label) configuration.remove_second_top_stack() # If right arc, add an arc s1 -> s0 with label and remove s0 from the stack else: configuration.add_arc(w1, w0, cur_label) configuration.remove_top_stack() return configuration
def apply(self, configuration: Configuration, transition: str) -> Configuration: """ ================================================================= Implement arc standard algorithm based on Incrementality in Deterministic Dependency Parsing(Nirve, 2004): Left-reduce Right-reduce Shift ================================================================= """ # TODO(Students) Start label = transition[2:-1] arc = transition[0] # print(label, arc) # LEFT, RIGHT OR SHIFT? if arc == 'S': if len(configuration.buffer) >= 1: configuration.shift() elif arc == 'L': if len(configuration.stack) >= 2: s1 = configuration.get_stack(0) s2 = configuration.get_stack(1) configuration.add_arc(s1, s2, label) configuration.remove_second_top_stack() elif arc == 'R': if len(configuration.stack) >= 2: s1 = configuration.get_stack(0) s2 = configuration.get_stack(1) configuration.add_arc(s2, s1, label) configuration.remove_top_stack() return configuration
def apply(self, configuration: Configuration, transition: str) -> Configuration: """ ================================================================= Implement arc standard algorithm based on Incrementality in Deterministic Dependency Parsing(Nirve, 2004): Left-reduce Right-reduce Shift ================================================================= """ # TODO(Students) Start #The buffer has words represented from 1 to n+1. Hence Stack will also have the same at any point #We will have to however map those back to words and then to word index #Ask Matt: should I use index mapping for the stack words? #Ask Matt: Is the last argument right in add_arc or should it be index as well? #Last argument can stay as an index for now becase it calls tree.set and refer tree.add for dtype if (transition[0:2] == 'L('): #Left Reduce #config.get_stack(0), config.get_stack(1) configuration.add_arc(configuration.get_stack(0), configuration.get_stack(1), transition[2:-1]) configuration.remove_second_top_stack() elif (transition[0:2] == 'R('): #Right Reduce configuration.add_arc(configuration.get_stack(1), configuration.get_stack(0), transition[2:-1]) configuration.remove_top_stack() else: #Shift configuration.shift() # TODO(Students) End return configuration
def apply(self, configuration: Configuration, transition: str) -> Configuration: """ ================================================================= Implement arc standard algorithm based on Incrementality in Deterministic Dependency Parsing(Nirve, 2004): Left-reduce Right-reduce Shift ================================================================= """ # TODO(Students) Start operand1 = configuration.get_stack(0) operand2 = configuration.get_stack(1) label = transition[2:-1] # left reduce operation. if transition.startswith("L"): configuration.add_arc(operand1, operand2, label) check = configuration.remove_second_top_stack() if not check: print("Only one element in the stack!") # right reduce operation. elif transition.startswith("R"): configuration.add_arc(operand2, operand1, label) check = configuration.remove_top_stack() if not check: print("Stack is empty!") # shift operation. elif transition == "S": configuration.shift() else: pass return configuration