Code conversion method and device, example set generation method and device, equipment and product

By acquiring initial language code snippets and their few sample example sets, constructing prompt information, and using a large language model for code conversion, the problem of low code conversion accuracy in existing technologies is solved, achieving higher code conversion accuracy.

CN121764481APending Publication Date: 2026-03-31SUNGROW POWER SUPPLY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-02-25
Publication Date
2026-03-31

AI Technical Summary

Technical Problem

Existing technologies suffer from low accuracy in target language code conversion due to a lack of sufficient training data.

Method used

Obtain the initial language code snippet and its corresponding few sample example set, construct prompt information and input it into the code conversion model, use the large language model to learn the mapping relationship between the initial language and the target language, and generate the target language code snippet.

Benefits of technology

This improves the accuracy of the code conversion model for target language code snippets, ensuring that the generated code meets expectations and thus enhancing the accuracy of code conversion.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121764481A_ABST
    Figure CN121764481A_ABST
Patent Text Reader

Abstract

The invention discloses a code conversion method and device, an example set generation method and device, equipment and a product, and relates to the technical field of artificial intelligence, and the code conversion method comprises the steps that an initial language code snippet and a few-sample example set corresponding to the initial language code snippet are obtained, the few-sample example set comprises dynamic sample examples, and the dynamic sample examples correspond to the initial language code snippet; the dynamic sample example comprises an initial language code snippet sample, a target language code snippet sample, a corresponding relation between the initial language code snippet sample and the target language code snippet sample and a conversion rule; on the basis of the initial language code snippets and the few-sample example set, prompt information is constructed; the prompt information is input into the code conversion model to obtain the target language code snippets, and the code conversion accuracy is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of artificial intelligence technology, and in particular to code conversion methods and apparatus, example set generation methods and apparatus, equipment and products. Background Technology

[0002] Code conversion refers to the process of converting program code written in one programming language into program code written in another programming language. This technology is of great significance in software migration, code reuse, and cross-platform development. Currently, code conversion mainly relies on machine learning methods. However, due to a lack of sufficient training data, the accuracy of the converted target language code is relatively low. Summary of the Invention

[0003] This application provides a code conversion method and apparatus, an example set generation method and apparatus, equipment and products, aiming to solve the technical problem of low accuracy in code conversion.

[0004] To achieve the above objectives, this application proposes a code conversion method, comprising:

[0005] Obtain the initial language code snippet and the corresponding few sample example set, wherein the few sample example set includes dynamic sample examples, and the dynamic sample examples include initial language code snippet samples, target language code snippet samples, the correspondence and conversion rules between initial language code snippet samples and target language code snippet samples;

[0006] Based on initial language code snippets and a small sample set, construct prompt messages;

[0007] Input the prompt information into the code conversion model to obtain the target language code snippet.

[0008] In one embodiment, obtaining the initial language code fragment and the corresponding few-sample example set includes:

[0009] Get the initial language code snippet;

[0010] The initial language code fragment is decomposed into a structured representation to obtain the structured representation of the initial language code fragment;

[0011] The structured representation is converted into a vector representation of the initial language code snippets;

[0012] From the first example library, retrieve dynamic sample examples that match the vector representation to generate a small sample example set.

[0013] In one embodiment, converting the structured representation into a vector representation of the initial language code fragment includes:

[0014] The structured representation of the input word embedding model is used to obtain the word vector representation of the initial language code fragment;

[0015] And / or, input the structured representation into the semantic embedding model to obtain the semantic vector representation of the initial language code fragment;

[0016] Vector representations include word vector representations and / or semantic vector representations.

[0017] In one embodiment, after converting the structured representation into a vector representation of the initial language code fragment, the method further includes:

[0018] Retrieve dynamic sample examples that match the vector representation from the first sample library, and retrieve preset sample examples that match the vector representation from the second sample library;

[0019] After merging the preset sample examples and dynamic sample examples, similarity sorting and filtering are performed to obtain a small sample example set;

[0020] The example library includes a first example library and a second example library. The first example library includes dynamic sample examples corresponding to the initial language code snippets that have undergone code conversion, and the second example library includes preset sample examples.

[0021] In one embodiment, the vector representation includes word vector representation and semantic vector representation. Retrieving dynamic sample examples that match the vector representation from a first example library includes:

[0022] Determine a first similarity score between the word vector representation and each dynamic sample example in the first example library, and determine a second similarity score between the semantic vector representation and each dynamic sample example in the first example library;

[0023] Based on the first similarity score and the second similarity score, the corresponding dynamic sample example is determined.

[0024] In one embodiment, the vector representation includes word vector representation and semantic vector representation. Retrieving preset sample examples that match the vector representation from the second example library includes:

[0025] Determine the third similarity score between the word vector representation and each preset sample example in the second example library, and determine the fourth similarity score between the semantic vector representation and each preset sample example in the second example library;

[0026] Based on the third and fourth similarity scores, the corresponding preset sample examples are determined.

[0027] In one embodiment, after inputting the prompt information into the code conversion model to obtain the target language code fragment, the method further includes:

[0028] The target language code fragment after the current initial language code fragment is converted is evaluated to obtain the evaluation result corresponding to the target language code fragment;

[0029] If the evaluation result meets the preset evaluation result, then proceed to the code conversion of the next initial language code segment, until the conversion of all initial language code segments is completed;

[0030] If the evaluation result does not meet the preset evaluation result, the process returns to retrieving dynamic sample examples that match the vector representation from the first example library and generating a small sample example set.

[0031] In one embodiment, the code conversion method further includes:

[0032] The current initial language code fragment whose evaluation result meets the preset evaluation result is associated with its corresponding target language code fragment to obtain a dynamic sample example;

[0033] Dynamic sample examples are stored in the first sample library.

[0034] In one embodiment, the code conversion method further includes:

[0035] If the target language code snippet conforms to the set syntax rules, and / or the target language code snippet passes the expected function verification, and / or the target language code snippet meets the set performance requirements, the evaluation result is determined to meet the preset evaluation result.

[0036] In one embodiment, the code conversion method further includes:

[0037] Based on the initial language code snippet, the context information of the initial language code snippet, and a small sample set, a prompt message is constructed.

[0038] Furthermore, to achieve the above objectives, this application also proposes a method for generating an example set, comprising:

[0039] Get the initial language code snippet;

[0040] The initial language code fragment is decomposed into a structured representation to obtain the structured representation of the initial language code fragment;

[0041] The structured representation is converted into a vector representation of the initial language code snippets;

[0042] Retrieve sample examples that match the vector representation from the example library to generate a small sample example set.

[0043] In one embodiment, converting the structured representation into a vector representation of the initial language code includes:

[0044] The structured representation of the input word embedding model is used to obtain the word vector representation of the initial language code fragment;

[0045] And / or, input the structured representation into the semantic embedding model to obtain the semantic vector representation of the initial language code fragment;

[0046] Vector representations include word vector representations and / or semantic vector representations.

[0047] In one embodiment, retrieving sample examples that match the vector representation from the example library to generate a few-sample example set includes:

[0048] Retrieve dynamic sample examples that match the vector representation from the first sample library, and retrieve preset sample examples that match the vector representation from the second sample library;

[0049] After merging the preset sample examples and dynamic sample examples, similarity sorting and filtering are performed to obtain a small sample example set;

[0050] The example library includes a first example library and a second example library. The first example library includes dynamic sample examples corresponding to the initial language code snippets that have undergone code conversion, and the second example library includes preset sample examples.

[0051] In one embodiment, the vector representation includes word vector representation and semantic vector representation. Retrieving dynamic sample examples that match the vector representation from a first example library includes:

[0052] Determine a first similarity score between the word vector representation and each dynamic sample example in the first example library, and determine a second similarity score between the semantic vector representation and each dynamic sample example in the first example library;

[0053] Based on the first similarity score and the second similarity score, the corresponding dynamic sample example is determined.

[0054] In one embodiment, the vector representation includes word vector representation and semantic vector representation. Retrieving preset sample examples that match the vector representation from the second example library includes:

[0055] Determine the third similarity score between the word vector representation and each preset sample example in the second example library, and determine the fourth similarity score between the semantic vector representation and each preset sample example in the second example library;

[0056] Based on the third and fourth similarity scores, the corresponding preset sample examples are determined.

[0057] Furthermore, to achieve the above objectives, this application also proposes a code conversion apparatus, comprising:

[0058] The first acquisition module is used to acquire the initial language code fragment and the corresponding few sample example set. The few sample example set includes dynamic sample examples, which include initial language code fragment samples, target language code fragment samples, the correspondence and conversion rules between the initial language code fragment samples and the target language code fragment samples.

[0059] The constructor module is used to construct prompt messages based on initial language code snippets and a small sample set of examples;

[0060] The first conversion module is used to input the prompt information into the code conversion model and obtain the target language code fragment.

[0061] Furthermore, to achieve the above objectives, this application also proposes an apparatus for generating an example set, comprising:

