def get_sisters(node: ParentedTree) -> List[ParentedTree]: sisters = [] # root node does not have sisters if node.parent() is None: return sisters for child in get_children(node.parent()): if child == node: continue sisters.append(child) return sisters
def __iterate(tree: ParentedTree): # the index of the current node parent_index = tree.label().split('|')[0] # if this is the root node, yield a index -> 0 relation if not tree.parent(): yield parent_index, 0 if not _is_leaf(tree): for subtree in tree: index = subtree.label().split('|')[0] if index != parent_index: yield index, parent_index for _item in __iterate(subtree): yield _item
def getTag(senArr, root): return senArr[int(root.label())][3] if __name__ == "__main__": senArr_eg = [ "1 I _ NN NN _ 2 _ _ _", "2 love _ VB VB _ 0 _ _ _", "3 to _ TO TO _ 4 _ _ _", "4 eat _ VB VB _ 2 _ _ _", "5 cabbage _ NN NN _ 4 _ _ _" ] print "****input_example:" print "\n".join(senArr_eg) senArr_eg = [line.split("\t") for line in senArr_eg] root = conll2tree(senArr_eg) print "****tree", root print "****height", root.height() print "root.label", root.label() print root[0] children_of_root = getChildren(root) for child in children_of_root: print "*** child of root", child.label() print "tree position", ParentedTree.treeposition(child) print "parent", ParentedTree.parent(child) print "parent_idx", ParentedTree.parent_index(child) print root[ParentedTree.parent_index(child)]
def getTag(senArr, root): return senArr[int(root.label())][3] if __name__ == "__main__": senArr_eg = ["1 I _ NN NN _ 2 _ _ _", "2 love _ VB VB _ 0 _ _ _", "3 to _ TO TO _ 4 _ _ _", "4 eat _ VB VB _ 2 _ _ _", "5 cabbage _ NN NN _ 4 _ _ _"] print "****input_example:" print "\n".join(senArr_eg) senArr_eg = [line.split("\t") for line in senArr_eg] root = conll2tree(senArr_eg) print "****tree", root print "****height", root.height() print "root.label", root.label() print root[0] children_of_root = getChildren(root) for child in children_of_root: print "*** child of root", child.label() print "tree position", ParentedTree.treeposition(child) print "parent", ParentedTree.parent(child) print "parent_idx", ParentedTree.parent_index(child) print root[ParentedTree.parent_index(child)]