Code generation method and system, electronic equipment and storage medium
By performing word embedding processing and semantic encoding and decoding of the target text data of the test cases, code consistent with structure, syntax and data flow is generated, which solves the problem of low code generation efficiency and accuracy in the existing technology, and achieves more efficient and accurate code generation.
Patent Information
- Application Number
- CN202411567426.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-05
- Publication Date
- 2025-03-14
AI Technical Summary
In the prior art, manual writing of test code is inefficient, and semi-automated tools perform poorly when dealing with complex test cases or custom requirements, resulting in low code generation efficiency and accuracy.
By obtaining the target text data of the target test case, inputting the pre-trained code generation model, performing word embedding processing on the text data, generating structural word embedding vectors, grammatical word embedding vectors, and data stream word embedding vectors, and semantic codecs to generate the target code.
Improves code writing efficiency and ensures that the generated code has a high degree of consistency in structure, syntax and data flow, thereby improving the code accuracy.
Smart Images

Figure CN119621543A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of code generation, and in particular to a code generation method, system, electronic device and storage medium. Background Art
[0002] At present, the traditional way of writing code is to write test code manually, in which testers convert test cases into code one by one. The advantage of this method is that it is flexible and engineers can adjust the code according to the specific needs of the project to ensure that each test case is fully implemented. However, its obvious disadvantage is low efficiency. For large projects or scenarios with a large number of test cases, manually writing test code is often time-consuming and error-prone. Due to the complexity of the test code and the changes in project requirements, maintaining these manually generated test codes is also extremely resource-intensive.
[0003] The second method is to use semi-automated tools to write test code. These semi-automated tools can assist in generating test code to a certain extent, and usually rely on some standardized test case templates or frameworks. This method can reduce some manual work, especially when dealing with simple or conventional test scenarios. However, when the test cases are more complex or there are more customized requirements in the project, the limitations of semi-automated tools will become apparent. The code generated by these tools often needs to be manually modified and optimized, and the writing efficiency and accuracy are relatively low.
[0004] Therefore, a new method of writing code is urgently needed. Summary of the invention
[0005] In view of the above problems, embodiments of the present application provide a code generation method, system, electronic device, and storage medium to overcome the above problems or at least partially solve the above problems.
[0006] In a first aspect of the present application, a code generation method is provided, the method comprising: Get the target text data of the target test case; Inputting the target text data into a pre-trained code generation model; Performing word embedding processing on the target text data to obtain a structural word embedding vector, a grammatical word embedding vector, and a data stream word embedding vector corresponding to the target text data; The structural word embedding vector, the grammatical word embedding vector and the data stream word embedding vector are semantically encoded and decoded to obtain the target code of the target test case.
[0007] Optionally, the code generation model is trained by the following steps: Acquire first text data of a historical test case and real code corresponding to the historical test case; Inputting the first text data into a code generation model to be trained, and obtaining a first text vector of the first text data through text preprocessing; Performing word embedding processing on the first text vector to obtain a structural word embedding vector of the first text data, a grammatical word embedding vector of the first text data, and a data stream word embedding vector of the first text data; Using a structure-aware encoder, concatenate the structural word embedding vector of the first text data, the grammatical word embedding vector of the first text data, and the data stream word embedding vector of the first text data to obtain a concatenated word embedding vector, and semantically encode and decode the concatenated word embedding vector to obtain a first code for the first text data; Calculate a target loss function according to the first code and the true code to obtain a loss value; Based on the loss value, parameters of the code generation model to be trained are updated to obtain the code generation model.
[0008] Optionally, performing word embedding processing on the first text vector to obtain a structural word embedding vector of the first text data, a grammatical word embedding vector of the first text data, and a data stream word embedding vector of the first text data includes: Inputting the first text vector into a structure word embedding tool for first word embedding processing, determining text structure features of the first text vector through the structure word embedding tool, and obtaining the structure word embedding vector based on the text structure features; Inputting the first text vector into a parser, generating an abstract syntax tree through the parser, and performing second word embedding processing using a grammatical word embedding tool to obtain the grammatical word embedding vector; The first text vector is input into a network diagram tool, a data flow diagram is generated by the network diagram tool, and a graph neural network is used to perform a third word embedding process to obtain the data flow word embedding vector.
[0009] Optionally, calculating a target loss function according to the first code and the true code includes: Defining a first loss function according to a character difference value between a character in the first code and a character in the true code; A second loss function is defined according to the path difference value between the AST path of the first code and the AST path of the real code, defining a third loss function according to a dependency difference value between the data dependency of the first code and the data dependency of the real code; The target loss function is determined according to the first loss function, the second loss function and the third loss function.
[0010] Optionally, determining the target loss function according to the first loss function, the second loss function and the third loss function includes: Get a first weight hyperparameter and a second weight hyperparameter; The target loss function is constructed by using the first weight hyperparameter as the weight of the second loss function and using the second weight hyperparameter as the weight of the third loss function.
[0011] Optionally, the structure-aware encoder includes: a Transformer encoder and a Transformer decoder, and the semantic encoding and decoding of the concatenated word embedding vector to obtain a first code of the first text data includes: Determining, by means of the Transformer encoder, dependency values of the concatenated word embedding vector in syntax and data streams; The first code is obtained by using the Transformer decoder and taking the dependency value of the concatenated word embedding vector in the grammar and the data stream as input.
[0012] Optionally, determining, by the Transformer encoder, a dependency value of the concatenated word embedding vector in a grammar and a data stream includes: Obtaining structural information of an abstract syntax tree of the concatenated word embedding vector and structural information of a data flow graph of the concatenated word embedding vector; Determining, through the self-attention mechanism in the Transformer encoder, a path similarity value between each node in the concatenated word embedding vector based on the structural information of the abstract syntax tree of the concatenated word embedding vector, and determining an adjacency relationship value between each node in the concatenated word embedding vector based on the structural information of the data flow graph of the concatenated word embedding vector; Determine the dependency value of the concatenated word embedding vector in the grammar and the data stream according to the path similarity value between each node in the concatenated word embedding vector and the adjacency value between each node in the concatenated word embedding vector.
[0013] In a second aspect of the present application, a code generation system is provided, the system comprising: An acquisition module is used to acquire target text data of a target test case; An input module, used for inputting the target text data into a pre-trained code generation model; A processing module, used for performing word embedding processing on the target text data to obtain a structural word embedding vector, a grammatical word embedding vector and a data stream word embedding vector corresponding to the target text data; The semantic encoding and decoding module is used to perform semantic encoding and decoding on the structural word embedding vector, the grammatical word embedding vector and the data stream word embedding vector to obtain the target code of the target test case.
[0014] Optionally, the system further comprises: A first acquisition submodule, used to acquire first text data of a historical test case and a real code corresponding to the historical test case; An input submodule, used for inputting the first text data into a code generation model to be trained, and obtaining a first text vector of the first text data through text preprocessing; A processing submodule, configured to perform word embedding processing on the first text vector to obtain a structural word embedding vector of the first text data, a grammatical word embedding vector of the first text data, and a data stream word embedding vector of the first text data; a concatenation submodule, configured to concatenate the structural word embedding vector of the first text data, the grammatical word embedding vector of the first text data, and the data stream word embedding vector of the first text data through a structure-aware encoder to obtain a concatenated word embedding vector, and to perform semantic encoding and decoding on the concatenated word embedding vector to obtain a first code of the first text data; A calculation submodule, used for calculating a target loss function according to the first code and the real code to obtain a loss value; A parameter updating submodule is used to update the parameters of the code generation model to be trained based on the loss value to obtain the code generation model.
[0015] Optionally, the word embedding processing is performed on the first text vector to obtain a structural word embedding vector of the first text data, a grammatical word embedding vector of the first text data, and a data stream word embedding vector of the first text data, and the processing submodule includes: A first processing subunit is used to input the first text vector into a structure word embedding tool for first word embedding processing, determine the text structure features of the first text vector through the structure word embedding tool, and obtain the structure word embedding vector based on the text structure features; A second processing subunit is used to input the first text vector into a parser, generate an abstract syntax tree through the parser, and perform second word embedding processing using a grammatical word embedding tool to obtain the grammatical word embedding vector; The third processing sub-unit is used to input the first text vector into a network diagram tool, generate a data flow diagram through the network diagram tool, and use a graph neural network to perform a third word embedding process to obtain the data flow word embedding vector.
[0016] Optionally, the calculating of the target loss function according to the first code and the real code, the calculating submodule comprises: A first definition subunit, configured to define a first loss function according to a character difference value between a character in the first code and a character in the real code; A second definition subunit is used to define a second loss function according to a path difference value between the AST path of the first code and the AST path of the real code, A third definition subunit is used to define a third loss function according to a dependency difference value between the data dependency of the first code and the data dependency of the real code; The first determination subunit is used to determine the target loss function according to the first loss function, the second loss function and the third loss function.
[0017] Optionally, the target loss function is determined according to the first loss function, the second loss function and the third loss function, and the first determination subunit includes: A first acquisition subunit, used to acquire a first weight hyperparameter and a second weight hyperparameter; A construction subunit is used to construct the target loss function using the first weight hyperparameter as the weight of the second loss function and the second weight hyperparameter as the weight of the third loss function.
[0018] Optionally, the structure-aware encoder includes: a Transformer encoder and a Transformer decoder, the semantic encoding and decoding of the concatenated word embedding vector is performed to obtain a first code of the first text data, and the concatenation submodule includes: A second determination subunit is used to determine the dependency value of the concatenated word embedding vector in the grammar and the data stream through the Transformer encoder; The third determination subunit is used to obtain the first code through the Transformer decoder by taking the dependency value of the concatenated word embedding vector in the grammar and the data stream as input.
[0019] Optionally, the determining, by the Transformer encoder, a dependency value of the concatenated word embedding vector in a grammar and a data stream, the second determining subunit comprises: A second acquisition subunit is used to acquire structural information of an abstract syntax tree of the concatenated word embedding vector and structural information of a data flow graph of the concatenated word embedding vector; A fourth determination subunit is used to determine, through a self-attention mechanism in the Transformer encoder, a path similarity value between each node in the concatenated word embedding vector based on the structural information of the abstract syntax tree of the concatenated word embedding vector, and to determine an adjacency relationship value between each node in the concatenated word embedding vector based on the structural information of the data flow graph of the concatenated word embedding vector; The fifth determining subunit is used to determine the dependency value of the concatenated word embedding vector in the grammar and the data stream according to the path similarity value between each node in the concatenated word embedding vector and the adjacency value between each node in the concatenated word embedding vector.
[0020] In a third aspect of the present application, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory, wherein the processor executes the computer program to implement the code generation method as described in the first aspect of the present application.
[0021] In a fourth aspect of the present application, a computer-readable storage medium is provided, on which a computer program / instruction is stored. When the computer program / instruction is executed by a processor, the code generation method as described in the first aspect of the present application is implemented.
[0022] Beneficial effects of this application: The present application provides a code generation method, the method comprising: obtaining target text data of a target test case; inputting the target text data into a pre-trained code generation model; performing word embedding processing on the target text data to obtain a structural word embedding vector, a grammatical word embedding vector, and a data flow word embedding vector corresponding to the target text data; performing semantic encoding and decoding on the structural word embedding vector, the grammatical word embedding vector, and the data flow word embedding vector to obtain the target code of the target test case. The present application converts text data into structural word embedding vectors, grammatical word embedding vectors, and data flow word embedding vectors, respectively, and obtains the target code by performing semantic encoding and decoding on the word embedding vectors in three formats, which not only improves the efficiency of code writing, but also makes the generated code highly consistent in structure, grammar, and data flow, thereby improving the accuracy of the code. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the description of the embodiments of the present application will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0024] Figure 1 This is a schematic diagram of the steps of a code generation method provided by an embodiment of the present application; Figure 2 This is a schematic diagram of the overall process framework of a code generation method provided in an embodiment of the present application; Figure 3 is a schematic diagram of a code generation system provided in an embodiment of the present application; Figure 4 It is a schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0025] The exemplary embodiments of the present application will be described in more detail below in conjunction with the accompanying drawings in the embodiments of the present application. Although the exemplary embodiments of the present application are shown in the accompanying drawings, it should be understood that the present application can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided in order to enable a more thorough understanding of the present application and to enable the scope of the present application to be fully communicated to those skilled in the art.
[0026] Manual coding is the most traditional testing method, where testers convert test cases into code one by one. Its main advantage is its flexibility, which allows engineers to adjust according to project-specific requirements to ensure that each test case is fully implemented. However, its significant disadvantage is its low efficiency, especially in large projects or with a large number of test cases, where manual coding is often time-consuming and error-prone. In addition, as the complexity of test code increases and project requirements change, maintaining these manually written codes will also consume a lot of resources.
[0027] Semi-automated tools can assist in generating test code to a certain extent, and they usually rely on standardized test case templates or frameworks. This approach can reduce some manual writing work, especially when dealing with simple or routine test scenarios. However, when faced with more complex test cases or customized requirements, the limitations of semi-automated tools will become apparent. The generated code often needs to be manually modified and optimized, so it cannot completely replace manual writing.
[0028] In addition, the BDD framework generates code from test scenarios described in natural language, allowing non-technical personnel to participate in the writing of test cases, thereby improving the efficiency of team collaboration. However, BDD performs poorly in dealing with complex test scenarios, especially when project requirements change frequently or highly customized tests are required. Its flexibility is insufficient. The generated code can usually only handle simple scenarios, and complex business logic still requires manual intervention by engineers.
[0029] Based on the above problems, the first aspect of the embodiment of the present application provides a code generation method, such as Figure 1 As shown, the method includes: Step S101, obtaining target text data of a target test case.
[0030] In this step, the target text data of the target test case is obtained, and the purpose and expected results of the test case need to be clarified. For example: the function to be tested, the interface, or the conditions for successful execution of the test, etc. Collect information about the target function from project documents, requirement analysis reports, or other relevant materials. This information will guide the writing of test cases in subsequent steps. According to the requirement analysis, clarify the input parameters and their values required for the test case, as well as the expected output results. For example, if the goal is to test the user login function, the input parameters may include the username and password, and the output result is the login status code. Integrate the collected information into natural language text to form a descriptive test case. For example: "Build a test function named 'test_login', call the 'login' API, pass parameters 'username='user1'' and 'password='pass1'', and expect to return 'status=200'." Step S102: input the target text data into a pre-trained code generation model.
[0031] In this step, the target text data obtained in step S101 is input into the pre-trained code generation model. Prior to this, the obtained target text data needs to be pre-processed, for example: cleaning the text, removing extra spaces, special characters, etc.; tokenizing, breaking the text into words or sub-words so that the model can understand and process it.
[0032] In some examples, the tokenized target text data also needs to be converted into an encoding format acceptable to the model. For example, the tokenized target text data can be converted into a vector representation.
[0033] Step S103, performing word embedding processing on the target text data to obtain a structural word embedding vector, a grammatical word embedding vector, and a data stream word embedding vector corresponding to the target text data.
[0034] In this step, the target text data is processed by word embedding through the code generation model. In this application, the structural word embedding can be processed by a word embedding model, such as the CodeBERT model, to obtain a structural word embedding vector. Specifically: using the CodeBERT model, the vocabulary of the target text data is embedded through the model to generate a structural word embedding vector. This process includes: inputting each word or subword into CodeBERT to obtain its representation in the word vector space. All elements in the text are embedded to form an overall structural word embedding vector, which reflects the overall semantics of the text.
[0035] In this step, the target text is analyzed in combination with the abstract syntax tree to generate a grammatical word embedding vector. This step includes: using the Javalang tool to parse the target text data and generate the corresponding abstract syntax tree. The nodes of the abstract syntax tree are embedded to capture the grammatical structure information and form a grammatical word embedding vector.
[0036] In this step, the target text is also analyzed using a data flow graph to generate a data flow word embedding vector. The steps include: using the NetworkX tool to build a data flow graph of the target text data, analyzing the data dependency and control flow information in the target text data. The nodes and edges in the data flow graph are embedded to generate a data flow word embedding vector to reflect the information of the data flow.
[0037] Step S104, performing semantic encoding and decoding on the structural word embedding vector, the grammatical word embedding vector and the data stream word embedding vector to obtain the target code of the target test case.
[0038] In this step, semantic encoding and decoding of the structural word embedding vector, grammatical word embedding vector and data stream word embedding vector are performed. The structural word embedding vector, grammatical word embedding vector and data stream word embedding vector need to be combined together as input data for semantic processing and conversion. Then, an encoder is used to process the input embedding vector to extract the semantic information and contextual relationship. This encoder can understand the connection between these vectors and generate a new representation. Next, a decoder is used to convert the representation generated by the encoder into actual code. The decoder gradually builds a complete code snippet according to certain rules and logic. Finally, the output of the decoder is the target code of the target test case.
[0039] This application converts text data into structural word embedding vectors, grammatical word embedding vectors and data flow word embedding vectors respectively, and obtains the target code by semantically encoding and decoding the word embedding vectors in three formats. This not only improves the code writing efficiency, but also makes the generated code highly consistent in structure, grammar and data flow, thereby improving the accuracy of the code.
[0040] In one embodiment, the code generation model is trained by the following steps: Acquire first text data of a historical test case and real code corresponding to the historical test case; Inputting the first text data into a code generation model to be trained, and obtaining a first text vector of the first text data through text preprocessing; Performing word embedding processing on the first text vector to obtain a structural word embedding vector of the first text data, a grammatical word embedding vector of the first text data, and a data stream word embedding vector of the first text data; Using a structure-aware encoder, concatenate the structural word embedding vector of the first text data, the grammatical word embedding vector of the first text data, and the data stream word embedding vector of the first text data to obtain a concatenated word embedding vector, and semantically encode and decode the concatenated word embedding vector to obtain a first code for the first text data; Calculate a target loss function according to the first code and the true code to obtain a loss value; Based on the loss value, parameters of the code generation model to be trained are updated to obtain the code generation model.
[0041] In this embodiment, a training step of a code generation model is provided, and the specific steps are as follows: Step S1: Collect the first text data of historical test cases and the real codes corresponding to these test cases. These data provide the basis for model training.
[0042] Step S2: Input the acquired first text data into the code generation model to be trained to perform text preprocessing. This step may include operations such as data cleaning, tokenization and encoding, and finally generate a first text vector.
[0043] Step S3: Perform word embedding processing on the generated first text vector to obtain three different embedding vectors: Structural word embedding vector: reflects the structural information of the text.
[0044] Grammatical word embedding vectors: capture the grammatical features of text.
[0045] Dataflow word embedding vectors: revealing data dependency and control flow information in text.
[0046] Step S4: Use a structure-aware encoder to concatenate the above three embedding vectors to form a unified concatenated word embedding vector, perform semantic encoding and decoding on the concatenated word embedding vector, and generate a first code corresponding to the first text data.
[0047] Step S5: Calculate the target loss function according to the difference between the generated first code and the real code to obtain a loss value. This loss value reflects the quality of the generated code.
[0048] Step S6: Based on the calculated loss value, update the parameters of the code generation model to be trained to optimize the performance and generation capability of the model, and finally obtain a trained code generation model.
[0049] In one embodiment, performing word embedding processing on the first text vector to obtain a structural word embedding vector of the first text data, a grammatical word embedding vector of the first text data, and a data stream word embedding vector of the first text data includes: Inputting the first text vector into a structure word embedding tool for first word embedding processing, determining text structure features of the first text vector through the structure word embedding tool, and obtaining the structure word embedding vector based on the text structure features; Inputting the first text vector into a parser, generating an abstract syntax tree through the parser, and performing second word embedding processing using a grammatical word embedding tool to obtain the grammatical word embedding vector; The first text vector is input into a network diagram tool, a data flow diagram is generated by the network diagram tool, and a graph neural network is used to perform a third word embedding process to obtain the data flow word embedding vector.
[0050] In this embodiment, the process of performing word embedding processing on the first text vector to obtain a structural word embedding vector, a grammatical word embedding vector, and a data stream word embedding vector can be described in detail as the following steps: The first text vector is input into the structural word embedding tool for first word embedding processing. In this application, the structural word embedding tool can be the CodeBERT model. In this process, the structural word embedding tool analyzes the text structure features of the first text vector, such as the sentence hierarchy, paragraph organization and overall layout. Based on the analyzed text structure features, a corresponding structural word embedding vector is generated. This vector reflects the logical and structural information of the text, which helps the subsequent code generation process. In this application, the generated structural word embedding vector is shown in the following formula (1): (1) in, Represents a set of structural word embedding vectors; Word embedding vector; T represents text vector.
[0051] The first text vector is input into a parser, and an abstract syntax tree is generated by the parser. In the present application, the parser may be a Javalang tool. The abstract syntax tree is a representation of the grammatical structure of the text, capturing the grammatical rules and relationships in the text. Then, a grammatical word embedding tool is used to perform a second word embedding process on the generated abstract syntax tree to obtain the grammatical word embedding vector. In the present application, the grammatical word embedding tool may be This vector provides an embedded representation of the grammatical features of the text, helping the model understand the grammatical structure of the language. In this application, the generated grammatical word embedding vector is shown in the following formula (2): (2) in, Represents a set of grammatical word embedding vectors; An abstract syntax tree representing a text vector T.
[0052] The first text vector is input into a network diagram tool, and a data flow diagram is generated using the tool. In the present application, the network diagram tool may be a NetworkX tool. The data flow diagram shows the data dependency and control flow information in the text, and reflects the interaction and dependency between variables. Then, the generated data flow diagram is processed by a graph neural network GNN to obtain the data flow word embedding vector. This vector captures the characteristics of the data flow in the text and provides important contextual information for generating code. The generated data flow word embedding vector is shown in the following formula (3): (3) in, Represents a data stream word embedding vector set; A data flow graph representing a text vector T.
[0053] In this embodiment, the first text vector is effectively converted into three different types of embedding vectors, where the structural word embedding vector focuses on the structure of the text, the grammatical word embedding vector focuses on the grammar of the text, and the data stream word embedding vector focuses on the data stream characteristics of the text.
[0054] In one embodiment, the structural word embedding vector, the grammatical word embedding vector and the data stream word embedding vector are concatenated to obtain a concatenated word embedding vector, wherein the concatenated word embedding vector is shown in the following formula (4): (4) in, Represents vector concatenation.
[0055] In one embodiment, the structure-aware encoder includes: a Transformer encoder and a Transformer decoder, and the semantic encoding and decoding of the concatenated word embedding vector to obtain the first code of the first text data includes: Determining, by means of the Transformer encoder, dependency values of the concatenated word embedding vector in syntax and data streams; The first code is obtained by using the Transformer decoder and taking the dependency value of the concatenated word embedding vector in the grammar and the data stream as input.
[0056] The structure-aware encoder provided in this application includes a Transformer encoder and a Transformer decoder.
[0057] In this embodiment, the concatenated word embedding vector is first input into the Transformer encoder. At this stage, the encoder analyzes the relationship between the elements in the concatenated word embedding vector through the self-attention mechanism, paying special attention to the dependencies in grammar and data flow. This means that the encoder captures the contextual dependencies and mutual influences between different elements (such as words, sentences, etc.). After multiple layers of processing, the Transformer encoder generates a set of dependency values, which contain the dependencies of the concatenated word embedding vector in structure, grammar, and data flow. The dependency values will be used as input to the subsequent decoder to provide rich contextual information.
[0058] Furthermore, the dependency values output by the encoder are input into the Transformer decoder, which uses these dependency values for processing to generate the first code. In this process, the decoder gradually generates a token sequence of the code, depending on the context information provided by the encoder. The decoder uses self-attention and cross-attention mechanisms to focus on the syntax and data flow information related to the current generation step, ensuring that the generated code not only conforms to the grammatical rules, but also reasonably reflects the data dependencies.
[0059] In one embodiment, determining, by the Transformer encoder, a dependency value of the concatenated word embedding vector in the grammar and the data stream includes: Obtaining structural information of an abstract syntax tree of the concatenated word embedding vector and structural information of a data flow graph of the concatenated word embedding vector; Determining, through the self-attention mechanism in the Transformer encoder, a path similarity value between each node in the concatenated word embedding vector based on the structural information of the abstract syntax tree of the concatenated word embedding vector, and determining an adjacency relationship value between each node in the concatenated word embedding vector based on the structural information of the data flow graph of the concatenated word embedding vector; Determine the dependency value of the concatenated word embedding vector in the grammar and the data stream according to the path similarity value between each node in the concatenated word embedding vector and the adjacency value between each node in the concatenated word embedding vector.
[0060] In this embodiment, first, the corresponding abstract syntax tree structure information and data flow graph structure information are extracted from the concatenated word embedding vector. These structural information provide the grammatical rules of the code and its data flow relationship, which is the basis for understanding the meaning of the code.
[0061] Furthermore, the self-attention mechanism in the Transformer encoder is used to analyze the path similarity values between the nodes in the concatenated word embedding vector based on the structural information of the abstract syntax tree. This process involves calculating the relationship and similarity between nodes, reflecting how they are connected in the grammatical structure. Then, by comparing the hierarchical relationships, connection paths, etc. between nodes, path similarity values are generated, which reveal the tightness of the grammatical structure in the code.
[0062] Similarly, based on the structural information of the data flow graph, the adjacency value between each node in the concatenated word embedding vector is determined. The adjacency relationship represents the direct connection between nodes and reflects the dependency relationship in the data flow. By analyzing the data transfer and control flow between nodes, the adjacency value can be calculated to represent the frequency and closeness of interaction between nodes.
[0063] Finally, based on the path similarity values and adjacency values between the nodes in the concatenated word embedding vector, the dependency values of the concatenated word embedding vector in the syntax and data flow are comprehensively obtained. These comprehensive values will be used in the subsequent decoding process to provide contextual support for the generated code.
[0064] In this way, the Transformer encoder can effectively capture the grammatical features and data flow characteristics of the concatenated word embedding vector, ensuring the quality and consistency of the generated code, as shown in the following formula (5): (5) in, represents the query vector; represents the key vector, Is a node in the abstract syntax tree and The path similarity between Represents the data flow graph and The adjacency relationship between is the dimension of the key vector.
[0065] Furthermore, the output of the Transformer encoder is represented as As input, a first code sequence is generated through further processing, as shown in the following formula (6): (6) in, represents a target code sequence; The Transformer decoder will gradually generate a token sequence of the first code through multiple layers of self-attention and cross-attention mechanisms. The cross-attention mechanism pays special attention to the structural information related to the current generation step. For example, if a function call is currently being generated, the Transformer decoder should be able to recognize and focus on the part of the Transformer encoder representation related to the function definition. The self-attention mechanism mainly helps the model to deeply understand the sequence information. The self-attention mechanism allows each element in the sequence (such as words, pixels, etc.) to interact with all other elements, thereby capturing the dependencies within the sequence. Through multi-layer stacking, each layer can further refine and abstract the sequence information based on the output of the previous layer, so that the model can have a deeper understanding of the semantics and structure of the sequence. Therefore, the output of the Transformer decoder can be further expressed as the following formula (7), (7) in, is the generated code character.
[0066] In one embodiment, the calculating the target loss function according to the first code and the true code includes: Defining a first loss function according to a character difference value between a character in the first code and a character in the true code; A second loss function is defined according to the path difference value between the AST path of the first code and the AST path of the real code, defining a third loss function according to a dependency difference value between the data dependency of the first code and the data dependency of the real code; The target loss function is determined according to the first loss function, the second loss function and the third loss function.
[0067] In this embodiment, a process of calculating the target loss function according to the first code and the true code is provided, which can be described in detail as follows: Define the first loss function: Compare the characters in the first code and the real code. The character difference value can be obtained by calculating the edit distance, the number of character matches, etc., reflecting the difference between the two at the character level. Based on the obtained character difference value, define the first loss function. This loss function quantifies the deviation between the generated code and the real code in character accuracy. The smaller the loss, the closer the generated code is to the real code, as shown in the following formula (8): (8) in, Indicates that in a given text vector Generate the correct code character in the case of probability.
[0068] Define the second loss function: Calculate the path difference of the abstract syntax tree, based on the abstract syntax tree of the generated first code and the real code, and compare their path structures. Analyze the number of nodes, connection relationship, etc. of the path to obtain the path difference value. Based on the path difference value, define the second loss function. This loss function evaluates the rationality of the generated code in terms of grammatical structure to ensure that it follows the expected grammatical rules and structure, as shown in the following formula (9): (9) in, Indicates The path of the code character in the real abstract syntax tree, Represents the probability of this path predicted by the model.
[0069] Define the third loss function: Analyze the data dependency between the first code and the real code, determine the dependency, control flow and other information between variables, and calculate the dependency difference value. Based on this difference value, define the third loss function. This loss function focuses on the rationality of data usage in the generated code, ensuring that the relationship between variables and data flow conforms to the actual situation, as shown in the following formula (10): (10) in, No. code characters mark data flow dependencies in the real code, is the probability of this dependency predicted by the model.
[0070] Finally, the first loss function, the second loss function and the third loss function are weightedly combined to determine the target loss function. Specifically, a first weight hyperparameter and a second weight hyperparameter are obtained; the first weight hyperparameter is used as the weight of the second loss function, and the second weight hyperparameter is used as the weight of the third loss function to construct the target loss function. The target loss function can be expressed as follows: (11) in, represents the first weight hyperparameter; represents the second weight hyperparameter.
[0071] In one embodiment, the present application provides a schematic diagram of the overall process framework of a code generation method, such as Figure 2 As shown, The first step of training is to prepare data. First, obtain the description text of the test case (i.e., the first text data) and its corresponding real code from the historical data. These real codes are verified correct code samples, which provide a benchmark for the model to learn.
[0072] The first text data obtained is input into the code generation model to be trained to perform text preprocessing, which includes cleaning and normalizing the text, and generating a first text vector.
[0073] Next, the first text vector is subjected to hierarchical word embedding processing to generate three types of word embedding vectors: structural word embedding vector: the first text vector is converted into a structural word embedding vector through the CodeBERT module; grammatical word embedding vector: after the abstract syntax tree is generated by the parser, it is input into the grammar embedding tool to generate a grammar embedding vector; data flow word embedding vector: GNN (graph neural network) is used to generate a data flow embedding vector based on the data flow graph.
[0074] The structural word embedding vector, the grammatical word embedding vector, and the data stream word embedding vector are concatenated to form a concatenated word embedding vector. Next, the concatenated word embedding vector is processed by the self-attention mechanism of the structure-aware encoder. The structure-aware encoder includes a Transformer encoder and a Transformer decoder. The encoder part determines the dependencies of the concatenated word embedding vector in the grammar and data stream, and the decoder uses this as input to generate code.
[0075] In the process of semantic encoding and decoding, the structure-aware encoder focuses on the syntax and data dependencies of the code to obtain the first code that conforms to the target text description.
[0076] Next, compare the generated first code with the real code in the historical data and calculate three loss values: Character-level difference loss (L_CE): compares the difference between the generated code and the real code at the character level to ensure that the content of the generated code is close to the real code.
[0077] AST path loss (L_AST): compares the abstract syntax tree path differences between the generated code and the real code to ensure the accuracy of the grammatical structure.
[0078] Data dependency loss (L_DFG): Compares the data flow dependency differences between the generated code and the real code to ensure the consistency of the data flow dependency.
[0079] These three loss values are combined through the Loss Function module to obtain the final target loss function, which is used to measure the overall error of the model-generated code. Finally, based on the value of the target loss function, the back propagation algorithm is used to optimize and update the model parameters. Through multiple iterations, the model gradually learns to generate code that conforms to the test case description, thereby improving the quality of the generated code.
[0080] Based on the same inventive concept, the second aspect of the present application provides a code generation system, such as Figure 3 As shown, the system comprises: An acquisition module 201 is used to acquire target text data of a target test case; An input module 202, used for inputting the target text data into a pre-trained code generation model; A processing module 203 is used to perform word embedding processing on the target text data to obtain a structural word embedding vector, a grammatical word embedding vector and a data stream word embedding vector corresponding to the target text data; The semantic encoding and decoding module 204 is used to perform semantic encoding and decoding on the structural word embedding vector, the grammatical word embedding vector and the data stream word embedding vector to obtain the target code of the target test case.
[0081] Optionally, the system further comprises: A first acquisition submodule, used to acquire first text data of a historical test case and a real code corresponding to the historical test case; An input submodule, used for inputting the first text data into a code generation model to be trained, and obtaining a first text vector of the first text data through text preprocessing; A processing submodule, configured to perform word embedding processing on the first text vector to obtain a structural word embedding vector of the first text data, a grammatical word embedding vector of the first text data, and a data stream word embedding vector of the first text data; a concatenation submodule, configured to concatenate the structural word embedding vector of the first text data, the grammatical word embedding vector of the first text data, and the data stream word embedding vector of the first text data through a structure-aware encoder to obtain a concatenated word embedding vector, and to perform semantic encoding and decoding on the concatenated word embedding vector to obtain a first code of the first text data; A calculation submodule, used for calculating a target loss function according to the first code and the real code to obtain a loss value; A parameter updating submodule is used to update the parameters of the code generation model to be trained based on the loss value to obtain the code generation model.
[0082] Optionally, the word embedding processing is performed on the first text vector to obtain a structural word embedding vector of the first text data, a grammatical word embedding vector of the first text data, and a data stream word embedding vector of the first text data, and the processing submodule includes: A first processing subunit is used to input the first text vector into a structure word embedding tool for first word embedding processing, determine the text structure features of the first text vector through the structure word embedding tool, and obtain the structure word embedding vector based on the text structure features; A second processing subunit is used to input the first text vector into a parser, generate an abstract syntax tree through the parser, and perform second word embedding processing using a grammatical word embedding tool to obtain the grammatical word embedding vector; The third processing sub-unit is used to input the first text vector into a network diagram tool, generate a data flow diagram through the network diagram tool, and use a graph neural network to perform a third word embedding process to obtain the data flow word embedding vector.
[0083] Optionally, the calculating of the target loss function according to the first code and the real code, the calculating submodule comprises: A first definition subunit, configured to define a first loss function according to a character difference value between a character in the first code and a character in the real code; A second definition subunit is used to define a second loss function according to a path difference value between the AST path of the first code and the AST path of the real code, A third definition subunit is used to define a third loss function according to a dependency difference value between the data dependency of the first code and the data dependency of the real code; The first determination subunit is used to determine the target loss function according to the first loss function, the second loss function and the third loss function.
[0084] Optionally, the target loss function is determined according to the first loss function, the second loss function and the third loss function, and the first determination subunit includes: A first acquisition subunit, used to acquire a first weight hyperparameter and a second weight hyperparameter; A construction subunit is used to construct the target loss function using the first weight hyperparameter as the weight of the second loss function and the second weight hyperparameter as the weight of the third loss function.
[0085] Optionally, the structure-aware encoder includes: a Transformer encoder and a Transformer decoder, the semantic encoding and decoding of the concatenated word embedding vector is performed to obtain a first code of the first text data, and the concatenation submodule includes: A second determination subunit is used to determine the dependency value of the concatenated word embedding vector in the grammar and the data stream through the Transformer encoder; The third determination subunit is used to obtain the first code through the Transformer decoder by taking the dependency value of the concatenated word embedding vector in the grammar and the data stream as input.
[0086] Optionally, the determining, by the Transformer encoder, a dependency value of the concatenated word embedding vector in a grammar and a data stream, the second determining subunit comprises: A second acquisition subunit is used to acquire structural information of an abstract syntax tree of the concatenated word embedding vector and structural information of a data flow graph of the concatenated word embedding vector; A fourth determination subunit is used to determine, through a self-attention mechanism in the Transformer encoder, a path similarity value between each node in the concatenated word embedding vector based on the structural information of the abstract syntax tree of the concatenated word embedding vector, and to determine an adjacency relationship value between each node in the concatenated word embedding vector based on the structural information of the data flow graph of the concatenated word embedding vector; The fifth determining subunit is used to determine the dependency value of the concatenated word embedding vector in the grammar and the data stream according to the path similarity value between each node in the concatenated word embedding vector and the adjacency value between each node in the concatenated word embedding vector.
[0087] Based on the same inventive concept, the third aspect of the present application provides a Figure 4 The electronic device 100 shown includes a memory 110, a processor 120, and a computer program stored in the memory 110. The processor 120 executes the computer program to implement the code generation method as described in the first aspect of the present application.
[0088] Based on the same inventive concept, the fourth aspect of the present application provides a computer-readable storage medium on which a computer program / instruction is stored. When the computer program / instruction is executed by a processor, the code generation method described in the first aspect of the present application is implemented.
[0089] Each embodiment in this specification focuses on the differences from other embodiments, and the same or similar parts between the embodiments can be referenced to each other.
[0090] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, devices, or computer program products. Therefore, the embodiments of the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the embodiments of the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.
[0091] The embodiments of the present application are described with reference to the flowcharts and / or block diagrams of the methods, terminal devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing terminal device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0092] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing terminal device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0093] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device so that a series of operating steps are executed on the computer or other programmable terminal device to produce a computer-implemented process, thereby providing instructions for implementing the process in the computer or other programmable terminal device. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0094] Although the preferred embodiments of the present application have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present application.
[0095] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or terminal device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or terminal device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or terminal device including the elements.
[0096] The above provides a detailed introduction to a code generation method, system, electronic device and storage medium. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea. At the same time, for a person skilled in the art, according to the idea of the present application, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.
Claims
1. A code generation method, characterized in that: The method comprises: Obtain target text data of target test case; Inputting the target text data into a pre-trained code generation model; Performing word embedding processing on the target text data to obtain a structural word embedding vector, a grammatical word embedding vector, and a data stream word embedding vector corresponding to the target text data; The structural word embedding vector, the grammatical word embedding vector and the data stream word embedding vector are semantically encoded and decoded to obtain the target code of the target test case.
2. The code generation method according to claim 1, characterized in that: The code generation model is trained by the following steps: Acquire first text data of a historical test case and real code corresponding to the historical test case; Inputting the first text data into a code generation model to be trained, and obtaining a first text vector of the first text data through text preprocessing; Performing word embedding processing on the first text vector to obtain a structural word embedding vector of the first text data, a grammatical word embedding vector of the first text data, and a data stream word embedding vector of the first text data; Using a structure-aware encoder, concatenate the structural word embedding vector of the first text data, the grammatical word embedding vector of the first text data, and the data stream word embedding vector of the first text data to obtain a concatenated word embedding vector, and semantically encode and decode the concatenated word embedding vector to obtain a first code for the first text data; Calculate a target loss function according to the first code and the true code to obtain a loss value; Based on the loss value, parameters of the code generation model to be trained are updated to obtain the code generation model.
3. The code generation method according to claim 2, characterized in that: The performing word embedding processing on the first text vector to obtain a structural word embedding vector of the first text data, a grammatical word embedding vector of the first text data, and a data stream word embedding vector of the first text data includes: Inputting the first text vector into a structure word embedding tool for first word embedding processing, determining text structure features of the first text vector through the structure word embedding tool, and obtaining the structure word embedding vector based on the text structure features; Inputting the first text vector into a parser, generating an abstract syntax tree through the parser, and performing second word embedding processing using a grammatical word embedding tool to obtain the grammatical word embedding vector; The first text vector is input into a network diagram tool, a data flow diagram is generated by the network diagram tool, and a graph neural network is used to perform a third word embedding process to obtain the data flow word embedding vector.
4. The code generation method according to claim 3, characterized in that: The calculating the target loss function according to the first code and the real code comprises: Defining a first loss function according to a character difference value between a character in the first code and a character in the true code; A second loss function is defined according to the path difference value between the AST path of the first code and the AST path of the real code, defining a third loss function according to a dependency difference value between the data dependency of the first code and the data dependency of the real code; The target loss function is determined according to the first loss function, the second loss function and the third loss function.
5. The code generation method according to claim 4, characterized in that: The determining the target loss function according to the first loss function, the second loss function and the third loss function comprises: Get a first weight hyperparameter and a second weight hyperparameter; The target loss function is constructed by using the first weight hyperparameter as the weight of the second loss function and using the second weight hyperparameter as the weight of the third loss function.
6. The code generation method according to claim 2, characterized in that: The structure-aware encoder includes: a Transformer encoder and a Transformer decoder, and the semantic encoding and decoding of the concatenated word embedding vector to obtain the first code of the first text data includes: Determining, by means of the Transformer encoder, dependency values of the concatenated word embedding vector in syntax and data streams; The first code is obtained by using the Transformer decoder and taking the dependency value of the concatenated word embedding vector in the grammar and the data stream as input.
7. The code generation method according to claim 6, characterized in that: The step of determining, by the Transformer encoder, a dependency value of the concatenated word embedding vector in the grammar and the data stream includes: Obtaining structural information of an abstract syntax tree of the concatenated word embedding vector and structural information of a data flow graph of the concatenated word embedding vector; Determining, through the self-attention mechanism in the Transformer encoder, a path similarity value between each node in the concatenated word embedding vector based on the structural information of the abstract syntax tree of the concatenated word embedding vector, and determining an adjacency relationship value between each node in the concatenated word embedding vector based on the structural information of the data flow graph of the concatenated word embedding vector; Determine the dependency value of the concatenated word embedding vector in the grammar and the data stream according to the path similarity value between each node in the concatenated word embedding vector and the adjacency value between each node in the concatenated word embedding vector.
8. A code generation system, characterized in that: The system comprises: An acquisition module is used to acquire target text data of a target test case; An input module, used for inputting the target text data into a pre-trained code generation model; A processing module, used for performing word embedding processing on the target text data to obtain a structural word embedding vector, a grammatical word embedding vector and a data stream word embedding vector corresponding to the target text data; The semantic encoding and decoding module is used to perform semantic encoding and decoding on the structural word embedding vector, the grammatical word embedding vector and the data stream word embedding vector to obtain the target code of the target test case.
9. An electronic device comprising a memory, a processor and a computer program stored in the memory, characterized in that: The processor executes the computer program to implement the code generation method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program / instruction stored thereon, characterized in that: When the computer program / instructions are executed by a processor, the code generation method according to any one of claims 1 to 7 is implemented.
Citation Information
Cited By
Low-code visual programming system based on bidirectional AST dynamic analysis
CN120335861A
Low-code visual programming system based on bidirectional AST dynamic parsing
CN120335861B
Code generation method based on grammar discriminator and related equipment
CN120371274A