Code generation method and related equipment
By learning the similar features between the description information of class code and the abstract syntax tree in the code generation model, the problem of missing abstract syntax tree information in the inference stage of the code generation model is solved, and the accuracy and quality of class code generation are improved.
Patent Information
- Application Number
- CN202510630107.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-16
- Publication Date
- 2025-06-13
- Estimated Expiration
- 2045-05-16
AI Technical Summary
The existing code generation model cannot obtain fragments of generated class code during the inference stage, resulting in the missing sequence of abstract syntax trees, thereby reducing the accuracy of generated class code.
By obtaining the generated description information of the class code to be generated, input it into the code generation model, and using the model to learn similar features between the description information of the class code and the abstract syntax tree, the target class code is generated.
It effectively avoids the lack of abstract syntax tree information in the model inference stage, improves the accuracy and quality of the generation of class codes, and makes the generated class code meet the user's expectations.
Smart Images

Figure CN120144135A_ABST
Abstract
Description
Technical Field
[0001] This application belongs to the technical field of code generation, and particularly relates to a code generation method and related devices. Background Art
[0002] Automatic code generation is of great significance for improving software development efficiency and software quality. Class code usually contains multiple member methods, member variable definitions, and relationships between classes. Class code not only involves rich syntactic structures and semantic logics, but also needs to capture the overall semantics and context dependencies of classes. There are many challenges in generating class code.
[0003] In an exemplary technique, an abstract syntax tree sequence is embedded in a model, and then weighted methods and the like are used to aggregate the input of the model with the embedded abstract syntax tree sequence to train a model for generating class code.
[0004] However, in the model inference stage, the fragments of the class code to be generated cannot be obtained, resulting in the absence of the abstract syntax tree sequence and low accuracy of the class code generated by the model. Summary of the Invention
[0005] The purpose of the embodiments of this application is to provide a code generation method and related devices to solve the problem of low accuracy of the class code generated by the model.
[0006] In a first aspect, the embodiments of this application provide a code generation method, including: Obtaining generation description information corresponding to the class code to be generated; Inputting the generation description information into a code generation model to obtain the target class code output by the code generation model, where the code generation model learns the similarity features between the description information of the class code and the abstract syntax tree.
[0007] In some possible implementation manners, before obtaining the generation description information corresponding to the class code to be generated, it further includes: Obtaining multiple training class codes and training description information corresponding to each training class code, where the training description information is used to indicate the description information of the training class code; Processing the training class codes to obtain the corresponding abstract syntax tree sequences of the training class codes; Constructing target training samples corresponding to the training class codes according to the training class codes, the corresponding abstract syntax tree sequences, and the training description information; Training a preset model according to each target training sample to obtain a code generation model; Among them, the preset model includes an encoder and a decoder. The encoder is used to learn the similarity features between the training description information of the training class code and the abstract syntax tree sequence, and the encoder generates a target feature vector based on the learned similarity features. The decoder is used to generate a class code according to the target feature vector, so as to update the parameters of the decoder and the encoder based on the generated class code and the training class code.
[0008] In some possible implementation manners, the encoder includes a first encoding module and a second encoding module, where The first encoding module is used to generate a syntax tree feature vector according to the abstract syntax tree; The second encoding module is used to generate a description feature vector according to the training description information. The preset model is used to update the parameters of the encoder according to the similarity between the description feature vector and the syntax feature vector; The updated encoder is used to generate a target feature vector that integrates class code semantics and the abstract syntax tree sequence according to the training description information and the abstract syntax tree sequence.
[0009] In some possible implementation manners, both the first encoding module and the second encoding module are pre-trained encoding modules.
[0010] In some possible implementation manners, training the preset model according to each of the target training samples includes: Performing similarity training on the encoder according to each first training sample. The first training sample is constructed based on the abstract syntax tree sequence and the training description information. The target training sample includes the first training sample; When the encoder completes the similarity training, training the encoder and the decoder in the preset model according to each second training sample to obtain a code generation model. The second training sample is constructed based on the training class code and the training description information. The target training sample further includes the second training sample.
[0011] In some possible implementation manners, constructing the target training sample corresponding to the training class code according to the training class code, the corresponding abstract syntax tree sequence, and the training description information includes: Randomly sampling the abstract syntax tree sequence to obtain a sampled syntax tree sequence; Constructing a positive training sample corresponding to the training class code according to the training class code, the corresponding abstract syntax tree sequence, and the training description information; Construct a negative training sample corresponding to the training class code according to the training class code, the corresponding sampled syntax tree sequence, and the training description information, where the target training sample includes the positive training sample and the negative training sample.
[0012] In some possible implementations, the processing of the training class code to obtain the abstract syntax tree sequence corresponding to the training class code includes: Convert the training class code into an abstract syntax tree; Use a sequence extraction module to extract a linear sequence from the abstract syntax tree to obtain the abstract syntax tree sequence.
[0013] In some possible implementations, class-level definition node information, deletion information, control node information, and marking identifiers are set in the sequence extraction module; Among them, the class-level definition node information is used to identify nodes in the abstract syntax tree; The deletion information is used to delete redundant information from statements of a first preset type; The control node information is used to perform the operations specified by the control node information on statements of a second preset type; The marking identifier is used to indicate the hierarchical relationship between word identification nodes.
[0014] In a second aspect, an embodiment of the present application provides a code generation device, including: An acquisition module, configured to acquire generation description information corresponding to a class code to be generated; An input module, configured to input the generation description information into a code generation model to obtain a target class code output by the code generation model, where the code generation model learns the similarity features between the description information of the class code and the abstract syntax tree.
[0015] In a third aspect, an embodiment of the present application provides an electronic device, which includes a processor and a memory, and the memory stores a program or instruction that can run on the processor. When the program or instruction is executed by the processor, the steps of the method described in the first aspect are implemented.
[0016] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, and a program or instruction is stored on the computer-readable storage medium. When the program or instruction is executed by a processor, the steps of the method described in the first aspect are implemented.
[0017] In a fifth aspect, an embodiment of the present application provides a computer program product, which is stored in a storage medium and is executed by at least one processor to implement the method described in the first aspect.
[0018] In the embodiments of the present application, by obtaining the generation description information of the class code to be generated, and inputting the generation description information into the code generation model, the target class code output by the code generation model is obtained. In the present application, the code generation model learns the similarity features between the description information of the class code and the abstract syntax tree, enabling the code generation model to have the ability to represent the description information and the abstract syntax tree similarly, avoiding the lack of abstract syntax tree information in the model inference stage, that is, effectively introducing the abstract syntax tree to enable the code generation model to accurately generate the class code, so that the generated class code meets the user's expectations, improving the generation accuracy of the class code, and thus improving the quality of the generated class code. Description of the Drawings
[0019] Figure 1 is one of the schematic flowcharts of a code generation method shown according to an exemplary embodiment; Figure 2 is the second schematic flowchart of a code generation method shown according to an exemplary embodiment; Figure 3 is the third schematic flowchart of a code generation method shown according to an exemplary embodiment; Figure 4 is the schematic training flowchart of the code generation model shown according to an exemplary embodiment; Figure 5 is the fourth schematic flowchart of a code generation method shown according to an exemplary embodiment; Figure 6 is the fifth schematic flowchart of a code generation method shown according to an exemplary embodiment; Figure 7 is the schematic extraction flowchart of the sequence extraction module shown according to an exemplary embodiment; Figure 8 is the structural block diagram of a code generation device shown according to an exemplary embodiment; Figure 9 is the structural block diagram of an electronic device shown according to an exemplary embodiment. Detailed Embodiments
[0020] Next, the technical solutions in the embodiments of the present application will be clearly described in conjunction with the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art belong to the scope of protection of the present application.
[0021] The terms "first", "second", etc. in the description and claims of this application are used to distinguish similar objects, rather than to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged under appropriate circumstances, so that the embodiments of this application can be implemented in an order other than those illustrated or described here, and the objects distinguished by "first", "second", etc. are usually of the same category, and the number of objects is not limited. For example, the first object can be one or more. In addition, "and / or" in the description and claims means at least one of the connected objects, and the character " / ", generally means before and after.
[0022] Code automatic generation is of great significance for improving software development efficiency and software quality. Code automatic generation based on deep learning is to input a concise natural language description of code generation requirements into a deep neural network model, and the model automatically generates corresponding executable code based on the generation requirements. At present, most of the code generation related technologies focus on code generation at the method level or fragment level. However, class-level code (class code) usually contains multiple member methods, member variable definitions, and relationships between classes. Class code not only involves richer syntax structures and semantic logics, but also needs to capture the overall semantics and context dependencies of the class. Compared with method-level code generation, there are still many challenges in class-level code generation.
[0023] When some current excellent generative large language models execute the task of generating class-level code, the performance metrics show a significant decline compared to method-level code generation. Programming languages have similar highly repetitive, predictable, and statistical characteristics to natural languages, so the statistical characteristics of program code can be captured by statistical language models in the field of natural language processing. However, different from natural languages, programming languages have precise and fixed syntax structures and semantic logic specifications, clear data flow and control flow dependency relationships, and are centered around logical expression and machine execution, emphasizing function implementation. Therefore, code generation models based on deep learning should be able to effectively learn the syntax structure and semantic logic information of code. The Abstract Syntax Tree (AST) is a tree-structured representation of source code and is the logical skeleton of source code. Many deep learning models will use the AST as input to capture the structural features of code and improve code understanding and generation capabilities. However, the AST is a hierarchical tree structure. Before the model inputs the AST, it usually traverses and extracts it to linearize it into an AST sequence, and then inputs it into the model through word embedding.
[0024] Existing deep learning-based code generation methods usually introduce the AST into the model in the following two ways. One way is to separately embed the AST sequence at the input end of the model, and then use methods such as weighting to aggregate it with other input information. This way requires an additional encoder to extract AST information and there is also a problem of asymmetry between the training and inference stages. That is, in the training stage, the corresponding AST sequence can be generated through the known code segments of the input, but in the inference stage, the AST information is missing because the code segments are not available.
[0025] Another way is to use the AST construction sequence as the model generation target, which increases the complexity of the code generation task. In short, when deep learning-based code generation methods are used for class-level code generation, there are two key problems. First, in the existing code AST extraction and serialization methods, the length of the extracted AST sequence is much longer than the natural language sequence describing the code, which increases the difficulty of the model learning to generate code and is not fully compatible with class-level code. Second, the way of introducing AST information into the model has problems such as asymmetry between the training and inference stages and relatively high model training complexity.
[0026] To solve the above problems, this application proposes a code generation method. This method obtains the generation description information of the class code to be generated, inputs the generation description information into the code generation model, and obtains the target class code output by the code generation model. The code generation model learns the similarity features between the description information of the class code and the abstract syntax tree, so that the code generation model has the ability to represent the description information and the abstract syntax tree similarly, avoiding the lack of abstract syntax tree information in the model inference stage, that is, effectively introducing the abstract syntax tree to enable the code generation model to accurately generate class code, so that the generated class code meets the user's expectations, improving the generation accuracy of the class code, and thus improving the quality of the generated class code.
[0027] The following uses specific embodiments to elaborate in detail on the code generation method and related devices provided in the embodiments of this application.
[0028] The following elaborates in detail on the code generation method provided in the embodiments of this application.
[0029] Refer to Figure 1 , Figure 1 One of the flow diagrams of the code generation method provided in this application is as Figure 1 shown. The code generation method includes the following steps: Step S101, obtain the generation description information corresponding to the class code to be generated.
[0030] In this embodiment, the execution entity is a code generation device, which can be a device, system, or server for generating class code. For the sake of convenience in description, the device is hereinafter used to refer to the code generation device.
[0031] When a user needs to generate class code, the user inputs generation description information corresponding to the class code to be generated to the device. Class code refers to class-level code, which includes multiple member methods, member variable definitions, and the relationships between classes.
[0032] The generation description information refers to the descriptive text of the class code to be generated in natural language. Natural language is different from programming language. Natural language refers to the language composed of words, that is, the user uses words to describe the class code to be generated to obtain the generation description information.
[0033] Step S102: Input the generation description information into the code generation model to obtain the target class code output by the code generation model. Among them, the code generation model learns the similar features between the description information of the class code and the abstract syntax tree.
[0034] The code generation model is set in the device. The code generation model is a model that has learned the similar features between the description information of the class code and the abstract syntax tree, that is, the code generation model has the ability to perform similar representation on the description information and the abstract syntax tree. Specifically, the encoder in the code generation model has learned the similar features between the description information of the class code and the abstract syntax tree, so that the encoder has the ability to perform similar representation on the description information and the abstract syntax tree.
[0035] After the device obtains the generation description information, it inputs the generation description information into the code generation model. The encoder in the code generation model infers an abstract syntax tree sequence based on the generation description information and the learned similar features. Then, the encoder generates class code through the abstract syntax tree sequence and the generation description information. This class code is defined as the target class code, that is, the device obtains the target class code output by the code generation model.
[0036] In this embodiment, by obtaining the generation description information of the class code to be generated and inputting the generation description information into the code generation model, the target class code output by the code generation model is obtained. In this embodiment, the code generation model learns the similar features between the description information of the class code and the abstract syntax tree, so that the code generation model has the ability to perform similar representation on the description information and the abstract syntax tree, avoiding the lack of abstract syntax tree information in the model inference stage. That is, effectively introducing the abstract syntax tree enables the code generation model to accurately generate class code, so that the generated class code meets the user's expectations, improving the generation accuracy of the class code and thus improving the quality of the generated class code.
[0037] Refer toFigure 2 , Figure 2 is the second flowchart diagram of the code generation method provided by this application. Based on the Figure 1 embodiment shown, before step S101, it further includes: Step S201, obtain multiple training class codes and the training description information corresponding to each training class code, where the training description information is used to indicate the description information of the training class code.
[0038] In this embodiment, the device can also be used for training the code generation model.
[0039] The device obtains multiple class codes, and these class codes are all defined as training class codes. Each training class code has a natural language comment, and the comment of the training class code is extracted as the description information, and this description information is defined as the training description information, that is, the training description information is used to indicate the description information of the training class code.
[0040] Step S202, process the training class code to obtain the abstract syntax tree sequence corresponding to the training class code.
[0041] The device processes the training class code to obtain the abstract syntax tree sequence of the training class code. Exemplarily, first perform syntax tree extraction on the training class code to obtain the abstract syntax tree AST, and then perform linear programming processing on the AST to obtain the abstract syntax tree sequence. Compared with the abstract syntax tree, the abstract syntax tree sequence is more concise, and the abstract syntax tree sequence retains the rich structural information of the class code.
[0042] Step S203, construct the target training sample corresponding to the training class code according to the training class code, the corresponding abstract syntax tree sequence, and the training description information.
[0043] The device can construct the target training sample corresponding to the training class code based on the training class code, the corresponding abstract syntax tree sequence, and the training description information. Each training class code corresponds to a target training sample, so the device can obtain multiple target training samples.
[0044] Step S204, train a preset model according to each target training sample to obtain a code generation model.
[0045] After obtaining multiple target training samples, train the preset model based on each target training sample to obtain a code generation model. The preset model includes an encoder and a decoder. The encoder is used to learn the similar features between the training description information of the training class code and the abstract syntax tree sequence, and the encoder generates a target feature vector based on the learned similar features. The decoder is used to generate a class code according to the target feature vector to update the parameters of the decoder and the encoder based on the generated class code and the training class code.
[0046] Further, the encoder includes a first encoding module and a second encoding module. The first encoding module and the second encoding module are encoders with the same network structure, and the first encoding module and the second encoding module form a siamese network. Among them, the first encoding module is used to generate a syntax tree feature vector according to the abstract syntax tree, and the second encoding module is used to generate a description feature vector according to the training description information. And the preset model updates the parameters of the encoder based on the similarity between the description feature vector and the syntax tree feature vector. After the parameters of the encoder are updated, the updated encoder is used to generate a target feature vector that combines class-level code semantics and the abstract syntax tree sequence according to the training description information and the abstract syntax tree sequence.
[0047] In addition, in order to enable the code generation model to learn effectively and quickly, both the first encoding module and the second encoding module are pre-trained encoding modules, that is, the encoders in both the first encoding module and the second encoding module are obtained through preliminary training.
[0048] In this embodiment, based on the siamese network structure composed of the pre-trained code model encoder, the AST structure information of the code can be effectively introduced into the code generation network model, making full use of the advantages of the pre-trained code model encoder, improving the effect of the code generation model in generating class-level code, and improving the training efficiency of the code generation model.
[0049] Refer to Figure 3 , Figure 3 is the third flowchart of the code generation method provided by this application. Based on Figure 2 the embodiment shown, step S204 includes: Step S301, perform similarity training on the encoder according to each first training sample. The first training sample is constructed based on the abstract syntax tree sequence and the training description information, and the target training sample includes the first training sample.
[0050] Step S302, when the encoder completes the similarity training, train the encoder and the decoder in the preset model according to each second training sample to obtain a code generation model. The second training sample is constructed based on the training class code and the training description information, and the target training sample further includes the second training sample.
[0051] In this embodiment, the encoder including the first encoding module and the second encoding module will be subjected to similarity training. After the similarity training of the encoder is completed, the encoder and the decoder will be trained as a whole. That is, the training of the preset model is divided into two rounds of training.
[0052] Exemplarily, the target training samples include first training samples and second training samples. The first training samples are constructed from abstract syntax tree sequences and training description information, and the second training samples are constructed from training class codes and training description information. The apparatus performs similarity training on the encoder containing the first encoding module and the second encoding module through each first training sample.
[0053] When the encoder completes the similarity training, it enters the second round of training. The apparatus trains the encoder and the decoder based on each second training sample. After the training is completed, the code generation model can be obtained. Refer to Figure 4 , based on Figure 4 , a brief description of the training in the two stages in this embodiment is as follows: 1. Preparation stage 1.1. First, obtain the training class code and the training description information, and the two constitute <NL description, class-level code>, which is also the second training sample. The NL description is the training description information, and the class-level code is the training class code; 1.2. Through the data processing layer, data collection and processing are performed, that is, the abstract syntax tree extraction and linear serialization processing are performed on the class-level code through the data processing layer to obtain <NL description, AST sequence>, that is, the first training sample is obtained, and the AST sequence is the abstract syntax tree sequence; 2. Similarity training stage of the siamese encoder Input the first training sample <NL description, AST sequence> into the ES encoder for similarity training of the siamese encoder. The similarity training stage is specifically as follows: 2.1. Input the AST sequence into the code AST sequence in the ES encoder. The code AST sequence is the AST input sequence layer. The code AST sequence performs feature processing on the AST, and the code AST sequence inputs the output features into the pre-trained code model encoder (the first encoding module). The first encoding module encodes the AST sequence A to obtain the hidden vector A, , where is the batch size, is the sequence length, is the dimension of the embedding vector; 2.2. Input the NL sequence into the NL description sequence in the ES encoder. The NL description sequence is the NL input sequence layer. The NL description sequence performs feature processing on the NL description. The NL description sequence inputs the output features into the pre-trained code model encoder (the second encoding module). The second encoding module encodes the NL description C to obtain the hidden vector C, , where (1) where is the type of the encoding module, but is only for illustration and does not limit the encoding module to be ; represents encoding.
[0054] 2.3. The ES encoder also includes an average pooling layer, which performs average pooling operations on the syntax tree feature vector and the description feature vector. The average pooling layer is used to calculate the Euclidean distance metric similarity for two three-dimensional vectors with different shapes and As shown in formula (2), average pooling operations are performed on two vectors C and A in the sequence length dimension. The dimension of the pooled vector is .
[0055] is the description feature vector, is the syntax tree feature vector.
[0056] 2.4. The ES encoder is also provided with a similarity metric layer for calculating the Euclidean distance between the two and output by the average pooling layer. As shown in formula (3), where represents at the th batch, the value of the th embedding dimension, represents at the th batch, the value of the th embedding dimension.
[0057] The loss function based on Euclidean distance is used to train the ES encoder to learn the similar representation of natural language description information and AST information. In order to enable the ES encoder to learn whether vectors C and A are similar, a loss function based on Euclidean distance is designed. As shown in formula (4), where is the label of the sample pair (y has two values, 1 and 0. 1 means they match the same code snippet, and 0 means they do not match), is the calculated Euclidean distance, is the threshold for controlling the loss of negative samples.
[0058] 2.5. Update the ES encoder through , and loop through 2.1 to 2.5 until the similarity training of the ES encoder is completed.
[0059] 3. Overall training of the ES encoder and the ES decoder (twin decoder), which is also the class-level code generation training phase 3.1. Input the second training sample <NL description, class-level code> into the model. Among them, the NL description is input into the ES encoder, and the class-level code is input into the ES decoder. The ES encoder can generate a hidden representation output that fuses code semantic and syntactic information , which is used to encode the demand information of the class-level natural language description that fuses the representation input, i.e., the target feature vector. Generated by formula (5): Among them, is the encoder, are the encoder parameters. is the input natural language description (NL description or NL description class-level code annotation).
[0060] 3.2. The ES decoder includes a target code sequence input layer and a decoding output layer. Input the class-level code (training class code) into the code sequence input layer, and input into the ES decoder. In the code generation task, the similarity feature between the code natural language description information and the AST information learned by the ES encoder is naturally introduced, which is used to overcome the problem of information asymmetry in the training and inference phases when using the AST. The decoding output layer can more accurately understand the natural language context and code structure features during the decoding and code generation process, and generate higher-quality code.
[0061] Among them, the hidden representation input , as shown in formula (5), is encoded and generated by the ES encoder, is taken from the ES encoder network parameters saved at the end of the ES encoder training, which have the encoding ability of the semantic and syntactic information representation of the fused class-level code.
[0062] The decoding output layer is used to decode , and generate the class-level code, as shown in formula (6), is the decoding output layer, are the decoding parameters, is the predicted probability distribution of the code token generated at the t-th time step.
[0063] The class-level code generation task loss function is used to train the ES decoder to generate high-quality class-level code, as shown in formula (7), is the predicted probability distribution of the code token generated by the model at time t, is the actual code token of the target class-level code sequence at time t, is the length of the sequence.
[0064] Update the parameters of the ES encoder and the ES decoder through formula (7), and loop through 3.1 and 3.2 until the training is completed, then the code generation model can be obtained.
[0065] After obtaining the code generation model, enter the class-level code generation inference stage, that is, input the class-level code annotation (generate description information) described by NL into the code generation model, and the code generation model will generate class code.
[0066] In this embodiment, in order to enable the code generation model to effectively learn and use the structural information of class code, this embodiment proposes a siamese encoder network and a similarity training process based on the pre-trained code model encoder and the similarity measurement layer, which is conducive to the contrast learning between the natural language description of the code and the code AST sequence, and is used to overcome the problem of information asymmetry in the training and inference stages when using AST; at the same time, it is conducive to effectively using the natural language understanding ability of the pre-trained code model.
[0067] Different from the traditional way of introducing AST structure information, this embodiment enables the model to have the ability to represent the natural language description of the code and the corresponding AST sequence as similar vectors. This process enables the model to learn the deep semantic and structural associations between the natural language description and the AST sequence of the same piece of code by sharing the weights of the siamese encoder network ( Figure 4 the weight W in), and improves the model's ability to model the matching of natural language and code structure. The siamese encoder network can represent the information of one modality (the semantic information of the natural language description) as a hidden vector that integrates the code semantics and syntax information, and provide it to the decoder to generate code, which is conducive to the model directly generating high-quality class-level code according to the required information of the input class-level natural language description during the inference stage.
[0068] Refer to Figure 5 , Figure 5 is the fourth flow chart of the code generation method provided by this application. Based on Figure 2 or Figure 3 the embodiments shown, step S203 includes: Step S501, randomly sample the abstract syntax tree sequence to obtain the sampled syntax tree sequence.
[0069] In this embodiment, when training the model, negative samples can be automatically generated based on positive samples without obtaining negative samples through artificial means.
[0070] Exemplarily, after obtaining the abstract syntax tree sequence, the device performs random sampling on the abstract syntax tree sequence to obtain a sampled syntax tree sequence.
[0071] Step S502: According to the training class code, the corresponding abstract syntax tree sequence, and the training description information, construct a positive training sample corresponding to the training class code.
[0072] Step S503: According to the training class code, the corresponding sampled syntax tree sequence, and the training description information, construct a negative training sample corresponding to the training class code. The target training sample includes the positive training sample and the negative training sample.
[0073] After obtaining the sampled syntax tree sequence, the device constructs the training class code, the corresponding abstract syntax tree sequence, and the training description information into a positive training sample; the device constructs the training class code, the corresponding sampled syntax tree sequence, and the training description information into a negative training sample. That is, the target training sample includes the positive training sample and the negative training sample. The negative training sample is a data pair where the training description information does not match the syntax tree sequence, and the positive training sample is a data pair where the training description information matches the abstract syntax tree sequence. In this way, the original data set composed of positive training samples is expanded to a data set twice as large. The expanded data set includes positive training samples and negative training samples.
[0074] In this embodiment, negative training samples are obtained by randomly sampling the abstract syntax tree, eliminating the need to obtain negative samples manually and improving the model training efficiency.
[0075] Refer to Figure 6 , Figure 6 is the fifth flowchart of the code generation method provided by this application. Based on Figures 2 to 5 any of the illustrated embodiments, step S202 includes: Step S601: Convert the training class code into an abstract syntax tree.
[0076] In this embodiment, a syntax tree conversion tool is provided in the device, and the training class code is converted into an abstract syntax tree through the syntax tree conversion tool.
[0077] Step S602: Use the sequence extraction module to extract a linear sequence from the abstract syntax tree to obtain an abstract syntax tree sequence.
[0078] The device is also provided with a sequence extraction module. After obtaining the abstract syntax tree, the sequence extraction module extracts a linear sequence from the abstract syntax tree to obtain an abstract syntax tree sequence.
[0079] In one example, the sequence extraction module is an existing sequence extraction tool.
[0080] In another example, the sequence extraction module is an improved sequence extraction tool. The sequence extraction module sets class-level definition node information, deletion information, control node information, and marking identification; wherein the class-level definition node information is used to identify nodes in the abstract syntax tree; the deletion information is used to delete redundant information of the first preset type statement; the control node information is used to perform the operation specified by the control node information on the second preset type statement; the marking identification is used to indicate the hierarchical relationship between word identification nodes. Figure 7 , Figure 7 This is a schematic diagram of the sequence extraction module in this example extracting linear sequences from the abstract syntax tree. The improvements are as follows: 1. Add node information with definition level, such as Figure 7 In line2, line2 is Figure 7 In the row where the sequence number 2 is located, the node information at the definition level is the class-level definition node information; 2. Reduce the redundancy of the expression_statement node. The expression_statement node is the first preset type statement. The deleted information refers to the information that needs to be deleted from the expression_statement node. For example, the reduced redundancy is line4, line6, line10, line17, and line20. 3. Add clause branch control node information, where the clause branch is, for example, a second preset type statement, and the clause branch control node information is information for executing a specified operation on the clause branch, such as line2; 4. Use semantic words such as Begin and End to identify the hierarchical relationship between nodes instead of using separators. Semantic Begin and End are identification tags. For example, identification tags are " / / Add start tag" in line11 and " / / Add end tag" in line21.
[0081] Through the processing of the abstract syntax tree by the sequence extraction module in this example, the AST of the class-level code is converted into a relatively short AST linear sequence, which contains rich structural information of the class-level code and is more conducive to model learning. Compared with the traditional AST serialization method, the sequence extraction module provided in this example shortens the AST sequence while retaining the structural information of the class-level code as much as possible, which is conducive to effectively introducing the AST information of the class-level code into the code generation model.
[0082] In this embodiment, in order to shorten the length of the AST sequence of the class-level code and retain as much class-level structure information as possible, the sequence extraction module is improved. Compared with the traditional AST serialization method, the improved sequence extraction module not only shortens the length of the AST sequence but also retains the structure information of the class-level code as much as possible. Without increasing the input data modality, it is beneficial to effectively introduce the AST information of the code into the code generation model and is also beneficial for the code generation model to effectively learn the correspondence between the natural language description information and the AST information of the code, thereby improving the code generation model's ability to recognize the similarities and differences between the two types of information corresponding to the same piece of code.
[0083] Based on the same inventive concept, the present application also provides a code generation device. The following will Figure 7 describe in detail the code generation device provided in the embodiments of the present application.
[0084] Figure 8 is a structural block diagram of a code generation device shown according to an exemplary embodiment.
[0085] As Figure 8 shown, the code generation device 800 may include: An acquisition module 810, configured to acquire the generation description information corresponding to the class code to be generated; An input module 820, configured to input the generation description information into the code generation model to obtain the target class code output by the code generation model, where the code generation model learns the similarity features between the description information of the class code and the abstract syntax tree.
[0086] In one embodiment, the code generation device 800 is further configured to: Acquire a plurality of training class codes and the training description information corresponding to each training class code, where the training description information is used to indicate the description information of the training class code; Process the training class codes to obtain the abstract syntax tree sequences corresponding to the training class codes; Construct target training samples corresponding to the training class codes according to the training class codes, the corresponding abstract syntax tree sequences, and the training description information; Train a preset model according to each target training sample to obtain a code generation model; Wherein, the preset model includes an encoder and a decoder. The encoder is configured to learn the similarity features between the training description information of the training class code and the abstract syntax tree sequence, and the encoder generates a target feature vector based on the learned similarity features. The decoder is configured to generate a class code according to the target feature vector to update the parameters of the decoder and the decoder based on the generated class code and the training class code.
[0087] In one embodiment, the encoder in the code generation device 800 includes a first encoding module and a second encoding module, where The first encoding module is used to generate a syntax tree feature vector according to the abstract syntax tree; The second encoding module is used to generate a description feature vector according to the training description information, where a preset model is used to update the parameters of the encoder according to the similarity between the description feature vector and the syntax feature vector; The updated encoder is used to generate a target feature vector that combines the code semantics and the abstract syntax tree sequence according to the training description information and the abstract syntax tree sequence.
[0088] In one embodiment, both the first encoding module and the second encoding module are pre-trained encoding modules.
[0089] In one embodiment, the code generation device 800 is further used for: Performing similarity training on the encoder according to each first training sample, where the first training sample is constructed based on the abstract syntax tree sequence and the training description information, and the target training sample includes the first training sample; When the encoder completes the similarity training, training the encoder and the decoder in the preset model according to each second training sample to obtain a code generation model, where the second training sample is constructed based on the training code and the training description information, and the target training sample further includes the second training sample.
[0090] In one embodiment, the code generation device 800 is further used for: Randomly sampling the abstract syntax tree sequence to obtain a sampled syntax tree sequence; Constructing a positive training sample corresponding to the training code according to the training code, the corresponding abstract syntax tree sequence, and the training description information; Constructing a negative training sample corresponding to the training code according to the training code, the corresponding sampled syntax tree sequence, and the training description information, and the target training sample includes the positive training sample and the negative training sample.
[0091] In one embodiment, the code generation device 800 is further used for: Converting the training code into an abstract syntax tree; Using a sequence extraction module to extract a linear sequence from the abstract syntax tree to obtain an abstract syntax tree sequence.
[0092] In one embodiment, class-level definition node information, deletion information, control node information, and marking identifiers are set in the sequence extraction module; Among them, the class-level definition node information is used to identify the nodes in the abstract syntax tree; Deletion information is used to delete redundant information from statements of the first preset type; Control node information is used to perform the operations specified by the control node information on statements of the second preset type; Marker identifier is used to indicate the hierarchical relationship between word identification nodes.
[0093] The code generation device provided by the embodiments of this application can implement each process implemented by the above - shown embodiments, achieving the same technical effects. To avoid repetition, it will not be elaborated here.
[0094] In some embodiments, as Figure 9 shown, the embodiments of this application also provide an electronic device 900, including a processor 901 and a memory 902. A program or instruction that can run on the processor 901 is stored on the memory 902. When the program or instruction is executed by the processor 901, it implements each step of the above - mentioned code generation method embodiment and can achieve the same technical effects. To avoid repetition, it will not be elaborated here.
[0095] It should be noted that the electronic devices in the embodiments of this application include the above - mentioned mobile electronic devices and non - mobile electronic devices.
[0096] The embodiments of this application also provide a computer - readable storage medium. A program or instruction is stored on the computer - readable storage medium. When the program or instruction is executed by a processor, it implements each process of the above - mentioned code generation method embodiment and can achieve the same technical effects. To avoid repetition, it will not be elaborated here.
[0097] Among them, the processor is the processor in the electronic device in the above - mentioned embodiment. The readable storage medium includes computer - readable storage media such as computer read - only memory, random access memory, magnetic disk, or optical disk, etc.
[0098] The embodiments of this application provide a computer program product. The program product is stored in a storage medium. The program product is executed by at least one processor to implement each process of the above - mentioned code generation method embodiment and can achieve the same technical effects. To avoid repetition, it will not be elaborated here.
[0099] It should be noted that in this text, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, such that a process, method, article or device comprising a series of elements not only includes those elements but also other elements not expressly listed, or elements inherent to such process, method, article or device. Without further limitation, an element defined by the statement "comprising one..." does not exclude the presence of additional identical elements in the process, method, article or device comprising that element. In addition, it should be pointed out that the scope of the methods and devices in the embodiments of the present application is not limited to performing functions in the order shown or discussed, but may also include performing functions in a substantially simultaneous manner or in the reverse order according to the functions involved. For example, the described methods may be performed in an order different from that described, and various steps may be added, omitted, or combined. Additionally, features described with reference to certain examples may be combined in other examples.
[0100] Through the description of the above embodiments, those skilled in the art can clearly understand that the methods of the above embodiments can be implemented by means of software plus a necessary general hardware platform. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation. Based on such an understanding, the technical solution of the present application, in essence or the part that contributes to the prior art, can be embodied in the form of a computer software product. The computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) and includes several instructions for causing a terminal (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods of the various embodiments of the present application.
[0101] The embodiments of the present application have been described above with reference to the accompanying drawings. However, the present application is not limited to the above specific embodiments. The above specific embodiments are merely illustrative and not restrictive. Under the inspiration of the present application, those of ordinary skill in the art can also make many forms without departing from the purpose of the present application and the scope protected by the claims, and all of them fall within the protection scope of the present application.
Claims
1. A code generation method, characterized in that: include: Obtain generation description information corresponding to the class code to be generated; The generated description information is input into a code generation model to obtain a target class code output by the code generation model, wherein the code generation model learns similar features between the description information of the class code and the abstract syntax tree.
2. The code generation method according to claim 1, characterized in that: Before obtaining the generation description information corresponding to the class code to be generated, the method further includes: Acquire multiple training class codes and training description information corresponding to each of the training class codes, where the training description information is used to indicate description information of the training class code; Processing the training code to obtain an abstract syntax tree sequence corresponding to the training code; Constructing a target training sample corresponding to the training class code according to the training class code, the corresponding abstract syntax tree sequence and the training description information; According to each of the target training samples, a preset model is trained to obtain a code generation model; Among them, the preset model includes an encoder and a decoder, the encoder is used to learn the training description information of the training class code and the similar features between the abstract syntax tree sequence, and the encoder generates a target feature vector based on the learned similar features, and the decoder is used to generate a class code according to the target feature vector, so as to update the parameters of the decoder and the encoder based on the generated class code and the training class code.
3. The code generation method according to claim 2, characterized in that: The encoder includes a first encoding module and a second encoding module, wherein: The first encoding module is used to generate a syntax tree feature vector according to the abstract syntax tree; The second encoding module is used to generate a description feature vector according to the training description information, wherein the preset model is used to update the parameters of the encoder according to the similarity between the description feature vector and the grammatical feature vector; The updated encoder is used to generate a target feature vector that integrates the semantics of the class code and the abstract syntax tree sequence according to the training description information and the abstract syntax tree sequence.
4. The code generation method according to claim 3, characterized in that: The first encoding module and the second encoding module are both pre-trained encoding modules.
5. The code generation method according to claim 2, characterized in that: The step of training the preset model according to each of the target training samples includes: Performing similarity training on the encoder according to each first training sample, wherein the first training sample is constructed based on the abstract syntax tree sequence and the training description information, and the target training sample includes the first training sample; When the encoder completes similarity training, the encoder and decoder in the preset model are trained according to each second training sample to obtain a code generation model, the second training sample is constructed based on the training class code and the training description information, and the target training sample also includes the second training sample.
6. The code generation method according to claim 2, characterized in that: The step of constructing a target training sample corresponding to the training class code according to the training class code, the corresponding abstract syntax tree sequence and the training description information includes: Randomly sampling the abstract syntax tree sequence to obtain a sampled syntax tree sequence; Constructing a positive training sample corresponding to the training class code according to the training class code, the corresponding abstract syntax tree sequence and the training description information; According to the training class code, the corresponding sampling syntax tree sequence and the training description information, a negative training sample corresponding to the training class code is constructed, and the target training sample includes the positive training sample and the negative training sample.
7. The code generation method according to claim 2, characterized in that: The processing of the training code to obtain an abstract syntax tree sequence corresponding to the training code includes: Converting the training class code into an abstract syntax tree; A sequence extraction module is used to extract a linear sequence from the abstract syntax tree to obtain the abstract syntax tree sequence.
8. The code generation method according to claim 7, characterized in that: The sequence extraction module sets class-level definition node information, deletion information, control node information and marking identification; Wherein, the class-level definition node information is used to identify nodes in the abstract syntax tree; The deletion information is used to delete redundant information of the first preset type of statement; The control node information is used to perform the operation specified by the control node information on the second preset type statement; The tag identifier is used to indicate the hierarchical relationship between word identifier nodes.
9. A code generating device, characterized in that: include: An acquisition module is used to obtain generation description information corresponding to the class code to be generated; An input module is used to input the generated description information into a code generation model to obtain a target class code output by the code generation model, wherein the code generation model learns similar features between the description information of the class code and the abstract syntax tree.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a program or instruction, and when the program or instruction is executed by a processor, the steps of the code generation method according to any one of claims 1 to 8 are implemented.
Citation Information
Patent Citations
Method for automatically generating Python codes from natural language
CN110489102A
Code generation method combining model driving and deep neural network
CN113849162A
Code generation method based on abstract syntax tree structure information enhancement
CN114385178A
Generation method and device of class-level code abstract
CN119248927A
Code Generation Tool for Cloud-Native High-Performance Computing
US20220261227A1