Semantic enhancement code retrieval method, system and storage medium based on program dependency
By constructing an abstract syntax tree and program graph, combined with a graph attention neural network and a Bi-LSTM network, the semantic mapping difficulty between code and natural language queries is solved, achieving more accurate code search and a better user experience.
Patent Information
- Application Number
- CN202411454232.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-17
- Publication Date
- 2025-09-09
- Estimated Expiration
- 2044-10-17
AI Technical Summary
Existing code retrieval technologies have difficulties in semantic mapping when processing code and natural language queries, structural information is not fully mined, and the limitations of graph neural networks have not been effectively addressed, resulting in insufficient retrieval accuracy and user experience.
A semantically enhanced code retrieval method based on program dependencies is adopted. By constructing an abstract syntax tree and program graph, using graph attention neural network and Bi-LSTM network, combining graph embedding and cross-domain vector representation, it captures the local structure and global dependency of code and text, and improves the accuracy of semantic mapping.
It improves the accuracy and user experience of the online code retrieval platform, provides more accurate query services, and enhances the effectiveness of code search.
Smart Images

Figure CN119441509B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a semantically enhanced code retrieval method, system, and storage medium based on program dependency. Background Art
[0002] With the rapid growth of the software industry over the past few years, the number of public and private repositories (e.g., on GitHub or Bitbucket) has reached an unprecedented level. It's widely recognized that the software industry is entering the "Big Code" era. Code search, which aims to identify key code in large code corpora (e.g., GitHub, Stack Overflow) based on natural language queries, has become a key issue in this "Big Code" era. Furthermore, some studies have shown that over 90% of software developers' efforts are focused on reusing existing code. Therefore, an accurate code search system can significantly improve software productivity and quality while reducing software development costs.
[0003] Some early code retrieval approaches used information retrieval (IR) techniques to capture the relationship between code and keyword-matching queries. However, IR techniques are limited when code and natural language do not share common keywords, resulting in poor performance. To address this issue, recent approaches have turned to deep learning (DL) techniques, which encode source code and queries into vectors (i.e., learn the representations behind the code and query) and then calculate the similarity between the two vectors (e.g., cosine similarity) to measure the semantic relevance between the code snippet and the query. Code snippets with higher similarity scores to the query are returned as search results. However, most of these works rely solely on sequential models, such as long short-term memory (LSTM) and self-attention models, to learn vector representations of code and queries. These sequence models have difficulty learning semantic relationships because they ignore the structural information hidden in the text.
[0004] Some approaches use abstract syntax trees (ASTs) and control flow graphs (CFGs) to parse code, but key challenges still exist. The current challenges are mainly three issues: (1) Code and natural language queries are heterogeneous, with completely different grammatical rules and language structures, which makes semantic mapping difficult; (2) The rich structural information behind code and queries cannot be mined. Failure to utilize rich structural information beyond simple text may limit the effectiveness of these methods for code search; (3) Although some existing works have attempted to use GNNs for code search, the limitations of GNNs have not been well addressed. Summary of the Invention
[0005] Based on the technical problems existing in the background technology, the present invention proposes a semantically enhanced code retrieval method, system and storage medium based on program dependencies, which accurately learns the semantic mapping of the program and the query of code search, thereby improving the accuracy and user experience of the online code retrieval platform.
[0006] The semantic enhancement code retrieval method based on program dependency proposed in the present invention inputs the text description to be retrieved into the code search model to output the code retrieval results;
[0007] The training process of the code search model is as follows:
[0008] Step 1: Obtain the code and the text description corresponding to the code to construct a training dataset, construct an abstract syntax tree for the code and extract it to obtain a program graph, and construct an abstract syntax tree for the text description and extract it to obtain a text graph;
[0009] Step 2: Calculate the cosine similarity between the i-th node vector in the text graph and all node vectors in the program graph, and use the obtained program similarity value as the weight of each node in the program graph. Based on the weight of each node in the program graph and each node vector in the program graph, calculate the weighted average embedding of all nodes in the program graph into the i-th node vector in the text graph, and obtain the text context global-level vector representation corresponding to the i-th node in the text graph. The i-th node vector in the text graph and the text context global-level vector representation are combined through the vector to obtain the text cross vector corresponding to the i-th node. Similarly, all text cross vectors are obtained, and each text cross vector replaces the corresponding node vector in the text graph to obtain the updated text graph.
[0010] Step 3. Calculate the cosine similarity between the ,th node vector in the program graph and all node vectors in the text graph, and use the obtained text similarity value as the weight of each node in the text graph. Based on the weight of each node in the text graph and each node vector in the text graph, calculate the weighted average embedding of all nodes in the text graph into the jth node vector in the program graph, and obtain the program context global-level vector representation corresponding to the jth node in the program graph. The ,th node vector in the program graph and the program context global-level vector representation are combined through the vector to obtain the program cross vector corresponding to the ,th node. Similarly, all program cross vectors are obtained, and each program cross vector replaces the corresponding node vector in the program graph to obtain the updated program graph.
[0011] Step 4: Feed the updated text graph and the updated program graph into two independently set graph attention neural networks respectively to obtain program graph representation and text graph representation; split the code and text description into sequences and input them into two independently set Bi-Lstm networks respectively to obtain program word representation and text word representation, concatenate the program word representation and program graph representation to obtain program vector, and concatenate the text word representation and text graph representation to obtain text vector;
[0012] Step 5: Construct a loss function based on the program vector and text vector to adjust the model parameters in the code search model.
[0013] Furthermore, in step 4, the two independently configured graph attention neural networks operate in the same way on the updated text graph and the updated program graph. The graph attention neural network processes the updated program graph as follows:
[0014] Based on the self-attention mechanism, the attention weights between the current node and other nodes in the updated program graph are calculated to determine the importance of different nodes to the current node;
[0015] Based on the attention weight, the self-attention mechanism is used to aggregate the neighbor information of the current node and output the backward aggregation vector of the current node;
[0016] By analogy, the backward aggregation vector of each node in the program graph is obtained, thereby obtaining the program graph representation.
[0017] Furthermore, the calculation formula of attention weight is as follows:
[0018]
[0019]
[0020] Among them, e ab represents the attention weight between the a-th node and the b-th node, W h is the weight matrix for linear transformation, Represents the weight vector matrix learned by the a-th node, D ab represents the attention weight of the a-th node to the b-th node, LeakyReLU represents the nonlinear activation function, Represents all neighbor nodes of node a in the program graph, e ak represents the attention weight between the a-th node and the k-th node, represents the node vector of the a-th node in layer 0, The node vector representing the b-th node in layer 0.
[0021] Furthermore, the calculation formula of the backward aggregation vector of the current node is as follows:
[0022]
[0023] is the weight matrix of the linear transformation of node features in the lth layer, represents the attention weight between the a-th node and the b-th node in the l-th layer, represents the node vector of the a-th node in the l-th layer, Represents the node vector of the b-th node in the l-1 layer.
[0024] Furthermore, in step 5, the loss function The build is as follows:
[0025]
[0026] Where θ represents the parameters of the code search model, c represents the program vector output by the code search model, q+ and q- represent the positive text vector and negative text vector output by the code search model respectively, ∈ represents a hyperparameter, t represents the paired code and text description pairs in the training dataset, C represents the code in the training dataset, Q+ represents the positive text description set in the training dataset that matches the code semantics, and Q- represents the negative text description randomly selected from the corpus of the training dataset excluding the positive text description.
[0027] Furthermore, in step one, an abstract syntax tree is constructed for the text description and a text graph is extracted. The text graph is represented as G = (V, E), where V is all nodes on the abstract syntax tree and E is an edge representing the relationship between nodes. The nodes are divided into two categories, one is the terminal nodes on the abstract syntax tree and the other is the non-terminal nodes on the abstract syntax tree. The terminal nodes on the abstract syntax tree correspond to identifiers in the code, while the non-terminal nodes on the abstract syntax tree represent different compilation units.
[0028] A semantically enhanced code retrieval system based on program dependencies inputs the text description to be retrieved into the code search model to output code retrieval results;
[0029] The code search model includes a building module, an extraction module, a text interaction module, a program interaction module, a graph attention neural network, a Bi-Lstm network, a splicing module, and a loss building module;
[0030] The training process of the code search model is as follows:
[0031] The construction module constructs a training data set based on the acquired codes and the text descriptions corresponding to the codes;
[0032] The extraction module is used to construct an abstract syntax tree for the code and extract it to obtain a program graph, and to construct an abstract syntax tree for the text description and extract it to obtain a text graph;
[0033] The text interaction module is used to calculate the cosine similarity between the i-th node vector in the text graph and all node vectors in the program graph. The obtained program similarity value is used as the weight of each node in the program graph. Based on the weight of each node in the program graph and each node vector in the program graph, the weighted average embedding of all nodes in the program graph into the i-th node vector in the text graph is calculated to obtain the text context global-level vector representation corresponding to the i-th node in the text graph. The i-th node vector in the text graph and the text context global-level vector representation are combined through vectors to obtain the text cross vector corresponding to the i-th node. This is repeated to obtain all text cross vectors. Each text cross vector is used to replace the corresponding node vector in the program graph to obtain an updated text graph.
[0034] The program interaction module is used to calculate the cosine similarity between the ,th node vector in the program graph and all node vectors in the text graph, and use the obtained text similarity value as the weight of each node in the text graph. Based on the weight of each node in the text graph and each node vector in the text graph, the weighted average embedding of all nodes in the text graph into the ,th node vector in the program graph is calculated to obtain the program context global-level vector representation corresponding to the j-th node in the program graph. The j-th node vector in the program graph and the program context global-level vector representation are combined through vectors to obtain the program cross vector corresponding to the ,th node. This is repeated to obtain all program cross vectors. Each program cross vector replaces the corresponding node vector in the program graph to obtain an updated program graph.
[0035] Two independently set graph attention neural networks are used to process the updated text graph and the updated program graph respectively to obtain program graph representation and text graph representation;
[0036] Two independently set Bi-Lstm networks are used to process the segmented code and text description respectively to obtain program word representation and text word representation;
[0037] The splicing module is used to splice the program word representation and the program graph representation to obtain the program vector, and to splice the text word representation and the text graph representation to obtain the text vector.
[0038] The loss building module builds a loss function based on the program vector and text vector to adjust the model parameters in the code search model.
[0039] A computer-readable storage medium stores a plurality of classification programs, wherein the plurality of classification programs are used to be called by a processor and execute the semantically enhanced code retrieval method as described above.
[0040] The advantages of the program dependency-based semantically enhanced code retrieval method, system and storage medium provided by the present invention are: the program dependency-based semantically enhanced code retrieval method, system and storage medium provided in the structure of the present invention design a graph attention neural network to capture local structural information in the graph, and use Bi-Lstm to capture global dependencies that the graph attention neural network cannot learn, so as to accurately learn the semantic mapping of the program and the query of code search; it can provide more accurate query services for online code retrieval, and improve the accuracy and user experience of programmer users when using the online code retrieval platform. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] Figure 1 It is a structural schematic diagram of the present invention;
[0042] Figure 2 This is a schematic diagram of the code syntax structure;
[0043] Figure 3 This is a schematic diagram of the code data flow;
[0044] Figure 4 Diagram of the text query syntax structure for the given text query "How to check for null". DETAILED DESCRIPTION
[0045] The technical solutions of the present invention are described in detail below through specific embodiments. Numerous specific details are set forth in the following description to facilitate a full understanding of the present invention. However, the present invention can be implemented in many other ways than those described herein, and those skilled in the art may make similar modifications without departing from the scope of the present invention. Therefore, the present invention is not limited to the specific embodiments disclosed below.
[0046] like Figures 1 to 4 As shown, the semantic enhancement code retrieval method based on program dependency proposed by the present invention inputs the code to be retrieved and the text description corresponding to the code into the code search model to output the code retrieval result;
[0047] The training process of the code search model is as follows:
[0048] Step 1: Obtain the code and the text description corresponding to the code to construct a training dataset, construct an abstract syntax tree for the code and extract it to obtain a program graph, and construct an abstract syntax tree for the text description and extract it to obtain a text graph;
[0049] The code is the cleaned standard function code, and the text description is the natural language description of the corresponding function code. The set of cleaned code and text description pairs F = {(c i ,q i )|ci ∈C,q i ∈Q}, i∈{1, 2, ..., n}, where F represents the set of codes and text descriptions, c i Indicates a single code, q i Represents a single text description, C represents all codes, Q represents all text descriptions, and n represents the total number of codes or the total number of text descriptions. Since codes and text descriptions are set in pairs, the total number between the two is consistent; a training dataset is constructed based on the set F.
[0050] The construction principles of program graphs and text graphs are the same. We will take the construction of text graphs as an example to illustrate. When constructing program graphs, you can just replace them directly. The text graph is represented by G = (V, E), where V is all the nodes on the abstract syntax tree (AST), and E is the edge representing the relationship between nodes. The nodes are divided into two categories, one is the terminal node on the abstract syntax tree, and the other is the non-terminal node on the abstract syntax tree. The terminal nodes on the abstract syntax tree correspond to the identifiers in the code, while the non-terminal nodes on the abstract syntax tree represent different compilation units, such as "Assign", "BinOp" and "Expr". Edges can be classified into AST edges, word order edges, cutting edges, neighbor read and write edges, and data flow edges. For details, see Figure 2 and 3 AST edges are based on the abstract syntax tree to connect all AST nodes; word order edges connect each leaf node in the AST to its successor node; cut edges define the connection of sub-tokens separated from identifiers in the code, that is, variable names and function names based on camelCase and pascal case conventions, for example, "fn_a" will be divided into "fn" and "a"; neighbor read and write edges represent the last read and write of each occurrence of a variable; data flow edges represent the flow of data between variables. Given a text description q, extract its dependency parsing graph as G = (V, E), where V is a set of nodes, each node is a word in the text description q, and E is a set of edges representing the dependency relationship between these tokens, for example, the edge "prep" is a prepositional modifier of a verb, adjective or noun, used to modify the meaning of verbs, adjectives, and nouns. The dependency parsing between text words is mainly based on the component parse tree and the dependency parse tree, and the upper and lower word order edges are also added. For details, see Figure 4 , given a text query "How to checkfor null", "How", "to" and "for" are used to describe the relationship with the verb "check" in the "advmod" (adverb modifier), "aux" (auxiliary verb), and "prep" (preposition) relations respectively.
[0051] The operations in steps 2 and 3 mainly include innovations in graph embedding and cross-domain vector representation learning. Specifically, a method is described for matching nodes in a text graph and a program graph using cosine similarity, and using this matching to update the vector representations of the nodes in the two graphs. The advantage of this operation is that by calculating the similarity between the nodes in the text graph and the program graph, the knowledge of two different domains (text and program code) can be integrated, so that the nodes in each domain can obtain richer semantic information. Through weighted average embedding and vector union, each node can not only capture the characteristics of its own domain, but also obtain cross-domain contextual information, thereby obtaining a more comprehensive representation. Using cosine similarity as the basis for node matching can effectively measure the similarity between node vectors, which is beneficial for subsequent tasks such as node classification, clustering, or recommendation. By replacing the original node vectors with the updated cross vectors, the structure of the graph can be dynamically adjusted to make it closer to the semantic relationships in practical applications.
[0052] The specific steps of step 2 are as follows: calculate the cosine similarity between the i-th node vector in the text graph and all the node vectors in the program graph, use the obtained program similarity value as the weight of each node in the program graph, and based on the weight of each node in the program graph and each node vector in the program graph, calculate the weighted average embedding of all nodes in the program graph into the i-th node vector in the text graph, and obtain the text context global-level vector representation corresponding to the i-th node in the text graph. The i-th node vector in the text graph and the text context global-level vector representation are combined through the vector to obtain the text cross vector corresponding to the i-th node. And so on, all the text cross vectors are obtained, and each text cross vector replaces the corresponding node vector in the text graph to obtain the updated text graph;
[0053] Calculate the cosine similarity between the i-th node vector in the text graph and all node vectors in the program graph:
[0054]
[0055] Among them, a i,j Represents the i-th node q in the text graph i and the jth node c in the program graph j The program similarity value between them, N represents the total number of nodes in the program graph.
[0056] The program similarity value a i,j As node c j Based on the weight of each node in the program graph and each node vector in the program graph, the weighted average embedding of all nodes in the program graph into the i-th node vector in the text graph is calculated to obtain the global level vector representation q of the text context corresponding to the i-th node in the text graph iG , qiG and q i Perform vector joint operation to form text cross vector q icon , use q icon Replace the corresponding node vector in the text graph. And so on, to get the updated text graph;
[0057]
[0058] Where N is the total number of nodes in the program graph.
[0059] Step 3. Calculate the cosine similarity between the ,th node vector in the program graph and all node vectors in the text graph, and use the obtained text similarity value as the weight of each node in the text graph. Based on the weight of each node in the text graph and each node vector in the text graph, calculate the weighted average embedding of all nodes in the text graph into the jth node vector in the program graph, and obtain the program context global-level vector representation corresponding to the jth node in the program graph. The ,th node vector in the program graph and the program context global-level vector representation are combined through the vector to obtain the program cross vector corresponding to the ,th node. Similarly, all program cross vectors are obtained, and each program cross vector replaces the corresponding node vector in the program graph to obtain the updated program graph.
[0060] The process of obtaining the updated program graph is similar to the process of obtaining the updated text graph in step 2. For example, in calculating the cosine similarity between the j-th node vector in the program graph and all node vectors in the text graph:
[0061]
[0062] Among them, a i,j Represents the i-th node q in the text graph i and the jth node c in the program graph j The program similarity value between them, K represents the total number of nodes in the text graph.
[0063] For example, in the computational program graph, the program context global level vector representation c corresponding to the jth node jG :
[0064]
[0065] Finally, c j and c jG Perform vector join operations to form program cross vector c jcon , using c jcon Replace the jth node c in the program graph j The node vector of , and so on, to obtain the updated program graph.
[0066] Step 4: Feed the updated text graph and the updated program graph into two independently set graph attention neural networks respectively to obtain program graph representation and text graph representation; split the code and text description into sequences and input them into two independently set Bi-Lstm networks respectively to obtain program word representation and text word representation, concatenate the program word representation and program graph representation to obtain program vector, and concatenate the text word representation and text graph representation to obtain text vector;
[0067] (A) Two independently configured graph attention neural networks process the updated text graph and the updated program graph in the same way. For example, given a graph G = (V, E), the graph attention neural network learns the node embedding vectors of the graph from both the incoming and outgoing directions. Each node r∈V is initialized by a learnable embedding matrix E and obtains its initial representation. d represents the dimension length of the node embedding vector. For node r, a function is applied to take as input a set of incoming neighboring node vectors and output a backward aggregation vector; the following graph attention neural network processes the updated program graph as follows:
[0068] (a1) Based on the self-attention mechanism, the attention weights between the current node and other nodes in the updated program graph are calculated to determine the importance of different nodes to the current node;
[0069] The calculation formula of attention weight is as follows:
[0070]
[0071]
[0072] Among them, e ab represents the attention weight between the a-th node and the b-th node, Wh is the weight matrix for linear transformation, Represents the weight vector matrix learned by the a-th node, D ab represents the attention weight of the a-th node to the b-th node, obtained by normalization, LeakyReLU represents the nonlinear activation function, Represents all neighbor nodes of node a in the program graph, e ak represents the attention weight between the a-th node and the k-th node, represents the node vector of the a-th node in layer 0, Represents the node vector of the b-th node in layer 0.
[0073] (a 2) Based on the attention weight, the self-attention mechanism is used to aggregate the neighbor information of the current node and output the backward aggregation vector of the current node;
[0074] Use the self-attention mechanism to aggregate the neighbor information of the node by using the attention weight D ij , the code search model can focus on those neighbor nodes that are more important to the current node i:
[0075] The calculation formula of the backward aggregation vector of the front node is as follows:
[0076]
[0077] Among them, σ represents the ReLU activation function, is the weight matrix of the linear transformation of node features in the lth layer, represents the attention weight between the a-th node and the b-th node in the l-th layer, represents the node vector of the a-th node in the l-th layer, Represents the node vector of the b-th node in the l-1 layer.
[0078] (a3) By analogy, the backward aggregation vector of each node in the program graph is obtained, thereby obtaining the program graph representation.
[0079] (B) The code and text description are split into sequences and fed into two independently configured Bi-Lstm networks. This is primarily to further capture global interactions in the graph that are missed by the graph attention neural network. Note that in step 1, the constructed program graph consists of terminal and non-terminal nodes, where terminal nodes are sequences and subsequences of the original code. Therefore, Bi-Lstm is directly applied to the sequence to capture the global dependencies between these terminal nodes, while ignoring non-terminal nodes for efficient learning. Finally, the overall vector representation is obtained by taking the average of the hidden layers.
[0080] For the graph attention neural network, the maximum pooling value is taken as the program graph representation or text graph representation; for the Bi-Lstm neural network, the hidden layer average value is taken as the program word representation or text word representation.
[0081] Step 5: Construct a loss function based on the program vector and text vector to adjust the model parameters in the code search model.
[0082] Based on the loss function, the program vectors and text vectors corresponding to the code and text description are embedded into the same high-dimensional space and the corresponding program vectors and text vectors are made as close as possible.
[0083] Based on each code and text in the training dataset, a triple <C, Q+, Q-> is constructed. C represents the code in the training dataset, Q+ represents the pre-set positive text description in the training dataset that matches the semantics of the code, and Q_ represents the negative text description randomly selected from the corpus excluding the positive text description in the training dataset. For each pair of triples, the loss function is minimized.
[0084]
[0085] Among them, θ represents the parameters of the code search model, c represents the program vector output by the code search model for the code, q+ and q- respectively represent the positive text vector and negative text vector output by the code search model for the positive text description and negative text description, t represents the paired code and text description pairs in the training dataset, ∈ represents the hyperparameter, called the margin, which controls the minimum separation between the positive text description and the negative text description.
[0086] Through steps one to five, a graph attention neural network is designed to capture the local structural information in the graph, and a Bi-Lstm is used to capture the global dependencies that the graph attention neural network cannot learn, so as to accurately learn the semantic mapping of the program and the query for code search.
[0087] In addition, this embodiment has greatly improved the existing code retrieval method and has been improved according to the structural characteristics of the code. This embodiment can provide more accurate query services for online code retrieval, improving the accuracy and user experience of programmer users when using the online code retrieval platform.
[0088] The above is only a preferred specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any person skilled in the art within the technical scope disclosed by the present invention, according to the technical solution and inventive concept of the present invention, making equivalent substitutions or changes, should be covered within the protection scope of the present invention.
Claims
1. A semantically enhanced code retrieval method based on program dependency, characterized by: Input the text description to be retrieved into the code search model to output the code retrieval results; The training process of the code search model is as follows: Step 1: Obtain the code and the text description corresponding to the code to construct a training dataset, construct an abstract syntax tree for the code and extract it to obtain a program graph, and construct an abstract syntax tree for the text description and extract it to obtain a text graph; Step 2: Calculate the cosine similarity between the i-th node vector in the text graph and all node vectors in the program graph, and use the obtained program similarity value as the weight of each node in the program graph. Based on the weight of each node in the program graph and each node vector in the program graph, calculate the weighted average embedding of all nodes in the program graph into the i-th node vector in the text graph, and obtain the text context global-level vector representation corresponding to the i-th node in the text graph. The i-th node vector in the text graph and the text context global-level vector representation are combined through the vector to obtain the text cross vector corresponding to the i-th node. Similarly, all text cross vectors are obtained, and each text cross vector replaces the corresponding node vector in the text graph to obtain the updated text graph. Step 3. Calculate the cosine similarity between the j-th node vector in the program graph and all node vectors in the text graph, and use the obtained text similarity value as the weight of each node in the text graph. Based on the weight of each node in the text graph and each node vector in the text graph, calculate the weighted average embedding of all nodes in the text graph into the j-th node vector in the program graph, and obtain the program context global-level vector representation corresponding to the j-th node in the program graph. The j-th node vector in the program graph and the program context global-level vector representation are combined through vectors to obtain the program cross vector corresponding to the j-th node. Similarly, all program cross vectors are obtained, and each program cross vector replaces the corresponding node vector in the program graph to obtain the updated program graph. Step 4: Feed the updated text graph and the updated program graph into two independently set graph attention neural networks respectively to obtain program graph representation and text graph representation; split the code and text description into sequences and input them into two independently set Bi-Lstm networks respectively to obtain program word representation and text word representation, concatenate the program word representation and program graph representation to obtain program vector, and concatenate the text word representation and text graph representation to obtain text vector; Step 5: Construct a loss function based on the program vector and text vector to adjust the model parameters in the code search model.
2. The semantically enhanced code retrieval method based on program dependency according to claim 1 is characterized in that: In step 4, the two independently configured graph attention neural networks operate on the updated text graph and the updated program graph in the same way. The graph attention neural network processes the updated program graph as follows: Based on the self-attention mechanism, the attention weights between the current node and other nodes in the updated program graph are calculated to determine the importance of different nodes to the current node; Based on the attention weight, the self-attention mechanism is used to aggregate the neighbor information of the current node and output the backward aggregation vector of the current node; By analogy, the backward aggregation vector of each node in the program graph is obtained, thereby obtaining the program graph representation.
3. The semantically enhanced code retrieval method based on program dependency according to claim 2 is characterized in that: The calculation formula of attention weight is as follows: Among them, e ab represents the attention weight between the a-th node and the b-th node, W h is the weight matrix for linear transformation, Represents the weight vector matrix learned by the a-th node, D ab represents the attention weight of the a-th node to the b-th node, LeakyReLU represents the nonlinear activation function, Represents all neighbor nodes of node a in the program graph, e ak represents the attention weight between the a-th node and the k-th node, represents the node vector of the a-th node in layer 0, Represents the node vector of the b-th node in layer 0.
4. The semantically enhanced code retrieval method based on program dependency according to claim 3 is characterized in that: The calculation formula of the backward aggregation vector of the current node is as follows: is the weight matrix of the linear transformation of node features in the lth layer, represents the attention weight between the a-th node and the b-th node in the l-th layer, represents the node vector of the a-th node in the l-th layer, Represents the node vector of the b-th node in the l-1 layer.
5. The semantically enhanced code retrieval method based on program dependency according to claim 1 is characterized in that: In step 5, the loss function The build is as follows: Where θ represents the parameters of the code search model, c represents the program vector output by the code search model, q+ and q- represent the positive text vector and negative text vector output by the code search model respectively, ∈ represents a hyperparameter, t represents the paired code and text description pairs in the training dataset, C represents the code in the training dataset, Q+ represents the positive text description set in the training dataset that matches the code semantics, and Q- represents the negative text description randomly selected from the corpus of the training dataset excluding the positive text description.
6. The semantically enhanced code retrieval method based on program dependency according to claim 1 is characterized in that: In step 1, an abstract syntax tree is constructed for the text description and a text graph is extracted. The text graph is represented as G = (V, E), where V is all nodes on the abstract syntax tree and E is an edge representing the relationship between nodes. The nodes are divided into two categories, one is the terminal nodes on the abstract syntax tree and the other is the non-terminal nodes on the abstract syntax tree. The terminal nodes on the abstract syntax tree correspond to identifiers in the code, while the non-terminal nodes on the abstract syntax tree represent different compilation units.
7. A semantically enhanced code retrieval system based on program dependencies, characterized by: Input the text description to be retrieved into the code search model to output the code retrieval results; The code search model includes a building module, an extraction module, a text interaction module, a program interaction module, a graph attention neural network, a Bi-Lstm network, a splicing module, and a loss building module; The training process of the code search model is as follows: The construction module constructs a training data set based on the acquired codes and the text descriptions corresponding to the codes; The extraction module is used to construct an abstract syntax tree for the code and extract it to obtain a program graph, and to construct an abstract syntax tree for the text description and extract it to obtain a text graph; The text interaction module is used to calculate the cosine similarity between the i-th node vector in the text graph and all node vectors in the program graph. The obtained program similarity value is used as the weight of each node in the program graph. Based on the weight of each node in the program graph and each node vector in the program graph, the weighted average embedding of all nodes in the program graph into the i-th node vector in the text graph is calculated to obtain the text context global-level vector representation corresponding to the i-th node in the text graph. The i-th node vector in the text graph and the text context global-level vector representation are combined through vectors to obtain the text cross vector corresponding to the i-th node. This is repeated to obtain all text cross vectors. Each text cross vector is used to replace the corresponding node vector in the program graph to obtain an updated text graph. The program interaction module is used to calculate the cosine similarity between the j-th node vector in the program graph and all the node vectors in the text graph, and use the obtained text similarity value as the weight of each node in the text graph. Based on the weight of each node in the text graph and each node vector in the text graph, the weighted average embedding of all nodes in the text graph into the j-th node vector in the program graph is calculated to obtain the program context global-level vector representation corresponding to the j-th node in the program graph. The j-th node vector in the program graph and the program context global-level vector representation are combined through vectors to obtain the program cross vector corresponding to the j-th node. This is repeated to obtain all program cross vectors. Each program cross vector replaces the corresponding node vector in the program graph to obtain an updated program graph. Two independently set graph attention neural networks are used to process the updated text graph and the updated program graph respectively to obtain program graph representation and text graph representation; Two independently set Bi-Lstm networks are used to process the segmented code and text description respectively to obtain program word representation and text word representation; The splicing module is used to splice the program word representation and the program graph representation to obtain the program vector, and to splice the text word representation and the text graph representation to obtain the text vector. The loss building module builds a loss function based on the program vector and text vector to adjust the model parameters in the code search model.
8. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a plurality of classification programs, which are used to be called by a processor and execute the semantically enhanced code retrieval method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Method for automatically generating Java code annotations based on Graph2Seq of deep reinforcement learning
CN110018820A
Code searching method based on program dependency graph
CN116661852A