[0062] The second acquisition module is used to acquire the initial language code;

[0063] The structured representation module is used to perform structured decomposition on the initial language code to obtain a structured representation of the initial language code;

[0064] The second conversion module is used to convert the structured representation into a vector representation of the initial language code;

[0065] The generation module is used to retrieve sample examples whose vector representations match from the example library and generate a small sample example set.

[0066] In addition, to achieve the above objectives, this application also proposes an electronic device, the device comprising: a memory, a processor, and a computer program stored on the memory and executable on the processor, the computer program being configured to implement the steps of the code conversion method described above, or the computer program being configured to implement the steps of the example set generation method described above.

[0067] In addition, to achieve the above objectives, this application also provides a computer program product, which includes a computer program that, when executed by a processor, implements the steps of the code conversion method described above, or, when executed by a processor, implements the steps of the example set generation method described above.

[0068] This application's embodiments construct prompt information based on an initial language code fragment and a corresponding few-sample example set. Inputting this prompt information into a code conversion model yields a few-sample example set of target language code fragments converted from the initial language code fragments. This helps the code conversion model learn the mapping relationship between the initial and target language code fragments. Furthermore, the few-sample example set includes dynamic sample examples, which include initial language code fragment samples, target language code fragment samples, the correspondence between the initial and target language code fragment samples, and conversion rules. The initial and target language code fragment samples demonstrate the code conversion results and rules, helping the code conversion model better understand the conversion process and generate target language code fragments that meet expectations. Based on the initial language code fragments and the few-sample example set, accurate prompt information can be constructed, clearly informing the code conversion model what type of code conversion is needed. This helps the code conversion model maintain the correct direction when generating target language code fragments, improving the accuracy of the generated target language code fragments. Attached Figure Description

[0069] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.

[0070] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0071] Figure 1 This is a flowchart illustrating a code conversion method in some embodiments of this application;

[0072] Figure 2 This is a detailed flowchart of step S10 in some embodiments of this application;

[0073] Figure 3 This is another detailed flowchart of step S10 in some embodiments of this application;

[0074] Figure 4 This is a detailed flowchart of step S15 in some embodiments of this application;

[0075] Figure 5 This is another detailed flowchart of step S15 in some embodiments of this application;

[0076] Figure 6 This is a schematic diagram illustrating the generation process of dynamic sample examples in some embodiments of this application;

[0077] Figure 7 This is a schematic diagram of the module structure of the code conversion device in some embodiments of this application;

[0078] Figure 8 This is a schematic diagram of the module structure of the example set generation device in some embodiments of this application;

[0079] Figure 9 This is a schematic diagram of the device structure of an electronic device in some embodiments of this application.

[0080] The purpose, features, and advantages of this application will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation

[0081] It should be understood that the specific embodiments described herein are merely illustrative of the technical solutions of this application and are not intended to limit this application.

[0082] To better understand the technical solution of this application, a detailed description will be provided below in conjunction with the accompanying drawings and specific implementation methods.

[0083] Code conversion refers to the process of converting program code written in one programming language into program code written in another. This technology has profound significance in areas such as software migration, code reuse, and cross-platform development, providing developers with greater flexibility and choices. With the continuous development of computer technology, code conversion technology has also undergone an evolution from simple to complex, and from manual to automated processes.

[0084] In early software development, due to the limited number of programming languages ​​and the relatively small size of software, the need for code conversion was not urgent. However, with the continuous advancement of computer technology, new programming languages ​​have emerged, and software size has gradually increased, making the need for code conversion increasingly prominent. Initially, code conversion relied mainly on manual work, requiring developers to manually convert code from one language line by line to another. This method was not only time-consuming and labor-intensive but also prone to errors, failing to meet the needs of large-scale software development.

[0085] To address this problem, rule-based code transformation methods were explored. These methods define a series of transformation rules to convert the syntax, data types, and algorithms of code snippets in the initial language into corresponding structures in the target language. However, due to significant differences in syntax and semantics between different programming languages, defining a complete set of transformation rules is extremely difficult. Furthermore, rule-based methods often cannot handle complex code structures and semantic relationships, resulting in low-quality transformed code.

[0086] With the rise of machine learning technology, attempts have begun to apply it to the field of code translation. Machine learning-based code translation methods train models to learn the mapping relationship between code snippets in the initial language and code snippets in the target language, thereby achieving automated code translation. This method has greater flexibility and adaptability, and can handle more complex code structures and semantic relationships. However, due to the inherent complexity of code translation tasks and the lack of sufficient training data, machine learning-based code translation methods still face many challenges. For example, the model may fail to accurately understand the intent and contextual information of the initial language code snippets, leading to problems such as syntax errors, logical errors, or semantic inconsistencies in the translated code.

[0087] To address the aforementioned issues, this application proposes a code conversion method. The main technical solution includes: obtaining an initial language code fragment and a corresponding few-sample example set, wherein the few-sample example set includes dynamic sample examples, which include initial language code fragment samples, target language code fragment samples, the correspondence between the initial language code fragment samples and the target language code fragment samples, and conversion rules; constructing prompt information based on the initial language code fragment and the few-sample example set; and inputting the prompt information into a code conversion model to obtain the target language code fragment.

[0088] This application's embodiments construct prompt information based on an initial language code fragment and a corresponding few-sample example set. This prompt information is input into a code conversion model to obtain a target language code fragment converted from the initial language code fragment. This allows the code conversion model to learn the mapping relationship between the initial and target language code fragments. The few-sample example set includes dynamic sample examples, which include initial language code fragment samples, target language code fragment samples, the correspondence between the initial and target language code fragment samples, and conversion rules. The initial and target language code fragment samples demonstrate the code conversion results and rules, helping the code conversion model better understand the conversion process and generate target language code fragments that meet expectations. Based on the initial language code fragment and the few-sample example set, accurate prompt information can be constructed, clearly informing the code conversion model what type of code conversion is needed. This helps the code conversion model maintain the correct direction when generating target language code fragments, improving the accuracy of the generated target language code fragments.

[0089] It should be noted that the executing entity in this embodiment can be a computing service device with data processing, network communication, and program execution functions, such as a tablet computer, personal computer, or mobile phone, or an electronic device capable of performing the above functions. The following description uses an electronic device as an example to illustrate this embodiment and the subsequent embodiments.

[0090] Based on this, embodiments of this application provide a code conversion method, referring to... Figure 1 , Figure 1 This is a flowchart illustrating some embodiments of the code conversion method of this application. In some embodiments of this application, the code conversion method includes:

[0091] Step S10: Obtain the initial language code fragment and the corresponding few sample example set. The few sample example set includes dynamic sample examples, which include initial language code fragment samples, target language code fragment samples, the correspondence between the initial language code fragment samples and the target language code fragment samples, and the conversion rules.

[0092] An initial language code snippet refers to a source code fragment that needs to be converted. It can be written in any programming language, such as C, C++, Python, or other types of programming languages. Initial language code snippets can be uploaded by users or retrieved from code development systems, code repositories, or specially created code collections via an interface to meet the needs for obtaining initial language code snippets in different scenarios.

[0093] A few-shot example set, also known as a few-form example set, can include dynamic sample examples of the same code transformation type as the initial language code snippet samples and the target language code snippet samples. For example, if the initial language code snippet samples are in C and the target language code snippet samples are in C++, then the few-shot example set includes dynamic sample examples of transforming from C to C++. A few-shot example set can also include dynamic sample examples of multiple different code transformation types, such as dynamic sample examples of transforming from C to C++, C++ to Python, and C to Python, to meet the code transformation needs of different types.

[0094] A few-shot example set can include multiple dynamic example examples. Dynamic example examples are sample examples dynamically constructed during the code conversion process. They contain historical code snippets from the same project that have been successfully converted. Dynamic example examples are used to further optimize subsequent code conversion tasks. Specifically, a dynamic example example includes an initial language code snippet sample, a target language code snippet sample, the correspondence between the initial and target language code snippets, and conversion rules. These conversion rules guide the code conversion model to convert the initial language code snippets to the target language code snippets. Furthermore, because a dynamic example example contains historical code snippets from the same project that have been successfully converted, it implicitly contains context information about the current initial language code snippet. This context information specifically includes the initial language code snippets that have been successfully converted before the current initial language code snippet and the corresponding target language code snippets.

[0095] The system can query similar examples from the example library based on the initial language code snippet being input, which can be used as dynamic sample examples. Therefore, when the initial language code snippet changes, the dynamic sample examples obtained will also change, and the set of few samples generated by the dynamic sample examples will also change. Such a set of few samples is more referential and can further improve the accuracy of the code conversion task.

[0096] It should be noted that since a single project may include multiple code snippets, when a code snippet in a project is first converted—that is, when the initial language code snippet is the first code snippet in the project to be converted—the few sample example set corresponding to that initial language code snippet only contains preset sample examples, and there are no dynamic sample examples. The preset sample examples refer to pre-prepared sample examples used to assist in the code conversion process.

