Name recommendation method based on structural mode and text mode fusion guidance

The method name recommendation method integrates the structural model and the text model, uses the text model implemented by AST and methods, and combines the co-attention and cross-attention mechanisms to solve the problem of poor method name recommendation in the existing technology and achieve a better Accurate and high-quality method name recommendations.

CN118585705APending Publication Date: 2024-09-03CHONGQING UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410727292.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-06
Publication Date
2024-09-03

AI Technical Summary

Technical Problem

Existing method name recommendation methods based on naming patterns are not effective in large open source software libraries and cannot accurately recommend method names with similar functions, especially the token similarity problem caused by different variable naming habits.

Method used

The method name recommendation method uses the fusion of structural mode and text mode, uses the abstract syntax tree (AST) of the code to retrieve methods with similar functions, and combines the text mode implemented by the method to integrate the two modes through the co-attention mechanism and the cross-attention mechanism. Director information to generate high-quality method names.

Benefits of technology

It significantly improves the accuracy and quality of method name recommendation and can outperform the baseline model on the Java data set, proving the effectiveness of the AST pattern guide in method name recommendation and the importance of the joint attention mechanism in information fusion.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118585705A
    Figure CN118585705A
Patent Text Reader

Abstract

The invention relates to a structural mode and text mode fusion guidance-based method name recommendation method, which comprises the following steps of: respectively retrieving the most similar methods by using an implementation token of a code and an AST, respectively extracting corresponding method names to form two mode directors, and simultaneously finishing method name recommendation in combination with method context information. In order to better fuse the information of the two mode directors, the method introduces a co-attention mechanism to fuse the mode information. Besides, in order to enable the mode director to better guide the context information of the method, the method uses a cross attention mechanism to identify the semantic correlation between the mode director and the context information, and the guidance of the mode director on the context information is improved. Two mode directors are utilized, a better information fusion mechanism is introduced, and the quality of method name recommendation is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of software method name retrieval, and in particular to a method name recommendation method guided by the fusion of a structure pattern and a text pattern. Background Art

[0002] The existing patent application (CN202211547271.X A method for recommending method names based on naming patterns) uses the names of the most similar methods as pattern guides and provides method naming patterns to recommend high-quality and meaningful method names. However, we have observed that the names of the most similar methods retrieved based on the method implementation context do not always provide the correct naming pattern. In large open source software libraries, many methods use the same naming on variables, which makes them have high token similarity, but they differ greatly in method functions. On the other hand, due to different variable naming habits among developers, two methods with similar functions may only have a low match in tokens. This makes a method for recommending method names based on naming patterns not always achieve good results.

[0003] To address this problem, we found that the code's abstract syntax tree (AST), as an abstract representation of the method's syntax structure, contains complete source program information and can highlight the structural information in the code. Therefore, we wonder whether we can use AST to retrieve methods with similar functions instead of just relying on the implementation of the method. Summary of the invention

[0004] In view of the above problems existing in the prior art, the technical problem to be solved by the present invention is: how to recommend method names more accurately.

[0005] In order to solve the above technical problems, the present invention adopts the following technical solution: a method for recommending method names based on the fusion guidance of structure mode and text mode, comprising the following steps:

[0006] S100: Select a public data set, which includes several project methods, randomly select multiple data from the public data set as a training set P, extract words from the data in the public data set to obtain a vocabulary, and use each project method in the training set as a training sample.

[0007] S200: Construct a method name recommendation model Mul-NamPat, wherein Mul-NamPat includes a naming pattern extraction module and a method name generation module.

[0008] The naming pattern extraction module includes a search engine retrieval module and a context extraction module.

[0009] S300: Select a p-th training sample from P, wherein the p-th training sample includes a number of real word tokens.

[0010] The pth training sample is used as the input of the naming pattern extraction module. The search engine retrieval module outputs two naming pattern guides of the pth training sample, which are a text pattern guide and a structural pattern guide respectively. The context extraction module outputs the context sequence of the pth training sample.

[0011] S400: The text naming pattern guide, the structural naming pattern guide and the context sequence obtained in S300 are used as inputs of a method name generation module, and the recommended method name of the pth training sample is obtained as output.

[0012] S500: Set the loss threshold and calculate the loss of the p-th training sample. When the loss of the p-th training sample is less than the loss threshold, obtain the trained Mul-NamPat model and execute the next step; otherwise, update the parameters of Mul-NamPa in reverse, set p=p+1, and return to S300.

[0013] S600: Input the project method to be recommended into the trained Mul-NamPa to obtain the recommended method name for the project method.

[0014] Furthermore, the method name generation module in S200 includes encoder one, encoder two, encoder three and a decoder, the encoder one, encoder two and encoder three are all single-layer bidirectional recurrent neural networks, and the decoder is a single-layer unidirectional recurrent neural network.

[0015] Furthermore, the specific steps of obtaining the name of the recommended method for the pth training sample in S400 are as follows:

[0016] S410: Input the text naming pattern guide of the p-th training sample into the encoder 1 to obtain the text pattern guide embedding vector x′, input the structure naming pattern guide of the p-th training sample into the encoder 2 to obtain the structure naming guide embedding vector x″, and input the context sequence of the p-th training sample into the encoder 3 to obtain the context embedding vector x, and the expressions are respectively expressed as follows:

[0017] x″=(x″1,x″2,...,x″ o ); (1)

[0018] x′=(x′1,x′2,...,x′ n ); (2)

[0019] x=(x1,x2,...,x m ); (3)

[0020] Among them, x′ nrepresents the embedding vector of the nth word token in x′, x″ o represents the embedding vector of the oth word token in x″, x m represents the embedding vector of the mth word token in x, n represents the length of x′, o represents the length of x″, and m represents the length of x;

[0021] S420: Use the Bi-LSTM unit to process x′, x″ and x, and calculate the hidden states h′, h″ and h corresponding to x′, x″ and x, respectively. The specific expressions are as follows:

[0022]

