Code annotation generation method, device, electronic device, storage medium and product
Automatically generate code annotations through large language model analysis and annotation prompt rules, solving the problem of inefficient manual writing, achieving efficient and accurate code annotations, and improving code readability.
Patent Information
- Application Number
- CN202510608698.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-13
- Publication Date
- 2025-08-15
- Estimated Expiration
- 2045-05-13
AI Technical Summary
In the prior art, code annotations mainly rely on manual writing, which is inefficient and the writing habits and methods of different programmers lead to the burden of understanding and interference, affecting the readability of the code.
By obtaining the target program code, analyzing the semantic information and statement types of code snippets using a large language model, combining pre-constructed annotation prompt rules, appropriate annotation text is automatically generated and inserted, including annotation types of expression statements, variable declaration statements, loop statements, etc.
It realizes fast and efficient code annotation, ensures the accuracy and practicality of the annotation, improves the readability of the code, and reduces the need for manual writing.
Smart Images

Figure CN120144167B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a code comment generation method, and also to a code comment generation device, electronic equipment, non-volatile storage medium, and computer program product. Background Art
[0002] Inline code comments, as an essential component of software documentation, are widely distributed throughout software projects. They describe the code's logic and implementation details in natural language and are essential textual practices in the software development process. They improve the readability of code within software projects and aid developers in understanding and reviewing the code. Therefore, writing comments has become a crucial activity for programmers in their programming tasks. Currently, writing comments for computer program code primarily relies on manual effort by programmers, which is relatively inefficient. Furthermore, due to the varying writing habits and approaches of different programmers, this can easily burden other programmers and interfere with development.
[0003] Therefore, how to achieve fast and efficient code annotation, ensure the accuracy and practicality of the annotation, and effectively improve the readability of the code is an urgent problem to be solved by those skilled in the art. Summary of the Invention
[0004] An object of the present invention is to provide a code comment generation method, which can realize fast and efficient code comment, ensure the accuracy and practicality of the comment, and thus effectively improve the readability of the code; another object of the present invention is to provide a code comment generation device, electronic device, non-volatile storage medium and computer program product, all of which have the above-mentioned beneficial effects.
[0005] In a first aspect, the present invention provides a method for generating code comments, comprising:
[0006] Obtaining target program code and determining comment prompt rules; the comment prompt rules include a correspondence between code statement types and code comment types;
[0007] Analyzing the target program code using a large language model to determine a code segment to be annotated in the target program code and semantic information and a statement type of the code segment to be annotated;
[0008] Determining a comment type corresponding to the code snippet to be annotated according to the comment prompt rule and the statement type of the code snippet to be annotated;
[0009] A target annotation text corresponding to the code snippet to be annotated is generated according to the annotation type and semantic information corresponding to the code snippet to be annotated, and the target annotation text is inserted into the annotation position corresponding to the code snippet to be annotated.
[0010] The target program code is analyzed using a large language model to determine the code fragment to be annotated in the target program code and the semantic information and statement type of the code fragment to be annotated, including:
[0011] Dividing the target program code using the large language model to obtain multiple target code fragments;
[0012] According to the code statement type included in the comment prompt rule, screening and determining the code segment to be annotated and the statement type of the code segment to be annotated in each target code segment;
[0013] Perform semantic analysis on the code snippet to be annotated to obtain semantic information of the code snippet to be annotated.
[0014] The target program code is divided using the large language model to obtain multiple target code fragments, including:
[0015] Performing a grammatical structure analysis on the target program code using the large language model to determine the grammatical structure of the target program code;
[0016] The target program code is divided according to the grammatical structure of the target program code to obtain a plurality of target code segments.
[0017] Wherein, the comment prompt rule also includes the correspondence between code statement type and code comment position;
[0018] Accordingly, inserting the target comment text into the comment position corresponding to the code snippet to be commented on includes:
[0019] Determine the annotation position corresponding to the code snippet to be annotated according to the annotation prompt rule;
[0020] Insert the target annotation text into the annotation position.
[0021] The code statement type includes one or more combinations of expression statements, variable declaration statements, return statements, loop statements, and conditional judgment statements;
[0022] The code comment type includes one or more combinations of implementation function description comments, implementation method description comments, and design decision description comments.
[0023] The process of generating the annotation prompt rules includes:
[0024] Code datasets are collected in computer program projects;
[0025] Extracting and obtaining code annotation pairs from the code dataset;
[0026] Determining a statement type of the code text and a comment type of the comment text in the code comment pair;
[0027] Determining, based on each code-annotation pair, the code text corresponding to each statement type, and the annotation text corresponding to each annotation type, an association relationship between each statement type and each annotation type;
[0028] The annotation prompt rule is generated according to the association relationship between each of the statement types and each of the annotation types.
[0029] The code data set collected in the computer program project includes:
[0030] Identify all computer program projects in the target open source platform;
[0031] Screening all the computer program projects according to preset screening rules to obtain a target computer program project;
[0032] The code data set is collected and obtained in the target computer program project.
[0033] The process of screening all the computer program projects to obtain the target computer program project according to the preset screening rules includes:
[0034] determining the project popularity of each of the computer program projects;
[0035] A preset number of the computer program projects with the highest project popularity are selected as the target computer program projects.
[0036] After selecting a preset number of the computer program projects with the highest project popularity as the target computer program projects, the method further includes:
[0037] determining a target annotation ratio in each of the target computer program projects;
[0038] Target computer program projects whose target annotation ratio is lower than a preset threshold are eliminated.
[0039] After selecting a preset number of the computer program projects with the highest project popularity as the target computer program projects, the method further includes:
[0040] determining a project type of each of the target computer program projects;
[0041] Eliminate target computer program projects whose project types are test projects and / or document projects and / or experimental projects.
[0042] The code annotation pairs extracted from the code dataset include:
[0043] For each computer program code in the code data set, performing syntax analysis on the computer program code to obtain an abstract syntax tree corresponding to the computer program code;
[0044] Determining a comment node in the computer program code according to the abstract syntax tree, and extracting the comment text at the comment node;
[0045] Determine the code text corresponding to the annotation text using a preset matching rule;
[0046] The code-annotation pair is generated using each of the annotation texts and each of the code texts.
[0047] The method further comprises: determining a comment node in the computer program code according to the abstract syntax tree, and extracting the comment text from the comment node;
[0048] Merge the annotation texts according to the preset merging rules;
[0049] Each of the annotation texts is cleaned according to preset cleaning rules.
[0050] The code annotation generation method further includes:
[0051] Determining the positional relationship between each of the annotation nodes and the code text corresponding to the annotation node;
[0052] Constructing a correspondence between a code statement type and a code comment position according to each of the position relationships and the statement type of each of the code texts;
[0053] The correspondence between the code statement type and the code comment position is added to the comment prompt rule.
[0054] Determining the annotation type of the annotation text in the code annotation pair includes:
[0055] Extracting features of the annotation text in the code annotation pair to obtain annotation text features;
[0056] The annotation type of the annotation text is determined according to the annotation text feature.
[0057] The feature extraction of the annotation text in the code annotation pair to obtain annotation text features includes:
[0058] Performing grammatical analysis on the annotation text in the code annotation pair using a preset classification model to obtain each word phrase in the annotation text;
[0059] Performing feature extraction on each of the word phrases to obtain features of each word;
[0060] Generate annotation text features of the annotation text according to each of the word features.
[0061] The annotation prompt rule is generated according to the association relationship between each of the statement types and each of the annotation types, including:
[0062] Dividing all the code comment pairs according to the statement type and the comment type to obtain a plurality of code comment pair sets; each code comment pair set corresponds to the same statement type and the same comment type;
[0063] Using a preset quality analysis tool to evaluate each of the code annotation pairs to obtain each evaluation result;
[0064] The association relationship between each of the statement types and each of the comment types is updated according to each of the evaluation results, and the corresponding relationship between the code statement type and the code comment type is obtained to generate the comment prompt rule.
[0065] In a second aspect, the present invention further discloses a code comment generation device, comprising:
[0066] An acquisition module is used to acquire target program code and determine comment prompt rules; the comment prompt rules include the correspondence between code statement types and code comment types;
[0067] An analysis module is used to analyze the target program code using a large language model to determine a code segment to be annotated in the target program code and semantic information and statement type of the code segment to be annotated;
[0068] A determination module, configured to determine a comment type corresponding to the code snippet to be annotated according to the comment prompt rule and the statement type of the code snippet to be annotated;
[0069] The annotation module is used to generate a target annotation text corresponding to the code snippet to be annotated according to the annotation type and semantic information corresponding to the code snippet to be annotated, and insert the target annotation text into the annotation position corresponding to the code snippet to be annotated.
[0070] In a third aspect, the present invention further discloses an electronic device, comprising:
[0071] memory for storing computer programs;
[0072] A processor is configured to implement the steps of any one of the code comment generation methods described above when executing the computer program.
[0073] In a fourth aspect, the present invention further discloses a non-volatile storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the steps of any one of the code comment generation methods described above are implemented.
[0074] In a fifth aspect, the present invention further discloses a computer program product, comprising a computer program / instruction, which, when executed by a processor, implements the steps of any one of the code comment generation methods described above.
[0075] The present invention provides a code comment generation method, comprising: obtaining target program code and determining comment prompt rules; the comment prompt rules include a correspondence between code statement types and code comment types; using a large language model to analyze the target program code to determine a code fragment to be annotated in the target program code and semantic information and statement types of the code fragment to be annotated; determining the comment type corresponding to the code fragment to be annotated according to the comment prompt rules and the statement type of the code fragment to be annotated; generating a target comment text corresponding to the code fragment to be annotated according to the comment type and semantic information corresponding to the code fragment to be annotated, and inserting the target comment text into the comment position corresponding to the code fragment to be annotated.
[0076] By applying the technical solution provided by the present invention, comment prompt rules are created in advance to indicate the correspondence between code statement types and code comment types, so that different comment type prompts can be given for codes of different statement types; a large language model is created in advance to implement semantic analysis and statement type analysis of the code; thus, for the target program code that needs to be annotated, the large language model can be used to analyze and determine the various code fragments to be annotated and their semantic information and statement types, and then the comment type corresponding to each code fragment to be annotated can be determined in combination with the comment prompt rules, so that the corresponding target comment text can be generated according to the comment type and semantic information corresponding to the code fragment to be annotated and inserted into the corresponding position to complete the automated code annotation. It can be seen that this technical solution does not require manual writing of comments, is faster and more efficient, can effectively ensure the accuracy and practicality of code comments, and further improve the readability of the code.
[0077] The code annotation generation device, electronic device, non-volatile storage medium and computer program product provided by the present invention also have the above-mentioned technical effects, and the present invention will not be described in detail here. BRIEF DESCRIPTION OF THE DRAWINGS
[0078] In order to more clearly illustrate the technical solutions in the prior art and the embodiments of the present invention, the following is a brief introduction to the drawings required for describing the prior art and the embodiments of the present invention. Of course, the drawings related to the embodiments of the present invention described below only describe some of the embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the provided drawings without inventive work, and the obtained other drawings also fall within the scope of protection of the present invention.
[0079] Figure 1 A flowchart of a code comment generation method provided by an embodiment of the present invention;
[0080] Figure 2 This is an overall architecture diagram of a code annotation generation system provided by an embodiment of the present invention;
[0081] Figure 3 A schematic diagram of the structure of a code comment generation device provided by an embodiment of the present invention;
[0082] Figure 4 The present invention provides a schematic structural diagram of an electronic device. DETAILED DESCRIPTION
[0083] The core of the present invention is to provide a code comment generation method, which can realize fast and efficient code annotation, ensure the accuracy and practicality of the annotation, and effectively improve the readability of the code; another core of the present invention is to provide a code comment generation device, electronic device, non-volatile storage medium and computer program product, all of which have the above-mentioned beneficial effects.
[0084] To more clearly and completely describe the technical solutions in the embodiments of the present invention, the technical solutions in the embodiments of the present invention will be introduced below in conjunction with the accompanying drawings of the embodiments of the present invention. Obviously, the embodiments described are only some of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts shall fall within the scope of protection of the present invention.
[0085] An embodiment of the present invention provides a code comment generation method.
[0086] Please refer to Figure 1 , Figure 1 This is a flow chart of a code comment generation method provided by an embodiment of the present invention. The code comment generation method may include the following S101 to S104.
[0087] S101: Obtain target program code and determine comment prompt rules; the comment prompt rules include the correspondence between code statement types and code comment types.
[0088] This step is intended to achieve the acquisition of the target program code and the determination of the comment prompt rules. Among them, the target program code is the computer program code that needs to be annotated, and it can be the computer program code in any development project. The comment prompt rules are used to characterize the correspondence between the code statement type and the code comment type, that is, codes of different statement types can correspond to different types of code comments. For example, for expression statements and variable declaration statements, the comment prompt rules tend to recommend the generation of implementation function description type comments; for return statements and loop statements, the comment prompt rules tend to recommend the generation of design decision description type comments. It should be pointed out that the comment prompt rules are constructed in advance based on large-scale data sample statistics and have high accuracy. The following examples give several different code statement types and code comment types.
[0089] In one embodiment of the present invention, code statement types may include a combination of one or more of expression statements, variable declaration statements, return statements, loop statements (For statements, While statements), and conditional judgment statements (If statements, Switch statements); code comment types may include a combination of one or more of implementation function description comments (what type comments), implementation method description comments (how type comments), and design decision description comments (why type comments).
[0090] It is understandable that the what type annotation is mainly used to describe the core functionality of the code / code snippet, the purpose of the variable / constant, or the calculation purpose of the expression; for example, explaining the result of a complex calculation or the meaning of a key variable. The why type annotation is intended to explain why the code / code snippet is designed in this way, the considerations for choosing a specific implementation method, or the purpose of a certain logical branch; for example, explaining why a specific if check or loop is needed to process a certain type of data. The how type annotation focuses on describing the specific implementation details of the code / code snippet, the algorithm steps, or how to use the code / code snippet correctly; for example, explaining the internal workflow of a complex algorithm or how to call an auxiliary function.
[0091] S102: Analyze the target program code using the large language model to determine the code fragment to be annotated in the target program code and the semantic information and statement type of the code fragment to be annotated.
[0092] This step is intended to implement target program code analysis using a large language model to determine the code snippets to be annotated and their semantic information and statement types. Specifically, a language model can be obtained by training with large-scale data samples. When using the large language model, it can be directly called to input the target program code into the large language model for processing. The output of the model is the code snippets to be annotated in the target program code and the semantic information and statement types of each code snippet to be annotated. The semantic information can be obtained by the large language model through contextual understanding of the target program code, and the statement type can be obtained by the large language model through grammatical structure analysis of the target program code.
[0093] In one embodiment of the present invention, analyzing the target program code using a large language model to determine the code snippets to be annotated in the target program code and the semantic information and statement types of the code snippets to be annotated may include:
[0094] Using a large language model to divide the target program code and obtain multiple target code fragments;
[0095] According to the code statement types included in the comment prompt rules, the code snippets to be annotated and the statement types of the code snippets to be annotated are screened and determined in each target code snippet;
[0096] Perform semantic analysis on the code snippet to be annotated to obtain semantic information of the code snippet to be annotated.
[0097] It is understandable that the target program code is generally composed of multiple target code fragments. However, in a complete target program code, not all target code fragments need to be annotated. As mentioned above, the comment prompt rule is used to characterize the correspondence between the code statement type and the code comment type. In essence, it also prompts the code statement type that generally needs to be annotated in the computer program code. Based on this, the complete target program code can be divided into multiple target code fragments first, and then the large language model is used to filter the code statement types involved in the comment prompt rule in all target code fragments to obtain the code fragments to be annotated, that is, only the target code fragments corresponding to the statement type included in the comment prompt rule are filtered out from all target code fragments, and finally the large language model is used to realize the semantic analysis of each code fragment to be annotated.
[0098] As described above, the statement type may be obtained by performing a grammatical structure analysis on the target program code using a large language model. Therefore, in one possible implementation, using the large language model to segment the target program code to obtain multiple target code segments may include: performing a grammatical structure analysis on the target program code using the large language model to determine the grammatical structure of the target program code; and segmenting the target program code according to the grammatical structure of the target program code to obtain multiple target code segments.
[0099] S103: Determine the comment type corresponding to the code snippet to be annotated according to the comment prompt rule and the statement type of the code snippet to be annotated.
[0100] This step is intended to determine the comment type corresponding to the code snippet to be annotated. Obviously, the comment type is used to indicate which type of comment is suitable for inserting into the corresponding code snippet to be annotated. It can be understood that since the comment prompt rule records the correspondence between the code statement type and the code comment type, after determining the statement type of the code snippet to be annotated, the comment type corresponding to each code snippet to be annotated can be determined by querying the comment prompt rule. It should be pointed out that this step can also be implemented based on the large language model, that is, combined with S102, the target program code and the comment prompt rule are input into the large language model together, so that the large language model can refer to the comment prompt rule to implement the analysis and processing of the target program code. At this time, the output of the model is the semantic information of the code snippet to be annotated and its corresponding comment type.
[0101] S104: Generate target annotation text corresponding to the code snippet to be annotated according to the annotation type and semantic information corresponding to the code snippet to be annotated, and insert the target annotation text into the annotation position corresponding to the code snippet to be annotated.
[0102] This step aims to generate and insert annotation text. Specifically, for each code snippet to be annotated, after determining its semantic information and corresponding annotation type, the corresponding target annotation text can be generated based on this semantic information and annotation type. This target annotation text is then inserted into the annotation location corresponding to the code snippet to be annotated, thus completing the automatic annotation of computer program code.
[0103] In one embodiment of the present invention, the comment prompt rule may further include a correspondence between code statement types and code comment locations; accordingly, inserting the target comment text into the comment location corresponding to the code snippet to be commented on may include:
[0104] Determine the comment position corresponding to the code snippet to be commented according to the comment prompt rules;
[0105] Insert the target annotation text into the annotation location.
[0106] Specifically, in order to achieve accurate insertion of the target comment text in the target program code, the correspondence between the code statement type and the code comment position can be added to the comment prompt rule, that is, codes of different statement types can correspond to code comments in different positions. For example, the comment position corresponding to the expression declaration statement is usually above the expression declaration statement, or it may be on the same line; the comment position corresponding to the conditional statement is usually above the conditional statement. In one possible implementation, the code comment position may include a combination of one or more of the code snippet starting position, the code snippet middle position, and the code snippet end position. Among them, the code snippet starting position refers to the line before the initial line of the code snippet, the code snippet end position refers to the line after the last line of the code snippet, and the code snippet middle position refers to the same line of the code snippet.
[0107] It can be seen that the code comment generation method provided by the embodiment of the present invention pre-creates comment prompt rules for indicating the correspondence between code statement types and code comment types, so that different comment type prompts can be given for codes of different statement types; a large language model is pre-created for implementing semantic analysis and statement type analysis of the code; thus, for the target program code that needs to be annotated, the large language model can be used to analyze and determine the various code snippets to be annotated and their semantic information and statement types, and then the comment type corresponding to each code snippet to be annotated can be determined in combination with the comment prompt rules, so that the corresponding target comment text can be generated according to the comment type and semantic information corresponding to the code snippet to be annotated and inserted into the corresponding position to complete the automated code commenting. It can be seen that this technical solution does not require manual writing of comments, is faster and more efficient, can effectively ensure the accuracy and practicality of code comments, and further improve the readability of the code.
[0108] Based on the above embodiments, this embodiment provides an implementation method for annotation prompt rules. Specifically, the process of generating annotation prompt rules may include:
[0109] S201: Collecting a code data set from a computer program project.
[0110] As mentioned above, the annotation prompt rules are constructed in advance based on large-scale data sample statistics. Therefore, when constructing the annotation prompt rules, it is necessary to collect large-scale data samples. The data sample is a collection of computer program codes with annotations, that is, the above-mentioned code data set, which can be collected from various computer program projects that have been developed or are being developed.
[0111] In one possible implementation, collecting a code dataset from a computer program project may include:
[0112] Identify all computer program projects in the target open source platform;
[0113] Screening all computer program projects according to preset screening rules to obtain a target computer program project;
[0114] A code dataset is collected from a target computer program project.
[0115] This embodiment provides an implementation method for collecting and obtaining a code data set in a computer program project. It is understandable that a large number of computer program projects are generally concentrated in open source platforms, so a portion of applicable target computer program projects can be screened in a specified open source platform to implement the collection of code data sets. Of course, the screening rules for computer program projects and the specific type of target open source platform can be set by technical personnel according to actual needs, and the present invention does not limit this. In one possible implementation method, the target open source platform can specifically be the GitHub open source platform, which is the world's largest open source code hosting platform, gathering a large number of high-quality computer program projects with active development and maintenance specifications.
[0116] Particularly, screening all computer program projects according to preset screening rules to obtain target computer program projects may include: determining the popularity of each computer program project; and selecting a preset number of computer program projects with the highest popularity as target computer program projects.
[0117] This embodiment provides a preset screening rule, that is, multiple computer program projects with higher project popularity can be selected as target computer program projects in the target open source platform. It is understandable that computer program projects with higher project popularity are generally more popular computer program projects. These projects are usually actively developed and may contain more inline comments. Taking the GitHub open source platform as an example, the project popularity ranking of computer program projects can be achieved based on the "star (star, meaning "collection / like") number" (the higher the number of stars, the higher the project popularity), thereby achieving the screening of target computer program projects. Of course, the specific value of the preset number does not affect the implementation of this technical solution, and it can be set according to actual conditions. The present invention does not limit this.
[0118] Furthermore, after selecting a preset number of computer program projects with the highest project popularity as target computer program projects, the method may further include: determining a target annotation ratio in each target computer program project; and eliminating target computer program projects whose target annotation ratio is lower than a preset threshold.
[0119] It is understandable that the comment prompt rule is used to give comment prompts. Therefore, the code data set used to construct the comment prompt rule should be a collection of computer program codes with comments, and the comment content should be relatively large to effectively ensure the accuracy of the comment prompt rule. Based on this, for each target computer program project, its target comment ratio can be counted separately, and some target computer program projects with a low target comment ratio can be eliminated. Similarly, the specific value of the preset threshold does not affect the implementation of the present technical solution, and can be set according to actual conditions. The present invention does not limit this.
[0120] It is worth noting that when used to achieve Chinese annotation generation, the above target annotation ratio should be the Chinese annotation ratio; when used to achieve English annotation generation, the above target annotation ratio should be the English annotation ratio.
[0121] Furthermore, after selecting a preset number of computer program projects with the highest project popularity as target computer program projects, the method may also include: determining the project type of each target computer program project; and eliminating target computer program projects with project types of test projects, document projects, and / or experimental projects.
[0122] This embodiment aims to eliminate toy-type computer program projects (such as the test, documentation, and experiment projects described above). Because toy-type computer program projects differ from development-type computer program projects, the accuracy of annotation prompt rules cannot be effectively guaranteed. During implementation, keyword matching techniques can be used to identify and eliminate toy-type computer program projects. Keywords that can be used for matching include "toy," "test," "experiment," "learn," "exercises," and so on. These keywords are matched against the readme files of each target computer program project. To further ensure accuracy, the readme files and code libraries of each target computer program project after toy-type computer program projects have been eliminated can be manually checked to ensure that the target computer program projects used to collect code datasets are non-toy-type computer program projects, namely, the development-type computer program projects described above.
[0123] S202: Extracting code annotation pairs from the code dataset.
[0124] This step aims to extract code-comment pairs, that is, to extract each code-comment pair from each code data sample in the code dataset. The code-comment pair is a combination of "code" and "comment", which corresponds one to one and can be expressed in the form of <comment, code>.
[0125] In a possible implementation, extracting each code annotation pair from the code dataset may include:
[0126] For each computer program code in the code data set, performing syntax analysis on the computer program code to obtain an abstract syntax tree corresponding to the computer program code;
[0127] Determine a comment node in the computer program code according to the abstract syntax tree, and extract comment text at the comment node;
[0128] Determine the code text corresponding to the comment text using preset matching rules;
[0129] Code-comment pairs are generated using each comment text and each code text.
[0130] This embodiment provides an implementation method for extracting code comment pairs in a code data set. It can be understood that extracting code comment pairs in a code data set is essentially to extract the comment text and the code text corresponding to the comment text from each code data sample (computer program code) in the code data set. On this basis, for each computer program code in the code data set, the comment node can be determined by constructing its corresponding abstract syntax tree. The comment node is the comment position, so that the comment text can be extracted at the comment node; further, the code text corresponding to the comment text is matched using preset matching rules. At this time, the comment text and the code text constitute a code comment pair. Among them, the construction of the abstract syntax tree can be implemented based on the JDT tool (Java Development Tools, a Java development tool) provided by the Eclipse platform (a development platform).
[0131] In addition, the preset matching rules can be specifically rules for the position correspondence between the comment text and the code text, and the code text corresponding to the comment text is determined by position matching. As shown in the above example, the comment position corresponding to the expression declaration statement is usually above the expression declaration statement, or it may be on the same line; the comment position corresponding to the conditional statement is usually above the conditional statement. Generally speaking, the position of the comment text and its corresponding code text are relatively close. On this basis, the following preset matching rules can be referred to: (1) If the comment text and the code text are on the same line (for example, a simple variable declaration code statement), the code text is selected as the corresponding code text of the comment text; (2) If the comment text is written before the code block, usually marked with a left bracket (for example, if{...} statement block), the code block is regarded as the code text corresponding to the comment text; (3) If the comment is on different code lines, all code statements at the same level as the comment text (that is, single-line code statements or code blocks before reaching a blank line or the next comment text) are selected as the code text corresponding to the comment text; (4) If the comment text does not match any of the above three matching rules, for example, the comment text is written on the last line of the code text, the comment text can be ignored.
[0132] Furthermore, after determining the comment nodes in the computer program code according to the abstract syntax tree and extracting the comment texts from the comment nodes, the method may further include: merging the comment texts according to a preset merging rule; and cleaning the comment texts according to a preset cleaning rule.
[0133] First, in actual projects, developers often split a semantically complete comment into multiple lines of " / / " comments. To restore its semantic boundaries, after obtaining the individual comment texts, they can be merged to obtain the complete comment text. In one possible implementation, the merging rules may include: (1) Inline comments consist of consecutive line comments with no blank lines; (2) Inline comments do not consist of consecutive line comments, but the corresponding code consists of multiple lines of single-line code.
[0134] Secondly, in order to effectively improve the quality and effectiveness of comment prompt rules, after obtaining each comment text, it can also be cleaned to obtain applicable comment text. In one possible implementation, the cleaning rules may include: (1) cleaning out empty comments. Such comments do not contain text information related to the code text. Therefore, inline comments containing blank text will be deleted; (2) cleaning out comment text corresponding to the comment code. Such comments are usually abandoned code that has been commented out. Therefore, inline comments containing abandoned source code will be deleted; (3) cleaning out non-English comments / English comments. When used to implement Chinese comment generation, English comments are cleaned out; when used to implement English comment generation, non-English comments are cleaned out. Taking cleaning out non-English comments as an example, non-English comment text can be deleted by checking whether the comment can only be encoded using ASCII characters. Comment text that cannot be encoded will be deleted because it contains characters from other alphabets.
[0135] S203: Determine the statement type of the code text and the comment type of the comment text in the code comment pair.
[0136] This step aims to determine the code text type and comment text type, so as to facilitate the construction of comment prompt rules. As mentioned above, code statement types can include one or more combinations of expression statements, variable declaration statements, return statements, loop statements, and conditional statements; code comment types can include one or more combinations of implementation function description comments, implementation method description comments, and design decision description comments.
[0137] In a possible implementation, determining the comment type of the comment text in the code comment pair may include:
[0138] Extract features from the comment text in the code comment pair to obtain comment text features;
[0139] Determine the annotation type of the annotation text according to the annotation text characteristics.
[0140] Among them, feature extraction of the comment text in the code comment pair to obtain comment text features can include: using a preset classification model to perform grammatical analysis on the comment text in the code comment pair to obtain each word phrase in the comment text; feature extraction of each word phrase to obtain each word feature; and generating comment text features of the comment text based on each word feature.
[0141] Furthermore, the preset classification model can specifically adopt a BERT (Bidirectional Encoder Representation from Transformers) model; the extracted feature types may include but are not limited to: the number of tokens TokenNum in the inline comment, whether the inline comment contains only punctuation marks OnlySymbol, the specific prepositional phrases PrepStr and the number PrepNum in the inline comment, the specific conjunction phrases ConjunStr and the number ConjunNum in the inline comment, the words Keywords in the inline comment that may represent the feature type, and the ratio Ratio of tokens in the inline comment to tokens in the corresponding code.
[0142] Specifically, larger TokenNum and Ratio may indicate that the comment has a higher probability of being an implementation detail or explanation intent type, that is, a How or Why type comment; PrepNum and ConjunNum represent specific relationships (prepositions or conjunctions) in the comment, for example, the word because may indicate a Why type comment; Keywords represent specific words that may indicate a type, for example, the word via may indicate a How type comment; OnlySymbol indicates that the comment contains only symbols, for example, a comment containing only "——" cannot be classified as a What, How, and Why type.
[0143] S204: Determine the association relationship between each statement type and each annotation type based on each code annotation pair, the code text corresponding to each statement type, and the annotation text corresponding to each annotation type.
[0144] S205: Generate annotation prompt rules according to the association relationship between each statement type and each annotation type.
[0145] The above steps are intended to determine the association between statement types and comment types, and then to generate comment prompt rules. It can be imagined that based on the above-mentioned code comment pairs, the code text corresponding to each statement type, and the comment text corresponding to each comment type, the number of code texts corresponding to each statement type and the number of comment texts corresponding to each comment type can be statistically obtained, and then a comprehensive analysis can be performed to obtain the text ratio of each comment type corresponding to each statement type. Taking expression statements as an example, according to the total number of comment texts corresponding to them and the comment type to which the comment texts belong, the ratio of What type comment texts, the ratio of How type comment texts, the ratio of Why type comment texts, and the ratio of Other type comment texts can be obtained respectively. Therefore, the generation of comment prompt rules can be realized by referring to the association between each statement type and each comment type to indicate which statement type of code text is suitable for generating which comment type of comment text.
[0146] In a possible implementation, generating comment prompt rules based on the association between each statement type and each comment type may include:
[0147] Divide all code comment pairs according to statement type and comment type to obtain multiple code comment pair sets; each code comment pair set corresponds to the same statement type and the same comment type;
[0148] Using a preset quality analysis tool to evaluate each code annotation set to obtain each evaluation result;
[0149] The association relationship between each statement type and each comment type is updated according to each evaluation result, and the corresponding relationship between the code statement type and the code comment type is obtained to generate the comment prompt rule.
[0150] To further improve the quality and effectiveness of comment prompt rules, quality assessment can be used to verify and update the relationship between statement types and comment types, so as to effectively improve the accuracy of comment prompt rules. Among them, the preset quality assessment tools used to implement quality assessment can be the PMD tool and the Checkstyle tool (two different static analysis tools), so as to effectively reduce the erroneous analysis caused by a single tool. PMD can check Java source code to identify potential problems and provide improvement suggestions; Checkstyle can statically check Java code according to specified coding conventions. Both tools can evaluate the input Java source code according to a set of rules, and after analysis, output error reports of possible violations in strips.
[0151] Furthermore, the process of constructing the above annotation prompt rules may also include:
[0152] Determine the positional relationship between each comment node and the code text corresponding to the comment node;
[0153] Constructing a correspondence between code statement types and code comment positions based on each position relationship and the statement type of each code text;
[0154] Added the correspondence between code statement types and code comment locations to comment hint rules.
[0155] As described above, in addition to the correspondence between code statement types and code comment types, comment prompt rules can also include the correspondence between code statement types and code comment locations. Based on this, this embodiment provides a method for generating the correspondence between code statement types and code comment locations. It can be imagined that the correspondence between code statement types and code comment locations is similar to the correspondence between code statement types and code comment types, and both can be achieved through big data statistical analysis, which will not be repeated here.
[0156] An embodiment of the present invention provides another method for generating code comments.
[0157] Please refer to Figure 2 , Figure 2 This is the overall architecture diagram of a code comment generation system provided by an embodiment of the present invention. The code comment generation system mainly includes the following four parts: construction of data sample set, statistical analysis of comment location and content, analysis of the correlation between comment content and code quality, and automatic generation of inline comments. Figure 2 The code comment generation system shown in the figure, the implementation process of the code comment generation method provided in this embodiment may include:
[0158] 1. Construction of data sample set.
[0159] 1. Project selection.
[0160] To ensure the representativeness and quality of the annotation data, we can select the GitHub open source platform as the data source, and select Java projects that meet the following criteria as potential experimental projects (target computer program projects):
[0161] (1) Popular projects: Popular projects are usually actively developed and may contain more inline comments. Specifically, the popularity of a project on GitHub can be measured by the number of stars. Java projects are ranked according to the number of stars, and a certain number of the top-ranked projects with the largest number of stars are selected as experimental projects.
[0162] (2) Projects with English annotations: that is, only projects with annotations written in English are considered; specifically, we first check whether the annotations are ASCII-encoded, and then calculate the percentage of all ASCII-encoded annotations in the project. If the percentage exceeds 90%, this project will be considered as an English-annotated project, otherwise it will be considered as a non-English annotated project and deleted from the potential projects.
[0163] (3) Non-toy software development projects: Potential experimental projects need to be mainly software development projects, rather than documentation / experimental / testing projects. Specifically, we can first use heuristic patterns to identify potential toy projects, check whether their readme files contain keywords such as "toy", "test", "experiment", "learn", "exercises", etc., and then manually check the readme files and code base of each obtained project to determine whether it is a toy project.
[0164] Based on the above screening rules, 1000 projects are statistically obtained as experimental projects. The statistical information of these projects is shown in Table 1, which is a project statistical information table provided by an embodiment of the present invention.
[0165] Table 1 Project information statistics
[0166]
[0167] Table 1 shows that 10.5% of methods (the ratio of 599,233 methods with inline comments to the total number of methods, 5,702,126) contain inline comments, while only 2.3% (the ratio of 132,896 methods with both header and inline comments to the total number of methods, 5,702,126) contain both header and inline comments. This means that in 8.2% of methods (the difference between 10.5% and 2.3%), developers rely solely on inline comments for code comprehension. Therefore, exploring the location, content, and automatic generation of inline comments is highly relevant.
[0168] 2. Annotation-code pair extraction.
[0169] The JDT tool provided by the Eclipse platform can construct an abstract syntax tree (AST) of the source code and support identifying comment nodes (such as LineComment and BlockComment) and their attached statement nodes (such as expressions, declarations, and control structures). By traversing the AST, comment nodes can be automatically located and their adjacent code statements retrieved, enabling preliminary <comment, code> extraction.
[0170] To improve the structural integrity and semantic coherence of comments, you can refer to the following rules to merge inline comments and determine the corresponding code of inline comments:
[0171] First, in actual projects, developers often split a semantically complete comment into multiple lines of “ / / ” comments. The following merging rules can be used: (1) Inline comments consist of consecutive line comments without blank lines; (2) Inline comments do not consist of consecutive line comments, but the corresponding code consists of multiple lines of single-line code. Take the following code snippet containing comments as an example:
[0172] "public RGBLuminanceSource(int width, int height, int[]pixels){ ...
[0173] / / In order to measure pure decoding speed, we convert the entireimage to a greyscale array
[0174] / / up front,which is the same as the Y channel of theYUVLuminanceSource in the real app
[0175] / /
[0176] / / Total number of pixels suffices, can ignore shape
[0177] int size= width * height;
[0178] luminances=new byte[size];
[0179] for(int offset0;offset < size; offset++){
[0180] ...}
[0181] }".
[0182] According to the merging rule (1), lines 3, 4, 5, and 6 will be merged into one comment because there are no blank lines between the code lines; according to the merging rule (2), the obtained merged comment will be further merged with lines 7 and 8 to form the final comment because line 7 is empty and the corresponding code consists of multiple single-line codes (lines 9 and 10).
[0183] Secondly, the merged inline comments need to be further accurately matched to the target code fragment they describe. The following heuristic matching method can be used: (1) If the comment and the code are on the same line (such as a simple variable declaration code statement), the code is selected as the corresponding code text of the comment; (2) If the comment is written before the code block, usually marked with a left bracket (for example, an if{...} statement block), the code block is regarded as the code text corresponding to the comment; (3) If the comment is on different code lines, all code statements at the same level as the comment (that is, single-line code statements or code blocks before reaching a blank line or the next comment text) are selected as the code text corresponding to the comment; (4) If the comment does not match any of the above three matching rules, for example, the comment is written on the last line of the code, the comment can be ignored.
[0184] 3. Data cleaning.
[0185] In order to effectively improve the quality and effectiveness of comment prompt rules, the following cleaning rules can be adopted: (1) Clean out empty comments. Such comments do not contain text information related to the code text. Therefore, inline comments containing blank text will be deleted; (2) Clean out the comment text corresponding to the commented code. Such comments are usually abandoned code that has been commented out. Therefore, inline comments containing abandoned source code will be deleted; (3) Clean out non-English comments. Comment text written in non-English can be deleted by checking whether the comment can only be encoded using ASCII characters. Comment text that cannot be encoded will be deleted because it contains characters from other alphabets.
[0186] The above cleaning rules are used because the analysis of comment location and content is focused on this technique. Therefore, even though some comments contain unique content, their locations are still worth exploring. For example, a comment consisting solely of "————" might be used to segment code, so its location is meaningful and won't be treated as noise.
[0187] 2. Statistical analysis of annotation location and content.
[0188] A portion of the data was sampled and subjected to multiple rounds of analysis experiments to classify the annotation locations and content into different categories. Specifically, the analysis experiments were completed manually, and the process was divided into multiple rounds. Specifically, in each round of the experiment, 500 annotation-code pairs were randomly sampled from the dataset without replacement. In the first round, the locations where the annotations appeared in the sample data were classified into different categories. When disagreements arose, the experimental participants discussed until consensus was reached. Then, each participant maintained the same shared category pool and, in the next round of data analysis, selected an existing category from the pool or added a new category to the shared category pool in the next round. This analysis process ended when no new categories were added after three consecutive rounds. Finally, the experiment analyzed the annotation categories from the perspectives of code structure and code syntax. After the analysis experiment was completed, a statistical analysis was performed on the frequency of occurrence of different types of annotation locations.
[0189] Among them, the annotation location category definition:
[0190] (1) The comment is located in the expression statement of the code, that is, the expression statement. This type of comment is usually located above the expression statement, or it may be located on the same line;
[0191] (2) The comment is located in the variable declaration statement of the code, that is, the VariableDeclaration statement. This type of comment is usually located above the variable declaration statement or on the same line.
[0192] (3) The comment is located in the conditional statement of the code, such as the If statement. This type of comment is usually located above the If statement structure;
[0193] (4) The comment is located in the return statement of the code, that is, the Return statement. This type of comment is usually located above the Return statement or on the same line.
[0194] (5) The comment is located in the loop statement of the code, such as the For statement. This type of comment is usually located above the loop statement;
[0195] (6) Notes are located elsewhere.
[0196] Among them, the annotation content category definition:
[0197] (1) What content type inline comments: mainly used to describe the core function of the code / code snippet, the purpose of the variable / constant, or the calculation purpose of the expression; for example, to explain the result of a complex calculation or the meaning of a key variable;
[0198] (2) How content type inline comments: focus on describing the specific implementation details of the code / code snippet, algorithm steps, or how to use the code / code snippet correctly; for example, explaining the internal workflow of a complex algorithm or how to call an auxiliary function;
[0199] (3) Why content type inline comments: intended to explain why the code / code snippet is designed in this way, the considerations for choosing a specific implementation method, or the purpose of a certain logical branch; for example, explaining why a specific if check or loop is needed to process a certain type of data.
[0200] Furthermore, after the sampling and classification is completed, the annotation content of the entire data set is automatically classified, and the annotation content types that appear most frequently in the annotations at different positions are counted. Among them, the feature calculation for automatic classification can use the StanfordParser tool, and the classification model can use the BERT model. Specifically, the Stanford Parser tool can obtain the grammatical composition of the annotation text, which includes verb phrases (VP), noun phrases (NP), etc. In the evaluation of the classification effect, BERT training uses a ten-fold cross-validation method, that is, a piece of data is randomly selected each time as the test set, and the other data is used as the training set. Five traditional indicators can be selected as indicators, namely precision (Precision), recall rate (Recall), F1-score (F1 score), accuracy (Accuracy) and Hamming loss (Hamming Loss). Finally, the experiment counts the annotation content types that appear most frequently in the annotations at different positions. Please refer to Table 2, which is a classification feature table of inline annotations provided in an embodiment of the present invention:
[0201] Table 2 Classification features of inline annotations
[0202]
[0203] Specifically, larger TokenNum and Ratio may indicate that the comment has a higher probability of being an implementation detail or explanation intent type, that is, a How or Why type comment; PrepNum and ConjunNum represent specific relationships (prepositions or conjunctions) in the comment, for example, the word because may indicate a Why type comment; Keywords represent specific words that may indicate a type, for example, the word via may indicate a How type comment; OnlySymbol indicates that the comment contains only symbols, for example, a comment containing only "——" cannot be classified as a What, How, and Why type.
[0204] Finally, in the process of classification using the BERT model, this technical solution achieved 90.0% accuracy, 90.1% recall rate, 90.0% F1-score, 90.0% precision and 0.0921 Hamming loss in the dataset, indicating the effectiveness of the BERT model.
[0205] In addition, the statistical experiment results based on the classification results are shown in Table 3, which is a statistical analysis table of annotation positions and contents provided by an embodiment of the present invention:
[0206] Table 3 Statistical analysis of annotation position and content
[0207]
[0208] Based on Table 3, we can see that: (1) The most frequently appearing comments in the comments covering different code statement locations are What type comments, that is, the comments left by developers in current software projects are mostly comments summarizing the code functions. This is particularly reflected in the comments corresponding to Expression statements, VariableDeclaration statements, and For statements, where the proportion of What type comments is 65.1%, 63.9%, and 67.7%, respectively. (2) There is a tendency in the comments covering different code statement locations. The proportion of What type comments written by developers in If statements and Return statements is relatively low, compared with other code statements, the proportion is 55.8% and 53.3%, respectively. The proportion of How and Why type comments is relatively high, at 23.3% and 20.7%, and 15.8% and 16.2%, respectively. This shows that developers are more inclined to explain code details and code intentions when writing comments corresponding to If statements and Return statements.
[0209] 3. Analysis of the correlation between comment content and code quality.
[0210] After obtaining the comment position and content, it is possible to further confirm whether the comment content at the current position will affect the code quality, that is, the type of comment content recommended for different code snippets, and provide high-quality comment content suggestions for large-scale model generation comments. Therefore, in order to measure whether there is a correlation between different comment contents and code quality metrics, analysis can be performed based on quality analysis tools, such as PMD and Checkstyle tools, to effectively reduce the error analysis caused by a single tool. Both can evaluate the input Java source code according to a set of rules, and after analysis, output error reports of possible violations in strips. It is understandable that the statistical analysis of the relationship between the comment content at different positions and the code quality is reflected in the difference in quality metric results, and provides some insights into prompt word rules. The analysis results based on the quality analysis tool are shown in Tables 4 and 5. Tables 4 and 5 are respectively a PMD tool analysis result information table and a Checkstyle tool analysis result information table provided in an embodiment of the present invention:
[0211] Table 4 PMD tool analysis result information table
[0212]
[0213] Table 5 Checkstyle tool analysis result information table
[0214]
[0215] The metric shown in Tables 4 and 5 is avg_err, calculated as: avg_err = data_num / err_num. This is the ratio of the total data set data_num to the number of errors err_num output by the code quality analysis tool. This indicates the number of code snippets for which an error occurs. In theory, a higher value is better, indicating fewer errors. The total data set refers to the total number of analyzed code snippets, and the number of errors output by the code quality analysis tool refers to the total number of error reports generated by the tool.
[0216] The comment prompt rules obtained based on the analysis of Tables 4 and 5 are as follows: (1) What-type comments have a positive correlation with Expression statements and VariableDeclaration statements. Therefore, in the subsequent comment generation, it is possible to focus on generating What-type comments in these two types of code statements. (2) Why-type comments have a positive correlation with Return statements, For statements, and If statements. Therefore, in the subsequent comment generation, it is possible to focus on generating Why-type comments in these two types of code statements. (3) When performing the task of automatic comment generation, the generation of How-type comments cannot be ignored. That is, all types of code statements may require comments that describe the implementation details of the code snippet or describe how to use the corresponding code snippet.
[0217] 4. Automatic generation of inline comments.
[0218] Based on the hint word rules described above, you can define hint words for comment generation and insert high-quality comments into appropriate code locations. The system has a built-in set of annotation rules to guide the LLM model (Large Language Model) in generating different types of comments. These rules provide biased recommendations based on the correlation analysis between comments and code quality. Specifically:
[0219] 1. What type comments: For statements such as Expression and VariableDeclaration, the rules tend to recommend generating this type of comments;
[0220] 2. Why comments: For control flow or logic key statements such as Return, For (loop), If (conditional judgment), the rules tend to recommend generating this type of comments;
[0221] 3. How type comments: For all types of code statements, the rules will consider suggesting the generation of such comments.
[0222] Finally, the implementation process of the LLM model combining context understanding to generate inline annotation content is as follows:
[0223] 1. Input Processing: The complete function or method body code (target program code) is provided to the LLM along with guiding natural language prompts (annotation prompts). The prompts guide the model to focus on specific code structural features and potential annotation needs, rather than pre-specifying annotation locations or types.
[0224] 2. Deep context understanding and structural analysis:
[0225] (1) Structural analysis: LLM first performs grammatical and structural analysis on the entire code block to understand the function signature, statement sequence, control flow (loops, conditions), data flow, and the logical hierarchy of the code block;
[0226] (2) Semantic understanding: understand the intent and semantics of the code, analyze the purpose and effect of function calls, understand the internal logic and calculation process of complex expressions or algorithms, and judge the importance of code fragments in the overall function and their dependencies with other parts.
[0227] 3. Intelligent Comment Point Identification: LLM leverages its deep contextual understanding capabilities, combined with experimental results highlighting specific statement types. Specifically, the model is guided to prioritize those statements that typically contain core logic, define key data, or control program flow, autonomously identifying the code locations most in need of comments. These types include, but are not limited to: Expression statements, particularly those with complex computations or unclear results; VariableDeclaration statements, particularly those declaring important state variables, configuration parameters, or variables requiring lifecycle or scope clarification; Return statements, explaining the meaning of return values or the reason for specific return conditions; For / While statements, explaining the purpose of the loop, the processed data set, or the key logic within the loop body; If / Switch statements, explaining the reason for the conditional, the branching logic, or coverage of specific scenarios.
[0228] 4. Determine Insertion Location: Once a comment is determined to be necessary, LLM generates high-quality inline comments based on the suggested comment types (What / Why / How) in the prompt word rules. LLM then integrates its deep understanding of the specific context of the code point (variable names, operations involved, preceding and following logic flow, the overall purpose of the code, etc.). For example, for a complex If condition, the prompt word might suggest a Why comment. LLM analyzes the specific components of the condition (variables, comparisons, logical operators) and its role within the overall function logic (e.g., handling a specific error condition or edge case). It then generates a comment similar to " / / Check [specific condition] to handle [specific reason or scenario]" rather than the general " / / Check condition." For another example, for a variable declaration, the prompt word suggests a What comment. LLM analyzes the variable's actual use and impact in subsequent code, generating a comment such as " / / Stores [specific data] for [subsequent key calculation / operation]" rather than simply " / / Declare variable."
[0229] It can be seen that the code comment generation method provided by the embodiment of the present invention pre-creates comment prompt rules for indicating the correspondence between code statement types and code comment types, so that different comment type prompts can be given for codes of different statement types; a large language model is pre-created for implementing semantic analysis and statement type analysis of the code; thus, for the target program code that needs to be annotated, the large language model can be used to analyze and determine the various code fragments to be annotated and their semantic information and statement types, and then the comment type corresponding to each code fragment to be annotated can be determined in combination with the comment prompt rules, so that the corresponding target comment text can be generated according to the comment type and semantic information corresponding to the code fragment to be annotated and inserted into the corresponding position to complete the automated code comment. It can be seen that this technical solution does not require manual writing of comments, is faster and more efficient, can effectively ensure the accuracy and practicality of code comments, and further improve the readability of the code.
[0230] Please refer to Figure 3 , Figure 3 This is a schematic diagram of the structure of a code comment generation device provided by the present invention, which may include:
[0231] Acquisition module 1 is used to acquire target program code and determine comment prompt rules; the comment prompt rules include the correspondence between code statement types and code comment types;
[0232] Analysis module 2, used to analyze the target program code using a large language model to determine the code fragment to be annotated in the target program code and the semantic information and statement type of the code fragment to be annotated;
[0233] Determining module 3, for determining the comment type corresponding to the code snippet to be annotated according to the comment prompt rule and the statement type of the code snippet to be annotated;
[0234] The annotation module 4 is used to generate a target annotation text corresponding to the code snippet to be annotated according to the annotation type and semantic information corresponding to the code snippet to be annotated, and insert the target annotation text into the annotation position corresponding to the code snippet to be annotated.
[0235] It can be seen that the code comment generation device provided by the embodiment of the present invention pre-creates comment prompt rules for indicating the correspondence between code statement types and code comment types, so that different comment type prompts can be given for codes of different statement types; a large language model is pre-created for implementing semantic analysis and statement type analysis of the code; thus, for the target program code that needs to be annotated, the large language model can be used to analyze and determine the various code snippets to be annotated and their semantic information and statement types, and then the comment type corresponding to each code snippet to be annotated can be determined in combination with the comment prompt rules, so that the corresponding target comment text can be generated according to the comment type and semantic information corresponding to the code snippet to be annotated and inserted into the corresponding position to complete the automated code commenting. It can be seen that this technical solution does not require manual writing of comments, is faster and more efficient, can effectively ensure the accuracy and practicality of code comments, and further improve the readability of the code.
[0236] In one embodiment of the present invention, the analysis module 2 may include:
[0237] A partitioning unit, configured to partition the target program code using the large language model to obtain multiple target code fragments;
[0238] A screening unit, configured to screen and determine the code snippets to be annotated and the statement types of the code snippets to be annotated in each target code snippet according to the code statement types included in the annotation prompt rules;
[0239] The analysis unit is used to perform semantic analysis on the code fragment to be annotated to obtain semantic information of the code fragment to be annotated.
[0240] In one embodiment of the present invention, the above-mentioned division unit can be specifically used to use a large language model to perform grammatical structure analysis on the target program code to determine the grammatical structure of the target program code; and divide the target program code according to the grammatical structure of the target program code to obtain multiple target code fragments.
[0241] In one embodiment of the present invention, the comment prompt rule may further include a correspondence between code statement types and code comment locations;
[0242] Accordingly, the annotation module 4 can be specifically used to determine the annotation position corresponding to the code snippet to be annotated according to the annotation prompt rule; and insert the target annotation text into the annotation position.
[0243] In one embodiment of the present invention, the code statement type includes a combination of one or more of expression statements, variable declaration statements, return statements, loop statements, and conditional judgment statements; the code comment type includes a combination of one or more of implementation function description comments, implementation method description comments, and design decision description comments.
[0244] In one embodiment of the present invention, the code comment generating device may further include:
[0245] An acquisition module, used for acquiring a code data set in a computer program project;
[0246] An extraction module is used to extract code annotation pairs from the code dataset;
[0247] A first determining module is used to determine the statement type of the code text and the comment type of the comment text in the code comment pair;
[0248] A second determining module is used to determine the association relationship between each statement type and each annotation type based on each code annotation pair, the code text corresponding to each statement type, and the annotation text corresponding to each annotation type;
[0249] The generation module is used to generate comment prompt rules according to the association relationship between each statement type and each comment type.
[0250] In one embodiment of the present invention, the acquisition module may include:
[0251] A first determining unit is configured to determine all computer program projects in a target open source platform;
[0252] A selection unit, configured to filter all computer program projects to obtain a target computer program project according to a preset screening rule;
[0253] The acquisition unit is used to acquire a code data set from a target computer program project.
[0254] In one embodiment of the present invention, the selection unit may be specifically configured to determine the popularity of each computer program project; and select a preset number of computer program projects with the highest popularity as target computer program projects.
[0255] In one embodiment of the present invention, the selection unit is further configured to determine the target annotation ratio in each target computer program project after selecting a preset number of computer program projects with the highest project popularity as target computer program projects; and eliminate target computer program projects whose target annotation ratio is lower than a preset threshold.
[0256] In one embodiment of the present invention, the selection unit is further configured to determine the project type of each target computer program project after selecting a preset number of computer program projects with the highest project popularity as target computer program projects; and eliminate target computer program projects whose project types are test projects and / or document projects and / or experimental projects.
[0257] In one embodiment of the present invention, the above-mentioned extraction module can be specifically used to perform syntax analysis on each computer program code in the code data set to obtain an abstract syntax tree corresponding to the computer program code; determine the comment nodes in the computer program code based on the abstract syntax tree, and extract the comment text at the comment node; use preset matching rules to determine the code text corresponding to the comment text; and use each comment text and each code text to generate a code comment pair.
[0258] In one embodiment of the present invention, the above-mentioned extraction module can also be used to determine the comment nodes in the computer program code based on the abstract syntax tree, and after extracting the comment text from the comment node, merge the various comment texts according to the preset merging rules; and clean the various comment texts according to the preset cleaning rules.
[0259] In one embodiment of the present invention, the above-mentioned extraction module can also be used to determine the positional relationship between each comment node and the code text corresponding to the comment node; construct the correspondence between the code statement type and the code comment position based on each positional relationship and the statement type of each code text; and add the correspondence between the code statement type and the code comment position to the comment prompt rule.
[0260] In one embodiment of the present invention, the first determining module may include:
[0261] An extraction unit, configured to extract features from the comment text in the code comment pair to obtain comment text features;
[0262] The second determining unit is configured to determine the annotation type of the annotation text according to the annotation text feature.
[0263] In one embodiment of the present invention, the above-mentioned extraction unit can be specifically used to use a preset classification model to perform grammatical analysis on the comment text in the code comment pair to obtain each word phrase in the comment text; perform feature extraction on each word phrase to obtain each word feature; and generate comment text features of the comment text based on each word feature.
[0264] In one embodiment of the present invention, the above-mentioned generation module can be specifically used to divide all code comment pairs according to statement type and comment type to obtain multiple code comment pair sets; each code comment pair set corresponds to the same statement type and the same comment type; each code comment pair set is evaluated using a preset quality analysis tool to obtain each evaluation result; the association relationship between each statement type and each comment type is updated according to each evaluation result to obtain the correspondence between the code statement type and the code comment type to generate comment prompt rules.
[0265] For an introduction to the apparatus provided by the embodiment of the present invention, please refer to the above method embodiment, and the present invention will not be elaborated here.
[0266] An embodiment of the present invention provides an electronic device.
[0267] Please refer to Figure 4 , Figure 4 This is a schematic structural diagram of an electronic device provided by the present invention, which may include:
[0268] Memory 11, for storing computer programs;
[0269] The processor 10 can implement the steps of any of the above-mentioned code comment generation methods when executing a computer program.
[0270] like Figure 4 FIG. 1 is a schematic diagram of the structure of an electronic device, which may include a processor 10, a memory 11, a communication interface 12, and a communication bus 13. The processor 10, the memory 11, and the communication interface 12 communicate with each other via the communication bus 13.
[0271] In the embodiment of the present invention, the processor 10 may be a central processing unit (CPU), an application specific integrated circuit, a digital signal processor, a field programmable gate array, or other programmable logic devices.
[0272] The processor 10 may call a program stored in the memory 11 . Specifically, the processor 10 may execute the operations in the embodiment of the code comment generation method.
[0273] The memory 11 is used to store one or more programs. The programs may include program codes, which include computer operating instructions. In the embodiment of the present invention, the memory 11 stores at least a program for implementing the following functions:
[0274] Obtaining target program code and determining comment prompt rules; the comment prompt rules include the correspondence between code statement types and code comment types;
[0275] Analyze the target program code using a large language model to determine the code fragments to be annotated in the target program code and the semantic information and statement types of the code fragments to be annotated;
[0276] Determine the comment type corresponding to the code snippet to be commented on based on the comment prompt rules and the statement type of the code snippet to be commented on;
[0277] A target annotation text corresponding to the code snippet to be annotated is generated according to the annotation type and semantic information corresponding to the code snippet to be annotated, and the target annotation text is inserted into the annotation position corresponding to the code snippet to be annotated.
[0278] In one possible implementation, the memory 11 may include a program storage area and a data storage area, wherein the program storage area may store an operating system and an application required for at least one function, etc.; the data storage area may store data created during use.
[0279] In addition, the memory 11 may include a high-speed random access memory and may also include a non-volatile memory, such as at least one disk storage device or other volatile solid-state storage device.
[0280] The communication interface 12 may be an interface of a communication module, and is used to connect to other devices or systems.
[0281] Of course, it needs to be explained that Figure 4 The structure shown does not constitute a limitation on the electronic device in the embodiment of the present invention. In actual applications, the electronic device may include Figure 4 More or fewer components than shown, or combinations of certain components.
[0282] An embodiment of the present invention provides a non-volatile storage medium.
[0283] The non-volatile storage medium provided in the embodiment of the present invention stores a computer program, and when the computer program is executed by a processor, the steps of any one of the above-mentioned code comment generation methods can be implemented.
[0284] Among them, non-volatile storage media can be any available media that can be stored by a computer or a data storage device such as a server or data center that includes one or more available media. For example, it can be magnetic media (such as floppy disks, hard disks, tapes, etc.), optical media (such as DVDs) or semiconductor media (such as solid-state drives) and other media that can store computer program codes.
[0285] For an introduction to the non-volatile storage medium provided in an embodiment of the present invention, please refer to the above method embodiment, and the present invention will not be elaborated here.
[0286] An embodiment of the present invention provides a computer program product.
[0287] The computer program product provided by the embodiment of the present invention includes a computer program / instruction. When the computer program / instruction is executed by a processor, the steps of any one of the above-mentioned code comment generation methods can be implemented.
[0288] Specifically, in each of the above embodiments, all or part of the embodiments may be implemented through software, hardware, firmware, or any combination thereof. When implemented through software, all or part of the embodiments may be implemented in the form of a computer program product.
[0289] The computer program product may include one or more computer programs / instructions, which, when loaded and executed on a computer, may generate, in whole or in part, the processes or functions described in accordance with the embodiments of the present invention. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. Computer instructions may be stored in a non-volatile storage medium or transmitted from one non-volatile storage medium to another non-volatile storage medium. For example, computer instructions may be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via a wired (e.g., coaxial cable, optical fiber, digital subscriber line, etc.) or wireless (e.g., infrared, wireless, microwave, etc.) method.
[0290] For an introduction to the computer program product provided by the embodiment of the present invention, please refer to the above method embodiment, and the present invention will not elaborate on it here.
[0291] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on the differences from other embodiments. Reference can be made to the common and similar parts between the various embodiments. For the devices disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the description is relatively simple, and the relevant parts can be referred to the method description.
[0292] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the above description has generally described the components and steps of each example according to their functions. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the present invention.
[0293] The steps of the methods or algorithms described in conjunction with the embodiments disclosed herein may be implemented directly using hardware, a software module executed by a processor, or a combination of the two. The software module may be placed in random access memory (RAM), internal memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.
[0294] The technical solution provided by the present invention is described in detail above. Specific examples are used herein to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core ideas. It should be pointed out that for ordinary technicians in this technical field, without departing from the principles of the present invention, the present invention can also be improved and modified in a number of ways, and these improvements and modifications also fall within the scope of protection of the present invention.
Claims
1. A code comment generation method, characterized in that: include: Obtain target program code and determine comment prompt rules; The comment prompt rules include the corresponding relationship between code statement types and code comment types; The code comment types include one or more combinations of implementation function description comments, implementation method description comments, and design decision description comments; Analyzing the target program code using a large language model to determine a code segment to be annotated in the target program code and semantic information and a statement type of the code segment to be annotated; Determining a comment type corresponding to the code snippet to be annotated according to the comment prompt rule and the statement type of the code snippet to be annotated; Generate a target annotation text corresponding to the code snippet to be annotated according to the annotation type and semantic information corresponding to the code snippet to be annotated, and insert the target annotation text into the annotation position corresponding to the code snippet to be annotated; The process of generating the comment prompt rule includes: collecting a code data set from a computer program project; extracting each code comment pair from the code data set; determining the statement type of the code text and the comment type of the comment text in the code comment pair; determining the association relationship between each statement type and each comment type based on each code comment pair, the code text corresponding to each statement type, and the comment text corresponding to each comment type; and generating the comment prompt rule based on the association relationship between each statement type and each comment type. The method of extracting each code comment pair from the code data set includes: for each computer program code in the code data set, performing syntax analysis on the computer program code to obtain an abstract syntax tree corresponding to the computer program code; determining a comment node in the computer program code according to the abstract syntax tree, and extracting the comment text from the comment node; determining the code text corresponding to the comment text using a preset matching rule; and generating the code comment pair using each comment text and each code text; Determining the annotation type of the annotation text in the code annotation pair includes: performing feature extraction on the annotation text in the code annotation pair to obtain annotation text features; determining the annotation type of the annotation text according to the annotation text features; the annotation text features include the number of tokens in the corresponding annotation text, whether it contains only punctuation marks, specific prepositional phrases and their number, specific conjunction phrases and their number, words representing feature types, and the ratio of tokens in the annotation text to tokens in the corresponding code text; Wherein, according to each of the code comment pairs, the code text corresponding to each of the statement types, and the comment text corresponding to each of the comment types, determining the association relationship between each of the statement types and each of the comment types, including: determining the number of code texts corresponding to each of the statement types and the number of comment texts corresponding to each of the comment types according to each of the code comment pairs, the code text corresponding to each of the statement types, and the comment text corresponding to each of the comment types; determining the text ratio of each comment type corresponding to each of the statement types according to the number of code texts corresponding to each of the statement types and the number of comment texts corresponding to each of the comment types, so as to obtain the association relationship between each of the statement types and each of the comment types; Among them, the comment prompt rule is generated according to the association relationship between each of the statement types and each of the comment types, including: dividing all the code comment pairs according to the statement type and the comment type to obtain multiple code comment pair sets; each code comment pair set corresponds to the same statement type and the same comment type; using a preset quality analysis tool to evaluate each of the code comment pair sets to obtain each evaluation result; the preset quality analysis tool includes a PMD tool and a Checkstyle tool; according to each of the evaluation results of the two preset quality analysis tools, the association relationship between each of the statement types and each of the comment types is updated to obtain the corresponding relationship between the code statement type and the code comment type to generate the comment prompt rule.
2. The code comment generation method according to claim 1, characterized in that: Analyzing the target program code using a large language model to determine a code segment to be annotated in the target program code and semantic information and statement type of the code segment to be annotated, including: Dividing the target program code using the large language model to obtain multiple target code fragments; According to the code statement type included in the comment prompt rule, screening and determining the code segment to be annotated and the statement type of the code segment to be annotated in each target code segment; Perform semantic analysis on the code snippet to be annotated to obtain semantic information of the code snippet to be annotated.
3. The code comment generation method according to claim 2, characterized in that: The target program code is divided using the large language model to obtain multiple target code fragments, including: Performing a grammatical structure analysis on the target program code using the large language model to determine the grammatical structure of the target program code; The target program code is divided according to the grammatical structure of the target program code to obtain a plurality of target code segments.
4. The code comment generation method according to claim 1, characterized in that: The comment prompt rules also include the correspondence between code statement types and code comment positions; Accordingly, inserting the target comment text into the comment position corresponding to the code snippet to be commented on includes: Determine the annotation position corresponding to the code snippet to be annotated according to the annotation prompt rule; Insert the target annotation text into the annotation position.
5. The code comment generation method according to claim 1, characterized in that: The code statement type includes one or more combinations of expression statements, variable declaration statements, return statements, loop statements, and conditional judgment statements.
6. The code comment generation method according to claim 1, characterized in that: The code dataset is collected from the computer program project, including: Identify all computer program projects in the target open source platform; Screening all the computer program projects according to preset screening rules to obtain a target computer program project; The code data set is collected and obtained in the target computer program project.
7. The code comment generation method according to claim 6, characterized in that: Screening all the computer program projects according to the preset screening rules to obtain a target computer program project includes: determining the project popularity of each of the computer program projects; A preset number of the computer program projects with the highest project popularity are selected as the target computer program projects.
8. The code comment generation method according to claim 7, characterized in that: After selecting a preset number of the computer program projects with the highest project popularity as the target computer program projects, the method further includes: determining a target annotation ratio in each of the target computer program projects; Target computer program projects whose target annotation ratio is lower than a preset threshold are eliminated.
9. The code comment generation method according to claim 7, characterized in that: After selecting a preset number of the computer program projects with the highest project popularity as the target computer program projects, the method further includes: determining a project type of each of the target computer program projects; Eliminate target computer program projects whose project types are test projects and / or document projects and / or experimental projects.
10. The code comment generation method according to claim 1, characterized in that: Determining a comment node in the computer program code according to the abstract syntax tree, and extracting the comment text from the comment node, further comprising: Merge the annotation texts according to the preset merging rules; Each of the annotation texts is cleaned according to preset cleaning rules.
11. The code comment generation method according to claim 1, characterized in that: Also includes: Determining the positional relationship between each of the annotation nodes and the code text corresponding to the annotation node; Constructing a correspondence between a code statement type and a code comment position according to each of the position relationships and the statement type of each of the code texts; The correspondence between the code statement type and the code comment position is added to the comment prompt rule.
12. The code comment generation method according to claim 1, characterized in that: Extracting features from the comment text in the code comment pair to obtain comment text features includes: Performing grammatical analysis on the annotation text in the code annotation pair using a preset classification model to obtain each word phrase in the annotation text; Performing feature extraction on each of the word phrases to obtain features of each word; Generate annotation text features of the annotation text according to each of the word features.
13. A code comment generation device, characterized in that: include: An acquisition module is used to acquire target program code and determine comment prompt rules; The comment prompt rules include the correspondence between code statement types and code comment types; the code comment types include one or more combinations of implementation function description comments, implementation method description comments, and design decision description comments; An analysis module is used to analyze the target program code using a large language model to determine a code segment to be annotated in the target program code and semantic information and statement type of the code segment to be annotated; A determination module, configured to determine a comment type corresponding to the code snippet to be annotated according to the comment prompt rule and the statement type of the code snippet to be annotated; An annotation module, configured to generate a target annotation text corresponding to the code snippet to be annotated according to the annotation type and semantic information corresponding to the code snippet to be annotated, and insert the target annotation text into the annotation position corresponding to the code snippet to be annotated; The comment prompt rule generation module is configured to collect a code data set from a computer program project; extract code comment pairs from the code data set; determine the statement type of the code text and the comment type of the comment text in the code comment pair; determine the association between each statement type and each comment type based on each code comment pair, the code text corresponding to each statement type, and the comment text corresponding to each comment type; and generate the comment prompt rule based on the association between each statement type and each comment type. The comment prompt rule generation module is specifically configured to perform syntax analysis on each computer program code in the code data set to obtain an abstract syntax tree corresponding to the computer program code; determine comment nodes in the computer program code according to the abstract syntax tree, and extract the comment text at the comment node; determine the code text corresponding to the comment text using a preset matching rule; and generate the code comment pair using each comment text and each code text; The comment prompt rule generation module is specifically used to extract features of the comment text in the code comment pair to obtain comment text features; Determining the annotation type of the annotation text according to the annotation text features; the annotation text features include the number of tokens in the corresponding annotation text, whether it contains only punctuation, specific prepositional phrases and their number, specific conjunction phrases and their number, words representing feature types, and the ratio of tokens in the annotation text to tokens in the corresponding code text; The comment prompt rule generation module is specifically used to determine the number of code texts corresponding to each statement type and the number of comment texts corresponding to each comment type according to each code comment pair, the code text corresponding to each statement type, and the comment text corresponding to each comment type; determine the text ratio of each comment type corresponding to each statement type according to the number of code texts corresponding to each statement type and the number of comment texts corresponding to each comment type, so as to obtain the association relationship between each statement type and each comment type; The comment prompt rule generation module is specifically used to divide all the code comment pairs according to the statement type and the comment type to obtain multiple code comment pair sets; each code comment pair set corresponds to the same statement type and the same comment type; A preset quality analysis tool is used to evaluate each set of code comments to obtain each evaluation result; the preset quality analysis tool includes a PMD tool and a Checkstyle tool; according to the evaluation results of the two preset quality analysis tools, the association relationship between each statement type and each comment type is updated to obtain the corresponding relationship between the code statement type and the code comment type to generate the comment prompt rule.
14. An electronic device, characterized in that: include: memory for storing computer programs; A processor, configured to implement the steps of the code annotation generating method according to any one of claims 1 to 12 when executing the computer program.
15. A non-volatile storage medium, characterized in that: The non-volatile storage medium stores a computer program, which, when executed by a processor, implements the steps of the code comment generation method according to any one of claims 1 to 12.
16. A computer program product comprising a computer program / instructions, characterized in that When the computer program / instructions are executed by a processor, the steps of the code annotation generation method according to any one of claims 1 to 12 are implemented.
Citation Information
Patent Citations
Source code annotation automatic generation method
CN110399162A
Code annotation generation method, device and equipment based on AI, RPA and AI Agent
CN119045876A