[0097] In one feasible implementation, dynamic sample examples corresponding to the initial language code snippet can be retrieved from an example library. These retrieved dynamic sample examples are then formatted into a format acceptable to the code conversion model, ensuring there are no errors or inconsistencies. After completing these steps, suitable dynamic sample examples are extracted. Specifically, dynamic sample examples with similar logical structures and algorithmic functions to the initial language code snippet to be converted can be selected. These extracted dynamic sample examples are then combined into a few-sample example set.

[0098] Step S20: Construct prompt information based on the initial language code snippet and a small sample example set.

[0099] The prompt information is a set of example inputs and expected outputs used to guide the code conversion model in performing a specific task. In this application, the prompt information is dynamically constructed to guide the code conversion process. Specifically, the prompt information includes at least an initial language code snippet and a few-sample example set, and may also include a description of the task requirements.

[0100] In one feasible implementation, a generic prompt template is designed, which is populated with dynamic sample examples from a few-sample set. Specifically, the initial language code snippets, target language code snippets, and corresponding correspondences and conversion rules of the dynamic sample examples from the few-sample set can be inserted into the prompt template. Furthermore, the task requirement description can be explicitly stated in the prompt template; for example, the task requirement description could be "Convert the following source code into target code".

[0101] In another feasible implementation, since the code conversion model's capabilities are creative and uncertain, if a code conversion fails, a more diverse prompt template can be used for another attempt. Specifically, the prompt information can be optimized to ensure that the few-sample set contains diverse dynamic sample examples, thereby improving the model's generalization ability. For example, the number N of dynamic sample examples can be adjusted according to the actual situation to achieve the best learning effect; or, different orders of dynamic sample examples can be tried to find the optimal prompt sequence.

[0102] By using the above method, a small sample set can be dynamically created, thereby constructing an effective prompt message to guide the code conversion model in learning and conversion. This maximizes the use of the existing knowledge base to improve the performance of the code conversion model, while maintaining flexibility to adapt to ever-changing needs.

[0103] Step S30: Input the prompt information into the code conversion model to obtain the target language code fragment.

[0104] The code conversion model in this application can be a Large Language Model (LLM). A Large Language Model (LLM) possesses natural language understanding capabilities, can extract information from irregular natural language input by the user, and proactively asks the user questions when information is missing. The core technologies of a Large Language Model include pre-training and fine-tuning.

[0105] Pre-training: During the pre-training phase, large language models are trained using large-scale text datasets to learn language patterns, structures, and semantic information.

[0106] Fine-tuning: In the fine-tuning stage, the pre-trained large language model is applied to a specific task or dataset, and the model parameters are adjusted through supervised learning to adapt them to the requirements of the specific task.

[0107] It should be noted that the code conversion model used in this application can be a general large language model or a pre-trained special large language model, preferably a pre-trained special large language model. The pre-trained special large language model has powerful natural language processing capabilities and a certain amount of professional knowledge reserves, so as to accurately understand the prompt information and improve the quality of code conversion.

[0108] In one feasible implementation, the pre-trained dedicated large language model converts the initial language code fragment into the target language code fragment based on the input prompt information and using dynamic sample examples from the few sample example set in the prompt information as examples.

[0109] In this embodiment, prompt information is constructed based on an initial language code fragment and a corresponding few-sample example set. This prompt information is input into a code conversion model to obtain a target language code fragment converted from the initial language code fragment. This allows the code conversion model to learn the mapping relationship between the initial and target language code fragments. The few-sample example set includes dynamic sample examples, which include initial language code fragment samples, target language code fragment samples, the correspondence between the initial and target language code fragment samples, and conversion rules. The initial and target language code fragment samples demonstrate the code conversion results and rules, helping the code conversion model better understand the conversion process and generate target language code fragments that meet expectations. Based on the initial language code fragment and the few-sample example set, accurate prompt information can be constructed, clearly informing the code conversion model what type of code conversion is needed. This helps the code conversion model maintain the correct direction when generating target language code fragments, improving the accuracy of the generated target language code fragments.

[0110] Reference Figure 2 In some embodiments of this application, obtaining the initial language code fragment and the corresponding few-sample example set includes:

[0111] Step S11: Obtain the initial language code snippet.

[0112] Initial language code snippets can be uploaded by users; they can also be read from code development systems, code repositories, or specially created code collections through interfaces to meet the needs of obtaining initial language code snippets in different scenarios.

[0113] Step S12: Perform structured decomposition on the initial language code fragment to obtain a structured representation of the initial language code fragment.

[0114] Structured representations are used to represent the syntactic structure of initial language code snippets. These structured representations can be abstract syntax trees, control flow graphs, data flow graphs, or other intermediate representations; this embodiment uses an abstract syntax tree structure as an example.

[0115] In one feasible implementation, the initial language code snippets are first parsed using a suitable parser to obtain a syntax tree structure. Next, node classification is performed, including identifier extraction and logical structure identification. Then, a structured representation is generated, including node tagging. Finally, the code architecture is constructed, including building from basic units to the overall framework, defining relationships between components, and optimizing and validating the structure.

[0116] The following will provide a detailed introduction to each part:

[0117] The parser used in the above parsing process can be ANTLR, Pygments, etc.

[0118] The identifiers used in the above node classification process include variable names, function names, etc., from the initial language code snippet. All variable names, function names, and other identifiers can be extracted from the initial language code snippet. Logical structures include sequential structures, selection structures, and loop structures. Sequential structures can be obtained by recognizing simple statement sequences; selection structures can be obtained by recognizing conditional branches such as if-else statements and switch-case statements; and loop structures can be obtained by recognizing repeatedly executed statement blocks such as for loops and while loops.

[0119] The node labels in the above structured representation process can be assigned to each node with a specific label or type for subsequent processing.

[0120] The above-described code architecture construction process involves building from basic units to the overall framework. This includes starting with basic code components such as functions, classes, and modules, and gradually constructing the structural framework of the entire project. This step aims to clearly demonstrate the relationships between the various components and their position within the system. Defining the relationships between components involves, at a high level, defining dependencies, interaction patterns, and data flow methods between components to build a model reflecting the project's logical structure. This process is not limited to a tree structure; it can also be a graph structure or other forms suitable for expressing the project's characteristics. Structural optimization and verification involve further optimizing component design based on the constructed architectural model to ensure the system's scalability and maintainability, and verifying the effectiveness and stability of the architecture through code review, testing, and other methods.

[0121] Step S13: Convert the structured representation into a vector representation of the initial language code fragment.

[0122] Vector representation refers to converting the extracted identifiers and logical structures into corresponding vector representations. Specifically, it can be converting variable names into vector representations or converting keywords of logical structures into vector representations.

[0123] In one feasible implementation, feature extraction and mapping can be performed on the structured representation to convert the structured representation of the initial language code fragment into a vector representation of the initial language code fragment. By extracting features from the structured representation, each feature is mapped to a vector space. For example, for text data, models or methods such as the bag-of-words model, term frequency-inverse document frequency, Word2Vec, and BERT can be used to convert text into vectors.

[0124] In another feasible implementation, the structured representation can be input into the embedding model, transforming the structured representation of the initial language code snippet into a vector representation of the initial language code snippet. An embedding model is a method that maps data structures to a low-dimensional vector space, aiming to preserve semantic relationships between data. For example, the Word2Vec model can be used to map words or phrases to vectors while preserving their semantic similarity, and the GloVe model can be used to learn vector representations of words through global contextual information.

[0125] In another feasible implementation, the structured representation can be input into a neural network model to convert the structured representation of the initial language code snippet into a vector representation of the initial language code snippet. The neural network model can convert structured data into vectors by learning the representation of the data. For example, a convolutional neural network, a recurrent neural network, an attention mechanism, or a Transformer model can be used to obtain the vector representation of the initial language code snippet.

[0126] Step S14: Retrieve dynamic sample examples that match the vector representation from the first example library to generate a small sample example set.

[0127] The first example library can store dynamic sample examples of one code conversion type, meaning it can include dynamic sample examples of the same code conversion type as the initial language code snippet sample and the target language code snippet sample. For example, assuming the initial language code snippet sample is in C and the target language code snippet sample is in C++, then the dynamic sample example would be a dynamic sample example converting C to C++. The first example library can also include dynamic sample examples of multiple different code conversion types. For instance, it could include dynamic sample examples converting C to C++, C++ to Python, and C to Python, etc., to meet the code conversion needs of different types.

[0128] After obtaining the vector representation of the initial language code snippet, matching dynamic sample examples can be retrieved from the first example library based on the vector representation, and a small sample example set can be generated based on the retrieved dynamic sample examples.

