Code annotation generation method and device and storage medium
By generating a data stream sequence and a code sequence of the target code, and using a code annotation generation model to output target annotations, the problem of low accuracy in code annotation generation in existing technologies is solved, achieving higher accuracy and readability.
Patent Information
- Application Number
- CN202511603652.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-04
- Publication Date
- 2026-02-10
AI Technical Summary
Existing technologies have low accuracy in generating code comments, making it difficult to handle changes in comments caused by code updates or refactoring.
By receiving the target code, the corresponding data stream sequence and code sequence are generated. The target annotation is generated by using code annotations, including parsing into an abstract syntax tree, identifying variable nodes and dependencies, performing data cleaning and normalization, and using a network structure with dual encoders and a single decoder for feature vector fusion.
It improves the accuracy of code comment generation, enriches feature extraction information, and enhances code readability and maintainability.
Smart Images

Figure CN121501337A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of artificial intelligence, and more specifically, to a method, apparatus, and storage medium for generating code annotations. Background Technology
[0002] Code comment generation refers to the process of generating natural language descriptions for source code snippets, either automatically or semi-automatically. High-quality code comments help other developers quickly understand the functionality and design principles of the code, reducing code reading and debugging time and improving code readability and maintainability.
[0003] In existing technologies, code annotation generation technology is usually based on information retrieval. This method mainly generates new annotations by retrieving and matching known annotations. It lacks sufficient ability to capture and process code features and long-distance dependencies in the code context. Therefore, its effectiveness is highly dependent on the quality and richness of the corpus. Moreover, it is difficult to handle annotation changes caused by code updates or refactoring, resulting in inaccurate annotations.
[0004] There is currently no effective solution to the problem of low accuracy in code comment generation in related technologies. Summary of the Invention
[0005] The main objective of this application is to provide a method, apparatus, and storage medium for generating code comments, so as to solve the problem of low accuracy in the generation of code comments in related technologies.
[0006] To achieve the above objectives, according to one aspect of this application, a method for generating code annotations is provided. The method includes: receiving target code for which annotations are to be generated; generating a data stream sequence corresponding to the target code and obtaining a code sequence of the target code; inputting the data stream sequence and the code sequence into a code annotation generation model; and using the code annotation generation model to output target annotations corresponding to the target code.
[0007] Furthermore, generating the data flow sequence corresponding to the target code includes: using a code parsing tool to parse the target code into an abstract syntax tree; traversing the abstract syntax tree to identify multiple variable nodes and obtaining the variable dependencies between the multiple variable nodes; and generating the data flow sequence corresponding to the target code based on the variable dependencies.
[0008] Furthermore, obtaining the code sequence of the target code includes: cleaning the target code to obtain the first code; decomposing the first code into a token sequence and normalizing the token sequence to obtain the second code; and adding a preset tag to the second code to obtain the code sequence.
[0009] Furthermore, the target annotation corresponding to the target code is output using the code annotation generation model, which includes: determining the second feature vector of the data stream sequence based on the code encoder in the code annotation generation model; determining the first feature vector of the data stream sequence based on the data stream encoder in the code annotation generation model; and fusing the first feature vector and the second feature vector based on the preset decoder in the code annotation generation model to generate the target annotation.
[0010] Furthermore, the training steps of the code annotation generation model include: obtaining a training dataset of code annotations, which includes multiple code samples and annotation descriptions for each code sample; generating corresponding data stream sequence samples based on the code samples, and extracting code sequence samples from the code samples; and training a preset recurrent neural network model based on the data stream sequence samples, code sequence samples, and annotation descriptions to obtain the code annotation generation model.
[0011] Furthermore, training a pre-defined recurrent neural network model based on data stream sequence samples, code sequence samples, and annotation descriptions to obtain a code annotation generation model includes: training a first sub-model in the pre-defined recurrent neural network model based on data stream sequence samples and annotation descriptions to obtain a trained first sub-model; obtaining the data stream encoder in the trained first sub-model and transferring the data stream encoder to a second sub-model in the pre-defined recurrent neural network model to obtain a transferred second sub-model; and training the transferred second sub-model based on data stream sequence samples, code sequence samples, and annotation descriptions to obtain a code annotation generation model.
[0012] Furthermore, before training the first sub-model in the preset recurrent neural network model based on the data stream sequence samples and annotation descriptions, the method further includes: determining a first encoder, a first decoder, a second encoder, and a second decoder based on the gated recurrent unit; determining a first sub-model based on the first encoder and the first decoder; and determining a second sub-model based on the second encoder and the second decoder.
[0013] To achieve the above objectives, according to another aspect of this application, a code annotation generation apparatus is provided. The apparatus includes: a code receiving unit for receiving target code for which annotations are to be generated; a sequence acquisition unit for generating a data stream sequence corresponding to the target code and acquiring a code sequence of the target code; and an annotation generation unit for inputting the data stream sequence and the code sequence into a code annotation generation model, and using the code annotation generation model to output target annotations corresponding to the target code.
[0014] Furthermore, the sequence acquisition unit includes: a code parsing module, used to parse the target code into an abstract syntax tree using a code parsing tool; a node traversal module, used to traverse the abstract syntax tree to identify multiple variable nodes and obtain the variable dependencies between the multiple variable nodes; and a data stream sequence generation module, used to generate a data stream sequence corresponding to the target code based on the variable dependencies.
[0015] Furthermore, the sequence acquisition unit includes: a first acquisition module for cleaning the target code to obtain a first code; a second acquisition module for decomposing the first code into a token sequence and normalizing the token sequence to obtain a second code; and a third acquisition module for adding a preset marker to the second code to obtain a code sequence.
[0016] Furthermore, the annotation generation unit includes: a first vector module, used to determine the second feature vector of the data stream sequence based on the code encoder in the code annotation generation model; a second vector module, used to determine the first feature vector of the data stream sequence based on the data stream encoder in the code annotation generation model; and a result generation module, used to fuse the first feature vector and the second feature vector based on the preset decoder in the code annotation generation model and generate the target annotation.
[0017] Furthermore, the device includes: a dataset acquisition unit for acquiring a training dataset of code annotations, the training dataset including multiple code samples and annotation descriptions for each code sample; a sample sequence unit for generating corresponding data stream sequence samples based on the code samples and extracting code sequence samples from the code samples; and a model training unit for training a preset recurrent neural network model based on the data stream sequence samples, code sequence samples, and annotation descriptions to obtain a code annotation generation model.
[0018] Furthermore, the model training unit includes: a first training module, used to train a first sub-model in a preset recurrent neural network model based on data stream sequence samples and annotation descriptions, to obtain a trained first sub-model; an encoder transfer module, used to obtain a data stream encoder in the trained first sub-model and transfer the data stream encoder to a second sub-model in the preset recurrent neural network model, to obtain a transferred second sub-model; and a second training module, used to train the transferred second sub-model based on data stream sequence samples, code sequence samples, and annotation descriptions, to obtain a code annotation generation model.
[0019] Furthermore, the device also includes: a gated recurrent module for determining a first encoder, a first decoder, a second encoder, and a second decoder based on the gated recurrent unit before training a first sub-model in a preset recurrent neural network model based on data stream sequence samples and annotation descriptions; a first determination module for determining a first sub-model based on the first encoder and the first decoder; and a second determination module for determining a second sub-model based on the second encoder and the second decoder.
[0020] According to another aspect of this application, a computer-readable storage medium is provided, the computer-readable storage medium including a stored program, wherein, when the program is running, it controls the device where the computer-readable storage medium is located to execute any method for generating code comments.
[0021] According to another aspect of this application, an electronic device is provided, comprising: one or more processors, a memory, and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs include a method for generating any kind of code annotation.
[0022] According to another aspect of this application, a computer program product is provided, including computer instructions, and steps for generating code comments that implement any of the above when the computer instructions are executed by a processor.
[0023] In this embodiment, the technical problem of low accuracy in generating code annotations in the prior art is solved by receiving the target code to be annotated; generating a data stream sequence corresponding to the target code and obtaining the code sequence of the target code; inputting the data stream sequence and the code sequence into the code annotation generation model; and using the code annotation generation model to output the target annotation corresponding to the target code.
[0024] By receiving the target code and obtaining the corresponding data stream sequence and code sequence, the structural information and code text of the target code are extracted. Compared with the existing technology that only linearly reads the target code, this enriches the information for feature extraction. Then, the target annotation is generated through the code annotation generation model, which improves the accuracy of code annotation generation. Attached Figure Description
[0025] The accompanying drawings, which form part of this application, are used to provide a further understanding of this application. The illustrative embodiments and descriptions of this application are used to explain this application and do not constitute an undue limitation of this application. In the drawings:
[0026] Figure 1 A hardware block diagram of a computer terminal for implementing a method for generating code comments is shown.
[0027] Figure 2This is a flowchart of a method for generating code comments according to an embodiment of this application;
[0028] Figure 3 This is a schematic diagram of a code annotation generation apparatus provided according to an embodiment of this application;
[0029] Figure 4 This is a structural block diagram of an electronic device according to an embodiment of this application. Detailed Implementation
[0030] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present application, and not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative effort should fall within the scope of protection of the present application.
[0031] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0032] First, some nouns or terms that appear in the description of the embodiments of this application shall be interpreted as follows:
[0033] It should be noted that the information collected in this application (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for display, data used for analysis, etc.) are information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, storage, use, processing, transmission, provision, disclosure, and application of this data all comply with relevant laws, regulations, and standards, necessary confidentiality measures have been taken, and they do not violate public order and good morals. Corresponding access points are provided for users to choose to authorize or refuse. For example, interfaces are set up between this system and relevant users or organizations, providing users with corresponding access points to choose to agree to or refuse automated decision-making results; if the user chooses to refuse, the process proceeds to the expert decision-making stage.
[0034] Example 1
[0035] According to an embodiment of this application, a method embodiment for generating code comments is also provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.
[0036] The method embodiment provided in Embodiment 1 of this application can be executed on a mobile terminal, computer terminal, or similar computing device. Figure 1 A hardware block diagram of a computer terminal (or mobile device) for implementing a method for generating code comments is shown. Figure 1 As shown, the computer terminal 10 (or mobile device) may include one or more processors 102 (shown as 102a, 102b, ..., 102n in the figure) 102 (processor 102 may include, but is not limited to, a microprocessor MCU or a programmable logic device FPGA, etc.), a memory 104 for storing data, and a transmission device 106 for communication functions. In addition, it may also include: a display, an input / output interface (I / O interface), a universal serial bus (USB) port (which may be included as one of the ports of a BUS bus), a network interface, a power supply, and / or a camera. Those skilled in the art will understand that... Figure 1 The structure shown is for illustrative purposes only and does not limit the structure of the aforementioned electronic device. For example, computer terminal 10 may also include... Figure 1 The more or fewer components shown, or having the same Figure 1 The different configurations shown.
[0037] It should be noted that the aforementioned one or more processors 102 and / or other data processing circuits are generally referred to herein as "data processing circuits". These data processing circuits may be embodied, in whole or in part, in software, hardware, firmware, or any other combination thereof. Furthermore, the data processing circuits may be a single, independent processing module, or may be integrated, in whole or in part, into any other element within the computer terminal 10 (or mobile device). As involved in the embodiments of this application, the data processing circuits serve as a processor control mechanism (e.g., selection of a variable resistor termination path connected to an interface).
[0038] The memory 104 can be used to store software programs and modules of application software, such as the program instructions / data storage device corresponding to the code comment generation method in this embodiment. The processor 102 executes various functional applications and data processing by running the software programs and modules stored in the memory 104, thereby implementing the above-mentioned code comment generation method. The memory 104 may include high-speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include memory remotely located relative to the processor 102, and these remote memories can be connected to the computer terminal 10 via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.
[0039] The transmission device 106 is used to receive or send data via a network. Specific examples of the network described above may include a wireless network provided by the communication provider of the computer terminal 10. In one example, the transmission device 106 includes a Network Interface Controller (NIC), which can connect to other network devices via a base station to communicate with the Internet. In another example, the transmission device 106 may be a Radio Frequency (RF) module, used for wireless communication with the Internet.
[0040] The display may be, for example, a touchscreen liquid crystal display (LCD) that allows the user to interact with the user interface of the computer terminal 10 (or mobile device).
[0041] Under the aforementioned operating environment, this application provides the following: Figure 2 The method for generating code comments is shown. Figure 2 This is a flowchart of the method for generating code comments according to Embodiment 1 of this application.
[0042] Step S201: Receive the target code to be annotated.
[0043] Optionally, the code comment generation method in this embodiment can be integrated with a code repository and the target code can be pulled from the repository. A code upload interface can also be set up, allowing users to directly upload files containing the target code to be annotated. Once the file is received through the code upload interface, the target code is parsed from the file.
[0044] Step S202: Generate the data stream sequence corresponding to the target code and obtain the code sequence of the target code.
[0045] Optionally, the data flow sequence is used to characterize the code execution logic and data flow direction of the target code, while the code sequence is used to characterize the code text of the target code. Existing technologies only consider the code sequence, ignoring the structural information of the target code. This embodiment, however, generates a data flow sequence corresponding to the target code, thus reflecting the structural information from the perspectives of code execution logic and data flow direction.
[0046] Step S203: Input the data stream sequence and code sequence into the code annotation generation model, and use the code annotation generation model to output the target annotation corresponding to the target code.
[0047] Optionally, the data stream sequence and code sequence can be vectorized separately through an embedding layer to obtain vector representations of the data stream sequence and the code sequence. These two vector representations are then input into the code annotation generation model. The code annotation generation model can be a network structure with a dual encoder and a single decoder. The dual encoders are a data stream encoder and a code encoder. An attention mechanism can be introduced into the recurrent neural network model to obtain the aforementioned data stream encoder and code encoder. The output of the code annotation generation model is the target annotation for the target code. Those skilled in the art, knowing the above-mentioned input data, network structure, and parameters of the code annotation generation model, can set the number of layers in the recurrent neural network for each encoder and train the code annotation generation model as needed.
[0048] In summary, by receiving the target code and obtaining the corresponding data stream sequence and code sequence, the structural information and code text of the target code are extracted. Compared with the existing technology that only linearly reads the target code, this method enriches the information for feature extraction. Furthermore, the target annotation is generated through the code annotation generation model, thereby improving the accuracy of code annotation generation.
[0049] To improve the accuracy of code comment generation, optionally, generating the data flow sequence corresponding to the target code includes: parsing the target code into an abstract syntax tree using a code parsing tool; traversing the abstract syntax tree to identify multiple variable nodes and obtaining the variable dependencies between the multiple variable nodes; and generating the data flow sequence corresponding to the target code based on the variable dependencies.
[0050] Optionally, code parsing tools can be used to parse the target code into an abstract syntax tree (AST). An AST is a tree-like data structure that clearly displays the syntax information of the target code. The terminal leaf nodes in the AST are used to identify variable sequences. Traversal can begin from the root node of the AST. By traversing each node, multiple variable nodes and their dependencies can be identified. For example, variable dependencies could be variable updates within a for loop or parameter passing in a function call. Based on the AST traversal, variable nodes are treated as nodes, and variable dependencies as edges. A data flow graph is constructed based on the nodes and edges, and then the data flow graph is converted into a sequence to obtain the data flow sequence.
[0051] In summary, the code parsing tool parses the target code into an abstract syntax tree, obtains the variable dependencies between multiple variable nodes, and then generates the data flow sequence corresponding to the target code. It effectively captures the data flow between variables from the perspective of dynamic code execution, provides richer semantic information for the code annotation generation model, and improves the accuracy of code annotation generation.
[0052] To improve the accuracy of code comment generation, optionally, obtaining the code sequence of the target code includes: performing data cleaning on the target code to obtain the first code; decomposing the first code into a token sequence and normalizing the token sequence to obtain the second code; and adding preset tags to the second code to obtain the code sequence.
[0053] Optionally, data cleaning of the target code can be performed to remove irrelevant information, such as comments, spaces, newlines, whitespace characters, version control instructions, and version declarations, to highlight the substantive content of the target code. After removing irrelevant information, the remaining code can be uniformly converted into a standard format to ensure consistency and obtain the first code.
[0054] Optionally, the first code can be decomposed into a series of tokens using tokenization (i.e., word segmentation). These tokens can include function names, variable names, operators, and keywords, resulting in a token sequence. Normalizing this token sequence—for example, by standardizing identifiers (e.g., converting all variable names into a generic VAR tag)—helps the code annotation model focus on the core semantics of the target code, thus yielding the second code.
[0055] Optionally, a preset marker can be added to the second code, such as adding a start marker at the beginning of the second code (e.g., <s>And add an end marker at the end of the second code (e.g., <e>), to help the code comment generation model identify the start point and the end point of the code sequence, and further enhance the understanding of the code sequence by the code comment model.
[0056] In summary, through data cleaning, standardization processing and adding preset markers, not only the redundant information in the target code is removed, but also the code sequence is obtained in a standardized and structured manner, so that the code comment generation model can more easily learn the substantive content of the target code, and the generation accuracy of the code comment is improved.
[0057] To improve the generation accuracy of the code comment, optionally, outputting the target comment corresponding to the target code by the code comment generation model comprises: determining a second feature vector of the data flow sequence according to a code encoder in the code comment generation model; determining a first feature vector of the data flow sequence according to a data flow encoder in the code comment generation model; and fusing the first feature vector and the second feature vector by a preset decoder in the code comment generation model to generate the target comment.
[0058] For example, the code comment generation model can be a network structure of double encoders plus a single decoder, the double encoders being a data flow encoder and a code encoder. The code encoder can be used to encode the data flow sequence to extract the flow information between different variables of the target code during execution, to obtain the first feature vector; and the code encoder can be used to encode the code sequence to extract the code text meaning of the target code, to obtain the second feature vector. The preset decoder is used to fuse the first feature vector and the second feature vector, and generate a target prediction sequence word by word through a gated recurrent unit, and the target comment is obtained by texturizing (for example, using the string function str to texturize) according to the target prediction sequence.
[0059] In summary, by using two encoders for encoding, the first feature vector and the second feature vector are obtained, not only the code text meaning of the target code is extracted, but also the flow information between different variables of the target code is extracted, so that the generated comment is more in line with the actual function of the target code, and the generation accuracy of the code comment is improved.
[0060] To improve the generation accuracy of the code comment, optionally, the training steps of the code comment generation model comprise: obtaining a training data set of code comments, the training data set comprising a plurality of code samples and a comment description of each code sample; generating a corresponding data flow sequence sample according to the code sample, and extracting a code sequence sample of the code sample; and training a preset recurrent neural network model according to the data flow sequence sample, the code sequence sample and the comment description, to obtain the code comment generation model.
[0061] Optionally, code samples and corresponding natural language annotation descriptions can be collected from an open source code repository to build a training data set, for example, the code data set DeepCom in the open source code repository is taken as the training data set. As in the processing manner of step 202 described above, in the training stage, data stream sequence samples (corresponding to the data stream sequence in the inference stage) and code sequence samples (corresponding to the code sequence in the inference stage) also need to be obtained according to the code samples in the training data set. The preset recurrent neural network can include a data stream encoder, a code encoder and a preset decoder.
[0062] In summary, by collecting the training data set of code annotations and obtaining the data stream sequence samples and the code sequence samples, and then taking the data stream sequence samples and the code sequence samples as the prediction factors and the annotation descriptions as the prediction labels, the preset recurrent neural network model is trained to obtain the code annotation generation model, thereby improving the generation accuracy of the code annotations.
[0063] In order to improve the generation accuracy of the code annotations, optionally, the preset recurrent neural network model is trained according to the data stream sequence samples, the code sequence samples and the annotation descriptions to obtain the code annotation generation model, including: the first sub-model in the preset recurrent neural network model is trained according to the data stream sequence samples and the annotation descriptions to obtain the trained first sub-model; the data stream encoder in the trained first sub-model is obtained and is migrated to the second sub-model of the preset recurrent neural network model to obtain the migrated second sub-model; the migrated second sub-model is trained according to the data stream sequence samples, the code sequence samples and the annotation descriptions to obtain the code annotation generation model.
[0064] Optionally, the training process of the code annotation generation model in the embodiment is multi-task training, which is specifically divided into two training stages. In the first training stage, a first sub-model is trained, the first sub-model including a first encoder and a first decoder, the first sub-model being used to learn how to map data stream sequence samples to annotation descriptions (i.e., the input data is the data stream sequence samples, and the output data is the annotation descriptions), to obtain a trained first sub-model, and the first encoder in the trained first sub-model is determined as a data stream encoder. A second sub-model includes a second encoder and a second decoder, the second encoder being used to encode code sequence samples to generate corresponding feature vectors, and the second encoder being used to map the feature vectors output by the data stream encoder and the feature vectors output by the second encoder to the annotation descriptions. In the second training stage, a double-encoder-single-decoder structure (the data stream encoder, the second encoder, and the second decoder) is formed according to the data stream encoder and the second sub-model. At this time, the data stream sequence samples are input into the data stream encoder, and the code sequence samples are input into the second encoder. The second sub-model after migration is trained to update the parameters of the data stream encoder, the second encoder, and the second decoder, to obtain the code annotation model. The second training stage and the first training stage can use the same training data set. The loss function can be an average cross-entropy loss function, and the optimizer can select an Aadm optimizer or a gradient descent optimization algorithm.
[0065] In summary, in the first training stage, the data stream encoder learns how to encode the data stream sequence samples in the process of mapping the data stream sequence samples to the annotation descriptions. In the second training stage, the second encoder captures the static information of the code sequence samples, and the second decoder learns how to fit the feature vectors output by the data stream encoder and the second encoder to the annotation descriptions. Through the two training stages, the preset recurrent neural network model can gradually deepen the understanding of the target code to the annotation descriptions on the same training data. First, the relationship between the data stream sequence samples and the annotation descriptions is learned, and then the relationship between the data stream sequence samples and the code sequence samples and the annotation descriptions is learned. This ensures that the model can generate accurate annotations for unseen code segments, and improves the generation accuracy of the code annotations.
[0066] To improve the generation accuracy of the code annotations, before the first sub-model in the preset recurrent neural network model is trained according to the data stream sequence samples and the annotation descriptions, the method further includes: determining the first encoder, the first decoder, the second encoder, and the second decoder according to the gated recurrent unit; determining the first sub-model according to the first encoder and the first decoder; and determining the second sub-model according to the second encoder and the second decoder.
[0067] Optionally, the gated recurrent unit is a variant of recurrent neural network, which can effectively avoid the gradient vanishing problem and control the information flow through the gating mechanism when processing long sequence data, so when constructing the encoder and the decoder in the embodiment, the gated recurrent unit can be selected to enhance the long-distance memory and learning ability of the preset recurrent neural network. The first sub-model and the second sub-model are both network structures of encoder-decoder, the first encoder and the second decoder both include an input layer, a gated recurrent unit layer and a context vector output layer, the input layer can be an embedding layer, used to receive the input sequence (the first encoder receives the data stream sequence sample, and the second encoder receives the code sequence sample) to convert the sequence into a vector representation; the gated recurrent unit layer is used to use the gated recurrent unit to be in the sequence, receive the current input and the hidden state of the last time step at each time step and output the hidden state of the current time step; the context vector output layer is used to take the last hidden state of the encoder as the context vector. The first decoder and the second decoder both include an input layer, a gated recurrent unit layer and an output layer, the input layer is used to receive the context vector, the gated recurrent unit layer is used to process the context vector using the gated recurrent unit, and the output layer can be a fully connected layer, used to convert the output of the gated recurrent unit into a vector of the size of the vocabulary and generate a probability distribution through a softmax function to represent the probability of the next word. For example, the first sub-model and the second sub-model can both be a seq2seq model (sequence-to-sequence, a deep learning model for processing sequence-to-sequence mapping), to introduce the attention mechanism into the gated recurrent unit to further improve the performance of the preset recurrent neural network model.
[0068] In summary, by using the gated recurrent unit to construct the first sub-model and the second sub-model, the ability of the preset recurrent neural network model to process long sequence data is improved, and the generation accuracy of the code annotation is improved.
[0069] It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0070] Embodiment 2
[0071] The embodiment of the present application also provides a code annotation generation device. It should be noted that the code annotation generation device of the embodiment of the present application can be used to execute the code annotation generation method provided by the embodiment of the present application. The code annotation generation device provided by the embodiment of the present application is introduced as follows.
[0072] According to the embodiment of the present application, a device for implementing the above-mentioned code annotation generation method is also provided, such as a computer device. Figure 3 The device comprises:
[0073] The code receiving unit 301 is configured to receive target code to be generated with annotations.
[0074] The sequence obtaining unit 302 is configured to generate a data flow sequence corresponding to the target code and obtain a code sequence of the target code.
[0075] The annotation generating unit 303 is configured to input the data flow sequence and the code sequence into a code annotation generation model, and output target annotations corresponding to the target code by using the code annotation generation model.
[0076] The code annotation generation device provided in the embodiments of the present application receives target code to be generated with annotations by using the code receiving unit 301, generates a data flow sequence corresponding to the target code and obtains a code sequence of the target code by using the sequence obtaining unit 302, inputs the data flow sequence and the code sequence into a code annotation generation model by using the annotation generating unit 303, and outputs target annotations corresponding to the target code by using the code annotation generation model, thereby solving the problem of low accuracy of code annotation generation in the related art. Furthermore, the effect of improving the checking efficiency of the standard-compliant materials is achieved.
[0077] Optionally, in the code annotation generation device provided in the embodiments of the present application, the sequence obtaining unit 302 comprises: a code parsing module configured to parse the target code into an abstract syntax tree by using a code parsing tool; a node traversal module configured to traverse the abstract syntax tree to identify a plurality of variable nodes and obtain variable dependency relationships between the plurality of variable nodes; and a data flow sequence generation module configured to generate a data flow sequence corresponding to the target code according to the variable dependency relationships.
[0078] Optionally, in the code annotation generation device provided in the embodiments of the present application, the sequence obtaining unit 302 comprises: a first obtaining module configured to perform data cleaning on the target code to obtain first code; a second obtaining module configured to decompose the first code into a token sequence and perform normalization processing on the token sequence to obtain second code; and a third obtaining module configured to add a preset mark to the second code to obtain the code sequence.
[0079] Optionally, in the code annotation generation device provided in the embodiments of the present application, the annotation generating unit 303 comprises: a first vector module configured to determine a second feature vector of the data flow sequence according to a code encoder in the code annotation generation model; a second vector module configured to determine a first feature vector of the data flow sequence according to a data flow encoder in the code annotation generation model; and a result generating module configured to fuse the first feature vector and the second feature vector and generate the target annotations according to a preset decoder in the code annotation generation model.
[0080] Optionally, in the code annotation generation apparatus provided in the embodiment of the present application, the apparatus comprises: a data set acquisition unit configured to acquire a training data set of code annotation, the training data set comprising a plurality of code samples and an annotation description of each code sample; a sample sequence unit configured to generate a corresponding data stream sequence sample according to the code sample, and extract a code sequence sample of the code sample; and a model training unit configured to train a preset recurrent neural network model according to the data stream sequence sample, the code sequence sample and the annotation description, and obtain a code annotation generation model.
[0081] Optionally, in the code annotation generation apparatus provided in the embodiment of the present application, the model training unit comprises: a first training module configured to train a first sub-model in the preset recurrent neural network model according to the data stream sequence sample and the annotation description, and obtain a trained first sub-model; an encoder migration module configured to acquire a data stream encoder in the trained first sub-model, and migrate the data stream encoder to a second sub-model of the preset recurrent neural network model, and obtain a migrated second sub-model; and a second training module configured to train the migrated second sub-model according to the data stream sequence sample, the code sequence sample and the annotation description, and obtain the code annotation generation model.
[0082] Optionally, in the code annotation generation apparatus provided in the embodiment of the present application, the apparatus further comprises: a gated recurrent module configured to determine a first encoder, a first decoder, a second encoder and a second decoder according to a gated recurrent unit before training the first sub-model in the preset recurrent neural network model according to the data stream sequence sample and the annotation description; a first determination module configured to determine the first sub-model according to the first encoder and the first decoder; and a second determination module configured to determine the second sub-model according to the second encoder and the second decoder.
[0083] It should be noted that the code receiving unit 301, the sequence acquiring unit 302 and the annotation generating unit 303 correspond to steps S201 to S203 in Embodiment 1, and the units have the same instances and application scenarios as the corresponding steps, but are not limited to the content disclosed in Embodiment 1. It should be noted that the above modules or units can be hardware components or software components stored in a memory (for example, the memory 104) and processed by one or more processors (for example, the processors 102a, 102b, …, 102n), and the above modules can also be a part of the apparatus and can run in the computer terminal 10 provided in Embodiment 1.
[0084] Embodiment 3
[0085] The embodiments of the present application can provide an electronic device, Figure 4 is a structural block diagram of an electronic device according to an embodiment of the present application. As shown in Figure 4 As shown, the electronic device may include: one or more ( Figure 4 (Only one is shown) processor 1002, memory 1004, memory controller, and peripheral interface, wherein the peripheral interface is connected to the radio frequency module, audio module and display.
[0086] The memory can be used to store software programs and modules, such as the program instructions / modules corresponding to the methods and apparatus in the embodiments of this application. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory, thereby implementing the above-described methods. The memory may include high-speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory may further include memory remotely located relative to the processor, and these remote memories can be connected to the terminal via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.
[0087] The processor can access information and applications stored in memory via a transmission device to perform the following steps: receiving target code to be annotated; generating a data stream sequence corresponding to the target code and obtaining the code sequence of the target code; inputting the data stream sequence and code sequence into the code annotation generation model, and using the code annotation generation model to output the target annotation corresponding to the target code.
[0088] The processor can also invoke information and applications stored in memory via a transmission device to perform the following steps: using a code parsing tool to parse the target code into an abstract syntax tree; traversing the abstract syntax tree to identify multiple variable nodes and obtain the variable dependencies between the multiple variable nodes; and generating a data stream sequence corresponding to the target code based on the variable dependencies.
[0089] The processor can also call the information and application stored in the memory through the transmission device to perform the following steps: clean the target code to obtain the first code; decompose the first code into a token sequence and normalize the token sequence to obtain the second code; add a preset mark to the second code to obtain the code sequence.
[0090] The processor can also call the information and application stored in the memory through the transmission device to perform the following steps: determine the second feature vector of the data stream sequence based on the code encoder in the code annotation generation model; determine the first feature vector of the data stream sequence based on the data stream encoder in the code annotation generation model; and fuse the first feature vector and the second feature vector based on the preset decoder in the code annotation generation model to generate the target annotation.
[0091] The processor can also access information and applications stored in the memory via a transmission device to perform the following steps: obtaining a training dataset of code annotations, which includes multiple code samples and annotation descriptions for each code sample; generating corresponding data stream sequence samples based on the code samples and extracting code sequence samples from the code samples; training a preset recurrent neural network model based on the data stream sequence samples, code sequence samples, and annotation descriptions to obtain a code annotation generation model.
[0092] The processor can also call the information and application stored in the memory through the transmission device to perform the following steps: train the first sub-model in the preset recurrent neural network model according to the data stream sequence samples and annotation descriptions to obtain the trained first sub-model; obtain the data stream encoder in the trained first sub-model and transfer the data stream encoder to the second sub-model of the preset recurrent neural network model to obtain the transferred second sub-model; train the transferred second sub-model according to the data stream sequence samples, code sequence samples and annotation descriptions to obtain the code annotation generation model.
[0093] The processor can also call the information and application stored in the memory through the transmission device to perform the following steps: determining the first encoder, the first decoder, the second encoder and the second decoder based on the gating loop unit; determining the first sub-model based on the first encoder and the first decoder; and determining the second sub-model based on the second encoder and the second decoder.
[0094] This application provides a scheme for generating code comments. It involves receiving the target code for which comments are to be generated; generating a data stream sequence corresponding to the target code and obtaining the code sequence of the target code; inputting the data stream sequence and the code sequence into a code comment generation model; and using the code comment generation model to output the target comments corresponding to the target code. This solves the technical problem of low accuracy in code comment generation in the prior art.
[0095] Those skilled in the art will understand that Figure 4 The structure shown is for illustrative purposes only. Electronic devices can also be smartphones, tablets, handheld computers, mobile internet devices (MIDs), PADs, and other terminal devices. Figure 4 This does not limit the structure of the aforementioned electronic device. For example, electronic devices may also include components that are more... Figure 4 The more or fewer components shown (such as network interfaces, display devices, etc.), or having the same Figure 4 The different configurations shown.
[0096] Those skilled in the art will understand that all or part of the steps in the various methods of the above embodiments can be implemented by a program instructing the hardware related to the terminal device. The program can be stored in a computer-readable storage medium, which may include: flash drive, read-only memory (ROM), random access memory (RAM), disk or optical disk, etc.
[0097] Example 4
[0098] Embodiments of this application also provide a storage medium. Optionally, in this embodiment, the storage medium can be used to store the program code executed by the code comment generation method provided in Embodiment 1.
[0099] Optionally, in this embodiment, the storage medium may be located in any computer terminal in a group of computer terminals in a computer network, or in any mobile terminal in a group of mobile terminals.
[0100] Optionally, in this embodiment, the computer-readable storage medium is configured to store program code for performing the following steps: receiving target code to be annotated; generating a data stream sequence corresponding to the target code and obtaining a code sequence of the target code; inputting the data stream sequence and the code sequence into a code annotation generation model, and using the code annotation generation model to output the target annotation corresponding to the target code.
[0101] Optionally, in this embodiment, the computer-readable storage medium is further configured to store program code for performing the following steps: parsing the target code into an abstract syntax tree using a code parsing tool; traversing the abstract syntax tree to identify multiple variable nodes and obtaining the variable dependencies between the multiple variable nodes; and generating a data stream sequence corresponding to the target code based on the variable dependencies.
[0102] Optionally, in this embodiment, the computer-readable storage medium is further configured to store program code for performing the following steps: data cleaning of the target code to obtain first code; decomposing the first code into a token sequence and normalizing the token sequence to obtain second code; adding a preset tag to the second code to obtain a code sequence.
[0103] Optionally, in this embodiment, the computer-readable storage medium is further configured to store program code for performing the following steps: determining a second feature vector of the data stream sequence based on the code encoder in the code annotation generation model; determining a first feature vector of the data stream sequence based on the data stream encoder in the code annotation generation model; and fusing the first feature vector and the second feature vector based on the preset decoder in the code annotation generation model to generate a target annotation.
[0104] Optionally, in this embodiment, the computer-readable storage medium is further configured to store program code for performing the following steps: obtaining a training dataset of code annotations, the training dataset including multiple code samples and annotation descriptions for each code sample; generating corresponding data stream sequence samples based on the code samples, and extracting code sequence samples from the code samples; training a preset recurrent neural network model based on the data stream sequence samples, code sequence samples, and annotation descriptions to obtain a code annotation generation model.
[0105] Optionally, in this embodiment, the computer-readable storage medium is further configured to store program code for performing the following steps: training a first sub-model in a preset recurrent neural network model based on data stream sequence samples and annotation descriptions to obtain a trained first sub-model; obtaining a data stream encoder in the trained first sub-model and transferring the data stream encoder to a second sub-model of the preset recurrent neural network model to obtain a transferred second sub-model; training the transferred second sub-model based on data stream sequence samples, code sequence samples, and annotation descriptions to obtain a code annotation generation model.
[0106] Optionally, in this embodiment, the computer-readable storage medium is further configured to store program code for performing the following steps: determining a first encoder, a first decoder, a second encoder, and a second decoder based on the gating loop unit; determining a first sub-model based on the first encoder and the first decoder; and determining a second sub-model based on the second encoder and the second decoder.
[0107] This application also provides a computer program product that, when executed on a data processing device, is adapted to perform method steps for generating code comments.
[0108] The sequence numbers of the embodiments in this application are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.
[0109] In the above embodiments of this application, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments.
[0110] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. The device embodiments described above are merely illustrative; for example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the displayed or discussed mutual coupling, direct coupling, or communication connection may be through some interfaces; the indirect coupling or communication connection between units or modules may be electrical or other forms.
[0111] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0112] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0113] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard drive, magnetic disk, or optical disk.
[0114] The above description is only a preferred embodiment of this application. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of this application, and these improvements and modifications should also be considered within the scope of protection of this application.< / e> < / s>
Claims
1. A method for generating code comments, characterized in that, include: Receive the target code to be annotated; Generate the data stream sequence corresponding to the target code, and obtain the code sequence of the target code; The data stream sequence and the code sequence are input into the code annotation generation model, and the code annotation generation model is used to output the target annotation corresponding to the target code.
2. The method according to claim 1, characterized in that, The data stream sequence corresponding to the target code includes: The target code is parsed into an abstract syntax tree using a code parsing tool; The abstract syntax tree is traversed to identify multiple variable nodes and obtain the variable dependencies between the multiple variable nodes; The data stream sequence corresponding to the target code is generated based on the variable dependencies.
3. The method according to claim 2, characterized in that, The code sequence for obtaining the target code includes: The target code is cleaned to obtain the first code; The first code is decomposed into a token sequence, and the token sequence is normalized to obtain the second code; Add a preset marker to the second code to obtain the code sequence.
4. The method according to claim 3, characterized in that, The target annotations output by the code annotation generation model for the target code include: The second feature vector of the data stream sequence is determined based on the code encoder in the code annotation generation model; The first feature vector of the data stream sequence is determined by the data stream encoder in the model based on the code comments. The first feature vector and the second feature vector are fused together using the preset decoder in the code annotation generation model to generate the target annotation.
5. The method according to claim 1, characterized in that, The training steps for the code annotation generation model include: Obtain a training dataset of code comments, the training dataset including multiple code samples and comment descriptions for each code sample; Generate a corresponding data stream sequence sample based on the code sample, and extract the code sequence sample from the code sample; The code annotation generation model is obtained by training a preset recurrent neural network model based on the data stream sequence sample, the code sequence sample, and the annotation description.
6. The method according to claim 5, characterized in that, The code annotation generation model is obtained by training a preset recurrent neural network model based on the data stream sequence samples, the code sequence samples, and the annotation descriptions, including: The first sub-model in the preset recurrent neural network model is trained based on the data stream sequence samples and the annotation description to obtain the trained first sub-model. Obtain the data stream encoder in the first sub-model after training, and transfer the data stream encoder to the second sub-model of the preset recurrent neural network model to obtain the transferred second sub-model; The transferred second sub-model is trained based on the data stream sequence samples, the code sequence samples, and the annotation descriptions to obtain the code annotation generation model.
7. The method according to claim 6, characterized in that, Before training the first sub-model in the preset recurrent neural network model based on the data stream sequence samples and the annotation description, the method further includes: The first encoder, first decoder, second encoder, and second decoder are determined based on the gated loop unit; The first sub-model is determined based on the first encoder and the first decoder; The second sub-model is determined based on the second encoder and the second decoder.
8. A code annotation generation apparatus, characterized in that, include: The code receiving unit is used to receive the target code to be annotated. A sequence acquisition unit is used to generate a data stream sequence corresponding to the target code and acquire the code sequence of the target code; An annotation generation unit is used to input the data stream sequence and the code sequence into the code annotation generation model, and use the code annotation generation model to output the target annotation corresponding to the target code.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a stored executable program, wherein, when the executable program is executed, it controls the device on which the computer-readable storage medium is located to perform the code annotation generation method according to any one of claims 1 to 7.
10. An electronic device, characterized in that, include: Memory, which stores executable programs; A processor for running the program, wherein the program, when running, executes the code comment generation method according to any one of claims 1 to 7.
11. A computer program product comprising computer instructions, characterized in that, When the computer instructions are executed by the processor, they implement the steps of the code annotation generation method according to any one of claims 1 to 7.