Method and system for code summarization based on hypergraph representation
By transforming source code into an abstract syntax tree and constructing a code hypergraph, and employing a three-step message-passing mechanism in the hypergraph neural network layer, the problem of insufficient code summarization accuracy in existing technologies is solved, generating a more accurate and comprehensive code summary, improving code readability and reducing maintenance costs.
Patent Information
- Application Number
- CN202610725855.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-05-25
- Publication Date
- 2026-08-25
AI Technical Summary
In existing technologies, when using ordinary graphs for code readability classification tasks, the generated code summaries are not very accurate and cannot effectively capture high-order relationships and group interactions.
The source code is transformed into an abstract syntax tree and pruned to construct a code hypergraph. A hypergraph neural network layer with a three-step message passing mechanism is used to aggregate information from the code hypergraph data and generate a code summary.
By using hypergraph representation and a three-step message passing mechanism, the limitations of second-order relations in traditional graphs are overcome, effectively capturing higher-order dependencies and group interactions in the code, generating more accurate and comprehensive code summaries, improving code readability and reducing software development and maintenance costs.
Smart Images

Figure CN122633238A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of code data processing technology, and more specifically, to a code digest generation method and system based on hypergraph representation. Background Technology
[0002] Source code summarization is a task that involves creating a concise natural language (NL) description of a code snippet. It allows programmers to understand the purpose of the code without having to read it. Code summarization improves code readability, which is crucial for reducing maintenance overhead.
[0003] The drawback of using ordinary graphs to represent code is that an edge in an ordinary graph can only connect two vertices, and can only represent second-order relationships. It cannot represent higher-order relationships (such as one vertex interacting with multiple vertices), such as multiple variables participating in shared data flow logic or synchronous updates within a control block. This limits its ability to capture group interactions, resulting in poor accuracy of code summaries generated when using ordinary graphs for code readability classification tasks. Summary of the Invention
[0004] The problem that this invention aims to solve is that the accuracy of the code summaries generated when using ordinary graphs for code readability classification tasks is not good.
[0005] To address the above problems, in a first aspect, the present invention provides a code digest generation method based on hypergraph representation, comprising: Transform the source code into an abstract syntax tree and then prune the abstract syntax tree; Construct a code hypergraph using the pruned abstract syntax tree; Based on the constructed code hypergraph, the code hypergraph data is input into the trained summary generation model to obtain code summaries. The summary generation model includes an encoding layer, which includes a hypergraph neural network layer. The hypergraph neural network layer uses a three-step message passing mechanism to aggregate information from the code hypergraph data.
[0006] Optionally, the code hypergraph includes parent-child hyperedges, data flow hyperedges, row-level hyperedges, and block hyperedges. Parent-child hyperedges connect a node and all its child nodes to enable higher-order interactions between the parent and child nodes. Data flow hyperedges connect a defining node and all its user nodes, as well as a user node and its user defining node. Row-level hyperedges connect all leaf nodes belonging to the same row, unit by line of source code sequence. Block hyperedges connect nodes whose terms together constitute a control flow block.
[0007] Optionally, the encoding layer further includes an activation layer and a residual connection and normalization layer, wherein the hypergraph neural network layer, the activation layer, and the residual connection and normalization layer are connected in sequence; The summary generation model also includes an embedding layer, a decoding layer, and a classifier. Multiple encoding layers are connected in sequence, and multiple decoding layers are connected in sequence. The output of the embedding layer is connected to the input of the first encoding layer and the input of the first decoding layer, respectively. The output of the last decoding layer is connected to the input of the classifier. The decoding layer includes a masked multi-head attention layer, an encoder-decoder multi-head attention layer and a feedforward neural network layer connected in sequence, and the output of the encoder layer is connected to the input of the encoder-decoder multi-head attention layer; The output of the classifier is connected to the input of the embedding layer.
[0008] Optionally, based on the constructed code hypergraph, the code hypergraph data is input into the trained summary generation model to obtain a code summary, including: Within an encoding layer, the initial node feature matrix of the code hypergraph is input into the embedding layer to obtain the shared node feature matrix; the shared node feature matrix and the correlation matrix of the code hypergraph are input into the hypergraph neural network layer to obtain the aggregated and updated node feature matrix; the updated node feature matrix processed by the activation layer and the initial node feature matrix are input into the residual connection and the normalization layer, and the output result is transmitted to the next encoding layer. After processing through multiple coding layers, the encoded node feature matrix is obtained; Within a decoding layer, the initial node feature matrix is input to the masked multi-head attention layer in the decoding layer, and the output of the masked multi-head attention layer and the encoded node feature matrix are input to the encoder-decoder multi-head attention layer to obtain the summary representation matrix; the summary representation matrix is input to the feedforward neural network layer, and the output of the feedforward neural network layer is input to the next decoding layer; After processing through multiple decoding layers, the final output is input into a classifier, which then outputs a code summary word by word in a loop.
[0009] Optionally, the three-step message passing mechanism includes three message passing stages: node to hyperedge, hyperedge to hyperedge, and hyperedge to node; In the message passing phase from node to hyperedge, the attention mechanism is used to calculate the attention weights of all nodes belonging to the same hyperedge, and the information of all nodes of the same hyperedge is aggregated by the attention weights as the feature vector of the hyperedge. In the message passing phase from hyperedge to hyperedge, there are two message passing modes: node mode and full mode. In node mode, information exchange between hyperedges is carried out through the nodes they share. In full mode, any hyperedges exchange information directly through self-attention. In the message passing phase from hyperedge to node, an attention mechanism is used to calculate the attention weights of multiple hyperedges containing the same node, and the information of multiple hyperedges containing the same node is aggregated by the attention weights as the feature vector of the node.
[0010] Optionally, during the message passing phase from node to superedge, in, Let be the first aggregated feature matrix of the hyperedge. For point-to-edge attention weights, Represents a node The value vector of the eigenmatrix, Indicates the relationship with the hyperedge The set of all connected nodes. Indicates the superedge The query vector of type embedding vector, Represents a node The key vectors of the feature matrix, where H is the hidden dimension. Represents a node Value vector weights, Indicates the superedge The first query vector weight, Indicates the superedge Type embedding vector, Represents a node The key vector weights, Represents a node The characteristic matrix of .
[0011] Optionally, during the message passing phase from one superedge to another: In node mode Represents the aggregated bond vector; Indicates the superedge The key vector of the features, Representation and shared nodes The set of all connected hyperedges; in, This represents the edge-to-edge attention weights in node mode. Indicates the superedge The query vector of the features, Indicates the superedge The weight of the second query vector. in, This represents the second aggregated feature matrix of the hyperedge. Indicates the superedge The feature value vector, This represents the set of all superedges that share nodes with other superedges. In full mode in, This represents the edge-to-edge attention weights in full mode. Indicates the superedge The key vector of the first aggregated feature matrix, Indicates the superedge The weights of the first key vector; in, Indicates the relationship with the hyperedge The set of all hyperedges that exchange information.
[0012] Optionally, during the message passing phase from the hyperedge to the node, in, This represents the attention weight from the edge to the vertex. Represents a node The query vector of the feature matrix. Indicates the superedge The key vector of the second aggregated feature matrix, Represents a node Query vector weights, Indicates the superedge The weights of the second key vector. in, Represents a node The aggregated feature matrix, Indicates the superedge The value vector of the second aggregated feature matrix, Indicates the superedge Value vector weights, Represents nodes The set of all connected superedges.
[0013] Optionally, the summary generation model is trained, and the training method includes: Obtain a dataset for the code summarization task, where each sample in the dataset includes source code and the corresponding real code summary, and divide the dataset into training set, validation set and test set; The source code within the samples in the dataset is transformed into an abstract syntax tree, the abstract syntax tree is pruned, and the pruned abstract syntax tree is used to construct a code hypergraph. The summary generation model is trained based on the code hypergraph constructed for each sample in the training and validation sets. The loss value is calculated based on the predicted code summary output by the summary generation model and the real code summary in the sample. The parameters of the summary generation model are adjusted in reverse based on the loss value until the training termination condition is met. The trained summary generation model was tested using samples from the test set to obtain evaluation metrics for measuring its effectiveness.
[0014] Secondly, the present invention also provides a code digest generation system based on hypergraph representation, comprising: The source code transformation module is used to transform source code into an abstract syntax tree and then prune the abstract syntax tree. The Hypergraph Building Module is used to build code hypergraphs using a cropped Abstract Syntax Tree; The summary generation module is used to obtain code summaries by inputting the code hypergraph data into the trained summary generation model based on the constructed code hypergraph. The summary generation model includes an encoding layer, which includes a hypergraph neural network layer. The hypergraph neural network layer uses a three-step message passing mechanism to aggregate information from the code hypergraph data.
[0015] This invention provides a code digest generation method and system based on hypergraph representation. Compared with the prior art, it has the following advantages: By constructing a code hypergraph after transforming and processing the source code, and aggregating high-order interaction information of the code hypergraph using a three-step message passing mechanism, this method breaks through the limitation of traditional ordinary graphs that can only express second-order relations. It can effectively capture high-order dependencies and group interactions in the code, fully represent the complex semantics of the code, and solve the problem of semantic bias and information omission in code summarization caused by the lack of high-order relations in existing technologies. It can improve the accuracy of code summarization generation, accurately reflect the core functions of the code, improve code readability, and reduce software development and maintenance costs. Attached Figure Description
[0016] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0017] Figure 1 A flowchart illustrating a code digest generation method based on hypergraph representation provided in an embodiment of the present invention; Figure 2 This is a schematic diagram of the structure of the abstract generation model provided in an embodiment of the present invention; Figure 3 This is a schematic diagram of the structure of a code digest generation system based on hypergraph representation provided in an embodiment of the present invention. Detailed Implementation
[0018] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions in the embodiments of this application are described clearly and completely. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments in this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0019] To better understand the above technical solutions, the following will provide a detailed explanation of the technical solutions in conjunction with the accompanying drawings and specific implementation methods.
[0020] like Figure 1 As shown in the figure, an embodiment of this application provides a code digest generation method based on hypergraph representation, including: S1: Transform the source code into an abstract syntax tree and prune the abstract syntax tree.
[0021] S2: Construct a code hypergraph using the pruned abstract syntax tree.
[0022] S3: Based on the constructed code hypergraph, the code hypergraph data is input into the trained summary generation model to obtain the code summary. The summary generation model includes an encoding layer, which includes a hypergraph neural network layer. The hypergraph neural network layer uses a three-step message passing mechanism to aggregate information from the code hypergraph data.
[0023] In this optional embodiment, by constructing a code hypergraph, multiple related nodes can be directly connected using a single hyperedge, thus intuitively and efficiently representing their collaborative relationships. This hypergraph representation significantly enhances the ability to model complex semantics and high-order interactions in code, extracting richer and more accurate semantic information from the source code. Its advantages are particularly evident when dealing with complex code logic involving the combined effects of multiple variables or operators. The hypergraph neural network layer included in the summarization model, especially its three-step message passing mechanism, effectively aggregates information on the hypergraph structure. This mechanism allows nodes and hyperedges to fully integrate their neighborhood information, including high-order relationship information directly represented by the hyperedges, thereby generating more expressive feature representations. Based on these high-quality feature representations, the summarization model can generate more accurate and comprehensive code summaries, effectively solving the problem of poor summarization accuracy caused by insufficient graph representation capabilities in existing technologies, and significantly improving the quality and practicality of code summarization.
[0024] The following is a detailed description of each step.
[0025] S1: Transform the source code into an abstract syntax tree and prune the abstract syntax tree.
[0026] Specifically, in the stage of transforming source code into an abstract syntax tree (AST), a programming language-specific parser (such as the tree-sitter open-source library for converting code into a syntax tree) can be used to perform lexical and syntactic analysis on the source code, thereby generating a complete AST. This parser can identify various syntactic elements in the source code and organize them into a hierarchical tree structure. When pruning this AST, nodes that are not important to the semantic understanding or summarization task can be removed based on preset pruning rules or human experience. For example, nodes representing comments, whitespace, or debugging information can be simply deleted to reduce the size and complexity of the AST.
[0027] The Abstract Syntax Tree (AST) is pruned using the following rules: bottom-level full inclusion rule, value or name identification rule, line-first positioning rule, compound rule, and edge reconnection mechanism. Specifically, the bottom-level full inclusion rule states that the node is the deepest non-leaf node in the AST, and all its descendants are leaf nodes. This rule ensures that the most basic tree-like syntax structure of the code is not destroyed. The value or name identification rule states that the node has one leaf node as a child node, and this leaf node represents the node's specific value or name. This rule helps the model distinguish between "user-defined values / names" (e.g., variable name b) and "predefined syntax keywords" (e.g., the symbol / =). The line-first positioning rule states that the node has one leaf node as a child node, and this leaf node is exactly the first token of a line of source code. Since each node meeting this condition covers one or more lines of source code, this visually depicts the two-dimensional layout information of the code. The compound rule states that a child node of the node simultaneously satisfies the above value or name identification rule and the line-first positioning rule.
[0028] Edge reconnection mechanism: During pruning, if a non-leaf node is deleted because it does not meet any of the above conditions, all its child nodes will be directly connected to its parent node across levels. This ensures that although redundant nodes are deleted, the overall hierarchical structure of the code is still preserved.
[0029] S2: Construct a code hypergraph using the pruned abstract syntax tree.
[0030] Specifically, when constructing the code hypergraph, each node in the pruned abstract syntax tree can be mapped to a node in the hypergraph. The code hypergraph includes parent-child hyperedges, data flow hyperedges, row-level hyperedges, and block hyperedges. Parent-child hyperedges connect a node and all its child nodes to enable higher-order interactions between parent and child nodes. Data flow hyperedges connect a definition node and all its user nodes, as well as a user node and its user definition node, to capture higher-order data dependencies. Row-level hyperedges connect all leaf nodes belonging to the same row (a leaf node in the AST is a token in the source code sequence, and all leaf nodes organized in chronological order constitute the entire source code sequence) to enhance the interaction of local token information in the source code sequence. Block hyperedges connect nodes whose terms collectively constitute a control flow block (e.g., one or more lines of source code controlled by if, for, while, etc.), which are semantically closely related, such as conditional statements and corresponding execution branches.
[0031] When this code hypergraph, rich in structural and semantic information, is fed into the hypergraph neural network layer within the encoding layer of the code summary generation model, the three-step message-passing mechanism employed by the hypergraph neural network layer fully leverages these different types of hyperedges. During message passing, nodes not only obtain information from directly connected nodes, but also acquire structural context through parent-child hyperedges, data dependency context through data flow hyperedges, local lexical context through row-level hyperedges, and control flow context through block hyperedges. This multi-dimensional, high-order information aggregation enables the model to form a more comprehensive and in-depth understanding of the source code, thereby generating more accurate and context-sensitive code summaries.
[0032] S3: Based on the constructed code hypergraph, the code hypergraph data is input into the trained summary generation model to obtain the code summary.
[0033] Specifically, such as Figure 2 As shown, the summary generation model includes an encoding layer, which comprises a hypergraph neural network layer. The hypergraph neural network layer uses a three-step message passing mechanism to aggregate information from the code hypergraph data. The encoding layer also includes an activation layer and a residual connection and normalization layer, which are sequentially connected. The summary generation model also includes an embedding layer, a decoding layer, and a classifier. Multiple encoding layers are sequentially connected, and multiple decoding layers are sequentially connected. The output of the embedding layer is connected to the input of the first encoding layer and the first decoding layer, respectively, and the output of the last decoding layer is connected to the input of the classifier. The decoding layer includes a masked multi-head attention layer, an encoder-decoder multi-head attention layer, and a feedforward neural network layer, which are sequentially connected. The output of the encoding layer is connected to the input of the encoder-decoder multi-head attention layer. The output of the classifier is connected to the input of the embedding layer.
[0034] The S3 step specifically includes the following details: Within an encoding layer, the initial node feature matrix from the code hypergraph is input to the embedding layer to obtain a shared node feature matrix. After passing through the shared embedding layer, it is embedded into a specified dimension. The embedding layer is essentially a learnable lookup table that maps discrete symbols (such as words or tokens) to continuous, low-dimensional, dense vectors. Neural networks cannot directly process text; the embedding layer converts discrete symbols into numerical values that the neural network can compute. The shared node feature matrix and the association matrix of the code hypergraph are input to the hypergraph neural network layer to obtain an aggregated and updated node feature matrix. The updated node feature matrix processed by the activation layer and the initial node feature matrix are input to the residual connection and normalization layer, and the output is transmitted to the next encoding layer. The fundamental function of the activation layer is to introduce nonlinear transformations. Without activation layers, regardless of the number of layers in the neural network, its overall transformation will degenerate into a simple linear function, thus completely losing the ability to learn and simulate complex patterns. The node feature matrix after the activation layer and the node feature matrix initially processed by the embedding layer are input to the residual connection and normalization layer. The two node feature matrices are added together and graph normalization is performed to prevent gradient vanishing and vector shifts caused by multi-layer computation.
[0035] After processing through multiple coding layers, the encoded node feature matrix is obtained.
[0036] Within a decoding layer, the initial node feature matrix is input to the masked multi-head attention layer in the decoding layer, and the output of the masked multi-head attention layer and the encoded node feature matrix are input to the encoder-decoder multi-head attention layer to obtain the summary representation matrix; the summary representation matrix is input to the feedforward neural network layer, and the output of the feedforward neural network layer is input to the next decoding layer.
[0037] After processing through multiple decoding layers, the final output is input into a classifier, which then outputs a code summary word by word in a loop.
[0038] Specifically, the decoder generates the summary word by word (i.e., autoregressive generation). Assuming the standard summary is: [convert, pixel, to, meter], after adding control characters, it becomes: [ <bos>,convert,pixel,to,meter, <eos>Input to the decoder <bos>The input matrix is copied three times, serving as the Query, Key, and Value inputs respectively. A multi-head attention layer with residual connections and normalization masks is used. The mask is crucial; it acts like a shield, ensuring that when the model processes the i-th word (i.e., the token), it absolutely cannot see the (i+1)-th word, strictly guaranteeing the sequential logic of the autoregressive generation. The multi-head attention layer with residual connections and normalization ensures that the input matrix (containing the currently learned context, currently containing only one summary token) is processed. <bos>The algorithm searches for the most relevant structural features in the "code hypergraph". The inputs to the multi-head attention layer with residual connections and normalization include Query (output from the masked multi-head attention layer), Key (node feature matrix from the final output of the encoding layer), and Value (node feature matrix from the final output of the encoding layer). The output of the multi-head attention layer is a summary representation matrix that incorporates graph structural features. The feedforward neural network performs independent fully connected nonlinear mappings (usually first increasing dimensionality and then decreasing dimensionality) on the feature vector at each token's location, further refining and deepening the higher-order semantic representation. After passing through 8 decoder layers, the data is fed into a linear classifier to calculate the probability distribution of each word in the vocabulary and predict the first word (e.g., convert). The convert word is then concatenated into the input to become... <bos>The process of `convert` repeats through the 8-layer decoder to predict the second word (e.g., "pixel"), and continues in this loop until the model predicts a specific end-of-line character. <eos>Only then will the entire generation process stop.
[0039] The code summarization model leverages multi-layered encoding and decoding mechanisms to gain a deeper understanding of the complex structure and semantic information of the code hypergraph. The embedding layer transforms raw features into a unified semantic representation, laying the foundation for subsequent processing. Multi-layered encoding, combined with hypergraph neural network layers, activation layers, and residual connections and normalization layers, effectively captures higher-order relationships and long-distance dependencies in the code hypergraph, overcoming challenges in training deep networks and extracting richer and more robust code semantic features. Multi-layered decoding, through masked multi-head attention layers and encoder-decoder multi-head attention layers, effectively fuses and aligns the generated summary content with the encoder output, enabling the model to fully utilize contextual information and code semantics during summary generation, avoiding semantic loss or incoherent summaries that may occur with simpler models. The classifier ensures accurate prediction of the final summary terms. This model architecture significantly improves the accuracy, fluency, and semantic consistency of code summarization, allowing the generated summaries to more accurately reflect the function and intent of the source code.
[0040] A hypergraph is a generalized graph structure where edges (called hyperedges) can connect any number of vertices. However, traditional two-step message-passing hypergraph neural networks primarily aggregate features through the direct relationship between nodes and hyperedges. Hyperedges only interact indirectly with other nodes through the nodes they contain, and cannot directly capture higher-order relationships between hyperedges. Therefore, a three-step message-passing mechanism is designed, comprising three message-passing stages: node to hyperedge, hyperedge to hyperedge, and hyperedge to node. During the message passing phase from a node to a hyperedge, an attention mechanism is used to calculate the attention weights of all nodes belonging to the same hyperedge, and the attention weights are then used to aggregate the information of all nodes on the same hyperedge as the feature vector of the hyperedge. The specific analysis process is as follows.
[0041] in, Let be the first aggregated feature matrix of the hyperedge. For point-to-edge attention weights, Represents a node The value vector of the eigenmatrix, Indicates the relationship with the hyperedge The set of all connected nodes. Indicates the superedge The query vector of type embedding vector, Represents a node The key vectors of the feature matrix, where H is the hidden dimension. Represents a node Value vector weights, Indicates the superedge The first query vector weight, Indicates the superedge Type embedding vector, Represents a node The key vector weights, Represents a node The characteristic matrix of .
[0042] In the message passing phase from hyperedge to hyperedge, there are two message passing modes: node mode and full mode. In node mode, information exchange between hyperedges occurs through nodes they share. In full mode, any hyperedge directly exchanges information through self-attention. The specific analysis process is as follows.
[0043] In node mode Represents the aggregated bond vector; Indicates the superedge The key vector of the features, Representation and shared nodes The set of all connected hyperedges; in, This represents the edge-to-edge attention weights in node mode. Indicates the superedge The query vector of the features, Indicates the superedge The weight of the second query vector. in, This represents the second aggregated feature matrix of the hyperedge. Indicates the superedge The feature value vector, This represents the set of all superedges that share nodes with other superedges. In full mode in, This represents the edge-to-edge attention weights in full mode. Indicates the superedge The key vector of the first aggregated feature matrix, Indicates the superedge The weights of the first key vector; in, Indicates the relationship with the hyperedge The set of all hyperedges that exchange information.
[0044] In the message passing phase from hyperedge to node, an attention mechanism is used to calculate the attention weights of multiple hyperedges containing the same node, and the information of multiple hyperedges containing the same node is aggregated using the attention weights as the feature vector of the node. The specific analysis process is as follows.
[0045] in, This represents the attention weight from the edge to the vertex. Represents a node The query vector of the feature matrix. Indicates the superedge The key vector of the second aggregated feature matrix, Represents a node Query vector weights, Indicates the superedge The weights of the second key vector. in, Represents a node The aggregated feature matrix, Indicates the superedge The value vector of the second aggregated feature matrix, Indicates the superedge Value vector weights, Represents nodes The set of all connected superedges.
[0046] The hypergraph neural network layer employs a three-step message-passing mechanism to achieve effective information aggregation of code hypergraph data. First, in the node-to-hyperedge message-passing phase, information converges from various nodes in the hypergraph to their respective hyperedges. By introducing an attention mechanism, the model can compute and assign attention weights to all nodes belonging to the same hyperedge, ensuring that more relevant nodes contribute a greater influence when aggregating node information as hyperedge feature vectors. This allows each hyperedge to form a feature representation containing rich contextual information about its internal nodes. Second, in the hyperedge-to-hyperedge message-passing phase, hyperedges exchange information to capture broader contextual dependencies. This phase offers two modes: in node mode, hyperedges interact indirectly through their shared nodes, which helps propagate information along paths connected by common code elements, reflecting structural or semantic overlap. In full mode, arbitrary hyperedges interact directly through a self-attention mechanism, allowing for more global information exchange and capturing long-distance dependencies and complex interactions between different code structures (represented by hyperedges), even if they do not share direct nodes. This phase refines the hyperedge representation by integrating information from relevant hyperedges. Finally, in the final message passing phase from hyperedge to node, the refined hyperedge information is propagated back to its contained nodes. Since a node may belong to multiple hyperedges, each providing a different contextual perspective, this phase also utilizes an attention mechanism to compute attention weights for multiple hyperedges containing the same node. Through these weights, nodes can selectively aggregate information from the most relevant hyperedges, thereby enriching their feature representations with higher-order, context-dependent hypergraph structural information. This iterative, phased message passing process enables hypergraph neural networks to effectively aggregate and propagate information within the complex, multi-relational structure of code hypergraphs. In this way, the model can generate highly expressive node and hyperedge feature embeddings that capture local and global dependencies in the source code, which are then used by subsequent layers of the summarization model to generate accurate and coherent code summaries.
[0047] In addition, the summary generation model needs to be trained before use. Training methods include: Obtain a dataset for the code summarization task, where each sample in the dataset includes source code and a corresponding real code summary. Divide the dataset into training, validation, and test sets. Common division ratios include, but are not limited to, 70% training set, 15% validation set, and 15% test set, or 80% training set, 10% validation set, and 10% test set.
[0048] The source code within the samples in the dataset is transformed into an abstract syntax tree (AST), the AST is pruned, and the pruned AST is used to construct a code hypergraph.
[0049] The code summarization model is trained using a code hypergraph built for each sample in the training and validation sets. The loss value is calculated by comparing the predicted code summaries output by the model with the actual code summaries in the samples. The model's parameters are then adjusted inversely based on the loss value to minimize the difference between the predicted and actual outputs, until the training termination condition is met. The loss value is a quantitative metric that measures the difference between the model's predictions and the actual results; a larger loss value indicates a less accurate prediction. Commonly used loss functions include cross-entropy loss or negative log-likelihood loss, commonly used in sequence generation tasks. Training termination conditions include reaching a preset maximum number of training epochs, the performance metric on the validation set no longer improving within a certain number of epochs, or the loss value falling below a certain threshold.
[0050] The trained summary generation model was tested using samples from the test set to obtain evaluation metrics for measuring its effectiveness.
[0051] Specifically, the constructed summary generation model is trained using training and validation sets to obtain trained model parameters. The model is then tested using a test set to obtain a summary for each test sample. Finally, the summaries generated by the model are compared with the initial test dataset. Code, Summary The model's generated summaries are compared with real code summaries to derive evaluation metrics. These metrics include BLEU, ROUGE-L, and METEOR, with higher scores being better. BLEU is calculated by multiplying the average n-gram precision score by a "brevity penalty" (used to penalize excessively short generated sentences), statistically analyzing the phrase overlap between the generated text and the reference translation / summary, focusing on fluency and word accuracy. ROUGE-L relies on the number of overlapping units (e.g., n-grams, word sequences, and word pairs) between the model-generated text and the reference text (standard answer), quantifying the matching degree and completeness of the generated content by comparing the n-gram overlap between the generated text and the standard reference summary. METEOR is calculated based on the harmonic mean of precision and recall for unigram matching between text sequences, balancing precision and recall for semantic matching that more closely resembles human judgment. Evaluation on the test set allows for an objective measurement of the model's performance, providing reliable code summarization capabilities for practical applications and greatly enhancing the practicality and effectiveness of the aforementioned code summarization method based on hypergraph representation.
[0052] like Figure 3 As shown in the figure, an embodiment of this application provides a code digest generation system based on hypergraph representation, comprising: The source code conversion module 100 is used to convert source code into an abstract syntax tree and prune the abstract syntax tree; Hypergraph building module 200 is used to build code hypergraphs using the clipped abstract syntax tree; The summary generation module 300 is used to input the code hypergraph data into the trained summary generation model based on the constructed code hypergraph to obtain a code summary. The summary generation model includes an encoding layer, which includes a hypergraph neural network layer. The hypergraph neural network layer uses a three-step message passing mechanism to aggregate information from the code hypergraph data.
[0053] In this optional embodiment, by combining code hypergraph representation with a hypergraph neural network layer using a three-step message passing mechanism, higher-order relationships and group interactions between nodes in the code can be directly modeled. This solves the problem of insufficient code summarization accuracy caused by the inability of ordinary graphs to represent many-to-many interactions in existing technologies, thus improving the accuracy and practicality of code summarization. The code hypergraph can directly connect multiple nodes using a single hyperedge, such as connecting multiple variables and operators, intuitively representing their shared participation relationships and significantly enhancing the ability to model complex code semantics. The three-step message passing mechanism of the hypergraph neural network layer effectively aggregates information on the hypergraph structure, enabling nodes and hyperedges to fuse higher-order relationship information and generate more expressive feature representations. Therefore, this system can extract richer and more accurate code semantic information, especially when dealing with complex logic involving the combined effects of multiple variables or operators. The generated code summaries are more accurate and comprehensive, effectively overcoming the problem of poor summarization accuracy caused by insufficient graph representation capabilities in existing technologies.
[0054] In summary, compared with existing technologies, it has the following beneficial effects: 1. Hypergraph representation of code data solves the problem that ordinary simple graphs cannot obtain high-order data relationships, thus improving the quality of code summary generation.
[0055] 2. In the process of hypergraph construction, the construction and use of row-level hyperedges, data flow hyperedges and block hyperedges solve the problem of lack of contextual semantic information in AST, and improve the expressive power and generalization ability of hypergraph representation.
[0056] 3. In the process of building the three-step message-passing hypergraph neural network model, the accuracy of the code summarization task is significantly improved by adding information passing from hyperedge to hyperedge.
[0057] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0058] The above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application.< / eos> < / bos> < / bos> < / bos> < / eos> < / bos>
Claims
1. A code digest generation method based on hypergraph representation, characterized in that, include: Transform the source code into an abstract syntax tree and then prune the abstract syntax tree; Construct a code hypergraph using the pruned abstract syntax tree; Based on the constructed code hypergraph, the code hypergraph data is input into the trained summary generation model to obtain code summaries. The summary generation model includes an encoding layer, which includes a hypergraph neural network layer. The hypergraph neural network layer uses a three-step message passing mechanism to aggregate information from the code hypergraph data.
2. The code digest generation method based on hypergraph representation as described in claim 1, characterized in that, The code hypergraph includes parent-child hyperedges, data flow hyperedges, row-level hyperedges, and block hyperedges. Parent-child hyperedges connect a node and all its child nodes to enable higher-order interactions between the parent and child nodes. Data flow hyperedges connect a defining node and all its user nodes, as well as a user node and its user defining node. Row-level hyperedges connect all leaf nodes belonging to the same row, unit by line of source code sequence. Block hyperedges connect nodes whose terms together form a control flow block.
3. The code digest generation method based on hypergraph representation as described in claim 1, characterized in that, The encoding layer further includes an activation layer and a residual connection and normalization layer, and the hypergraph neural network layer, activation layer and residual connection and normalization layer are connected in sequence; The summary generation model also includes an embedding layer, a decoding layer, and a classifier. Multiple encoding layers are connected in sequence, and multiple decoding layers are connected in sequence. The output of the embedding layer is connected to the input of the first encoding layer and the input of the first decoding layer, respectively. The output of the last decoding layer is connected to the input of the classifier. The decoding layer includes a masked multi-head attention layer, an encoder-decoder multi-head attention layer and a feedforward neural network layer connected in sequence, and the output of the encoder layer is connected to the input of the encoder-decoder multi-head attention layer; The output of the classifier is connected to the input of the embedding layer.
4. The code digest generation method based on hypergraph representation as described in claim 3, characterized in that, The constructed code hypergraph is used to input code hypergraph data into a trained summary generation model to obtain code summaries, including: Within an encoding layer, the initial node feature matrix of the code hypergraph is input into the embedding layer to obtain the shared node feature matrix; the shared node feature matrix and the correlation matrix of the code hypergraph are input into the hypergraph neural network layer to obtain the aggregated and updated node feature matrix; the updated node feature matrix processed by the activation layer and the initial node feature matrix are input into the residual connection and the normalization layer, and the output result is transmitted to the next encoding layer. After processing through multiple coding layers, the encoded node feature matrix is obtained; Within a decoding layer, the initial node feature matrix is input to the masked multi-head attention layer in the decoding layer, and the output of the masked multi-head attention layer and the encoded node feature matrix are input to the encoder-decoder multi-head attention layer to obtain the summary representation matrix; the summary representation matrix is input to the feedforward neural network layer, and the output of the feedforward neural network layer is input to the next decoding layer; After processing through multiple decoding layers, the final output is input into a classifier, which then outputs a code summary word by word in a loop.
5. The code digest generation method based on hypergraph representation as described in claim 1, characterized in that, The three-step message passing mechanism includes three message passing stages: node to hyperedge, hyperedge to hyperedge, and hyperedge to node. In the message passing phase from node to hyperedge, the attention mechanism is used to calculate the attention weights of all nodes belonging to the same hyperedge, and the information of all nodes of the same hyperedge is aggregated by the attention weights as the feature vector of the hyperedge. In the message passing phase from hyperedge to hyperedge, there are two message passing modes: node mode and full mode. In node mode, information exchange between hyperedges is carried out through the nodes they share. In full mode, any hyperedges exchange information directly through self-attention. In the message passing phase from hyperedge to node, an attention mechanism is used to calculate the attention weights of multiple hyperedges containing the same node, and the information of multiple hyperedges containing the same node is aggregated by the attention weights as the feature vector of the node.
6. The code digest generation method based on hypergraph representation as described in claim 5, characterized in that, During the message passing phase from node to hyperedge in, Let be the first aggregated feature matrix of the hyperedge. For point-to-edge attention weights, Represents a node The value vector of the eigenmatrix, Indicates the relationship with the hyperedge The set of all connected nodes. Indicates the superedge The query vector of type embedding vector, Represents a node The key vectors of the feature matrix, where H is the hidden dimension. Represents a node Value vector weights, Indicates the superedge The first query vector weight, Indicates the superedge Type embedding vector, Represents a node The key vector weights, Represents a node The characteristic matrix of .
7. The code digest generation method based on hypergraph representation as described in claim 6, characterized in that, During the message passing phase from one hyperedge to another: In node mode Represents the aggregated bond vector; Indicates the superedge The key vector of the features, Representation and shared nodes The set of all connected hyperedges; in, This represents the edge-to-edge attention weights in node mode. Indicates the superedge The query vector of the features, Indicates the superedge The weight of the second query vector. in, This represents the second aggregated feature matrix of the hyperedge. Indicates the superedge The feature value vector, This represents the set of all superedges that share nodes with other superedges. In full mode in, This represents the edge-to-edge attention weights in full mode. Indicates the superedge The key vector of the first aggregated feature matrix, Indicates the superedge The weights of the first key vector; in, Indicates the relationship with the hyperedge The set of all hyperedges that exchange information.
8. The code digest generation method based on hypergraph representation as described in claim 7, characterized in that, During the message passing phase from the hyperedge to the node in, This represents the attention weight from the edge to the vertex. Represents a node The query vector of the feature matrix. Indicates the superedge The key vector of the second aggregated feature matrix, Represents a node Query vector weights, Indicates the superedge The weights of the second key vector. in, Represents a node The aggregated feature matrix, Indicates the superedge The value vector of the second aggregated feature matrix, Indicates the superedge Value vector weights, Represents nodes The set of all connected superedges.
9. The code digest generation method based on hypergraph representation as described in claim 1, characterized in that, The summary generation model is trained using the following methods: Obtain a dataset for the code summarization task, where each sample in the dataset includes source code and the corresponding real code summary, and divide the dataset into training set, validation set and test set; The source code within the samples in the dataset is transformed into an abstract syntax tree, the abstract syntax tree is pruned, and the pruned abstract syntax tree is used to construct a code hypergraph. The summary generation model is trained based on the code hypergraph constructed for each sample in the training and validation sets. The loss value is calculated based on the predicted code summary output by the summary generation model and the real code summary in the sample. The parameters of the summary generation model are adjusted in reverse based on the loss value until the training termination condition is met. The trained summary generation model was tested using samples from the test set to obtain evaluation metrics for measuring its effectiveness.
10. A code digest generation system based on hypergraph representation, characterized in that, include: The source code transformation module is used to transform source code into an abstract syntax tree and then prune the abstract syntax tree. The Hypergraph Building Module is used to build code hypergraphs using a cropped Abstract Syntax Tree; The summary generation module is used to obtain code summaries by inputting the code hypergraph data into the trained summary generation model based on the constructed code hypergraph. The summary generation model includes an encoding layer, which includes a hypergraph neural network layer. The hypergraph neural network layer uses a three-step message passing mechanism to aggregate information from the code hypergraph data.