[0023] in, Represents a connection operation. represents the hidden state of the current Bi-LSTM unit propagating to the right, represents the hidden state of the previous unit from left to right, represents the hidden state of the current Bi-LSTM unit propagating to the left, represents the hidden state of the previous unit from right to left, h l represents the hidden state of the current unit; x a It is an intermediate variable and has no practical significance;

[0024] Formula (4), Formula (5) and Formula (6) are used to calculate the context sequence hidden state h corresponding to x, the text mode guide hidden state h′ corresponding to x′ and the structure mode guide hidden state h″ corresponding to x″ respectively. The specific expressions are as follows:

[0025] When x a ∈{x1,x2,…,x m}, we get h=(h1,h2...h m ), i=1, 2, ..., m;

[0026] When x a ∈{x′1,x′2,…,x′ n}, we get h′=(h′1,h′2…h′ n ), j = 1, 2, ..., n;

[0027] When x a ∈{x″1,x″2,...,x″ o}, we get h″=(h″1,h″2...h″ o ), k = 1, 2, ..., o;

[0028] S430: Mode fusion, using the co-attention mechanism to fuse h′ and h″, and calculate the text mode guide vector V′ and the structure mode guide vector V″, which is specifically expressed as follows:

[0029] g′ i =h″β i ; (6)

[0030] β i =softmax(h″ T W β h′ i ); (7)

[0031]

[0032] Where W′ hg represents the trainable parameters, β i is h′ i The attention weights for the structural pattern guides are measured, and the weights for each structural pattern guide token pair h′ are measured. i The importance of g′ is represented by the perceptual feature vector corresponding to h′. i Information related to the text mode director is highlighted from the structure mode director and retained. β ∈R 2d×2d is a trainable parameter.

[0033] g″ i =h″β′ i ; (9)

[0034]

[0035]

[0036] Where W″ hg represents the trainable parameters, β′ i It is h″ j The attention weights for the text pattern guides and the weights of the pattern guide tokens on h″ j The importance of g″ represents the perceptual feature vector corresponding to h″, g″ i The information related to the structural pattern guide is highlighted and preserved from the method implementation pattern guide.

[0037] S440: Use V′ and V″ to calculate cross attention for context respectively

[0038] S=h V′ T ; (12)

[0039] S row =softmax row (S); (13)

[0040] S column =softmax column(S); (14)

[0041] A row =S row V′; (15)

[0042] A column =S row S column T h; (16)

[0043] A row =S row V′; (17)

[0044] E code =AVG(h,A row ,h⊙A row ,h⊙A column ); (18)

[0045] Where S represents the association matrix between context information and method implementation guide. row and S column Respectively represent the correlation between method context and method implementation guide and structural pattern guide. row With A column represents the word-based cross-attention weights from method context to method implementation pattern guide and from method implementation pattern guide to method context. code Representation method context vector representation.

[0046] Similarly, the calculation method of the structural pattern guide for the association guidance of the method context information is the same as that of the structural pattern guide. Using the structural pattern guide vector through formulas (12)-(18), we can get the new method context vector representation E ast Finally, by fusing the two new representations, we get the final method context vector representation E:

[0047] E=γE code +(1-γ)E ast ; (19)

[0048] Among them, γ is a configurable hyperparameter.

[0049] S450: Set the total time step T, select the tth time step from T, and calculate the hidden state s of the decoder at time step t t , the specific expression is as follows:

[0050] s t =LSTM(s t-1 ,y t-1 ); (20)

[0051] Among them, st-1 represents the hidden state of the decoder at the previous time step, y t-1 represents the embedding of a word token;

[0052] S460: Calculate the weight of the hidden state of the context sequence and the weight of the hidden state of the two mode guides respectively, and the calculation expression is as follows:

[0053]

[0054] α t =softmax(e t ); (twenty two)

[0055] in, represents the attention feature obtained by the i-th hidden state, α t represents the attention score of all hidden states, e t represents the attention features obtained from all hidden states, i.e., v, W h and W s Both represent learnable parameters, v T represents the transpose of v;

[0056] When e t When representing the attention features of all hidden states of the context sequence, that is, And the attention weight of the hidden state of the context sequence is denoted as α t =α t ,in,

[0057] When e t When representing the attention features of all hidden states of the text mode guide sequence, that is, And the attention weight of the hidden state of the text mode guide sequence is denoted as α t =α′ t ,in,

[0058] When e t When representing the attention features of all hidden states of the structure pattern guide sequence, that is, And the attention weight of the hidden state of the structure pattern guide sequence is denoted as α t =α″ t ,in,

[0059] S470: Calculate mode guide-context sequence vector The specific expression is as follows:

[0060]

[0061] in, Represents the context sequence vector, and the calculation expression is as follows:

[0062]

[0063] in, represents the weight of the i-th word token at the t-th step, i = 1, 2, ..., m, E i Representation method: Representation of the i-th word in the context vector;

[0064] Represents the text mode driver vector, and the calculation expression is as follows:

[0065]

[0066] in, V represents the weight of the jth word token at the tth step, j = 1, 2, ..., n, V j ′ represents the representation of the i-th word of the text pattern guide vector;

[0067] Represents the structural mode director vector, and the calculation expression is as follows:

[0068]

[0069] in, represents the weight of the k-th word token at the t-th step, j = 1, 2, ..., k, V″ k represents the representation of the i-th word of the structural pattern guide vector;

[0070] S480: Calculate the probability p of generating a word token from the vocabulary gen , and its calculation formula is as follows:

[0071]

[0072] Among them, p gen ∈[0, 1], W s , W z and b ptr represents the learnable parameters, σ represents the sigmoid function, s t represents the hidden state of the decoder at time step t, z t represents the input of the decoder at the current time t, represents a pattern guide-context sequence vector;

[0073] S490: Calculate the output probability P(y) of each real word token y in the p-th training sample at time step t, and the calculation expression is as follows:

[0074]

[0075] Among them, P vocab (y) represents the probability of generating each word token from the vocabulary;

[0076] S495: Select the word token corresponding to the maximum probability at time step t;

