A code readability evaluation method based on a program graph and a graph neural network
By constructing a program graph and combining it with a graph neural network, the shortcomings of existing code readability assessment models in terms of semantic information extraction and deep learning fusion are addressed, resulting in higher prediction accuracy.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BEIJING UNIV OF TECH
- Filing Date
- 2022-09-11
- Publication Date
- 2026-07-24
Smart Images

Figure CN116048614B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the fields of software theory, software engineering, and services, and relates to a code readability assessment method, specifically a method based on program graphs and graph neural networks. This invention introduces graph representation learning methods based on graph neural networks, which can effectively extract syntactic and semantic information, thereby obtaining more accurate code readability assessment results. Background Technology
[0002] Modern software operation and development require programmers to extend or modify existing code. Therefore, a programmer's ability to correctly and effectively understand source code determines their work efficiency. In other words, the higher the code readability, the easier the software is to maintain and redevelop. Code readability is defined as the ease with which developers can read and understand source code. Code readability assessment is closely related to software quality and greatly affects software characteristics such as portability and reusability.
[0003] In code readability assessment research, model evaluation results predict the level of code readability. Given the importance of code readability, recent studies have proposed numerous code readability models. Code representation is the foundation and prerequisite for readability assessment, and existing readability models suffer from the following two problems in code representation. First, traditional feature engineering-based representation schemes are highly subjective and have poor generalization ability, making them difficult to integrate organically with currently widely adopted deep learning frameworks. Second, sequence representation schemes for neural networks lack effective representation of high-level semantics and other readability-related information. For example, character sequence and word sequence models mostly capture structural information of the code. However, code itself is rich in semantic information such as data or control, and different statements produce different functions. Existing models lack relevant representations of code space structure, data flow, and control flow, severely limiting model performance.
[0004] To address the shortcomings of current code representation methods, such as the lack of semantic information extraction and difficulty in organically integrating with deep learning frameworks, this invention proposes an effective graph representation method that simultaneously improves the accuracy of readability classification. Summary of the Invention
[0005] The purpose of this invention is to provide a code readability assessment method based on program graphs and graph neural networks. This method learns syntactic and semantic features from code fragments through abstract syntax trees and graph neural networks, thereby obtaining more accurate code readability classification results. To achieve the above objective, this invention adopts the following technical solution:
[0006] Step 1: Data preprocessing.
[0007] Step 1.1: Data collection. Collect labeled readable, neutral, and unreadable code datasets.
[0008] Step 1.2: Graph representation, encoding the labeled dataset obtained in Step 1.1.
[0009] Step 1.2.1: Program graph representation based on abstract syntax tree. Convert the code snippet into an abstract syntax tree (AST) to serve as the main input to the graph neural network.
[0010] Step 1.2.2: Add control flow. Connect the statements that the program may traverse during code execution, representing all possible scenarios.
[0011] Step 1.2.3: Add data flow. Track the usage of variables during runtime, connecting all the usage (calls and modifications) of each variable in the code.
[0012] Step 1.2.4: Mark the flows from Steps 1.2.2 and 1.2.3 and add them as edges to the program graph. The final output is a combination of nodes and edges, i.e., in the form of a graph.
[0013] Step 2: Graph node embedding based on pre-trained model and one-hot encoding.
[0014] Step 2.1: For the code dataset obtained in Step 1.1, use a Bert-type pre-trained model to perform vector representation. Divide the source code into code and comments as model input, and after training, obtain a one-dimensional code information representation vector.
[0015] Step 2.1.1: The code and comments are separately segmented into symbol sequences, using... <s>or< / s> Used as a separator, and then connected before and after to form a complete symbol sequence.
[0016] Step 2.1.2: Input the symbol sequence obtained in step 2.1.1 into the pre-trained model and output a two-dimensional vector.
[0017] Step 2.1.3: For the above two-dimensional vector, since the first dimension is not uniform, we use summation and averaging to make the value of the first dimension consistent, and finally obtain a one-dimensional code information representation vector.
[0018] Step 2.2: Based on the graph obtained in Step 1.2, transform the node type in the graph using one-hot encoding to represent it as a one-dimensional vector.
[0019] Step 2.3: Combine the one-dimensional information vectors obtained in steps 2.1 and 2.2 by concatenation to obtain a one-dimensional vector representation.
[0020] Step 3: Code readability assessment based on graph neural networks.
[0021] Step 3.1: Use the combined one-dimensional vector obtained in Step 2.3 as the initial node representation of the graph, and represent the edges in the program graph in Step 1.2 using an adjacency matrix.
[0022] Step 3.2: Input the adjacency matrix and initial node representations of the graph into the graph convolutional network for training. The graph convolutional network extracts features at the node structure level.
[0023] Step 3.3: Based on the features extracted in Step 3.2, a pooling method is provided to generate a supernode for graph-level representation.
[0024] Step 3.4: Using the node information obtained from the pooling mechanism in Step 3.3, train a graph neural network to capture structural information. Then, use a classifier to obtain the readability classification prediction result.
[0025] Current code readability assessment models primarily extract structural information from code snippets, neglecting some crucial semantic information. Compared to existing research, this invention provides a code readability classification method based on abstract syntax trees (ASTs), program graphs, and graph neural networks. This method extracts semantic information from the code, improving prediction accuracy. Attached Figure Description
[0026] Figure 1 This is a flowchart illustrating the implementation of the present invention.
[0027] Figure 2 This is a schematic diagram of the program graph based on the abstract syntax tree of the present invention. Detailed Implementation
[0028] The present invention will be further described in detail below with reference to specific embodiments and accompanying drawings.
[0029] The technical solution adopted in this invention is a code readability assessment method based on program graphs and graph neural networks. This method is an automatic three-classification method for code readability assessment, and the specific implementation steps are as follows:
[0030] Step 1: Data preprocessing.
[0031] Step 1.1: Data Collection. Collect labeled Java readable, neutral, and unreadable code datasets. Industry-standard datasets like D can be used directly. Scalabrino It contains a total of 200 available, annotated code snippets, of which 50 are readable, 50 are unreadable, and 100 are neutral. For example, here is a readable code snippet:
[0032]
[0033] Step 1.2: Graph representation, encoding the labeled dataset obtained in Step 1.1.
[0034] Step 1.2.1: Based on the abstract syntax tree (AST), the program graph representation is converted into an abstract syntax tree (AST) using the JavaParser tool. This AST serves as the main input to the graph neural network. Figure 2 As shown.
[0035] Step 1.2.2: Add control flow. Connect the statements that the program may traverse during code execution, representing all possible scenarios.
[0036] Step 1.2.3: Add data flow. Track the usage of variables during runtime, connecting all the usage (calls and modifications) of each variable in the code.
[0037] Step 1.2.4: Tag the streams from Steps 1.2.2 and 1.2.3 and add them as edges to the abstract syntax tree. The final output is a combination of nodes and edges, i.e., in the form of a graph.
[0038]
[0039] Step 2: Node embedding based on pre-trained model and one-hot encoding.
[0040] Step 2.1: For the code dataset obtained in Step 1.1, use the CodeBert pre-trained model to perform vector representation. Divide the source code into code and comments as model input, and after training, obtain a one-dimensional code information representation vector.
[0041] Step 2.1.1: The code and comments are separated into individual tags, forming two sequences. Then use... <s>< / s> As a separator, it is placed around the two sequences mentioned above to distinguish between code and code comments.
[0042]
[0043] Step 2.1.2: Input the pre-trained model into the input and output a two-dimensional vector.
[0044]
[0045]
[0046] Step 2.1.3: For the above two-dimensional vector, since the first dimension is not uniform, the solution is to use summation and averaging to make the value of the first dimension consistent, and finally obtain a one-dimensional code information representation vector.
[0047]
[0048] Step 2.2: Based on the graph obtained in Step 1.2, transform the node type in the graph using one-hot encoding to represent it as a one-dimensional vector.
[0049]
[0050] Step 2.3: Combine the one-dimensional information vectors obtained in steps 2.1 and 2.2 by concatenation to obtain a one-dimensional vector representation.
[0051]
[0052] Step 3: Code readability evaluation based on graph convolutional networks, DMoN pooling, and k-dimensional graph neural networks.
[0053] Step 3.1: Use the combined one-dimensional vector obtained in Step 2.3 as the initial node representation of the graph, and represent the edges in the program graph in Step 1.2 using an adjacency matrix.
[0054] Step 3.2: Input the adjacency matrix and initial node representations of the graph into the graph convolutional network for training. The graph convolutional network extracts features from the program graph at the node structure level.
[0055] H (l+1) =σ(AH (l) W (l) )
[0056] Step 3.3: Based on the features extracted in Step 3.2, a DMoN pooling method is provided to generate a supernode representing a graph level. The above pooling method is based on node information. Considering that node information can only be passed through edges, it is difficult to summarize it into a higher-level graph representation.
[0057] Step 3.4: Using the node information obtained from the pooling mechanism in Step 3.3, k-GNNs layers are used to pass messages between subgraph structures and capture structural information. Then, a multilayer perceptron is used to obtain a readable classification prediction result.
[0058] Experimental results show that the accuracy rates for three-class and two-class classification of code readability can reach 67% and 88%, respectively. This demonstrates that by employing graph representation and training with graph neural networks, the model can effectively acquire semantic information and significantly improve model efficiency.
Claims
1. A code readability evaluation method based on program graphs and graph neural networks, characterized in that: The steps of this method are as follows: Step 1: Data preprocessing; Step 1.1: Data collection, collecting labeled readable, neutral, and unreadable code datasets; Step 1.2: Program graph representation, encoding the labeled code dataset obtained in Step 1.1; Step 1.2.1: Program graph representation based on abstract syntax tree; convert code snippets into abstract syntax trees as input to graph neural networks; Step 1.2.2: Add control flow; Connect the statements that the program may traverse during code execution to represent all possible scenarios; Step 1.2.3: Add a data stream; Track the usage of variables during runtime and connect all the usage of each variable in the code; Step 1.2.4: Mark the flows from Steps 1.2.2 and 1.2.3 and add them as edges to the program graph. The final output uses a combination of nodes and edges. Step 2: Embedding program graph nodes based on BERT-class pre-trained models and one-hot encoding; Step 2.1: For the code dataset obtained in Step 1.1, use a Bert-type pre-trained model to perform vector representation. Divide the source code into code and comments as input to the Bert-type pre-trained model. After training, a one-dimensional information vector is obtained. Step 2.1.1: The code and comments are separately segmented into symbol sequences, using... <s> or< / s> As a separator, the symbols are then joined together to form a complete sequence of symbols; Step 2.1.2: Input the symbol sequence obtained in step 2.1.1 into the pre-trained Bert-type pre-trained model and output a two-dimensional vector; Step 2.1.3: For the above two-dimensional vector, the summation and averaging are used to keep the value of the first dimension consistent, and finally a one-dimensional information vector is obtained; Step 2.2: Based on the program graph representation obtained in Step 1.2, the node types in the program graph are transformed using one-hot encoding to represent them as one-dimensional information vectors; Step 2.3: Combine the one-dimensional information vectors obtained in steps 2.1 and 2.2 by concatenation to obtain the combined one-dimensional vector representation; Step 3: Code readability assessment based on graph neural networks; Step 3.1: Use the combined one-dimensional vector representation obtained in Step 2.3 as the initial node representation of the program graph, and represent the edges in the program graph in Step 1.2 using an adjacency matrix; Step 3.2: Input the adjacency matrix and the initial node representation of the program graph into the graph convolutional network for training; the graph convolutional network extracts its features at the node structure level; Step 3.3: Based on the features extracted in Step 3.2, a pooling method is provided to generate a supernode for graph-level representation; Step 3.4: Use the information of the supernodes obtained by the pooling mechanism in Step 3.3 to train a graph neural network to capture structural information; then use a classifier to obtain the readability classification prediction results.