Code generation method and related device

By learning the similar features of the description information of class code and the abstract syntax tree in the code generation model, the problem of ambiguous information of abstract syntax tree in class-level code generation is solved, and the accuracy and quality of class code generation is improved.

CN120144135BActive Publication Date: 2025-08-19SHENZHEN UNIV
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202510630107.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-05-16
Publication Date
2025-08-19
Estimated Expiration
2045-05-16

AI Technical Summary

Technical Problem

When the existing deep learning-based code generation method generates class-level code, there is a problem that abstract syntax tree information is ambiguous in the training and inference stages, resulting in low accuracy of the generated class code.

Method used

By obtaining the generated description information of the class code to be generated, input it into the code generation model, the pre-trained twin encoder network learns the similar features between the description information of the class code and the abstract syntax tree, and generates the target class code to avoid the missing information of the abstract syntax tree.

Benefits of technology

Improve the accuracy and quality of class code generation to ensure that the generated class code meets user expectations.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120144135B_ABST
    Figure CN120144135B_ABST
Patent Text Reader

Abstract

This application discloses a code generation method and related devices, belonging to the field of code generation technology. The method comprises: 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, wherein the code generation model learns similarities between the class code description information and the abstract syntax tree. In this application, the accuracy of class code generation is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application belongs to the field of code generation technology, and specifically relates to a code generation method and related equipment. Background Art

[0002] Automatic code generation is crucial for improving software development efficiency and quality. Class code typically contains multiple member methods, member variable definitions, and relationships between classes. Class code not only involves rich grammatical structure and semantic logic, but also needs to capture the overall semantics and contextual dependencies of the class. Generating class code presents numerous challenges.

[0003] In the exemplary technology, an abstract syntax tree sequence is embedded in the model, and then weighting and other methods are used to aggregate the model input with the embedded abstract syntax tree sequence to train a model for generating class code.

[0004] However, during the model inference phase, the fragments of the class code that need to be generated cannot be obtained, which will lead to the missing of the abstract syntax tree sequence and the low accuracy of the class code generated by the model. Summary of the Invention

[0005] The purpose of the embodiments of the present application is to provide a code generation method and related equipment to solve the problem of low accuracy of class code generated by the model.

[0006] In a first aspect, an embodiment of the present application provides a code generation method, comprising:

[0007] Get the generation description information corresponding to the class code to be generated;

[0008] 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.

[0009] In some possible implementations, before obtaining the generation description information corresponding to the class code to be generated, the method further includes:

[0010] 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;

[0011] Processing the training code to obtain an abstract syntax tree sequence corresponding to the training code;

[0012] 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;

[0013] According to each of the target training samples, the preset model is trained to obtain a code generation model;

[0014] In which, 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 to update the parameters of the decoder and the encoder based on the generated class code and the training class code.

[0015] In some possible implementations, the encoder includes a first encoding module and a second encoding module, wherein:

[0016] The first encoding module is used to generate a syntax tree feature vector according to the abstract syntax tree;

[0017] The second encoding module is used to generate a description feature vector based on the training description information, wherein the preset model is used to update the parameters of the encoder based on the similarity between the description feature vector and the grammatical feature vector;

[0018] 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.

[0019] In some possible implementations, the first encoding module and the second encoding module are both pre-trained encoding modules.

[0020] In some possible implementations, the training of a preset model according to each of the target training samples includes:

[0021] 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;

[0022] 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.

[0023] In some possible implementations, 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:

[0024] Randomly sampling the abstract syntax tree sequence to obtain a sampled syntax tree sequence;

[0025] 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;

[0026] According to the training class code, the corresponding sampled 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.

[0027] In some possible implementations, processing the training code to obtain an abstract syntax tree sequence corresponding to the training code includes:

[0028] Converting the training class code into an abstract syntax tree;

[0029] A sequence extraction module is used to extract a linear sequence from the abstract syntax tree to obtain the abstract syntax tree sequence.

[0030] In some possible implementations, the sequence extraction module sets class-level definition node information, deletion information, control node information, and marking identifiers;

[0031] Wherein, the class-level definition node information is used to identify nodes in the abstract syntax tree;

[0032] The deletion information is used to delete redundant information of the first preset type of statement;

