Adaptive code annotation updating method and system, terminal and storage medium
By building abstract syntax trees and using deep learning, graph neural network and other technologies to generate code annotations, the problems of timeliness and accuracy of code annotations in the software development environment are solved, and efficient maintenance and readability of the code is achieved.
Patent Information
- Application Number
- CN202411503013.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-25
- Publication Date
- 2025-03-07
AI Technical Summary
The prior art is difficult to maintain the timeliness and accuracy of code annotations in a rapidly iterative software development environment, resulting in an increase in annotation debt, affecting code quality and user experience.
By constructing an abstract syntax tree, using deep learning models for semantic analysis, semantic vectors are generated, and program dependency graphs are constructed based on these vectors, the graph neural network is used to analyze the dependencies between code elements, and finally generate code annotations using natural language generation models.
Automatic update of code comments is realized, ensuring that the comments are consistent with the actual functions and structure of the code, improving the readability and maintainability of the code, and reducing maintenance costs.
Smart Images

Figure CN119576363A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of software development, and in particular relates to an adaptive code annotation updating method, system, terminal and storage medium. Background Art
[0002] In the complex and lengthy software development process, code comments play a vital role. They are not only the valuable key for developers to understand code logic and grasp program architecture, but also the right-hand man for subsequent maintenance personnel to quickly get started and accurately locate problems. Clear and detailed code comments can greatly promote communication and collaboration between teams and ensure the effective inheritance of project knowledge. However, with the continuous advancement of software projects, frequent changes in requirements, and the continuous expansion and reconstruction of the code base, existing code comments often quickly become outdated and difficult to accurately reflect the actual functions and internal implementation logic of the current code.
[0003] This situation not only weakens the value of comments as "code manuals", but may also mislead maintainers, increase the difficulty of debugging and fixing errors, and thus affect the stability and reliability of the entire software product. What's more difficult is that manually updating these comments is a time-consuming and laborious task, which is easily overlooked or perfunctory during the tight development cycle, leading to inconsistencies between the comments and the actual state of the code, forming the so-called "comment debt". This debt not only reduces development efficiency, but may also introduce new bugs due to comment errors, seriously damaging code quality and user experience.
[0004] Therefore, how to ensure the timeliness and accuracy of code comments in a fast-iterating development environment has become an important issue that needs to be solved. Exploring automated comment generation and update tools, or establishing a more stringent comment maintenance and review mechanism, may be an effective way to alleviate this dilemma. Summary of the invention
[0005] In view of the above-mentioned deficiencies in the prior art, the present invention provides an adaptive code annotation updating method, system, terminal and storage medium to solve the above-mentioned technical problems.
[0006] In a first aspect, the present invention provides an adaptive code comment updating method, comprising: Confirm that there is an update to the code, extract an updated code set, and build an abstract syntax tree for the code combination; Performing semantic analysis on the abstract syntax tree using a deep learning model to generate a semantic vector, and constructing a program dependency graph based on the semantic vector; Analyzing dependency relationships between code elements in the program dependency graph using a graph neural network; A natural language generation model is used to generate code comments based on the semantic vectors and dependencies between code elements.
[0007] In an optional implementation, confirming that there is an update to the code, extracting an updated code set, and constructing an abstract syntax tree for the code set includes: Use a custom script to monitor all submission actions in the code repository. After the custom script is triggered by the submission action, call the script interface to obtain all change records during this round of monitoring. The change record includes a list of newly added, modified or deleted files. Based on the file list, extract code details from the corresponding files. The code details include the changed code line number; An abstract syntax tree is constructed using a parser based on the code file corresponding to the change record and the code detailed information.
[0008] In an optional implementation, constructing an abstract syntax tree based on the code file corresponding to the change record and the code detailed information by using a parser includes: Generate a new version of code based on the code file corresponding to the change record and the code detailed information, and preprocess the new version of code, wherein the preprocessing includes removing dead code, parsing macros and inlining conditional expressions; Using a parser to convert the preprocessed new version code into an abstract syntax tree based on the language rules of the code, in which the code structures of function calls, control flow statements, loops, and conditional judgments are converted into corresponding node types, and the connection relationship between the nodes identifies the logic and sequence relationship of the code; Remove duplicate nodes in the abstract syntax tree.
[0009] In an optional embodiment, the deep learning model is a bimodal pre-trained model, which has 12 layers, each layer has 12 self-attention heads, and the dimension of each self-attention head is 64. The hidden dimension is 768, and the dimension of the FF layer is 3072.
[0010] In an optional embodiment, the pre-training method of the deep learning model includes: Set the input to the concatenation of the code sequence, the natural language sequence and the special token, and set the output to the context vector representation for each token of the code and natural language and the summary sequence representation; Acquire unimodal data and bimodal data from a database, and filter the data sets using preset rules to obtain training data; Given a data point of NL-PL pair (X=w,c) as input, where NL is a natural language word, PL is a code, w is a sequence of NL, c is a sequence of PL tokens, select a random set of positions of NL and PL to be masked, and then replace the tokens at the selected positions with special tokens, where 15% of the tokens in X are replaced; then predict the original tokens that were replaced; First, a data generator is trained with unimodal natural language and code data respectively to generate reasonable alternatives for random mask positions; in addition, there is a discriminator that learns the fusion representation between natural language and code to detect whether a word is the original word; the discriminator is a binary classifier. If the generator produces the correct token, the label of the token is true, otherwise it is false.
[0011] In an optional implementation, using a graph neural network to analyze the dependency relationship between code elements in the program dependency graph includes: Using graph neural networks to capture the topology and features of nodes in a program dependency graph, where the nodes are basic elements of the code, including variables, function calls, or control flow statements; Based on the topological structure and features of the nodes, key parts are identified, where the key parts include loop bodies, conditional branches, and function calls.
[0012] In a second aspect, the present invention provides an adaptive code comment updating system, comprising: A code parsing module, used to confirm that there is an update to the code, extract the updated code set, and construct an abstract syntax tree for the code combination; A relationship parsing module, used to perform semantic analysis on the abstract syntax tree using a deep learning model, generate a semantic vector, and construct a program dependency graph based on the semantic vector; A relationship learning module, used for analyzing the dependency relationship between code elements in the program dependency graph using a graph neural network; The comment generation module is used to generate code comments based on the semantic vector and the dependency relationship between code elements by using a natural language generation model.
[0013] In an optional implementation, the code parsing module includes: An update monitoring unit is used to monitor all submission actions of the code repository using a custom script. After the custom script is triggered by the submission action, the script interface is called to obtain all change records during this round of monitoring. The change record includes a list of newly added, modified or deleted files. Based on the file list, detailed code information is extracted from the corresponding file. The detailed code information includes the changed code line number. The grammar description unit is used to use a parser to construct an abstract syntax tree based on the code file corresponding to the change record and the code detailed information.
[0014] In a third aspect, a terminal is provided, including: A memory for storing an adaptive code annotation update program; A processor is used to implement the steps of the adaptive code annotation updating method provided in the first aspect when executing the adaptive code annotation updating program.
[0015] In a fourth aspect, a computer-readable storage medium is provided, on which an adaptive code comment updating program is stored. When the adaptive code comment updating program is executed by a processor, the steps of the adaptive code comment updating method provided in the first aspect are implemented.
[0016] The beneficial effects of the present invention are that the adaptive code comment updating method, system, terminal and storage medium provided by the present invention can accurately identify the changed code part and its context by using a deep learning model to perform semantic analysis on the abstract syntax tree, build a program dependency graph based on the semantic analysis, and use a graph neural network to mine the program dependency graph, which can further analyze the dependency relationship between code elements, clearly display the structure and logical relationship of the code, and pay attention to the overall architecture of the code and the connection between the various parts, so that when generating comments later, not only comments are generated for the modified code lines, but also comments are updated for other codes affected by the modified code, thereby improving the accuracy of the entire code comment. The automatically updated comments of the present invention ensure the real-time and accuracy of the code document, greatly improve the readability and maintainability of the code, and reduce maintenance costs.
[0017] In addition, the invention has a reliable design principle, a simple structure and a very broad application prospect. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0019] Figure 1 is a schematic flow chart of a method according to an embodiment of the present invention.
[0020] Figure 2 is a schematic block diagram of a system according to an embodiment of the present invention.
[0021] Figure 3 A schematic diagram of the structure of a terminal provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0022] In order to enable those skilled in the art to better understand the technical solutions in the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0023] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art of the present invention. The terms used in the specification of the present invention herein are only for the purpose of describing specific embodiments and are not intended to limit the present invention.
[0024] The adaptive code annotation updating method provided by the embodiment of the present invention is executed by a computer device, and accordingly, the adaptive code annotation updating system runs in the computer device.
[0025] Figure 1 is a schematic flow chart of a method according to an embodiment of the present invention. Figure 1 The execution subject may be an adaptive code comment updating system. According to different requirements, the order of the steps in the flow chart may be changed, and some may be omitted.
[0026] like Figure 1 As shown, the method includes: Step 110, confirming that there is an update to the code, extracting an updated code set, and building an abstract syntax tree for the code set; Step 120, using a deep learning model to perform semantic analysis on the abstract syntax tree, generate a semantic vector, and construct a program dependency graph based on the semantic vector; Step 130, using a graph neural network to analyze the dependency relationship between code elements in the program dependency graph; Step 140: Generate code comments based on the semantic vector and the dependency relationship between code elements by using a natural language generation model.
[0027] To facilitate understanding of the present invention, the following further describes the adaptive code annotation updating method provided by the present invention based on the principle of the adaptive code annotation updating method of the present invention and the process of updating the annotation of the adaptive code in the embodiment.
[0028] Specifically, the adaptive code comment updating method includes: S1. Confirm that there is an update to the code, extract an updated code set, and build an abstract syntax tree for the code combination.
[0029] S101. Use a custom script to monitor all submission actions in the code repository. After the custom script is triggered by the submission action, call the script interface to obtain all change records during this round of monitoring. The change record includes a list of newly added, modified or deleted files. Based on the file list, detailed code information is extracted from the corresponding file. The detailed code information includes the changed code line number.
[0030] Use Git hooks (such as post-commit or pre-push hooks) to monitor all commit actions in the code repository. Whenever a developer submits an update to the code base, the corresponding hook triggers an automated script. The script calls the GitAPI to obtain all change records since the last commit, including a list of newly added, modified, or deleted files. For each changed file, the script further performs a `git diff` operation to extract specific code change details in detail, such as the line number and content of the change. This information is then encapsulated into an event object and passed to subsequent processing modules for more in-depth analysis and comment updates. This process ensures that code changes can be captured quickly and accurately, laying the foundation for efficient comment updates.
[0031] After the code change detection step, the system of the present invention performs detailed extraction operations on the detected changes. This step involves parsing the change events captured by the Git hook to extract specific code change details. The system first determines the type of change, including the addition of new files, modification or deletion of existing files. For each type of change, the system uses Git commands (such as `git diff`) to obtain detailed changes since the last commit. This includes the line number of the change, the specific lines of code added or deleted. The extracted information is organized into a structured data format, such as JSON or XML, in which each file change is recorded in detail. These structured data include not only changes in the code itself, but also metadata of the file, such as file path and author information of the change, providing complete context information for subsequent semantic analysis and graph structure analysis.
[0032] S102. Using a parser, construct an abstract syntax tree based on the code file corresponding to the change record and the code detailed information.
[0033] A new version of code is generated based on the code file corresponding to the change record and the detailed information of the code, and the new version of code is preprocessed, wherein the preprocessing includes removing dead code, parsing macros and inline conditional expressions; the preprocessed new version of code is converted into an abstract syntax tree based on the language rules of the code by using a parser, in which the code structures of function calls, control flow statements, loops and conditional judgments are converted into corresponding node types, and the connection relationship between the nodes identifies the logic and sequence relationship of the code; and duplicate nodes in the abstract syntax tree are removed.
[0034] For each programming language, the system uses a specific parser to process the code. For example, for Java code, the system may use the Java Parser library; for Python code, it may use Python's ast module. These parsers can understand the grammatical rules of the corresponding language and convert the source code into AST. In AST, code structures such as function calls, control flow statements, loops, conditional judgments, etc. are converted into specific node types, and the connection relationship between nodes represents the logic and sequence relationship of the code.
[0035] In the process of building AST, the system also needs to handle various complex programming patterns and language features, such as template metaprogramming, anonymous functions, decorators, etc. In order to improve the accuracy and readability of AST, the system will perform some additional processing steps, including removing dead code, parsing macros, and inline conditional expressions. In addition, the system will optimize AST to reduce its size and improve processing efficiency, such as by merging insignificant nodes or removing duplicate structures.
[0036] During the AST construction process, the system also needs to record important elements in the code, such as variable definitions, function signatures, class and interface declarations, etc. These elements are crucial for subsequent semantic analysis and annotation generation. After the AST is built, the system will use this structured information to perform in-depth code understanding and annotation updates to ensure that the annotations are consistent with the actual function and structure of the code.
[0037] S2. Use a deep learning model to perform semantic analysis on the abstract syntax tree, generate a semantic vector, and construct a program dependency graph based on the semantic vector.
[0038] The deep learning model is a bimodal pre-trained model, such as a CodeBERT model. The bimodal pre-trained model has 12 layers, each layer has 12 self-attention heads, and the dimension of each self-attention head is 64. The hidden dimension is 768, and the dimension of the FF layer is 3072.
[0039] Pre-training methods for deep learning models include: Set the input to the concatenation of the code sequence, the natural language sequence and the special token, and set the output to the context vector representation for each token of the code and natural language and the summary sequence representation; Acquire unimodal data and bimodal data from a database, and filter the data sets using preset rules to obtain training data; Given a data point of NL-PL pair (X=w,c) as input, where NL is a natural language word, PL is a code, w is a sequence of NL, c is a sequence of PL tokens, select a random set of positions of NL and PL to be masked, and then replace the tokens at the selected positions with special tokens, where 15% of the tokens in X are replaced; then predict the original tokens that were replaced; First, a data generator is trained with unimodal natural language and code data respectively to generate reasonable alternatives for random mask positions; in addition, there is a discriminator that learns the fusion representation between natural language and code to detect whether a word is the original word; the discriminator is a binary classifier. If the generator produces the correct token, the label of the token is true, otherwise it is false.
[0040] The trained deep learning model is used to perform semantic analysis on the abstract syntax tree to generate a semantic vector. By embedding the nodes of the AST, the model is able to generate a vector representation that reflects the logic and functionality of the code. This process involves converting code snippets into a series of numerical vectors that can represent their semantics, which capture the structure, logic, and potential business logic of the code. In addition, the system analyzes the control flow and data flow in the code to identify complex program behaviors such as loops, conditional branches, and function calls. In this way, semantic analysis provides a precise semantic basis for the automatic update of code comments, ensuring that the comments closely correspond to the actual functions of the code.
[0041] In a specific example, the semantic analysis process is as follows: Suppose we have a simple Python code as follows: def add(a, b): return a + b.
[0042] The CodeBERT model is used to process the AST of this code and generate the corresponding semantic vector.
[0043] Use a compiler front-end tool (such as Tree-sitter, etc.) to parse this code into an AST. The parsed AST may look like this (in simplified form): FunctionDefinition ├── Name: "add" ├── Parameters │├── Name: "a" │└── Name: "b" └── Body └── ReturnStatement └── BinaryExpression ├── Operator: "+" ├── Left: Variable │└── Name: "a" └── Right: Variable └── Name: "b".
[0044] Convert the AST into a serialization format suitable for input into the CodeBERT model. In this example, convert the AST nodes and edges into a series of tokens or node pairs. For example, one possible serialization method is to convert the AST into a list of strings containing node types and relationships between nodes, as shown below: ["FunctionDefinition", "Name:add", "Parameter:a", "Parameter:b", "ReturnStatement", "BinaryExpression:+", "Left:Variable:a", "Right:Variable:b"].
[0045] Serialize the AST into a format suitable for input into the CodeBERT model. The model receives these inputs and processes them to generate semantic vectors.
[0046] The CodeBERT model processes the input AST internally and uses its pre-trained weights and architecture to capture the semantic information of the code. After processing the input, the model outputs one or more semantic vectors. These vectors are usually represented in a high-dimensional space and can capture the semantic features of the code.
[0047] In this example, assume that the CodeBERT model outputs a 768-dimensional semantic vector (because the CodeBERT model usually has a hidden layer size of 768). This vector can be represented as an array of the following form: [0.123, -0.456, 0.789, ..., -0.234]# This is a 768-dimensional array, only some of the elements are shown.
[0048] After generating the semantic vector, it is necessary to extract the dependencies in the source code. These dependencies may include data dependency, control dependency, etc. Data dependency refers to the assignment and usage relationship between variables, while control dependency refers to the execution order relationship between statements. There are many methods to extract dependencies, such as static analysis, dynamic analysis, data flow analysis, etc. These methods can identify dependencies based on the grammatical structure and semantic information of the source code.
[0049] After extracting the dependencies, we can start to build the program dependency graph. The program dependency graph is a directed graph where nodes represent code snippets (such as statements, expressions, etc.) and edges represent dependencies.
[0050] When building a program dependency graph, nodes and edges can be added to the graph based on the extracted dependencies. At the same time, semantic vectors can be used to enhance the representation of the graph. For example, semantic vectors can be used as attributes of nodes, or semantic vectors can be used to calculate the similarity between nodes, thereby further enriching the structure and information of the graph.
[0051] It can be understood that a program dependency graph (PDG) consists of nodes and edges, where nodes represent basic elements of the code, such as variables, function calls, or control flow statements, and edges represent the dependencies between these elements, including data dependency, control dependency, and call dependency.
[0052] S3. Analyze the dependency relationship between code elements in the program dependency graph using a graph neural network.
[0053] PDG is analyzed using a graph neural network (GNN). GNN is a deep learning model specifically designed to process graph-structured data, which can capture the topological structure and node features of nodes. In the present invention, GNN is used to identify key parts of the code, such as loop bodies, conditional branches, and function calls, and how they are interconnected through dependencies. In addition, GNN can also identify the potential impact of code changes on existing code structures, including newly introduced circular dependencies, broken modular designs, or the impact of changes on code control flow.
[0054] In order to analyze PDG, select a suitable GNN model. In this example, select Graph Convolutional Network (GCN) as the GNN model. GCN is a commonly used graph neural network model that can effectively capture node features and topological structure information in the graph structure.
[0055] Before inputting PDG into the GNN model, some preprocessing is required. This includes: Node feature extraction: For each node in PDG, we need to extract its corresponding features. These features can include the type of node (such as statement, expression, etc.), the semantic vector of the node (generated by CodeBERT model), etc.
[0056] Edge weight setting: For each edge in PDG, we can set its weight according to the strength or importance of the dependency. For example, data dependency and control dependency can be given different weights respectively.
[0057] Next, the preprocessed PDG is input into the GCN model for training. During the training process, tasks such as node classification, graph classification, or link prediction are used to supervise the training of the model. These tasks can help the model learn the node features and topological structure information in the PDG.
[0058] After training, the GNN model is used to identify the key parts in the code. This can be achieved by classifying the nodes in the PDG. For example, key parts such as loop bodies, conditional branches, and function calls are marked as different categories and the GNN model is used to classify the nodes in the PDG.
[0059] In addition to identifying critical parts, GNN models can also be used to analyze the potential impact of code changes on existing code structures. This can be achieved by performing operations such as subgraph matching or graph embedding on PDG. For example, we can use GNN models to calculate the similarity between the changed code and the existing code to identify newly introduced circular dependencies, broken modular designs, or the impact of changes on code control flow.
[0060] S4. Generate code comments based on the semantic vector and the dependencies between code elements using a natural language generation model.
[0061] After gaining a deep understanding of the code changes through semantic analysis and graph structure analysis, the system uses the code semantic vectors and PDG analysis results obtained in the previous steps to construct comment text through a natural language generation (NLG) model.
[0062] The NLG model is responsible for converting the semantic representation of the code into human-readable text. The model uses machine learning technology to learn a large number of code-comment pairs to understand how to generate appropriate comments based on the semantics of the code. When generating comments, the model considers information such as function description, parameter list, return value, and possible error handling. In addition, the model will generate more descriptive comments based on the contextual information of the code, such as variable names and method names.
[0063] The system also designs a set of rule engines to assist in the generation of comments. These rules are based on programming experience and community best practices to guide the style and content of comments. For example, the rule engine may suggest including explanations about complex algorithm logic in comments, or specifying the conditions for the use of specific parameters in comments.
[0064] The generated annotations are first checked by the quality assessment module, which evaluates the clarity, accuracy, and completeness of the annotations. If the annotations do not meet the predetermined quality standards, they are sent back to the NLG model for iterative optimization or provided to developers for manual editing. In this way, the system ensures that the update of annotations is both automated and meets high quality standards.
[0065] Finally, the updated comments will be integrated into the code base as a new commit along with the changed code. This integration process can be automated through continuous integration (CI) tools to ensure that the update of comments is seamlessly connected with code changes, thereby improving the documentation level and maintainability of the entire code base.
[0066] In a specific example, the process of generating annotations is as follows: S401. Integration of code semantic vector and PDG analysis results After obtaining the code semantic vector and PDG analysis results, the system integrates the two. By combining the semantic information and structural information of the code, the system can more comprehensively understand the context and potential impact of the code changes.
[0067] S402. Natural Language Generation (NLG) Model Construction Annotation Text Next, the system uses a natural language generation (NLG) model to construct the comment text. The NLG model has been trained with a large number of code-comment pairs and is able to understand the semantics of the code and generate appropriate comments. When generating comments, the model considers the following factors: Functional description: Accurately describe the function and purpose of the code.
[0068] Parameter list: Lists the function's parameters, their types, and purposes.
[0069] Return value: describes the type and meaning of the function's return value.
[0070] Error handling: Describe the error handling logic and exception conditions in your code.
[0071] In addition, the model generates more descriptive comments based on the contextual information of the code (such as variable names and method names), thereby improving the readability and understanding of the comments.
[0072] S403. Rule engine assisted annotation generation To further improve the quality of annotations, the system also designed a set of rule engines to assist in the generation of annotations. These rules are based on programming experience and community best practices to guide the style and content of annotations. For example, the rule engine may suggest including explanations about complex algorithm logic in the annotations, or specifying the conditions for the use of specific parameters in the annotations. By applying these rules, the system is able to generate more standardized, accurate, and useful annotations.
[0073] S404. Annotation quality assessment and optimization The generated annotations are first checked by the quality assessment module. This module evaluates the clarity, accuracy, and completeness of the annotations to ensure that the annotations meet the predetermined quality standards. If the annotations do not meet these standards, they will be sent back to the NLG model for iterative optimization or provided to the developer for manual editing. In this way, the system can ensure that the generated annotations are both automated and meet high quality standards.
[0074] S405. Annotation Integration and Continuous Integration Finally, the quality-assessed and optimized comments will be integrated into the code base and submitted as a new commit along with the changed code. This integration process can be automated through continuous integration (CI) tools to ensure that comment updates are seamlessly integrated with code changes. In this way, the system can improve the documentation and maintainability of the entire code base, providing developers with clearer, more accurate, and useful code comments.
[0075] The specific implementation method of step S402 is as follows: Collect code-comment pair data: Collect a large number of code files and corresponding comments from open source projects, code repositories, or software libraries. Ensure that the collected data is diverse, covering codes in different programming languages and different fields.
[0076] Data preprocessing: Parse the code and extract key elements such as functions, classes, variables and their attributes (such as type, scope, etc.). Clean the comments to remove irrelevant information and retain key content such as description of code functions, parameter descriptions, and return value explanations. Align the code and comments to ensure that each code snippet has a corresponding comment.
[0077] Select the NLG model architecture: According to the task requirements and data characteristics, select the appropriate NLG model architecture, such as the Transformer-based sequence-to-sequence (Seq2Seq) model, GPT series model, etc. Consider factors such as the model's generative ability, interpretability, and computational efficiency.
[0078] Design model input and output: Input: vector representation of code snippets (which can be obtained through code embedding technology) and possible context information (such as variable names, method names, etc.). Output: generated comment text.
[0079] Define loss function and optimization strategy: Select cross entropy loss and BLEU score as the loss function to evaluate the difference between the generated annotations and the true annotations.
[0080] Design optimization strategies, such as using the Adam optimizer and learning rate decay, to improve the training effect of the model.
[0081] Use the preprocessed code-comment data to train the model. During the training process, monitor the model's loss value, generation quality and other indicators, and adjust the training parameters in time.
[0082] Model evaluation and tuning: Use the validation set to evaluate the model and calculate the accuracy, recall, F1 score and other indicators of the generated annotations. Based on the evaluation results, tune the model, such as adjusting the model architecture, increasing training data, and improving data preprocessing.
[0083] Input code snippet: Input the code snippet to be annotated into the trained NLG model.
[0084] Generate comments: The model generates corresponding comment text based on the input code snippet and context information.
[0085] Post-processing: Post-process the generated comments, such as removing redundant information, adjusting sentence structure, etc., to improve the readability and accuracy of the comments.
[0086] In some embodiments, the adaptive code comment updating system may include multiple functional modules composed of computer program segments. The computer programs of each program segment in the adaptive code comment updating system may be stored in a memory of a computer device and executed by at least one processor to perform (see Figure 1 Description) Functionality for adaptive code comment updates.
[0087] In this embodiment, the adaptive code comment updating system can be divided into multiple functional modules according to the functions it performs, such as Figure 2 As shown. The functional modules of the system 200 may include: a code parsing module 210, a relationship parsing module 220, a relationship learning module 230 and an annotation generation module 240. The module referred to in the present invention refers to a series of computer program segments that can be executed by at least one processor and can complete fixed functions, which are stored in a memory. In this embodiment, the functions of each module will be described in detail in subsequent embodiments.
[0088] A code parsing module, used to confirm that there is an update to the code, extract the updated code set, and construct an abstract syntax tree for the code combination; A relationship parsing module, used to perform semantic analysis on the abstract syntax tree using a deep learning model, generate a semantic vector, and construct a program dependency graph based on the semantic vector; A relationship learning module, used for analyzing the dependency relationship between code elements in the program dependency graph using a graph neural network; The comment generation module is used to generate code comments based on the semantic vector and the dependency relationship between code elements by using a natural language generation model.
[0089] Optionally, as an embodiment of the present invention, the code parsing module includes: An update monitoring unit is used to monitor all submission actions of the code repository using a custom script. After the custom script is triggered by the submission action, the script interface is called to obtain all change records during this round of monitoring. The change record includes a list of newly added, modified or deleted files. Based on the file list, detailed code information is extracted from the corresponding file. The detailed code information includes the changed code line number. The grammar description unit is used to use a parser to construct an abstract syntax tree based on the code file corresponding to the change record and the code detailed information.
[0090] Optionally, as an embodiment of the present invention, using a parser to construct an abstract syntax tree based on the code file corresponding to the change record and the code detailed information includes: Generate a new version of code based on the code file corresponding to the change record and the code detailed information, and preprocess the new version of code, wherein the preprocessing includes removing dead code, parsing macros and inlining conditional expressions; Using a parser to convert the preprocessed new version code into an abstract syntax tree based on the language rules of the code, in which the code structures of function calls, control flow statements, loops, and conditional judgments are converted into corresponding node types, and the connection relationship between the nodes identifies the logic and sequence relationship of the code; Remove duplicate nodes in the abstract syntax tree.
[0091] Optionally, as an embodiment of the present invention, the deep learning model is a bimodal pre-trained model, the bimodal pre-trained model has 12 layers, each layer has 12 self-attention heads, and the dimension of each self-attention head is 64. The hidden dimension is 768, and the dimension of the FF layer is 3072.
[0092] Optionally, as an embodiment of the present invention, the pre-training method of the deep learning model includes: Set the input to the concatenation of the code sequence, the natural language sequence and the special token, and set the output to the context vector representation for each token of the code and natural language and the summary sequence representation; Acquire unimodal data and bimodal data from a database, and filter the data sets using preset rules to obtain training data; Given a data point of NL-PL pair (X=w,c) as input, where NL is a natural language word, PL is a code, w is a sequence of NL, c is a sequence of PL tokens, select a random set of positions of NL and PL to be masked, and then replace the tokens at the selected positions with special tokens, where 15% of the tokens in X are replaced; then predict the original tokens that were replaced; First, a data generator is trained with unimodal natural language and code data respectively to generate reasonable alternatives for random mask positions; in addition, there is a discriminator that learns the fusion representation between natural language and code to detect whether a word is the original word; the discriminator is a binary classifier. If the generator produces the correct token, the label of the token is true, otherwise it is false.
[0093] Optionally, as an embodiment of the present invention, using a graph neural network to analyze the dependency relationship between code elements in the program dependency graph includes: Using graph neural networks to capture the topology and features of nodes in a program dependency graph, where the nodes are basic elements of the code, including variables, function calls, or control flow statements; Based on the topological structure and features of the nodes, key parts are identified, where the key parts include loop bodies, conditional branches, and function calls.
[0094] Figure 3 The present invention is a schematic diagram of the structure of a terminal 300 provided in an embodiment of the present invention. The terminal 300 can be used to execute the adaptive code annotation updating method provided in an embodiment of the present invention.
[0095] The terminal 300 may include: a processor 310, a memory 320 and a communication unit 330. These components communicate via one or more buses. Those skilled in the art will appreciate that the server structure shown in the figure does not limit the present invention, and it may be a bus structure or a star structure, and may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently.
[0096] The memory 320 can be used to store the execution instructions of the processor 310, and the memory 320 can be implemented by any type of volatile or non-volatile storage terminal or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk. When the execution instructions in the memory 320 are executed by the processor 310, the terminal 300 can perform some or all of the steps in the following method embodiments.
[0097] The processor 310 is the control center of the storage terminal, and uses various interfaces and lines to connect various parts of the entire electronic terminal. It runs or executes software programs and / or modules stored in the memory 320, and calls data stored in the memory to perform various functions of the electronic terminal and / or process data. The processor can be composed of an integrated circuit (IC), for example, it can be composed of a single packaged IC, or it can be composed of multiple packaged ICs with the same or different functions. For example, the processor 310 can only include a central processing unit (CPU). In the embodiment of the present invention, the CPU can be a single computing core or multiple computing cores.
[0098] The communication unit 330 is used to establish a communication channel so that the storage terminal can communicate with other terminals, receive user data sent by other terminals or send user data to other terminals.
[0099] The present invention also provides a computer storage medium, wherein the computer storage medium may store a program, and when the program is executed, the program may include some or all of the steps in each embodiment provided by the present invention. The storage medium may be a magnetic disk, an optical disk, a read-only memory (ROM) or a random access memory (RAM).
[0100] Therefore, the present invention can accurately identify the changed code parts and their contexts by using a deep learning model to perform semantic analysis on the abstract syntax tree, build a program dependency graph based on the semantic analysis, and use a graph neural network to mine the program dependency graph, which can further analyze the dependencies between code elements, clearly display the structure and logical relationships of the code, and pay attention to the overall architecture of the code and the connection between the various parts, so that when generating comments later, not only comments are generated for the modified code lines, but also comments are updated for other codes affected by the modified code, thereby improving the accuracy of the entire code comment. The technical effects that can be achieved by this embodiment can be found in the description above, and will not be repeated here.
[0101] Those skilled in the art can clearly understand that the technology in the embodiments of the present invention can be implemented by means of software plus a necessary general hardware platform. Based on this understanding, the technical solution in the embodiments of the present invention, in essence or in other words, the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a disk or an optical disk, and other media that can store program codes, including several instructions for enabling a computer terminal (which can be a personal computer, a server, or a second terminal, a network terminal, etc.) to execute all or part of the steps of the methods described in each embodiment of the present invention.
[0102] In this specification, the same or similar parts between the various embodiments can be referred to each other. In particular, for the terminal embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the description in the method embodiment.
[0103] In the several embodiments provided by the present invention, it should be understood that the disclosed systems and methods can be implemented in other ways. For example, the system embodiments described above are only schematic. For example, the division of the modules is only a logical function division. There may be other division methods in actual implementation, such as multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of systems or modules, which can be electrical, mechanical or other forms.
[0104] The modules described as separate components may or may not be physically separated, and the components shown as modules may or may not be physical modules, that is, they may be located in one place or distributed on multiple network modules. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0105] In addition, each functional module in each embodiment of the present invention may be integrated into one processing module, or each module may exist physically separately, or two or more modules may be integrated into one module.
[0106] Although the present invention has been described in detail with reference to the accompanying drawings and in combination with the preferred embodiments, the present invention is not limited thereto. Without departing from the spirit and essence of the present invention, a person of ordinary skill in the art may make various equivalent modifications or substitutions to the embodiments of the present invention, and these modifications or substitutions shall be within the scope of the present invention. Any person of ordinary skill in the art may easily think of changes or substitutions within the technical scope disclosed by the present invention, and these shall be within the scope of protection of the present invention.
Claims
1. An adaptive code annotation updating method, characterized in that: include: Confirm that there is an update to the code, extract an updated code set, and build an abstract syntax tree for the code combination; Performing semantic analysis on the abstract syntax tree using a deep learning model to generate a semantic vector, and constructing a program dependency graph based on the semantic vector; Analyzing dependency relationships between code elements in the program dependency graph using a graph neural network; A natural language generation model is used to generate code comments based on the semantic vectors and dependencies between code elements.
2. The method according to claim 1, characterized in that Confirm that there is an update to the code, extract the updated code set, and build an abstract syntax tree for the code combination, including: Use a custom script to monitor all submission actions in the code repository. After the custom script is triggered by the submission action, call the script interface to obtain all change records during this round of monitoring. The change record includes a list of newly added, modified or deleted files. Based on the file list, extract code details from the corresponding files. The code details include the changed code line number; An abstract syntax tree is constructed using a parser based on the code file corresponding to the change record and the code detailed information.
3. The method according to claim 2, characterized in that Using a parser to construct an abstract syntax tree based on the code file corresponding to the change record and the code detailed information, including: Generate a new version of code based on the code file corresponding to the change record and the code detailed information, and preprocess the new version of code, wherein the preprocessing includes removing dead code, parsing macros and inlining conditional expressions; Using a parser to convert the preprocessed new version code into an abstract syntax tree based on the language rules of the code, in which the code structures of function calls, control flow statements, loops, and conditional judgments are converted into corresponding node types, and the connection relationship between the nodes identifies the logic and sequence relationship of the code; Remove duplicate nodes in the abstract syntax tree.
4. The method according to claim 1, characterized in that The deep learning model is a bimodal pre-trained model, which has 12 layers, each layer has 12 self-attention heads, and the dimension of each self-attention head is 64. The hidden dimension is 768, and the dimension of the FF layer is 3072.
5. The method according to claim 4, characterized in that The pre-training method of the deep learning model includes: Set the input to the concatenation of the code sequence, the natural language sequence and the special token, and set the output to the context vector representation for each token of the code and natural language and the summary sequence representation; Acquire unimodal data and bimodal data from a database, and filter the data sets using preset rules to obtain training data; Given a data point of NL-PL pair (X=w,c) as input, where NL is a natural language word, PL is a code, w is a sequence of NL, c is a sequence of PL tokens, select a random set of positions of NL and PL to be masked, and then replace the tokens at the selected positions with special tokens, where 15% of the tokens in X are replaced; then predict the original tokens that were replaced; First, a data generator is trained with unimodal natural language and code data respectively to generate reasonable alternatives for random mask positions; in addition, there is a discriminator that learns the fusion representation between natural language and code to detect whether a word is the original word; the discriminator is a binary classifier. If the generator produces the correct token, the label of the token is true, otherwise it is false.
6. The method according to claim 1, characterized in that The dependency relationship between the code elements in the program dependency graph is analyzed using a graph neural network, including: Using graph neural networks to capture the topology and features of nodes in a program dependency graph, where the nodes are basic elements of code, including variables, function calls, or control flow statements; Based on the topological structure and features of the nodes, key parts are identified, including loop bodies, conditional branches, and function calls.
7. An adaptive code comment updating system, characterized in that: include: A code parsing module, used to confirm that there is an update to the code, extract the updated code set, and construct an abstract syntax tree for the code combination; A relationship parsing module, used to perform semantic analysis on the abstract syntax tree using a deep learning model, generate a semantic vector, and construct a program dependency graph based on the semantic vector; A relationship learning module, used for analyzing the dependency relationship between code elements in the program dependency graph using a graph neural network; The comment generation module is used to generate code comments based on the semantic vector and the dependency relationship between code elements by using a natural language generation model.
8. The system according to claim 7, characterized in that The code parsing module includes: An update monitoring unit is used to monitor all submission actions of the code repository using a custom script. After the custom script is triggered by the submission action, the script interface is called to obtain all change records during this round of monitoring. The change record includes a list of newly added, modified or deleted files. Based on the file list, detailed code information is extracted from the corresponding file. The detailed code information includes the changed code line number. The grammar description unit is used to use a parser to construct an abstract syntax tree based on the code file corresponding to the change record and the code detailed information.
9. A terminal, characterized in that: include: A memory for storing an adaptive code annotation update program; A processor, configured to implement the steps of the adaptive code annotation updating method as described in any one of claims 1 to 6 when executing the adaptive code annotation updating program.
10. A computer-readable storage medium storing a computer program, characterized in that: The readable storage medium stores an adaptive code annotation updating program, and when the adaptive code annotation updating program is executed by a processor, the steps of the adaptive code annotation updating method according to any one of claims 1 to 6 are implemented.
Citation Information
Cited By
Code annotation generation method and device, electronic equipment, storage medium and product
CN120144167A
Code reconstruction method and system, computing device, storage medium and program product
CN120315754A
Natural language and code conversion method, system and equipment and storage medium
CN120723207A
Automatic construction method and equipment for fragment-level alignment data and readable storage medium
CN120743282A
Fragment-level alignment data automatic construction method, device and readable storage medium
CN120743282B