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


Python Tree.span方法代码示例

本文整理汇总了Python中nltk.tree.Tree.span方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.span方法的具体用法?Python Tree.span怎么用?Python Tree.span使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在nltk.tree.Tree的用法示例。


在下文中一共展示了Tree.span方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: unfolded_decoding

# 需要导入模块: from nltk.tree import Tree [as 别名]
# 或者: from nltk.tree.Tree import span [as 别名]
def unfolded_decoding(W_d, b_d, tree, encoded):
    (n, m) = W_d.shape

    # store all a_e results in tree structure
    decoding_tree = Tree(encoded, [])
    try:
        decoding_tree.span = tree.span
    except:
        pass

    # if the given node (root) has children, decode the node's encoding, split it,
    # and use this as the children's encoding (output) to recurse back, until terminal
    # nodes are reached
    if type(tree) == nltk.tree.Tree and len(tree) > 0:
        decoded = decode(W_d, b_d, encoded)
        for i, child in enumerate(tree):

            # NOTE: the number of branchings n is NOT assumed, but that it is uniform and that
            # len(input layer) = n*len(encoding) IS assumed
            full_decoded = unfolded_decoding(W_d, b_d, child, decoded[i * m : m + (i * m)])
            decoding_tree.append(full_decoded)
        return decoding_tree
    else:
        decoding_tree = Tree(encoded, [])
        try:
            decoding_tree.span = tree.span
        except:
            pass
        return decoding_tree
开发者ID:ajratner,项目名称:patent-similarity,代码行数:31,代码来源:uRAE_lib.py

示例2: init_tree

# 需要导入模块: from nltk.tree import Tree [as 别名]
# 或者: from nltk.tree.Tree import span [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.span方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。