Example #1
0
    def execComposePart(self, pp, cp):
        parClustIdx = pp._clustIdx
        chdClustIdx = cp._clustIdx
        pcl = Clust.getClust(parClustIdx)
        ccl = Clust.getClust(chdClustIdx)
        dep = pp.getArguments()[cp._parArgIdx]._path.getDep()
        pp._relTreeRoot.addChild(dep, cp._relTreeRoot)
        nrti = RelType.getRelType(pp._relTreeRoot)

        ncl = Clust.getClust(next(iter(Clust.getClustsWithRelType(nrti))))
        nci = ncl.getId()

        pp.removeArgument(cp._parArgIdx, clust_only=True)

        for argIdx, arg in pp.getArguments().items():
            pp.unsetArgClust(argIdx)
            arg._argPart.unsetParent()

        pp.changeClust(nci, nrti, clust_only=True)

        for argIdx, arg in pp.getArguments().items():
            ati = arg._path.getArgType()
            aci = -1

            if ati not in ncl._argTypeIdx_argClustIdxs:
                aci = ncl.createArgClust(ati)
            elif len(ncl._argTypeIdx_argClustIdxs[ati]) == 0:
                aci = ncl.createArgClust(ati)
            else:
                aci = next(iter(ncl._argTypeIdx_argClustIdxs[ati]))

            arg._argPart.setParent(pp, argIdx)
            pp.setArgClustOnly(argIdx, aci)

        pp.setRelTypeIdx(nrti)

        for argIdx, arg in cp.getArguments():
            ati = arg._path.getArgType()
            aci = -1

            if ati not in ncl._argTypeIdx_argClustIdxs:
                aci = ncl.createArgClust(ati)
            elif len(ncl._argTypeIdx_argClustIdxs[ati]) == 0:
                aci = ncl.createArgClust(ati)
            else:
                aci = next(iter(ncl._argTypeIdx_argClustIdxs[ati]))

            cp.unsetArgClustOnly(argIdx)
            pp.setArgClust(pp.addArgument(arg), aci)
            arg._argPart.setParent(pp, pp.addArgument(arg))

        cp.destroy()

        return None
Example #2
0
    def part_from_node(ai, sj, sent, k, tok):
        if not Parse.isIgnore(sent, k):
            tn = TreeNode(genTreeNodeID(ai, sj, k), tok)
            part = Part(tn)
            relTypeIdx = part.getRelTypeIdx()
            clustIdxs = Clust.getClustsWithRelType(relTypeIdx)

            if clustIdxs is not None:
                clustIdx = next(iter(clustIdxs))
            else:
                clustIdx = Clust.createClust(relTypeIdx)

            part.setClust(clustIdx)

        return None
Example #3
0
    def scoreOpComposePart(self, pp, cp):
        score = 0
        rcl, acl = [Clust.getClust(x._clustIdx) for x in (pp, cp)]

        ptn, ctn = [x._relTreeRoot for x in (pp, cp)]
        ptn.addChild(dep, ctn)
        nrti = RelType.getRelType(ptn)

        if Clust.getClustsWithRelType(nrti) is None:
            return score

        pai = cp._parArgIdx
        pcarg = pp.getArguments()[pai]
        dep = pcarg._path.getDep()
        orti = pp._relTypeIdx

        ncl = Clust.getClust(Clust.getClustsWithRelType(nrti).next())
        nci = ncl._clustIdx

        if pp.getParPart() is not None:
            ppp = pp.getParPart()
            ppcl = Clust.getClust(ppp.getClustIdx())
            ac = ppcl._argClusts[ppp.getArgClust(pp.getParArgIdx())]
            oc = ac._chdClustIdx_cnt[rcl._clustIdx]
            nc = ac._chdClustIdx_cnt[nci]
        else:
            oc = Clust.clustIdx_rootCnt[rcl]
            nc = Clust.clustIdx_rootCnt[ncl]

        score += log(nc) - log(oc)

        for aci, ais in pp._argClustIdx_argIdxs.items():
            ac = rcl._argClusts[aci]
            score -= (log(ac._argNum_cnt[len(ais)]) - log(ac._ttlArgCnt))

            for ai in ais:
                arg = pp.getArgument(ai)
                score -= (log(ac._chdClustIdx_cnt[arg._argPart._clustIdx]) \
                        - log(ac._ttlArgCnt))
                score -= (log(ac._argTypeIdx_cnt[arg._path.getArgType()]) \
                        - log(ac._ttlArgCnt))

        ai_newaci = dict()

        for ai, arg in pp._args.items():
            if ai == pai:
                pass
            else:
                ati = arg._path.getArgType()
                aci = ncl._argTypeIdx_argClustIdxs[ati].next()
                ai_newaci[ai] = aci

        newArgClustIdx_ais = dict()

        for ai, aci in ai_newaci.items():
            if aci not in newArgClustIdx_ais:
                newArgClustIdx_ais[aci] = set()

            newArgClustIdx_ais[aci].add(ais)

        for aci, ais in newArgClustIdx_ais.items():
            ac = ncl._argClusts[aci]
            score += (log(ac._argNum_cnt[len(ais)]) - log(ac._ttlArgCnt))

            for ai in ais:
                arg = pp.getArgument(ai)
                score -= (log(ac._chdClustIdx_cnt[arg._argPart._clustIdx]) \
                        - log(ac._ttlArgCnt))
                score -= (log(ac._argTypeIdx_cnt[arg._path.getArgType()]) \
                        - log(ac._ttlArgCnt))

        return score
