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 element1 = configuration.get_stack(1) element2 = configuration.get_stack(0) if transition.startswith("L"): configuration.add_arc(element2, element1, transition[2:-1]) configuration.remove_second_top_stack() elif transition.startswith("R"): configuration.add_arc(element1, element2, transition[2:-1]) configuration.remove_top_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