[0129] In one feasible implementation, the construction process of the first example library can be as follows: Collect a large number of dynamic sample examples composed of initial language code snippets and target language code snippets from existing open-source projects, code repositories, or specially created code collections; filter and clean the collected dynamic sample examples, removing invalid or irrelevant code segments to ensure the quality of the first example library. Each code snippet is labeled, for example, with its function, purpose, input / output information, etc., to enable more accurate matching during subsequent retrieval. Next, the initial language code snippets are structurally decomposed, converting their variables, logical structures, etc., into vector representations. Then, an efficient vector storage system, such as Faiss, Pinecone, or Elasticsearch, is selected. These systems support fast vector retrieval, and the vector representations obtained from the initial language code snippets are stored in the vector storage system. The vector storage is used to store the sample examples after vector representation to achieve efficient retrieval. Finally, metadata is attached to each stored vector representation, including but not limited to the original language, target language, functional description, and input / output examples of the initial language code snippet. An index is built to accelerate the similarity query process and ensure the efficiency of retrieval operations. Through the above methods, the vector representation is stored, ensuring that each dynamic sample example in the first example library has a clear vector representation.

[0130] In another feasible implementation, a preset vector representation similar to the vector representation of the initial language code fragment is retrieved from the first example library, and dynamic sample examples associated with this similar preset vector representation are extracted. For example, dynamic sample examples associated with vector representations whose similarity to the vector representation of the initial language code fragment is greater than a preset similarity can be extracted from the first example library, and all extracted dynamic sample examples are combined to obtain a small sample example set. Specifically, the cosine similarity or Euclidean distance between the vector representation of the initial language code fragment and the preset vector representations of each dynamic sample example in the first example library can be calculated to extract similar vector representations from the first example library.

[0131] In this embodiment, the initial language code fragment is structurally decomposed to obtain a structured representation of the initial language code fragment. This structured representation is then converted into a vector representation. Finally, dynamic sample examples matching the vector representation of the initial language code fragment are retrieved from a first example library to generate a small sample example set. By structurally decomposing the initial language code fragment, it can be broken down into smaller, easier-to-understand and process units. The structured representation helps reveal the internal logic and structure of the code, making code analysis more accurate and efficient. Furthermore, when using the vector representation of the initial language code fragment for retrieval, the sample example that best matches a given vector representation can be found by calculating the similarity between vectors. This method can more accurately reflect the similarity and relationships between codes, thereby improving retrieval accuracy. In addition, the vector representation of the initial language code fragment typically has low dimensionality, which reduces the computational cost when calculating similarity and improves retrieval efficiency.

[0132] Due to syntactic and semantic differences between different programming languages, even state-of-the-art compiler-driven methods may not be able to fully capture all the details, especially when dealing with complex control flow and data structures. This imprecision can cause the converted code to malfunction in certain situations. Therefore, to address this issue, in some embodiments of this application, converting the structured representation into a vector representation of the original language code snippet includes:

[0133] Step S131: The structured representation of the input word embedding model is used to obtain the word vector representation of the initial language code fragment.

[0134] Word embedding is a method for converting words or phrases in text into vector representations that capture not only the meaning of words but also the semantic relationships between them. In this application, word embedding is used to convert words in the structured representation of an initial language code fragment into corresponding word vector representations for subsequent similarity comparisons.

[0135] The word embedding model can be one of Word2Vec, GloVe, or BERT. These word embedding models can convert the words in the structured representation of the initial language code snippet into the corresponding word vector representation.

[0136] Word embedding models can be either pre-trained or self-trained.

[0137] The aforementioned pre-trained models refer to models pre-trained on large datasets. These models typically perform well on certain general tasks and can serve as a starting point for subsequent specific tasks. Their basic principle can be summarized as "pre-training + transfer learning + fine-tuning." First, a deep neural network model is pre-trained on a large-scale dataset to obtain the model's parameters. Then, through transfer learning, the parameters of the pre-trained model are used as initialization parameters for training other related tasks. Finally, based on transfer learning, the model's parameters are fine-tuned to adapt to the new task. If this application uses a pre-trained model, it directly loads the pre-trained model to obtain word embeddings specific to the programming language.

[0138] The self-trained model described above utilizes existing labeled data and an initial model to generate pseudo-labeled data, thereby expanding the training set and improving model performance. Through continuous iterative training, the model can gradually learn more features and patterns, thus improving its performance on new tasks. If this application adopts a self-trained model, a large amount of code dataset is required to train the model to obtain word embeddings specific to programming language characteristics.

[0139] In one feasible implementation, inputting the structured representation into a word embedding model to obtain word vector representations of the initial language code fragment includes: First, converting the structured representation of the initial language code fragment into a format that the word embedding model can process. Specifically, each node in the structured representation is converted into one or more tokens, which typically involves extracting information such as the node's type, value, and attributes. The tokenized nodes are serialized into a linear sequence so that the word embedding model can process them; this may require considering the node traversal order, such as depth-first search or breadth-first search. A vocabulary is constructed from the serialized token sequence, where each unique token has a unique index. Next, a suitable word embedding model is selected. Finally, using the selected word embedding model, the serialized token sequence is converted into a word vector sequence. Specifically, for each serialized token, its corresponding index is looked up using the vocabulary, and the word embedding model is used to convert that index into a word vector. The word vectors of each token are sequentially combined into a word vector sequence, which represents the vector representation of the structured representation.

[0140] And / or, in step S132, the structured representation is input into the semantic embedding model to obtain the semantic vector representation of the initial language code fragment.

[0141] Semantic embedding is a technique that transforms text fragments, such as sentences, paragraphs, or even documents, into vector representations that reflect their deeper meaning. These vectors not only accurately represent the content of the text but also capture the semantic similarities and connections between texts. In this application, semantic embedding technology is applied to convert code blocks of a certain length into semantic vectors for efficient semantic comparison and matching.

[0142] Semantic embedding models can be BERT, ELMo, etc., which can convert code text fragments into semantic vector representations that capture their semantic features.

[0143] The semantic embedding model can be a pre-trained model or a self-trained model. If this application uses a pre-trained model, the pre-trained model is directly loaded to obtain semantic embeddings specific to the programming language characteristics; if this application uses a self-trained model, a large amount of code data is required to train the model to obtain semantic embeddings specific to the programming language characteristics.

[0144] In one feasible implementation, inputting the structured representation into a semantic embedding model to obtain a semantic vector representation of the initial language code fragment includes: for each field in the structured representation, converting it into a vector representation using an appropriate feature extraction method, such as using a word embedding model to convert text fields into word vectors. The vectors extracted from each field are then concatenated or combined to form a semantic vector representation of the entire structured data.

[0145] In another feasible implementation, inputting the structured representation into a semantic embedding model to obtain a semantic vector representation of the initial language code fragment includes: passing each field in the structured representation as input to the ELMo model; the ELMo model processes the input fields and generates context-dependent word vectors for each word; extracting the word vectors for each word from the output of the ELMo model, these word vectors contain contextual information and reflect the different meanings of the word in different contexts; for each field in the structured data, combining its corresponding word vectors, the combination method can be simple concatenation, weighted summation, or a more complex attention mechanism. Finally, a semantic vector representation of the entire structured data is obtained.

[0146] In this embodiment, by structurally decomposing the initial language code fragments and using word embedding models and semantic embedding models for vectorization, word vector representations and semantic vector representations are obtained. This can more accurately capture the local and global syntactic and semantic features of the source code, thereby improving the accuracy of the conversion.

[0147] Reference Figure 3In some embodiments of this application, the example library may include a first example library and a second example library. The first example library includes dynamic sample examples corresponding to the initial language code snippets that have undergone code conversion. The first example library includes preset sample examples. Specifically, after converting the structured representation into a vector representation of the initial language code snippets, it further includes:

[0148] Step S11: Obtain the initial language code snippet.

[0149] Step S12: Perform structured decomposition on the initial language code fragment to obtain a structured representation of the initial language code fragment.

[0150] Step S13: Convert the structured representation into a vector representation of the initial language code fragment.

[0151] Step S15: Retrieve dynamic sample examples that match the vector representation from the first sample library, and retrieve preset sample examples that match the vector representation from the second sample library.

[0152] The first example library refers to a dynamically built collection of examples during the conversion process. It contains code snippets that have been successfully converted and are used to further optimize subsequent conversion tasks. The second example library refers to a pre-prepared collection of examples used to assist in the code conversion process.

[0153] In one feasible implementation, a preset vector representation of a dynamic sample example similar to the vector representation of the initial language code fragment is retrieved from a first example library, and dynamic sample examples associated with the preset vector representations of these similar dynamic sample examples are extracted. For example, dynamic sample examples associated with preset vector representations whose similarity to the vector representation of the initial language code fragment is greater than a preset similarity can be extracted from the first example library. Here, the cosine similarity or Euclidean distance between the vector representation of the initial language code fragment and the preset vector representations of each dynamic sample example in the first example library can be calculated to retrieve dynamic sample examples matching the vector representations from the first example library.

[0154] In another feasible implementation, a preset vector representation of a preset sample example that is similar to the vector representation of the initial language code fragment is retrieved from the second example library, and preset sample examples associated with the preset vector representations of these similar preset sample examples are extracted. For example, preset sample examples associated with preset vector representations that have a similarity greater than a preset similarity to the vector representation of the initial language code fragment can be extracted from the second example library. Here, the cosine similarity or Euclidean distance between the vector representation of the initial language code fragment and the preset vector representations of each preset sample example in the second example library can be calculated to retrieve preset sample examples that match the vector representations from the second example library.

