Exemplo n.º 1
0
def match_copies_with_crds(dag: DAG) -> ProofNet:
    def get_copy_color(copy_: Node) -> str:
        incoming_ = set(map(lambda inc_: inc_.dep, dag.incoming(copy_)))
        assert len(incoming_) == 1
        return fst(list(incoming_))

    proof = set()

    copies = list(
        filter(
            lambda node: is_copy(dag, node) and not is_gap(
                dag, node, _head_deps), dag.nodes))
    copy_colors = list(map(get_copy_color, copies))
    copy_types = list(map(lambda copy: dag.attribs[copy]['type'], copies))

    conjunction_hierarchies: List[List[Tuple[Node, List[Optional[Node]]]]] = \
        list(map(lambda node: participating_conjunctions(dag, node), copies))

    proof = merge_proofs(
        proof,
        list(
            map(lambda ch, ct, cc: intra_crd_match(dag, ch, ct, cc),
                conjunction_hierarchies, copy_types, copy_colors)))

    crd_types = list(
        map(lambda ch: get_crd_type(dag, fst(fst(ch))),
            conjunction_hierarchies))
    results = list(map(last_instance_of, crd_types))
    matches = list(map(identify_missing, results, copy_types, copy_colors))
    return reduce(lambda proof_, pair: match(proof_, fst(pair), snd(pair)),
                  zip(copy_types, matches), proof)
Exemplo n.º 2
0
def match_copy_gaps_with_crds(dag: DAG) -> ProofNet:
    proof = set()

    gaps = list(
        filter(
            lambda node: is_copy(dag, node) and is_gap(dag, node, _head_deps),
            dag.nodes))
    gap_types = list(map(lambda node: dag.attribs[node]['type'], gaps))
    gap_colors = list(map(lambda type_: type_.argument.color, gap_types))
    gap_types = list(map(lambda type_: type_.argument.argument, gap_types))
    conjunction_hierarchies = list(
        map(
            lambda node: participating_conjunctions(
                dag, node, exclude_heads=True), gaps))

    proof = merge_proofs(
        proof,
        list(
            map(lambda ch, gt, gc: intra_crd_match(dag, ch, gt, gc),
                conjunction_hierarchies, gap_types, gap_colors)))

    crd_types = list(
        map(lambda ch: get_crd_type(dag, fst(fst(ch))),
            conjunction_hierarchies))
    results = list(map(last_instance_of, crd_types))
    matches = list(map(identify_missing, results, gap_types, gap_colors))
    return reduce(lambda proof_, pair: match(proof_, fst(pair), snd(pair)),
                  zip(gap_types, matches), proof)
Exemplo n.º 3
0
def add_ghost_nodes(dag: DAG[str, Any]) -> None:
    edges = dag.edges
    copy_edges = set(filter(lambda edge: is_copy(dag, edge.target), edges))
    gap_edges = set(filter(lambda edge: is_gap(dag, edge.target, HeadDeps), edges))
    copy_gap_edges = copy_edges.intersection(gap_edges)
    copy_edges = copy_edges - copy_gap_edges

    copy_gap_edges = set(filter(lambda edge: edge.dep not in HeadDeps, copy_gap_edges))

    copies_and_types = list(map(lambda copy: (copy, get_copy_annotation(dag, copy)), copy_edges.union(copy_gap_edges)))
    for c, t in copies_and_types:
        add_edge(dag, c, t)
