当前位置: 首页>>代码示例>>Python>>正文


Python Tree.cn方法代码示例

本文整理汇总了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
开发者ID:ajratner,项目名称:patent-similarity,代码行数:46,代码来源:uRAE_lib.py


注:本文中的nltk.tree.Tree.cn方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。