Example #4
0
    def execCompose(self, op):
        parClustIdx = op._parClustIdx
        chdClustIdx = op._chdClustIdx
        new_clust_id = -1

        #
        # If either cluster are None, return -1
        #
        if Clust.getClust(parClustIdx) is None or Clust.getClust(
                chdClustIdx) is None:
            return -1

        new_clust = None
        parent_child_pair = (parClustIdx, chdClustIdx)
        part_ids = set()
        part_ids.update(
            Part.pairClustIdxs_pairPartRootNodeIds[parent_child_pair])

        deleted_parts = []

        for parent_id, child_id in part_ids:
            if parent_id in deleted_parts or child_id in deleted_parts:
                continue

            parent_part = Part.getPartByRootNodeId(parent_id)
            child_part = Part.getPartByRootNodeId(child_id)
            dep = parent_part.getArguments()[
                child_part._parArgIdx]._path.getDep()
            parent_part._relTreeRoot.addChild(dep, child_part._relTreeRoot)
            nrti = RelType.getRelType(parent_part._relTreeRoot)

            if new_clust is None:
                # on first loop
                rel_clusts = Clust.getClustsWithRelType(nrti)
                if rel_clusts is None:
                    new_clust = Clust.getClust(Clust.createClust(nrti))
                elif len(rel_clusts) > 1:
                    raise Exception
                else:
                    new_clust = Clust.getClust(next(iter(rel_clusts)))

                new_clust_id = new_clust.getId()

            parent_part.removeArgument(child_part._parArgIdx)

            if parent_part.getClustIdx() != new_clust_id:
                for argIdx in parent_part.getArguments():
                    parent_part.unsetArgClust(argIdx)
                    arg = parent_part.getArgument(argIdx)
                    arg._argPart.unsetParent()

                parent_part.changeClust(new_clust_id, nrti)

                for argIdx, arg in parent_part.getArguments().items():
                    arg_type = arg._path.getArgType()
                    arg_clust_id = -1

                    if arg_type not in new_clust._argTypeIdx_argClustIdxs:
                        arg_clust_id = new_clust.createArgClust(arg_type)
                    elif len(
                            new_clust._argTypeIdx_argClustIdxs[arg_type]) == 0:
                        arg_clust_id = new_clust.createArgClust(arg_type)
                    else:
                        arg_clust_id = next(
                            iter(new_clust._argTypeIdx_argClustIdxs[arg_type]))

                    arg._argPart.setParent(parent_part, argIdx)
                    parent_part.setArgClust(argIdx, arg_clust_id)

                parent_part.setRelTypeIdx(nrti)
            else:
                parent_part.unsetRelTypeIdx()
                parent_part.setRelTypeIdx(nrti)

            #
            # Connect the child part's arguments directly to the parent part now
            #

            for argIdx, arg in child_part.getArguments().items():
                child_part.unsetArgClust(argIdx)
                arg_type = arg._path.getArgType()
                arg_clust_id = -1

                if arg_type not in new_clust._argTypeIdx_argClustIdxs:
                    arg_clust_id = new_clust.createArgClust(arg_type)
                elif len(new_clust._argTypeIdx_argClustIdxs[arg_type]) == 0:
                    arg_clust_id = new_clust.createArgClust(arg_type)
                else:
                    arg_clust_id = next(
                        iter(new_clust._argTypeIdx_argClustIdxs[arg_type]))

                newArgIdx = parent_part.addArgument(arg)
                arg._argPart.setParent(parent_part, newArgIdx)
                parent_part.setArgClust(newArgIdx, arg_clust_id)

            #
            # Remove the old child part
            #

            deleted_parts.append(child_part.getRelTreeRoot().getId())
            child_part.destroy()

        # Part.clustIdx_pairClustIdxs[parClustIdx].remove(pci)
        # Part.clustIdx_pairClustIdxs[chdClustIdx].remove(pci)
        del Part.pairClustIdxs_pairPartRootNodeIds[parent_child_pair]

        return new_clust_id