[0077] S499: Traverse all time steps in the time step T to obtain the word token corresponding to the maximum probability in each time step, that is, obtain T word tokens, and the T word tokens constitute the name of the recommendation method.

[0078] Furthermore, the specific steps of calculating the loss of the pth training sample in S500 are as follows:

[0079] Formula (29) is used to calculate the loss of the pth training sample:

[0080]

[0081] in, represents the probability of generating the target vocabulary, represents the output probability corresponding to the real word token of the p-th training sample.

[0082] Furthermore, the method name generation module in S200 may be a seq2seq attention model.

[0083] Furthermore, the public data set in S100 can also be used as a retrieval corpus.

[0084] Compared with the prior art, the present invention has at least the following advantages:

[0085] The present invention proposes a method name recommendation method Mul-NamPat guided by the fusion of structural patterns and text patterns. The method uses the implementation tokens and AST of the code to retrieve the most similar methods respectively, and extracts the corresponding method names respectively, forming two pattern directors, and at the same time combines the method context information to complete the method name recommendation. In order to better integrate the information of the two pattern directors and guide the method implementation text information to learn the method voice, Mul-NamPat introduces the common attention mechanism and the cross attention mechanism. Improve the quality of method name recommendation.

[0086] This paper conducts a large-scale empirical analysis on 15M Java methods, and the results show that the use of pattern directors obtained based on AST retrieval can significantly improve the performance of method name recommendation tasks with method-implemented pattern directors. Extensive experiments were conducted on a widely used Java dataset, including ① performance comparison between Mul-NamPat and baseline models; ② analysis of the effectiveness of AST pattern directors on models; ③ the impact of common attention mechanisms and cross-attention mechanisms on the semantics of pattern director fusion and guidance method implementation context learning methods; ④ the impact of cross-attention mechanism fusion weights on models. Experimental results show that Mul-NamPat outperforms all baseline models. BRIEF DESCRIPTION OF THE DRAWINGS

[0087] Figure 1 Here are the source codes for both methods.

[0088] Figure 2 This is an example of an AST sequence.

[0089] Figure 3 It is the overall framework diagram of the method of the present invention. DETAILED DESCRIPTION

[0090] The present invention is described in further detail below.

[0091] A method name recommendation method based on the fusion guidance of structure mode and text mode includes the following steps:

[0092] S100: Select a public data set, which includes several project methods, randomly select multiple data from the public data set as a training set P, extract words from the data in the public data set to obtain a vocabulary, and use each project method in the training set as a training sample.

[0093] S200: Construct a method name recommendation model Mul-NamPat, wherein Mul-NamPat includes a naming pattern extraction module and a method name generation module.

[0094] The naming pattern extraction module includes a search engine retrieval module and a context extraction module, and the search engine retrieval module adopts Lucene.

[0095] S300: Select a p-th training sample from P, wherein the p-th training sample includes a number of real word tokens.

[0096] The pth training sample is used as the input of the naming pattern extraction module. The search engine retrieval module outputs two naming pattern guides of the pth training sample, which are a text pattern guide and a structural pattern guide respectively. The context extraction module outputs the context sequence of the pth training sample.

[0097] S400: The text naming pattern guide, the structural naming pattern guide and the context sequence obtained in S300 are used as inputs of a method name generation module, and the recommended method name of the pth training sample is obtained as output.

[0098] S500: Set the loss threshold and calculate the loss of the p-th training sample. When the loss of the p-th training sample is less than the loss threshold, obtain the trained Mul-NamPat model and execute the next step; otherwise, update the parameters of Mul-NamPa in reverse, set p=p+1, and return to S300.

[0099] S600: Input the project method to be recommended into the trained Mul-NamPa to obtain the recommended method name for the project method.

[0100] Specifically, the method name generation module in S200 includes encoder one, encoder two, encoder three and a decoder, the encoder one, encoder two and encoder three are all single-layer bidirectional recurrent neural networks, and the decoder is a single-layer unidirectional recurrent neural network.

[0101] Specifically, the specific steps of obtaining the name of the recommended method for the pth training sample in S400 are as follows:

[0102] S410: Input the text naming pattern guide of the p-th training sample into the encoder 1 to obtain the text pattern guide embedding vector x′, input the structure naming pattern guide of the p-th training sample into the encoder 2 to obtain the structure naming guide embedding vector x″, and input the context sequence of the p-th training sample into the encoder 3 to obtain the context embedding vector x, and the expressions are respectively expressed as follows:

[0103] x″=(x″1,x″2,…,x″ o ); (1)

[0104] x′=(x′1,x′2,…,x′ n ); (2)

[0105] x=(x1,x2,…,x m ); (3)

[0106] Among them, x′ n represents the embedding vector of the nth word token in x′, x″ o represents the embedding vector of the oth word token in x″, x m represents the embedding vector of the mth word token in x, n represents the length of x′, o represents the length of x″, and m represents the length of x;

[0107] S420: Use the Bi-LSTM unit to process x′, x″ and x, and calculate the hidden states h′, h″ and h corresponding to x′, x″ and x, respectively. The specific expressions are as follows:

[0108]

[0109] in, Represents a connection operation. represents the hidden state of the current Bi-LSTM unit propagating to the right, represents the hidden state of the previous unit from left to right, represents the hidden state of the current Bi-LSTM unit propagating to the left, represents the hidden state of the previous unit from right to left, h l represents the hidden state of the current unit; x a It is an intermediate variable and has no practical significance;

[0110] Formula (4), Formula (5) and Formula (6) are used to calculate the context sequence hidden state h corresponding to x, the text mode guide hidden state h′ corresponding to x′ and the structure mode guide hidden state h″ corresponding to x″ respectively. The specific expressions are as follows:

[0111] When x a ∈{x1,x2,…,x m}, we get h=(h1,h2...h m ), i=1, 2, ..., m;

[0112] When x a ∈{x′1,x′2,...,x′ n}, we get h′=(h′1,h′2...h′ n ), j = 1, 2, ..., n;

