def parse_ml(stack, queue, graph, trans): try: if stack and trans[:2] == 'ra': queue[0]['deprel'] = trans[3:] queue[0]['head'] = stack[0]['id'] stack, queue, graph = transition.right_arc(stack, queue, graph, trans[3:]) return stack, queue, graph, 'ra' if stack and trans[:2] == 'la': stack[0]['deprel'] = trans[3:] stack[0]['head'] = queue[0]['id'] stack, queue, graph = transition.left_arc(stack, queue, graph, trans[3:]) return stack, queue, graph, 'la' if stack and trans[:2] == 're': stack[0]['head'] = queue[0]['id'] stack, queue, graph = transition.reduce(stack, queue, graph) return stack, queue, graph, 're' except Exception: stack, queue, graph = transition.shift(stack, queue, graph) return stack, queue, graph, 'sh' stack, queue, graph = transition.shift(stack, queue, graph) return stack, queue, graph, 'sh'
def reference(stack, queue, graph): """ Gold standard parsing Produces a sequence of transitions from a manually-annotated corpus: sh, re, ra.deprel, la.deprel :param stack: The stack :param queue: The input list :param graph: The set of relations already parsed :return: the transition and the grammatical function (deprel) in the form of transition.deprel """ # Right arc if stack and stack[0]['id'] == queue[0]['head']: # print('ra', queue[0]['deprel'], stack[0]['cpostag'], queue[0]['cpostag']) deprel = '.' + queue[0]['deprel'] stack, queue, graph = transition.right_arc(stack, queue, graph) return stack, queue, graph, 'ra' + deprel # Left arc if stack and queue[0]['id'] == stack[0]['head']: # print('la', stack[0]['deprel'], stack[0]['cpostag'], queue[0]['cpostag']) deprel = '.' + stack[0]['deprel'] stack, queue, graph = transition.left_arc(stack, queue, graph) return stack, queue, graph, 'la' + deprel # Reduce if stack and transition.can_reduce(stack, graph): for word in stack: if (word['id'] == queue[0]['head'] or word['head'] == queue[0]['id']): # print('re', stack[0]['cpostag'], queue[0]['cpostag']) stack, queue, graph = transition.reduce(stack, queue, graph) return stack, queue, graph, 're' # Shift # print('sh', [], queue[0]['cpostag']) stack, queue, graph = transition.shift(stack, queue, graph) return stack, queue, graph, 'sh'
def parse_ml(stack, queue, graph, trans): if stack and trans[:2] == 'ra' and transition.can_rightarc(stack): stack, queue, graph = transition.right_arc(stack, queue, graph, trans[3:]) return stack, queue, graph, 'ra' elif stack and trans[:2] == 'la' and transition.can_leftarc(stack, graph): stack, queue, graph = transition.left_arc(stack, queue, graph, trans[3:]) return stack, queue, graph, 'la' elif stack and trans[:2] == 're' and transition.can_reduce(stack, graph): stack, queue, graph = transition.reduce(stack, queue, graph) return stack, queue, graph, 're' elif stack and trans[:2] == 'sh': stack, queue, graph = transition.shift(stack, queue, graph) return stack, queue, graph, 'sh' else: stack, queue, graph = transition.shift(stack, queue, graph) return stack, queue, graph, 'sh'
def process(stack, queue, graph, trans): if trans[:2] == 'ra': return transition.right_arc(stack, queue, graph) elif trans[:2] == 'la': return transition.left_arc(stack, queue, graph) elif trans[:2] == 're': return transition.reduce(stack, queue, graph) elif trans[:2] == 'sh': return transition.shift(stack, queue, graph)
def reference(stack, queue, graph): """ Gold standard parsing Produces a sequence of transitions from a manually-annotated corpus: sh, re, ra.deprel, la.deprel :param stack: The stack :param queue: The input list :param graph: The set of relations already parsed :return: the transition and the grammatical function (deprel) in the form of transition.deprel """ # This is a continuous list of if statements, but each has a return statement inside of it, so it behaves similar to # a set of elif statements. However, there is a priority level to these operations, # 1) Right-arc, 2) Left-arc, 3) Reduce, 4) Shift # Right arc if stack and stack[0]['id'] == queue[0]['head']: # If stack is non-null and # the top of the stack's id key is the same as the front of the queue's head key are the same # Then we do a right-arc # print('ra', queue[0]['deprel'], stack[0]['cpostag'], queue[0]['cpostag']) deprel = '.' + queue[0]['deprel'] stack, queue, graph = transition.right_arc(stack, queue, graph) return stack, queue, graph, 'ra' + deprel # Left arc if stack and queue[0]['id'] == stack[0]['head']: # If the stack is non-null AND # the front of the queue's ID is the same as the top of the stack's head value # Then we do a left-arc # print('la', stack[0]['deprel'], stack[0]['cpostag'], queue[0]['cpostag']) deprel = '.' + stack[0]['deprel'] stack, queue, graph = transition.left_arc(stack, queue, graph) return stack, queue, graph, 'la' + deprel # Reduce if stack and transition.can_reduce(stack, graph): # If the stack is non-null and the stack can be reduced, then for word in stack: # For each word present in the stack if (word['id'] == queue[0]['head'] or # If the word's ID is the same as the front of the queue's HEAD value word['head'] == queue[0]['id']): # OR the word's HEAD value matches the front of the queue's ID value # print('re', stack[0]['cpostag'], queue[0]['cpostag']) # Then we can reduce the value that is on the stack stack, queue, graph = transition.reduce(stack, queue, graph) return stack, queue, graph, 're' # Shift # We will only get here if none of the other operations did NOT occur # This is because each of the if statements for the operations has a return statement # Stack was pushed to, queue had front removed, and graph is unmodified by shift actions stack, queue, graph = transition.shift(stack, queue, graph) return stack, queue, graph, 'sh'
def parse_ml(stack, queue, graph, trans): #right arc if stack and trans[:2] == 'ra' and transition.can_rightarc(stack): stack, queue, graph = transition.right_arc(stack, queue, graph, trans[3:]) return stack, queue, graph, 'ra' #left arc if stack and trans[:2] == 'la' and transition.can_leftarc(stack, graph): stack, queue, graph = transition.left_arc(stack, queue, graph, trans[3:]) return stack, queue, graph, 'la' #reduce if stack and trans[:2] == 're' and transition.can_reduce(stack, graph): stack, queue, graph = transition.reduce(stack, queue, graph) return stack, queue, graph, 're' #shift if stack and trans[:2] == 'sh': stack, queue, graph = transition.shift(stack, queue, graph) return stack, queue, graph, 'sh' #action not possible -> shift else: stack, queue, graph = transition.shift(stack, queue, graph) return stack, queue, graph, 'sh'
def execute_transition(stack, queue, graph, trans): if stack and trans[:2] == 'ra': stack, queue, graph = transition.right_arc(stack, queue, graph, trans[3:]) return stack, queue, graph, 'ra' if stack and trans[:2] == 'la': stack, queue, graph = transition.left_arc(stack, queue, graph, trans[3:]) return stack, queue, graph, 'la' if stack and trans == 're': stack, queue, graph = transition.reduce(stack, queue, graph) return stack, queue, graph, 're' stack, queue, graph = transition.shift(stack, queue, graph) return stack, queue, graph, 'sh'
def parse_ml(stack, queue, graph, trans): if stack and trans[:2] == 'la': stack, queue, graph = transition.left_arc(stack, queue, graph, trans[3:]) return stack, queue, graph, 'la' elif stack and trans[:2] == 'ra': stack, queue, graph = transition.right_arc(stack, queue, graph, trans[3:]) return stack, queue, graph, 'ra' elif stack and trans == 're': stack, queue, graph = transition.reduce(stack, queue, graph) return stack, queue, graph, 're' elif trans == 'sh': stack, queue, graph = transition.shift(stack, queue, graph) return stack, queue, graph, 'sh' else: print("parsing failed", file=sys.stderr)
def parse_ml(stack, queue, graph, predicted_transition): if stack and predicted_transition[0][:2] == 'ra': stack, queue, graph = transition.right_arc(stack, queue, graph, predicted_transition[0][3:]) return stack, queue, graph, 'ra' if stack and predicted_transition[0][:2] == 'la': stack, queue, graph = transition.left_arc(stack, queue, graph, predicted_transition[0][3:]) return stack, queue, graph, 'la' if stack and predicted_transition[0] == 're': stack, queue, graph = transition.reduce(stack, queue, graph) return stack, queue, graph, 're' stack, queue, graph = transition.shift(stack, queue, graph) return stack, queue, graph, 'sh'
def parse_ml(stack, queue, graph, trans): #print(trans) if stack and transition.can_rightarc(stack) and trans[:2] == 'ra': stack, queue, graph = transition.right_arc(stack, queue, graph, trans[3:]) return stack, queue, graph, 'ra' elif stack and transition.can_leftarc( stack, graph) and trans[:2] == 'la': #VARFÖR :2 stack, queue, graph = transition.left_arc(stack, queue, graph, trans[3:]) return stack, queue, graph, 'la' elif stack and transition.can_reduce(stack, graph) and trans[:2] == 're': stack, queue, graph = transition.reduce(stack, queue, graph) return stack, queue, graph, 're' else: stack, queue, graph = transition.shift(stack, queue, graph) return stack, queue, graph, "sh"
def parse_ml(stack, queue, graph, trans): if stack and trans[:2] == 'ra': stack, queue, graph = transition.right_arc(stack, queue, graph, trans[3:]) return stack, queue, graph, 'ra' if trans[:2] == 'la': stack, queue, graph = transition.left_arc(stack, queue, graph, trans[3:]) return stack, queue, graph, 'la' if trans[:2] == 're': stack, queue, graph = transition.reduce(stack, queue, graph) return stack, queue, graph, 're' if trans[:2] == 'sh': stack, queue, graph = transition.shift(stack, queue, graph) return stack, queue, graph, 'sh'
def parse_ml(stack, queue, graph, trans): if stack and trans[:2] == 'ra': stack, queue, graph = transition.right_arc(stack, queue, graph, trans[3:]) return stack, queue, graph, 'ra' if stack and trans[:2] == 'la': stack, queue, graph = transition.left_arc(stack, queue, graph, trans[3:]) return stack, queue, graph, 'la' if trans == 're': stack, queue, graph = transition.reduce(stack, queue, graph) return stack, queue, graph, 're' if trans == 'sh': stack, queue, graph = transition.shift(stack, queue, graph) return stack, queue, graph, 'sh' print(trans, "is not a valid action") return None