[0155] Step S16: After merging the preset sample examples and dynamic sample examples, perform similarity sorting and filtering to obtain a small sample example set.

[0156] There may be one or more matching dynamic sample examples retrieved from the first sample library, and there may be one or more matching preset sample examples retrieved from the second sample library.

[0157] The dynamic sample examples retrieved from the first sample library may be the same as or different from the preset sample examples retrieved from the first sample library. Therefore, after retrieving the preset sample examples and dynamic sample examples, operations such as merging, deduplication, sorting, and filtering can be performed on the preset sample examples and dynamic sample examples to obtain a smaller sample example set.

[0158] In one feasible implementation, dynamic sample examples retrieved from a first sample library are merged with preset sample examples retrieved from a second sample library. If there are duplicate sample examples between the dynamic sample examples retrieved from the first sample library and the preset sample examples retrieved from the second sample library, the one with the highest similarity score is retained. The merged sample examples are then sorted again according to the similarity score. Next, the N most relevant sample examples are selected from the merged sample examples to form a minority sample set. Here, N is an integer of 1 or greater than 1.

[0159] Since the retrieval process can calculate similarity scores using vector representation, each retrieved preset sample example, each dynamic sample example, and the initial language code fragment have corresponding similarity scores. Therefore, the above operations such as deduplication and sorting can be performed based on the similarity scores.

[0160] In this embodiment of the application, information can be retrieved from two different example libraries simultaneously. One example library contains preset sample examples, while the other example library contains dynamic sample examples corresponding to the initial language code snippets that have undergone code conversion. This can effectively find the most relevant sample examples to the initial language code snippets in the two different example libraries, improving the accuracy and efficiency of retrieval, and providing valuable information for dynamic few-shot learning.

[0161] Reference Figure 4 In some embodiments of this application, based on the above embodiments, the vector representation includes word vector representation and semantic vector representation. Retrieving dynamic sample examples that match the vector representation from the first example library includes:

[0162] Step S151: Determine the first similarity score between the word vector representation and each dynamic sample example in the first example library, and determine the second similarity score between the semantic vector representation and each dynamic sample example in the first example library.

[0163] The cosine similarity or Euclidean distance between the word vector representation of the initial language code fragment and the preset word vector representation of each dynamic sample example in the first example library can be calculated to obtain a first similarity score. Simultaneously, the cosine similarity or Euclidean distance between the semantic vector representation of the initial language code fragment and the preset semantic vector representation of each dynamic sample example in the first example library can be calculated to obtain a second similarity score.

[0164] Step S152: Determine the corresponding dynamic sample example based on the first similarity score and the second similarity score.

[0165] In one feasible implementation, dynamic sample examples with a first similarity score greater than a first preset similarity score can be obtained, and dynamic sample examples with a second similarity score greater than a second preset similarity score can be obtained. A small sample example set is generated based on the dynamic sample examples with the first similarity score greater than the first preset similarity score and the dynamic sample examples with the second similarity score greater than the second preset similarity score.

[0166] In another feasible implementation, dynamic sample examples with a first similarity score greater than a first preset similarity score can be obtained, as well as dynamic sample examples with a second similarity score greater than a second preset similarity score. The dynamic sample examples with the first similarity score greater than the first preset similarity score are sorted by similarity, and the top N similar first dynamic sample examples are selected. Simultaneously, the dynamic sample examples with the second similarity score greater than the second preset similarity score are sorted by similarity, and the top N similar second dynamic sample examples are selected. A few-sample example set is generated based on the top N similar first dynamic sample examples and the top N similar second dynamic sample examples. It should be noted that if the top N similar first dynamic sample examples and the top N similar second dynamic sample examples contain identical dynamic sample examples, deduplication can be performed to obtain an accurate few-sample example set.

[0167] Wherein, N can be 1 or an integer greater than 1, such as 2 or 3. The first preset similarity score and the second preset similarity score can be the same or different, and can be set or adjusted according to the actual situation.

[0168] In this embodiment, retrieval is performed using both word vectors and semantic vectors, capturing different features of code or text at both the lexical and semantic levels. This multi-dimensional matching helps improve the comprehensiveness of the retrieval, making the results more aligned with actual needs. Similarity ranking further filters out dynamic sample examples that best match the given vector representation. By calculating the similarity between vectors, the closeness between dynamic sample examples and the given vector representation can be quantified, resulting in more accurate retrieval results. Furthermore, since the first example library stores a dynamically constructed set of examples during the conversion process, containing successfully converted code snippets, it better reflects the specific needs of the current problem, improving conversion quality and efficiency.

[0169] Reference Figure 5 In some embodiments of this application, based on the above embodiments, the vector representation includes word vector representation and semantic vector representation. Retrieving preset sample examples matching the vector representation from the second example library includes:

[0170] Step S153: Determine the third similarity score between the word vector representation and each preset sample example in the second example library, and determine the fourth similarity score between the semantic vector representation and each preset sample example in the second example library.

[0171] The cosine similarity or Euclidean distance between the word vector representation of the initial language code fragment and the preset word vector representation of each preset sample example in the second example library can be calculated to obtain a third similarity score. Simultaneously, the cosine similarity or Euclidean distance between the semantic vector representation of the initial language code fragment and the preset semantic vector representation of each preset sample example in the second example library can be calculated to obtain a fourth similarity score.

[0172] Step S154: Determine the corresponding preset sample example based on the third similarity score and the fourth similarity score.

[0173] In one feasible implementation, preset sample examples with a third similarity score greater than a third preset similarity score can be obtained, as well as preset sample examples with a fourth similarity score greater than a fourth preset similarity score. Based on the preset sample examples with the third similarity score greater than the third preset similarity score and the preset sample examples with the fourth similarity score greater than the fourth preset similarity score, a corresponding few-sample example set is generated.

[0174] In another feasible implementation, preset sample examples with a third similarity score greater than a third preset similarity score can be obtained, as well as preset sample examples with a fourth similarity score greater than a fourth preset similarity score. The preset sample examples with a third similarity score greater than the third preset similarity score are sorted by similarity, and the top N similar third dynamic sample examples are selected. Simultaneously, the preset sample examples with a fourth similarity score greater than the fourth preset similarity score are sorted by similarity, and the top N similar fourth dynamic sample examples are selected. A few-sample example set is generated based on the top N similar third dynamic sample examples and the top N similar fourth dynamic sample examples. It should be noted that if the top N similar third dynamic sample examples and the top N similar fourth dynamic sample examples contain the same dynamic sample example, deduplication can be performed to obtain an accurate few-sample example set.

[0175] Wherein, N can be 1 or an integer greater than 1, such as 2 or 3. The third preset similarity score and the fourth preset similarity score can be the same or different, and can be set or adjusted according to the actual situation.

[0176] In this embodiment, retrieval is performed using both word vectors and semantic vectors, capturing different features of code or text at the lexical and semantic levels. This multi-dimensional matching helps improve the comprehensiveness of the retrieval, making the search results more aligned with actual needs. Similarity ranking further filters out preset sample examples that best match the given vector representation. By calculating the similarity between vectors, the closeness between the preset sample examples and the given vector representation can be quantified, resulting in more accurate search results.

[0177] In some embodiments of this application, since a project may include different functions, each function can represent a node, thus resulting in different code nodes. Each code node corresponds to an initial language code fragment, which needs to be converted into the corresponding target language code fragment. During the conversion process, each initial language code fragment can be converted sequentially to obtain the target language code fragment for each initial language code fragment. Alternatively, parallel conversion of initial language code fragments can be performed, simultaneously converting multiple initial language code fragments into their corresponding target language code fragments to improve conversion efficiency. Considering that there may be nesting between initial language code fragments, i.e., the conversion of a later initial language code fragment needs to consider the conversion result of the previous initial language code fragment, this application uses a serial code conversion method as an example.

[0178] Specifically, after inputting the prompt information into the code conversion model and obtaining the target language code snippet, the process also includes:

[0179] Step S110: Evaluate the target language code segment after the conversion of the current initial language code segment to obtain the evaluation result corresponding to the target language code segment.

[0180] The current initial language code snippet can be the code snippet corresponding to the first code node in the code conversion, or it can be the code snippet corresponding to a code node that is not the last code node in the code conversion.

[0181] The evaluation result is used to assess the quality of the target language code fragment converted from the current initial language code fragment, and it can be a score.

[0182] In one feasible implementation, the converted target language code fragment can be evaluated from the perspectives of syntax detection, functional verification, and performance evaluation to obtain the evaluation results corresponding to the target language code fragment.