[0113] When x a ∈{x″1,x″2,...,x″ o}, we get h″=(h″1,h″2...h″ o ), k = 1, 2, ..., o;

[0114] S430: Mode fusion, using the co-attention mechanism to fuse h′ and h″, and calculate the text mode guide vector V′ and the structure mode guide vector V″, which is specifically expressed as follows:

[0115] And output a perceptual feature vector for h′ and h″ respectively, then connect h′ and its corresponding perceptual vector and output them to the subsequent layer to obtain the text mode guide vector V′, and connect h″ and its corresponding perceptual vector and output them to the subsequent layer to obtain the structural mode guide vector V″.

[0116] g′ i =h″β i ; (6)

[0117] β i =softmax(h″ T W β h′ i ); (7)

[0118]

[0119] Where W′ hg represents the trainable parameters, β i is h′ i The attention weights for the structural pattern guides are measured, and the weights for each structural pattern guide token pair h′ are measured. i The importance of g′ is represented by the perceptual feature vector corresponding to h′. i Information related to the text mode director is highlighted from the structure mode director and retained. β ∈R 2d×2d is a trainable parameter.

[0120] g″ i =h″β′ i ; (9)

[0121]

[0122] Where W″ hg represents the trainable parameters, β′ i It is h″ j The attention weights for the text pattern guides and the weights of the pattern guide tokens on h″ j The importance of g″ represents the perceptual feature vector corresponding to h″, g″ i The information related to the structural pattern guide is highlighted and preserved from the method implementation pattern guide.

[0123] S440: Use the cross attention layer to help the method context information capture the pattern information in the pattern guide. Because there are two different pattern guides V′ and V″, the two pattern guides need to guide the method context separately. The method implementation pattern guide performs cross attention calculation on the method context:

[0124] Use V′ and V″ to calculate the cross attention of the context respectively

[0125] S=hV′ T ; (12)

[0126] S row =softmaxrow (S); (13)

[0127] S column =softmax column (S); (14)

[0128] A row =S row V′; (15)

[0129] A column =S row S column T h; (16)

[0130] A row =S row V′; (17)

[0131] E code =AVG(h,A row ,h⊙A row ,h⊙A column ); (18)

[0132] Where S represents the association matrix between context information and method implementation guide. row and S column Respectively represent the correlation between method context and method implementation guide and structural pattern guide. row With A column represents the word-based cross-attention weights from method context to method implementation pattern guide and from method implementation pattern guide to method context. code Representation Method The context vector representation is built on top of these cross-attention weights and is computed by averaging the sum of the four terms.

[0133] Similarly, the calculation method of the structural pattern guide for the association guidance of the method context information is the same as that of the structural pattern guide. Using the structural pattern guide vector through formulas (12)-(18), we can get the new method context vector representation E ast Finally, by fusing the two new representations, we get the final method context vector representation E:

[0134] E=γE code +(1-γ)E ast ; (19)

[0135] Among them, γ is a configurable hyperparameter, which represents the weight assigned to different cross-attention representation results during the fusion process.

[0136] S450: Set the total time step T, select the tth time step from T, and calculate the hidden state s of the decoder at time step t t , the specific expression is as follows:

[0137] s t =LSTM(s t-1 ,y t-1 ); (20)

[0138] Among them, s t-1 represents the hidden state of the decoder at the previous time step, y t-1 represents the embedding of a word token;

[0139] S460: Calculate the weight of the hidden state of the context sequence and the weight of the hidden state of the two mode guides respectively, and the calculation expression is as follows:

[0140]

[0141] α t =softmax(e t ); (twenty two)

[0142] in, represents the attention feature obtained by the i-th hidden state, α t represents the attention score of all hidden states, e t represents the attention features obtained from all hidden states, i.e., v, W h and W s Both represent learnable parameters, v T represents the transpose of v;

[0143] When e t When representing the attention features of all hidden states of the context sequence, that is, And the attention weight of the hidden state of the context sequence is denoted as α t =α t ,in,

[0144] When e t When representing the attention features of all hidden states of the text mode guide sequence, that is, And the attention weight of the hidden state of the text mode guide sequence is denoted as α t =α′ t ,in,

[0145] When e t When representing the attention features of all hidden states of the structure pattern guide sequence, that is, And the attention weight of the hidden state of the structure pattern guide sequence is denoted as αt =α″ t ,in,

[0146] S470: Calculate mode guide-context sequence vector The specific expression is as follows:

[0147]

[0148] in, Represents the context sequence vector, and the calculation expression is as follows:

[0149]

[0150] in, represents the weight of the i-th word token at the t-th step, i = 1, 2, ..., m, E i Representation method: Representation of the i-th word in the context vector;

[0151] Represents the text mode driver vector, and the calculation expression is as follows:

[0152]

[0153] in, V represents the weight of the jth word token at the tth step, j = 1, 2, ..., n, V j ′ represents the representation of the i-th word of the text pattern guide vector;

[0154] Represents the structural mode director vector, and the calculation expression is as follows:

[0155]

[0156] in, represents the weight of the k-th word token at the t-th step, j = 1, 2, ..., k, V″ k represents the representation of the i-th word of the structural pattern guide vector;

[0157] S480: Calculate the probability p of generating a word token from the vocabulary gen , and its calculation formula is as follows:

[0158]

[0159] Among them, p gen ∈[0, 1], W s , W z and b ptr represents the learnable parameters, σ represents the sigmoid function, st represents the hidden state of the decoder at time step t, z t represents the input of the decoder at the current time t, represents a pattern guide-context sequence vector;

[0160] S490: Calculate the output probability P(y) of each real word token y in the p-th training sample at time step t, and the calculation expression is as follows:

[0161]

[0162] Among them, P vocab (y) represents the probability of generating each word token from the vocabulary;

[0163] S495: Select the word token corresponding to the maximum probability at time step t;

[0164] S499: Traverse all time steps in the time step T to obtain the word token corresponding to the maximum probability in each time step, that is, obtain T word tokens, and the T word tokens constitute the name of the recommendation method.

