def parse_tree(sentence,parents,labels): nodes = {} parents = [p - 1 for p in parents] #change to zero based sentence=[w for w in sentence.strip().split()] for i in xrange(len(parents)): if i not in nodes: idx = i prev = None while True: node = tNode(idx) if prev is not None: assert prev.idx != node.idx node.add_child(prev) node.label = labels[idx] nodes[idx] = node if idx < len(sentence): node.word = sentence[idx] parent = parents[idx] if parent in nodes: assert len(nodes[parent].children) < 2 nodes[parent].add_child(node) break elif parent == -1: root = node break prev = node idx = parent return root
def parse_tree(sentence, parents): nodes = {} parents = [p - 1 for p in parents] # change to zero based sentence = [w for w in sentence.strip().split()] for i in range(len(parents)): if i not in nodes: idx = i prev = None while True: node = tNode(idx) if prev is not None: assert prev.idx != node.idx node.add_child(prev) nodes[idx] = node if idx < len(sentence): node.word = sentence[idx] parent = parents[idx] if parent in nodes: assert len(nodes[parent].children) < 2 nodes[parent].add_child(node) break elif parent == -1: root = node break prev = node idx = parent return root
def parse_tree(sentence, parents, labels): nodes = {} parents = [p - 1 for p in parents] # change to zero based sentence = [w for w in sentence.strip().split()] root = None for i in xrange(len(parents)): nodes[i] = tNode(i) for i in xrange(len(parents)): nodes[i].label = labels[i] if i < len(sentence): nodes[i].word = sentence[i] parent = parents[i] if parent != -1: nodes[parent].add_child(nodes[i]) nodes[i].add_parent(nodes[parent]) else: root = nodes[i] return root