Exemplo n.º 4
0
def match_copied_gaps_with_crds(dag: DAG[str, str]) -> ProofNet:
    def extract_color(type_: WordType) -> str:
        if isinstance(type_, FunctorType):
            if isinstance(type_.argument, DiamondType):
                return type_.argument.diamond
            if isinstance(type_.argument, BoxType):
                return type_.argument.box
        raise ProofError(f'Expected {type_} to be a higher order functor.')

    def extract_arg(type_: WordType) -> WordType:
        if isinstance(type_, FunctorType):
            if isinstance(type_.argument, DiamondType):
                return type_.argument.argument
            if isinstance(type_.argument, BoxType):
                return type_.argument.argument
        raise ProofError(f'Expected {type_} to be a higher order functor.')

    gaps = list(filter(lambda node:
                       is_copy(dag, node) and is_gap(dag, node, HeadDeps),
                       dag.nodes))
    gap_types: WordTypes = list(map(lambda node: dag.attribs[node]['type'], gaps))

    gap_colors = list(map(lambda gaptype: extract_color(gaptype), gap_types))
    gap_args = list(map(lambda gaptype: extract_arg(gaptype), gap_types))

    conj_hierarchies = list(map(lambda node:
                                participating_conjunctions(dag, node, exclude_heads=True),
                                gaps))

    proofs = list(map(lambda ch, ga, gc:
                      intra_crd_match(dag, ch, ga, gc),
                      conj_hierarchies,
                      gap_args,
                      gap_colors))

    crds = list(map(lambda ch:
                    get_crd_type(dag, fst(fst(ch))),
                    conj_hierarchies))

    results = list(map(lambda crd: last_instance_of(crd), crds))

    matches = list(map(lambda res, ga, gc:
                       identify_missing(res, ga, gc),
                       results,
                       gap_args,
                       gap_colors))

    return reduce(lambda proof_, pair:
                  match(proof_, fst(pair), snd(pair)),
                  zip(gap_args, matches),
                  merge_proofs(proofs))
Exemplo n.º 5
0
def add_ghost_nodes(dag: DAG) -> DAG:
    edges = dag.edges
    copies = set(filter(lambda edge: is_copy(dag, edge.target), edges))
    gaps = set(filter(lambda edge: is_gap(dag, edge.target, _head_deps),
                      edges))
    copy_gaps = copies.intersection(gaps)
    copies = list(copies - copy_gaps)

    # todo. assertions

    copy_gaps = set(filter(lambda edge: edge.dep not in _head_deps, copy_gaps))
    copy_gaps = list(copy_gaps)

    copy_types = list(map(lambda copy: get_copy_annotation(dag, copy), copies))
    gap_types = list(
        map(lambda copy: get_copy_annotation(dag, copy), copy_gaps))

    dag = reduce(lambda dag_, pair_: add_edge(dag_, fst(pair_), snd(pair_)),
                 zip(copies, copy_types), dag)
    dag = reduce(lambda dag_, pair_: add_edge(dag_, fst(pair_), snd(pair_)),
                 zip(copy_gaps, gap_types), dag)

    return dag
Exemplo n.º 6
0
 def is_gap_copy_parent(edge_: Edge) -> bool:
     return edge_.dep in _head_deps and is_copy(
         dag, edge_.target) and is_gap(dag, edge_.target, _head_deps)
Exemplo n.º 7
0
def annotate_simple_branch(dag: DAG,
                           parent: Node) -> Tuple[ProofNet, WordType]:
    def simplify_crd(crd_type: WordType,
                     arg_types_: List[WordType]) -> WordType:
        xs, result = isolate_xs(crd_type), last_instance_of(crd_type)

        if len(set(map(depolarize, arg_types))) > 1:
            raise ProofError('Non polymorphic conjunction.')

        if arg_types_ == xs:
            return crd_type
        else:
            xs, result = list(map(lambda x: x.result, xs)), result.result
            crd_type = reduce(
                lambda res_, arg_: ColoredType(arg_, res_, 'cnj'),
                reversed(xs), result)
            return simplify_crd(crd_type, arg_types_)
            # xs, result = list(map(get_functor_result, xs)), get_functor_result(result)
        # return reduce(lambda res_, arg_: ColoredType(arg_, res_, 'cnj'), reversed(xs), result)

    def is_gap_copy_parent(edge_: Edge) -> bool:
        return edge_.dep in _head_deps and is_copy(
            dag, edge_.target) and is_gap(dag, edge_.target, _head_deps)

    branch_proof = set()

    outgoing = dag.outgoing(parent)
    outgoing = set(
        filter(
            lambda edge: not is_copy(dag, edge.target) or is_gap_copy_parent(
                edge), outgoing))

    head = list(
        filter(
            lambda out: out.dep in _head_deps and dag.attribs[out.target][
                'type'] != placeholder, outgoing))
    if len(head) != 1:
        ToGraphViz()(dag)
        import pdb
        pdb.set_trace()
    head = fst(head)
    outgoing = list(filter(lambda out: out.dep not in _head_deps, outgoing))
    mods = list(filter(lambda out: out.dep in _mod_deps, outgoing))
    mods = order_nodes(dag, set(map(lambda out: out.target, mods)))
    args = list(filter(lambda out: out.dep not in _mod_deps, outgoing))
    sorted_args = order_nodes(dag, set(map(lambda out: out.target, args)))
    args = sorted(args, key=lambda x: sorted_args.index(x.target))
    arg_types = list(
        map(lambda out: get_simple_argument(dag, out.target), args))
    arg_deps = list(map(lambda out: out.dep, args))

    if dag.attribs[parent]['cat'] == 'conj':
        branch_output = simplify_crd(dag.attribs[head.target]['type'],
                                     arg_types)
    else:
        branch_output = get_simple_functor(dag, head.target)

    arg_proof, branch_output = align_args(branch_output, arg_types, arg_deps)
    mod_proof, branch_output = align_mods(
        branch_output,
        list(map(lambda node: get_simple_argument(dag, node), mods)))
    branch_proof = merge_proofs(branch_proof, (arg_proof, mod_proof))
    return branch_proof, branch_output