[0165] Specifically, the specific steps of calculating the loss of the p-th training sample in S500 are as follows:

[0166] Formula (29) is used to calculate the loss of the pth training sample:

[0167]

[0168] in, represents the probability of generating the target vocabulary, represents the output probability corresponding to the real word token of the p-th training sample.

[0169] Specifically, the method name generation module in S200 may be a seq2seq attention model. The public dataset in S100 may also be used as a retrieval corpus.

[0170] 1. Feasibility Analysis of the Method of the Invention

[0171] Show an example of low similarity in code implementation tokens but extremely high similarity in code AST. As shown in Figure 1, the source code of two methods is shown. Both methods implement the function of finding the sum of two numbers from a given array that meets the target sum. The method names of the two methods are inconsistent, namely "getTwoNums" and "getTwoNumbers", but they have the same naming pattern "getTwo". We expect that these two methods can be the most similar methods to each other in the retrieval module to obtain the shared naming pattern. However, through observation, we found that the two methods are different in the naming of variables, which leads to a low similarity in the code implementation of the two methods. If the code implementation is used as a search, the two methods will not be retrieved as the most similar methods by each other. By observing the AST of the two methods, we found that the two methods have a surprising similarity in AST structure. This shows that the method name of the most similar code can be obtained as a pattern guide by using the code AST as a search to help method name generation.

[0172] In view of the above observations, we believe that we can use the implementation tokens of the code and the AST to retrieve the most similar methods respectively, and extract the corresponding method names respectively, forming two pattern guides combined with method context information to complete high-quality method name recommendations.

[0173] 2. Generality analysis of the method of the present invention

[0174] To complete the empirical study, we used a widely used Java dataset constructed by Nguyen et al., processed 15,294,489 Java methods, and ensured that the method names were in a stable state. Using the existing processing method, the context of each method is collected, including the types of parameters, return type, name of the closure, and the body of the method, and the collected program entity names are split into tokens using camel case and underscore naming conventions, and the obtained tokens are converted to lowercase. In addition, in order to retrieve the AST of the method in subsequent research, it is necessary to process the AST representation of each method. Here we use ASTNN, which is a more advanced AST representation method in code representation. As Figure 2 As shown, the AST sequence is from Figure 1 We extract the code from the code examples in , split each large AST into a set of small statement tree sequences, and each line of code is split into a statement sequence block. This method can better represent the structural information of the code. In order to remove the influence of method names in the retrieval process, we delete the method names in the AST sequence.

[0175] We studied the frequency of the tokens that make up the target method name in the names of the most similar methods retrieved by method implementation and the names of the most similar methods retrieved by method AST sequence. From 15,294,489 method names, we extracted 41,119,385 tokens, which means that each method name consists of 3 tokens on average. Therefore, we analyze the frequency from two perspectives: method name and sub-tokens. As shown in Table 1, there are 75.5% examples whose tokens of the method name can be observed in the names of the most similar methods retrieved by method implementation, while there are 78.6% examples whose tokens of the method name can be observed in the names of the most similar methods retrieved by AST sequence. That is to say, in the case of retrieval by AST sequence, the target method tokens and the most similar method name tokens can be found in more examples. This means that the name of the most similar method retrieved by AST can provide rich predictive information for the name of the target method. At the same time, the results in the token dimension also show this phenomenon. In the case of retrieval by AST sequence, 3.4% more tokens can be observed compared with the method implementation. These results indicate that AST sequences may be more suitable for retrieving two similar codes, and that using the names of the most similar methods retrieved by AST sequences as pattern guides can improve the quality of method name recommendations.

[0176] Afterwards, we counted the examples where the method name token only appeared in the names of the most similar methods retrieved by method implementation, accounting for 3.2%; on the contrary, the examples where the method name token only appeared in the names of the most similar methods retrieved by AST sequence accounted for 5.4%. The research results show that the method names of the most similar methods obtained by both retrieval methods can provide unique prediction information for the name of the target method. The experimental results on the token dimension also confirmed this phenomenon, accounting for 2.9% and 5.2% respectively.

[0177] In summary, for the tokens that make up the method name, 67.1% of the tokens can be observed in the name of the most similar method retrieved by AST sequence. And compared with the method implementation, 5.2% of the tokens can only be observed in the name of the most similar method retrieved by AST sequence. This means that we can use two retrieval methods and get two pattern guides to provide prediction information for the target method name.

[0178] Table 1. How often method name tokens can be found in the names of the two most similar methods.

[0179]

[0180] We propose a method name recommendation method Mul-NamPat that integrates structural patterns and text patterns. The overall framework of Mul-NamPat is as follows: Figure 3 As shown in the figure, it mainly includes two modules: naming pattern extraction module and method name generation module. In the naming pattern extraction module, two retrieval libraries are established based on the training dataset, one of which is based on the method implementation token and the other is based on the method AST sequence. For a method, the open source search engine Lucene is used to retrieve the most similar methods in the two retrieval libraries according to the method implementation token sequence and AST sequence. Then, the names of the most similar methods are extracted as pattern guides and AST pattern guides with naming patterns. At the same time, multiple context sequences of the input method are extracted to represent the semantics of the input method. In the method name generation module, a seq2seq framework is used to generate method names. Specifically, three encoders are used to encode the two pattern guides and implementation context into vector representations respectively. The decoder generates the target method name by predicting the probability order of tokens in the method name.

[0181] 3. Experimental Analysis

[0182] (1) Dataset

[0183] In order to evaluate the performance of Mul-NamPat on the method name recommendation task, in our study, we chose the widely used Java dataset built by Nguyen et al. This dataset contains more than 10,000 Java projects collected from GitHub and ensures that each method name has a relatively stable version. We extracted 17M training methods and 580K test methods from this dataset.

[0184] (2) Benchmark method

[0185] In order to evaluate the performance of Mul-NamPat on the method name recommendation task, we selected four method name recommendation models, each of which has a representative recommendation method and has achieved certain results. They are code2seq, code2seq, MNire and Cognac. In addition, NamPat is included in the baseline model.

