Semantic enhancement code retrieval method and system based on program dependence and storage medium

By constructing abstract syntax trees and program diagrams, combined with graph attention neural networks and Bi-Lstm networks, the semantic mapping difficulties of code and natural language queries are solved, and the accuracy and user experience of code retrieval are improved.

CN119441509AActive Publication Date: 2025-02-14ARTIFICIAL INTELLIGENCE RES INST OF HEFEI COMPREHENSIVE NAT SCI CENT (ANHUI ARTIFICIAL INTELLIGENCE LAB)
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202411454232.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-17
Publication Date
2025-02-14
Estimated Expiration
2044-10-17

Smart Images

  • Figure CN119441509A_ABST
    Figure CN119441509A_ABST
Patent Text Reader

Abstract

The invention discloses a semantic enhancement code retrieval method and system based on program dependency and a storage medium, and relates to the technical field of artificial intelligence, and the training process of a code search model is as follows: obtaining a code and a text description corresponding to the code, and constructing a program graph and a text graph; interacting the program diagram and the text diagram to obtain an updated text diagram and an updated program diagram; respectively sending the updated text graph and the updated program graph into two independently arranged graph attention neural networks to obtain a program graph representation and a text graph representation; respectively inputting the code and the text description into two independently arranged Bi-Lstm networks to obtain a program word representation and a text word representation, splicing the program word representation and the program graph representation to obtain a program vector, and splicing the text word representation and the text graph representation to obtain a text vector; according to the semantic enhancement code retrieval method and system and the storage medium, the accuracy of an online code retrieval platform and the user experience are improved.
Need to check novelty before this filing date? Find Prior Art

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 development of the software industry in the past few years, the number of public and private repositories (e.g., on GitHub or Bitbucket) has reached an unprecedented level. It has been widely recognized that the software industry is entering the era of "big code". Code search aims to find key codes based on querying large code corpora from natural language (e.g., Github, StackOverflow), which has become a key issue in the era of "big code". In addition, some studies have also shown that more than 90% of the efforts of software developers are to reuse existing code. Therefore, an accurate code search system can greatly improve the productivity and quality of software while reducing the cost of software development.

[0003] Some early code retrieval approaches used information retrieval (IR) techniques to capture the relationship between code and keyword matching queries. However, information retrieval techniques are limited when code and natural language do not have common keywords, and the performance is poor. 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 only on sequential models, i.e., long short-term memory (LSTM), self-attention models, etc., 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] There are also some approaches that use abstract syntax trees (AST) and control flow graphs (CFG) 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 the code and query 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 GNN for code search, the limitations of GNN 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 dependency, which accurately learns the semantic mapping of the program and the query of code search, and improves the accuracy and user experience of the online code retrieval platform.

[0006] The program dependency-based semantic enhancement code retrieval method proposed in the present invention inputs the text description to be retrieved into the code search model to output the code retrieval result;

[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 data set, 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 the 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 vectors to obtain the text cross vector corresponding to the i-th node. By analogy, all text cross vectors are obtained, and each text cross vector replaces the corresponding node vector in the text graph to obtain an 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, use the obtained text similarity value as the weight of each node in the text graph, and calculate the weighted average embedding of all nodes in the text graph into the jth node vector in the program graph based on the weight of each node in the text graph and each node vector in the text 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 vectors to obtain the program cross vector corresponding to the ,th node, and so on to obtain all program cross vectors, and replace each program cross vector with the corresponding node vector in the program graph to obtain an updated program graph;

[0011] Step 4: Send 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 a program vector, and concatenate the text word representation and text graph representation to obtain a text vector;

[0012] Step 5: Construct a loss function based on the program vector and the text vector to adjust the model parameters in the code search model.

[0013] Furthermore, in step 4, the two independently set graph attention neural networks operate in the same principle on the updated text graph and the updated program graph. The processing process of the graph attention neural network on the updated program graph is 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, Represents the node vector of 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-1th layer.

[0024] Furthermore, in step 5, the loss function The build is as follows:

[0025]

[0026] Among them, θ 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 the hyperparameter, t represents the pairs of code and text description 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 by 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 terminal nodes on the abstract syntax tree, and the other is non-terminal nodes on the abstract syntax tree. The terminal nodes on the abstract syntax tree correspond to identifiers in the code, and the non-terminal nodes on the abstract syntax tree represent different compilation units.

[0028] A semantically enhanced code retrieval system based on program dependency inputs the text description to be retrieved into the code search model to output the 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 the program graph, and to construct an abstract syntax tree for the text description and extract the 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 the 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, 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. By analogy, all text cross vectors are obtained, and each text cross vector is replaced with 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 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 ,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, and each program cross vector is used to replace 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 the program graph representation and the 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 concatenation module is used to concatenate the program word representation and the program graph representation to obtain the program vector, and to concatenate 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 the 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 It is a schematic diagram of the code syntax structure edge;

[0043] Figure 3 This is a schematic diagram of the code data flow;

[0044] Figure 4 Diagram of the text query syntax structure for a given text query "How to check for null". DETAILED DESCRIPTION

[0045] Below, the technical solution of the present invention is described in detail through specific embodiments. Many 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 different from those described herein, and those skilled in the art can make similar improvements without violating the connotation of the present invention. Therefore, the present invention is not limited to the specific implementation disclosed below.

[0046] like Figures 1 to 4 As shown, the semantically enhanced 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 data set, 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 a cleaned standard function code, and the text description is a natural language description of the corresponding function code. The set of cleaned code and text description pairs F is obtained. i ,q i )|ci ∈C,q i ∈Q}, i∈{1, 2, ..., n}, where F represents the set of codes and text descriptions, c i Represents 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. Because codes and text descriptions are set in pairs, the total number between the two is consistent; a training data set is constructed based on set F.