[0183] In another feasible implementation, if the target language code snippet conforms to the set syntax rules, and / or, the target language code snippet passes the expected functional verification, and / or, the target language code snippet meets the set performance requirements, the evaluation result is determined to meet the preset evaluation result. Here, functional verification can be performed through unit testing or other methods to verify whether the generated target language code snippet implements the expected function.

[0184] In another feasible implementation, corresponding weights can be assigned to syntax detection, functional verification, and performance evaluation. If the target language code segment conforms to the set syntax rules, the score for the syntax detection item is obtained; if the target language code segment passes the expected functional verification, the score for the functional verification item is obtained; if the target language code segment meets the set performance requirements, the score for the performance evaluation is obtained; finally, the scores for the syntax detection item, the functional verification item, and the performance evaluation item, along with the weights corresponding to each item, are weighted and summed to obtain the evaluation result.

[0185] Step S120: If the evaluation result meets the preset evaluation result, then proceed with the code conversion of the next initial language code segment until the conversion of all initial language code segments is completed.

[0186] The preset evaluation result can be fixed according to the actual situation. If the evaluation result meets the preset evaluation result, the code conversion of the initial language code fragment corresponding to the next code node is performed, and the above operation is repeated until the code conversion of the initial language code fragments corresponding to all code nodes is completed.

[0187] If the evaluation result does not meet the preset evaluation result, return to step S14 to retrieve dynamic sample examples that match the vector representation from the first example library and generate a small sample example set.

[0188] If the evaluation result does not meet the preset evaluation result, it indicates that the current initial language code snippet conversion failed or the code quality is poor. In this case, a few-sample example set can be generated by retrieving dynamic sample examples matching the vector representation from the first example library, or by retrieving dynamic sample examples matching the vector representation from the first example library, and a preset sample example set matching the vector representation from the second example library. The preset sample examples and dynamic sample examples are then merged, sorted by similarity, and filtered to obtain the few-sample example set. By redefining the few-sample example set, the code conversion quality can be improved.

[0189] In this embodiment, the initial language code fragment corresponding to each code node is sequentially converted and evaluated to obtain the target language code fragment corresponding to the initial language code fragment of each code node, thereby improving the quality of code conversion.

[0190] In other embodiments, if the evaluation result of the current initial language code segment fails to meet the preset evaluation result after a preset number of consecutive evaluations, an alarm message for the current initial language code segment is generated, and code conversion can be performed by manual intervention.

[0191] In other embodiments, if the conversion of the current initial language code snippet fails, the reason for the code conversion failure and detailed information are recorded. A retry mechanism can also be set up to try different prompts or parameter settings of the code conversion model to improve the success rate of code conversion.

[0192] Reference Figure 6 In some embodiments of this application, the code conversion method further includes:

[0193] Step S210: Associate the initial language code fragment whose evaluation result meets the preset evaluation result with its corresponding target language code fragment to obtain a dynamic sample example.

[0194] Step S220: Store the dynamic sample examples in the first sample library.

[0195] If the evaluation result of the target language code fragment converted from the current initial language code fragment meets the preset evaluation result, the current initial language code fragment and the target language code fragment are associated to obtain a dynamic sample example. If the evaluation result of the target language code fragment converted from the initial language code fragment of the next code node also meets the preset evaluation result, the initial language code fragment of the next code node can also be associated with its converted target language code fragment to obtain a dynamic sample example. The dynamic sample examples obtained by the above association can be stored in the first example library for subsequent code nodes to use for code conversion of the initial language code fragments.

[0196] In this embodiment of the application, an initial language code segment whose evaluation result meets the preset evaluation result is associated with its corresponding target language code segment to obtain a dynamic sample example. The dynamic sample example is then stored in a first example library, thereby enriching the dynamic sample examples in the first example library. As more effective examples are gradually accumulated during the conversion process, the retrieval quality and the conversion quality of subsequent code nodes can be further improved.

[0197] In some embodiments of this application, the code conversion method further includes: constructing prompt information based on the initial language code fragment, the context information of the initial language code fragment, and a few sample example set.

[0198] The context information of the initial language code fragment specifically includes the initial language code fragments corresponding to the code nodes that have successfully undergone code conversion before the code node corresponding to the current initial language code fragment.

[0199] In this embodiment, the context information of the initial language code fragment is combined with the context information implied by the dynamic sample examples in the few-sample example set, making the context information of the initial language code fragment more comprehensive and improving the accuracy of code conversion.

[0200] It should be noted that the above examples are only for understanding this application and do not constitute a limitation on the code conversion method of this application. Any simple transformations based on this technical concept are all within the protection scope of this application.

[0201] Based on the same inventive concept, this application proposes a method for generating an example set. In some embodiments of this application, the method for generating the example set includes:

[0202] Step B11: Obtain the initial language code snippet.

[0203] Step B12: Perform a structured decomposition on the initial language code fragment to obtain a structured representation of the initial language code fragment.

[0204] Step B13 converts the structured representation into a vector representation of the initial language code snippet.

[0205] Step B14: Retrieve sample examples whose vector representations match from the example library to generate a small sample example set.

[0206] In this embodiment, the initial language code fragment is structurally decomposed to obtain a structured representation of the initial language code fragment. This structured representation is then converted into a vector representation. Finally, sample examples matching the vector representation of the initial language code fragment are retrieved from an example library, generating a small sample example set. By structurally decomposing the initial language code fragment, it can be broken down into smaller, easier-to-understand and process units. The structured representation helps reveal the internal logic and structure of the code, making code analysis more accurate and efficient. Furthermore, when using the vector representation of the initial language code fragment for retrieval, the similarity between vectors can be calculated to find the sample example that best matches a given vector representation. This method more accurately reflects the similarity and relationships between codes, thereby improving retrieval accuracy. In addition, the vector representation of the initial language code fragment typically has low dimensionality, which reduces computational cost when calculating similarity and improves retrieval efficiency.

[0207] Due to syntactic and semantic differences between different programming languages, even state-of-the-art compiler-driven methods may not be able to fully capture all the details, especially when dealing with complex control flow and data structures. This imprecision can cause the converted code to malfunction in certain situations. Therefore, to address this issue, in some embodiments of this application, converting the structured representation into a vector representation of the original language code snippet includes:

[0208] Step B131 involves embedding the structured representation into the word embedding model to obtain word vector representations of the initial language code fragments.

[0209] And / or, in step B132, the structured representation is input into the semantic embedding model to obtain the semantic vector representation of the initial language code fragment; wherein, the vector representation includes: word vector representation and / or semantic vector representation.

[0210] In this embodiment, by structurally decomposing the initial language code fragments and using word embedding models and semantic embedding models for vectorization, word vector representations and semantic vector representations are obtained. This can more accurately capture the local and global syntactic and semantic features of the source code, thereby improving the accuracy of the conversion.

[0211] In some embodiments of this application, the example library includes a first example library and a second example library. The first example library includes dynamic sample examples corresponding to the initial language code snippets that have undergone code conversion, and the second example library includes preset sample examples. Specifically, retrieving sample examples that match the vector representation from the example library to generate a small sample example set includes:

[0212] Step B15: Retrieve dynamic sample examples that match the vector representation from the first sample library, and retrieve preset sample examples that match the vector representation from the second sample library.

[0213] Step B16: After merging the preset sample examples and dynamic sample examples, perform similarity sorting and filtering to obtain a small sample example set.

[0214] In this embodiment of the application, information can be retrieved from two different example libraries simultaneously. One example library contains preset sample examples, while the other example library contains dynamic sample examples corresponding to the initial language code snippets that have undergone code conversion. This can effectively find the most relevant sample examples to the initial language code snippets in the two different example libraries, improving the accuracy and efficiency of retrieval, and providing valuable information for dynamic few-shot learning.

[0215] In some embodiments of this application, the vector representation includes word vector representation and semantic vector representation. Retrieving preset sample examples that match the vector representation from the first example library includes:

[0216] Step B151: Determine the first similarity score between the word vector representation and each dynamic sample example in the first example library, and determine the second similarity score between the semantic vector representation and each dynamic sample example in the first example library.

[0217] Step B152: Determine the corresponding dynamic sample example based on the first similarity score and the second similarity score.

[0218] In this embodiment, retrieval is performed using both word vectors and semantic vectors, capturing different features of code or text at both the lexical and semantic levels. This multi-dimensional matching helps improve the comprehensiveness of the retrieval, making the results more aligned with actual needs. Similarity ranking further filters out dynamic sample examples that best match the given vector representation. By calculating the similarity between vectors, the closeness between dynamic sample examples and the given vector representation can be quantified, resulting in more accurate retrieval results. Furthermore, since the first example library stores a dynamically constructed set of examples during the conversion process, containing successfully converted code snippets, it better reflects the specific needs of the current problem, improving conversion quality and efficiency.

[0219] In some embodiments of this application, based on the above embodiments, the vector representation includes word vector representation and semantic vector representation. Retrieving dynamic sample examples that match the vector representation from the second example library includes:

[0220] Step B153: Determine the third similarity score between the word vector representation and each preset sample example in the second example library, and determine the fourth similarity score between the semantic vector representation and each preset sample example in the second example library.

