本文整理汇总了Python中tree.Tree.relative_position方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.relative_position方法的具体用法?Python Tree.relative_position怎么用?Python Tree.relative_position使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree.Tree
的用法示例。
在下文中一共展示了Tree.relative_position方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: print_features
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import relative_position [as 别名]
def print_features(self, relation, which, to_file, prev=None):
conn_leaves = relation.conn_leaves
conn_str = '_'.join(n.value for n in conn_leaves)
conn_node = Tree.find_least_common_ancestor(conn_leaves, with_leaves=True)
if conn_node is None:
return []
curr_root = conn_node.goto_tree().root
if prev is not None:
candidates = [(prev, 'None')]
else:
candidates = self.pruning(conn_node, relation, which)
for node, label in candidates:
to_file_line = ''
to_file_line += 'conn:'+conn_str+' '
to_file_line += 'conn_lc:'+conn_str.lower()+' '
to_file_line += 'nt_cat:'+node.value
if node.parent_node is not None:
to_file_line += '^nt_pt:' + node.parent_node.value
curr_idx = node.parent_node.child_nodes.index(node)
if curr_idx > 0:
to_file_line += '^nt_lsib:' + node.parent_node.child_nodes[curr_idx - 1].value
else:
to_file_line += '^nt_lsib:NULL'
if curr_idx < len(node.parent_node.child_nodes) - 1:
to_file_line += '^nt_rsib:' + node.parent_node.child_nodes[curr_idx + 1].value
else:
to_file_line += '^nt_rsib:NULL'
else:
to_file_line += '^nt_pt:NULL'
to_file_line += ' '
node_root = node.goto_tree().root
if curr_root != node_root:
path, _ = Tree.find_constituent_path(conn_node, curr_root)
path += '->_<cross>_->' + node_root.value
else:
path, _ = Tree.find_constituent_path(conn_node, node)
relpos = Tree.relative_position(conn_node, node)
to_file_line += 'conn_to_node:'+path+' '
lsibs = conn_node.all_left_siblings()
rsibs = conn_node.all_right_siblings()
to_file_line += 'conn_node_lsib_size='+str(len(lsibs))+' '
to_file_line += 'conn_node_rsib_size='+str(len(rsibs))+' '
if len(lsibs) > 1 :
to_file_line += 'conn_to_node:'+path+'^conn_node_lsib_size:>1 '
to_file_line += 'conn_to_node_relpos:'+relpos+' '
to_file.write('%s %s\n' % (to_file_line, label))
return [c[0] for c in candidates]