[0186] (3) Evaluation indicators

[0187] In order to evaluate the effectiveness of Mul-NamPat in method name recommendation, the evaluation indicators Precision, Recall, F1-score and EM Acc are used. All indicators are mainly evaluated on the token dimension. For the target method name t and the recommended method name r, the evaluation indicators Precision(t, r), Recall(t, r) and F-score(t, r) are calculated as follows:

[0188]

[0189] Here, token(n) refers to all constituent tokens in method name n. The evaluation metrics of the model are calculated as the average of all samples in the dataset.

[0190] (4) Experimental setup

[0191] Mul-NamPat is implemented based on the PyTorch framework. In the data processing before the experiment, we used javalang to parse the Java code to obtain the AST sequence information and context sequence information of the method. The length of the context sequence is limited to 400, and the part exceeding 400 is truncated. The length of the two naming pattern director sequences is limited to 6.

[0192] In terms of model settings, we set the token embedding dimension to 150, the LSTM hidden state dimension to 400, and the batch size (i.e., the number of training samples per batch) to 128. We use the Adagrad optimizer to optimize the training model, with a learning rate of 0.15 and an initial accumulator value of 0.1. During the training process, we set a callback method to end the training early. If no better loss value is achieved on the validation set for five consecutive times, the training process will end early. The model was trained for approximately 1 million iterations (8 epochs). All experiments on the model were conducted on an NVIDIA Titan V GPU with 12GB of memory.

[0193] (5) Mul-NamPat experiment and result analysis

[0194] 1) Performance analysis of Mul-NamPat

[0195] Mul-NamPat mines the structured naming patterns existing in the names of the most similar methods based on AST retrieval, fully integrates them with the naming patterns obtained based on method implementation as the retrieval mechanism, and uses the cross-attention mechanism to enable the two naming patterns to fully guide the method context information.

[0196] The experimental results are shown in Table 2. The performance of Mul-NamPat is better than all baseline models in all indicators. Specifically, in terms of precision, Mul-NamPat is improved by 3.7% to 31.8% compared with all baseline models, which shows that our model can predict method name tokens more accurately. This is due to the pattern director based on AST retrieval introduced by us. It introduces method name tokens that cannot be observed in method context information and method implementation pattern director. These additional token information helps the model predict more accurate method name tokens. In terms of recall, Mul-NamPat is improved by 6.3% to 70.2% compared with all baseline models. This shows that the present invention can predict accurate method names when generating fewer token lengths, reflecting that the present invention has certain advantages in understanding method semantics. This is due to the introduction of the common attention mechanism and the cross attention mechanism, which enables the information of the two pattern directors to be fully integrated, better guides the semantic understanding in the method context information, and recommends more accurate method names. In terms of exact matching accuracy (EM Acc), Mul-NamPat has a relative improvement of 10.0% to 60.6% compared to all baseline models, which shows that the present invention has a high performance in predicting accurate method names, can correctly match the functions implemented by the method body, and has a certain structured naming pattern. This is due to the two pattern directors we use to help the model generate method names with naming patterns. Compared with the baseline model that simply mines context information, generating method names with naming patterns is obviously more in line with method semantics.

[0197] Experiments show that Mul-NamPat outperforms all baseline models in various indicators.

[0198] Table 2 Comparison of recommended method names

[0199]

[0200] 2) AST Mode Director Performance Analysis

[0201] In Mul-NamPat, we introduced a pattern guide based on AST retrieval to help recommend method names. In order to explore the effectiveness of the AST pattern guide, we conducted an ablation experiment. Before the experiment, in order to better explore the performance of the AST pattern guide, we excluded the common attention mechanism and the cross attention mechanism from Mul-NamPat to reduce the impact on the effectiveness of the experiment, and only retained the method implementation pattern guide and the AST pattern guide.

[0202] The experimental results are shown in Table 3. "-AST Pattern Guider" indicates the case with only the method implementation pattern guide, and "+AST Pattern Guider" indicates the case with the AST pattern guide added on the basis of the method implementation pattern guide. The experimental results show that when we introduce the AST pattern guide obtained based on AST retrieval, the performance of the model has been greatly improved. Specifically, after the introduction of the AST pattern guide, the precision (Precision) increased from 74.2% to 76.2%, the recall rate (Recall) increased from 71.4% to 73.9%, the F-score (F-score) increased from 72.8% to 75.0%, and the exact match accuracy (EM Acc) increased from 50.4% to 53.1%. The improvement in precision and recall shows that the introduced AST pattern guide can bring more tokens of method names and provide more prediction information.

[0203] As analyzed above, the AST pattern director contains some additional token information, which cannot be observed in the context information and method implementation pattern director. In terms of exact match accuracy (EM Acc), the introduction of the AST pattern director also has a significant improvement, which shows that although there is no fusion mechanism to fuse the two pattern directors, the AST pattern director can still provide method naming patterns to help generate method names. The introduction of the AST pattern director increases the probability of the model generating the correct naming pattern.

[0204] The above results show that the pattern guide obtained based on AST retrieval can improve the performance of method name recommendation tasks.

[0205] Table 3 Effect of AST mode director on Mul-NamPat

[0206]

[0207] 3) Performance analysis of the fusion mechanism of the two naming modes

[0208] In this experiment, we want to explore the impact of the co-attention mechanism and cross-attention mechanism introduced in Mul-NamPat on model performance. The co-attention mechanism is used to associate and fuse the naming pattern information in two different pattern directors. The cross-attention mechanism makes the method context information pay more attention to the information in the pattern director, reads the pattern information in the pattern director, understands the method context semantics, and generates structured and meaningful method names.

