Example #1
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 #2
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