Exemplo n.º 8
0
 def is_gap_copy_parent(edge_: Edge[str, str]) -> bool:
     return edge_.dep in HeadDeps and is_copy(dag, edge_.target) and is_gap(dag, edge_.target, HeadDeps)
Exemplo n.º 9
0
def annotate_simple_branch(dag: DAG[str, str], parent: str) -> Tuple[ProofNet, WordType]:
    def simplify_crd(crd_type: WordType, arg_types_: WordTypes) -> WordType:
        xs = isolate_xs(crd_type)
        result = last_instance_of(crd_type)

        if len(set(map(depolarize, arg_types_))) > 1:
            raise ProofError('Non polymorphic conjunction.')

        if arg_types == xs:
            return crd_type
        else:
            xs = list(map(lambda x: x.result, xs))
            result = result.result

            crd_type = reduce(lambda res_, arg_:
                              DiamondType(argument=arg_, result=res_, diamond='cnj'),
                              reversed(xs),
                              result)
            return simplify_crd(crd_type, arg_types_)

    def is_gap_copy_parent(edge_: Edge[str, str]) -> bool:
        return edge_.dep in HeadDeps and is_copy(dag, edge_.target) and is_gap(dag, edge_.target, HeadDeps)

    outgoing = dag.outgoing(parent)
    outgoing = set(filter(lambda edge:
                          not is_copy(dag, edge.target) or is_gap_copy_parent(edge),
                          outgoing))

    heads = list(filter(lambda edge:
                        edge.dep in HeadDeps and dag.attribs[edge.target]['type'] != placeholder,
                        outgoing))

    if len(heads) != 1:
        raise ProofError(f'Too many heads: {heads}.')

    head = fst(heads)

    outgoing = set(filter(lambda edge:
                          edge.dep not in HeadDeps,
                          outgoing))
    modding_edges = list(filter(lambda edge:
                                edge.dep in ModDeps,
                                outgoing))
    arg_edges = list(filter(lambda edge:
                            edge.dep not in ModDeps,
                            outgoing))
    mods = order_nodes(dag, list(map(lambda edge: edge.target, modding_edges)))
    args = order_nodes(dag, list(map(lambda edge: edge.target, arg_edges)))
    arg_edges = sorted(arg_edges, key=lambda x: args.index(x.target))
    arg_types = list(map(lambda edge:
                         get_simple_argument(dag, edge.target),
                         arg_edges))
    arg_deps = list(map(lambda edge:
                        edge.dep,
                        arg_edges))

    if dag.attribs[parent]['cat'] == 'conj':
        branch_output = simplify_crd(dag.attribs[head.target]['type'], arg_types)
    else:
        branch_output = get_simple_functor(dag, head.target)

    arg_proof, branch_output = align_args(branch_output,
                                          arg_types,
                                          arg_deps)
    mod_proof, branch_output = align_mods(branch_output, list(map(lambda node: get_simple_argument(dag, node), mods)))
    return merge_proof(arg_proof, mod_proof), branch_output