[0221] Step B154: Determine the corresponding preset sample example based on the third similarity score and the fourth similarity score.

[0222] In this embodiment, retrieval is performed using both word vectors and semantic vectors, capturing different features of code or text at the lexical and semantic levels. This multi-dimensional matching helps improve the comprehensiveness of the retrieval, making the search results more aligned with actual needs. Similarity ranking further filters out preset sample examples that best match the given vector representation. By calculating the similarity between vectors, the closeness between the preset sample examples and the given vector representation can be quantified, resulting in more accurate search results.

[0223] The specific implementation methods of each step in the method for generating the example set in this application can be referred to the description of the corresponding steps in the code conversion method, which is the same as the description of the corresponding steps in the code conversion method, and will not be repeated here.

[0224] It should be noted that the above examples are only for understanding this application and do not constitute a limitation on the method of generating the example set of this application. Any simple modifications based on this technical concept are within the protection scope of this application.

[0225] Based on the same inventive concept, this application also provides a code conversion device, please refer to... Figure 7 The code conversion device includes:

[0226] The first acquisition module 10 is used to acquire an initial language code fragment and a small sample example set corresponding to the initial language code fragment. The small sample example set includes dynamic sample examples, which include initial language code fragment samples, target language code fragment samples, the correspondence and conversion rules between the initial language code fragment samples and the target language code fragment samples.

[0227] Module 20 is used to construct prompt messages based on the initial language code snippets and a small sample set of examples.

[0228] The first conversion module 30 is used to input the prompt information into the code conversion model to obtain the target language code fragment.

[0229] Optionally, the first acquisition module 10 is further configured to: acquire an initial language code fragment; perform a structured decomposition on the initial language code fragment to obtain a structured representation of the initial language code fragment; convert the structured representation into a vector representation of the initial language code fragment; and retrieve dynamic sample examples that match the vector representation from the first example library to generate a small sample example set.

[0230] Optionally, the first acquisition module 10 is further configured to: input the structured representation into the word embedding model to obtain the word vector representation of the initial language code fragment; and / or input the structured representation into the semantic embedding model to obtain the semantic vector representation of the initial language code fragment; wherein the vector representation includes: word vector representation and / or semantic vector representation.

[0231] Optionally, the first acquisition module 10 is further configured to: retrieve dynamic sample examples matching the vector representation from the first example library, and retrieve preset sample examples matching the vector representation from the second example library; merge the preset sample examples and dynamic sample examples, and then perform similarity sorting and filtering to obtain a small sample example set; wherein the example library includes the first example library and the second example library, the first example library includes dynamic sample examples corresponding to the initial language code fragments that have undergone code conversion, and the second example library includes preset sample examples.

[0232] Optionally, the first acquisition module 10 is further configured to: determine a first similarity score between the word vector representation and each dynamic sample example in the first example library, and determine a second similarity score between the semantic vector representation and each dynamic sample example in the first example library; and determine the corresponding dynamic sample example based on the first similarity score and the second similarity score.

[0233] Optionally, the first acquisition module 10 is further configured to: determine a third similarity score between the word vector representation and each preset sample example in the second example library, and determine a fourth similarity score between the semantic vector representation and each preset sample example in the second example library; and determine the corresponding preset sample example based on the third similarity score and the fourth similarity score.

[0234] Optionally, the code conversion device further includes an evaluation module, which is used to: evaluate the target language code fragment after the current initial language code fragment is converted, and obtain the evaluation result corresponding to the target language code fragment; if the evaluation result meets the preset evaluation result, then perform the code conversion of the next initial language code fragment, until the conversion of all initial language code fragments is completed; if the evaluation result does not meet the preset evaluation result, then return to the execution of retrieving dynamic sample examples that match the vector representation from the first example library and generating a small sample example set.

[0235] Optionally, the evaluation module is also used to: determine that the evaluation result meets the preset evaluation result if the target language code snippet conforms to the set syntax rules, and / or the target language code snippet passes the expected function verification, and / or the target language code snippet meets the set performance requirements.

[0236] Optionally, the code conversion device further includes a storage module, which is used to: associate the current initial language code segment whose evaluation result meets the preset evaluation result with its corresponding target language code segment to obtain a dynamic sample example; and store the dynamic sample example in a first example library.

[0237] Optionally, the construction module 30 is also used to: construct prompt information based on the initial language code snippet, the context information of the initial language code snippet, and a few sample example set.

[0238] The code conversion apparatus provided in this application, employing the code conversion method described in the above embodiments, can solve the technical problem of low accuracy in code conversion. Compared with the prior art, the beneficial effects of the code conversion apparatus provided in this application are the same as those of the code conversion method described in the above embodiments, and other technical features in the code conversion apparatus are the same as those disclosed in the methods of the above embodiments, and will not be repeated here.

[0239] Based on the same inventive concept, this application also provides an apparatus for generating an example set, please refer to... Figure 8 The apparatus for generating the example set includes:

[0240] The second acquisition module 40 is used to acquire the initial language code fragment;

[0241] The structured representation module 50 is used to perform structured decomposition on the initial language code fragment to obtain a structured representation of the initial language code fragment;

[0242] The second conversion module 60 is used to convert the structured representation into a vector representation of the initial language code fragment;

[0243] The generation module 70 is used to retrieve sample examples that match the vector representation from the example library and generate a small sample example set.

[0244] Optionally, the second conversion module 60 is used to: input the structured representation into the word embedding model to obtain the word vector representation of the initial language code fragment; and / or input the structured representation into the semantic embedding model to obtain the semantic vector representation of the initial language code fragment; wherein the vector representation includes: word vector representation and / or semantic vector representation.

[0245] Optionally, the generation module 70 is further configured to: retrieve dynamic sample examples matching the vector representation from the first example library, and retrieve preset sample examples matching the vector representation from the second example library; merge the preset sample examples and dynamic sample examples, and then perform similarity sorting and filtering to obtain a small sample example set; wherein the example library includes the first example library and the second example library, the first example library includes dynamic sample examples corresponding to the initial language code fragments that have undergone code conversion, and the second example library includes preset sample examples.

[0246] Optionally, the generation module 70 is further configured to: determine a first similarity score between the word vector representation and each dynamic sample example in the first example library, and determine a second similarity score between the semantic vector representation and each dynamic sample example in the first example library; and determine the corresponding dynamic sample example based on the first similarity score and the second similarity score.

[0247] Optionally, the generation module 70 is further configured to: determine a third similarity score between the word vector representation and each preset sample example in the second example library, and determine a fourth similarity score between the semantic vector representation and each preset sample example in the second example library; and determine the corresponding preset sample example based on the third similarity score and the fourth similarity score.

[0248] The example set generation apparatus provided in this application, employing the example set generation method in the above embodiments, can solve the technical problem of low accuracy in code conversion. Compared with the prior art, the beneficial effects of the example set generation apparatus provided in this application are the same as those of the example set generation method provided in the above embodiments, and other technical features in the example set generation apparatus are the same as those disclosed in the methods of the above embodiments, and will not be repeated here.

[0249] Based on the same inventive concept, this application provides an electronic device, which includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to perform the code conversion method in the above embodiments, or to enable the at least one processor to perform the example set generation method in the above embodiments.

[0250] The following is for reference. Figure 9The diagram illustrates a structural schematic of an electronic device suitable for implementing embodiments of this application. The electronic devices in these embodiments may include, but are not limited to, mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Descriptions), PMPs (Portable Media Players), in-vehicle terminals (e.g., in-vehicle navigation terminals), and fixed terminals such as digital TVs and desktop computers. Figure 9 The electronic device shown is merely an example and should not impose any limitation on the functionality and scope of use of the embodiments of this application.

[0251] like Figure 9 As shown, the electronic device may include a processing unit 1001 (e.g., a central processing unit, a graphics processing unit, etc.), which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 1002 or a program loaded from a storage device 1003 into a random access memory (RAM) 1004. The RAM 1004 also stores various programs and data required for the operation of the electronic device. The processing unit 1001, ROM 1002, and RAM 1004 are interconnected via a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Typically, the following systems can be connected to the I / O interface 1006: input devices 1007 including, for example, touchscreens, touchpads, keyboards, mice, image sensors, microphones, accelerometers, gyroscopes, etc.; output devices 1008 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; storage devices 1003 including, for example, magnetic tapes, hard disks, etc.; and communication devices 1009. Communication device 1009 allows electronic devices to communicate wirelessly or wiredly with other devices to exchange data. While electronic devices with various systems are shown in the figures, it should be understood that implementation or possession of all the systems shown is not required. More or fewer systems may be implemented alternatively.

[0252] Specifically, according to the embodiments disclosed in this application, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments disclosed in this application include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device, or installed from storage device 1003, or installed from ROM 1002. When the computer program is executed by processing device 1001, it performs the functions defined in the methods of the embodiments disclosed in this application.