[0209] The experimental results are shown in Table 4. "Mul-NamPat (w / o co)" means removing the co-attention mechanism from the model. That is to say, when we use the co-attention mechanism, it increases the precision from 76.4% to 77.0%, the recall from 74.3% to 75.9%, the F-score from 75.3% to 76.4%, and the exact match rate from 53.8% to 55.4%. The recall rate and exact match accuracy have been greatly improved, which means that when using the co-attention mechanism, the model can better integrate the information of the two mode directors, so as to provide a more accurate naming pattern for method name recommendation and be more accurate in method semantic understanding. "Mul-NamPat (w / o cross)" means removing the cross attention mechanism from the model. Specifically, when the model uses the cross attention mechanism, it improves the precision from 76.3% to 77.0%, the recall from 75.1% to 75.9%, the F-score from 75.7% to 76.4%, and the exact match rate from 54.4% to 55.4%. All indicators have been significantly improved. The experimental results show that when Mul-NamPat uses the cross attention mechanism, the model can better learn the naming pattern information from the pattern guide to guide the semantic learning of the method context information, help the method context information better understand the functional semantics of the method, and thus recommend the correct method name.

[0210] The above results show that the joint attention mechanism and the cross attention mechanism can help the model better learn pattern information and method context information, and improve the performance of the method name recommendation task.

[0211] Table 4 Effects of cross attention and common attention mechanisms on Mul-NamPat

[0212]

[0213] 4) Analysis of weight parameters of two naming modes

[0214] The purpose is to explore the impact of different fusion parameters on the model when using the cross attention mechanism in Mul-NamPat. When using cross attention, different pattern directors will perform a cross attention on the method context information, and the group will be fused in different proportions, as shown in formula (19), γ represents the weight of the method implementation pattern director, and 1-γ represents the weight of the AST pattern director. We want to explore the impact of different weight parameters γ on model performance.

[0215] The experimental results are shown in Table 5. The performance of the fusion model will be different according to different weights γ. We observed that when the weight parameter γ is 0.4, the model shows the best performance. This means that when the model assigns more weight to the AST pattern director, the model can have better performance. As mentioned above, in the AST pattern director, more method name sharing tokens can be observed, and more additional token information can be observed. However, when the weight is further increased, we find that the performance of the model decreases instead. In other words, we cannot rely solely on the pattern information in the AST pattern director. The pattern in it does not always correctly reflect the naming pattern of the current method, but needs to be combined with the information in the method implementation pattern director, which also includes a certain proportion of additional tokens. When we continue to increase the weight parameter γ, the performance of the model decreases significantly. When the weight parameter is greater than 0.8, the performance appreciation of the model is lower than that without the cross attention mechanism. This shows that when more weight is tilted to the method implementation pattern director, the information in the pattern director cannot guide the learning of method context information well.

[0216] The above results show that when using the cross-attention mechanism, assigning a higher weight to the AST pattern guide can improve the pattern guide's understanding of method context information and improve the performance of the method name recommendation task.

[0217] Table 5 The impact of cross attention fusion weight on Mul-NamPat

[0218]

[0219] Finally, it should be noted that the above embodiments are only used to illustrate the technical solution of the present invention rather than to limit it. Although the present invention has been described in detail with reference to the preferred embodiments, those skilled in the art should understand that the technical solution of the present invention can be modified or replaced by equivalents without departing from the purpose and scope of the technical solution of the present invention, which should be included in the scope of the claims of the present invention.

Claims

1. A method name recommendation method based on the fusion guidance of structural mode and text mode, characterized by: The steps include: S100: Select a public data set, which includes several project methods, randomly select multiple data from the public data set as a training set P, extract words from the data in the public data set to obtain a vocabulary, and use each project method in the training set as a training sample; S200: constructing a method name recommendation model Mul-NamPat, wherein Mul-NamPat includes a naming pattern extraction module and a method name generation module; The naming pattern extraction module includes a search engine retrieval module and a context extraction module; S300: Selecting a p-th training sample from P, wherein the p-th training sample includes a number of real word tokens; The p-th training sample is used as the input of the naming pattern extraction module, and the search engine retrieval module outputs two naming pattern guides of the p-th training sample, which are a text pattern guide and a structure pattern guide; The context extraction module outputs the context sequence of the pth training sample; S400: using the text naming pattern guide, the structure naming pattern guide and the context sequence obtained in S300 as inputs of a method name generation module, and outputting a recommended method name for the pth training sample; S500: setting a loss threshold, calculating the loss of the p-th training sample, and when the loss of the p-th training sample is less than the loss threshold, obtaining a trained Mul-NamPat model, and executing the next step; Otherwise, update the parameters of Mul-NamPa in reverse direction, set p=p+1, and return to S300; S600: Input the project method to be recommended into the trained Mul-NamPa to obtain the recommended method name for the project method.

2. The method for recommending method names based on the fusion guidance of structural mode and text mode according to claim 1, characterized in that: The method name generation module in S200 includes encoder 1, encoder 2, encoder 3 and a decoder, wherein encoder 1, encoder 2 and encoder 3 are all single-layer bidirectional recurrent neural networks, and the decoder is a single-layer unidirectional recurrent neural network.