[0033] The control node information is used to perform an operation specified by the control node information on a statement of a second preset type;

[0034] The tag identifier is used to indicate the hierarchical relationship between word identifier nodes.

[0035] In a second aspect, an embodiment of the present application provides a code generation device, comprising:

[0036] An acquisition module is used to obtain generation description information corresponding to the class code to be generated;

[0037] An input module is used to input the generation 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.

[0038] In a third aspect, an embodiment of the present application provides an electronic device comprising a processor and a memory, wherein the memory stores programs or instructions that can be run on the processor, and when the programs or instructions are executed by the processor, the steps of the method described in the first aspect are implemented.

[0039] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, on which a program or instruction is stored. When the program or instruction is executed by a processor, the steps of the method described in the first aspect are implemented.

[0040] 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.

[0041] In an embodiment of the present application, by obtaining the generation description information of the class code to be generated, the generation description information is input into the code generation model to obtain the target class code output by the code generation model. In the present application, 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 similarly represent the description information and the abstract syntax tree, avoiding the loss of abstract syntax tree information in the model reasoning stage, that is, effectively introducing the abstract syntax tree so that the code generation model can accurately generate the class code, so that the generated class code meets the user's expectations, improves the generation accuracy of the class code, and thus improves the quality of the generated class code. BRIEF DESCRIPTION OF THE DRAWINGS

[0042] Figure 1 This is one of the flowcharts of a code generation method according to an exemplary embodiment;

[0043] Figure 2 This is a second flowchart of a code generation method according to an exemplary embodiment;

[0044] Figure 3 This is a third flowchart of a code generation method according to an exemplary embodiment;

[0045] Figure 4 is a schematic diagram of a training process of a code generation model according to an exemplary embodiment;

[0046] Figure 5 This is a fourth flowchart of a code generation method according to an exemplary embodiment;

[0047] Figure 6 This is a fifth flowchart of a code generation method according to an exemplary embodiment;

[0048] Figure 7 is a schematic diagram of an extraction process of a sequence extraction module according to an exemplary embodiment;

[0049] Figure 8 is a structural block diagram of a code generating device according to an exemplary embodiment;

[0050] Figure 9 The figure is a structural block diagram of an electronic device according to an exemplary embodiment. DETAILED DESCRIPTION

[0051] The following will be combined with the accompanying drawings in the embodiments of this application to clearly describe the technical solutions in the embodiments of this application. Obviously, the embodiments described are part of the embodiments of this application, not all of the embodiments. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field are within the scope of protection of this application.

[0052] The terms "first," "second," and the like in the specification and claims of this application are used to distinguish similar objects, and are not used to describe a particular order or precedence. It should be understood that the terms used in this manner are interchangeable where appropriate, so that the embodiments of this application can be implemented in an order other than that illustrated or described herein, and that the objects distinguished by "first," "second," and the like are generally of the same type, and do not limit the number of objects; for example, the first object can be one or more. In addition, the term "and / or" in the specification and claims refers to at least one of the connected objects, and the character " / " generally refers to before and after.

[0053] Automatic code generation is of great significance to improving software development efficiency and quality. Deep learning-based automatic code generation involves inputting concise natural language descriptions of code generation requirements into a deep neural network model, and the model automatically generates corresponding executable code based on the generation requirements. Currently, most code generation technologies focus on method-level or fragment-level code generation. However, class-level code (class code) typically contains multiple member methods, member variable definitions, and relationships between classes. Class code not only involves richer grammatical structures and semantic logic, but also needs to capture the overall semantics and contextual dependencies of the class. Compared with method-level code generation, class-level code generation still faces many challenges.

[0054] Some current leading generative language models exhibit significant performance degradation when generating class-level code compared to method-level code generation. Programming languages share similar high repeatability, predictability, and statistical properties with natural languages, so these statistical properties can be captured using statistical language models from the field of natural language processing. However, unlike natural languages, programming languages have precise, fixed grammatical structures and semantic logic specifications, with clear data flow and control flow dependencies. They are centered around logical expression and machine execution, emphasizing functional implementation. Therefore, deep learning-based code generation models should be able to effectively learn the grammatical structure and semantic logic of code. An Abstract Syntax Tree (AST) is a tree-structured representation of source code, serving as its logical skeleton. Many deep learning models use ASTs as input to capture the structural features of code and improve code comprehension and generation capabilities. However, ASTs are hierarchical tree structures. Before inputting the AST into a model, it is typically first traversed and extracted to linearize it into an AST sequence, which is then fed into the model using word embeddings.

