Method, device, computer equipment and medium for generating annotation information
By combining the generation model and the retrieval model, and using the code abstract syntax tree path and the correlation model, the problem of insufficient accuracy in annotation information generation in the existing technology is solved, and high-quality automatic annotation information generation is achieved, which is suitable for code development.
Patent Information
- Application Number
- CN202010670003.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-07-13
- Publication Date
- 2024-08-23
- Estimated Expiration
- 2040-07-13
AI Technical Summary
The existing technology lacks a method to automatically generate high-quality annotation information. The statements generated by the generation model have poor readability. The retrieval model ignores the code context and cannot accurately generate annotation information without code changes, resulting in insufficient accuracy in automated annotation information generation.
Combining the generation model and the retrieval model, by obtaining code changes, inputting the generation model and the retrieval model respectively, generating candidate annotation information, and selecting the final annotation information based on the relevance, using the code abstract syntax tree path and the relevance model to improve the accuracy of the annotation information performance and readability.
It improves the accuracy of automatically generating annotation information for code changes, avoids the shortcomings of a single model, and achieves more accurate and readable annotation information generation, which is suitable for actual code development.
Smart Images

Figure CN113934450B
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computers, and more specifically, to a method, apparatus, computer device, and medium for generating annotation information. Background Art
[0002] As software scale and complexity increase, code hosting systems (such as Codeup, GitHub, TortoiseSVN) are becoming more and more important in the software development lifecycle. They provide version control services for users. During the software update and iteration process, developers are required to submit comments for each change to record the intention of the change. Collaborative developers can understand the current project progress and code changes by simply browsing these submission information, rather than spending a lot of energy directly reading the source code. Therefore, high-quality submission comment information is crucial for developers to quickly understand the current project evolution. At present, the submission of high-quality comment information in the industry is mainly sent manually.
[0003] There is no product in the industry that can automatically generate annotation information for code changes. Academically, generative models and retrieval models have been proposed to automatically generate annotation information. The sentences generated by the generative model are poorly readable, may be incomplete sentences or incoherent, and have low accuracy. The retrieval model ignores the context of the code itself, and the generated results may be irrelevant. In addition, accurate results cannot be generated for code changes that have not occurred. Therefore, a method for automatically generating annotation information that can further improve accuracy so that it can be used in actual code development is needed. Summary of the invention
[0004] In view of this, the present disclosure aims to further improve the accuracy of automatically generating annotation information for code changes.
[0005] According to one aspect of the present disclosure, a method for generating annotation information for a code change is provided, comprising:
[0006] Get code changes;
[0007] Inputting the code changes into a generation model and a retrieval model respectively, and the generation model and the retrieval model respectively output candidate annotation information;
[0008] The generated annotation information is determined from the candidate annotation information respectively output by the generation model and the retrieval model.
[0009] Optionally, determining the generated annotation information from the candidate annotation information respectively output by the generation model and the retrieval model comprises:
[0010] Determining the relevance between the candidate annotation information respectively output by the generation model and the retrieval model and the code change;
[0011] The candidate annotation information is selected according to the association degree as the generated annotation information.
[0012] Optionally, the inputting the code change into the generation model and the retrieval model respectively comprises:
[0013] Generate a code abstract syntax tree AST path of the code change and input it into the generation model;
[0014] The code changes are input into the search model.
[0015] Optionally, the generation model is pre-trained in the following manner: for a code change snippet sample in a code change snippet sample set, an AST path of the code change snippet sample is generated, the generated AST path is input into the generation model, annotation information is output by the generation model, and the annotation information output by the generation model is compared with annotation information pre-corresponding to the code change snippet sample; if the ratio of consistent annotation information in the code change snippet sample set is greater than a predetermined ratio, the generation model training is successful.
[0016] Optionally, the retrieval model includes a code change and annotation information correspondence library and a code change comparison sub-model, the code change and annotation information correspondence library includes a pre-set correspondence between code changes and annotation information, the code change comparison sub-model determines the similarity between the input code changes and the code changes in the annotation information correspondence library, and determines the annotation information from the annotation information correspondence library based on the similarity as candidate annotation information output by the retrieval model.
[0017] Optionally, the determination of the correlation between the candidate annotation information respectively output by the generation model and the retrieval model and the code change is achieved by a correlation model, wherein the correlation model is pre-generated in the following manner: for a code change and annotation information sample pair in a set of code change and annotation information sample pairs, the code change and annotation information sample pair is input into the correlation model, the correlation model outputs the correlation of the code change and annotation information sample pair, and compares it with a predetermined correlation of the code change and annotation information sample pair; if the ratio of consistent correlations in the set of code change and annotation information sample pairs is greater than a predetermined ratio, the correlation model is successfully trained.
[0018] Optionally, the association model is a convolutional neural network.
[0019] Optionally, the selecting, according to the degree of association, candidate annotation information as the generated annotation information includes: selecting the candidate annotation information with the greatest degree of association as the generated annotation information.
[0020] Optionally, selecting candidate annotation information according to the relevance as the generated annotation information includes: displaying the candidate annotation information in order according to the relevance, and using the candidate annotation information selected by the user as the generated annotation information.
[0021] Optionally, selecting candidate annotation information according to the association degree as the generated annotation information includes:
[0022] Receive a first portion of annotation information input by a user;
[0023] causing the generation model and the retrieval model to re-output candidate annotation information starting with the first part according to the code change;
[0024] Determining the relevance of the candidate annotation information re-output by the generation model and the candidate annotation information re-output by the retrieval model to the code change;
[0025] The re-output candidate annotation information is selected according to the association degree as the generated annotation information.
[0026] Optionally, according to one aspect of the present disclosure, a method for generating annotation information for a code change is provided, comprising:
[0027] Display the code before and after the change;
[0028] Determine and display the code change based on the code before and after the change;
[0029] Display candidate annotation information obtained by the generation model and the retrieval model for the code change respectively;
[0030] Based on the user's selection of the candidate annotation information, the generated annotation information is determined.
[0031] Optionally, the displaying of candidate annotation information obtained by the generation model and the retrieval model for the code change includes:
[0032] The candidate annotation information is displayed in order according to the relevance between the candidate annotation information and the code change.
[0033] According to one aspect of the present disclosure, there is provided a device for generating annotation information for a code change, comprising:
[0034] A code change acquisition unit, used for acquiring code changes;
[0035] A candidate annotation information generating unit, used for inputting the code change into a generation model and a retrieval model respectively, and the generation model and the retrieval model respectively output candidate annotation information;
[0036] The annotation information determining unit is used to determine the generated annotation information from the candidate annotation information outputted by the generation model and the retrieval model respectively.
[0037] Optionally, the annotation information determining unit is further used to:
[0038] Determining the relevance between the candidate annotation information respectively output by the generation model and the retrieval model and the code change;
[0039] The candidate annotation information is selected according to the association degree as the generated annotation information.
[0040] Optionally, the candidate annotation information generation unit is further used to generate a code abstract syntax tree AST path of the code change, input the code change into the generation model, and input the code change into the retrieval model.
[0041] Optionally, the generation model is pre-trained in the following manner: for a code change snippet sample in a code change snippet sample set, an AST path of the code change snippet sample is generated, the generated AST path is input into the generation model, annotation information is output by the generation model, and the annotation information output by the generation model is compared with annotation information pre-corresponding to the code change snippet sample; if the ratio of consistent annotation information in the code change snippet sample set is greater than a predetermined ratio, the generation model training is successful.
[0042] Optionally, the retrieval model includes a code change and annotation information correspondence library and a code change comparison sub-model, the code change and annotation information correspondence library includes a pre-set correspondence between code changes and annotation information, the code change comparison sub-model determines the similarity between the input code changes and the code changes in the annotation information correspondence library, and determines the annotation information from the annotation information correspondence library based on the similarity as candidate annotation information output by the retrieval model.
[0043] Optionally, the association determination unit is implemented by an association model, wherein the association model is pre-generated in the following manner: for a code change and annotation information sample pair in a set of code change and annotation information sample pairs, the code change and annotation information sample pair is input into the association model, the association model outputs the association of the code change and annotation information sample pair, and compares it with a predetermined association of the code change and annotation information sample pair; if the ratio of consistent associations in the set of code change and annotation information sample pairs is greater than the predetermined ratio, the association model is trained successfully.
[0044] Optionally, the association model is a convolutional neural network.
[0045] Optionally, the annotation information generating unit is further configured to: use the candidate annotation information with the greatest correlation as the generated annotation information.
[0046] Optionally, the annotation information generating unit is further used to: sort and display the candidate annotation information according to the relevance, and use the candidate annotation information selected by the user as the generated annotation information.
[0047] Optionally, the annotation information generating unit is further used to:
[0048] Receive a first portion of annotation information input by a user;
[0049] causing the generation model and the retrieval model to re-output candidate annotation information starting with the first part according to the code change;
[0050] Determining the relevance of the candidate annotation information re-output by the generation model and the candidate annotation information re-output by the retrieval model to the code change;
[0051] The re-output candidate annotation information is selected according to the association degree as the generated annotation information.
[0052] According to one aspect of the present disclosure, there is provided a device for generating annotation information for a code change, comprising:
[0053] A pre- and post-change code display unit, used to display the pre- and post-change codes;
[0054] A code change display unit, used to determine and display the code change based on the code before the change and the code after the change;
[0055] A candidate annotation information display unit, used to display candidate annotation information obtained by the generation model and the retrieval model for the code change respectively;
[0056] The annotation information determining unit is used to determine the generated annotation information based on the user's selection of the candidate annotation information.
[0057] Optionally, the candidate comment information display unit is further configured to: sort and display the candidate comment information according to the degree of association between the candidate comment information and the code change.
[0058] According to one aspect of the present disclosure, a computer device is provided, including: a memory for storing computer executable code; and a processor for executing the computer executable code to implement the method for generating annotation information for code changes as described above.
[0059] According to one aspect of the present disclosure, a computer-readable medium is provided, comprising a computer-executable code, wherein when the computer-executable code is executed by a processor, the method for generating annotation information for a code change as described above is implemented.
[0060] The disclosed embodiment adopts both a generation model and a retrieval model, and determines the generated annotation information from the candidate annotation information respectively output by the generation model and the retrieval model, rather than determining the generated annotation information based on the output result of only one of the models. If a certain candidate annotation information generated by the generation model is inaccurate, or a certain candidate annotation information generated by the retrieval model is inaccurate, it may not be selected as the generated annotation information during comprehensive selection. In this way, the advantages of the generation model and the retrieval model are combined, avoiding the shortcomings of poor readability of sentences generated by a single generation model and the inability of a single retrieval model to be used for code changes that have not occurred, and improving the accuracy of automatically generating annotation information for code changes. BRIEF DESCRIPTION OF THE DRAWINGS
[0061] The above and other objects, features and advantages of the present disclosure will become more apparent through the description of the embodiments of the present disclosure with reference to the following drawings, in which:
[0062] Figure 1 The hardware structure of a computer device used in the method for generating annotation information for code changes according to an embodiment of the present disclosure is shown;
[0063] Figure 2A -B shows an interface change diagram for generating annotation information for code changes according to an embodiment of the present disclosure;
[0064] Figure 3 A schematic diagram showing a process of generating annotation information for code changes according to an embodiment of the present disclosure is shown;
[0065] Figure 4 A flowchart of the training process of each model or module required for generating annotation information for code changes according to an embodiment of the present disclosure is shown;
[0066] Figure 5 A flowchart of a method for generating annotation information for code changes according to an embodiment of the present disclosure is shown;
[0067] Figure 6 A flowchart of a method for generating annotation information for code changes according to an embodiment of the present disclosure is shown;
[0068] Figure 7 A block diagram showing an apparatus for generating annotation information for code changes according to an embodiment of the present disclosure;
[0069] Figure 8A block diagram of an apparatus for generating annotation information for code changes according to an embodiment of the present disclosure is shown. DETAILED DESCRIPTION
[0070] The present disclosure is described below based on embodiments, but the present disclosure is not limited to these embodiments. In the detailed description of the present disclosure below, some specific details are described in detail. It is possible for a person skilled in the art to fully understand the present disclosure without the description of these details. In order to avoid confusing the essence of the present disclosure, well-known methods, processes, and procedures are not described in detail. In addition, the drawings are not necessarily drawn to scale.
[0071] According to an embodiment of the present disclosure, a method for generating annotation information for code changes may include: Figure 1 The computer device 800 is implemented. Figure 1 800 according to an embodiment of the present disclosure is described below. Figure 1 The computer device 800 shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure. It can be a general computing device or a dedicated device dedicated to generating annotation information for code changes. It can be in the form of a desktop computer, a laptop computer, a mobile device, a personal digital assistant (PDA), etc., or it can be a server. It can also be a cluster of multiple terminal devices or multiple server devices, such as a collection of multiple devices on a cloud platform.
[0072] Figure 1 The computer device 800 is a single general-purpose computing device. The components of the computer device 800 may include, but are not limited to: the at least one processing unit 810, the at least one storage unit 820, and a bus 830 connecting different system components (including the storage unit 820 and the processing unit 810).
[0073] The storage unit stores program codes, which can be executed by the processing unit 810 so that the processing unit 810 executes each step in the program.
[0074] The storage unit 820 may include a readable medium in the form of a volatile storage unit, such as a random access memory unit (RAM) 8201 and / or a cache memory unit 8202 , and may further include a read-only memory unit (ROM) 8203 .
[0075] The storage unit 820 may also include a program / utility 8204 having a set (at least one) of program modules, wherein one of the program models is an integrated development environment (IDE) component 8205. The integrated development environment component 8205 is an application for providing a program development environment, generally including tools such as a code editor, a compiler, a debugger, and a graphical user interface. It is an integrated development software service set that integrates code writing functions, analysis functions, compilation functions, debugging functions, etc. The generation of annotation information for code changes in the disclosed embodiment is a task in code development. In the code update iteration process of code development, developers are required to submit annotation information for each change to record the intention of the change. Collaborative developers can understand the current project progress and code changes by simply browsing these submission information, rather than spending a lot of energy directly reading the source code. The disclosed embodiment integrates the function of automatically generating annotation information in the IDE component 8205. When the IDE component 8205 is called and executed by the processing unit 810, the purpose of not manually generating annotation information is achieved.
[0076] The Git client 8206 is a component in the IDE component 8205, which is a distributed version control system used to record code changes during the software development process. When a user develops software, if the code is changed, the Git client 8206 will automatically identify and record it. The Git client 8206 of the prior art can automatically identify changes but does not generate comment information. In one embodiment of the present disclosure, the function of generating comment information for code changes can be integrated into the Git client 8206, so that the Git client 8206 can not only automatically identify code changes, but also generate comment information. In one embodiment of the present disclosure, the function of generating comment information for code changes can be included as a part of the IDE component 8205, which is not a part of the Git client 8206 integrated in the IDE component 8205.
[0077] Bus 830 may represent one or more of several types of bus structures, including a memory unit bus or memory unit controller, a peripheral bus, an accelerated graphics port, a processing unit, or a local bus using any of a variety of bus architectures.
[0078] The computer device 800 may also communicate with one or more external devices 700 (e.g., keyboards, pointing devices, Bluetooth devices, etc.), may also communicate with one or more devices that enable a user to interact with the computer device 800, and / or communicate with any device that enables the computer device 800 to communicate with one or more other computing devices (e.g., routers, modems, etc.). Such communication may be performed via an input / output (I / O) interface 850. Furthermore, the computer device 800 may also communicate with one or more networks (e.g., local area networks (LANs), wide area networks (WANs), and / or public networks, such as the Internet) via a network adapter 860. As shown, the network adapter 860 communicates with other modules of the computer device 800 via a bus 830. It should be understood that, although not shown in the figure, other hardware and / or software modules may be used in conjunction with the computer device 800, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.
[0079] Figure 2A -B shows an interface change diagram for generating annotation information for code changes according to an embodiment of the present disclosure, which shows the interface changes on the display of the computer device 800 when executing the method for generating annotation information for code changes according to an embodiment of the present disclosure.
[0080] Figure 2A The interface shows the original code frame 199 and the changed code frame 198 side by side. The original code frame 199 displays the original code. The changed code frame 198 displays the changed code. Figure 1 The Git client 8206 will automatically identify the difference between the changed code and the original code, that is, the code change 197. Figure 2A As shown, code changes 197 include code line deletions and code line additions. Figure 2A In the figure, code line deletion is represented by a dotted box, and code line addition is represented by a solid box.
[0081] According to the embodiment of the present disclosure, the computer device 800 (specifically the Git client 8206 or the non-Git client 8206 part integrated in the IDE component 8205) generates candidate annotation information 196 according to the identified code changes, allowing the user to select, such as Figure 2A The candidate annotation information is presented in the candidate annotation information box 195. Figure 2A In the example, the user selects the second candidate annotation information and submits it. In this way, the submitted annotation information is the second candidate annotation information.
[0082] exist Figure 2B In the annotation information box 194 , the second candidate annotation information submitted is displayed as the final annotation information 106 .
[0083] In software code development, the code hosting system provides version control services for users. In the process of software update iteration, developers are required to submit annotation information for each change to record the intention of the change. Collaborative developers can browse the submitted annotation information to understand the development progress, instead of spending a lot of energy directly reading the source code. At present, annotation information is mainly filled in manually, which wastes a lot of time for development users. There is no product in the industry that can automatically generate annotation information.
[0084] Academically, generative models and retrieval models have been proposed to automatically generate annotation information, but they have not yet been applied to production because they have not yet achieved the accuracy required in practice.
[0085] A generative model is a neural network model, such as neural network machine translation (NMT). It represents code changes as a sequence and annotation information as another sequence. Therefore, the generative model becomes a model that translates from one sequence to another. It uses a large number of code change samples for training, and each code change sample has a corresponding annotation information label. The code change sample is input into the neural network model, and the neural network model outputs the determined annotation information, which is compared with the corresponding annotation information label. If the ratio of the determined annotation information and the annotation information label in all code change samples exceeds the preset ratio, the neural network model is considered to be successfully trained. If it does not exceed the preset ratio, the neural network model is adjusted until the ratio of the determined annotation information and the annotation information label in all code change samples exceeds the preset ratio. After the model is successfully trained, the code changes are input into the trained generative model, and the generative model can obtain the annotation information. Its disadvantage is that the generative model will tend to output high-frequency words. In addition, the directly generated sentences are less readable and may not be complete sentences or sentences are not coherent. In addition, common generative models use flat word sequences as input, ignoring the grammatical and semantic structure of the code snippet itself, resulting in low accuracy of the generated intent.
[0086] The retrieval model is a model that retrieves the annotation information corresponding to similar code change fragments in history as the currently submitted annotation information. It requires a large number of information pairs consisting of historical code changes and annotation information as support. On the other hand, it requires a relatively accurate code change similarity comparison method. With the help of this comparison method, the code change with the greatest similarity to the current code change in the history is found, and then the information pair consisting of the code change and the annotation information is searched to obtain the annotation information. Since the retrieval model searches based on the similarity of code changes, it mainly relies on the word overlap between the current code change and the historical code changes, ignoring the context of the code itself, which may lead to completely irrelevant results. In addition, accurate annotation information cannot be generated for code changes that have never appeared.
[0087] Due to the above reasons, the generation model and the retrieval model each have their own advantages and disadvantages, and the accuracy is not enough to be applied to actual production. Therefore, a method for automatically generating annotation information that can further improve the accuracy so that it can be used in actual code development is needed.
[0088] According to one embodiment of the present disclosure, a method for generating annotation information for code changes is provided. The method is executed by a Git client 8206 in a computer device 800 or a portion of a non-Git client 8206 integrated in an IDE component 8205. Figure 5 As shown, the method includes:
[0089] Step 210: Obtain code changes;
[0090] Step 220: input the code change into the generation model and the retrieval model respectively, and the generation model and the retrieval model respectively output candidate annotation information;
[0091] Step 230: Determine the generated annotation information from the candidate annotation information outputted by the generation model and the retrieval model respectively.
[0092] These steps are described in detail below.
[0093] In step 210, code changes are obtained.
[0094] like Figure 2A As shown, when the user changes the written code on the interface of the computer device, the Git client 8206 will automatically recognize and record the code changes 197.
[0095] exist Figure 3 In the overall schematic diagram of the embodiment of the present disclosure, the Git client 8206 automatically obtains code changes 101 from the development code 107 and sends the obtained code changes to the generation model 102 and the retrieval model 103 for parallel processing.
[0096] In step 220, the code changes are input into the generation model and the retrieval model respectively, and the generation model and the retrieval model respectively output candidate annotation information.
[0097] As described above, the generation model and the retrieval model are two models that generate annotation information based on code changes. In the prior art, the two are used separately. The code changes are input into the generation model, and the generation model outputs the annotation information with the highest probability, or the code changes are input into the retrieval model, and the retrieval model outputs the annotation information with the highest probability. Since the generation model has the disadvantages that the generated sentences are less readable, may not be complete sentences or the sentences are not fluent, and the retrieval model has the disadvantages of ignoring the context of the code itself and being unable to generate accurate annotation information for code changes that have not occurred, the embodiment of the present disclosure cleverly combines the two models together, allowing each to generate some candidate annotation information, for example, the annotation information selected by the top N probabilities of each is used as candidate annotation information, and then through the subsequent steps 230 and 240, the correlation between these candidate annotation information and the code changes is compared, and the final annotation information is selected according to the correlation, which cleverly avoids the shortcomings of the generation model and the retrieval model and gives full play to their respective strengths.
[0098] The candidate annotation information refers to the annotation information from which the finally generated annotation information is selected, and the finally generated annotation information is selected from the candidate annotation information.
[0099] In one embodiment, step 220 includes: generating a code abstract syntax tree (AST) path of the code change and inputting it into the generation model; and inputting the code change into the retrieval model.
[0100] AST can express the semantic structure of a code snippet and is an abstract representation of the grammatical structure of the source code. It represents the grammatical structure of a programming language in a tree-like form, and each node on the tree represents a structure in the source code. The reason why the grammar is "abstract" is that the grammar here does not represent every detail that appears in the real grammar. For example, nested parentheses are implied in the tree structure and are not presented in the form of nodes. The AST path is the structure corresponding to the changed code part in the entire tree structure.
[0101] AST paths can better reflect the meaning of code structure than source code and can express longer source code. Therefore, compared with directly inputting code changes into the generation model, generating the AST path of the code changes and inputting the generation model is conducive to expressing the structural meaning of code changes, improving the accuracy of the generation model generation structure, and expressing longer code change sequences.
[0102] In addition, in one embodiment, after the AST path of the code change is generated, the AST path can be standardized, and then the standardized AST path is input into the generation model. The role of standardization is that the AST path of the code change is a tree structure, which belongs to a multi-dimensional structure. Converting it into a flat data structure is conducive to improving coding efficiency and increasing the information content of the unit code.
[0103] In one embodiment, the generation model 102 can be pre-trained in the following manner. First, Figure 4 As shown, a code change snippet sample set 109 containing a large number of code change snippet samples is set, wherein each code change snippet sample corresponds to an annotation information in advance (for example, given by an expert labeling method). For the code change snippet sample in the code change snippet sample set 109, the preprocessing module 105 generates the AST path of the code change snippet sample, and the generated AST path is input into the generation model 102. In addition, the preprocessing module 105 can perform truncation processing on the annotation information corresponding to the code change snippet sample, for example, only retain the first sentence of the annotation information and provide it to the generation model 102. The first sentence or the preceding sentences of the annotation information basically covers most of the meaning of the annotation information. Therefore, as long as the first sentence or the preceding sentences of the annotation information are sent to the generation model 102, it has sufficient discrimination, achieving the purpose of achieving a better model training effect with as little information as possible, and improving the model training efficiency. The generation model 102 outputs the annotation information, and the annotation information output by the generation model 102 is compared with the annotation information corresponding to the code change snippet sample sent by the preprocessing module 108 in advance. If the ratio of the consistent annotation information in the code change snippet sample set is greater than the predetermined ratio, the generation model training is successful. If not, the internal parameters of the generation model are adjusted until the ratio of consistent annotation information in the code change snippet sample set is greater than a predetermined ratio.
[0104] In one embodiment, the retrieval model 103 includes a code change and annotation information correspondence library and a code change comparison submodel (not shown). The code change and annotation information correspondence library includes a pre-set correspondence between code changes and annotation information. For example, all code changes recorded when code changes occurred in history are recorded in the code change and annotation information correspondence library in correspondence with the annotation information submitted when the code changes occurred. The code change comparison submodel is used to determine the similarity between the input code change and the code change in the annotation information correspondence library. When a code change is received from the preprocessing module 105, the code change comparison submodel calculates the similarity between the code change and all code changes in the code change and annotation information correspondence library, and determines the annotation information from the annotation information correspondence library based on the similarity, for example, the annotation information corresponding to the code change with the largest similarity or the top N similarities (N is a positive integer greater than or equal to 2) is used as the candidate annotation information output by the retrieval model 103.
[0105] The code change comparison submodel can be trained in advance in the following manner: a set containing a large number of code change sample pairs is set in advance, wherein each code change sample pair includes two code change samples, and labels of their similarities are attached in advance by experts. Each code change sample pair is input into the code change comparison submodel, and the similarity determined by the code change comparison submodel output is compared with the similarity label attached in advance. If the ratio of the similarity output by the code change comparison submodel in the set of code change sample pairs and the similarity label is consistent exceeds a predetermined ratio, it is considered that the code change comparison submodel is trained successfully. If it does not exceed the predetermined ratio, the parameters in the code change comparison submodel will be adjusted until the ratio of the similarity output by the code change comparison submodel in the set of code change sample pairs and the similarity label is consistent exceeds a predetermined ratio. After the code change comparison submodel is successfully trained, any two code changes are input into the submodel, and the submodel can output the determined similarity of the two code changes.
[0106] The generation model 102 and the retrieval model 103 do not necessarily generate one candidate annotation information each, but may generate multiple candidate annotation information, for example, N, where N is a positive integer greater than or equal to 2. For the generation model 102, in the process of outputting the annotation information, it actually gives confidence probabilities to many possible annotation information, and generally selects the one with the largest confidence probability as the output result. In the embodiment of the present disclosure, if the generation model 102 generates one candidate annotation information, the candidate annotation information with the largest confidence probability is selected. If the generation model 102 generates N candidate annotation information, the candidate annotation information with the top N confidence probabilities is selected. If the retrieval model generates one candidate annotation information, it means that only the code change with the largest similarity determined by the code change comparison submodel is selected, and its corresponding annotation information is found in the code change and annotation information correspondence library. If the retrieval model generates N (N is a positive integer greater than or equal to 2) candidate annotation information, it means that the code change with the top N similarities determined by the code change comparison submodel is selected, and its corresponding annotation information is found in the code change and annotation information correspondence library.
[0107] In step 230, generated annotation information is determined from the candidate annotation information outputted by the generation model and the retrieval model respectively.
[0108] In one embodiment, step 230 includes: determining the degree of association between the candidate annotation information outputted by the generation model and the retrieval model respectively and the code change; and selecting the candidate annotation information as the generated annotation information according to the degree of association.
[0109] The relevance is the degree to which the candidate annotation information is associated with the code change.
[0110] In one embodiment, determining the association between the candidate annotation information outputted by the generation model and the retrieval model and the code change is implemented by an association model, wherein the association model is pre-generated in the following manner: a code change and annotation information sample pair set consisting of a large number of code change and annotation information sample pairs is pre-set, and an expert pre-assigns an association label to each of the large number of code change and annotation information sample pairs in the set. For the code change and annotation information sample pairs in the code change and annotation information sample pair set, the code change and annotation information sample pairs are input into the association model, and the association model outputs the association of the code change and annotation information sample pair, which is compared with its association label. If the ratio of the output association to the association label is greater than a predetermined ratio, the association model is successfully trained. If the ratio of the output association to the association label is not greater than a predetermined ratio, the parameters in the association model are adjusted until the ratio of the output association to the association label is greater than a predetermined ratio.
[0111] In one embodiment, the relevance model is a convolutional neural network (CNN), which is a deep learning model that mines high-level abstract representations. The relevance model uses CNN, which evaluates the relevance between the candidate information output from the generation model 102 and the retrieval model 103 and the code change, and outputs it to the candidate annotation information for selection as needed, thereby improving the accuracy of annotation information generation.
[0112] In one embodiment, the candidate annotation information output by the generation model and the candidate annotation information output by the retrieval model can be first filtered according to the ratio of the frequency of occurrence of the word in the document and the frequency of occurrence in the corpus (TF-IDF, Term Frequency-inverse Document Frequency), and the filtering results are input into the relevance model to determine the relevance.
[0113] TF-IDF is a statistical analysis method for keywords, used to evaluate the importance of a word to a document set or a corpus. The importance of a word is proportional to the number of times it appears in the document and inversely proportional to the number of times it appears in the corpus. This calculation method can effectively avoid the influence of common words on keywords and improve the relevance between keywords and documents.
[0114] When filtering, the frequency of occurrence of each word in the candidate annotation information output by the generation model and the candidate annotation information output by the retrieval model in the document and the frequency of occurrence in the corpus can be calculated, and the two frequencies can be divided, and the words whose division result is less than the predetermined ratio can be filtered out, and the filtering result can be input into the relevance model. Since TF-IDF eliminates the influence of common words on keywords, the accuracy of the generated annotation information is improved.
[0115] When training the relevance model, for the code change and annotation information sample pairs in the code change and annotation information sample pair set, the annotation information samples of the code change and annotation information sample pairs can be subjected to TF-IDF to remove the words whose frequency of appearance in the document and frequency of appearance in the corpus are lower than a predetermined threshold, and input into the relevance model, and the relevance of the code change and annotation information sample pairs is output by the relevance model, and compared with its relevance label, if the ratio of the output relevance to the relevance label is greater than the predetermined ratio, the training of the relevance model is successful. If the ratio of the output relevance to the relevance label is not greater than the predetermined ratio, the parameters in the relevance model are adjusted until the ratio of the output relevance to the relevance label is greater than the predetermined ratio.
[0116] In one embodiment, selecting candidate annotation information according to the relevance as the generated annotation information includes: selecting the candidate annotation information with the greatest relevance as the generated annotation information. If the generation model 102 outputs N candidate annotation information, and the retrieval model 103 outputs N candidate annotation information, where N is a positive integer greater than or equal to 2, the relevance of each of the 2N candidate annotation information with the code change can be calculated, and the greatest one among them can be used as the generated annotation information.
[0117] In another embodiment, selecting candidate annotation information as generated annotation information according to the relevance includes: sorting and displaying the candidate annotation information according to the relevance, and using the candidate annotation information selected by the user as the generated annotation information. If the generation model 102 outputs N candidate annotation information, and the retrieval model 103 outputs N candidate annotation information, where N is a positive integer greater than or equal to 2, the relevance of each of the 2N candidate annotation information with the code change can be calculated, and the 2N candidate annotation information can be displayed in descending order of relevance, such as Figure 2A As shown, the candidate annotation information selected by the user is used as the generated annotation information, such as Figure 2B shown. Figure 3 Module 104 for sorting candidate annotation information generated by the generation model 102 and candidate annotation information generated by the retrieval model 103 is shown.
[0118] In one embodiment, if the user is not satisfied with the displayed candidate annotation information, the user can input the beginning part of the candidate annotation information that he / she wants, such as the first word or the first letter of the first word, so that the generation model 102 and the retrieval model 103 can regenerate candidate annotation information that meets the beginning part according to the beginning part. That is, in the generation of candidate annotation information, the pre-manual input 105 is incorporated as a sorting precondition to achieve the effect of automatic completion of input information and improve the usability of the results. When the user does not have the pre-manual input 105, the result list is directly output to the user for selection. When the user has the pre-manual input 105, the user input is used as the beginning part of the candidate annotation information to regenerate the candidate annotation information by the generation model 102 and the retrieval model 103, re-evaluate the correlation between each candidate annotation information and the code change, sort the candidate annotation information according to the correlation 104, and let the user choose to generate the final annotation information 106.
[0119] In this embodiment, the computer device 800 receives the first part of the annotation information input by the user. The generation model 102 and the retrieval model 103 re-output the candidate annotation information starting with the first part according to the code change. For example, the first word of the annotation information desired by the user is "insert". At this time, the generation model 102 will find several candidate annotation information with the highest confidence probability among those annotation information starting with "insert" and re-output them to the association model. The association model will determine the degree of association between the candidate annotation information re-output by the generation model and the candidate annotation information re-output by the retrieval model and the code change, and select the re-output candidate annotation information according to the degree of association (for example, select the candidate annotation information with the highest degree of association or sort the candidate annotation information from high to low according to the degree of association and let the user choose) as the generated annotation information.
[0120] In this embodiment, on the basis of recommending candidate annotation information, the ability to submit annotation completion is added. When there are relatively few data samples, completion may not be used, and when there are enough data samples, completion may be considered, thereby optimizing the completion effect and further improving the result availability.
[0121] The disclosed embodiment uses both the generation model and the retrieval model to fuse and sort candidate annotation information, which can improve the problem of low readability of the generation model results and the inability of the retrieval model to handle new changes. At the same time, by using the AST path as input, the problem of the traditional generation model losing the structural meaning of the code can be solved, and a longer code change sequence can be expressed.
[0122] According to another embodiment of the present disclosure, Figure 6 As shown, a method for generating annotation information for code changes is also provided, including:
[0123] Step 310: Display the code before and after the change;
[0124] Step 320: determine and display the code change based on the code before the change and the code after the change;
[0125] Step 330, displaying candidate annotation information obtained by the generation model and the retrieval model for the code change respectively;
[0126] Step 340: Determine the generated annotation information based on the user's selection of the candidate annotation information.
[0127] In step 310, when the user needs to change the code, he / she makes modifications based on the code before the change displayed on the interface. Figure 2A As shown, the Git client 8206 will automatically record the changed code, display the pre-changed code in the pre-changed code box 199, and display the post-changed code in the post-changed code box 198.
[0128] In step 320, if Figure 2A As shown, the Git client 8206 automatically identifies the difference between the changed code and the original code, that is, code change 197, and displays code change 197. Code change 197 includes code line deletion and code line addition. Figure 2A In the figure, code line deletion is represented by a dotted box, and code line addition is represented by a solid box.
[0129] In step 330, similar to step 220, candidate annotation information 196 is obtained for the code change 197 by the generation model and the retrieval model, and displayed, as shown in FIG. Figure 2A shown.
[0130] In step 340, the user selects the finally generated annotation information 106 by checking the check box next to the displayed candidate annotation information 196. The finally generated annotation information 106 is as follows: Figure 2B shown.
[0131] In one embodiment, step 330 includes: sorting and displaying the candidate annotation information according to the relevance between the candidate annotation information and the code change. The process of determining the relevance has been combined with Figure 5 This is discussed in detail in the description of , so I will not repeat it here.
[0132] Steps 310-340 are only described from the perspective of the interface. The implementation details are in Figure 5 Steps 210-230 have been described in detail in the embodiment, and will not be described again for the sake of space saving.
[0133] Commercial value of this disclosure
[0134] The disclosed embodiment uses ranking to simultaneously apply the generation model 102 and the retrieval model 103, combining the advantages of the two methods, and the accuracy can be greatly improved. Experiments have shown that compared with the existing technology, the accuracy is improved by 30.72%, and it has broad market prospects.
[0135] Currently, the automatic generation of annotation information is still in the academic stage, and its results are still somewhat different from actual scene usage. The disclosed embodiment integrates the generation model 102 and the retrieval model 103, and can use user input as a prerequisite to transform the learning task into submitting information completion, which can further improve the model's usability and accuracy and has good commercial prospects.
[0136] like Figure 7 As shown, according to an embodiment of the present disclosure, a device for generating annotation information for code changes is provided, comprising:
[0137] A code change acquisition unit 610, used to acquire code changes;
[0138] A candidate annotation information generating unit 620, configured to input the code change into a generation model and a retrieval model respectively, and the generation model and the retrieval model respectively output candidate annotation information;
[0139] The annotation information determining unit 630 is used to determine the generated annotation information from the candidate annotation information outputted by the generation model and the retrieval model respectively.
[0140] Optionally, the annotation information determination unit 630 is further used to: determine the degree of association between the candidate annotation information output by the generation model and the retrieval model respectively and the code change; and select the candidate annotation information as the generated annotation information according to the degree of association.
[0141] Optionally, the candidate annotation information generating unit 620 is further used to generate a code abstract syntax tree AST path of the code change, input the code change into the generation model, and input the code change into the retrieval model.
[0142] Optionally, the generation model is pre-trained in the following manner: for a code change snippet sample in a code change snippet sample set, an AST path of the code change snippet sample is generated, the generated AST path is input into the generation model, annotation information is output by the generation model, and the annotation information output by the generation model is compared with annotation information pre-corresponding to the code change snippet sample; if the ratio of consistent annotation information in the code change snippet sample set is greater than a predetermined ratio, the generation model training is successful.
[0143] Optionally, the retrieval model includes a code change and annotation information correspondence library and a code change comparison sub-model, the code change and annotation information correspondence library includes a pre-set correspondence between code changes and annotation information, the code change comparison sub-model determines the similarity between the input code changes and the code changes in the annotation information correspondence library, and determines the annotation information from the annotation information correspondence library based on the similarity as candidate annotation information output by the retrieval model.
[0144] Optionally, the association determination unit 630 is implemented by an association model, wherein the association model is pre-generated in the following manner: for a code change and annotation information sample pair in a set of code change and annotation information sample pairs, the code change and annotation information sample pair is input into the association model, the association model outputs the association of the code change and annotation information sample pair, and compares it with a predetermined association of the code change and annotation information sample pair; if the ratio of consistent associations in the set of code change and annotation information sample pairs is greater than the predetermined ratio, the association model is trained successfully.
[0145] Optionally, the association model is a convolutional neural network.
[0146] Optionally, the annotation information determining unit 630 is further configured to: use the candidate annotation information with the greatest correlation as the generated annotation information.
[0147] Optionally, the annotation information determination unit 630 is further configured to: display the candidate annotation information in order according to the relevance, and use the candidate annotation information selected by the user as the generated annotation information.
[0148] Optionally, the annotation information determining unit 630 is further configured to:
[0149] Receive a first portion of annotation information input by a user;
[0150] causing the generation model and the retrieval model to re-output candidate annotation information starting with the first part according to the code change;
[0151] Determining the relevance of the candidate annotation information re-output by the generation model and the candidate annotation information re-output by the retrieval model to the code change;
[0152] The re-output candidate annotation information is selected according to the association degree as the generated annotation information.
[0153] Figure 7 The implementation details of each unit of the device for generating annotation information for code changes are shown in FIG. Figure 5 The method is the same as in the embodiment, so in order to save space, it will not be repeated.
[0154] like Figure 8 As shown, according to an embodiment of the present disclosure, a device for generating annotation information for code changes is provided, comprising:
[0155] A code display unit 310 for displaying the code before and after the change;
[0156] A code change display unit 320, configured to determine and display a code change based on the code before the change and the code after the change;
[0157] A candidate annotation information display unit 330, used to display candidate annotation information obtained by the generation model and the retrieval model for the code change respectively;
[0158] The annotation information determining unit 340 is used to determine the generated annotation information based on the user's selection of the candidate annotation information.
[0159] Optionally, the candidate comment information display unit 330 is further configured to: sort and display the candidate comment information according to the degree of association between the candidate comment information and the code change.
[0160] Figure 8 The implementation details of each unit of the device for generating annotation information for code changes are shown in FIG. Figure 6 The method is the same as in the embodiment, so in order to save space, it will not be repeated.
[0161] It should be understood that the above is only a preferred embodiment of the present disclosure and is not intended to limit the scope of the present invention. For those skilled in the art, there are many variations of the embodiments of this specification. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
[0162] It should be understood that the various embodiments in this specification are described in a progressive manner, and the same or similar parts between the various embodiments can be referenced to each other, and each embodiment focuses on the differences from other embodiments.
[0163] It should be understood that the above is a description of a specific embodiment of the present specification. Other embodiments are within the scope of the claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the accompanying drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0164] It should be understood that an element described in singular form herein or shown in the drawings as only one does not limit the number of the element to one. In addition, modules or elements described or shown as separate herein may be combined into a single module or element, and modules or elements described or shown as single herein may be split into multiple modules or elements.
[0165] It should also be understood that the terms and expressions used herein are for description only, and one or more embodiments of this specification should not be limited to these terms and expressions. The use of these terms and expressions does not mean to exclude any equivalent features of the illustrations and descriptions (or parts thereof), and it should be recognized that various modifications that may exist should also be included in the scope of the claims. Other modifications, changes and substitutions may also exist. Accordingly, the claims should be deemed to cover all such equivalents.
Claims
1. A method for generating annotation information for code changes, comprising: Get code changes; Inputting the code changes into a generation model and a retrieval model respectively, and the generation model and the retrieval model respectively output candidate annotation information; Determining generated annotation information from candidate annotation information outputted by the generation model and the retrieval model respectively; The step of determining the generated annotation information from the candidate annotation information respectively outputted by the generation model and the retrieval model comprises: Determining the relevance between the candidate annotation information respectively output by the generation model and the retrieval model and the code change; selecting candidate annotation information according to the association degree as generated annotation information; The step of inputting the code changes into the generation model and the retrieval model respectively comprises: Generate a code abstract syntax tree AST path of the code change and input it into the generation model; inputting the code changes into the search model; The generative model is pre-trained in the following way: For a code change snippet sample in the code change snippet sample set, generate an AST path of the code change snippet sample, input the generated AST path into a generation model, output annotation information by the generation model, compare the annotation information output by the generation model with the annotation information corresponding to the code change snippet sample in advance, and if the ratio of the consistent annotation information in the code change snippet sample set is greater than a predetermined ratio, the generation model training is successful; The retrieval model includes a code change and annotation information correspondence library and a code change comparison sub-model. The code change and annotation information correspondence library includes a pre-set correspondence between code changes and annotation information. The code change comparison sub-model determines the similarity between the input code changes and the code changes in the annotation information correspondence library, and determines the annotation information from the annotation information correspondence library based on the similarity as candidate annotation information output by the retrieval model.
2. The method according to claim 1, wherein: The determining of the relevance between the candidate annotation information respectively output by the generation model and the retrieval model and the code change is implemented by a relevance model, wherein the relevance model is pre-generated in the following manner: For a code change and annotation information sample pair in a set of code change and annotation information sample pairs, the code change and annotation information sample pair is input into a correlation model, and the correlation model outputs the correlation of the code change and annotation information sample pair, which is compared with a predetermined correlation of the code change and annotation information sample pair. If the ratio of consistent correlations in the set of code change and annotation information sample pairs is greater than the predetermined ratio, the correlation model is successfully trained.
3. The method according to claim 2, wherein: The association model is a convolutional neural network.
4. The method according to claim 1, wherein: The step of selecting candidate annotation information according to the association degree as generated annotation information includes: The candidate annotation information with the greatest correlation degree is used as the generated annotation information.
5. The method according to claim 1, wherein: The step of selecting candidate annotation information according to the association degree as generated annotation information includes: The candidate annotation information is displayed in order according to the relevance, and the candidate annotation information selected by the user is used as the generated annotation information.
6. The method according to claim 1, wherein: The step of selecting candidate annotation information according to the association degree as generated annotation information includes: Receive a first portion of annotation information input by a user; causing the generation model and the retrieval model to re-output candidate annotation information starting with the first part according to the code change; Determining the relevance of the candidate annotation information re-output by the generation model and the candidate annotation information re-output by the retrieval model to the code change; The re-output candidate annotation information is selected according to the association degree as the generated annotation information.
7. The method according to claim 1, comprising: Display the code before and after the change; Determine and display the code change based on the code before and after the change; Display candidate annotation information obtained by the generation model and the retrieval model for the code change respectively; Based on the user's selection of the candidate annotation information, the generated annotation information is determined.
8. The method according to claim 7, wherein: The displaying of candidate annotation information obtained by the generation model and the retrieval model for the code change includes: The candidate annotation information is displayed in order according to the relevance between the candidate annotation information and the code change.
9. A device for generating annotation information for code changes, comprising: A code change acquisition unit, used for acquiring code changes; A candidate annotation information generating unit, used for inputting the code change into a generation model and a retrieval model respectively, and the generation model and the retrieval model respectively output candidate annotation information; An annotation information determination unit, configured to determine generated annotation information from candidate annotation information outputted by the generation model and the retrieval model respectively; The annotation information determination unit is further used for: Determining the relevance between the candidate annotation information respectively output by the generation model and the retrieval model and the code change; selecting candidate annotation information according to the association degree as generated annotation information; The candidate annotation information generating unit is further used to generate a code abstract syntax tree AST path of the code change, input the code change into the generation model, and input the code change into the retrieval model; The generative model is pre-trained in the following way: For a code change snippet sample in a code change snippet sample set, an AST path of the code change snippet sample is generated, the generated AST path is input into a generation model, the generation model outputs annotation information, the annotation information output by the generation model is compared with annotation information corresponding to the code change snippet sample in advance, and if the ratio of the consistent annotation information in the code change snippet sample set is greater than a predetermined ratio, the generation model is trained; The retrieval model includes a code change and annotation information correspondence library and a code change comparison sub-model. The code change and annotation information correspondence library includes a pre-set correspondence between code changes and annotation information. The code change comparison sub-model determines the similarity between the input code changes and the code changes in the annotation information correspondence library, and determines the annotation information from the annotation information correspondence library based on the similarity as candidate annotation information output by the retrieval model.
10. The device according to claim 9, wherein: The annotation information determination unit determines the relevance between the candidate annotation information output by the generation model and the retrieval model and the code change by a relevance model, wherein the relevance model is pre-generated in the following manner: For a code change and annotation information sample pair in a set of code change and annotation information sample pairs, the code change and annotation information sample pair is input into a correlation model, and the correlation model outputs the correlation of the code change and annotation information sample pair, which is compared with a predetermined correlation of the code change and annotation information sample pair. If the ratio of consistent correlations in the set of code change and annotation information sample pairs is greater than the predetermined ratio, the correlation model is successfully trained.
11. The device according to claim 10, wherein: The association model is a convolutional neural network.
12. The device according to claim 9, wherein: The annotation information determination unit is further used for: The candidate annotation information with the greatest correlation degree is used as the generated annotation information.
13. The device according to claim 9, wherein: The annotation information determination unit is further used for: The candidate annotation information is displayed in order according to the relevance, and the candidate annotation information selected by the user is used as the generated annotation information.
14. The device according to claim 9, wherein: The annotation information determination unit is further used for: Receive a first portion of annotation information input by a user; causing the generation model and the retrieval model to re-output candidate annotation information starting with the first part according to the code change; Determining the relevance of the candidate annotation information re-output by the generation model and the candidate annotation information re-output by the retrieval model to the code change; The re-output candidate annotation information is selected according to the association degree as the generated annotation information.
15. The apparatus according to claim 9, comprising: A pre- and post-change code display unit, used to display the pre- and post-change codes; A code change display unit, used to determine and display the code change based on the code before the change and the code after the change; A candidate annotation information display unit, used to display candidate annotation information obtained by the generation model and the retrieval model for the code change respectively; The annotation information determining unit is used to determine the generated annotation information based on the user's selection of the candidate annotation information.
16. The device according to claim 15, wherein: The candidate comment information display unit is further used to: sort and display the candidate comment information according to the relevance between the candidate comment information and the code change.
17. A computer device comprising: A memory for storing computer executable code; A processor is used to execute the computer executable code to implement the method for generating annotation information for code changes according to any one of claims 1-8.
18. A computer-readable medium comprising computer executable codes, wherein when the computer executable codes are executed by a processor, the method for generating annotation information for code changes according to any one of claims 1 to 8 is implemented.
