Code readability classification method and system based on hypergraph representation
By constructing a code readability classification method based on hypergraph representation and utilizing a hypergraph neural network model, the problem of not being able to obtain deep structural information in existing technologies is solved, and more accurate code readability classification is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- HEFEI UNIV OF TECH
- Filing Date
- 2025-11-04
- Publication Date
- 2026-05-12
AI Technical Summary
Existing technologies cannot effectively obtain deep structural information of code, resulting in inaccurate code readability classification results.
A hypergraph representation method is constructed, which generates a code hypergraph through an abstract syntax tree, extracts row-level hyperedges and node features, and uses a hypergraph neural network model for readability classification, including a hypergraph convolution module, an attention module, and a pooling module, and integrates node features and an association matrix for training.
By mining the contextual semantic information and deep structural information of the code, the accuracy and generalization ability of code readability classification are improved.
Smart Images

Figure CN121278489B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of neural network technology, and more specifically, to a code readability classification method and system based on hypergraph representation. Background Technology
[0002] Code readability is defined as a measure of how easily developers can read and understand source code. Code readability classification refers to categorizing a piece of source code into certain predefined levels based on its readability, such as classifying code snippets as readable or unreadable.
[0003] Existing technologies treat a piece of code as an image, resulting in a lack of contextual semantic information. Convolutional neural networks can only find local structural information of the code and cannot obtain deep structural information, leading to a lack of high-order information and inaccurate readability classification results. Summary of the Invention
[0004] The problem that this invention aims to solve is that existing code readability classification methods cannot obtain deep structural information, resulting in inaccurate readability classification results.
[0005] To address the above problems, in a first aspect, the present invention provides a code readability classification method based on hypergraph representation, comprising:
[0006] Obtain the source code to be categorized;
[0007] Construct an abstract syntax tree from the source code;
[0008] The edges between nodes with parent-child relationships in the abstract syntax tree are adjusted to parent-child hyperedges to obtain the code hypergraph. Each node in the code hypergraph has a start mark and an end mark to determine the position range of the node in the source code. The feature of each node in the code hypergraph is the average value of all tokens contained in the position range corresponding to each node.
[0009] Line-level hyperedges are constructed line by line for the code sequence of the source code, where the node features of the line-level hyperedges are tokens extracted from the code by line segmentation;
[0010] Based on the row-level hyperedges, nodes, and node features in the source code, as well as the parent-child hyperedges, nodes, and node features in the code hypergraph, the node feature matrix and the association matrix are obtained.
[0011] By inputting the node feature matrix and correlation matrix into the trained hypergraph neural network model, the readability classification results are obtained.
[0012] Optionally, the hypergraph neural network model includes a hypergraph convolution module, a hypergraph attention module, a hypergraph pooling module, and a classifier;
[0013] The process of inputting the node feature matrix and correlation matrix into the trained hypergraph neural network model yields readability classification results, including:
[0014] The node feature matrix and the correlation matrix are input into the hypergraph convolution module to obtain the first updated node feature matrix. The first updated node feature matrix does not change the number of nodes in the node feature matrix, but only updates the node feature dimension.
[0015] The node feature matrix and association matrix are input into the hypergraph attention module to obtain the node feature matrix updated in the second time. The number of nodes remains unchanged in the second update node feature matrix, and only the node feature dimension is updated.
[0016] The node feature matrix of the second update is input into the hypergraph pooling module to obtain the aggregated feature matrix. The aggregated feature matrix is based on the node feature matrix of the second update, where the node number dimension is aggregated to one dimension, while the node feature dimension remains unchanged.
[0017] The aggregated feature matrix is input into the classifier to obtain the readability classification result.
[0018] Optionally, the hypergraph convolution module is used to statically update the node feature values in the node feature matrix;
[0019]
[0020] Among them, X (l) X represents the node feature values in the updated node feature matrix after the hypergraph convolution module update, σ represents the non-linear activation function, D and B represent the degree matrix of the node and the degree matrix of the hyperedge in the code hypergraph, respectively, H represents the association matrix, W represents the hyperedge weight matrix, and X represents the hyperedge weight matrix. (l-1) P1 represents the node feature matrix of the input hypergraph convolution module, and P2 represents the weight matrix of the hypergraph convolution module.
[0021] Optionally, the hypergraph attention module uses an attention mechanism to dynamically learn and update node feature values;
[0022]
[0023] in, Let represent the node feature value of the i-th node in the second-order updated node feature matrix after being updated by the hypergraph attention module. LayerNorm is a function that normalizes each feature dimension. K represents the number of attention heads, and E... i V represents the set of hyperedges connected to node i. e Let represent the set of all nodes contained in the e-th superedge. P1 represents the node feature value of the j-th node in the updated node feature matrix of the input hypergraph attention module, and P2 is the first weight matrix. This represents the attention weight of node j to hyperedge e in the k-th attention head;
[0024]
[0025] Where exp represents the natural exponential function, a k Let P3 represent the scoring function of the k-th attention head, and let P3 represent the second weight matrix. and Let e represent the node feature values of the j-th node and the t-th node in the node feature matrix during a single update. e This represents the set of all nodes contained within the hyperedge e connected to node i.
[0026] Optionally, the code readability classification method based on hypergraph representation further includes training the hypergraph neural network model, the training process including:
[0027] Obtain a source code set, wherein each line of code in the source code set is assigned a corresponding readability score tag, and the source code set is divided into a readable source code set and an unreadable source code set according to the readability score tags;
[0028] Based on the source code in the source code set, construct the row-level hyperedge and code hypergraph corresponding to each source code;
[0029] Based on the row-level hyperedges in the source code and the parent-child hyperedges in the code hypergraph, the node feature matrix and the association matrix are obtained;
[0030] By inputting the node feature matrix and association matrix into the constructed hypergraph neural network model, the readability classification results are obtained;
[0031] The readability classification results are compared with the readability score labels corresponding to the source code to obtain the loss value and accuracy.
[0032] Based on the loss value and accuracy, adjust the parameters of the hypergraph neural network model until the loss value and accuracy meet the requirements, thus obtaining the trained hypergraph neural network model.
[0033] Secondly, the present invention also provides a code readability classification system based on hypergraph representation, comprising:
[0034] The code acquisition module is used to acquire the source code to be categorized.
[0035] Abstract syntax tree building module, used to build abstract syntax trees from source code;
[0036] The code hypergraph generation module is used to adjust the edges between nodes with parent-child relationships in the abstract syntax tree into parent-child hyperedges to obtain the code hypergraph. Each node in the code hypergraph has a start mark and an end mark to determine the position range of the node in the source code. The feature of each node in the code hypergraph is the average value of all tokens contained in the position range corresponding to each node.
[0037] The row-level hyperedge construction module is used to construct row-level hyperedges line by line from the code sequence of the source code. The node features of the row-level hyperedges are tokens extracted from the code line by line.
[0038] The feature integration module is used to obtain the node feature matrix and the association matrix based on the row-level hyperedges, nodes and node features in the source code, as well as the parent-child hyperedges, nodes and node features in the code hypergraph;
[0039] The classification module is used to input the node feature matrix and correlation matrix into the trained hypergraph neural network model to obtain readable classification results.
[0040] Thirdly, the present invention provides an electronic device, including a memory and a processor;
[0041] The memory is used to store computer programs;
[0042] The processor is configured to implement the code readability classification method based on hypergraph representation as described in the first aspect when executing the computer program.
[0043] Fourthly, the present invention provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the code readability classification method based on hypergraph representation as described in the first aspect.
[0044] This invention provides a code readability classification method and system based on hypergraph representation. Compared with existing technologies, it has the following advantages:
[0045] Based on the source code, row-level hyperedges and code hypergraphs with parent-child hyperedges are constructed. The node features of row-level hyperedges are extracted, and the node features of parent-child hyperedges in the code hypergraph are aggregated to mine the contextual semantic information in the code data and the deep structural information of the source code, avoiding the loss of high-order information. The mined node features and hyperedge information are integrated into a node feature matrix and an association matrix, which are input into the trained hypergraph neural network model to obtain the readability classification results, which are more accurate. Attached Figure Description
[0046] 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.
[0047] Figure 1 A flowchart illustrating a code readability classification method based on hypergraph representation provided in an embodiment of the present invention;
[0048] Figure 2 A schematic diagram of the source code provided for an embodiment of the present invention;
[0049] Figure 3 A schematic diagram of an abstract syntax tree provided for an embodiment of the present invention.
[0050] Figure 4 This is a schematic diagram of the code hypergraph provided for an embodiment of the present invention;
[0051] Figure 5 This is a schematic diagram of a row-level hyperedge structure provided in an embodiment of the present invention;
[0052] Figure 6 This is a schematic diagram of the node feature matrix provided in an embodiment of the present invention;
[0053] Figure 7 A schematic diagram of the association matrix provided in an embodiment of the present invention;
[0054] Figure 8 This is a schematic diagram of the structure of a code readability classification system based on hypergraph representation provided in an embodiment of the present invention. Detailed Implementation
[0055] 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.
[0056] 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.
[0057] like Figure 1 As shown in the embodiments of this application, a code readability classification method based on hypergraph representation is provided, including:
[0058] S1: Obtain the source code to be categorized.
[0059] S2: Construct an abstract syntax tree based on the source code.
[0060] S3: Adjust the edges between nodes with parent-child relationships in the abstract syntax tree to parent-child hyperedges to obtain the code hypergraph. Each node in the code hypergraph has a start mark and an end mark to determine the position range of the node in the source code. The feature of each node in the code hypergraph is the average value of all tokens contained in the position range corresponding to each node.
[0061] S4: Construct line-level hyperedges for the code sequence of the source code, where the node features of the line-level hyperedges are tokens extracted from the code by line segmentation.
[0062] S5: Based on the row-level hyperedges, nodes, and node features in the source code, as well as the parent-child hyperedges, nodes, and node features in the code hypergraph, obtain the node feature matrix and the association matrix.
[0063] S6: Input the node feature matrix and correlation matrix into the trained hypergraph neural network model to obtain the readability classification results.
[0064] In this optional embodiment, row-level hyperedges and code hypergraphs with parent-child hyperedges are constructed based on the source code, and the node features of the row-level hyperedges are extracted. The node features of the parent-child hyperedges in the code hypergraph are aggregated to mine the contextual semantic information in the code data and the deep structural information of the source code, avoiding the loss of high-order information. The mined node features and hyperedge information are integrated into a node feature matrix and an association matrix, which are input into the trained hypergraph neural network model to obtain the readability classification result, which is more accurate.
[0065] The following is a detailed description of each step.
[0066] S1: Obtain the source code to be categorized. Specifically, such as... Figure 2 As shown, here is an example code snippet.
[0067] S2: Construct an abstract syntax tree based on the source code.
[0068] Specifically, the tree-sitter open-source toolkit, which transforms code into a syntax tree, is used to generate an abstract syntax tree from the source code, such as... Figure 3 As shown, in this abstract syntax tree, the edges connecting parent and child nodes are simple edges.
[0069] S3: Adjust the edges between nodes with parent-child relationships in the abstract syntax tree to parent-child hyperedges to obtain the code hypergraph. Each node in the code hypergraph has a start mark and an end mark to determine the position range of the node in the source code. The feature of each node in the code hypergraph is the average value of all tokens contained in the position range corresponding to each node.
[0070] Specifically, a typical graph structure consists of a vertex set and an edge set. A vertex represents an object. An edge represents a relationship between objects. In a typical graph structure, an edge connects only two vertices / nodes, such as... Figure 3 As shown. A hypergraph structure consists of a vertex set and a hyperedge set, but unlike a regular graph structure, a hyperedge can connect more than two vertices, such as... Figure 4 As shown. Parent-child hyperedges with parent-child relationships are constructed based on the abstract syntax tree, such as... Figure 4 As shown, an edge connects a parent node and multiple child nodes. The nodes in the parent-child hyperedge are nodes in the Abstract Syntax Tree (ABST). Each node in the ABST has two attributes, `start_byte` and `end_byte`, which can be used to determine its position range in the source code. The CodeBERT model is used to segment the code within this position range in the source code to extract tokens, and the tokens are embedded into a 768-dimensional array. Since this position range may contain multiple tokens, the embeddings of multiple tokens are summed and averaged to obtain the embedding of the node in the ABST. Specifically, as shown in the figure, the ABST node contains the attributes `start_byte` and `end_byte` to determine its position range in the source code. For example, the range of the `class_declaration` node in the source code is this entire code segment, and the range of the `class_body` node in the source code is lines 2-5 of code.
[0071] S4: Construct line-level hyperedges for the code sequence of the source code, where the node features of the line-level hyperedges are tokens extracted from the code by line segmentation.
[0072] Specifically, the source code sequence directly creates row-level superedges line by line, such as... Figure 5 As shown, the nodes of the row-level hyperedges are tokens extracted from the code line by line using the CodeBERT model. The tokens in the row-level hyperedges are directly embedded into the 768-dimensional code using CodeBERT.
[0073] S5: Based on the row-level hyperedges, nodes, and node features in the source code, as well as the parent-child hyperedges, nodes, and node features in the code hypergraph, obtain the node feature matrix and the association matrix.
[0074] Specifically, the row-level hyperedges and their node feature information are integrated with the parent-child hyperedges and their node feature information in the code hypergraph to obtain the node feature matrix and the association matrix. For example... Figure 6As shown, the node feature matrix V is the number of nodes × feature dimension (each node has a dimension of d). Since nodes not present in the source code have been added to the code hypergraph, the nodes in the source code and the nodes in the code hypergraph are integrated. Similarly, the parent-child hyperedges in the code hypergraph and the row-level hyperedges in the source code are integrated. Therefore, the number of nodes includes both nodes in the source code and the new nodes in the code hypergraph. For example... Figure 7 As shown, the incidence matrix H is the number of nodes × the number of hyperedges (the incidence matrix indicates which hyperedge a node i belongs to. When a node i belongs to a certain hyperedge, it is represented by 1, otherwise it is 0), and the number of hyperedges is the number of row-level hyperedges and the number of parent-child hyperedges.
[0075] S6: Input the node feature matrix and correlation matrix into the trained hypergraph neural network model to obtain the readability classification result. The hypergraph neural network model includes a hypergraph convolution module, a hypergraph attention module, a hypergraph pooling module, and a classifier. This step specifically includes:
[0076] The node feature matrix and the correlation matrix are input into the hypergraph convolution module to obtain the first updated node feature matrix. The first updated node feature matrix keeps the number of nodes unchanged and only updates the node feature dimension.
[0077] Specifically, the hypergraph convolution module is used to statically update the node feature values in the node feature matrix, and the hypergraph convolution module can use the HypergraphConv model.
[0078]
[0079] Among them, X (l) σ represents the node feature values in the updated node feature matrix after the hypergraph convolution module update, σ represents the non-linear activation function, which improves the expressive power and training stability of the hypergraph convolution module; D and B represent the degree matrix of the nodes and the degree matrix of the hyperedges in the code hypergraph, respectively; H represents the association matrix; W represents the hyperedge weight matrix (a matrix with 1s on the main diagonal and 0s elsewhere); P1 represents the weight matrix of the hypergraph convolution module, with a size of d. l-1 ×d l , where d l-1 It is the node feature dimension of the previous module, d l This refers to the dimensions updated after learning by the current module. For example, if the weight matrix P of the hypergraph convolution module is 768×128, then the 768-dimensional node feature dimensions input to the hypergraph convolution module become 128-dimensional after learning and updating. (l -1) This represents the node feature matrix of the input hypergraph convolution module (the number of nodes remains unchanged, the feature values are updated, and the feature dimensions may or may not change).
[0080] The node feature matrix and association matrix are input into the hypergraph attention module to obtain the node feature matrix updated in the second stage. The number of nodes remains unchanged in the second stage node feature matrix, and only the node feature dimension is updated.
[0081] Specifically, the hypergraph attention module dynamically learns and updates node feature values using an attention mechanism. The hypergraph attention module uses a multi-head attention module to aggregate information from nodes to hyperedges, and then evenly updates the representation of each node with the hyperedge information.
[0082]
[0083] in, Let represent the node feature value of the i-th node in the second-order updated node feature matrix after being updated by the hypergraph attention module. LayerNorm is a function that normalizes each feature dimension. K represents the number of attention heads. E i V represents the set of hyperedges connected to node i. e Let represent the set of all nodes contained in the e-th superedge. P1 represents the node feature value of the j-th node in the first update node feature matrix of the input hypergraph attention module; P2 is the first weight matrix, which is a learnable weight matrix of size d for all nodes in all hyperedges connected to update node i. l ×d' l , (where d l It is the node feature dimension after the hypergraph convolution module is updated, d' l It refers to the dimension after being updated by the hypergraph attention module. For example, P2=128×128 means that the node feature dimension of the hypergraph convolution module is 128-dimensional, and the node feature dimension is still 128-dimensional after being updated by the hypergraph attention module.
[0084] Let represent the attention weight of node j to hyperedge e in the k-th attention head, calculated as follows.
[0085]
[0086] Where exp represents the natural exponential function, a k This represents the scoring function for the k-th attention. and Let e represent the node feature values of the j-th node and the t-th node in the node feature matrix during a single update. eP1 represents the set of all nodes contained within the hyperedge e connected to node i. P3 represents the second weight matrix, which is a learnable matrix for updating all nodes in a hyperedge connected to node i. Suppose there are 10 nodes in the hyperedge containing node i. After calculation by P3, the contribution of each node to node i may be 8%, 12%, etc., and the sum of all nodes is 100%. The percentage contribution of each node to the hyperedge (i.e., attention weight) is obtained by dynamically learning using P3.
[0087] The node feature matrix of the second update is input into the hypergraph pooling module to obtain the aggregated feature matrix. The aggregated feature matrix is based on the node feature matrix of the second update, where the node number dimension is aggregated to one dimension, while the node feature dimension remains unchanged.
[0088] Specifically, the hypergraph pooling module can use a multi-head attention module to extract graph-level features from the node feature matrix. The input to the hypergraph pooling module is the second-updated node feature matrix after being updated by the hypergraph attention module, and the output is a 1×d' aggregation of all nodes in the second-updated node feature matrix based on their respective attention scores. l The hypergraph feature representation (i.e., the aggregated feature matrix).
[0089] The aggregated feature matrix is input into the classifier to obtain the readability classification result.
[0090] Specifically, the classifier is a linear layer that divides 1×d' l The hypergraph feature representation is transformed into the number of categories that need to be classified (here, it is 2, readable and unreadable, with readable represented by 0 and unreadable represented by 1).
[0091] In S6, by using a hypergraph convolution module and a custom hypergraph attention module, more refined feature representation and stronger semantic recognition capabilities are achieved, significantly improving the accuracy and generalization ability of code readability classification tasks.
[0092] In an optional embodiment of this application, the code readability classification method based on hypergraph representation further includes training the hypergraph neural network model, and the training process specifically includes the following steps.
[0093] Obtain a source code set, wherein each line of code in the source code set is assigned a corresponding readability score tag, and the source code set is divided into a readable source code set and an unreadable source code set based on the readability score tags.
[0094] Specifically, publicly available datasets for code readability classification tasks were collected, and the collected data were labeled according to their corresponding readability scores (e.g., readable is labeled 0, unreadable is labeled 1). The code-readability-krod dataset for code readability classification was found on a website. Its data comes from 100 high-quality code repositories on GitHub, with a readability score of 3.68. The code was then deliberately modified to reduce readability, resulting in a readability score of 3.26. The code with a score of 3.68 was placed in the readable folder, and the code with a score of 3.26 was placed in the unreadable folder. The folder names are the labels of the data's initial readability classification.
[0095] Based on the source code in the source code set, construct the row-level hyperedge and code hypergraph corresponding to each source code.
[0096] Specifically, the tree-sitter open-source library, which transforms code into a syntax tree, is used to generate an abstract syntax tree JSON file with the same name as the source code in the readable and unreadable folders. Parent-child hyperedges with parent-child relationships are then constructed based on these JSON files, with nodes in the hyperedges being nodes in the abstract syntax tree. Line-level hyperedges are directly created line by line for the source code sequence, and the nodes in these hyperedges are tokens extracted from the code line by line using the Codebert model.
[0097] Based on the row-level hyperedges in the source code and the parent-child hyperedges in the code hypergraph, the node feature matrix and the correlation matrix are obtained.
[0098] By inputting the node feature matrix and correlation matrix into the constructed hypergraph neural network model, the readability classification results are obtained.
[0099] Specifically, the tokens in the row-level hyperedges are directly embedded into the 768-dimensional code using Codebert. Each node in the abstract syntax tree has two attributes, start_byte and end_byte, which can be used to determine its position range in the source code. The Codebert model is used to segment the code within this position range in the source code to extract tokens and embed the tokens into the 768-dimensional code. Since this position range may contain multiple tokens, the embedding of multiple tokens is summed and averaged as the embedding of the node in the abstract syntax tree. Finally, the node feature matrix and association matrix in the source code hypergraph representation are obtained, which serve as the input data for training the hypergraph neural network model.
[0100] The readability classification results are compared with the readability score labels corresponding to the source code to obtain the loss value and accuracy.
[0101] Based on the loss value and accuracy, adjust the parameters of the hypergraph neural network model until the loss value and accuracy meet the requirements, thus obtaining the trained hypergraph neural network model.
[0102] Specifically, the source code set is divided into a training set and a validation set according to a certain ratio. The training set and validation set are used for training and learning. During the training process, the parameters of the hypergraph neural network model are continuously adjusted according to the loss value until the loss value reaches an acceptable level. Training is then paused, and the trained weights are obtained. The test set is used to test and obtain the accuracy. If the accuracy meets the requirements, the final trained hypergraph neural network model is obtained. If the accuracy is low and does not meet the requirements, further refined training and fine-tuning can be carried out until the accuracy meets the requirements.
[0103] like Figure 8 As shown in the embodiment of this application, a code readability classification system based on hypergraph representation is provided, including:
[0104] Code acquisition module 10 is used to acquire the source code to be categorized.
[0105] Abstract Syntax Tree Building Module 20 is used to build an abstract syntax tree from source code.
[0106] The code hypergraph generation module 30 is used to adjust the edges between nodes with parent-child relationships in the abstract syntax tree into parent-child hyperedges to obtain the code hypergraph. Each node in the code hypergraph has a start mark and an end mark to determine the position range of the node in the source code. The feature of each node in the code hypergraph is the average value of all tokens contained in the position range corresponding to each node.
[0107] The row-level hyperedge construction module 40 is used to construct row-level hyperedges line by line from the code sequence of the source code, wherein the node features of the row-level hyperedges are tokens extracted by word segmentation of the code line by line.
[0108] The feature integration module 50 is used to obtain the node feature matrix and the association matrix based on the row-level hyperedges, nodes and node features in the source code, as well as the parent-child hyperedges, nodes and node features in the code hypergraph.
[0109] The classification module 60 is used to input the node feature matrix and the correlation matrix into the trained hypergraph neural network model to obtain readable classification results.
[0110] In this embodiment, the beneficial effects of the code readability classification system based on hypergraph representation are similar to those of the code readability classification method based on hypergraph representation described above, and will not be repeated here.
[0111] An electronic device provided in this application includes a memory and a processor; the memory is used to store a computer program; the processor is used to implement the code readability classification method based on hypergraph representation as described above when executing the computer program.
[0112] This application provides a computer-readable storage medium storing a computer program. When the computer program is executed by a processor, it implements the code readability classification method based on hypergraph representation as described above.
[0113] In this embodiment, the beneficial effects of electronic devices and computer-readable storage media are similar to those of the code readability classification method based on hypergraph representation described above, and will not be repeated here.
[0114] The present invention describes electronic devices that can serve as servers or clients of this application, which are examples of hardware devices that can be applied to various aspects of this application. Electronic devices are intended to represent various forms of digital electronic computer devices, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. Electronic devices can also represent various forms of mobile devices, such as personal digital assistant devices, cellular phones, smartphones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the application described and / or claimed herein.
[0115] Electronic devices include a computing unit that can perform various appropriate actions and processes based on a computer program stored in read-only memory (ROM) or loaded from a storage unit into random access memory (RAM). The RAM can also store various programs and data required for device operation. The computing unit, ROM, and RAM are interconnected via a bus. Input / output (I / O) interfaces are also connected to the bus.
[0116] Those skilled in the art will understand that all or part of the processes in the above embodiments can be implemented by a computer program instructing related hardware. The program can be stored in a computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. The storage medium can be a magnetic disk, optical disk, read-only memory (ROM), or random access memory (RAM), etc. In this application, the separately described modules may or may not be physically separate. Some or all of the modules can be selected to achieve the purpose of the embodiments of this application according to actual needs. Furthermore, the functional modules in the various embodiments of this application can be integrated into one processing unit, or each module can exist physically separately, or two or more modules can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0117] 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.
[0118] 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.
Claims
1. A code readability classification method based on hypergraph representation, characterized in that, include: Obtain the source code to be categorized; Construct an abstract syntax tree from the source code; The edges between nodes with parent-child relationships in the abstract syntax tree are adjusted to parent-child hyperedges to obtain the code hypergraph. Each node in the code hypergraph has a start mark and an end mark to determine the position range of the node in the source code. The feature of each node in the code hypergraph is the average value of all tokens contained in the position range corresponding to each node. Line-level hyperedges are constructed line by line for the code sequence of the source code, where the node features of the line-level hyperedges are tokens extracted from the code by line segmentation; Based on the row-level hyperedges, nodes, and node features in the source code, as well as the parent-child hyperedges, nodes, and node features in the code hypergraph, the node feature matrix and the association matrix are obtained. By inputting the node feature matrix and correlation matrix into the trained hypergraph neural network model, the readability classification results are obtained.
2. The code readability classification method based on hypergraph representation as described in claim 1, characterized in that, The hypergraph neural network model includes a hypergraph convolution module, a hypergraph attention module, a hypergraph pooling module, and a classifier; The process of inputting the node feature matrix and correlation matrix into the trained hypergraph neural network model yields readability classification results, including: The node feature matrix and the correlation matrix are input into the hypergraph convolution module to obtain the first updated node feature matrix. The first updated node feature matrix does not change the number of nodes in the node feature matrix, but only updates the node feature dimension. The node feature matrix and association matrix are input into the hypergraph attention module to obtain the node feature matrix updated in the second time. The number of nodes remains unchanged in the second update node feature matrix, and only the node feature dimension is updated. The node feature matrix of the second update is input into the hypergraph pooling module to obtain the aggregated feature matrix. The aggregated feature matrix is based on the node feature matrix of the second update, where the node number dimension is aggregated to one dimension, while the node feature dimension remains unchanged. The aggregated feature matrix is input into the classifier to obtain the readability classification result.
3. The code readability classification method based on hypergraph representation as described in claim 2, characterized in that, The hypergraph convolution module is used to statically update the node feature values in the node feature matrix; Among them, X (l) X represents the node feature values in the updated node feature matrix after the hypergraph convolution module update, σ represents the non-linear activation function, D and B represent the degree matrix of the node and the degree matrix of the hyperedge in the code hypergraph, respectively, H represents the association matrix, W represents the hyperedge weight matrix, and X represents the hyperedge weight matrix. (l-1) P1 represents the node feature matrix of the input hypergraph convolution module, and P2 represents the weight matrix of the hypergraph convolution module.
4. The code readability classification method based on hypergraph representation as described in claim 2, characterized in that, The hypergraph attention module uses an attention mechanism to dynamically learn and update node feature values; in, Let represent the node feature value of the i-th node in the second-order updated node feature matrix after being updated by the hypergraph attention module. LayerNorm is a function that normalizes each feature dimension. K represents the number of attention heads, and E... i V represents the set of hyperedges connected to node i. e Let represent the set of all nodes contained in the e-th superedge. P1 represents the node feature value of the j-th node in the updated node feature matrix of the input hypergraph attention module, and P2 is the first weight matrix. This represents the attention weight of node j to hyperedge e in the k-th attention head; Where exp represents the natural exponential function, a k Let P3 represent the scoring function of the k-th attention head, and let P3 represent the second weight matrix. and Let e represent the node feature values of the j-th node and the t-th node in the node feature matrix during a single update. e This represents the set of all nodes contained within the hyperedge e connected to node i.
5. The code readability classification method based on hypergraph representation as described in claim 1, characterized in that, It also includes training the hypergraph neural network model, the training process including: Obtain a source code set, wherein each line of code in the source code set is assigned a corresponding readability score tag, and the source code set is divided into a readable source code set and an unreadable source code set according to the readability score tags; Based on the source code in the source code set, construct the row-level hyperedge and code hypergraph corresponding to each source code; Based on the row-level hyperedges in the source code and the parent-child hyperedges in the code hypergraph, the node feature matrix and the association matrix are obtained; By inputting the node feature matrix and association matrix into the constructed hypergraph neural network model, the readability classification results are obtained; The readability classification results are compared with the readability score labels corresponding to the source code to obtain the loss value and accuracy. Based on the loss value and accuracy, adjust the parameters of the hypergraph neural network model until the loss value and accuracy meet the requirements, thus obtaining the trained hypergraph neural network model.
6. A code readability classification system based on hypergraph representation, characterized in that, include: The code acquisition module is used to acquire the source code to be categorized. Abstract syntax tree building module, used to build abstract syntax trees from source code; The code hypergraph generation module is used to adjust the edges between nodes with parent-child relationships in the abstract syntax tree into parent-child hyperedges to obtain the code hypergraph. Each node in the code hypergraph has a start mark and an end mark to determine the position range of the node in the source code. The feature of each node in the code hypergraph is the average value of all tokens contained in the position range corresponding to each node. The row-level hyperedge construction module is used to construct row-level hyperedges line by line from the code sequence of the source code. The node features of the row-level hyperedges are tokens extracted by word segmentation of the code line by line. The feature integration module is used to obtain the node feature matrix and the association matrix based on the row-level hyperedges, nodes and node features in the source code, as well as the parent-child hyperedges, nodes and node features in the code hypergraph; The classification module is used to input the node feature matrix and association matrix into the trained hypergraph neural network model to obtain readable classification results.
7. An electronic device, characterized in that, Including memory and processor; The memory is used to store computer programs; The processor is configured to implement the code readability classification method based on hypergraph representation as described in any one of claims 1 to 5 when executing the computer program.
8. A computer-readable storage medium, characterized in that, The storage medium stores a computer program that, when executed by a processor, implements the code readability classification method based on hypergraph representation as described in any one of claims 1 to 5.