3. The method for recommending method names based on the fusion guidance of structural mode and text mode according to claim 2, characterized in that: The specific steps of obtaining the name of the recommended method for the pth training sample in S400 are as follows: S410: Input the text naming pattern guide of the p-th training sample into the encoder 1 to obtain the text pattern guide embedding vector x′, input the structure naming pattern guide of the p-th training sample into the encoder 2 to obtain the structure naming guide embedding vector x″, and input the context sequence of the p-th training sample into the encoder 3 to obtain the context embedding vector x, and the expressions are respectively expressed as follows: x″=(x″1,x″2,...,x″ o ); (1) x′=(x′1,x′2,...,x′ n ); (2) x=(x1,x2,...,x m ); (3) Among them, x′ n represents the embedding vector of the nth word token in x′, x″ o represents the embedding vector of the oth word token in x″, x m represents the embedding vector of the mth word token in x, n represents the length of x′, o represents the length of x″, and m represents the length of x; S420: Use the Bi-LSTM unit to process x′, x″ and x, and calculate the hidden states h′, h″ and h corresponding to x′, x″ and x, respectively. The specific expressions are as follows: in, Represents a connection operation. represents the hidden state of the current Bi-LSTM unit propagating to the right, represents the hidden state of the previous unit from left to right, represents the hidden state of the current Bi-LSTM unit propagating to the left, represents the hidden state of the previous unit from right to left, h l represents the hidden state of the current unit; x a It is an intermediate variable and has no practical significance; Formula (4), Formula (5) and Formula (6) are used to calculate the context sequence hidden state h corresponding to x, the text mode guide hidden state h′ corresponding to x′ and the structure mode guide hidden state h″ corresponding to x″ respectively. The specific expressions are as follows: When x a ∈{x1,x2,...,x m }, we get h=(h1,h2...h m ), i=1,2,…,m; When x a ∈{x′1,x′2,...,x′ n }, we get h′=(h′1,h′2...h′ n ), j = 1, 2, ..., n; When x a ∈{x″1,x″2,...,x″ o }, we get h″=(h″1,h″2...h″ o ), k = 1, 2, ..., o; S430: Mode fusion, using the co-attention mechanism to fuse h′ and h″, and calculate the text mode guide vector V′ and the structure mode guide vector V″, which is specifically expressed as follows: g′ i =h″β i (6) β i =softmax(h″ T W β h′ i ); (7) Where W′ hg represents the trainable parameters, β i is h′ i The attention weights for the structural pattern guides are measured, and the weights for each structural pattern guide token pair h′ are measured. i The importance of h′, g′ represents the perceptual feature vector corresponding to h′, g′ i The information related to the text mode guide is highlighted from the structure mode guide and retained. β ∈R 2d×2d is a trainable parameter; g″ i =h″β′ i (9) Where W″ hg represents the trainable parameters, β′ i It is h″ j The attention weights for the text pattern guides and the weights of the pattern guide tokens on h″ j The importance of g″ represents the perceptual feature vector corresponding to h″, g″ i The information related to the structural pattern guide is highlighted and retained from the method implementation pattern guide; S440: Use V′ and V″ to calculate cross attention for context respectively S=hV′ T (12) S row =softmax row (S); (13) S column =softmax column (S); (14) A row =S row V′; (15) A column =S row S column T h; (16) A row =S row V′; (17) E code =AVG(h,A row ,h⊙A row ,h⊙A column ); (18) Among them, S represents the association matrix between context information and method implementation guide, S row and S column Respectively represent the correlation between method context and method implementation guide and structural pattern guide, A row With A column represents the word-based cross-attention weights from method context to method implementation pattern guide and from method implementation pattern guide to method context, E code Representation method: context vector representation; Using the structural pattern guide vector through formulas (12)-(18), we can get the new method context vector representation E ast , and finally, by fusing the two new representations, we get the final method context vector representation E: E=γE code +(1−γ)E ast (19) Among them, γ is a configurable hyperparameter; S450: Set the total time step T, select the tth time step from T, and calculate the hidden state s of the decoder at time step t t , the specific expression is as follows: s t =LSTM(s t-1 ,y t-1 ); (20) Among them, s t-1 represents the hidden state of the decoder at the previous time step, y t-1 represents the embedding of a word token; S460: Calculate the weight of the hidden state of the context sequence and the weight of the hidden state of the two mode guides respectively, and the calculation expression is as follows: a t =softmax(e t ); (22) in, represents the attention feature obtained by the i-th hidden state, α t represents the attention score of all hidden states, e t Represents the attention features obtained from all hidden states, that is, v, W h and W s Both represent learnable parameters, v T represents the transpose of v; When e t When representing the attention features of all hidden states of the context sequence, that is, And the attention weight of the hidden state of the context sequence is denoted as α t =α t ,in, When e t When representing the attention features of all hidden states of the text mode guide sequence, that is, And the attention weight of the hidden state of the text mode guide sequence is denoted as α t =α′ t ,in, When e t When representing the attention features of all hidden states of the structure pattern guide sequence, that is, And the attention weight of the hidden state of the structure pattern guide sequence is denoted as α t =α″ t ,in, S470: Calculate mode guide-context sequence vector The specific expression is as follows: in, Represents the context sequence vector, and the calculation expression is as follows: in, represents the weight of the i-th word token at the t-th step, i = 1, 2, ..., m, E i Representation method: Representation of the i-th word in the context vector; Represents the text mode driver vector, and the calculation expression is as follows: in, V represents the weight of the jth word token at the tth step, j = 1, 2, ..., n, j ′ represents the representation of the i-th word of the text pattern guide vector; Represents the structural mode director vector, and the calculation expression is as follows: in, represents the weight of the k-th word token at the t-th step, j = 1, 2, ..., k, V" k represents the representation of the i-th word of the structural pattern guide vector; S480: Calculate the probability p of generating a word token from the vocabulary gen , and its calculation formula is as follows: Among them, p gen ∈[0,1], W s , W z and b ptr represents the learnable parameters, σ represents the sigmoid function, s t represents the hidden state of the decoder at time step t, z t represents the input of the decoder at the current time t, represents a pattern guide-context sequence vector; S490: Calculate the output probability P(y) of each real word token y in the p-th training sample at time step t, and the calculation expression is as follows: Among them, P vocab (y) represents the probability of generating each word token from the vocabulary; S495: Select the word token corresponding to the maximum probability at time step t; S499: Traverse all time steps in the time step T to obtain the word token corresponding to the maximum probability in each time step, that is, obtain T word tokens, and the T word tokens constitute the name of the recommendation method.

4. The method for recommending method names based on the fusion guidance of structural mode and text mode according to claim 1, characterized in that: The specific steps of calculating the loss of the p-th training sample in S500 are as follows: Formula (29) is used to calculate the loss of the pth training sample: in, represents the probability of generating the target vocabulary, represents the output probability corresponding to the real word token of the p-th training sample.

5. The method for recommending method names based on the fusion guidance of structural mode and text mode according to claim 1, characterized in that: The method name generation module in S200 may be a seq2seq attention model.

6. The method for recommending method names based on the fusion guidance of structural mode and text mode according to claim 1, characterized in that: The public data set in S100 may also be used as a search corpus.