A code search method and system based on multi-structure graph fusion
By constructing a functional program graph and using meta-path aggregation graph neural networks and graph attention mechanisms, the shortcomings of existing code search models in semantic matching are addressed, achieving more efficient code retrieval.
Patent Information
- Application Number
- CN202511590209.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-03
- Publication Date
- 2026-01-23
- Estimated Expiration
- 2045-11-03
AI Technical Summary
Existing code search models are insufficient in capturing complex syntactic structures and logical connections, resulting in a lack of representational ability in semantic matching, which limits the accuracy and generalization ability of retrieval.
By introducing abstract syntax trees, control flow graphs, and data dependency graphs from code snippets, a functional program graph is constructed. Then, using meta-path aggregation graph neural networks and graph attention mechanisms, information weights are dynamically adjusted to optimize the query matching process.
It accurately captures code features and semantic information, improving the accuracy and generalization ability of code search, and enhancing the retrieval efficiency and quality of code snippets.
Smart Images

Figure CN121050768B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to a code search method and system based on multi-structure graph fusion, belonging to the technical field of computer software. BACKGROUND
[0002] In the field of software engineering, most people engaged in software development usually spend about one-third of the development time in retrieving the required relevant code fragments in large code libraries, because they can effectively improve the software development efficiency and quality, and significantly reduce the software development cost by reusing or rewriting the existing code. However, due to the complexity and diversity of the code, its intelligentization has not yet reached the expected level, and the mapping between the searched code and the query and their semantic association are often ignored, which destroys the structural integrity and leads to a lack of sufficient representation ability in semantic matching. SUMMARY
[0003] The purpose of the present application is to overcome the above-mentioned problems in the prior art. In the code search task, the lack of functional semantic expression in the search model limits the ability to capture complex syntax structures and logical associations. This limitation makes it difficult for the model to accurately align with the intent of the natural language query when generating code representations, thereby limiting the accuracy and generalization ability of the search. Therefore, it is necessary to propose a code search method based on graph representation and multi-structure fusion. This method not only uses graph representation learning to extract complex semantic association information of the code, but also retains the global dependency relationships and structural information under the query conditions by fusion, dynamically adjusts the information weight ratio, and further enhances the search ability of this method.
[0004] Technical solution: A code search method based on multi-structure graph fusion, which introduces the abstract syntax tree (AST), control flow graph (CFG), and data dependency graph (DDG) information of the code fragment, and through an early fusion strategy based on graph optimization, the abstract syntax tree, data dependency graph, and control flow graph of the code statement are fused to build a functional program graph, which accurately captures the code features and semantic information. For code feature representation, this method uses a meta-path aggregation graph neural network to capture the global structural information of the heterogeneous graph, and by introducing a graph attention mechanism, avoids the problem of missing intermediate node information and repeated aggregation of nodes. Finally, the similarity matching of the model is optimized through the loss function, and the accurate required code fragment is returned by completing the code search.
[0005] Specifically, the method comprises the following steps:
[0006] (1) Starting from the code side, the multi-dimensional features of the code fragments in the code library are extracted, and through an early fusion strategy based on graph optimization, the abstract syntax tree, data dependency graph, and control flow graph of the code statement are fused to build a functional program graph, which accurately captures the code features and semantic information, and obtains an enhanced code feature representation.
[0007] (2) For code feature representation, a meta-path aggregated graph neural network is used to capture the global information of the functional program graph. By introducing a graph attention mechanism and a meta-path graph structure, the selection of meta-paths is dynamically adjusted and the repetition and omission of node information are avoided. Different information is assigned appropriate weights. Finally, after processing by the meta-path aggregated graph neural network, it is converted into a code feature vector.
[0008] (3) The natural language query is converted into a vector representation by a query encoder and embedded into a unified vector space to obtain a query vector. The similarity between the query vector and the code snippet vector is calculated, the cosine similarity between the query vector and the code vector is calculated, and the loss function is optimized for query matching. Thus, the top k code snippets with higher scores are returned according to the similarity ranking.
[0009] The specific steps of the early fusion strategy based on graph optimization are as follows:
[0010] (1.1) Input the control flow graph G CF , the data dependency graph G DD and the abstract syntax tree G AST into the code search model. These three graphs capture the control structure, data flow information and syntax hierarchy of the code snippet, respectively, serving as the basic data source for the construction of the functional program graph.
[0011] (1.2) Initialize the functional program graph G CF-DD-AST as an empty graph represented by an adjacency list. It is used to integrate the control flow graph, data dependency graph and syntax structure graph information. The node set and edge set of the functional program graph will be obtained by merging the control flow graph G CF , data dependency graph G DD and abstract syntax tree G AST .
[0012] (1.3) First, traverse each edge (ui, uj) in the control flow graph G CF . The edge (ui, uj) represents the control flow relationship from node ui to node uj in the program. For each edge: if node ui or uj does not currently exist in the functional program graph G CF-DD-AST , add it to the node set of the functional program graph G CF-DD-AST . Add an edge (ui, uj, 0) with type 0 to the functional program graph G CF-DD-AST , where 0 represents that the edge (ui, uj) comes from the control flow graph G CF .
[0013] (1.4) Then traverse the data dependency graph G DDEach edge (ui, uj) in the function graph G represents a dependency relationship between variables in the program. Each edge is processed as follows: if an edge (ui, uj) or its reverse edge (uj, ui) does not exist in the function graph G... CF-DD-AST In the edge set, add an edge (ui, uj, 1) with type label 1, where 1 indicates that the edge belongs to the data dependency graph G. DD This step avoids symmetrically adding edges to the graph while preserving the structural differences between control flow and data flow.
[0014] (1.5) Then traverse the abstract syntax tree G. AST Each structural edge (ui, uj) in the functional program graph G represents a parent-child hierarchical relationship or a connection of grammatical rules within the grammatical structure. If ui or uj is not yet in the functional program graph G... CF-DD-AST If an element appears in the graph, it is added as a new node; an edge (ui, uj, 2) with type label 2 is added to the graph, where 2 indicates that the edge originates from the abstract syntax tree G. AST .
[0015] (1.6) Finally, return to the functional program diagram G after the fusion is complete. CF-DD-AST This graph uniformly encodes three types of program semantic information: control flow (type 0), data dependency (type 1), and syntactic structure (type 2), providing structural support for subsequent code representation learning and graph neural network modeling.
[0016] After constructing the functional program graph, a meta-path aggregation graph neural network is needed to process different edge types in the graph, distinguishing between control flow, data dependencies, and syntax tree relationships, thereby extracting the functional program graph G. CF-DD-AST By analyzing code features, we can enhance our understanding of the semantic structure of the code. The specific steps are as follows:
[0017] (2.1) Feature extraction is performed using a code search model. During extraction, three different types of heterogeneous information need to be processed: normalization and alignment at the statement level, and linear mapping to project the representations of each type of node onto the same vector space to achieve consistency in dimensionality and distribution, thus better representing the code's execution logic, data dependencies, and syntactic structure. The three different types of heterogeneous information refer to control flow information, data dependency information, and abstract syntactic information. To project the representations of each type of node onto the same vector space, a linear transformation is performed on each type of node, as follows:
[0018]
[0019] in It is the original feature vector generated by encoding the code statement corresponding to node u. M is the projection of node u. dA W represents a vector space of dimension dA. A It is the type-aware feature transformation parameter weight matrix in the meta-path aggregation graph neural network;
[0020] (2.2) The meta-path aggregation graph neural network determines how to connect with neighboring nodes through different edge types based on the specified meta-path P, i.e., according to the requirements. It collects the features of the target node's neighboring nodes w and performs weighted aggregation based on the edge type. The formula for information aggregation is as follows:
[0021]
[0022] in, It is the feature of node u at layer t+1 after aggregation and update. Let N(u) represent the feature of neighbor node w at layer t, and N(u) be the set of neighbor nodes of node u. The neighbor node weights are calculated by the attention mechanism, and W represents the learnable weight matrix used for linear variation.
[0023] (2.3) By introducing a graph attention mechanism, different attention weights are assigned to different neighbor nodes. Under this mechanism, the relationship between a node and its neighbor nodes is determined by the attention coefficient. Decide,
[0024]
[0025] in This represents the attention parameter vector associated with the meta-path P. This represents the feature vector of the target node u in the current layer for feature updating. This represents the features obtained by aggregating meta-path instances; where a meta-path instance refers to a specific path corresponding to the type of meta-path P, that is, in a heterogeneous graph, a complete path that starts from the target node u, follows a specified type order, and finally reaches the neighbor node w.
[0026] (2.4) After completing the linear transformation of node information and weighted aggregation of neighbor nodes in steps (2.1)-(2.3), for each target node u, the code search model collects the features of neighbor nodes w related to node u based on the specified meta-path, and performs weighted summation according to the edge type. The model uses the Softmax function to normalize and update all selected paths:
[0027]
[0028]
[0029] in This represents all neighboring nodes of the target node u. This represents the attention coefficient under the metapath P. This refers to the exponential function used to transform the raw attention score. This represents the attention weights after softmax normalization. Represents a non-linear activation function. This represents the representation vector after aggregation calculation under the metapath P. This represents the set of neighboring nodes associated with node u under the metapath P.
[0030] After processing the code snippet using the aforementioned meta-path aggregation graph neural network, the feature representation of the code snippet and the relationships between nodes were successfully learned. To achieve effective matching between the query and the code snippet, semantic representation modeling of the query statement is also required. The specific steps are as follows:
[0031] (3.1) Semantic representation modeling of query statements presents the problem of varying code lengths. Therefore, each statement is first segmented to obtain a corresponding word sequence. Then, word embedding technology is used to convert each word into a vector, and the vectors from the word sequence are averaged and integrated to obtain the feature vector of each statement. Assuming the segmented statement consists of n words, it is represented as:
[0032] S= <s1,s2,…,s n >
[0033] Where n is the length of the sequence, the word vector sequence obtained after word embedding is:
[0034] Z s = <c s1 ,c s2 ,…,c sn >
[0035] Where c sn It is the nth word s in the sentence n The word vector representation obtained after word embedding;
[0036] (3.2) By integrating the vector representations of the word sequence through average pooling, the feature vector of the node statement is:
[0037]
[0038] (3.3) A similarity-based loss function is used to optimize the code search model. Unlike traditional ranking-based loss functions, this similarity-based loss function can handle the node similarity problem in graph structures, thus returning the top k code snippets by similarity ranking. The minimum loss formula used in this method is as follows:
[0039]
[0040] in This represents the semantic and structural information in the code function diagram, while This represents the feature vector of positive samples that semantically match the query statement. Let represent the k-th negative sample feature vector that does not semantically match the query statement, and sim is a similarity function used to quantify the degree of similarity between two feature vectors in the semantic space.
[0041] A code search system based on multi-structure graph fusion includes the following modules:
[0042] Module 1 is used to extract multimodal features from source code and fuse and construct functional program graphs. The implementation process is as follows: starting from the code end, multidimensional features of code fragments in the code library are extracted, and the abstract syntax tree, data dependency graph and control flow graph of code statements are fused and constructed into a functional program graph through an early fusion strategy based on graph optimization.
[0043] Module 2 is used to capture global information of the functional program graph. The implementation process is as follows: For code feature representation, a meta-path aggregation graph neural network is used to capture global information of the functional program graph. By introducing a graph attention mechanism and a meta-path graph structure, the meta-path selection is dynamically adjusted to ensure that node information is not repeated or omitted, and corresponding weights are assigned to different information. Finally, after processing by the meta-path aggregation graph neural network, it is converted into a code feature vector.
[0044] Module 3 is used to return the top k code snippets. The implementation process is as follows: the natural language query is converted into a vector representation by a query encoder, and the vector representation of the query is embedded into a unified vector space to obtain the query vector. Then, the similarity between the query vector and the code snippet vector is calculated, the cosine similarity between the query vector and the code vector is calculated, and the query matching is optimized using a loss function. Thus, the top k code snippets with higher scores are returned according to the similarity ranking.
[0045] The implementation process of the code search system based on multi-structure graph fusion is the same as the implementation steps of the above methods, and will not be repeated here.
[0046] A computer device includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the code search method based on multi-structure graph fusion as described above.
[0047] A computer-readable storage medium having a computer program / instructions stored thereon, which, when executed by a processor, implements the steps of the code search method based on multi-structure graph fusion as described above.
[0048] Beneficial effects: Compared with existing technologies, the code search method based on multi-structure graph fusion of this invention has the following advantages:
[0049] (1) By using an early fusion strategy to fuse multiple types of information in the code, the problem of limited semantic coverage and low information coupling in the later fusion method is overcome.
[0050] (2) By integrating the abstract syntax tree, control flow graph and data dependency graph of the code fragment into a functional program graph, the execution control, data flow dependency and hierarchical syntax structure are preserved in the graph structure at the same time, which makes up for the one-sidedness of single-mode feature expression and accurately captures code features and semantic information.
[0051] (3) For the feature representation of heterogeneous graphs, a meta-path aggregation graph neural network and graph attention mechanism are introduced to realize information propagation and dynamic weight allocation across layers and semantic relationships. While ensuring the semantic integrity of the code, key semantic elements are highlighted and the accuracy of code matching is improved. Attached Figure Description
[0052] Fig. 1 This is a flowchart of a method according to an embodiment of the present invention;
[0053] Fig. 2 This is a flowchart of the feature extraction process for the primitive path aggregation graph neural network. Detailed Implementation
[0054] The present invention will be further illustrated below with reference to specific embodiments. It should be understood that these embodiments are for illustrative purposes only and are not intended to limit the scope of the invention. After reading the present invention, any modifications of the present invention in various equivalent forms by those skilled in the art will fall within the scope defined by the appended claims.
[0055] like Figs. 1-2 As shown, the code search method based on multi-structure graph fusion includes the following steps:
[0056] (1) Code search based on structure graph fusion is divided into code end and query end. First, starting from the code end, the multimodal features of the source code are extracted from the code library using open source extraction tools. Through the early fusion strategy based on graph optimization, the abstract syntax tree, data dependency graph and control flow graph of the code statement are fused to construct the functional program graph, accurately capture code features and semantic information, and enhance the representation of code features.
[0057] (1.1) Input three types of source graphs: control flow graph G CF Data dependency graph G DD and Abstract Syntax Tree G AST The three elements respectively carry complementary semantic information such as the program's execution order, variable read / write dependencies, and syntax hierarchy, forming the basis for subsequent graph fusion;
[0058] (1.2) Initialization function program diagram G CF-DD-AST An empty graph is stored using an adjacency list to gradually aggregate nodes and edges from different sources, while reserving a set of edge types corresponding to control flow, data dependencies, and syntactic relationships;
[0059] (1.3) First, traverse the control flow graph G. CF For each edge (ui, uj) in graph G, if ui or uj does not currently exist in graph G. CF-DD-AST In the middle, it is included in the node set; then in G CF-DD-AST Insert a directed edge (ui, uj, 0) with type identifier 0 to preserve control flow constraints;
[0060] (1.4) Next, traverse the data dependency graph G. DD Given each edge (ui, uj) in G. If edge (ui, uj) and its reverse edge (uj, ui) do not exist in G. CF-DD-AST In the edge set, a directed edge (ui, uj, 1) with type identifier 1 is inserted to avoid symmetric redundancy and accurately express the direction of data dependency.
[0061] (1.5) Then traverse the abstract syntax tree G. AST Each structural edge (ui, uj) in G. If ui or uj is not already in G... CF-DD-AST If a node appears in the graph, it is added as a new node; a directed edge (ui, uj, 2) with type identifier 2 is inserted into the graph, thereby explicitly preserving the hierarchical topology of the abstract syntax tree;
[0062] (1.6) Finally, complete the functional program diagram G. CF-DD-ASTThe integrated construction of this function program graph uniformly encodes three types of program semantic information: control flow (type 0), data dependency (type 1), and syntax structure (type 2), providing complete and consistent structural support for subsequent code semantic representation learning based on graph neural networks.
[0063] (2) After constructing the feature representation of the functional program graph, this method uses a meta-path aggregation graph neural network to capture the global information of the graph. By introducing the meta-path graph structure and graph attention mechanism, the meta-path selection is dynamically adjusted and the repetition and omission of node information are avoided. Finally, it is converted into feature representation. The feature extraction process is as follows: Fig. 2 As shown, different information types are distinguished by color-coded labels, and nodes of different colors represent the characteristics and interaction relationships of various types of information.
[0064] (2.1) In the extraction process of the code search model, it is first necessary to process three different types of heterogeneous information (control flow information, data dependency information, and abstract syntax information). That is, normalization and alignment are first completed at the statement level, and then the different types of node representations are unified into the same vector space through linear mapping to ensure dimensional isomorphism and distribution alignment, so as to better represent the execution logic, data dependencies, and syntax structure of the code. The linear transformation formula for node information is as follows:
[0065]
[0066] in It is the original feature vector generated based on the code statement content corresponding to the node. M is the projection of node u. dA W represents a vector space of dimension dA. A It is the type-aware feature transformation parameter weight matrix in the meta-path aggregation graph neural network;
[0067] (2.2) The meta-path aggregation graph neural network selects a specific path structure based on the specified meta-path P, i.e., according to the requirements, to determine how to connect with neighboring nodes through different edge types. By collecting the features of its neighboring nodes w, the information of neighboring nodes is weighted and aggregated according to the edge type. The formula for information aggregation is as follows:
[0068]
[0069] in, It is the feature of node u at layer t+1 after aggregation and update. It is the set of neighboring nodes of node u. The weights of neighboring nodes are calculated by the attention mechanism. This represents a learnable weight matrix that linearly transforms the feature vectors of neighboring nodes.
[0070] (2.3) Graph Attention Mechanism: Different attention weights are assigned to different neighbor nodes by introducing a graph attention mechanism. Under this mechanism, the relationship between a node and its neighbor nodes is determined by the attention coefficient. Decide
[0071]
[0072] in This represents the attention parameter vector associated with the meta-path P. This represents the feature vector of the target node u in the current layer for feature updating. This represents features aggregated from the entire metapath instance;
[0073] (2.4) After completing the linear transformation of node information and the weighted aggregation of neighbor node information, in order to achieve feature aggregation and generate the final representation of each node, the code search model can dynamically allocate the weights of different features and fuse information from different types of edges to generate the final feature representation of each node. The model uses the Softmax function to normalize and update all selected paths:
[0074]
[0075]
[0076] in This represents all neighboring nodes of the target node u. This represents the attention correlation coefficient between the target node u and its neighbor node w along the metapath P. This refers to the exponential function used to transform the raw attention score. This represents the attention weights after softmax normalization. This represents the nonlinear activation function used in feature transformation processing. This represents the representation vector after aggregation calculation under the metapath P. This represents the set of neighboring nodes associated with node u under the metapath P.
[0077] (3) After obtaining the final vector representation of the code, in order to achieve effective matching between the query and the code fragments, it is also necessary to perform semantic representation modeling on the query statement. The natural language query is converted into a vector representation by a query encoder, the cosine similarity between the query and the code vector is calculated, and the query matching is optimized using a loss function, so as to return the top k most relevant code fragments based on the similarity.
[0078] (3.1) Due to the varying lengths of code statements, each statement is first segmented into words, i.e., the query text is segmented to obtain the corresponding word sequence. Then, word embedding technology is used to convert each word into a vector, and the vectors from the word sequence are averaged and integrated to obtain the feature vector of each statement. Assuming the segmented statement consists of n words, it is represented as:
[0079]
[0080] Where n is the length of the sequence, the word vector sequence obtained after word embedding is:
[0081]
[0082] (3.2) By integrating the vector representations of the word sequence through average pooling and embedding the word vectors, the feature vector of the node sentence is:
[0083]
[0084] Where c sn It is the nth word s in the sentence n The word vector representation obtained after word embedding;
[0085] (3.3) A similarity-based loss function is used to optimize the code search model. Unlike traditional ranking-based loss functions, this loss function can handle the node similarity problem in graph structures, thus returning the top k most relevant code snippets based on similarity ranking. The minimum loss formula used in this method is as follows:
[0086]
[0087] in It is a feature representation extracted from the functional program diagram of the code, and This represents the feature vector of positive samples that semantically match the query statement. This represents the feature vector of the k-th negative sample that does not semantically match the query statement.
[0088] Obviously, those skilled in the art should understand that the steps of the code search method based on multi-structure graph fusion or the modules of the code search system based on multi-structure graph fusion in the above embodiments of the present invention can be implemented using general-purpose computing devices. They can be centralized on a single computing device or distributed across a network of multiple computing devices. Optionally, they can be implemented using computer-executable program code, thereby storing them in a storage device for execution by a computing device. In some cases, the steps shown or described can be performed in a different order than those presented here, or they can be fabricated as separate integrated circuit modules, or multiple modules or steps can be fabricated as a single integrated circuit module. Thus, the embodiments of the present invention are not limited to any particular hardware and software combination.
Claims
1. A code search method based on multi-structure graph fusion, characterized in that, Specifically, the steps include the following: Step 1: Starting from the code side, extract the multidimensional features of code fragments in the code library, and use an early fusion strategy based on graph optimization to merge the abstract syntax tree, data dependency graph and control flow graph of the code statements to construct a functional program graph. Step 2: For code feature representation, a meta-path aggregation graph neural network is used to capture global information of the functional program graph. By introducing a graph attention mechanism and a meta-path graph structure, the meta-path selection is dynamically adjusted to ensure that node information is not repeated or omitted, and corresponding weights are assigned to different information. Finally, after processing by the meta-path aggregation graph neural network, it is converted into a code feature vector. Step 3: The natural language query is converted into a vector representation by the query encoder, and the vector representation of the query is embedded into a unified vector space to obtain the query vector. Then, the similarity between the query vector and the code fragment vector is calculated. The cosine similarity between the query vector and the code vector is calculated, and the query matching is optimized using a loss function. The top k code fragments with the highest scores are returned according to the similarity ranking. A similarity-based loss function was used to optimize the code search model, returning the top k code snippets based on similarity ranking. The formula for minimizing the loss is as follows: ; in This represents the semantic and structural information in the code function diagram, while This represents the feature vector of positive samples that semantically match the query statement. Let represent the k-th negative sample feature vector that does not semantically match the query statement, and sim is a similarity function used to quantify the degree of similarity between two feature vectors in the semantic space.
2. The code search method based on multi-structure graph fusion according to claim 1, characterized in that, The specific steps for implementing the graph-optimized early fusion strategy are as follows: Step 1.1, Input control flow graph G CF Data dependency graph G DD and Abstract Syntax Tree G AST Into the code search model; Step 1.2, Initialize the function program diagram G CF-DD-AST This is an empty graph, represented using an adjacency list, used to unify and integrate information from the control flow graph, data dependency graph, and syntax structure graph. The node set and edge set of the function graph will be gradually merged into the control flow graph G later. CF Data dependency graph G DD and Abstract Syntax Tree G AST The three seed diagrams are obtained; Step 1.3, Traverse the control flow graph G CF Each edge (ui, uj) in the graph represents a control flow relationship between node ui and node uj in the program. For each edge: if node ui or uj is not currently present in the functional program graph G... CF-DD-AST In the middle, it is added to the functional program diagram G. CF-DD-AST The set of nodes; in the functional program graph G CF-DD-AST Add an edge (ui, uj, 0) with a type label of 0, where 0 indicates that the edge (ui, uj) originates from the control flow graph G. CF ; Step 1.4, Traverse the data dependency graph G DD Each edge (ui, uj) in the graph represents a dependency relationship between variables in the program. Each edge is processed as follows: if an edge (ui, uj) or its reverse edge (uj, ui) does not exist in the functional program graph G... CF-DD-AST In the edge set, add an edge (ui, uj, 1) with type label 1, where 1 indicates that the edge belongs to the data dependency graph G. DD ; Step 1.5, Traverse the abstract syntax tree G AST Each structural edge (ui, uj) in the functional program graph G represents a parent-child hierarchical relationship or a connection of grammatical rules in the grammatical structure; if ui or uj is not yet in the functional program graph G... CF-DD-AST If an element appears in the graph, it is added as a new node; an edge (ui, uj, 2) with type label 2 is added to the graph, where 2 indicates that the edge originates from the abstract syntax tree G. AST ; Step 1.6, return to the completed functional program diagram G. CF-DD-AST .
3. The code search method based on multi-structure graph fusion according to claim 1, characterized in that, After constructing the functional program graph, a meta-path aggregation graph neural network is needed to process different edge types in the graph, distinguishing between control flow, data dependencies, and syntax tree relationships, thereby extracting the functional program graph G. CF-DD-AST By analyzing code features, we can enhance our understanding of the semantic structure of the code. The specific steps are as follows: Step 2.1: Extract features using a code search model. During the extraction process, three different types of heterogeneous information need to be processed first. That is, normalization and alignment are completed at the statement level, and the representations of each type of node are projected onto the same vector space through linear mapping to achieve consistency in dimensionality and distribution. The three different types of heterogeneous information refer to control flow information, data dependency information, and abstract syntax information, respectively. In order to project the representations of each type of node onto the same vector space, a linear transformation is performed on each type of node. Step 2.2: The meta-path aggregation graph neural network selects the specific path structure based on the specified meta-path P, i.e., according to the requirements, to determine how to connect with adjacent nodes through different edge types; it collects the features of the target node's neighboring nodes w, and performs weighted aggregation based on the edge type. The information aggregation formula is as follows: ; in, It is the feature of node u at layer t+1 after aggregation and update. Let N(u) represent the feature of neighbor node w at layer t, and N(u) be the set of neighbor nodes of node u. The neighbor node weights are calculated by the attention mechanism, and W represents the learnable weight matrix used for linear variation. Step 2.3 involves introducing a graph attention mechanism to assign different attention weights to different neighbor nodes; the relationship between a node and its neighbors is determined by the attention coefficients. Decide; Step 2.4: After completing the linear transformation of node information and weighted aggregation of neighbor nodes in steps 2.1-2.3, for each target node u, the code search model collects the features of neighbor nodes w related to node u based on the specified meta-path, and performs weighted summation according to the edge type; the code search model uses the Softmax function to normalize and update all selected paths.
4. The code search method based on multi-structure graph fusion according to claim 1, characterized in that, To achieve effective matching between queries and code snippets, semantic representation modeling of the query statement is required. The specific steps are as follows: Step 3.1 involves semantic representation modeling of the query statements. Due to the varying lengths of the code statements, each statement is first segmented to obtain the corresponding word sequence. Subsequently, word embedding technology is used to convert each word into a vector, and then the vectors of the word sequence are averaged and integrated to obtain the feature vector of each sentence; let... The sentence after word segmentation consists of n tokens, represented as: S=<s1,s2,…,s n > Where n is the length of the sequence, the word vector sequence obtained after word embedding is: Z s =<c s1 ,c s2 ,…,c sn > Where c sn It is the nth word s in the sentence n The word vector representation obtained after word embedding; Step 3.2, by integrating the vector representations of the word sequence through average pooling, the feature vector of the node sentence is: ; Step 3.3 employs a similarity-based loss function to optimize the code search model, returning the top k code snippets by similarity ranking.
5. A code search system based on multi-structure graph fusion, characterized in that, Includes the following modules: Module 1 is used to extract multimodal features from source code and fuse and construct functional program graphs. The implementation process is as follows: starting from the code end, multidimensional features of code fragments in the code library are extracted, and the abstract syntax tree, data dependency graph and control flow graph of code statements are fused and constructed into a functional program graph through an early fusion strategy based on graph optimization. Module 2 is used to capture global information of the functional program graph. The implementation process is as follows: For code feature representation, a meta-path aggregation graph neural network is used to capture global information of the functional program graph. By introducing a graph attention mechanism and a meta-path graph structure, the meta-path selection is dynamically adjusted to ensure that node information is not repeated or omitted, and corresponding weights are assigned to different information. Finally, after processing by the meta-path aggregation graph neural network, it is converted into a code feature vector. Module 3, used to return the top k code snippets, works as follows: A query encoder converts the natural language query into a vector representation, embeds this vector representation into a unified vector space to obtain the query vector, and then calculates the similarity between the query vector and the code snippet vectors. The cosine similarity between the query vector and the code snippet vectors is calculated, and a loss function is used to optimize query matching. The top k code snippets with the highest similarity scores are then returned. A similarity-based loss function is used to optimize the code search model, returning the top k code snippets based on similarity ranking. The minimum loss formula used is as follows: ; in This represents the semantic and structural information in the code function diagram, while This represents the feature vector of positive samples that semantically match the query statement. Let represent the k-th negative sample feature vector that does not semantically match the query statement, and sim is a similarity function used to quantify the degree of similarity between two feature vectors in the semantic space.
6. A computer device, characterized in that, The computer device includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the code search method based on multi-structure graph fusion as described in any one of claims 1-4.
7. A computer-readable storage medium having a computer program / instructions stored thereon, characterized in that, When the computer program / instruction is executed by the processor, it implements the steps of the code search method based on multi-structure graph fusion as described in any one of claims 1-4.
Citation Information
Patent Citations
Vulnerability detection method based on local and global message hierarchical modeling and fusion
CN120639369A
Intelligent contract vulnerability detection method based on heterogeneous graph attention network
CN120705873A