[0050] The construction principles of program graphs and text graphs are the same. The text graph construction is taken as an example to illustrate. When constructing the program graph, the corresponding replacements can be made 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 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 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 connect all AST nodes based on the abstract syntax tree; 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 indicate the last read and write of each occurrence of a variable; data flow edges indicate the flow of data between variables. Given a text description q, extract its dependency parsing graph 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 component parse trees and dependency parse trees, and 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 step 2 and step 3 mainly include the innovation of graph embedding and cross-domain vector representation learning. Specifically, a method is described to match the nodes of the text graph and the program graph through cosine similarity, and use this matching to update the vector representation of the nodes in the two graphs. The advantage of this operation is that by calculating the similarity between the nodes of the text graph and the program graph, the knowledge of two different fields (text and program code) can be integrated, so that the nodes in each field can obtain richer semantic information. Through weighted average embedding and vector union, each node can not only capture the characteristics of its own field, 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 node classification, clustering or recommendation tasks. By replacing the original node vector with the updated cross vector, the structure of the graph can be dynamically adjusted to make it closer to the semantic relationship 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 calculate the weighted average embedding of all nodes in the program graph into the i-th node vector in the text graph based on the weight of each node in the program graph and each node vector in the program 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 vectors to obtain the text cross vector corresponding to the i-th node, and so on, to obtain all the text cross vectors, and replace each text cross vector with the corresponding node vector in the text graph to obtain an 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 join operation to form text intersection 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, use the obtained text similarity value as the weight of each node in the text graph, and calculate the weighted average embedding of all nodes in the text graph into the jth node vector in the program graph based on the weight of each node in the text graph and each node vector in the text 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 vectors to obtain the program cross vector corresponding to the ,th node, and so on to obtain all program cross vectors, and replace each program cross vector with the corresponding node vector in the program graph to obtain an 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: Send 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 a program vector, and concatenate the text word representation and text graph representation to obtain a 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 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 node embedding vector dimension length. For node r, a function is applied to take a set of incoming neighboring node vectors as input and output a backward aggregation vector; the following process of the updated program graph with the graph attention neural network is 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-1th 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 input into two independently set Bi-Lstm networks, mainly to further capture the global interactions in the graph that are missed in the graph attention neural network. Note that in step 1, the constructed program graph consists of terminal / non-terminal nodes, and the terminal nodes are sequences / subsequences of the original code. Therefore, Bi-Lstm is directly used on the sequence to capture the global dependencies between these terminal nodes, while ignoring the non-terminal nodes for effective learning. Finally, the hidden layer average is used to obtain the final overall vector expression.

[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 the 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, where C represents the code in the training dataset, Q+ represents the positive text description set in the training dataset and semantically matching 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 after passing through the code search model, q+ and q- respectively represent the positive text vector and negative text vector output by the positive text description and negative text description after passing through the code search model, t represents the paired code and text description pair 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 structure 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 of code search.

[0087] In addition, this embodiment has greatly improved the existing code retrieval method and made improvements in view of the structural characteristics of the code. This embodiment 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.

[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, makes equivalent substitutions or changes, and should be covered by the protection scope of the present invention.

Claims

1. A semantically enhanced code retrieval method based on program dependency, characterized in that: Input the text description to be retrieved into the code search model to output the code retrieval result; 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 data set, 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 the 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 vectors to obtain the text cross vector corresponding to the i-th node. By analogy, all text cross vectors are obtained, and each text cross vector replaces the corresponding node vector in the text graph to obtain an updated text graph. Step 3: Calculate the cosine similarity between the j-th node vector in the program graph and all the node vectors in the text graph, use the obtained text similarity value as the weight of each node in the text graph, and calculate the weighted average embedding of all nodes in the text graph into the j-th node vector in the program graph based on the weight of each node in the text graph and each node vector in the text 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, and so on, to obtain all the program cross vectors, and replace each program cross vector with the corresponding node vector in the program graph to obtain an updated program graph; Step 4: Send 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 a program vector, and concatenate the text word representation and text graph representation to obtain a text vector; Step 5: Construct a loss function based on the program vector and the text vector to adjust the model parameters in the code search model.

2. The method for semantically enhanced code retrieval based on program dependency according to claim 1, characterized in that: In step 4, the two independently set graph attention neural networks operate in the same way on the updated text graph and the updated program graph. The processing process of the graph attention neural network on the updated program graph is 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 method for semantically enhanced code retrieval based on program dependency according to claim 2, 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 method for semantically enhanced code retrieval 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-1th layer.

5. The method for semantically enhanced code retrieval based on program dependency according to claim 1, characterized in that: In step 5, the loss function The build is as follows: Among them, θ 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 the hyperparameter, t represents the pairs of code and text description 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 method for semantically enhanced code retrieval based on program dependency according to claim 1, characterized in that: In step one, an abstract syntax tree is constructed for the text description and a text graph is extracted. The text graph is represented by 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, and 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 result; 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 the program graph, and to construct an abstract syntax tree for the text description and extract the 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 the 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, 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. By analogy, all text cross vectors are obtained, and each text cross vector is replaced with 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. By analogy, all program cross vectors are obtained, and 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 the program graph representation and the 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 concatenation module is used to concatenate the program word representation and the program graph representation to obtain the program vector, and to concatenate 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 the 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, and the plurality of classification programs are used to be called by a processor and execute the semantically enhanced code retrieval method as described in any one of claims 1 to 6 above.

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

  • Semantic code retrieval using graph matching

    US20220107799A1