[0055] Existing deep learning-based code generation methods typically use the following two approaches to introduce ASTs into the model. One approach is to embed the AST sequence separately at the model input and then aggregate it with other input information using weighting or other methods. This approach requires an additional encoder to extract the AST information and also suffers from the imbalance between the training and inference phases. That is, during the training phase, the corresponding AST sequence can be generated from the input code snippet, but during the inference phase, the AST information is missing because the code snippet is unavailable.

[0056] Another approach is to use AST construction sequences as the model generation target, which increases the complexity of the code generation task. In summary, deep learning-based code generation methods face two key issues when applied to class-level code generation. First, existing code AST extraction and serialization methods extract AST sequences that are much longer than the natural language sequences describing the code, increasing the difficulty of model learning to generate code and making it incompletely compatible with class-level code. Second, the way AST information is introduced into the model leads to an imbalance between the training and inference phases, as well as high model training complexity.

[0057] In order to solve the above problems, the present application proposes a code generation method, which 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 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 similarly represent the description information and the abstract syntax tree, avoiding the loss of abstract syntax tree information in the model reasoning 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, improves the generation accuracy of the class code, and thus improves the quality of the generated class code.

[0058] The code generation method and related devices provided in the embodiments of the present application are described in detail below with specific examples.

[0059] The code generation method provided in the embodiment of the present application is described in detail below.

[0060] Reference Figure 1 , Figure 1 One of the flow charts of the code generation method provided in this application is as follows: Figure 1 As shown, the code generation method includes the following steps:

[0061] Step S101: Obtain generation description information corresponding to the class code to be generated.

[0062] In this embodiment, the execution subject is a code generating device, which can be a device, system or server for generating class code. For ease of description, the code generating device is referred to as a device below.

[0063] When the user needs to generate class code, he inputs the generation description information corresponding to the class code to be generated into the device. Class code refers to class-level code, which includes multiple member methods, member variable definitions, and relationships between classes.

[0064] Generating description information refers to using natural language to describe the class code that needs to be generated. Natural language is different from programming language. Natural language refers to a language composed of text. That is, the user uses text to describe the class code that needs to be generated to obtain the generated description information.

[0065] In step S102 , 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.

[0066] The device includes a code generation model that learns similarities between the description information of a class code and an abstract syntax tree. This model, in other words, has the ability to represent the description information and the abstract syntax tree as similar. Specifically, an encoder in the code generation model learns similarities between the description information of a class code and the abstract syntax tree, enabling the encoder to represent the description information and the abstract syntax tree as similar.

[0067] After obtaining the generation description information, the device inputs the generation description information into the code generation model. The encoder in the code generation model infers the abstract syntax tree sequence based on the generation description information and learned similar features. The encoder then generates class code through the abstract syntax tree sequence and the generation description information. The class code is defined as the target class code, that is, the device obtains the target class code output by the code generation model.

[0068] In this embodiment, the generation description information of the class code to be generated is obtained and input into the code generation model to obtain the target class code output by the code generation model. In this embodiment, the code generation model learns the similarities between the class code description information and the abstract syntax tree, enabling the code generation model to represent the description information and the abstract syntax tree in a similar manner, thus avoiding the loss of abstract syntax tree information during the model inference phase. In other words, the effective introduction of the abstract syntax tree enables the code generation model to accurately generate the class code, thereby ensuring that the generated class code meets the user's expectations, improving the accuracy of class code generation, and thus improving the quality of the generated class code.

[0069] Reference Figure 2 , Figure 2 This is the second flow chart of the code generation method provided by this application, based on Figure 1 In the embodiment shown, before step S101, the following steps are further included:

[0070] Step S201: Acquire multiple training class codes and training description information corresponding to each training class code, where the training description information is used to indicate description information of the training class code.

[0071] In this embodiment, the device can also be used to train a code generation model.

