causal_grammar.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:Causality 作者: vcla 项目源码 文件源码
def generate_parses(causal_tree):
    node_type = causal_tree["node_type"]
    if "children" not in causal_tree:
        return (causal_tree,)
    partial_causal_parses = []
    # make a copy of the current node, minus the children (so we're keeping symbol_type, symbol, energy, node_type, etc)
    current_node = causal_tree.copy()
    current_node.pop("children")
    if node_type in ("or","root",):
        for child_node in causal_tree["children"]:
            for parse in generate_parses(child_node):
                current_node["children"] = (parse,)
                partial_causal_parses.append(current_node.copy())
    elif node_type in ("and",):
        # generate causal parses on each tree
        # build all cartesian products of those causal parses;
        # each cartesian product is a set of children for the and node, a separate partial parse graph to return
        child_parses = []
        for child_node in causal_tree["children"]:
            child_parses.append(generate_parses(child_node),)
        for product in itertools.product(*child_parses):
            current_node["children"] = product
            partial_causal_parses.append(current_node.copy())
    else:
        raise Exception("UNKNOWN NODE TYPE: {}".format(node_type))
    return partial_causal_parses
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号