A typical case recommendation method based on code comment enhancement
By generating and evaluating code comments, combined with software design documents and a library of typical cases, the problems of manual reliance and readability in aerospace software code review have been solved, achieving efficient code review and defect detection.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- BEIJING JINGHANG COMPUTING & COMM RES INST
- Filing Date
- 2026-04-15
- Publication Date
- 2026-07-21
Smart Images

Figure CN122432036A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of equipment software testing technology, and in particular to a typical case recommendation method based on code annotation enhancement. Background Technology
[0002] Code review is a crucial step in the software testing process for aerospace equipment. Testers systematically review software program code based on relevant standards and software documentation, focusing on the consistency between the code and design, adherence to code execution standards, the correctness of code logic, the rationality of code structure, and code readability. Through decades of engineering practice and the analysis, classification, and refinement of problems discovered during software testing across tens of thousands of projects, aerospace software testing experts have developed semi-structured coding error review criteria and modification suggestions, forming a software testing typical case library. Typically, when new software problems are discovered, new criteria need to be developed and reviewed by domain experts before being added to the library. During testing, testers are generally required to ensure that no similar errors already included in the typical case library are overlooked.
[0003] Currently, code review by testers primarily relies on code reading tools for manual inspection. This method heavily depends on individual experience and knowledge accumulation, involving manual reading of code to identify potential defects and manually consulting a database of typical cases to avoid overlooking common errors. However, due to varying levels of coding standards among software developers, a large amount of code lacks comments or has poorly written comments. Furthermore, the large number of lines of code in aerospace software drastically reduces readability, posing a significant challenge to code review and resulting in extremely low efficiency. Additionally, testers' varying levels of familiarity with the project background and business logic, coupled with their diverse technical experience, require them to constantly recall and compare their knowledge from a vast database of typical cases during code review. This vulnerability to memory lapses or misjudgments can easily lead to missed defects, creating potential software quality issues. Summary of the Invention
[0004] Based on the above analysis, the embodiments of the present invention aim to provide a typical case recommendation method based on code annotation enhancement, in order to solve the problems of low review efficiency and high risk of missed code defects in existing aerospace software code review due to strong reliance on manual review, poor code readability, and low utilization efficiency of typical case library.
[0005] The objective of this invention is mainly achieved through the following technical solutions: This invention provides a typical case recommendation method based on code annotation enhancement, comprising the following steps: Obtain the source code and corresponding software design documents of the software to be reviewed; Based on the software source code and the software design document, generate code comments for the software source code and corresponding comment confidence levels; The generated code comments are evaluated based on the comment confidence level, and the target code comments of the software source code are obtained based on the evaluation results. Based on the matching of the target code annotations with the knowledge blocks in the typical case knowledge base, recommended typical cases are obtained based on the matching results.
[0006] Furthermore, based on the software source code and the software design document, code comments and corresponding comment confidence scores are generated for the software source code, including: The software source code is analyzed to obtain the code entity information of each function. The software design document is analyzed to obtain the structured design information of each function, and a structured function information database is constructed. Establish a mapping relationship between the code entity information of each function and the corresponding function in the structured function information library, and obtain a function mapping set based on the mapping relationship of each function; Based on the function mapping set, code comments and corresponding comment confidence scores are generated for the software source code.
[0007] Furthermore, the code entity information includes a function signature and a function body; the function signature includes a function name, return type, and parameter list; The structured design information includes function definition, function function description, function processing logic, function input, function output, and function return value.
[0008] Furthermore, establishing the mapping relationship between the code entity information of each function and the corresponding function in the structured function information library includes: Using the function name of each function as the search key, perform precise string retrieval in the structured function information database; When structured design information matching the function name is retrieved in the structured function information database, a mapping relationship between the function signature and the structured design information is established.
[0009] Furthermore, generating code comments and corresponding comment confidence scores for the software source code based on the function mapping set includes: for each function in the software source code, performing the following steps: Determine whether the current function exists in the mapping relationship of the structured function information base; When present, a first prompt word is constructed based on the structured design information corresponding to the current function in the function mapping set and the function body of the current function; wherein, the first prompt word includes the first role and task instruction, the function body of the current function, the structured design information and code comment examples; If it does not exist, a second prompt word is constructed based on the function body of the current function; the second prompt word includes the second role and task instructions, the function body of the current function, and code comment examples; Input the first prompt word / second prompt word into the large language model to obtain the code comments of the current function and the corresponding comment confidence.
[0010] Furthermore, the comment confidence corresponding to the code comment is obtained, including: Obtain the conditional probabilities of each lexical unit in the large language model when generating the code annotation; The geometric mean of the conditional probabilities of each obtained lexical unit is used to calculate the annotation confidence level corresponding to the code annotation.
[0011] Furthermore, the evaluation of the generated code comments based on the comment confidence score includes: evaluating the code comments for each function in the software source code using the following method: When the current function exists in the mapping relationship of the structured function information base, perform the following operations: Based on the code comments, function body, and corresponding structured design information of the current function, the content completeness score, writing style score, and requirement compliance score of the code comments of each function are obtained using a preset prompt word template; When the content completeness score, the writing style score, and the requirement compliance score are all greater than or equal to the preset scoring threshold, and the annotation confidence score is greater than or equal to the preset confidence threshold, the evaluation result is set to full adoption. When the writing style score and the requirement compliance score are both greater than or equal to the preset score threshold, while the content completeness score is lower than the preset score threshold and the annotation confidence is greater than or equal to the preset confidence threshold, the evaluation result is set to partial adoption. In all cases other than those mentioned above, the evaluation result will be set as "not accepted". If the current function does not exist in the mapping relationship of the structured function information base, perform the following operation: Based on the code comments and function body of the current function, a content completeness score and a writing style score for the code comments of each function are obtained using a preset prompt word template; When both the content completeness score and the writing style score are greater than or equal to the preset score threshold and the annotation confidence is greater than or equal to the preset confidence threshold, the evaluation result is set to full adoption. Otherwise, set the evaluation result to "not adopted".
[0012] Furthermore, the object code comments obtained based on the evaluation results for the software source code include: When the evaluation result is full acceptance, the code comment is used as the target code comment for the current function; When the evaluation result is partial adoption, the corresponding function-level comments in the structured design information and the line-level code in the code comments are used as the target code comments for the current function; When the evaluation result is not accepted, all comments in the function body of the current function are used as object code comments.
[0013] Furthermore, the abstract syntax tree analysis method is used to parse the software source code to obtain the code entity information of each function; The software design document is parsed using regular expressions to obtain the structured design information of each function.
[0014] Furthermore, the matching of the target code annotations with knowledge blocks in the typical case knowledge base to obtain several typical cases with the highest annotation relevance includes: performing the following operations on each function of the software source code: Extract function-level comments and line-level comments from the target code comments of the current function, perform preprocessing, and then concatenate them to obtain the comment text; Using a pre-trained embedding model, the annotation text of the current function and each knowledge block in the typical knowledge base are converted into corresponding semantic feature vectors. Calculate the similarity value between the semantic feature vector of the annotated text and the semantic feature vector of each knowledge block in the typical knowledge base; A typical example of obtaining the current function's recommendation by sorting the similarity values.
[0015] Compared with the prior art, the present invention can achieve at least one of the following beneficial effects: 1. This invention generates code comments based on software design documents derived from software source code, transforming a large amount of uncommented or low-quality software source code into structured code comments rich in semantic information, thus solving the problem of poor code readability.
[0016] 2. This invention reviews the generated code comments through multi-dimensional quality scoring and confidence scoring, ensuring that the generated comments are highly accurate and meet the design requirements of the software design document. This avoids the illusions that may arise from simply relying on a large model, making the generated code comments usable directly.
[0017] 3. This invention, through semantic vector-based typical case recommendation, can accurately match the software source code to be reviewed with a massive typical case library and recommend the most relevant typical cases, thus avoiding typical errors caused by human memory oversight or lack of experience.
[0018] In this invention, the above-described technical solutions can be combined with each other to achieve more preferred combinations. Other features and advantages of this invention will be set forth in the following description, and some advantages may become apparent from the description or be learned by practicing the invention. The objects and other advantages of this invention can be realized and obtained from what is particularly pointed out in the description and drawings. Attached Figure Description
[0019] The accompanying drawings are for illustrative purposes only and are not intended to limit the invention. Throughout the drawings, the same reference numerals denote the same parts.
[0020] Figure 1 This is a flowchart illustrating a typical case recommendation method based on code annotation enhancement in an embodiment of the present invention. Detailed Implementation
[0021] Preferred embodiments of the present invention will now be described in detail with reference to the accompanying drawings, which form part of this application and are used together with the embodiments of the present invention to illustrate the principles of the present invention, but are not intended to limit the scope of the present invention.
[0022] A specific embodiment of the present invention discloses a typical case recommendation method based on code annotation enhancement, such as... Figure 1 As shown, it includes the following steps S1-S4: Step S1: Obtain the source code of the software to be reviewed and the corresponding software design documents.
[0023] Specifically, the source code of the software to be reviewed is obtained from the file storage path specified by the software version control system, such as Git or SVN. It usually includes at least one project, and each project includes several source code files (such as .c, .cpp, .h, and .java files).
[0024] The software design document is a structured or semi-structured description of the software's system structure, module division, and detailed design of each function, based on the software design specification. It is an authoritative technical document that guides software implementation, testing, and maintenance.
[0025] Step S2: Based on the software source code and the software design document, generate code comments for the software source code and corresponding comment confidence levels.
[0026] Specifically, in the source code of aerospace equipment software, there are common problems such as missing comments, comments being disconnected from the design, or comments of poor quality. In order to accurately and effectively match the unstructured expert knowledge in the typical case library with the implementation of the software source code, it is necessary to generate effective comment content for the software source code based on the comments therein and the corresponding software design documents.
[0027] Furthermore, based on the software source code and the software design document, code comments and corresponding comment confidence scores are generated for the software source code, including the following steps S21-S24: Step S21: Analyze the software source code to obtain the code entity information of each function.
[0028] Specifically, each function in the software source code refers to a code block written in a programming language, defined in the source code through a specific syntax structure, that performs a specific function, and can be independently compiled or analyzed. In this embodiment, it is a basic atomic unit.
[0029] More specifically, unstructured software source code is parsed and processed into a set of structured function information that can be processed by the machine, providing a foundation for subsequent processing.
[0030] The code entity information includes a function signature and a function body; the function signature includes a function name, return type, and parameter list.
[0031] Furthermore, the abstract syntax tree analysis method is used to parse the software source code to obtain the code entity information of each function.
[0032] Specifically, all software source code is traversed. For each code file, the parser of its corresponding programming language is invoked to perform lexical analysis and syntax analysis in sequence. Based on the syntax rules of the programming language, a corresponding Abstract Syntax Tree (AST) is constructed. Each node in the tree represents a syntactic construct in the source code, including but not limited to expressions, statements, and declarations. A depth-first or breadth-first algorithm is used to traverse the AST. Whenever a tree node of type "function definition" is visited, a function entity to be processed is identified. For each function entity node, the code entity information is extracted from its child nodes.
[0033] More specifically, the "Function Identifier" child node is found in the node where the current function is defined, and the string therein is extracted as the function name, serving as the function's unique identifier; the string therein is extracted as the return type, for example, int, long, or char; the string therein is extracted as the parameter list, where each element in the list includes the type and name of a parameter, for example, int a, char b, etc.; and the string therein is extracted as the function body, representing the specific implementation logic of the function.
[0034] The extracted function signatures and function bodies are encapsulated into a structured data object and stored in a collection to obtain a set of code entity information for all functions.
[0035] For example, in a project comprising M software source code files, the code entity information set P is represented as: ; ; in, Indicates the first Software source code, including One function; This represents the j-th function in the i-th software source code; the entire project includes... There are 1 function, each consisting of several lines of code: ; in, This represents the first line of the j-th function in the i-th software source code, i.e., the function definition or declaration line; This represents the total number of lines in the j-th function within the i-th software source code.
[0036] Step S22: Parse the software design document to obtain the structured design information of each function, and build a structured function information database.
[0037] Specifically, the software design document typically includes design descriptions for each function. According to the standards followed by the software design document, such as the "Software Design Specification" standard in GJB 438B-2009, the design specification document is usually organized in a hierarchical structure. For each function or method, the document includes an independent chapter or paragraph as a design description for the function. This description paragraph describes the function's interface, functionality, and logic in the form of fixed metadata fields.
[0038] Furthermore, regular expressions are used to parse the software design document to obtain the structured design information of each function; wherein, the structured design information includes function definition, function function description, function processing logic, function input, function output, and function return value.
[0039] Specifically, regular expressions are used to extract key metadata from the document, and the extracted results are stored in the database in JSON format to obtain a structured function information repository, represented as: ; Where D represents the structured function information base; The function name representing function m; This represents the structured design information of function m; This represents the function definition of function m; Representation function Function description; This represents the function processing logic for function m; and These represent the input and output of function m, respectively. This represents the return value of function m.
[0040] In one specific embodiment, the software design document conforms to the GJB 438B-2009 standard, and the key metadata of a certain function in the "Software Design Specification" is represented as follows: 5.1 Display Function Function definition: void Display(unsigned char x, unsigned char y, unsigned char y, unsigned char y) time) Functionality: Displays the time at a specified location.
[0041] Function processing: The display coordinates will be moved from (0,0) to (4,3), and then the time content will be displayed.
[0042] Input: coordinates (x, y), string "time" Output: coordinates (row, line), string "time" Function returns: None.
[0043] The regular expression for extracting key element data of functions from the software design document is as follows: # Lock Chapter 5 content (?ms)^5(?:\.[0-9]+) \b. ?(?=^\d+\.(?:\d+) \b|\Z) # Match any section within Chapter 5, using a positive lookahead assertion to ensure that the section body contains a "function definition". (?ms)^5\.(?:\d+(?:\.\d+) )[^\n] \n(?=(?:). ?^ Function definition [::]\s [^\n]+))(?P. ?)(?=^\d+\.(?:\d+) \b|\Z) # Perform row-level key-value extraction (?m)^(?P <key>Function definition | Function function | Function processing | Input | Output | Function return) [::]\s (?P <val>.+)$”.
[0044] After parsing and extracting data using the corresponding regular expression, a key-value pair is generated. The structured design information for "Display" is represented as follows: json { Function definition: "Void Display (unsigned char x, unsigned char y, unsigned char time) "Function Description": "Displays a time string at the specified screen coordinates." "Processing logic": "Receive coordinate parameters, calculate the corresponding pixel position, and call the underlying display driver to write the time content." Input: "x-coordinate, y-coordinate, time string pointer 'time'." Output: "The time string that begins to be displayed at the screen position (x, y)." "Function return value": "None" }".
[0045] It should be noted that there is no strict execution order between steps S21 and S22. Those skilled in the art should understand that the processing of the above two branches is logically and temporally independent, and their execution order should not constitute a limitation on the scope of protection of this invention.
[0046] Step S23: Establish the mapping relationship between the code entity information of each function and the corresponding function in the structured function information library, and obtain the function mapping set based on the mapping relationship of each function.
[0047] Specifically, in order to obtain the structured design information corresponding to each function, for the code entity information of each function extracted in step S21, the function name is used as the search key to perform a precise string matching query in the structured function information database constructed in step S22.
[0048] Furthermore, establishing the mapping relationship between the code entity information of each function and the corresponding function in the structured function information library includes: Using the function name of each function as the search key, a precise string search is performed in the structured function information database.
[0049] Specifically, using the function names from the code entity information of each function extracted in step S21 as search criteria, the key-value pairs are searched in the structured function information database D. Perform precise string matching query; wherein, the precise string matching query requires that the function name of the structured design information in the structured function information base D is exactly the same as the query key, including uppercase and lowercase, without performing fuzzy matching, substring matching or semantic similarity calculation.
[0050] When structured design information matching the function name is retrieved in the structured function information database, a mapping relationship between the function signature and the structured design information is established.
[0051] Specifically, if a record with a name exactly matching the function is found in the structured function information database, the retrieval is successful. The code entity information of the matching function is then explicitly associated with the structured design information to form a function mapping set (MAP). ; Where Retrieve() represents the retrieval function; This indicates a null value returned when no corresponding record is found in the information database D.
[0052] If no structured design information matching the function name is found in the structured function information database, no mapping relationship will be established.
[0053] Step S24: Based on the function mapping set, generate code comments and corresponding comment confidence scores for the software source code.
[0054] Specifically, based on the basic instructions and function code segment examples, the function mapping set Map obtained in step S23 is used as a semantic prior constraint. According to the correspondence between each function and its structured information in the design document, a dynamic prompt word template is constructed to guide the large language model to generate enhanced annotations, reduce the illusion of the large model, and provide example data to guide the large model to generate answers similar to the example format and style according to the instructions.
[0055] Furthermore, generating code comments and corresponding comment confidence scores for the software source code based on the function mapping set includes: for each function in the software source code, performing the following steps S241-S243: Step S241: Determine whether the current function exists in the mapping relationship of the structured function information base.
[0056] Specifically, each mapping pair in the function mapping set Map is traversed to determine whether the current function is in the function mapping set Map. When the retrieval is successful, the corresponding structured design information is used as a constraint and input into the large language model along with the function body code to generate highly accurate code comments. When the retrieval fails, that is, the function of the current code does not have corresponding structured design information, the large language model infers and generates comments based on the semantics of the function body code itself, and the source of the comments is clearly indicated.
[0057] Step S242: When it exists, construct a first prompt word based on the structured design information corresponding to the current function in the function mapping set and the function body of the current function; wherein, the first prompt word includes the first role and task instruction, the function body of the current function, the structured design information and code comment examples; If it does not exist, a second prompt word is constructed based on the function body of the current function; the second prompt word includes the second role and task instruction, the function body of the current function, and a second code comment example.
[0058] Specifically, when the current function exists in the mapping relationship of the structured function information base, the generated comments should be strictly consistent with the design document. The constructed first prompt word, including the first role and task instruction, is a basic system instruction that cannot be modified. It defines the role and execution steps of the large language model and clarifies the requirements for comment generation. The function body of the current function is used to provide the large language model with the original information required to generate line-level comments. The structured design information is used to provide the large language model with the factual basis and accuracy constraints for generating function-level comments, ensuring that the generated comments are the same as the original design. The code comment example is used to guide the model to output comments that conform to specific style, terminology, and format requirements through few-sample learning, thereby improving the standardization and usability of the output results.
[0059] It should be noted that by pre-constructing a first prompt word template, efficient and standardized assembly of prompt words can be achieved; wherein, the first prompt word template includes pre-prepared first role and task instructions and code comment examples. For example, the first prompt word template can be: @ Instruction: Your role is an experienced C language code testing engineer.
[0060] Task: Generate function-level comments (function, input, output, processing, return) for a given function CodeBlock(1). (1) Value), and insert at the beginning of the function; (2) Generate line-level comments for each line in the function body to explain its semantics or variable role.
[0061] Requirements: 1. Use the structured information MetadataBlock as the description of the function CodeBlock, and insert the function's starting... 1. Starting position; 2. Based on the function description MetadataBlock and the code semantics, define each line of code within the function body CodeBlock. 3. Generate line-level comments to describe semantic effects or variable functions; 4. Use only those provided in CodeBlock and MetadataBlock. Comments should be generated based on the information and the code semantics of the function itself, and the introduction of any external background / assumptions / domain knowledge is prohibited; 4. If a certain information If the information cannot be determined from the CodeBlock or MetadataBlock, then mark 'TODO: Pending Confirmation' in the corresponding comment location. 5. Refer to the Example for annotation style, and keep it as concise as possible, with consistent terminology and avoid redundant words.
[0062] @CodeBlock: { } @MetadataBlock:{ } @Example: / /
[0063] Function Description: Displays the content and time at a fixed position on the screen.
[0064] Input: The string you want the monitor to display. Processing: Determine the display position and output the string. Output: The string at the specified position / / Void Display(unsigned char x, unsigned char y, unsigned char time) { unsigned char row = 0; / / The row starts at position 0 unsigned char line = 0; / / Column starts at position 0 row = (unsigned char)(x>>4); / / Move the row position 4 pixels to the right. line = (unsigned char)(y>>3); / / Shift the column position 3 pixels to the right. DisplayHZ(row, line, time) / / Content displayed starting from point (3, 4) on the screen } ".
[0065] Replace the function body and corresponding structured design information in the first prompt word template with the function body of the current function. { } and { } The first prompt word is obtained.
[0066] On the other hand, when the current function does not exist in the mapping relationship of the structured function information base, it indicates that there is no relevant design information for the current function in the design document. In this case, it is impossible to obtain the structured design information used to guide and constrain the generation of annotations by the large language model. Therefore, when using the large language model for annotation generation, generation is based solely on the semantics of the function body code itself. The constructed second prompt word includes the second role and task instruction, the function body of the current function, and code annotation examples. Similarly, by pre-constructing the second prompt word template, efficient and standardized assembly of prompt words can be achieved. The second prompt word template includes pre-prepared second role and task instruction and code annotation examples. It is understood that the first prompt word and the second prompt word should have the same code annotation generation form. For example, the second prompt word template can be: @ : Your role is an experienced C language code testing engineer.
[0067] Task: Generate function-level comments (function, input, output, processing, return) for a given function CodeBlock(1). (1) Value), and insert at the beginning of the function; (2) Generate line-level comments for each line in the function body to explain its semantics or variable role.
[0068] Requirements: 1. Based on the code context, generate function-level comments at the beginning of the function `CodeBlock`. Includes functionality, input / output, and processing logic, and marks the first line of the function-level comment with "". No design specifications provided; design based on code. Disconnection generation 2. Based on the code semantics, generate line-level comments for each line of code within the function body's CodeBlock to describe the semantics. Use OR variable functionality; 3. Only use the information provided in the CodeBlock itself and its code semantics to generate comments, prohibiting... 4. Introduce any external context / assumptions / domain knowledge; If information cannot be determined from the CodeBlock, mark it in the corresponding comment position. Note 'TODO: Pending confirmation'; do not fabricate values or infer details; 5. Refer to the Example for comment style, and keep it as concise and use consistent terminology as possible. To avoid redundant words.
[0069] @CodeBlock:{ } @Example: / /
[0070] Function Description: Displays the content and time at a fixed position on the screen.
[0071] Input: The string you want the monitor to display. Processing: Determine the display position and output the string. Output: The string at the specified position / / Void Display(unsigned char x, unsigned char y, unsigned char time) { unsigned char row = 0; / / The row starts at position 0 unsigned char line = 0; / / Column starts at position 0 row = (unsigned char)(x>>4); / / Move the row position 4 pixels to the right. line = (unsigned char)(y>>3); / / Shift the column position 3 pixels to the right. DisplayHZ(row, line, time) / / Content displayed starting from point (3, 4) on the screen } ".
[0072] Replace the function body of the current function in the second prompt word template. { } The second prompt word is obtained.
[0073] Step S243: Input the first prompt word / second prompt word into the large language model to obtain the code comments of the current function and the corresponding comment confidence.
[0074] Specifically, through the application programming interface of the large language model, the first prompt word / second prompt word is input into the large language model. Based on the context provided by the prompt word, the large language model predicts the next most likely word in sequence until a complete code comment text is generated. The generated text will be directly inserted into or mapped to the function body position of the source code to form a code block with comments and output.
[0075] During the process of generating code comments for the current function using a large language model, the determinism of the generation is simultaneously quantified to obtain the comment confidence level corresponding to the generated code comments.
[0076] Furthermore, the comment confidence corresponding to the code comment is obtained, including: Obtain the conditional probabilities of each lexical unit in the large language model when generating the code annotation; The geometric mean of the conditional probabilities of each obtained lexical unit is used to calculate the annotation confidence level corresponding to the code annotation.
[0077] Specifically, each new lexical unit is generated through autoregression in the large language model. At that time, by accessing the model's underlying output interface, the actual lexical units selected and generated by the model can be obtained in real time. The corresponding conditional probability value ,in, These are the lexical units generated at time t; represents the first t-1 generated word sequences; x represents the input prompt word.
[0078] By statistically calculating the conditional probabilities of each word output by the large language model during the generation of the entire annotation sequence, the annotation confidence of each function's code annotation is obtained. The statistical calculation method can be the geometric mean probability as shown below: ; in, This represents the confidence level of the code comment, and its value ranges from 1 to 2. A value closer to 1 indicates that the large language model has a high conditional probability at each step in generating the annotation sequence, meaning the overall generation process is highly deterministic, the annotation content is highly consistent with the input prompts, and the large language model has a low risk of hallucinations. Conversely, a lower annotation confidence value indicates more low-deterministic steps in the generation process, resulting in lower overall annotation reliability. T represents the total number of tokens output by the complete code annotation; P=[P1,P2,…P…] T ] represents a conditional probability sequence.
[0079] To ensure the consistency, controllability, and high confidence of the generated results, specific decoding parameters can be set when calling the large language model. In this embodiment, the temperature is set to a low value (e.g., 0.1) to significantly reduce the randomness of generation, making the model output more focused, deterministic, and reproducible. Combined with Top-K or Top-P sampling (e.g., Top-K=20, Top-P=1.0), the model almost always selects the word with the highest probability at low temperatures, further ensuring the stability and high confidence of the generation.
[0080] Step S3: Evaluate the generated code comments based on the comment confidence level, and obtain the target code comments of the software source code based on the evaluation results.
[0081] Specifically, although a small number of samples have been used to guide the generation of code comments that meet the design requirements when using large language models to generate code comments, there may still be an illusion problem in the code comments generated by large language models. Therefore, it is necessary to add an evaluation and review strategy to ensure the quality of the generated comments.
[0082] Furthermore, the evaluation of the generated code comments based on the comment confidence score includes: evaluating the code comments for each function in the software source code using the following method: When the current function exists in the mapping relationship of the structured function information base, perform the following operations: Specifically, when a function exists in the mapping relationship, it indicates that it has authoritative design information as a benchmark, and therefore a rigorous evaluation strategy is adopted.
[0083] Based on the code comments, function body, and corresponding structured design information of the current function, a content completeness score, writing style score, and requirement compliance score for the code comments of each function are obtained using a preset prompt word template.
[0084] Specifically, for the code comments corresponding to the current function generated by the large language model, and the original comments obtained by splicing the function body in the software source code and the structured design information in the corresponding software design document, three comment quality review prompts based on the large model are proposed: content completeness, writing style, and requirement compliance. These are used to assist manual review of code comments through the large language model. The content integrity score is based on the original comments and determines whether the code comments contain both function-level and line-level comments and cover key logic points.
[0085] The writing style score is based on the original comments and judges whether the code comments conform to the commenting habits of the corresponding code, whether they are concise, and whether they are more fluent and complete.
[0086] The requirement compliance score is based on the original comments to determine whether the code comments accurately and comprehensively reflect the functional, input, output, and processing logic requirements in the software design document.
[0087] More specifically, by pre-constructing a first annotation quality review prompt template, efficient and standardized assembly of prompts can be achieved; the first annotation quality review prompt template can be: " @Instruction: Your role is a code comment quality evaluator.
[0088] Task: Compare the given original commented code blocks in terms of content completeness, writing style, and requirement compliance. Old_comment and the code block New_comment that generates comments, and the generated comments are based on the given design specification Context. The code block `New_comment` provides three quantitative scores. The output must be in strict JSON format containing only three fields. The names and order are as follows: score_completeness, score_style, score_conformity; each field takes a float value. Points, range [0,1]. No explanations, inferences, or additional symbols may be output. No explanatory text or notes may be output. Explanation, additional fields or other symbols If a numerical value cannot be provided, please use null to represent the value of the corresponding field.
[0089] @Input: - Context: { } - Old_comment: {Old_comment} - New_comment: {New_comment} @Evaluation: 1. Content completeness score_completeness: Compare with the original commented code block Old_comment and generate a new comment. Does the code block `New_comment` contain both function-level and line-level comments? Please evaluate the generated commented code block and provide a numerical value. Fraction.
[0090] 2. Writing style score_style: Compares the original commented code block Old_comment to generate a new commented code block. Does `New_comment` conform more to C language commenting conventions? Is it concise and uses consistent terminology? Please evaluate the generated commented code block and provide feedback. Output numerical scores.
[0091] 3. Requirement Conformity (score_conformity): Compares the original commented code block (Old_comment) to generate a new comment code. Does the code block `New_comment` better match the requirement description `Context` and include more details? Please evaluate the generated commented code block. Give a numerical score.
[0092] @Output format (strict JSON only): { "score_completeness":<integer 0-1> , "score_style":<integer 0-1> , "score_conformity":<integer 0-1> } ".
[0093] Replace the first comment quality review prompt template with the current function's code comment. {New_ comment} The first comment quality audit prompt template is replaced with the original comment obtained by concatenating the function body of the current function in the software source code and the structured design information in the corresponding software design document. {Old_ comment} And replace the corresponding structured design information. { } We obtain the first annotation quality review prompt words and obtain various score values through the large language model.
[0094] Furthermore, when the content completeness score, the writing style score, and the requirement compliance score are all greater than or equal to a preset scoring threshold, and the annotation confidence score is greater than or equal to a preset confidence threshold, the evaluation result is set to full adoption.
[0095] Specifically, when all three scores are greater than or equal to the preset score threshold, it indicates that the annotations generated using the large language model are significantly better than the original annotations in terms of content completeness, writing style, and compliance with requirements. Furthermore, the generation process is stable and reliable, and can be fully trusted and adopted.
[0096] It should be noted that the scoring threshold can be flexibly set according to actual engineering needs. For example, in high-reliability software, the threshold can be increased to strictly control illusions. For instance, the scoring threshold can be set to 0.8 and the confidence threshold to 0.9. In legacy system document completion projects, the scoring threshold can be appropriately reduced to improve automation coverage. For instance, the scoring threshold can be set to 0.6 and the confidence threshold to 0.75.
[0097] Furthermore, when the writing style score and the requirement compliance score are both greater than or equal to the preset score threshold, while the content completeness score is lower than the preset score threshold and the annotation confidence is greater than or equal to the preset confidence threshold, the evaluation result is set to partial adoption.
[0098] Specifically, this situation indicates that although the annotations generated by the large language model have insufficient coverage, the generated content is highly reliable and can be partially adopted.
[0099] Furthermore, in all cases other than the two mentioned above, the evaluation result will be set as "not adopted".
[0100] Specifically, if the score or confidence level does not reach the threshold, the quality of the annotation content generated by the large language model is deemed substandard.
[0101] If the current function does not exist in the mapping relationship of the structured function information base, perform the following operation: Based on the code comments and function body of the current function, a content completeness score and a writing style score for the code comments of each function are obtained using a preset prompt word template.
[0102] Specifically, when the current function does not exist in the mapping relationship, the above-mentioned requirement compliance assessment benchmark is lacking. Therefore, a pre-built second annotation quality audit prompt template is used to score the code annotation only for content completeness and writing style.
[0103] More specifically, the template for the second annotation quality review prompt can be: " @Instruction: Your role is a code comment quality evaluator.
[0104] Task: Compare the given original comment code block `Old_comment` in terms of content completeness and writing style. The generated commented code block `New_comment` is evaluated using two quantitative scores. Output It must be a strict JSON format containing only two fields, with the field names and order as follows: score_completeness, score_ `style`; Each field takes a floating-point value, ranging from [0,1]. No explanations, inferences, or additional symbols may be output. No Output any explanatory text, comments, extra fields, or other symbols. If a value cannot be provided, please use null instead. The value of the field.
[0105] @Input: - Old_comment: {Old_comment} - New_comment: {New_comment} @Evaluation: 1. Content completeness score_completeness: Compare with the original commented code block Old_comment and generate a new comment. Does the code block `New_comment` contain both function-level and line-level comments? Please evaluate the generated commented code block and provide a numerical value. Fraction.
[0106] 2. Writing style score_style: Compares the original commented code block Old_comment to generate a new commented code block. Does `New_comment` conform more to C language commenting conventions? Is it concise and uses consistent terminology? Please evaluate the generated commented code block and provide feedback. Output numerical scores.
[0107] @Output format (strict JSON only): { "score_completeness":<integer 0-1> , "score_style":<integer 0-1> } ".
[0108] Replace the code comment in the current function with the second comment quality audit prompt template. {New_ comment} And replacing the function body of the current function in the software source code with the second comment quality audit prompt template. {Old_comment} We obtained the second annotation quality review prompt words and obtained various score values through the large language model.
[0109] Furthermore, when both the content integrity score and the writing style score are greater than or equal to a preset score threshold and the annotation confidence is greater than or equal to a preset confidence threshold, the evaluation result is set to full adoption.
[0110] Otherwise, set the evaluation result to "not adopted".
[0111] Specifically, when both scores are greater than or equal to the preset score threshold, it means that in the absence of design constraints, as long as the generated annotation itself is complete and standardized and the generation process is stable and reliable, it is considered to have value in assisting reading and can be completely trusted and adopted.
[0112] More specifically, if the generated annotations are incomplete or poorly written, they have little auxiliary value and may be erroneous without the need for design content to verify them, and therefore should not be adopted.
[0113] Furthermore, the object code comments obtained based on the evaluation results for the software source code include: When the evaluation result is full acceptance, the code comments are used as the target code comments for the current function.
[0114] Specifically, when the evaluation result is full adoption, the code block of the code comment generated by the large language model is directly used to replace the original comment code block. At this time, both the function-level comments and line-level comments of the function use the content obtained by the large language model, without any content modification or splicing.
[0115] When the evaluation result is partially adopted, the corresponding function-level comments in the structured design information and the line-level code in the code comments are used as the target code comments for the current function.
[0116] Specifically, since partial adoption only occurs when a function exists in the mapping relationship, for function-level comments in code blocks, the content generated by the large language model is discarded, and the function-level comments corresponding to the structured design information of the current function are forcibly extracted and used to ensure that the function-level comments in the generated target code comments are absolutely consistent with the design document, avoiding the illusion that the large language model may produce in key descriptive content; for line-level comments in code blocks, the line-level descriptions generated by the large language model are retained to explain the specific implementation content of the code.
[0117] When the evaluation result is not accepted, all comments in the function body of the current function are used as object code comments.
[0118] Specifically, if the quality of the annotations generated by the large language model is deemed substandard, the update is abandoned, and the source code and its original annotations are fully preserved to ensure that the annotation content is never worse than the original state, thus avoiding the introduction of unreliable or low-quality content and ensuring security.
[0119] Step S4: Match the target code comments with the knowledge blocks in the typical case knowledge base, and obtain recommended typical cases based on the matching results, including: for each function of the software source code, perform the following steps S41-S44: Step S41: Extract function-level comments and line-level comments from the target code comments of the current function, perform preprocessing, and then concatenate them to obtain the comment text.
[0120] Specifically, function-level comments and line-level comments are separated from the target code comments of the current function, and then cleaned and standardized, including but not limited to: removing irrelevant spaces, newlines and special punctuation, and converting the text to a uniform encoding format before concatenating them one by one to obtain the comment text.
[0121] It should be noted that a preset maximum number of comment lines can be set, for example, to 50. If the comment content is too long or contains redundant lines, function-level comments will be retained first, and then lines containing keywords, such as "timing", "interrupt", "memory" and other function-related words, will be selected. If the number of lines containing keywords is still greater than the preset maximum number of comment lines, then these lines will be retained in the order in which they appear in the original function.
[0122] Step S42: Using a pre-trained embedding model, the annotation text of the current function and each knowledge block in the typical knowledge base are converted into corresponding semantic feature vectors.
[0123] Specifically, the pre-trained embedding model is a deep learning model pre-trained on a large-scale general text corpus, such as OpenAI's embedding models text-embedding-3-large and Sentence-BERT, which are models capable of mapping text of arbitrary length into fixed-dimensional numerical vectors containing semantic information. The annotation text of the current function and each knowledge block in a typical knowledge base are vectorized using the same model to ensure semantic space consistency.
[0124] Step S43: Calculate the similarity value between the semantic feature vector of the annotated text and the semantic feature vector of each knowledge block in the typical knowledge base.
[0125] Specifically, the cosine similarity between the semantic feature vector of the function's annotation text and the semantic feature vector of all knowledge blocks is used as the case recommendation score. The higher the score, the higher the relevance between the corresponding knowledge block and the function.
[0126] Step S44: Sort the similarity values to obtain typical cases recommended by the current function.
[0127] Specifically, the similarity scores between the current function and all knowledge blocks are sorted from high to low. From the top of the sorted list, the top K (for example, K=3) knowledge blocks with the highest similarity are selected as typical cases recommended by the system to the current function.
[0128] In summary, the typical case recommendation method based on code annotation enhancement in this embodiment has the following beneficial effects: 1. This embodiment generates code comments based on software design documents derived from software source code, transforming a large amount of uncommented or low-quality software source code into structured code comments rich in semantic information, thus solving the problem of poor code readability.
[0129] 2. This embodiment reviews the generated code comments through multi-dimensional quality scoring and confidence scoring, ensuring that the generated comments are highly accurate and meet the design requirements of the software design document. This avoids the illusion that may be caused by simply relying on a large model, making the generated code comments usable directly.
[0130] 3. This embodiment uses semantic vector-based typical case recommendation to accurately match the software source code to be reviewed with a massive typical case library and recommend the most relevant typical cases, thus avoiding omissions of typical errors caused by human memory oversights or lack of experience.
[0131] Those skilled in the art will understand that all or part of the processes of the methods described in the above embodiments can be implemented by a computer program instructing related hardware, and the program can be stored in a computer-readable storage medium. The computer-readable storage medium may be a disk, optical disk, read-only memory, or random access memory, etc.
[0132] The above description is only a preferred embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any changes or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in the present invention should be included within the scope of protection of the present invention.< / val> < / key>
Claims
1. A typical case recommendation method based on code annotation enhancement, characterized in that, Includes the following steps: Obtain the source code and corresponding software design documents of the software to be reviewed; Based on the software source code and the software design document, generate code comments for the software source code and corresponding comment confidence levels; The generated code comments are evaluated based on the comment confidence level, and the target code comments of the software source code are obtained based on the evaluation results. Based on the matching of the target code annotations with the knowledge blocks in the typical case knowledge base, recommended typical cases are obtained based on the matching results.
2. The method according to claim 1, characterized in that, Based on the software source code and the software design document, generate code comments for the software source code and corresponding comment confidence scores, including: The software source code is analyzed to obtain the code entity information of each function. The software design document is analyzed to obtain the structured design information of each function, and a structured function information database is constructed. Establish a mapping relationship between the code entity information of each function and the corresponding function in the structured function information library, and obtain a function mapping set based on the mapping relationship of each function; Based on the function mapping set, code comments and corresponding comment confidence scores are generated for the software source code.
3. The method according to claim 2, characterized in that, The code entity information includes a function signature and a function body; the function signature includes a function name, return type, and parameter list; The structured design information includes function definition, function function description, function processing logic, function input, function output, and function return value.
4. The method according to claim 3, characterized in that, The process of establishing the mapping relationship between the code entity information of each function and the corresponding function in the structured function information database includes: Using the function name of each function as the search key, perform precise string retrieval in the structured function information database; When structured design information matching the function name is retrieved in the structured function information database, a mapping relationship between the function signature and the structured design information is established.
5. The method according to any one of claims 2-4, characterized in that, The step of generating code comments and corresponding comment confidence scores for the software source code based on the function mapping set includes: for each function in the software source code, performing the following steps: Determine whether the current function exists in the mapping relationship of the structured function information base; When present, a first prompt word is constructed based on the structured design information corresponding to the current function in the function mapping set, and the function body of the current function; wherein, the first prompt word includes the first role and task instruction, the function body of the current function, the structured design information, and code comment examples; If it does not exist, a second prompt word is constructed based on the function body of the current function; the second prompt word includes the second role and task instructions, the function body of the current function, and code comment examples; Input the first prompt word / second prompt word into the large language model to obtain the code comments of the current function and the corresponding comment confidence.
6. The method according to claim 5, characterized in that, The comment confidence score corresponding to the code comment is obtained, including: Obtain the conditional probabilities of each lexical unit in the large language model when generating the code annotation; The geometric mean of the conditional probabilities of each obtained lexical unit is used to calculate the annotation confidence level corresponding to the code annotation.
7. The method according to claim 5, characterized in that, The evaluation of the generated code comments based on the comment confidence score includes: evaluating the code comments for each function in the software source code using the following method: When the current function exists in the mapping relationship of the structured function information base, perform the following operations: Based on the code comments, function body, and corresponding structured design information of the current function, the content completeness score, writing style score, and requirement compliance score of the code comments of each function are obtained using a preset prompt word template; When the content completeness score, the writing style score, and the requirement compliance score are all greater than or equal to the preset scoring threshold, and the annotation confidence score is greater than or equal to the preset confidence threshold, the evaluation result is set to full adoption. When the writing style score and the requirement compliance score are both greater than or equal to the preset score threshold, while the content completeness score is lower than the preset score threshold and the annotation confidence is greater than or equal to the preset confidence threshold, the evaluation result is set to partial adoption. In all cases other than those mentioned above, the evaluation result will be set as "not accepted". If the current function does not exist in the mapping relationship of the structured function information base, perform the following operation: Based on the code comments and function body of the current function, a content completeness score and a writing style score for the code comments of each function are obtained using a preset prompt word template; When both the content completeness score and the writing style score are greater than or equal to the preset score threshold and the annotation confidence is greater than or equal to the preset confidence threshold, the evaluation result is set to full adoption. Otherwise, set the evaluation result to "not adopted".
8. The method according to claim 7, characterized in that, The object code comments obtained based on the evaluation results for the software source code include: When the evaluation result is full acceptance, the code comment is used as the target code comment for the current function; When the evaluation result is partial adoption, the corresponding function-level comments in the structured design information and the line-level code in the code comments are used as the target code comments for the current function; When the evaluation result is not accepted, all comments in the function body of the current function are used as object code comments.
9. The method according to claim 2, characterized in that, The software source code is parsed using the abstract syntax tree analysis method to obtain the code entity information of each function; The software design document is parsed using regular expressions to obtain the structured design information of each function.
10. The method according to claim 1, characterized in that, The process of matching the target code annotations with knowledge blocks in the typical case knowledge base to obtain several typical cases with the highest annotation relevance includes: performing the following operations on each function of the software source code: Extract function-level comments and line-level comments from the target code comments of the current function, perform preprocessing, and then concatenate them to obtain the comment text; Using a pre-trained embedding model, the annotation text of the current function and each knowledge block in the typical knowledge base are converted into corresponding semantic feature vectors. Calculate the similarity value between the semantic feature vector of the annotated text and the semantic feature vector of each knowledge block in the typical knowledge base; A typical example of obtaining the current function's recommendation by sorting the similarity values.