Code review method and device
The code review model is used to find related characters from the historical code review suggestion library and generate code review suggestions, which solves the problem of low code review efficiency in the existing technology and achieves more efficient and high-quality code review.
Patent Information
- Application Number
- CN202210025868.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-01-11
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2042-01-11
AI Technical Summary
The efficiency of code review in the existing technology is low, static scanning tools rely on limited rule sets, and deep learning models require full code review, resulting in a large workload and low efficiency.
The code review model is used to find characters associated with the code fragments to be reviewed from the historical code review suggestion library, and code review suggestions are generated through relevance conditions to reduce review complexity and improve efficiency.
Identify more problems through the code review model, improve the efficiency and quality of code review, avoid the need for full code review, and improve the efficiency of review of code change fragments.
Smart Images

Figure CN114443476B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence, and specifically to a code review method, device, electronic device and storage medium. The present application also relates to a code review model training method, device, electronic device and storage medium. Background Art
[0002] Code review means that after the coder finishes writing or modifying the code and submits the code file to the code library, the code reviewer reads the code to check whether the code submitted by the coder meets the coding standards and the quality of the code. Code review is an indispensable part of the software development process. By reviewing the code, the readability and maintainability of the code can be improved, and the quality of software development can be guaranteed.
[0003] At present, in the process of code review of code writing or code modification completed by coders, static scanning tools or deep learning models are usually used for code review. However, the use of static scanning tools to conduct code review on code writing data or code modification data relies on the rule set corresponding to code modification-code review. The limited rule set makes it impossible for code modifications not included in the rule set to match the corresponding code review opinions, reducing the scope of code review for code modifications; the use of deep learning models to conduct code review on code modification data requires code review of all code source files, which brings about a complex workload for code review work, resulting in low efficiency of code review work.
[0004] Therefore, how to improve the efficiency of code review for code changes is a technical problem that technical personnel in this field urgently need to solve. Summary of the invention
[0005] The embodiments of the present application provide a code review method, device, electronic device and computer storage medium to solve the technical problem of low efficiency of code review for code modification in the prior art. The present application also relates to a code review model training method, device, electronic device and storage medium.
[0006] An embodiment of the present application provides a code review method, comprising: obtaining a code snippet to be reviewed; obtaining code review suggestions for the code snippet to be reviewed based on a code review model; wherein the code review model is used to search for associated words corresponding to characters included in the code snippet to be reviewed from words included in historical code review suggestions, and the degree of association between the characters included in the code snippet to be reviewed and the associated words meets a preset degree of association condition, and a code review suggestion is generated for the code snippet to be reviewed based on the associated words.
[0007] Optionally, obtaining code review suggestions for the code snippet to be reviewed based on the code review model includes: obtaining a code character embedding vector for the code snippet to be reviewed corresponding to the code snippet to be reviewed; and generating a code review suggestion word embedding vector whose correlation with the code character embedding vector to be reviewed satisfies a preset correlation condition through the correspondence between historical code snippet character embedding vectors and historical code review suggestion word embedding vectors pre-stored in a historical code review suggestion library.
[0008] Optionally, obtaining the code character embedding vector to be reviewed corresponding to the code fragment to be reviewed includes: performing character splitting processing on the code fragment to be reviewed to obtain multiple sub-code characters; obtaining sub-code character embedding vectors corresponding to the multiple sub-code characters; and combining the obtained multiple sub-code character embedding vectors into the code character embedding vector to be reviewed in a preset manner.
[0009] Optionally, it also includes: obtaining sub-code character vectors corresponding to multiple sub-code characters; the obtaining of sub-code character embedding vectors corresponding to the multiple sub-code characters includes: for each sub-code character vector, generating a reduced-dimensional sub-code character embedding vector through a multi-layer embedding network model; wherein the multi-layer embedding network model is used to obtain a reduced-dimensional word vector for an input high-dimensional word vector, or to obtain a reduced-dimensional character vector for an input high-dimensional character vector.
[0010] Optionally, the correspondence between the historical code fragment character embedding vectors and the historical code review suggestion word embedding vectors pre-stored in the historical code review suggestion library is used to generate a code review suggestion word embedding vector whose correlation with the code character embedding vector to be reviewed meets a preset correlation condition, including: matching a plurality of sub-code character embedding vectors included in the code character embedding vector to be reviewed with sub-historical code character embedding vectors included in the historical code fragment character embedding vectors in the historical code review suggestion library respectively; obtaining a sub-historical code character embedding vector corresponding to the sub-character embedding vector; obtaining a sub-historical code review suggestion word embedding vector corresponding to the sub-historical code character embedding vector; using the sub-historical code review suggestion word embedding vector as the associated word embedding vector corresponding to the sub-character embedding vector; and generating a code review suggestion word embedding vector whose correlation with the code character embedding vector to be reviewed meets a preset correlation condition based on the obtained plurality of associated word embedding vectors.
[0011] Optionally, the method further includes: screening the multiple sub-code characters according to preset screening conditions to obtain multiple target sub-code characters; obtaining sub-code character vectors corresponding to the multiple sub-code characters includes: obtaining target sub-code character vectors corresponding to the multiple target sub-code characters.
[0012] Optionally, the code snippet to be reviewed is a changed code snippet in the source code; and obtaining code review suggestions for the code snippet to be reviewed according to a code review model includes: obtaining code review suggestions for the changed code snippet according to a code review model.
[0013] An embodiment of the present application also provides a method for training a code review model, comprising: obtaining a historical code snippet and a historical code review suggestion corresponding to the historical code snippet; obtaining a historical code snippet character embedding vector corresponding to the historical code snippet, and a historical code review suggestion word embedding vector corresponding to the historical code review suggestion; obtaining a first code review suggestion word embedding vector for the historical code snippet character embedding vector according to an initial code review model; calculating the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector, the cosine similarity being used to characterize the degree of similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector; and training the initial code review model based on the cosine similarity.
[0014] Optionally, obtaining the historical code fragment character embedding vector corresponding to the historical code fragment, and the historical code review suggestion word embedding vector corresponding to the historical code review suggestion, includes: performing character segmentation processing on the historical code fragment to obtain multiple sub-historical code characters; performing word segmentation processing on the historical code review suggestion to obtain multiple sub-historical code review suggestion words; obtaining sub-historical code character embedding vectors corresponding to multiple sub-historical code characters, and sub-historical code review suggestion word embedding vectors corresponding to multiple sub-historical code review suggestion words; accumulating the obtained multiple sub-historical code character embedding vectors to obtain a vector as the historical code fragment character embedding vector; correspondingly, accumulating the obtained multiple sub-historical code review suggestion word embedding vectors to obtain a vector as the historical code review suggestion word embedding vector.
[0015] Optionally, the obtaining of sub-historical code character embedding vectors corresponding to multiple sub-historical code characters and sub-historical code review suggestion word embedding vectors corresponding to multiple sub-historical code review suggestion words includes: obtaining sub-historical code character vectors corresponding to multiple sub-historical code characters and sub-historical code review suggestion word vectors corresponding to multiple sub-historical code review suggestion words; generating multiple reduced-dimensional sub-historical code character embedding vectors and multiple reduced-dimensional sub-historical code review suggestion word embedding vectors respectively through a multi-layer embedding network model; wherein the multi-layer embedding network model is used to obtain a reduced-dimensional word vector for an input high-dimensional word vector, or to obtain a reduced-dimensional character vector for an input high-dimensional character vector.
[0016] Optionally, calculating the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector includes: calculating the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector according to a first formula; wherein the first formula is:
[0017]
[0018] Wherein, similarity(A, B) represents the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector; A represents the first code review suggestion word embedding vector, A i represents the i-th element in the embedding vector of the first code review suggestion, B represents the embedding vector of the historical code review suggestion, and B i Represents the i-th element in the historical code review suggestion word embedding vector.
[0019] Optionally, the initial code review model is trained based on the cosine similarity, including: obtaining a first loss between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector based on the cosine similarity; transmitting the first loss back to the code review model being trained; and adjusting parameters of the code review model being trained based on the first loss.
[0020] Optionally, it also includes: if the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector is less than a predetermined threshold, terminating the training of the code review model; wherein the correlation between the first code review suggestion corresponding to the first code review suggestion word embedding vector and the historical code snippet satisfies a preset correlation condition.
[0021] An embodiment of the present application also provides a code review device, comprising: an acquisition unit, used to obtain a code snippet to be reviewed; a code review suggestion generation unit, used to obtain code review suggestions for the code snippet to be reviewed based on a code review model; wherein the code review model is used to search for associated words corresponding to the characters included in the code snippet to be reviewed from the words included in the historical code review suggestions, and the degree of association between the characters included in the code snippet to be reviewed and the associated words meets a preset degree of association condition, and a code review suggestion is generated for the code snippet to be reviewed based on the associated words.
[0022] An embodiment of the present application also provides a training device for a code review model, comprising: a first obtaining unit, used to obtain historical code snippets and historical code review suggestions corresponding to the historical code snippets; a second obtaining unit, used to obtain historical code snippet character embedding vectors corresponding to the historical code snippets, and historical code review suggestion word embedding vectors corresponding to the historical code review suggestions; a third obtaining unit, used to obtain a first code review suggestion word embedding vector for the historical code snippet character embedding vector according to the initial code review model; a calculation unit, used to calculate the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector, the cosine similarity being used to characterize the degree of similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector; and a training unit, used to train the initial code review model based on the cosine similarity.
[0023] An embodiment of the present application further provides an electronic device, comprising: a processor; and a memory for storing a program of the method, wherein when the program is read and executed by the processor, any of the above methods is executed.
[0024] An embodiment of the present application further provides a computer storage medium, wherein the computer storage medium stores a computer program, and when the program is executed, the program performs any of the above-mentioned methods.
[0025] Compared with the prior art, this application has the following advantages:
[0026] The code review method provided in the embodiment of the present application includes: obtaining a code snippet to be reviewed; obtaining code review suggestions for the code snippet to be reviewed based on a code review model; wherein the code review model is used to search for associated words corresponding to characters included in the code snippet to be reviewed from words included in historical code review suggestions, and the degree of association between the characters included in the code snippet to be reviewed and the associated words meets a preset degree of association condition, and a code review suggestion is generated for the code snippet to be reviewed based on the associated words.
[0027] The code review method provided in the present application adopts a code review model to obtain code review suggestions based on the code snippet to be reviewed. Specifically, the code review model generates corresponding code review suggestions for the code snippet to be reviewed based on the correspondence between the characters included in the historical code snippet pre-stored in the historical code review suggestion library and the words included in the historical code review suggestions. In the above method, compared with the limited domain knowledge of the reviewer, the code review model is obtained through massive prior knowledge training, and can identify more problems. Obtaining the code review suggestions corresponding to the code snippet to be reviewed based on the code review model not only improves the efficiency of code review, but also improves the quality of code review. BRIEF DESCRIPTION OF THE DRAWINGS
[0028] Figure 1 An application scenario diagram of a code review model training method provided in an embodiment of the present application.
[0029] Figure 2 A flowchart of a code review method provided for the first embodiment of the present application.
[0030] Figure 3 A schematic diagram of a code review device provided in the second embodiment of the present application.
[0031] Figure 4 A flowchart of a method for training a code review model provided in the third embodiment of the present application.
[0032] Figure 5 A schematic diagram of a training device for a code review model provided in the fourth embodiment of the present application.
[0033] Figure 6 A schematic diagram of an electronic device provided in accordance with the fifth embodiment of the present application. DETAILED DESCRIPTION
[0034] Many specific details are described in the following description to facilitate a full understanding of the present application. However, the present application can be implemented in many other ways than those described herein, and those skilled in the art can make similar generalizations without violating the connotation of the present application, so the present application is not limited by the specific implementation disclosed below.
[0035] The embodiment of the present application proposes a code review method, which searches for associated words whose association with the characters of the code snippet to be reviewed meets a preset association condition based on the association relationship between the characters included in the historical code snippets stored in the historical code review suggestion library in the code review model and the words included in the historical code review suggestions, and generates code review suggestions corresponding to the code snippet to be reviewed based on the associated words. This process does not require reading the full amount of code data for code review, which improves the efficiency of code review for code change snippets. Compared with the limited domain knowledge of the reviewer, the code review model is obtained through massive prior knowledge training, and can identify more problems. Obtaining code review suggestions corresponding to the code snippet to be reviewed based on the code review model not only improves the efficiency of code review, but also improves the quality of code review.
[0036] The code review model training method provided in the embodiment of the present application can be executed by any computer with data processing capabilities, such as a terminal device or a server.
[0037] Figure 1 A schematic diagram of an application scenario of a code review model training method is provided for an embodiment of the present application. Figure 1Provide a detailed introduction.
[0038] like Figure 1 As shown, the training process of the code review model includes: data processing stage, model training stage and code review suggestion generation stage.
[0039] In the data processing stage 101, the server receives the historical code review request suggestion request data 101-1 sent by the user end, splits the historical code review request suggestion request data 101-1, and obtains the historical code review suggestion 101-2 and the historical code change segment 101-3. The historical code review suggestion 101-2 is processed by word segmentation to obtain multiple sub-historical code review suggestion words, and the historical code change segment 101-3 is processed by character segmentation to obtain multiple sub-historical code characters.
[0040] Obtain sub-historical code character vectors 101-5 corresponding to multiple sub-historical code characters, and sub-historical code review suggestion word vectors corresponding to multiple sub-historical code review suggestion words; generate multiple reduced-dimensional sub-historical code character embedding vectors 101-4 and multiple reduced-dimensional sub-historical code review suggestion word embedding vectors respectively through a multi-layer embedding network model; wherein the multi-layer embedding network model is used to obtain a reduced-dimensional word vector for an input high-dimensional word vector, or to obtain a reduced-dimensional character vector for an input high-dimensional character vector.
[0041] A vector obtained by accumulating the multiple sub-historical code character embedding vectors is used as the historical code fragment character embedding vector; correspondingly, a vector obtained by accumulating the multiple sub-historical code review suggestion word embedding vectors is used as the historical code review suggestion word embedding vector.
[0042] In addition, the obtained multiple sub-historical code character embedding vectors may be accumulated, and the accumulated vectors may be normalized to obtain a vector as the historical code fragment character embedding vector. The obtained multiple sub-historical code review suggestion word embedding vectors may be accumulated, and the accumulated vectors may be normalized to obtain a vector as the historical code review suggestion word embedding vector.
[0043] For example, the vector obtained by accumulating the multiple sub-historical code character embedding vectors is {3, 2, 0, 1, 3}. In order to improve the calculation efficiency, the vector is normalized and the vector obtained after the normalization is used as the historical code fragment character embedding vector. Correspondingly, the vector obtained by accumulating the multiple sub-historical code review suggestion word embedding vectors is {3, 3, 6, 2, 3}. In order to improve the calculation efficiency, the vector is normalized and the vector obtained after the normalization is used as the historical code review suggestion word embedding vector.
[0044] In the model training stage 102, a first code review suggestion word embedding vector is obtained for the historical code snippet character embedding vector according to the initial code review model; a first loss between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector is obtained according to the cosine similarity by calculating the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector; the first loss is fed back to the code review model being trained; and the code review model being trained performs parameter adjustment according to the first loss.
[0045] Among them, the historical code review suggestion word embedding vector is 102-1, the historical code snippet character embedding vector is 102-2, and Rel(C,R)102-3 represents the correlation between the historical code review suggestion word embedding vector 102-1 and the historical code snippet character embedding vector 102-2.
[0046] If the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector is less than a predetermined threshold, the training of the code review model is terminated; this indicates that the correlation between the first code review suggestion corresponding to the first code review suggestion word embedding vector and the historical code snippet meets a preset correlation condition.
[0047] In the code review suggestion generation stage 103, the code snippet 103-1 to be reviewed is input into the trained code review model 103-2, and a code review suggestion 103-3 corresponding to the code snippet 103-1 to be reviewed outputted by the trained code review model 103-2 is obtained.
[0048] Among them, the code review model searches for associated words whose association with the characters included in the code snippet to be reviewed meets preset association conditions based on the correspondence between the characters included in the historical code snippets in the historical code review suggestion library and the words included in the historical code review suggestions, and generates corresponding code review suggestions for the code snippet to be reviewed based on the associated words.
[0049] The above code review method is described in more detail below in conjunction with specific embodiments.
[0050] First embodiment
[0051] like Figure 2 As shown, it is a flowchart of a code review method provided in the first embodiment of the present application, and the method includes the following steps.
[0052] Step S201: Obtain a code snippet to be reviewed.
[0053] This step is used to obtain the code snippets to be reviewed, and to provide a data basis for the subsequent analysis of the code review suggestions corresponding to the code snippets to be reviewed.
[0054] The code snippet to be reviewed is a code snippet that has been changed in the source code.
[0055] In the embodiment of the present application, a code review model is used to generate corresponding code review suggestions for the code snippets to be reviewed. The code review suggestions corresponding to the changed code snippets are analyzed based on the changed code snippets rather than the full code source files, thereby improving the efficiency of generating code review suggestions for the changed parts of the code.
[0056] Step S202: Obtain code review suggestions for the code snippet to be reviewed according to the code review model.
[0057] This step is used to obtain code review suggestions for the code snippet to be reviewed using the code review model.
[0058] Among them, the code review model is used to search for associated words corresponding to the characters included in the code snippet to be reviewed from the words included in the historical code review suggestions, the correlation between the characters included in the code snippet to be reviewed and the associated words meets the preset correlation condition, and code review suggestions are generated for the code snippet to be reviewed based on the associated words.
[0059] The words included in the historical code review suggestions are words in the historical code review suggestions pre-stored in the historical code review library in the code review model.
[0060] Specifically, the historical code review suggestion library stores the correspondence between historical code snippets and historical code review suggestions, including: the correspondence between the characters included in the historical code snippets and the words included in the historical code review suggestions, the correspondence between the historical code snippet character embedding vectors corresponding to the historical code snippets and the historical code review suggestion word embedding vectors corresponding to the historical code review suggestions, and the correspondence between the sub-historical code snippet character embedding vectors contained in the historical code snippet character embedding vectors and the sub-historical code review suggestion word embedding vectors contained in the historical code review suggestion word embedding vectors.
[0061] Obtaining code review suggestions for the code snippet to be reviewed based on the code review model may refer to: obtaining a code character embedding vector for the code snippet to be reviewed corresponding to the code snippet to be reviewed; and generating a code review suggestion word embedding vector whose correlation with the code character embedding vector to be reviewed satisfies a preset correlation condition through the correspondence between historical code snippet character embedding vectors and historical code review suggestion word embedding vectors pre-stored in a historical code review suggestion library.
[0062] After obtaining the code snippet to be reviewed, the character embedding vector of the code snippet to be reviewed corresponding to the code snippet to be reviewed is obtained, and the embedding vector related to the character embedding vector of the code snippet to be reviewed is queried in the historical code review suggestion library. Then, the historical code review suggestion data corresponding to the code review suggestion word embedding vector having a preset degree of similarity with the character embedding vector of the code snippet to be reviewed is obtained.
[0063] The obtaining of the code character embedding vector to be reviewed corresponding to the code fragment to be reviewed may refer to: performing character splitting processing on the code fragment to be reviewed to obtain multiple sub-code characters; obtaining sub-code character embedding vectors corresponding to the multiple sub-code characters; and combining the obtained multiple sub-code character embedding vectors into the code character embedding vector to be reviewed in a preset manner.
[0064] After obtaining the multiple sub-codes, the method further includes: obtaining sub-code character vectors corresponding to the multiple sub-code characters.
[0065] The method of obtaining the sub-code character embedding vectors corresponding to the multiple sub-code characters includes: for each sub-code character vector, generating a sub-code character embedding vector of reduced dimension through a multi-layer embedding network model; wherein the multi-layer embedding network model is used to obtain a reduced-dimensional word vector for an input high-dimensional word vector, or to obtain a reduced-dimensional character vector for an input high-dimensional character vector.
[0066] After obtaining the plurality of subcodes, the method further includes: screening the plurality of subcode characters according to a preset screening condition to obtain a plurality of target subcode characters. The method of obtaining the subcode character vectors corresponding to the plurality of subcode characters includes: obtaining the target subcode character vectors corresponding to the plurality of target subcode characters.
[0067] The method generates a code review suggestion word embedding vector whose correlation with the code character embedding vector to be reviewed meets a preset correlation condition based on the correspondence between the historical code fragment character embedding vectors and the historical code review suggestion word embedding vectors pre-stored in the historical code review suggestion library, including: matching a plurality of sub-code character embedding vectors included in the code character embedding vector to be reviewed with the sub-historical code character embedding vectors included in the historical code fragment character embedding vectors in the historical code review suggestion library respectively; obtaining a sub-historical code character embedding vector corresponding to the sub-character embedding vector; obtaining a sub-historical code review suggestion word embedding vector corresponding to the sub-historical code character embedding vector; using the sub-historical code review suggestion word embedding vector as the associated word embedding vector corresponding to the sub-character embedding vector; and generating a code review suggestion word embedding vector whose correlation with the code character embedding vector to be reviewed meets the preset correlation condition based on the obtained plurality of associated word embedding vectors.
[0068] Second embodiment
[0069] Corresponding to the application scenario embodiment of the training method of the code review model provided by the present application and the code review method provided by the first embodiment, the second embodiment of the present application also provides a code review device. Since the device embodiment is basically similar to the application scenario embodiment and the first embodiment, the description is relatively simple, and the relevant parts can be referred to the application scenario embodiment and the partial description of the first embodiment. The device embodiment described below is only illustrative.
[0070] Please refer to Figure 3 , which is a schematic diagram of a code review device provided in the second embodiment of the present application.
[0071] The code review device includes:
[0072] An obtaining unit 301 is used to obtain a code snippet to be reviewed;
[0073] A code review generating unit 302 is used to obtain a code review suggestion for the code segment to be reviewed according to the code review model;
[0074] Among them, the code review model is used to search for associated words corresponding to the characters included in the code snippet to be reviewed from the words included in the historical code review suggestions, the correlation between the characters included in the code snippet to be reviewed and the associated words meets the preset correlation condition, and code review suggestions are generated for the code snippet to be reviewed based on the associated words.
[0075] Optionally, obtaining code review suggestions for the code snippet to be reviewed based on the code review model includes: obtaining a code character embedding vector for the code snippet to be reviewed corresponding to the code snippet to be reviewed; and generating a code review suggestion word embedding vector whose correlation with the code character embedding vector to be reviewed satisfies a preset correlation condition through the correspondence between the historical code snippet character embedding vectors and the historical code review suggestion word embedding vectors pre-stored in the historical code review suggestion library.
[0076] Optionally, obtaining the code character embedding vector to be reviewed corresponding to the code fragment to be reviewed includes: performing character splitting processing on the code fragment to be reviewed to obtain multiple sub-code characters; obtaining sub-code character embedding vectors corresponding to the multiple sub-code characters; and combining the obtained multiple sub-code character embedding vectors into the code character embedding vector to be reviewed in a preset manner.
[0077] Optionally, the device is also used to: obtain sub-code character vectors corresponding to multiple sub-code characters; the obtaining of sub-code character embedding vectors corresponding to the multiple sub-code characters includes: for each sub-code character vector, generating a reduced-dimensional sub-code character embedding vector through a multi-layer embedding network model; wherein the multi-layer embedding network model is used to obtain a reduced-dimensional word vector for an input high-dimensional word vector, or to obtain a reduced-dimensional character vector for an input high-dimensional character vector.
[0078] Optionally, the correspondence between the historical code fragment character embedding vectors and the historical code review suggestion word embedding vectors pre-stored in the historical code review suggestion library is used to generate a code review suggestion word embedding vector whose correlation with the code character embedding vector to be reviewed meets a preset correlation condition, including: matching a plurality of sub-code character embedding vectors included in the code character embedding vector to be reviewed with sub-historical code character embedding vectors included in the historical code fragment character embedding vectors in the historical code review suggestion library, respectively; obtaining a sub-historical code character embedding vector corresponding to the sub-character embedding vector; obtaining a sub-historical code review suggestion word embedding vector corresponding to the sub-historical code character embedding vector; using the sub-historical code review suggestion word embedding vector as the associated word embedding vector corresponding to the sub-character embedding vector; and generating a code review suggestion word embedding vector whose correlation with the code character embedding vector to be reviewed meets a preset correlation condition based on the obtained plurality of associated word embedding vectors.
[0079] Optionally, the device is also used to: filter the multiple sub-code characters according to preset filtering conditions to obtain multiple target sub-code characters; obtaining sub-code character vectors corresponding to the multiple sub-code characters includes: obtaining target sub-code character vectors corresponding to the multiple target sub-code characters.
[0080] Optionally, the code snippet to be reviewed is a changed code snippet in the source code; and obtaining code review suggestions for the code snippet to be reviewed according to a code review model includes: obtaining code review suggestions for the changed code snippet according to a code review model.
[0081] Third embodiment
[0082] In the above-mentioned first embodiment, a code review method is provided, and the code review method mainly generates code review suggestions for the code fragments to be reviewed whose relevance meets the preset relevance conditions through the code review model. Correspondingly, the third embodiment of the present application provides a training method for the code review model. Since the code review method has been described in detail in the first embodiment, please refer to the description of the first embodiment for the relevant parts, and no further description is given here.
[0083] The third embodiment of the present application provides a method for training a code review model. Figure 4 , which is a flowchart of a method for training a code review model provided in the third embodiment of the present application.
[0084] Step S401: Obtain a historical code snippet and a historical code review suggestion corresponding to the historical code snippet.
[0085] This step is used to obtain historical code snippets and historical code review suggestions, which serve as training data for training the code review model.
[0086] The historical code snippets and the historical code review suggestions corresponding to the historical code snippets are used as training data because the correlation between the historical code snippets and the historical code review suggestions meets the preset correlation condition. The code review model trained in this embodiment can generate code review suggestions for the code snippets to be reviewed, whose correlation with the code snippets to be reviewed meets the preset correlation condition.
[0087] Therefore, in an embodiment of the present application, a historical code snippet and a historical code review suggestion are used as sample data pairs, and a first code review suggestion corresponding to the historical code snippet is generated through an initial code review model; the cosine similarity between the first code review suggestion and the historical code review suggestions corresponding to the historical code snippet stored in a historical code review suggestion database is calculated; the degree of difference between the first code review suggestion and the historical code review suggestion is determined based on the obtained cosine similarity value; and the code review model is further trained based on the degree of difference.
[0088] The following steps describe the training process of the code review model.
[0089] Before training the code review model, it is also necessary to perform data processing on the above historical code snippets and historical code review suggestions, please refer to step S402.
[0090] Step S402: Obtain a historical code snippet character embedding vector corresponding to the historical code snippet, and a historical code review suggestion word embedding vector corresponding to the historical code review suggestion.
[0091] This step is used to perform data vectorization processing on the obtained historical code snippets and historical code review suggestions.
[0092] The obtaining of the historical code snippet character embedding vector corresponding to the historical code snippet and the historical code review suggestion word embedding vector corresponding to the historical code review suggestion includes:
[0093] The historical code fragment is processed by character segmentation to obtain a plurality of sub-historical code characters; the historical code review suggestion is processed by word segmentation to obtain a plurality of sub-historical code review suggestion words.
[0094] The historical code snippet is a historical code snippet that has been changed in the source code. The changed historical code snippet is processed by character segmentation. Since the changed historical code snippet usually includes special words related to the code project but with low frequency, such as variable names, derivative words, method names, plural words, etc. The above words represent the attributes of the historical code change code. In order to avoid the phenomenon that the above special words cannot be represented by word vectors, the historical code snippet is processed by character segmentation and data vectorization to obtain sub-historical code character embedding vectors corresponding to multiple sub-historical code characters.
[0095] The historical code review suggestions are segmented and data vectorized, and the sub-historical code review suggestion word embedding vectors corresponding to multiple sub-historical code review suggestion words are obtained.
[0096] A vector obtained by accumulating the multiple sub-historical code character embedding vectors is used as the historical code fragment character embedding vector; correspondingly, a vector obtained by accumulating the multiple sub-historical code review suggestion word embedding vectors is used as the historical code review suggestion word embedding vector.
[0097] In addition, the obtained multiple sub-historical code character embedding vectors may be accumulated, and the accumulated vectors may be normalized to obtain a vector as the historical code fragment character embedding vector. The obtained multiple sub-historical code review suggestion word embedding vectors may be accumulated, and the accumulated vectors may be normalized to obtain a vector as the historical code review suggestion word embedding vector.
[0098] For example, the vector obtained by accumulating the multiple sub-historical code character embedding vectors is {3, 2, 0, 1, 3}. In order to improve the calculation efficiency, the vector is normalized and the vector obtained after the normalization is used as the historical code fragment character embedding vector. Correspondingly, the vector obtained by accumulating the multiple sub-historical code review suggestion word embedding vectors is {3, 3, 6, 2, 3}. In order to improve the calculation efficiency, the vector is normalized and the vector obtained after the normalization is used as the historical code review suggestion word embedding vector.
[0099] In addition, the above historical code character embedding vectors and historical code review suggestion word embedding vectors are obtained by the following method:
[0100] The method of obtaining sub-historical code character embedding vectors corresponding to multiple sub-historical code characters and sub-historical code review suggestion word embedding vectors corresponding to multiple sub-historical code review suggestion words includes: obtaining sub-historical code character vectors corresponding to multiple sub-historical code characters and sub-historical code review suggestion word vectors corresponding to multiple sub-historical code review suggestion words; generating multiple reduced-dimensional sub-historical code character embedding vectors and multiple reduced-dimensional sub-historical code review suggestion word embedding vectors respectively through a multi-layer embedding network model; wherein the multi-layer embedding network model is used to obtain a reduced-dimensional word vector for an input high-dimensional word vector, or to obtain a reduced-dimensional character vector for an input high-dimensional character vector.
[0101] For example, the historical code review suggestion includes 5 sub-historical code review suggestion words, and each sub-historical code review suggestion word corresponds to a sub-historical code review suggestion word embedding vector. The first sub-historical code review suggestion word embedding vector can be defined as the first element is 1, and the remaining 4 elements are 0, and the vector can be expressed as {1, 0, 0, 0, 0}. Correspondingly, the second sub-historical code review suggestion word embedding vector can be defined as the second element is 1, and the remaining 4 elements are 0, and the vector can be expressed as {0, 1, 0, 0, 0}; the third sub-historical code review suggestion word embedding vector can be defined as the third element is 1, and the remaining 4 elements are 0, and the vector can be expressed as {0, 0, 1, 0, 0}; the fourth sub-historical code review suggestion word embedding vector can be defined as the fourth element is 1, and the remaining 4 elements are 0, and the vector can be expressed as {0, 0, 0, 1, 0}; the fifth sub-historical code review suggestion word embedding vector can be defined as the fifth element is 1, and the remaining 4 elements are 0, and the vector can be expressed as {0, 0, 0, 0, 1}.
[0102] The historical code review suggestion word embedding vector obtained by accumulating the above-mentioned first sub-historical code review suggestion word embedding vector, second sub-historical code review suggestion word embedding vector, third sub-historical code review suggestion word embedding vector, fourth sub-historical code review suggestion word embedding vector, and fifth sub-historical code review suggestion word embedding vector can be expressed as {1, 1, 1, 1, 1}.
[0103] Correspondingly, the vector representation method of the historical code character embedding vector is similar to that of the historical code review suggestion word embedding vector.
[0104] Step S403: obtaining a first code review suggestion word embedding vector for the historical code snippet character embedding vector according to the initial code review model;
[0105] This step is used to use the initial code review model to analyze the historical code snippet character embedding vector and generate a first code review suggestion word embedding vector for the historical code snippet character embedding vector.
[0106] The first code review suggestion word embedding vector obtained in this step is used to compare with the historical code review suggestions corresponding to the historical code snippets. According to the degree of difference between the two obtained by the comparison, the initial code review model is further trained.
[0107] Specifically, the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector is calculated, and the degree of difference between the two is determined according to the cosine similarity, which is specifically described in step S404.
[0108] Step S404: Calculate the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector, where the cosine similarity is used to characterize the degree of similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector.
[0109] This step is used to calculate the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector according to the formula, so as to provide a training basis for further training of the initial code review model in step S405.
[0110] Specifically, calculating the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector includes:
[0111] Calculate the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector according to a first formula;
[0112] Among them, the first formula is:
[0113]
[0114] Wherein, similarity(A, B) represents the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector; A represents the first code review suggestion word embedding vector, A i represents the i-th element in the embedding vector of the first code review suggestion, B represents the embedding vector of the historical code review suggestion, and B i represents the i-th element in the historical code review suggestion word embedding vector; n is the dimension of the historical code review suggestion word embedding vector and the first code review suggestion word embedding vector, and n is not less than 2.
[0115] The value of cosine similarity is between 0 and 1. The larger the value of cosine similarity is, the smaller the angle between the two vectors is, and the more similar the two vectors are; the smaller the value of cosine similarity is, the larger the angle between the two vectors is, and the greater the difference between the two vectors is.
[0116] Step S405: training the initial code review model according to the cosine similarity.
[0117] This step is used to train the initial code review model based on cosine similarity and determine when to stop training the initial code review model.
[0118] The training of the initial code review model according to the cosine similarity includes:
[0119] According to the cosine similarity, a first loss between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector is obtained; the first loss is fed back to the code review model in training; and the code review model in training performs parameter adjustment according to the first loss.
[0120] From the above formula, it can be seen that when the value of cosine similarity is larger, it means that the angle between the two vectors is smaller, the two vectors are more similar, and the difference between the two vectors is smaller, which means that the difference between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector is smaller, which means that the training effect of the initial code review model is better.
[0121] When the value of cosine similarity is smaller, it means that the angle between the two vectors is larger, and the difference between the two vectors is greater, which means that the difference between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector is greater, which means that the training effect of the initial code review model is worse, and the initial code review model needs to continue to be trained.
[0122] If the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector is less than a predetermined threshold, the training of the code review model is terminated; wherein the correlation between the first code review suggestion corresponding to the first code review suggestion word embedding vector and the historical code snippet satisfies a preset correlation condition.
[0123] The above cosine similarity is less than the predetermined threshold, indicating that the difference between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector is within the preset difference degree threshold range, the initial code review model has been adjusted to a state that meets the requirements, and the purpose of training has been achieved.
[0124] In addition, the following conditions can also be used as training conditions for terminating the initial code review model: continuing to provide historical code snippets to the initial code review model, the cosine similarity value between the first code review suggestion obtained and the historical code review suggestions tends to be stable.
[0125] This training result shows that the initial code review model currently used has reached its capacity limit due to the limitations of its internal architecture. There is not much point in continuing training, and the training can be stopped.
[0126] The above is a detailed description of the training method of the code review model provided in the embodiment of the present application.
[0127] The training method of the code review model provided in the embodiment of the present application includes: obtaining a historical code snippet and a historical code review suggestion corresponding to the historical code snippet; obtaining a historical code snippet character embedding vector corresponding to the historical code snippet, and a historical code review suggestion word embedding vector corresponding to the historical code review suggestion; obtaining a first code review suggestion word embedding vector for the historical code snippet character embedding vector according to the initial code review model; calculating the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector, the cosine similarity being used to characterize the degree of similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector; and training the initial code review model based on the cosine similarity.
[0128] The above method trains the initial code review model by using the historical code review suggestions corresponding to the historical code snippets as learning samples, so that the trained code review model generates code review suggestions for the code snippets to be reviewed, whose correlation with the code snippets to be reviewed meets the correlation threshold. Specifically, the first code review suggestion corresponding to the historical code snippet obtained by the initial code review model calculates the cosine similarity between the word embedding vector corresponding to the first code review suggestion and the word embedding vector of the historical code review suggestion, and determines the degree of difference between the word embedding vector corresponding to the first code review suggestion and the word embedding vector of the historical code review suggestion based on the cosine similarity to decide whether it is necessary to continue training the initial code review model. In this way, the code review model has better analytical capabilities and obtains code review suggestions for the code snippets to be reviewed, whose correlation meets the correlation conditions.
[0129] Fourth embodiment
[0130] Corresponding to the application scenario embodiment of the code review model training method provided by the present application and the code review model training method provided by the third embodiment, the fourth embodiment of the present application also provides a code review model training device. Since the device embodiment is basically similar to the application scenario embodiment and the third embodiment, the description is relatively simple, and the relevant parts can be referred to the application scenario embodiment and the partial description of the third embodiment. The device embodiment described below is only illustrative.
[0131] Please refer to Figure 5 , which is a schematic diagram of a training device for a code review model provided in the fourth embodiment of the present application.
[0132] The training setup for the code review model includes:
[0133] A first obtaining unit 501 is used to obtain a historical code segment and a historical code review suggestion corresponding to the historical code segment;
[0134] A second obtaining unit 502 is used to obtain a historical code snippet character embedding vector corresponding to the historical code snippet, and a historical code review suggestion word embedding vector corresponding to the historical code review suggestion;
[0135] A third obtaining unit 503 is used to obtain a first code review suggestion word embedding vector for the historical code snippet character embedding vector according to the initial code review model;
[0136] A calculation unit 504 is used to calculate the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector, wherein the cosine similarity is used to represent the similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector;
[0137] The training unit 505 is used to train the initial code review model according to the cosine similarity.
[0138] Optionally, obtaining the historical code fragment character embedding vector corresponding to the historical code fragment, and the historical code review suggestion word embedding vector corresponding to the historical code review suggestion, includes: performing character segmentation processing on the historical code fragment to obtain multiple sub-historical code characters; performing word segmentation processing on the historical code review suggestion to obtain multiple sub-historical code review suggestion words; obtaining sub-historical code character embedding vectors corresponding to multiple sub-historical code characters, and sub-historical code review suggestion word embedding vectors corresponding to multiple sub-historical code review suggestion words; accumulating the obtained multiple sub-historical code character embedding vectors to obtain a vector as the historical code fragment character embedding vector; correspondingly, accumulating the obtained multiple sub-historical code review suggestion word embedding vectors to obtain a vector as the historical code review suggestion word embedding vector.
[0139] Optionally, the obtaining of sub-historical code character embedding vectors corresponding to multiple sub-historical code characters and sub-historical code review suggestion word embedding vectors corresponding to multiple sub-historical code review suggestion words includes: obtaining sub-historical code character vectors corresponding to multiple sub-historical code characters and sub-historical code review suggestion word vectors corresponding to multiple sub-historical code review suggestion words; generating multiple reduced-dimensional sub-historical code character embedding vectors and multiple reduced-dimensional sub-historical code review suggestion word embedding vectors respectively through a multi-layer embedding network model; wherein the multi-layer embedding network model is used to obtain a reduced-dimensional word vector for an input high-dimensional word vector, or to obtain a reduced-dimensional character vector for an input high-dimensional character vector.
[0140] Optionally, calculating the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector includes: calculating the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector according to a first formula; wherein the first formula is:
[0141]
[0142] Wherein, similarity(A, B) represents the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector; A represents the first code review suggestion word embedding vector, A i represents the i-th element in the embedding vector of the first code review suggestion, B represents the embedding vector of the historical code review suggestion, and B i Represents the i-th element in the historical code review suggestion word embedding vector.
[0143] Optionally, the initial code review model is trained based on the cosine similarity, including: obtaining a first loss between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector based on the cosine similarity; transmitting the first loss back to the code review model being trained; and adjusting parameters of the code review model being trained based on the first loss.
[0144] Optionally, the device is also used to: terminate the training of the code review model if the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector is less than a predetermined threshold; wherein the correlation between the first code review suggestion corresponding to the first code review suggestion word embedding vector and the historical code snippet satisfies a preset correlation condition.
[0145] Fifth embodiment
[0146] Corresponding to the methods provided in the first and third embodiments of the present application, the fifth embodiment of the present application further provides an electronic device. Since the fifth embodiment is basically similar to the above method embodiments provided in the present application, the description is relatively simple, and the relevant parts can be referred to the description of the above method embodiments provided in the present application. The fifth embodiment described below is merely illustrative.
[0147] like Figure 6 As shown, Figure 6 A schematic diagram of an electronic device provided for the ninth embodiment of the present application.
[0148] The electronic device comprises:
[0149] at least one processor 601, at least one communication interface 602, at least one memory 603 and at least one communication bus 604;
[0150] Optionally, the communication interface 602 may be an interface of a communication module, such as an interface of a WLAN (Wireless Local Area Network) module;
[0151] The processor 601 may be a processor CPU, or an application specific integrated circuit ASIC (Application Specific Integrated Circuit), or one or more integrated circuits configured to implement the embodiments of the present application.
[0152] The memory 603 may include a high-speed RAM memory, and may also include a non-volatile memory (non-volatile memory), such as at least one disk memory.
[0153] The memory 603 stores a program, and the processor 601 calls the program stored in the memory 603 to execute the above method provided in the embodiment of the present application.
[0154] Sixth embodiment
[0155] Corresponding to the above method embodiments provided by the present application, the sixth embodiment of the present application further provides a computer storage medium. Since the sixth embodiment is basically similar to the above method embodiments provided by the present application, the description is relatively simple. For the relevant parts, please refer to the description of the above method embodiments provided by the present application. The sixth embodiment described below is only illustrative.
[0156] The computer storage medium stores a computer program, and when the program is executed, the method provided in the above method embodiment is implemented.
[0157] It should be noted that the detailed description of the electronic device and computer storage medium provided in the embodiments of the present application can refer to the relevant description of the above-mentioned method embodiments provided in the present application, and will not be repeated here.
[0158] Although the present application is disclosed as above in the form of a preferred embodiment, it is not intended to limit the present application. Any technical personnel in this field may make possible changes and modifications without departing from the spirit and scope of the present application. Therefore, the scope of protection of the present application shall be based on the scope defined by the claims of the present application.
[0159] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0160] The memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.
[0161] 1. Computer-readable media includes permanent and non-permanent, removable and non-removable media that can be used to store information by any method or technology. Information can be computer-readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined in this article, computer-readable media does not include non-transitor7 media such as modulated data signals and carrier waves.
[0162] 2. Those skilled in the art should understand that the embodiments of the present application can be provided as a system or electronic device. Therefore, the present application can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present application can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
Claims
1. A code review method, characterized in that: include: Get the code snippet to be reviewed; Obtaining code review suggestions for the code snippet to be reviewed according to the code review model; The code review model is used to search for associated words corresponding to the characters included in the code snippet to be reviewed from the words included in the historical code review suggestions, the degree of association between the characters included in the code snippet to be reviewed and the associated words meets a preset degree of association condition, and a code review suggestion is generated for the code snippet to be reviewed according to the associated words; The obtaining, according to the code review model, a code review suggestion for the code fragment to be reviewed includes: Obtaining a code character embedding vector corresponding to the code snippet to be reviewed; Matching the multiple sub-code character embedding vectors included in the code character embedding vector to be reviewed with the sub-historical code character embedding vectors included in the historical code fragment character embedding vector in the historical code review suggestion library respectively; Obtaining a sub-history code character embedding vector corresponding to the sub-code character embedding vector; Obtaining a sub-historical code review suggestion word embedding vector corresponding to the sub-historical code character embedding vector; Using the sub-historical code review suggestion word embedding vector as the associated word embedding vector corresponding to the sub-code character embedding vector; Based on the obtained multiple associated word embedding vectors, a code review suggestion word embedding vector is generated, the degree of association between which and the to-be-reviewed code character embedding vector satisfies a preset association condition.
2. The method according to claim 1, characterized in that The step of obtaining the code character embedding vector corresponding to the code snippet to be reviewed includes: Performing character splitting processing on the code fragment to be reviewed to obtain multiple sub-code characters; Obtaining subcode character embedding vectors corresponding to the plurality of subcode characters; The obtained multiple sub-code character embedding vectors are combined in a preset manner to form the code character embedding vector to be reviewed.
3. The method according to claim 1, characterized in that The code snippet to be reviewed is a code snippet that has been changed in the source code; The obtaining, according to the code review model, a code review suggestion for the code fragment to be reviewed includes: According to the code review model, a code review suggestion for the changed code segment is obtained.
4. A method for training a code review model, characterized in that: include: Obtaining a historical code snippet and a historical code review suggestion corresponding to the historical code snippet; Obtaining a historical code snippet character embedding vector corresponding to the historical code snippet and a historical code review suggestion word embedding vector corresponding to the historical code review suggestion; Obtaining a first code review suggestion word embedding vector for the historical code snippet character embedding vector according to the initial code review model; Calculate the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector, where the cosine similarity is used to represent the degree of similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector; The initial code review model is trained according to the cosine similarity.
5. The method according to claim 4, characterized in that The obtaining of the historical code snippet character embedding vector corresponding to the historical code snippet and the historical code review suggestion word embedding vector corresponding to the historical code review suggestion includes: The historical code fragment is processed by character segmentation to obtain a plurality of sub-historical code characters; the historical code review suggestion is processed by word segmentation to obtain a plurality of sub-historical code review suggestion words; Obtaining sub-historical code character embedding vectors corresponding to a plurality of sub-historical code characters, and sub-historical code review suggestion word embedding vectors corresponding to a plurality of sub-historical code review suggestion words; Accumulating the obtained multiple sub-historical code character embedding vectors to obtain a vector as the historical code fragment character embedding vector; Correspondingly, a vector obtained by accumulating the multiple sub-historical code review suggestion word embedding vectors is used as the historical code review suggestion word embedding vector.
6. The method according to claim 4, characterized in that The training of the initial code review model according to the cosine similarity includes: Obtaining a first loss between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector according to the cosine similarity; Passing the first loss back into the code review model being trained; The code review model in training performs parameter adjustment according to the first loss.
7. A code review device, characterized in that: include: Obtaining unit, used to obtain the code snippet to be reviewed; A code review suggestion generating unit, used for obtaining a code review suggestion for the code fragment to be reviewed according to the code review model; The code review model is used to search for associated words corresponding to the characters included in the code snippet to be reviewed from the words included in the historical code review suggestions, the degree of association between the characters included in the code snippet to be reviewed and the associated words meets a preset degree of association condition, and a code review suggestion is generated for the code snippet to be reviewed according to the associated words; A code review suggestion generating unit is specifically used to obtain a code character embedding vector to be reviewed corresponding to the code fragment to be reviewed; respectively match a plurality of sub-code character embedding vectors included in the code character embedding vector to be reviewed with sub-historical code character embedding vectors included in the historical code fragment character embedding vector in the historical code review suggestion library; obtain a sub-historical code character embedding vector corresponding to the sub-code character embedding vector; obtain a sub-historical code review suggestion word embedding vector corresponding to the sub-historical code character embedding vector; use the sub-historical code review suggestion word embedding vector as an associated word embedding vector corresponding to the sub-code character embedding vector; and generate a code review suggestion word embedding vector whose association with the code character embedding vector to be reviewed meets a preset association condition based on the obtained plurality of associated word embedding vectors.
8. A training device for a code review model, characterized in that: include: A first obtaining unit, configured to obtain a historical code segment and a historical code review suggestion corresponding to the historical code segment; A second obtaining unit is used to obtain a historical code snippet character embedding vector corresponding to the historical code snippet, and a historical code review suggestion word embedding vector corresponding to the historical code review suggestion; A third obtaining unit is used to obtain a first code review suggestion word embedding vector for the historical code snippet character embedding vector according to the initial code review model; a calculation unit, configured to calculate the cosine similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector, wherein the cosine similarity is used to represent the degree of similarity between the first code review suggestion word embedding vector and the historical code review suggestion word embedding vector; A training unit is used to train the initial code review model according to the cosine similarity.
9. An electronic device, characterized in that: include: processor; A memory for storing a method program, wherein when the program is read and executed by a processor, the method according to any one of claims 1 to 6 is executed.
10. A computer storage medium, characterized in that: The computer storage medium stores a computer program, and when the program is executed, the method according to any one of claims 1 to 6 is performed.
Citation Information
Patent Citations
Machine learning-based code auditing method, device, equipment and storage medium
CN110781072A