[0072] The device obtains a plurality of class codes, each of which is defined as a training class code. Each training class code has a natural language annotation, and the annotation of the training class code is extracted as description information, which is defined as training description information, that is, the training description information is used to indicate description information of the training class code.

[0073] Step S202: Process the training code to obtain an abstract syntax tree sequence corresponding to the training code.

[0074] The device processes the training class code to obtain an abstract syntax tree sequence for the training class code. For example, the training class code is first subjected to syntax tree extraction to obtain an abstract syntax tree (AST), which is then linearly programmed to obtain an abstract syntax tree sequence. Compared to an abstract syntax tree, the abstract syntax tree sequence is more concise and retains the rich structural information of the class code.

[0075] Step S203: construct 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.

[0076] Based on the training class code, the corresponding abstract syntax tree sequence and the training description information, the device can construct the target training sample corresponding to the training class code. Each training class code corresponds to a target training sample, so the device can obtain multiple target training samples.

[0077] Step S204: training the preset model according to each target training sample to obtain a code generation model.

[0078] After obtaining multiple target training samples, a preset model is trained 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 similar features between the training description information of the training class code and the abstract syntax tree sequence. The encoder generates a target feature vector based on the learned similar features. The decoder is used to generate the class code based on the target feature vector, and the decoder and encoder parameters are updated based on the generated class code and the training class code.

[0079] Furthermore, the encoder includes a first encoding module and a second encoding module, the first encoding module and the second encoding module are encoders of the same network structure, and the first encoding module and the second encoding module constitute a twin network. Among them, the first encoding module is used to generate a syntax tree feature vector based on the abstract syntax tree, and the second encoding module is used to generate a description feature vector based on the training description information, and the preset model updates the encoder parameters based on the similarity between the description feature vector and the syntax tree feature vector. After the encoder parameters are updated, 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 based on the training description information and the abstract syntax tree sequence.

[0080] In addition, in order to enable the code generation model to learn effectively and quickly, the first encoding module and the second encoding module are both pre-trained encoding modules, that is, the first encoding module and the second encoding module both contain encoders obtained through preliminary training.

[0081] In this embodiment, the siamese network structure based on the pre-trained code model encoder can effectively introduce the AST structure information of the code into the code generation network model, make full use of the advantages of the pre-trained code model encoder, improve the effect of the code generation model in generating class-level code, and improve the training efficiency of the code generation model.

[0082] Refer to Figure 3 , Figure 3 which is the third flowchart of the code generation method provided by this application. Based on Figure 2 the embodiment shown in

[0083] 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.

[0084] Step S302: When the encoder finishes the similarity training, train the encoder and the decoder in the preset model according to each second training sample to obtain the 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.

[0085] In this embodiment, the encoder including the first encoding module and the second encoding module will be trained for similarity. 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.

[0086] Exemplarily, the target training sample includes the first training sample and the second training sample. The first training sample is constructed by the abstract syntax tree sequence and the training description information, and the second training sample is constructed by the training class code and the training description information. The device performs similarity training on the encoder including the first encoding module and the second encoding module through each first training sample.

[0087] When the encoder finishes the similarity training, enter the second round of training. The device 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 , briefly describe the training in two stages in this embodiment:

[0088] 1. Preparation stage

[0089] 1.1. First, obtain the training class code and the training description information. The two constitute <NL description, class-level code>, which is the second training sample. The NL description is the training description information, and the class-level code is the training class code;

[0090] 1.2. Data collection and processing are performed through the data processing layer. That is, the data processing layer extracts the abstract syntax tree and performs linear serialization processing on the class-level code to obtain the <NL description, AST sequence>, that is, the first training sample. The AST sequence is the abstract syntax tree sequence.

[0091] 2. Similarity training stage of the Siamese encoder

[0092] 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 as follows:

[0093] 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 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;

[0094] 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 and 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

[0095] (1)

[0096] where is the type of the encoding module, but is only for example and does not limit the encoding module to be ; represents encoding.

[0097] 2.3. The ES encoder also includes an average pooling layer. The average pooling layer 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 [[ID=—44]] As shown in formula (2), average pooling operations are performed on the two vectors C and A in the sequence length dimension. The dimension of the pooled vector is .

[0098]