[0253] The electronic device provided in this application, employing the code conversion method described in the above embodiments, can solve the technical problem of low accuracy in code conversion. Compared with the prior art, the beneficial effects of the electronic device provided in this application are the same as those of the code conversion method provided in the above embodiments, and other technical features of the electronic device are the same as those disclosed in the methods of the above embodiments, and will not be repeated here.

[0254] The electronic device provided in this application, employing the example set generation method in the above embodiments, can solve the technical problem of low accuracy in code conversion. Compared with the prior art, the beneficial effects of the electronic device provided in this application are the same as those of the example set generation method provided in the above embodiments, and other technical features in this electronic device are the same as those disclosed in the methods of the above embodiments, and will not be repeated here.

[0255] It should be understood that the various parts disclosed in this application can be implemented using hardware, software, firmware, or a combination thereof. In the description of the above embodiments, specific features, structures, materials, or characteristics can be combined in any suitable manner in one or more embodiments or examples.

[0256] Based on the same inventive concept, this application also provides a computer program product, including a computer program, wherein when the computer program is executed by a processor, it implements the steps of the code conversion method or the example set generation method described above.

[0257] The computer program product provided in this application can solve the technical problem of low accuracy in code conversion. Compared with the prior art, the beneficial effects of the computer program product provided in this application are the same as the beneficial effects of the code conversion method or the example set generation method provided in the above embodiments, and will not be repeated here.

[0258] The above are only some embodiments of this application and do not limit the patent scope of this application. All equivalent structural transformations made under the technical concept of this application and using the contents of the specification and drawings of this application, or direct / indirect applications in other related technical fields, are included in the patent protection scope of this application.

Claims

1. A transcoding method, characterized by, The code conversion method comprises: obtaining an initial language code segment and a few-shot example set corresponding to the initial language code segment, wherein the few-shot example set comprises dynamic sample examples, and the dynamic sample examples comprise an initial language code segment sample, a target language code segment sample, a corresponding relationship between the initial language code segment sample and the target language code segment sample, and a conversion rule; based on the initial language code segment and the few-shot example set, constructing prompt information; inputting the prompt information into a code conversion model to obtain a target language code segment.

2. The transcoding method of claim 1, wherein, The obtaining of the initial language code segment and the few-shot example set corresponding to the initial language code segment comprises: obtaining an initial language code segment; structurally decomposing the initial language code segment to obtain a structured representation of the initial language code segment; converting the structured representation into a vector representation of the initial language code segment; retrieving, from a first example library, dynamic sample examples matching the vector representation to generate the few-shot example set.

3. The transcoding method of claim 2, wherein, The conversion of the structured representation into the vector representation of the initial language code segment comprises: inputting the structured representation into a word embedding model to obtain a word vector representation of the initial language code segment; and / or inputting the structured representation into a semantic embedding model to obtain a semantic vector representation of the initial language code segment; wherein the vector representation comprises the word vector representation and / or the semantic vector representation.

4. The transcoding method of claim 2, wherein, After the conversion of the structured representation into the vector representation of the initial language code segment, the method further comprises: retrieving, from a first example library, dynamic sample examples matching the vector representation, and retrieving, from a second example library, preset sample examples matching the vector representation; performing similarity sorting and screening on the preset sample examples and the dynamic sample examples after merging to obtain the few-shot example set; wherein the example library comprises the first example library and the second example library, the first example library comprises dynamic sample examples corresponding to initial language code segments that have been code converted, and the second example library comprises preset sample examples.

5. The transcoding method of claim 2 or 4, wherein, The vector representation comprises a word vector representation and a semantic vector representation, and the retrieving of the dynamic sample examples matching the vector representation from the first example library comprises: determining a first similarity score between the word vector representation and each dynamic sample example in the first example library, and determining a second similarity score between the semantic vector representation and each dynamic sample example in the first example library; determining a corresponding dynamic sample example according to the first similarity score and the second similarity score.

6. The transcoding method of claim 4, wherein, The vector representation comprises a word vector representation and a semantic vector representation, and the retrieving of the preset sample examples matching the vector representation from the second example library comprises: determining a third similarity score between the word vector representation and each preset sample example in the second example library, and determining a fourth similarity score between the semantic vector representation and each preset sample example in the second example library; According to the third similarity score and the fourth similarity score, a corresponding preset sample example is determined.

7. The transcoding method of claim 2 wherein, After the prompt information is input into the code conversion model to obtain the target language code segment, the method further includes: evaluating the target language code segment converted from the current initial language code segment to obtain an evaluation result corresponding to the target language code segment; if the evaluation result meets a preset evaluation result, code conversion of the next initial language code segment is performed until conversion of all initial language code segments is completed; if the evaluation result does not meet the preset evaluation result, the method returns to the step of retrieving a dynamic sample example matching the vector representation from the first example library to generate the few-shot sample set.

8. The transcoding method of claim 7 wherein, The code conversion method further includes: associating the current initial language code segment whose evaluation result meets the preset evaluation result with the target language code segment corresponding thereto to obtain a dynamic sample example; storing the dynamic sample example in the first example library.

9. The transcoding method of claim 7 or 8, wherein, The code conversion method further includes: if the target language code segment meets a set syntax rule, and / or the target language code segment passes an expected function verification, and / or the target language code segment meets a set performance requirement, it is determined that the evaluation result meets the preset evaluation result.

10. The transcoding method of claim 1, wherein, The code conversion method further includes: constructing the prompt information based on the initial language code segment, context information of the initial language code segment, and the few-shot sample set.

11. A method of generating an example set, the method comprising: The method for generating the sample set includes: obtaining an initial language code segment; structurally decomposing the initial language code segment to obtain a structured representation of the initial language code segment; converting the structured representation into a vector representation of the initial language code segment; retrieving a sample example matching the vector representation from an example library to generate a few-shot sample set.

12. The method of claim 11, wherein the set of examples is generated by: The conversion of the structured representation into the vector representation of the initial language code segment includes: inputting the structured representation into a word embedding model to obtain a word vector representation of the initial language code segment; and / or inputting the structured representation into a semantic embedding model to obtain a semantic vector representation of the initial language code segment; wherein the vector representation includes the word vector representation and / or the semantic vector representation.

13. The method of claim 11, wherein the set of examples is generated by: The retrieval of the sample example matching the vector representation from the example library to generate the few-shot sample set includes: retrieving a dynamic sample example matching the vector representation from a first example library, and retrieving a preset sample example matching the vector representation from a second example library; performing similarity sorting and screening on the preset sample example and the dynamic sample example after merging to obtain the few-shot sample set; wherein the example library includes the first example library and the second example library, the first example library includes a dynamic sample example corresponding to an initial language code segment that has undergone code conversion, and the second example library includes a preset sample example.

14. The method of claim 13, wherein the set of examples is generated by: The vector representation includes a word vector representation and a semantic vector representation, and the retrieving, from the first example library, of a dynamic sample example matching the vector representation includes: determining a first similarity score between the word vector representation and each dynamic sample example in the first example library, and determining a second similarity score between the semantic vector representation and each dynamic sample example in the first example library; determining a corresponding dynamic sample example according to the first similarity score and the second similarity score.

15. The method of claim 13, wherein the set of examples is generated by: The vector representation includes a word vector representation and a semantic vector representation, and the retrieving, from the second example library, of a preset sample example matching the vector representation includes: determining a third similarity score between the word vector representation and each preset sample example in the second example library, and determining a fourth similarity score between the semantic vector representation and each preset sample example in the second example library; determining a corresponding preset sample example according to the third similarity score and the fourth similarity score.

16. A transcoding device, characterized by The code conversion device includes: a first acquisition module configured to acquire an initial language code segment and a corresponding small sample example set of the initial language code segment, wherein the small sample example set includes a dynamic sample example, and the dynamic sample example includes an initial language code segment sample, a target language code segment sample, a corresponding relationship between the initial language code segment sample and the target language code segment sample, and a conversion rule; a construction module configured to construct prompt information based on the initial language code segment and the small sample example set; a first conversion module configured to input the prompt information into a code conversion model to obtain a target language code segment.

17. An apparatus for generating an example set, the apparatus comprising: The example set generation device includes: a second acquisition module configured to acquire an initial language code segment; a structured representation module configured to perform structured decomposition on the initial language code segment to obtain a structured representation of the initial language code segment; a second conversion module configured to convert the structured representation into a vector representation of the initial language code segment; a generation module configured to retrieve a sample example matching the vector representation from an example library to generate a small sample example set.

18. An electronic device, comprising: The electronic device includes a memory, a processor, and a computer program stored on the memory and executable on the processor, the computer program being configured to implement the steps of the code conversion method according to any one of claims 1 to 10, or the computer program being configured to implement the steps of the example set generation method according to any one of claims 11 to 15.

19. A computer program product, characterised in that, The computer program product includes a computer program, which, when executed by a processor, implements the steps of the code conversion method according to any one of claims 1 to 10, or, when executed by a processor, implements the steps of the example set generation method according to any one of claims 11 to 15.