A transcoding method, apparatus and electronic device
By constructing a code semantic network and utilizing pre-trained language models and similar conversion instances, the problems of time-consuming, labor-intensive, and inaccurate cross-language code conversion are solved, achieving efficient and accurate code conversion, ensuring the integrity and consistency of dependencies, and generating code that is easy to maintain.
Patent Information
- Application Number
- CN202411886058.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-20
- Publication Date
- 2025-10-24
- Estimated Expiration
- 2044-12-20
AI Technical Summary
Existing cross-language code conversion methods are time-consuming, labor-intensive, and prone to errors. Especially when dealing with complex code structures and specific business logic, the code generated by LLM may lack a deep understanding of the overall system, leading to functional deviations or performance degradation, which limits its reliability and effectiveness in practical applications.
A code semantic network is constructed to ensure the integrity and consistency of dependencies between code blocks during conversion through preorder traversal and dependency management. Combined with a pre-trained language model and similar conversion instances, the code conversion is dynamically adapted to the code conversion task, and the code semantic network and contextual information are used for code conversion.
It improves the accuracy and efficiency of code conversion, reduces the need for manual intervention, and generates target code with a clear structure and comments, which facilitates subsequent development and maintenance. It also enhances the ability to understand complex code blocks and ensures the integrity and consistency of dependencies.
Smart Images