[0099] That is, it describes the feature vector, which is the syntax tree feature vector.

[0100] 2.4. The ES encoder also has a similarity measurement layer for calculating the Euclidean distance between two and output by the average pooling layer. As shown in formula (3), where, represents in the th batch and the value of the th embedding dimension, represents in the th batch and the value of the th embedding dimension.

[0101]

[0102] The loss function based on the Euclidean distance is used to train the ES encoder to learn the similar representations of natural language description information and AST information. In order to enable the ES encoder to learn whether the vectors C and A are similar, a loss function based on the 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 the two 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.

[0103]

[0104] 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.

[0105] 3. The overall training of the ES encoder and the ES decoder (twin decoder), that is, the class-level code generation training stage

[0106] 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 then generate a hidden representation output that fuses code semantics and syntax information for encoding the demand information of the class-level natural language description that fuses the representation input, which is the target feature vector. Generated by formula (5):

[0107]

[0108] in, For the encoder, are encoder parameters. The natural language description of the input (NL description or NL description class level code annotation).

[0109] 3.2, ES decoder includes target code sequence input layer and decoding output layer, inputs class level code (training class code) into code sequence input layer, and Input to the ES decoder. The code generation task naturally incorporates the similarity between the natural language description of the code learned by the ES encoder and the AST information. This overcomes the information asymmetry during the training and reasoning phases when using the AST. The decoder output layer allows for a more accurate understanding of the natural language context and code structure during the decoding and code generation process, resulting in higher-quality code generation.

[0110] Among them, hidden means input , as shown in formula (5), the ES encoder generates, The ES encoder network parameters with the ability to represent the encoding of semantic and syntactic information of fused class-level codes are taken from the ES encoder saved at the end of training.

[0111] Decoding output layer, used for decoding , generate class-level code, as shown in formula (6), For the decoding output layer, For decoding parameters, is the predicted probability distribution of the code token generated at the t-th time step.

[0112]

[0113] The class-level code generation task loss function is used to train the ES decoder to generate high-quality class-level codes, 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.

[0114]

[0115] Update the parameters of the ES encoder and ES decoder using formula (7), and repeat steps 3.1 and 3.2 until training is completed to obtain the code generation model.

[0116] After obtaining the code generation model, the class-level code generation reasoning stage is entered, that is, the class-level code annotations (generation description information) described by NL are input into the code generation model, and the code generation model generates class code.

[0117] In this embodiment, in order to enable the code generation model to effectively learn and use the structural information of the class code, this embodiment proposes a twin encoder network and a similarity training process based on a pre-trained code model encoder and a similarity measurement layer. This is conducive to the comparative learning of the natural language description of the code and the code AST sequence, and is used to overcome the information asymmetry problem in the training and reasoning stage when using AST; at the same time, it is conducive to effectively utilizing the pre-trained code model's natural understanding ability of natural language.

