def getparse(tree, parse): """ Get parse tree :type tree: SpanNode instance :param tree: an binary RST tree :type parse: string :param parse: parse tree in string format """ if (tree.lnode is None) and (tree.rnode is None): # Leaf node parse += " ( EDU " + str(tree.nucedu) else: parse += " ( " + tree.form # get the relation from its satellite node if tree.form == 'NN': parse += "-" + extractrelation(tree.rnode.relation) elif tree.form == 'NS': parse += "-" + extractrelation(tree.rnode.relation) elif tree.form == 'SN': parse += "-" + extractrelation(tree.lnode.relation) else: raise ValueError("Unrecognized N-S form") # print tree.relation if tree.lnode is not None: parse = getparse(tree.lnode, parse) if tree.rnode is not None: parse = getparse(tree.rnode, parse) parse += " ) " return parse
def bracketing(self): """ Generate brackets according an Binary RST tree """ nodelist = postorder_DFT(self.tree, []) nodelist.pop() # Remove the root node brackets = [] for node in nodelist: relation = extractrelation(node.relation) b = (node.eduspan, node.prop, relation) brackets.append(b) return brackets
def decodeSRaction(tree): """ Decoding Shift-reduce actions from an binary RST tree :type tree: SpanNode instance :param tree: an binary RST tree """ # Start decoding post_nodelist = postorder_DFT(tree, []) # print len(post_nodelist) actionlist = [] for node in post_nodelist: if (node.lnode is None) and (node.rnode is None): actionlist.append(('Shift', None, None)) elif (node.lnode is not None) and (node.rnode is not None): form = node.form if (form == 'NN') or (form == 'NS'): relation = extractrelation(node.rnode.relation) else: relation = extractrelation(node.lnode.relation) actionlist.append(('Reduce', form, relation)) else: raise ValueError("Can not decode Shift-Reduce action") return actionlist