Team Ai
Apppublic

CVPR/v-doc_abstractive_mac

sourceHugging Faceupdated 4y agoView on Hugging Face
4likes
program_translator.py105 linesDownload Raw Back to root
1 2class ProgramTranslator(object):3    def __init__(self, programDict, maxArity):4        self.programDict = programDict5        self.maxArity = maxArity6 7        self.maxStack = 08 9    def functionToKey(self, function, withValInputs = True):10        valInputs = ""11        if withValInputs:12            valInputs = "_" + ",".join(function["value_inputs"])13        functionKey = function["function"] if "_" in function["function"] else \14                      "_".join([function["function"], function["function"]])15        return str(len(function["inputs"])) + "_" + functionKey + valInputs16 17    def typeToKey(self, function, withValInputs = True):18        valInputs = ""19        if withValInputs:20            valInputs = "_" + ",".join(function["value_inputs"])21        functionKey = function["type"] if "_" in function["type"] else \22                      "_".join([function["type"], function["type"]])23        return str(len(function["inputs"])) + "_" + functionKey + valInputs24 25    def keyToFunction(self, key):26        assert key not in self.programDict.invalidSymbols27        function = {}28        parts = key.split("_")29        arity = int(parts[0])30        function["function"] = "_".join([parts[1], parts[2]])31        function["value_inputs"] = []32        if len(parts) == 4:33            function["value_inputs"] = parts[3].split(",")34        function["inputs"] = []35        return function, arity36    37    def keyToArity(self, key):38        if key in self.programDict.invalidSymbols:39            return 040        return int(key.split("_")[0])41 42    def keyToType(self, key):43        if key in self.programDict.invalidSymbols:44            return ["0", "0", "0"]45        return ["0:" + key.split("_")[0], "1:" + key.split("_")[1], "2:" + key.split("_")[2]]46 47    def programToPostfixProgram(self, program):48        newProgram = []49        50        def programToPostfixAux(currIndex = -1):51            childrenIndices = program[currIndex]["inputs"]52            #[int(child) for child in program[currIndex]["inputs"]]53            childrenNewIndices = []54            for child in childrenIndices:55                programToPostfixAux(child)56                childrenNewIndices.append(len(newProgram) - 1)57            program[currIndex]["inputs"] = childrenNewIndices58            newProgram.append(program[currIndex])59        60        programToPostfixAux()61        return newProgram62 63    def programToSeq(self, program):64        return [self.functionToKey(function) for function in program]65 66    def pdfProgramToSeq(self, program):67        return [self.typeToKey(function) for function in program]68 69    def programToInputs(self, program, offset = 0):70        inputs = [function["inputs"] for function in program]71        offsetedInputs = [[FuncInput + offset for FuncInput in FuncInputs] for FuncInputs in inputs]72        return offsetedInputs73 74    # def seqToProgram(self, seq, enforceValidPrograms = True):75    #     program = []76 77    #     def seqToProgramAux(currIndex = len(seq) - 1):78    #         if currIndex < 0:79    #             program = None80    #             return81    #         currFunc, arity = self.keyToFunction(seq[currIndex])82    #         nextIndex = currIndex - 183    #         program.append(currFunc)84    #         for _ in arity:85    #             currFunc["inputs"].append(nextIndex)86    #             nextIndex = seqToProgramAux(nextIndex)87    #         currFunc["inputs"].reverse()88    #         return nextIndex89 90    #     if enforceValidPrograms:91    #         seqToProgramAux()92    #         if program is not None:93    #             program.reverse()94    #     else:95    #         stack = [0] * self.maxArity96    #         for i in range(len(seq)):97    #             func, arity = self.keyToFunction(seq[i])98    #             func["inputs"] = stack[len(stack) - arity:]99    #             newLength = max(len(stack) - arity, self.maxArity)100    #             stack = stack[:newLength] + [i + self.maxArity]101    #             self.maxStack = max(len(stack), self.maxStack)102    #             program.append(func)103 104    #     return program105