[0118] 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 is achieved by sharing the weights of the twin encoder network ( Figure 4 The weight W in [ ] enables the model to learn the deep semantic and structural connections between the natural language description and the AST sequence of the same code segment, improving its ability to model the matching of natural language and code structure. The Siamese encoder network can represent information of one modality (the semantic information of the natural language description) as a latent vector that combines the semantic and syntactic information of the code. This vector is then fed into the decoder for code generation. This allows the model to generate high-quality class-level code directly based on the requirements of the input class-level natural language description during the inference phase.

[0119] Reference Figure 5 , Figure 5 This is the fourth flow chart of the code generation method provided by this application, based on Figure 2 or Figure 3 In the illustrated embodiment, step S203 includes:

[0120] Step S501: Randomly sample an abstract syntax tree sequence to obtain a sampled syntax tree sequence.

[0121] In this embodiment, when performing model training, negative samples can be automatically generated based on positive samples, without the need to obtain negative samples manually.

[0122] Exemplarily, after obtaining the abstract syntax tree sequence, the apparatus randomly samples the abstract syntax tree sequence to obtain a sampled syntax tree sequence.

[0123] Step S502 : 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.

[0124] Step S503 : constructing negative training samples corresponding to the training class code according to the training class code, the corresponding sampled syntax tree sequence and the training description information. The target training samples include positive training samples and negative training samples.

[0125] 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 a positive training sample and a negative training sample, the negative training sample is a data pair that does not match the training description information and the syntax tree sequence, and the positive training sample is a data pair that matches the training description information and the abstract syntax tree sequence. In this way, the original data set composed of the positive training sample is expanded to a data set that is twice the size, and the expanded data set includes positive training samples and negative training samples.

[0126] In this embodiment, negative training samples are obtained by randomly sampling the abstract syntax tree, and there is no need to obtain negative samples manually, thereby improving the efficiency of model training.

[0127] Reference Figure 6 , Figure 6 This is the fifth flow chart of the code generation method provided by this application, based on Figures 2 to 5 In any of the illustrated embodiments, step S202 includes:

[0128] Step S601: Convert the training code into an abstract syntax tree.

[0129] In this embodiment, a syntax tree conversion tool is provided in the apparatus, and the training code is converted into an abstract syntax tree by the syntax tree conversion tool.

[0130] Step S602: Using a sequence extraction module to extract a linear sequence from the abstract syntax tree to obtain an abstract syntax tree sequence.

[0131] 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.

[0132] In one example, the sequence extraction module is an existing sequence extraction tool.

[0133] 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 tag identifiers; 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 a first preset type statement; the control node information is used to perform the operation specified by the control node information on a second preset type statement; and the tag identifier is used to indicate the hierarchical relationship between word identifier nodes. Figure 7 , Figure 7 This is a diagram of the sequence extraction module in this example extracting linear sequences from the abstract syntax tree. The improvements are as follows:

[0134] 1. Add node information with definition level, such as Figure 7 Line2 in the Figure 7 In the row where number 2 is located, the node information at the definition level is the class-level definition node information;

[0135] 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.

[0136] 3. Add clause branch control node information. The clause branch is, for example, a second preset type statement. The clause branch control node information is information for executing a specified operation on the clause branch, such as line2.

[0137] 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 line 11 and " / / Add end tag" in line 21.

[0138] The sequence extraction module in this example processes the abstract syntax tree, converting the class-level code's AST into a relatively short linear AST sequence. This AST sequence contains rich structural information about the class-level code, making it more conducive to model learning. Compared to traditional AST serialization methods, the sequence extraction module provided in this example shortens the AST sequence while preserving the class-level code's structural information as much as possible, facilitating the effective introduction of class-level code AST information into the code generation model.

[0139] In this embodiment, in order to shorten the length of the class-level code AST sequence and retain as much class-level structural information as possible, the sequence extraction module is improved. Compared with the traditional AST serialization method, the improved sequence extraction module shortens the AST sequence length while retaining as much class-level code structural information as possible. Without increasing the input data modality, it is beneficial to effectively introduce the code AST information into the code generation model and facilitate the code generation model to effectively learn the correspondence between the natural language description information of the code and the AST information, thereby improving the code generation model's ability to identify the similarities and differences between the two types of information corresponding to the same code segment.

[0140] Based on the same inventive concept, the present application also provides a code generation device. Figure 7 The code generation device provided in the embodiment of the present application is described in detail.

[0141] Figure 8 The figure is a structural block diagram of a code generating device according to an exemplary embodiment.

[0142] like Figure 8 As shown, the code generating device 800 may include:

[0143] An acquisition module 810 is used to acquire generation description information corresponding to the class code to be generated;

[0144] The input module 820 is used to input the generated description information into the code generation model to obtain the target class code output by the code generation model, wherein the code generation model learns the similarity between the description information of the class code and the abstract syntax tree.

[0145] In one embodiment, the code generating device 800 is further configured to:

[0146] Acquire multiple training class codes and training description information corresponding to each training class code, where the training description information is used to indicate description information of the training class code;

[0147] Processing the training code to obtain an abstract syntax tree sequence corresponding to the training code;

[0148] Construct target training samples corresponding to the training class code according to the training class code, the corresponding abstract syntax tree sequence and the training description information;

[0149] According to each target training sample, the preset model is trained to obtain a code generation model;

[0150] 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. The decoder is used to generate the class code according to the target feature vector to update the decoder and the decoder parameters based on the generated class code and the training class code.

[0151] In one embodiment, the encoder in the code generating apparatus 800 includes a first encoding module and a second encoding module, wherein:

[0152] A first encoding module is used to generate a syntax tree feature vector according to the abstract syntax tree;

[0153] A second encoding module is used to generate a description feature vector based on the training description information, wherein the preset model is used to update the parameters of the encoder based on the similarity between the description feature vector and the grammatical feature vector;

[0154] 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.

[0155] In one embodiment, the first encoding module and the second encoding module are both pre-trained encoding modules.

[0156] In one embodiment, the code generating device 800 is further configured to:

[0157] Performing similarity training on the encoder based on 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;

[0158] 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 training description information, and the target training sample also includes the second training sample.

[0159] In one embodiment, the code generating device 800 is further configured to:

[0160] Randomly sample the abstract syntax tree sequence to obtain a sampled syntax tree sequence;

[0161] Constructing positive training samples corresponding to the training class code according to the training class code, the corresponding abstract syntax tree sequence and the training description information;

[0162] 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. The target training sample includes a positive training sample and a negative training sample.

[0163] In one embodiment, the code generating device 800 is further configured to:

[0164] Convert the training class code into an abstract syntax tree;

[0165] The sequence extraction module is used to extract the linear sequence of the abstract syntax tree to obtain the abstract syntax tree sequence.

[0166] In one embodiment, the sequence extraction module sets class-level definition node information, deletion information, control node information, and marking identification;

[0167] Among them, the class level defines node information, which is used to identify nodes in the abstract syntax tree;

[0168] Deletion information, used to delete redundant information of the first preset type of statement;

[0169] Control node information, used to execute the operation specified by the control node information on the second preset type statement;

[0170] Tag identifiers are used to indicate the hierarchical relationship between word identifier nodes.

[0171] The code generation device provided in the embodiment of the present application can implement the various processes implemented in the embodiment shown above and achieve the same technical effects. To avoid repetition, it will not be described here.

[0172] In some embodiments, as Figure 9 As shown, an embodiment of the present application further provides an electronic device 900, including a processor 901 and a memory 902, wherein the memory 902 stores a program or instruction that can be run on the processor 901, and when the program or instruction is executed by the processor 901, the various steps of the above-mentioned code generation method embodiment are implemented, and the same technical effect can be achieved. To avoid repetition, they are not repeated here.

[0173] It should be noted that the electronic devices in the embodiments of the present application include the above-mentioned mobile electronic devices and non-mobile electronic devices.

[0174] An embodiment of the present application also provides a computer-readable storage medium, on which a program or instruction is stored. When the program or instruction is executed by a processor, the various processes of the above-mentioned code generation method embodiment are implemented and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.

[0175] The processor is the processor in the electronic device in the above embodiment. The readable storage medium includes a computer-readable storage medium, such as a computer read-only memory, a random access memory, a magnetic disk, or an optical disk.

[0176] An embodiment of the present application provides a computer program product, which is stored in a storage medium. The program product is executed by at least one processor to implement the various processes of the above-mentioned code generation method embodiment and can achieve the same technical effect. To avoid repetition, it will not be repeated here.

[0177] It should be noted that, in this article, the terms "comprise", "include" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device comprising 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 device. In the absence of further restrictions, an element defined by the statement "comprises a ..." does not exclude the presence of other identical elements in the process, method, article or device comprising the element. In addition, it should be noted 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, and may also include performing functions in a substantially simultaneous manner or in the opposite order according to the functions involved. For example, the described method may be performed in an order different from that described, and various steps may also be added, omitted, or combined. In addition, the features described with reference to certain examples may be combined in other examples.

[0178] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus the necessary general hardware platform. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present application, 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 a number of instructions for enabling a terminal (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods of each embodiment of the present application.

[0179] The embodiments of the present application are described above in conjunction with the accompanying drawings, but the present application is not limited to the above-mentioned specific implementation methods. The above-mentioned specific implementation methods are merely illustrative and not restrictive. Under the guidance of this application, ordinary technicians in this field can also make many forms without departing from the purpose of this application and the scope of protection of the claims, all of which are within the protection of this application.

Claims

1. A code generation method, characterized in that: include: 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 (AST) 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, wherein the target training sample includes a first training sample and a second training sample, the first training sample being constructed based on the abstract syntax tree sequence and the training description information, and the second training sample being constructed based on the training class code and the training description information; According to each of the target training samples, a preset model is trained to obtain a code generation model, wherein 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, 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, and the encoder includes a first encoding module and a second encoding module; Get the generation description information corresponding to the class code to be generated; Inputting the generated description information into the code generation model to obtain the 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; The step of training the preset model according to each of the target training samples includes: According to each first training sample, the encoder is subjected to similarity training, including: inputting the abstract syntax tree sequence into the code AST sequence in the encoder, the code AST sequence performs feature processing on the abstract syntax tree, inputting the output features into the first encoding module, the first encoding module encodes the abstract syntax tree sequence to obtain a syntax tree feature vector; inputting the training description information into the natural language NL description sequence in the encoder, the NL description sequence performs feature processing on the training description information, inputting the output features into the second encoding module, the second encoding module encodes the training description information to obtain a description feature vector; the average pooling layer of the encoder performs an average pooling operation on the syntax tree feature vector and the description feature vector to obtain a pooled description feature vector and a syntax tree feature vector; the similarity measurement layer in the encoder calculates the Euclidean distance between the pooled description feature vector and the syntax tree feature vector; and updating the encoder through a loss function based on the Euclidean distance until the similarity training of the encoder is completed; 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.

2. The code generation method according to claim 1, characterized in that 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 syntax tree 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.

3. The code generation method according to claim 1, characterized in that The first encoding module and the second encoding module are both pre-trained encoding modules.

4. The code generation method according to claim 1, 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 sampled 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.

5. The code generation method according to claim 1, characterized in that: The processing of the training code to obtain an abstract syntax tree (AST) 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 AST sequence.

6. The code generation method according to claim 5, 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 an operation specified by the control node information on a statement of a second preset type; The tag identifier is used to indicate the hierarchical relationship between word identifier nodes.

7. A code generating device, characterized in that: include: A training description information acquisition module is used to obtain a plurality of training class codes and training description information corresponding to each of the training class codes, wherein the training description information is used to indicate description information of the training class code; A sequence obtaining unit, configured to process the training code to obtain an abstract syntax tree (AST) sequence corresponding to the training code; a sample construction unit, configured to construct 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, wherein the target training sample includes a first training sample and a second training sample, the first training sample being constructed based on the abstract syntax tree sequence and the training description information, and the second training sample being constructed based on the training class code and the training description information; a model training unit, configured to train a preset model according to each of the target training samples to obtain a code generation model, wherein the preset model includes an encoder and a decoder, the encoder is configured to learn 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 configured to generate a class code according to the target feature vector, and to update parameters of the decoder and the encoder based on the generated class code and the training class code, and the encoder includes a first encoding module and a second encoding module; An acquisition module is used to obtain generation description information corresponding to the class code to be generated; An input module, configured to input the generation description information into the code generation model to obtain a target class code output by the code generation model, wherein the code generation model learns similarities between the description information of the class code and the abstract syntax tree; When the model training unit trains the preset model according to each of the target training samples, it includes: According to each first training sample, the encoder is subjected to similarity training, including: inputting the abstract syntax tree sequence into the code AST sequence in the encoder, the code AST sequence performs feature processing on the abstract syntax tree, inputting the output features into the first encoding module, the first encoding module encodes the abstract syntax tree sequence to obtain a syntax tree feature vector; inputting the training description information into the natural language NL description sequence in the encoder, the NL description sequence performs feature processing on the training description information, inputting the output features into the second encoding module, the second encoding module encodes the training description information to obtain a description feature vector; the average pooling layer of the encoder performs an average pooling operation on the syntax tree feature vector and the description feature vector to obtain a pooled description feature vector and a syntax tree feature vector; the similarity measurement layer in the encoder calculates the Euclidean distance between the pooled description feature vector and the syntax tree feature vector; and updating the encoder through a loss function based on the Euclidean distance until the similarity training of the encoder is completed; 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.

8. The code generating device according to claim 7, characterized in that The first encoding module and the second encoding module are both pre-trained encoding modules.

9. 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 6 are implemented.

Citation Information

Patent Citations

  • Code generation method based on abstract syntax tree structure information enhancement

    CN114385178A