Figure CN119806535B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of computer language compilation, and particularly relates to a code conversion method and device and electronic equipment. BACKGROUND
[0002] With the rapid development of information technology, the types and quantities of programming languages are increasing, and each language has its unique characteristics and application scenarios. Enterprises often need to migrate existing code from one programming language to another when upgrading, maintaining and refactoring systems. In this process, the demand for cross-language code conversion is growing. Traditional manual conversion methods are not only time-consuming and laborious, but also prone to errors, making it difficult to meet the development needs of large-scale and high efficiency. Although rule-based automated tools can improve efficiency to some extent, they still have limitations in handling complex code structures and specific business logic.
[0003] In recent years, with the rapid development of deep learning and natural language processing technologies, machine learning models have gradually become a new approach to solving cross-language code conversion problems, such as LLM (Large Language Model). Models can generate high-quality conversion suggestions and effectively handle complex context problems by learning a large amount of code data. The application of these models in automatic code conversion not only improves the accuracy and efficiency of conversion, but also reduces the cost of manual intervention.
[0004] However, although LLM has made significant progress in automatic code conversion, it still faces some challenges. In particular, when it is necessary to preserve specific business logic or optimize performance, the code generated by LLM often lacks a deep understanding of the overall system, which may lead to functional deviations or performance degradation, resulting in semantic errors or unstable conversion results. These problems limit the reliability and effectiveness of LLM in practical applications. SUMMARY
[0005] To solve the problems of the prior art, the embodiments of the present application provide a code conversion method, device and electronic equipment. The technical solution is as follows:
[0006] On the one hand, a code conversion method is provided, the method comprising:
[0007] obtaining a code to be converted and a target programming language;
[0008] constructing a code semantic network based on the code to be converted; the code semantic network indicates elements in the code to be converted and relationships between the elements;
[0009] performing a pre-order traversal on the code semantic network, determining a current to-be-converted code block in the to-be-converted code based on a current traversed element and a traversal path corresponding to the current traversed element;
[0010] expressing the current to-be-converted code block into the target programming language based on the code semantic network to obtain target code corresponding to the current to-be-converted code block;
[0011] in a case where there is an element in the code semantic network that has not been traversed, continuing the pre-order traversal, updating the current to-be-converted code block, determining target code corresponding to the updated current to-be-converted code block, and ending the pre-order traversal until there is no element in the code semantic network that has not been traversed, thereby obtaining target code corresponding to the to-be-converted code.
[0012] In another aspect, a code conversion apparatus is provided, and the apparatus comprises:
[0013] an information acquisition module configured to acquire to-be-converted code and a target programming language;
[0014] a network construction module configured to construct a code semantic network based on the to-be-converted code; the code semantic network indicates elements in the to-be-converted code and relationships between the elements;
[0015] a first traversal module configured to perform a pre-order traversal on the code semantic network, determine a current to-be-converted code block in the to-be-converted code based on a current traversed element and a traversal path corresponding to the current traversed element;
[0016] a language conversion module configured to express the current to-be-converted code block into the target programming language based on the code semantic network to obtain target code corresponding to the current to-be-converted code block;
[0017] a second traversal module configured to, in a case where there is an element in the code semantic network that has not been traversed, continue the pre-order traversal, update the current to-be-converted code block, determine target code corresponding to the updated current to-be-converted code block, and end the pre-order traversal until there is no element in the code semantic network that has not been traversed, thereby obtaining target code corresponding to the to-be-converted code.
[0018] In an exemplary embodiment, the language conversion module comprises:
[0019] a current language module configured to determine a current programming language of the to-be-converted code;
[0020] a candidate instance module configured to determine a candidate conversion instance in a code conversion instance based on the current programming language and the target programming language;
[0021] a similar instance module configured to determine, based on a preset similarity requirement, a similar conversion instance corresponding to the current code block to be converted from among the candidate conversion instances;
[0022] an instance guidance module configured to express the current code block to be converted into the target programming language based on the code semantic network, to obtain target code corresponding to the current code block to be converted, with the similar conversion instance corresponding to the current code block to be converted as guidance.
[0023] In an exemplary embodiment, the apparatus further comprises a direct conversion module configured to perform code conversion without instance guidance in the absence of a similar conversion instance, the direct conversion module comprising:
[0024] a direct conversion module configured to express the current code block to be converted into the target programming language based on the code semantic network, to obtain target code corresponding to the current code block to be converted, in the absence of a similar conversion instance corresponding to the current code block to be converted from among the code conversion instances.
[0025] In an exemplary embodiment, the apparatus further comprises an instance adding module configured to accumulate code conversion instances in the conversion process of a code block, the instance adding module comprising:
[0026] an instance adding module configured to add the current code block to be converted and target code corresponding to the current code block to be converted into the code conversion instance, to update the code conversion instance.
[0027] In an exemplary embodiment, the language conversion module, or the instance guidance module, or the direct conversion module, comprises:
[0028] a target rule module configured to determine a target conversion rule in a code conversion rule based on the current programming language and the target programming language;
[0029] a rule guidance module configured to express the current code block to be converted into the target programming language based on the target conversion rule and the code semantic network, to obtain target code corresponding to the current code block to be converted.
[0030] In an exemplary embodiment, the language conversion module comprises:
[0031] a document acquisition module configured to acquire a project document corresponding to the code to be converted;
[0032] A project guidance module is configured to express the current code block to be converted into the target programming language based on the code semantic network and project documents corresponding to the code to be converted, to obtain target code corresponding to the current code block to be converted.
[0033] In an exemplary embodiment, the device further comprises an annotation guidance conversion module configured to annotate the current code block to be converted to guide code conversion, and the annotation guidance conversion module comprises:
[0034] A code annotation module is configured to annotate the current code block to be converted based on the code semantic network in the absence of project documents corresponding to the code to be converted.
[0035] An annotation guidance module is configured to express the current code block to be converted into the target programming language based on the code semantic network and annotations of the code to be converted, to obtain target code corresponding to the current code block to be converted.
[0036] In an exemplary embodiment, the language conversion module comprises:
[0037] A context information module is configured to determine context information of the current code block to be converted based on the code semantic network.
[0038] A model conversion module is configured to input the current code block to be converted, the target programming language, and the context information into a pre-trained language model, so that the pre-trained language model expresses the current code block to be converted into the target programming language to generate target code corresponding to the current code block to be converted.
[0039] In another aspect, an electronic device is provided, comprising a processor and a memory, the memory storing at least one instruction or at least one program, the at least one instruction or the at least one program being loaded and executed by the processor to implement the code conversion method of any of the above aspects.
[0040] In another aspect, a computer-readable storage medium is provided, the computer-readable storage medium storing at least one instruction or at least one program, the at least one instruction or the at least one program being loaded and executed by a processor to implement the code conversion method of any of the above aspects.
[0041] In another aspect, a computer program product or computer program is provided, the computer program product or computer program comprising computer instructions stored in a computer-readable storage medium. The processor of the electronic device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions to cause the electronic device to perform the code conversion method of any of the above aspects.
[0042] The embodiment of the present application constructs a code semantic network corresponding to the to-be-converted code, performs a pre-order traversal on the code semantic network, determines a current to-be-converted code block in the to-be-converted code based on a current traversal element and a traversal path corresponding to the current traversal element, and expresses the current to-be-converted code block into a target programming language based on the code semantic network. When each code block is converted, it is ensured that all code blocks on which the current to-be-converted code block depends have been converted into the target programming language, thereby guaranteeing the integrity and consistency of the dependency relationship in the to-be-converted code in the target code and avoiding functional loss or logical errors caused by incorrect dependency order. In addition, the code semantic network also helps to understand the context of the current to-be-converted code block during code conversion, thereby improving the accuracy of code conversion and enhancing the understanding ability when processing complex code blocks. BRIEF DESCRIPTION OF DRAWINGS
[0043] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed in the embodiment description will be briefly introduced. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative labor.
[0044] Figure 1 is a flowchart of a code conversion method provided by an embodiment of the present application;
[0045] Figure 2 is an example diagram of a code semantic network provided by an embodiment of the present application;
[0046] Figure 3 is a flowchart of another code conversion method provided by an embodiment of the present application;
[0047] Figure 4 is a structural block diagram of a code conversion system provided by an embodiment of the present application;
[0048] Figure 5 is a structural block diagram of a code conversion device provided by an embodiment of the present application;
[0049] Figure 6 is a hardware structural block diagram of an electronic device provided by an embodiment of the present application. DETAILED DESCRIPTION
[0050] The technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are only some embodiments of the present application, not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative labor are within the scope of protection of the present application.
[0051] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and in the above drawings are used to distinguish similar objects, and do not necessarily have to be used to describe a specific order or sequence. It should be understood that the data thus used can be interchanged under appropriate circumstances, so that the embodiments of the present application described herein can be implemented in an order other than that illustrated or described herein. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion, for example, a process, method, system, product or server including a series of steps or units does not have to be limited to those steps or units clearly listed, but can include other steps or units not clearly listed or inherent to these processes, methods, products or devices.
[0052] It can be understood that in the specific embodiments of the present application, data related to user information and the like is involved, and when the above embodiments of the present application are applied to specific products or technologies, user permission or consent needs to be obtained, and the collection, use and processing of related data needs to comply with relevant laws, regulations and standards of countries and regions.
[0053] Please refer to Figure 1 , which is a flowchart of a code conversion method provided by an embodiment of the present application. It should be noted that the present specification provides method operation steps such as embodiments or flowcharts, but more or fewer operation steps can be included based on conventional or non-creative labor. The order of steps listed in the embodiments is only one of the many execution orders, and does not represent the only execution order. In actual system or product execution, the method order shown in the embodiments or drawings can be executed in sequence or in parallel (for example, in a parallel processor or multi-threaded processing environment). Specifically, as shown in Figure 1 , the method can include:
[0054] S101, obtaining a code to be converted and a target programming language.
[0055] Among them, the code to be converted refers to the source code that needs to be converted from one programming language to another programming language.
[0056] Among them, the target programming language refers to the programming language to which the code to be converted is finally converted.
[0057] For example, from Java to TypeScript, from Python to Java, or from C++ to JavaScript.
[0058] Specifically, the user or system provides the source code file to be converted and the target programming language, reads the source code file, parses its content, determines the language type of the source code and the target programming language.
[0059] S103, constructing a code semantic network based on the to-be-converted code.
[0060] The code semantic network is a graph structure that represents elements in the to-be-converted code and the relationships between the elements in the form of nodes and edges. In specific implementations, the code semantic network can take the form of an intelligent graph as shown in FIG. 1, which can represent functions, classes, variables, and other elements in the code in the form of nodes and edges and reveal the relationships between them. Figure 2
[0061] In specific implementations, the code blocks can be taken as the element units, the nodes in the code semantic network represent the code blocks, such as functions, classes, variables, etc., and the edges in the code semantic network represent the dependency relationships between the code blocks, such as function call relationships, class inheritance relationships, variable reference relationships, module import relationships, object instantiation relationships, data flow relationships, event triggering relationships, conditional dependency relationships, and loop dependency relationships, etc.
[0062] In specific implementations, the lexical analyzer and the syntax analyzer can be used to parse the source code, extract the syntax and semantic information therein, and convert the parsed information into nodes and edges to construct the code semantic network. The nodes represent the code blocks, and the edges represent the dependency relationships between the code blocks.
[0063] For example, assuming that we have a piece of source code, and the parsed code blocks and dependency relationships are as follows: node A represents the main function, node B represents auxiliary function 1, node C represents auxiliary function 2, edge A->B represents that the main function calls auxiliary function 1, and edge A->C represents that the main function calls auxiliary function 2.
[0064] S105, performing a pre-order traversal on the code semantic network, and determining a current to-be-converted code block in the to-be-converted code based on a current traversal element and a traversal path corresponding to the current traversal element.
[0065] The pre-order traversal is an algorithm for traversing a tree or a graph, which traverses in the order of first visiting the root node, and then visiting the left and right subtrees in turn. In code conversion, the pre-order traversal ensures that all dependent code blocks of a certain code block have been processed before the code block is processed.
[0066] The current traversal element refers to the node being currently accessed when the code semantic network is traversed.
[0067] The traversal path corresponding to the current traversal element refers to the path from the root node to the current traversal element. This path records all the nodes and edges passed through to reach the current traversal element, provides context information, and helps to understand the dependency relationships and positions of the current traversal element.
[0068] The current code block to be converted refers to a code block represented by the current traversal element and a corresponding traversal path. The code block is a part that needs to be converted into the target programming language and includes the code of the current traversal element and context information thereof.
[0069] In a specific implementation, elements representing functions and calling relationships between the elements can be extracted from the code semantic network to obtain a calling tree. A pre-order traversal is performed on the calling tree to determine the current code block to be converted. Specifically, if a function to be converted contains called functions, the called functions are added to the function to be converted, and the function to be converted is reconstructed as the current code block to be converted. This process ensures that complete context information can be provided during code conversion, thereby improving the accuracy of conversion.
[0070] In S107, the current code block to be converted is expressed into the target programming language based on the code semantic network to obtain target code corresponding to the current code block to be converted.
[0071] The target code corresponding to the current code block to be converted refers to code obtained by converting the current code block to be converted into the target programming language.
[0072] Specifically, the current code block to be converted is converted into target code according to syntax and semantic rules of the target programming language, and the converted code block is stored to form part of the final target code.
[0073] Specifically, in the process of expressing the current code block to be converted into the target programming language, the code semantic network is used to provide context information of the current code block to be converted. Compared with a code conversion method based on pure text in the prior art, the code semantic network can provide more abundant context information, capture the structure, dependency relationship, and business logic of the code, thereby improving the accuracy and stability of conversion and further generating target code that is more in line with the original intention.
[0074] In an exemplary implementation, the above step S107 can include the following steps:
[0075] Based on the code semantic network, context information of the current code block to be converted is determined.
[0076] The current code block to be converted, the target programming language, and the context information are input into a pre-trained language model, so that the pre-trained language model expresses the current code block to be converted into the target programming language to generate target code corresponding to the current code block to be converted.
[0077] The context information refers to the environmental information related to the current code block to be converted, including but not limited to dependency, scope, function description, etc. In specific implementation, the extracted dependency, scope and function description and other information are integrated into a structured context information.
[0078] The pre-trained language model refers to a deep learning model that has been trained on a large amount of data and can understand and generate natural language or programming language text, such as LLM.
[0079] In specific implementation, a large amount of code data and related context information are obtained, including collecting pairs of implementation codes of the same function in different programming languages, such as Java and Python. The collected code and context information are converted into a format that can be processed by the language model. The model architecture suitable for code conversion task is selected, such as Transformer, GPT, BERT, etc. The size and complexity of the model are considered, and the appropriate number of layers and parameter quantity are selected. The weights and bias terms of the language model are initialized. The preprocessed data is input into the language model, and the output of the model is calculated. The loss function is calculated according to the output of the model and the real label, and the parameters of the model are updated to minimize the loss function.
[0080] Specifically, taking LLM as an example, LLM is usually based on Transformer architecture. The input is converted into a digital form that can be processed by the model, and the input data is processed by multiple layers of neural networks of the model to generate intermediate representations. Specifically, the encoder layer is responsible for processing the input sequence to generate intermediate representations. Each encoder layer usually includes self-attention mechanism and feedforward neural network. The self-attention mechanism allows the model to focus on different parts of the input sequence to capture long-range dependencies, and the feedforward neural network performs nonlinear transformation on the features of each position. The decoder layer is responsible for generating the output sequence. Each decoder layer includes self-attention mechanism, encoder-decoder attention mechanism and feedforward neural network. The self-attention mechanism allows the model to focus on the generated part of the output sequence, the encoder-decoder attention mechanism allows the model to focus on the relevant information in the input sequence, and the feedforward neural network performs nonlinear transformation on the features of each position. The intermediate representation of the model is converted into the final output code. Specifically, the output of the decoder layer is converted into a vector of the size of the vocabulary table through linear transformation, and the softmax function is applied to convert the output vector into a probability distribution, representing the probability of each word appearing in the next position. The next word can be selected by selecting the word with the highest probability (greedy search) or by sampling (such as Top-k sampling, temperature sampling). The generated word is added to the output sequence as input for the next time step, and the next word is generated, until the maximum length is reached or the end symbol is generated. The generated digital sequence is converted back to code form.
[0081] From the above technical solutions of the embodiments of the present application, it can be seen that in the embodiments of the present application, based on the code semantic network and the context information, the pre-trained language model can more accurately understand and convert complex code blocks. The context information provides detailed environmental information, which helps to generate more accurate target code. Using the pre-trained language model for code conversion greatly reduces the need for manual intervention and improves the efficiency and consistency of code conversion. The automatically generated target code has a clear structure and annotations, which facilitates subsequent development and maintenance. By utilizing the language understanding and generation capabilities of the pre-trained language model, a highly automated code conversion process can be achieved, improving the efficiency and quality of code conversion.
[0082] In one exemplary embodiment, as shown in Figure 3 The above step S107 can include:
[0083] S301, determining a current programming language of the code to be converted.
[0084] The current programming language refers to the programming language currently used by the code to be converted.
[0085] Specifically, the source code file is parsed to determine the language type of the source code.
[0086] S303, based on the current programming language and the target programming language, determining whether there is a candidate conversion instance in the code conversion instance.
[0087] Specifically, if the result of the determination is yes, step S305 can be performed; otherwise, if the result of the determination is no, step S309 can be performed.
[0088] The code conversion instance stores a conversion example of any programming language to another programming language, including a source code block and a corresponding target code block.
[0089] The candidate conversion instance stores a conversion example from the current programming language to the target programming language, including a source code block and a corresponding target code block.
[0090] Specifically, it is determined whether there is a conversion instance matching the current programming language and the target programming language in the code conversion instance. If there is, it is filtered out as a candidate conversion instance. If there is not, conversion is directly performed based on the code semantic network.
[0091] S305, based on a preset similarity requirement, determining whether there is a similar conversion instance corresponding to the current code block to be converted in the candidate conversion instance.
[0092] Specifically, if the result of the determination is yes, step S307 can be performed; otherwise, if the result of the determination is no, step S309 can be performed.
[0093] The similar conversion instance refers to a conversion instance in the candidate conversion instances that meets a preset similarity requirement.
[0094] Specifically, the similarity of the current code block to be converted and each candidate conversion instance is calculated, and a conversion instance meeting a preset similarity requirement is selected. In a specific implementation, the preset similarity requirement usually includes that the similarity is greater than a preset threshold, and the similarity can also be required to be in the top few. For example, there are 5 instances in the candidate conversion instances that have a similarity greater than a preset threshold with the current code block to be converted, and the instances with the top three similarities can be selected as the similar conversion instances.
[0095] Specifically, if there is a similar conversion instance meeting the preset similarity requirement, the similar conversion instance is used as a guide, and code conversion is performed in combination with the code semantic network. If there is no similar conversion instance, conversion is directly performed based on the code semantic network.
[0096] S307, based on the code semantic network, the current code block to be converted is expressed into the target programming language using the similar conversion instance corresponding to the current code block to be converted as a guide, to obtain target code corresponding to the current code block to be converted.
[0097] Specifically, step S3011 is continuously executed.
[0098] S309, based on the code semantic network, the current code block to be converted is expressed into the target programming language, to obtain target code corresponding to the current code block to be converted.
[0099] S3011, the current code block to be converted and the target code corresponding to the current code block to be converted are added to the code conversion instance, to update the code conversion instance.
[0100] Specifically, taking Java to TypeScript conversion as an example, in the initial stage of code conversion, if there is a lack of Java to TypeScript conversion samples, a zero-shot method can be used for conversion, and Java is directly converted into TypeScript code based on the understanding of the two languages and the conversion rule. When a certain number of code conversion samples are accumulated, a RAG
[0101] (Retrieval-Augmented Generation, retrieval-augmented generation) method is used, which provides a reference example of similar code during conversion to help better understand the conversion rule, so as to improve the quality and consistency of the conversion.
[0102] The existing LLM usually relies on pre-trained knowledge when performing code conversion, and this static knowledge base is difficult to cope with various complex scenarios in actual development. Changes in code base, adjustments in business requirements, and other factors will affect the accuracy of code conversion, and traditional fine-tuning methods require a large amount of time and computing resources to retrain the model, which is inefficient. The RAG is selected instead of the traditional fine-tuning method in the embodiments of the present application, mainly because RAG can utilize the existing code base or locally stored code corpus, enabling it to dynamically adapt to different code conversion tasks without the need for extensive retraining. Utilizing the existing code conversion repository to store code conversion instances, dynamically retrieving the most relevant examples for the current conversion task as input context to guide the LLM to generate new code segments. The RAG method allows the model to maintain its generation capabilities while incorporating reference information provided by the external knowledge base, significantly improving the quality of code conversion. This dynamic adaptation capability enables the model to flexibly cope with complex scenarios in actual development, thereby improving the efficiency and accuracy of code conversion.
[0103] Please refer to Figure 4 , which shows a structural block diagram of a code conversion system provided by the embodiments of the present application, mainly divided into the following parts:
[0104] Prompt Template (Prompt Template) is a pre-defined template used to build the input prompt of the conversion task.
[0105] Java Code to be Translated (Java Code to be Translated) is the original Java code that needs to be converted into TypeScript.
[0106] Most Similar Translation Examples (Most Similar Translation Examples) are the most similar conversion examples retrieved from the ChromaDB vector database.
[0107] Few-shot Prompt With Examples (Few-shot Prompt With Examples) refers to combining the most similar conversion examples and the Java code to be converted to form a new prompt containing multiple examples.
[0108] Embedding Model (Embedding Model) is used to convert text data into numerical vectors for comparison and analysis.
[0109] Embedding from Examples (Embedding from Examples) is the embedding vector extracted from the conversion example.
[0110] Embedding from Java is an embedding vector extracted from the Java code to be converted.
[0111] ChromaDB Vector Database of Code Examples is a database that stores code example vectors for fast retrieval of similar code snippets.
[0112] Similarity Matrix records the similarity between different code snippets.
[0113] LLM is responsible for generating the corresponding output according to the input prompt.
[0114] Response with TS Translation is the result generated by LLM, which contains the version of Java code converted to TypeScript.
[0115] Specifically, an input prompt is constructed by a prompt template and the Java code to be converted; then, the most similar conversion example is retrieved from the ChromaDB vector database and merged with the input prompt to form a new prompt with examples; next, the embedding vectors of the example and the Java code are obtained using the embedding model, and the similarity between them is calculated; finally, the prompt with examples is input into LLM to generate the conversion result.
[0116] As can be seen from the above technical solutions of the embodiments of the present application, the embodiments of the present application can more accurately understand and convert complex code blocks based on code semantic networks and similar conversion examples. Similar conversion examples provide a reference and help to generate more accurate target code. The use of similar conversion examples improves the flexibility of conversion and can better handle different types of code blocks. The code semantic network provides detailed context information, enhances the understanding of complex code blocks, and makes the conversion process more reliable and efficient, further improving the accuracy of conversion. By adding the converted code block and its corresponding target code to the code conversion example, conversion experience can be continuously accumulated, improving the efficiency and accuracy of subsequent conversion, and gradually improving the performance and reliability of code conversion.
[0117] In an exemplary embodiment, the above step S107, or the step of expressing the current code block to be converted into the target programming language based on the code semantic network in the above step S307 to obtain the target code corresponding to the current code block to be converted, or the above step S309 can include the following steps:
[0118] determine target conversion rules in the code conversion rules based on the current programming language and the target programming language;
[0119] express the current code block to be converted into the target programming language based on the target conversion rules and the code semantic network, to obtain target code corresponding to the current code block to be converted.
[0120] The target conversion rules refer to specific conversion rules for the current programming language and the target programming language.
[0121] Specifically, the code conversion rules are loaded from the configuration file or the database, which cover the conversion between multiple programming languages, and the conversion rules suitable for the current programming language and the target programming language are selected from the loaded code conversion rules. The conversion rules are verified to ensure that the selected conversion rules are complete and effective, and can cover all syntax and semantic requirements of the current code block to be converted. These rules help the model to better meet the syntax requirements of the target language during conversion, and ensure that the generated code is correct and conforms to the coding habits of the target language.
[0122] For example, assuming that the current programming language is Java and the target programming language is TypeScript, taking the conversion from Java to TypeScript as an example, the code conversion rules are stored in the configuration file or the database, including a set of conversion rules written in advance for the common syntax differences between Java and TypeScript. The code conversion rules are loaded, and the conversion rules from Java to TypeScript are selected, and it is verified whether these rules cover the conversion of common syntax structures such as class definition, method call, and variable declaration.
[0123] Specifically, the context information of the current code block to be converted, such as dependency relationship and scope, is obtained from the code semantic network. The syntax and semantics of the current code block to be converted are converted into the syntax and semantics of the target programming language according to the target conversion rules. Special constructs and language features in the source code are processed to ensure that the converted code runs correctly in the target programming language.
[0124] As can be seen from the above technical solutions of the embodiments of the present application, the TypeScript code generated by the embodiments of the present application not only conforms to the standard of the target language, but also retains the readability and maintainability of the original code, which facilitates subsequent code maintenance and upgrading.
[0125] In an exemplary embodiment, the above step S107 can include the following steps:
[0126] Obtain the project document corresponding to the code to be converted;
[0127] Based on the code semantic network and the project document corresponding to the code to be converted, the current code block to be converted is expressed into the target programming language to obtain the target code corresponding to the current code block to be converted.
[0128] The project document is a document related to the code to be converted, including but not limited to design documents, API documents, user manuals, development guidelines, etc., which provides the function, purpose and context information of the code, and is used to help understand the structure and function of the project.
[0129] Specifically, the project document is parsed to extract information such as function description, design document, API document, etc. related to the code to be converted. The document integrity is verified to ensure that the project document is complete and effective, and can provide sufficient context information to support code conversion.
[0130] Specifically, the context information of the current code block to be converted, such as dependency relationship, scope, etc., is obtained from the code semantic network. The function description, design document, API document, etc. in the project document are integrated into the code conversion process to ensure that the converted code meets the expected function and design. The current code block to be converted is converted into the code of the target programming language according to the syntax and semantic rules of the target programming language.
[0131] If the corresponding project document is missing, function annotations can be generated to help better understand the business environment of the code, improving the understanding ability when processing complex code blocks.
[0132] Specifically, in the case where the project document corresponding to the code to be converted is missing, the current code block to be converted is annotated based on the code semantic network; and based on the code semantic network and the annotations of the code to be converted, the current code block to be converted is expressed into the target programming language to obtain the target code corresponding to the current code block to be converted.
[0133] Specifically, the context information of the current code block to be converted, such as dependency relationship, scope, etc., is obtained from the code semantic network. Based on the code semantic network, annotations for the current code block to be converted are automatically generated to explain the function and purpose of the code. The generated annotation information is integrated into the code conversion process.
[0134] As can be seen from the above technical solutions of the embodiments of the present application, the code semantic network and the project document are used to ensure that the dependency relationship in the source code is completely preserved in the target code, avoiding the loss of function or logic error caused by incorrect dependency order. The project document provides detailed context information, which helps to generate more accurate target code. In the case where the project document is missing, the automatically generated annotations provide additional context information.
[0135] S109, it is judged whether there is an element in the code semantic network that has not been traversed.
[0136] Specifically, if the result of the judgment is yes, step S105 can be executed; otherwise, if the result of the judgment is no, step S1011 can be executed.
[0137] Specifically, in the case that there is an element in the code semantic network that has not been traversed, the pre-order traversal is continued, the current code block to be converted is updated, the target code corresponding to the updated current code block to be converted is determined, and the pre-order traversal is ended until there is no element in the code semantic network that has not been traversed.
[0138] S1011, ending the pre-order traversal to obtain the target code corresponding to the code to be converted.
[0139] The target code corresponding to the code to be converted refers to the code after the code to be converted is converted into the target programming language.
[0140] Specifically, all the converted code blocks are combined to form a complete target code, and the generated target code is saved to a file or returned to a user.
[0141] As can be seen from the above technical solutions of the embodiments of the present application, the embodiments of the present application ensure that the dependency relationship in the source code is completely preserved in the target code through pre-order traversal and dependency relationship management, avoiding functional loss or logical errors caused by incorrect dependency order; based on the code semantic network and pre-order traversal, complex code blocks can be more accurately understood and converted, the code semantic network provides detailed context environment information, which helps to generate more accurate target code. The code semantic network provides context environment information in the conversion process, enhances the understanding ability of complex code blocks, and makes the conversion process more reliable and efficient. The workload and error rate of manual code conversion are reduced, and the risks that may be caused in the code conversion process are reduced, especially in the cross-language conversion of complex systems, the code conversion quality can be better guaranteed.
[0142] Corresponding to the code conversion method provided in the above several embodiments, the embodiments of the present application also provide a code conversion device. Since the code conversion device provided by the embodiments of the present application corresponds to the code conversion method provided by the above several embodiments, the implementation modes of the foregoing code conversion method are also applicable to the code conversion device provided by the embodiments of the present application, which will not be described in detail in the embodiments.
[0143] Please refer to Figure 5 which is a structural schematic diagram of a code conversion device provided by the embodiments of the present application. The device has the function of implementing the code conversion method in the method embodiments, which can be realized by hardware or corresponding software executed by hardware. As shown in Figure 5 the device can include:
[0144] The information obtaining module 510 is configured to obtain the code to be converted and a target programming language;
[0145] The network constructing module 520 is configured to construct a code semantic network based on the code to be converted. The code semantic network indicates elements in the code to be converted and relationships between the elements.
[0146] The first traversal module 530 is configured to perform a pre-order traversal on the code semantic network, and determine a current code block to be converted in the code to be converted based on a current traversal element and a traversal path corresponding to the current traversal element.
[0147] The language conversion module 540 is configured to express the current code block to be converted into the target programming language based on the code semantic network, to obtain target code corresponding to the current code block to be converted.
[0148] The second traversal module 550 is configured to continue the pre-order traversal, update the current code block to be converted, and determine target code corresponding to the updated current code block to be converted, until there is no element in the code semantic network that has not been traversed, and the pre-order traversal is ended, to obtain the target code corresponding to the code to be converted.
[0149] In an exemplary embodiment, the language conversion module comprises:
[0150] The current language module is configured to determine a current programming language of the code to be converted.
[0151] The candidate instance module is configured to determine a candidate conversion instance in the code conversion instance based on the current programming language and the target programming language.
[0152] The similar instance module is configured to determine a similar conversion instance corresponding to the current code block to be converted in the candidate conversion instance based on a preset similarity requirement.
[0153] The instance guidance module is configured to express the current code block to be converted into the target programming language based on the code semantic network, to obtain target code corresponding to the current code block to be converted, with the similar conversion instance corresponding to the current code block to be converted as guidance.
[0154] In an exemplary embodiment, the apparatus further comprises a direct conversion module for code conversion without instance guidance in the absence of the similar conversion instance. The direct conversion module comprises:
[0155] The direct conversion module is configured to express the current code block to be converted into the target programming language based on the code semantic network, to obtain target code corresponding to the current code block to be converted, in the absence of the similar conversion instance corresponding to the current code block to be converted in the code conversion instance.
[0156] In an example embodiment, the apparatus further comprises an instance adding module for accumulating the code conversion instance in the conversion process of the code block, the instance adding module comprising:
[0157] The instance adding module is configured to add the current code block to be converted and the target code corresponding to the current code block to be converted into the code conversion instance, so as to update the code conversion instance.
[0158] In an example embodiment, the language conversion module, or the instance guiding module, or the direct conversion module comprises:
[0159] The target rule module is configured to determine a target conversion rule in the code conversion rule based on the current programming language and the target programming language.
[0160] The rule guiding module is configured to express the current code block to be converted into the target programming language based on the target conversion rule and the code semantic network, to obtain the target code corresponding to the current code block to be converted.
[0161] In an example embodiment, the language conversion module comprises:
[0162] The document obtaining module is configured to obtain a project document corresponding to the code to be converted.
[0163] The project guiding module is configured to express the current code block to be converted into the target programming language based on the code semantic network and the project document corresponding to the code to be converted, to obtain the target code corresponding to the current code block to be converted.
[0164] In an example embodiment, the apparatus further comprises an annotation guiding conversion module for annotating the current code block to be converted to guide the code conversion, the annotation guiding conversion module comprising:
[0165] The code annotation module is configured to annotate the current code block to be converted based on the code semantic network in the case that the project document corresponding to the code to be converted is absent.
[0166] The annotation guiding module is configured to express the current code block to be converted into the target programming language based on the code semantic network and the annotation of the code to be converted, to obtain the target code corresponding to the current code block to be converted.
[0167] In an example embodiment, the language conversion module comprises:
[0168] The context information module is configured to determine context information of the current code block to be converted based on the code semantic network.
[0169] The model conversion module is configured to input the current code block to be converted, the target programming language, and the context information into the pre-trained language model, so that the pre-trained language model expresses the current code block to be converted in the target programming language, and generates the target code corresponding to the current code block to be converted.
[0170] It should be noted that the apparatus provided in the above embodiments is only used as an example for dividing the above functional modules to implement the functions thereof, and in actual applications, the above functions can be completed by different functional modules according to needs, that is, the internal structure of the apparatus is divided into different functional modules to complete all or part of the above-described functions. In addition, the apparatus and method embodiments provided in the above embodiments belong to the same concept, and the specific implementation process is detailed in the method embodiments, which will not be described here.
[0171] The electronic device provided in the embodiments of the present application includes a processor and a memory, and the memory stores at least one instruction or at least one program, which is loaded and executed by the processor to implement any one of the code conversion methods provided in the above method embodiments.
[0172] The memory can be used to store software programs and modules, and the processor can execute various function applications and data processing by running the software programs and modules stored in the memory. The memory can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, application programs required for functions, etc.; and the data storage area can store data created according to the use of the device, etc. In addition, the memory can include a high-speed random access memory, and can also include a non-volatile memory, such as at least one magnetic disk storage device, a flash memory device, or other volatile solid-state memory device. Accordingly, the memory can also include a memory controller to provide access of the processor to the memory.
[0173] The method embodiments provided in the embodiments of the present application can be executed in a computer terminal, a server, or a similar computing device, that is, the above electronic device can include a computer terminal, a server, or a similar computing device. Figure 6 is a hardware structure block diagram of a computer device running a code conversion method provided in the embodiments of the present application, as Figure 6 shown, the internal structure of the computer device can include but is not limited to a processor, a network interface, and a memory. Among them, the processor, the network interface, and the memory in the computer device can be connected through a bus or other means, and in the embodiment of the present application Figure 6 , the connection through the bus is taken as an example.
[0174] The processor (or CPU (Central Processing Unit)) is the computing core and control core of the computer device. The network interface can optionally include a standard wired interface, a wireless interface (such as WI-FI, a mobile communication interface, etc.). The memory is a memory device in the computer device, used to store programs and data. It can be understood that the memory here can be a high-speed RAM memory device, or a non-volatile memory device (such as at least one disk storage device); optionally, it can also be at least one storage device located away from the aforementioned processor. The memory provides a storage space that stores the operating system of the electronic device, which can include but is not limited to: a Windows system (an operating system), a Linux (an operating system), an Android (a mobile operating system) system, an IOS (a mobile operating system) system, etc., and the present application does not limit this; and in the storage space, one or more instructions suitable for being loaded and executed by the processor are also stored, which can be one or more computer programs (including program codes). In the embodiment of the present application, the processor loads and executes one or more instructions stored in the memory to implement the code conversion method provided by the above-mentioned method embodiment.
[0175] The embodiment of the present application also provides a computer readable storage medium, which can be arranged in an electronic device to save at least one instruction or at least one program related to a code conversion method, and the at least one instruction or the at least one program is loaded and executed by the processor to implement any one of the code conversion methods provided by the above-mentioned method embodiment.
[0176] Optionally, in the present embodiment, the storage medium can include but is not limited to: a U disk, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk, and various media that can store program codes.
[0177] It should be noted that: the above-mentioned sequence of the embodiments of the present application is only for description, not representing the advantages and disadvantages of the embodiments. And the above-mentioned description is made for specific embodiments of the present application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be executed in different order from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multi-task processing and parallel processing are possible or can be advantageous.
[0178] Each of the embodiments in the specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other. Each of the embodiments focuses on the difference from other embodiments. In particular, for the device embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the part of the method embodiments.
[0179] A person of ordinary skill in the art can understand that all or part of the steps of the above embodiments can be completed by hardware, or a program can be used to instruct the relevant hardware to complete the steps, and the program can be stored in a computer readable storage medium. The storage medium mentioned above can be a read-only memory, a magnetic disk or an optical disk.
[0180] The above is only a preferred embodiment of the present application, and is not used to limit the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.
Claims
1. A transcoding method, characterized by, The method comprises: acquiring a code to be converted and a target programming language; constructing a code semantic network based on the code to be converted; the code semantic network indicates elements in the code to be converted and relationships between the elements; performing a pre-order traversal on the code semantic network, determining a current code block to be converted in the code to be converted based on a current traversal element and a traversal path corresponding to the current traversal element; expressing the current code block to be converted into the target programming language based on the code semantic network to obtain target code corresponding to the current code block to be converted; in a case where there is an element that has not been traversed in the code semantic network, continuing the pre-order traversal, updating the current code block to be converted, determining target code corresponding to the updated current code block to be converted, and ending the pre-order traversal until there is no element that has not been traversed in the code semantic network, thereby obtaining target code corresponding to the code to be converted; wherein the expressing the current code block to be converted into the target programming language based on the code semantic network to obtain target code corresponding to the current code block to be converted comprises: determining context information of the current code block to be converted based on the code semantic network; inputting the current code block to be converted, the target programming language and the context information into a pre-trained language model, so that the pre-trained language model expresses the current code block to be converted into the target programming language and generates target code corresponding to the current code block to be converted.
2. The transcoding method of claim 1, wherein, The method further comprises: determining a current programming language of the code to be converted; determining a candidate conversion instance in a code conversion instance based on the current programming language and the target programming language; determining a similar conversion instance corresponding to the current code block to be converted in the candidate conversion instance based on a preset similarity requirement; expressing the current code block to be converted into the target programming language based on the code semantic network guided by the similar conversion instance corresponding to the current code block to be converted, to obtain target code corresponding to the current code block to be converted.
3. The transcoding method of claim 2, wherein, The method further comprises: in a case where there is no similar conversion instance corresponding to the current code block to be converted in the code conversion instance, expressing the current code block to be converted into the target programming language based on the code semantic network to obtain target code corresponding to the current code block to be converted.
4. The transcoding method of claim 3, wherein, The method further comprises: adding the current code block to be converted and the target code corresponding to the current code block to be converted into the code conversion instance to update the code conversion instance.
5. The transcoding method of claim 4, wherein, The method further comprises: determining a target conversion rule in a code conversion rule based on the current programming language and the target programming language; Express the current code block to be converted into the target programming language based on the target conversion rule and the code semantic network, to obtain the target code corresponding to the current code block to be converted.
6. The transcoding method of claim 1, wherein, The method further comprises: In the case where the project document corresponding to the code to be converted is absent, annotating the current code block to be converted based on the code semantic network; Express the current code block to be converted into the target programming language based on the code semantic network and the annotation of the code to be converted, to obtain the target code corresponding to the current code block to be converted.
7. The transcoding method of claim 6, wherein, The apparatus comprises: An information obtaining module configured to obtain code to be converted and a target programming language; A network constructing module configured to construct a code semantic network based on the code to be converted; the code semantic network indicates elements in the code to be converted and relationships between the elements; 8. A transcoding device, characterized by A first traversal module configured to perform a pre-order traversal on the code semantic network, determine a current code block to be converted in the code to be converted based on a current traversal element and a traversal path corresponding to the current traversal element; A language converting module configured to express the current code block to be converted into the target programming language based on the code semantic network, to obtain the target code corresponding to the current code block to be converted; A second traversal module configured to continue the pre-order traversal, update the current code block to be converted, determine the target code corresponding to the updated current code block to be converted, until there is no element in the code semantic network that has not been traversed, end the pre-order traversal, and obtain the target code corresponding to the code to be converted. The language converting module comprises a context information module configured to determine context information of the current code block to be converted based on the code semantic network, and a model converting module configured to input the current code block to be converted, the target programming language and the context information into a pre-trained language model, so that the pre-trained language model expresses the current code block to be converted into the target programming language and generates the target code corresponding to the current code block to be converted. The apparatus comprises a processor and a memory, the memory stores at least one instruction or at least one program, the at least one instruction or the at least one program is loaded and executed by the processor to implement the code conversion method according to any one of claims 1-7. 9. An electronic device, comprising:
Citation Information
Patent Citations
Automatic code conversion and evaluation method and device from C language to RUST language
CN118170672A
Translating between programming languages independently of sequence-to-sequence decoders
US20230325164A1