本文整理汇总了Python中nltk.tree.Tree.cn方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.cn方法的具体用法?Python Tree.cn怎么用?Python Tree.cn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.tree.Tree
的用法示例。
在下文中一共展示了Tree.cn方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: init_tree
# 需要导入模块: from nltk.tree import Tree [as 别名]
# 或者: from nltk.tree.Tree import cn [as 别名]
def init_tree(tree, bf, cn=0):
# if it is a tree, might not be non terminal...
if type(tree) == nltk.tree.Tree:
# check for correct branching factor
if len(tree) == bf:
# check for errors (False return) and recurse up to discard entire tree
children = []
for i, t in enumerate(tree):
initialized_tree = init_tree(t, bf, cn * bf + i)
if initialized_tree is not False:
children.append(initialized_tree)
else:
return False
out = Tree(tree.node, children)
out.span = (out[0].span[0], out[-1].span[1])
# if tree & len = 0, is a terminal node (if not NoneType node)
elif len(tree) == 0:
out = Tree(tree.node, [])
try:
out.span = (cn * len(out.node), (cn + 1) * len(out.node) - 1)
except TypeError:
print "non-terminal ERROR: NoneType node encountered, tree discarded from training set."
return False
# if not a terminal node or of correct branching factor, discard
else:
print "non-terminal ERROR: wrong branching factor, tree discarded from training set."
return False
# if it is not a tree and not None, assume it is a terminal node array, wrap in tree
elif tree is not None:
out = Tree(tree, [])
out.span = ((cn - 1) * len(out.node), cn * len(out.node) - 1)
# if None though, discard
else:
print "non-terminal ERROR: terminal node " + str(cn) + " is NoneType, tree discarded from training set."
return False
out.cn = cn
return out