A method name recommendation method based on naming pattern

Through the NamPat method, the naming pattern extraction module and the method name generation module are used to solve the problem of inaccurate method name recommendation in the prior art. The generated method name is more semantic and readable, which is better than the prior art.

CN115826947BActive Publication Date: 2025-03-18SHENZHEN QIANHAI ZHONGKE JINGWEI INFORMATION TECHNOLOGY CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211547271.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-12-05
Publication Date
2025-03-18
Estimated Expiration
2042-12-05

AI Technical Summary

Technical Problem

In the prior art, when recommending method names, it is difficult to accurately capture the semantics and naming patterns of the method, resulting in poor readability of the generated method names.

Method used

NamPat, a method name recommendation method based on naming mode, is adopted to build a named mode extraction module and a method name generation module, and uses Lucene search engine and context extraction module to generate recommended method names in combination with encoder and decoder.

Benefits of technology

Improve the accuracy and readability of method names, and better recommend semantic method names, which are better than the prior art on various evaluation indicators.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115826947B_ABST
    Figure CN115826947B_ABST
Patent Text Reader

Abstract

The present invention relates to a method for recommending method names based on naming patterns, which comprises the following steps: firstly, a search library is constructed by using a large-scale data set, a target project method is input, and the method most similar to the target project method is retrieved from the search library through the token-level similarity of the code; then, a context sequence is obtained by using a search engine, and the name of the most similar method is used as a pattern director to provide a naming pattern for the target method name; and NamPat is used to combine the context information of the target method with the pattern director to recommend the method name. The method can make more accurate name recommendations for unknown methods.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of method names, and in particular to a method name recommendation method based on naming patterns. Background Art

[0002] Meaningful and concise method names usually reflect the function of the method, which helps developers understand the behavior of the program. On the contrary, misleading method names may confuse developers and lead to API misuse, resulting in software defects. Therefore, it is crucial to find a suitable name for the method. In fact, constructing a high-quality method name is a challenging and time-consuming task.

[0003] In order to obtain meaningful method names, researchers have proposed various automated methods to recommend meaningful method names. Early studies mainly adopted information retrieval (IR) methods, such as Liu et al., who believed that two methods with similar bodies should have similar names. They retrieved similar methods and reused their method names. However, in many cases, two methods with similar bodies are given different names because they may have different semantics or target different tasks. In addition, IR-based methods cannot recommend new method names that are not seen in the training data.

[0004] With the rapid development of deep learning, researchers use deep learning techniques to recommend suitable method names. Based on the idea that programming languages ​​can greatly benefit from representations that exploit the structured nature of their syntax. Code2vec and Code2seq use a set of paths between two leaf nodes in an abstract syntax tree (AST) to represent the method body for predicting method names. Nguyen et al. pointed out that for method naming, the naturalness factor, i.e., the name of the program entity, is more important than the structured representation. Therefore, they proposed MNire, which generates method names using program entity tags in three contexts, i.e., implementation context, interface context, and enclosing context. Wang et al. found that MNire still has limitations when dealing with methods with little content, so they developed Cognac, which introduces the caller / callee context of the method as prior knowledge to complete the recommendation of method names.

[0005] This type of approach mainly focuses on introducing more contextual information, which may include target method name tokens for recommending method names. Their methods usually generate method names by combining tokens extracted from contextual information, however, this method name contains structured naming patterns, and this combination may result in poor readability of method names. Summary of the invention

[0006] 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.

[0007] In order to solve the above technical problems, the present invention adopts the following technical solution: a method for recommending method names based on naming patterns, comprising the following steps:

[0008] 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;

[0009] S200: constructing a method name recommendation model NamPat, wherein NamPat includes a naming pattern extraction module and a method name generation module;

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

[0011] The method name generation module includes an encoder 1, an encoder 2 and a decoder, wherein the encoder 1 and the encoder 2 are both single-layer bidirectional neural networks, and the decoder is a single-layer unidirectional neural network;

[0012] S300: Selecting a p-th training sample from P, wherein the p-th training sample includes a number of real word tokens;

[0013] The pth training sample is used as the input of the naming pattern extraction module, and the naming pattern guide of the pth training sample is output through the search engine retrieval module; the context sequence of the pth training sample is output through the context extraction module;

[0014] S400: using the naming pattern guide and context sequence obtained in S300 as inputs of a method name generation module, and outputting a recommended method name for the pth training sample;

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

[0016] S600: Input the project method to be recommended into the trained NamPat model to obtain the recommended method name for the project method.

[0017] Preferably, the specific steps of obtaining the name of the recommended method for the p-th training sample in S400 are as follows:

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

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

[0020] x=(x1,x2,...,x m ); (2)

[0021] Among them, x′ n represents the nth word token embedding vector in the pattern guide, x m represents the embedding vector of the mth word token in the context sequence, n represents the length of x′, and m represents the length of x;

[0022] S420: Use the Bi-LSTM unit to process x′ and x, and calculate the context sequence hidden state and the pattern director hidden state. The specific expressions are as follows:

[0023]

[0024] 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 actual physical meaning;

[0025] Use formula (3), formula (4) and formula (5) to calculate the context sequence hidden state h respectively i and the pattern guide hidden state h′ j , specifically described as follows:

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

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

[0028] S430: 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:

[0029] s t =LSTM(s t-1 ,y t-1 ); (6)

[0030] Among them, s t-1 represents the hidden state before the decoder, y t-1 represents the embedding of a word token;

[0031] S440: Calculate the weight of the hidden state of the context sequence and the weight of the hidden state of the pattern director respectively, and the calculation expression is as follows:

[0032]

[0033] α t =softmax(e t ); (8)

[0034] 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;

[0035] 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,

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

[0037] S450: Calculate the mode guide-context sequence vector The specific expression is as follows:

[0038]

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

[0040]

[0041] in, represents the weight of the i-th word token at the t-th step, i = 1, 2, ..., m;

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

[0043]

[0044] in, represents the weight of the j-th word token at the t-th step, j = 1, 2, ..., n;

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

[0046]

[0047] Among them, p gen represents the probability of a word token being marked from the vocabulary and p gen ∈[0,1], W s , W z and b ptr represents the learnable parameters, σ represents the sigmoid function, s t represents the state of the decoder at the current time t, z t represents the input of the decoder at the current time t;

[0048] S470: 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:

[0049]

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

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

[0052] S490: 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.

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

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

[0055]

[0056] 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;

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

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

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

[0060] 1. The method names recommended by this method have greatly improved in various evaluation indicators compared with other comparison methods, and can recommend more accurate, more semantic, and more readable method names for the target method. Through the names of the most similar methods, we can get the repeated naming patterns, and then combine the context information of the target method to encode the naming patterns and context information respectively, and then generate the recommended method names through decoding.

[0061] 2. Empirical study: Our study found that method names have many repeated naming patterns, and methods with similar body codes usually have the same naming patterns.

[0062] 3. Method name recommendation method: We propose a new method name recommendation method NamPat. It uses the names of the most similar methods as pattern guides to provide naming patterns and recommends method names in combination with the context of the method.

[0063] 4. Performance Evaluation: We conducted experiments on a large-scale Java dataset to evaluate the performance of NamPat. The experimental results show that the overall performance of NamPat is better than the most commonly used methods. BRIEF DESCRIPTION OF THE DRAWINGS

[0064] Figure 1 An example of an original method in the present invention and a search schematic diagram of a similar method.

[0065] Figure 2 Schematic diagram of the NamPat model process of the present invention.

[0066] Figure 3 Schematic diagram of the distribution of method name lengths in the experiments of the present invention.

[0067] Figure 4 Example schematic diagram of method names that have been generated in the experiments of the present invention. DETAILED DESCRIPTION

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

[0069] This experiment conducted a large-scale empirical analysis and found that: (1) method names contain many repeated naming patterns, and the names of two methods with similar body codes usually have the same naming pattern; (2) the markers of the most similar method name can be observed in forming the target method name. The name of the most similar method can better infer the target method name. Based on this, the present invention proposes a method name recommendation model NamPat based on naming patterns. NamPat includes two modules: a naming pattern extraction module and a method name generation module: the naming pattern extraction module not only extracts the context information of the target method, but also retrieves the most similar methods and extracts their method names as pattern guides; the method name generation module is a pointer generator network that learns the semantics of naming patterns and context information and combines them to recommend method names.

[0070] See also Figure 1-Figure 2 , a method name recommendation method based on naming pattern, comprising the following steps:

[0071] 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;

[0072] The public data set in S100 may also be used as a search corpus, which is a database used for search when running a search engine search module.

[0073] S200: constructing a method name recommendation model NamPat, wherein NamPat includes a naming pattern extraction module and a method name generation module;

[0074] The naming pattern extraction module includes a search engine retrieval module and a context extraction module. The search engine retrieval module uses Lucene, which is an existing technology. The context extraction module is used to extract context information of the project method. Many existing methods can realize the context extraction work.

[0075] The method name generation module includes an encoder 1, an encoder 2 and a decoder, wherein the encoder 1 and the encoder 2 are both single-layer bidirectional neural networks, the decoder is a single-layer unidirectional neural network, and the neural network is a prior art;

[0076] The method name generation module in S200 may be a seq2seq attention model, and the seq2seq attention model is a new pointer generator network which is an existing technology.

[0077] S300: Selecting a p-th training sample from P, wherein the p-th training sample includes a number of real word tokens;

[0078] The p-th training sample is used as the input of the naming pattern extraction module, and the naming pattern guide of the p-th training sample is output through the search engine retrieval module; the context sequence of the p-th training sample is output through the context extraction module, and the search engine retrieval module performs retrieval based on the retrieval corpus, and returns the method most similar to the sample method according to the similarity of the code tags, and then extracts the method name from the most similar method, and processes the method name into a tag level sequence, and the tag level sequence is a pattern guide with a naming pattern; the context extraction module extracts the implementation context, interface context and closure context in a certain project method, and then processes the three contexts into token level sequences respectively and connects them to obtain the context sequence corresponding to the project method;

[0079] S400: using the naming pattern guide and context sequence obtained in S300 as inputs of a method name generation module, and outputting a recommended method name for the pth training sample;

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

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

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

[0083] x=(x1,x2,...,xm ); (2)

[0084] Among them, x′ n represents the nth word token embedding vector in the pattern guide, x m represents the embedding vector of the mth word token in the context sequence, n represents the length of x′, and m represents the length of x;

[0085] S420: Use the Bi-LSTM unit to process x′ and x, and calculate the context sequence hidden state and the pattern director hidden state. The Bi-LSTM is a single-layer bidirectional long short-term memory, which is a prior art. The specific expression is as follows:

[0086]

[0087] 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 actual physical meaning;

[0088] Use formula (3), formula (4) and formula (5) to calculate the context sequence hidden state h respectively i and the pattern guide hidden state h′ j , specifically described as follows:

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

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

[0091] S430: 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:

[0092] s t=LSTM(s t-1 ,y t-1 ); (6)

[0093] Among them, s t-1 represents the hidden state before the decoder, y t-1 represents the embedding of a word token. During training, y t-1 is the embedding of the (t-1)th word or word token in the correct word token; at test time, y t-1 represents the previous word or word token emitted by the decoder;

[0094] S440: Calculate the weight of the hidden state of the context sequence and the weight of the hidden state of the pattern director respectively, and the calculation expression is as follows:

[0095]

[0096] α t =softmax(e t ); (8)

[0097] 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;

[0098] 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,

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

[0100] At the t-th time step, NamPat pays more attention to the relevant tokens of the context sequence and the pattern guide sequence as needed. Because different word tokens of the method name are considered to be related to different parts of the method, an attention mechanism is introduced in the method name generation model. For example, when the model generates the token "get", it notices the token "return" in the method. Similarly, NamPat can also focus on certain parts of the pattern guide when generating method names. Therefore, according to the attention mechanism, the weight α of each word token in the context sequence and the pattern guide is calculated.

[0101] S450: Calculate the mode guide-context sequence vector Pattern Guide - Context Vector It can be regarded as a fusion representation of the context information in the method and the naming pattern of the method name in the pattern guide. The specific expression is as follows:

[0102]

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

[0104]

[0105] in, represents the weight of the i-th word token at the t-th step, i = 1, 2, ..., m;

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

[0107]

[0108] in, represents the weight of the j-th word token at the t-th step, j = 1, 2, ..., n;

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

[0110]

[0111] Among them, p gen represents the probability of a word token being marked from the vocabulary and p gen ∈[0,1], W s , W z and b ptr represents the learnable parameters, σ represents the sigmoid function, s t represents the state of the decoder at the current time t, zt represents the input of the decoder at the current time t; because NamPat is allowed to copy word tokens from the input sequence and generate word tokens from a fixed vocabulary; thus calculating the generation probability;

[0112] S470: 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:

[0113]

[0114] Among them, P vocab (y) represents the probability of generating each word token from the vocabulary, 1-p gen represents the probability of directly copying the word token from the input sequence as the output; the first part represents the probability of generating y from the fixed vocabulary, and the second part represents the probability of copying y from the input sequence. It is necessary to copy not only from the context sequence of the input method, but also from the pattern-guided tag sequence. Therefore, the attention distribution here includes two parts: the context tag sequence and the pattern-guided tag sequence; if y is an OOV word token, the probability of generating the word token y from the fixed vocabulary is 0, then P vocab (y) is 0. That is, the model replicates words based on the attention distribution of the input token sequence. This will enable the model to generate OOV word tokens.

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

[0116] S490: 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.

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

[0118] The specific steps of calculating the loss of the p-th training sample in S500 are as follows:

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

[0120]

[0121] 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;

[0122] S600: Input the project method to be recommended into the trained NamPat model to obtain the recommended method name for the project method.

[0123] Experimental setup

[0124] Dataset

[0125] To evaluate the performance of NamPat on the method name recommendation task, this experiment tests it on a widely used Java dataset constructed by Nguyen et al. This dataset contains more than 10K Java projects, which are collected from the top-ranked public projects on GitHub. Following the same settings of Nguyen et al., this experiment randomly splits the dataset into 9772 training projects and 450 testing projects, including 17M training methods and 580K testing methods. In addition, the corpus is shuffled according to projects instead of files, which avoids the unfair performance improvement caused by file-based shuffling.

[0126] Evaluation indicators

[0127] To evaluate the quality of the method names generated by the NamPat model, this experiment uses the same metric as previous work, which measures case-insensitive Precision, Recall, and F-score. Specifically, for the target method name t and the recommended method name r, their Precision(t,r), Recall(t,r), and F-score(t,r) are calculated as:

[0128]

[0129]

[0130]

[0131] where token(n) returns the token in name n. The overall performance is calculated as the average of all samples in the dataset. An additional metric Exact Match Accuracy (EM Acc) is also used, which also takes into account the order of tokens.

[0132] Implementation details

[0133] NamPat is implemented based on the PyTorch framework. In this experiment, the word embedding dimension is set to 150, the LSTM hidden state dimension is set to 400, and the batch size is set to 128. Gradient clipping with a maximum gradient norm of 2 is used, and Javalang is used to parse the Java code and extract context information. In the experiment, the sequence constituting the method context is limited to 400 tokens, the pattern leader sequence is limited to 6 tokens, and the length of the target method name is limited to 6. In the test phase, beam search with a beam size of 4 is used to generate method names. The model is trained for about 1.3 million iterations (10 epochs) using Adagrad with a learning rate of 0.15 and an initial accumulator value of 0.1. All experiments of the NamPat model are performed on an NVIDIA Titan V GPU with 12GB of memory.

[0134] Results Evaluation

[0135] This experiment compares NamPat with the following state-of-the-art name recommendation models:

[0136] Code2vec: An attention-based neural model decomposes the code into a set of paths in its AST and aggregates all path vectors into a single vector through an attention mechanism. The method name recommendation task is cast as a classification task, using the representation vector of the method to predict the method name.

[0137] Code2seq: An extended method of Code2vec, which leverages the seq2seq framework to represent code snippets as paths set in their AST using LSTMs, and uses an attention mechanism to select relevant paths when generating method name sub-tokens.

[0138] MNire: The RNN-based seq2seq method uses the program entity names in the method body and the enclosing class name to recommend method names.

[0139] Cognac: Context-guided method name recommendation method recommends method names by using the local context of methods, the global context of interacting methods, and the probabilities of tokens in different contexts.

[0140] As shown in Table 1, NamPat outperforms all baseline models in all metrics. In terms of exact match accuracy, NamPat achieves 50.4%, which is a relative increase of 16.9% to 46.1% compared to the baselines. The higher the exact match accuracy, the more method names recommended by NamPat exactly match the ground truth. The method names recommended by all baseline models sometimes have high f-scores, but they cannot achieve exact matches because their models usually cannot learn the naming patterns of method names written by developers. For example, the ground truth is "prepare update item statement" and the recommended name is "prepare delete item statement". In this case, it gets a good f-score, but no exact match occurs. In NamPat, the names of the most similar methods are considered as pattern guides, which are written by people with naming patterns. The pattern guide will guide NamPat to recommend method names based on the same naming pattern so that the recommended method names can better match the ground truth.

[0141] Table 1 Comparison of recommended method names

[0142] method Accuracy Recall F-score Full match accuracy code2vec 58.4% 44.6% 50.6% 38.4% code2seq 67.4% 57.5% 61.9% 41.3% MNire 70.1% 64.3% 67.1% 43.1% Cognac 69.8% 55.8% 62.1% 34.5% NamPat 74.2% 71.4% 72.8% 50.4%

[0143] As shown in Table 1, both Code2vec and Code2seq represent methods by using AST paths. However, due to the complexity of methods, the structure of ASTs is usually large and deep. This complex representation leads to lower performance than MNire on all metrics, which uses the names of program entities instead of AST paths to generate method names. These results prove that the NamPat model is correct in using the names of program entities, and NamPat has the highest recall and precision compared to all baselines. Specifically, NamPat improves precision by 5.8% to 27.1%, recall by 11.1% to 60.1%, and f-score by 8.5% to 43.9%, which means that the tokens in the method names generated by the NamPat model can cover more target tokens than other baselines. Through analysis, the pattern guide plays an important role in NamPat, which has a strong correlation with the target method name and introduces more tokens that constitute the target method name.

[0144] NamPat outperforms the state-of-the-art methods. Therefore, the NamPat model can fully recommend method names that can accurately describe the functions of the method body.

[0145] Table 2 Effect of mode guide on NamPat

[0146] Model Accuracy Recall F-score Full match accuracy NamPat-Pattern Guide 67.6% 54.7% 60.5% 34.1% NamPat 74.2% 71.4% 72.8% 50.4%

[0147] In the NamPat model, the context information of the three methods and the name of the most similar method are used as pattern guides to generate method names. In order to explore the effectiveness of the pattern guide, this experiment removed it from NamPat and only used the three context information as input to recommend method names.

[0148] The experimental results are shown in Table 2. When the pattern guide is used in this experiment, the performance of NamPat is greatly improved. Specifically, it improves the accuracy from 67.6% to 74.2%, the recall from 54.7% to 71.4%, the F-score from 60.5% to 78.8%, and the exact matching accuracy from 34.1% to 50.4%. Among them, there are significant improvements in recall and exact matching accuracy. These results mean that it is difficult to generate a method name with the correct naming pattern by relying solely on the three local context information of the method (implementation context, interface context, and enclosing context). When NamPat uses a pattern guide, it can generate more accurate matching method names based on the naming pattern of the pattern guide and cover more target tags.

[0149] In order to measure the performance of NamPat under different method name length settings, this experiment first statistically analyzed the number of methods with different method name lengths in the test corpus. Figure 3 As shown in the figure, methods with a length greater than or equal to 5 are put together, accounting for about 12.7% of the total methods in the test corpus. At the same time, it can be found that the proportion of method names with a length of 1 is only 8.9%. When the length of the method name is too short, it may be difficult to describe the function of the method. The number of method names with a length of 2 and 3 is the largest, accounting for 64.1% in total. Almost two out of every three methods have names with a length of 2 or 3. These results show that developers tend to use two or three words to represent method names.

[0150] Table 3 Performance of NamPat on method names of different lengths

[0151] Length 1 Length 2 Length 3 Length 4 Length ≥ 5 Accuracy 40.3% 68.1% 79.1% 83.1% 84.5% Recall 70.0% 75.5% 76.2% 73.3% 61.3 F-score 51.2% 71.6% 77.6% 77.1% 71.0% Full match accuracy 48.1% 56.1% 61.3% 43.5% 17.1%

[0152] As shown in Table 3, this experiment tests the performance of NamPat on method names of different lengths. NamPat performs best on method names with a length of 3, with an f-score of 77.6% and an exact match accuracy of 61.3%. For method names with a length of 2, NamPat also achieves good performance, with an f-score of 71.6% and an exact match accuracy of 56.1%. These results show that the method names generated by the NamPat model are very consistent with the naming habits of developers. When the method name length is 1, NamPat's F-score is only 51.2%, which shows that NamPat prefers to recommend detailed names rather than simple names. For method names with a length greater than or equal to 5, NamPat still has an f-score of 71.0%, because the pattern guide provides a unique naming pattern and many predictable tokens. But NamPat's exact match accuracy is only 17.1%, indicating that as the length of the method name increases, it will become more difficult to generate method names that exactly match the ground truth.

[0153] NamPat model validity explanation

[0154] A. Qualitative analysis

[0155] like Figure 4 Figure 2 shows four examples generated by NamPat and the state-of-the-art baseline Cognac from the test set. Figure 4 In Example 1 and Example 2 in , Cognac recommends method names "invoke user" and "applypaint", where tokens can be observed in the context of the method, but the recommended names do not match the true semantics of the method. In NamPat, with the help of the pattern guide, this verification obtains the naming patterns "create_" and "set_" of the target method name, which helps NamPat generate more meaningful method names; in addition, it can be observed in Example 3 that the naming pattern in the pattern guide is "on create__", and the naming pattern provides specific prediction information, that is, the tokens of "on" and "create" appear in the pattern guide, rather than being observed in the method context; in Example 4, the method name recommended by Cognac is "set attributes", and its naming pattern is "set__", which does not conform to the semantic structure of the method, but with the help of the pattern guide, NamPat can obtain the correct naming pattern "parse__". The above 4 examples show that the method names recommended by NamPat are close to the correct method names, while the previous models tend to generate tokens observed in the context and sometimes fail to recommend meaningful method names.

[0156] B. Performance enhancement from replication mechanism

[0157] Previous studies, such as MNire based on the seq2seq model, can only generate tokens from its own fixed vocabulary, but cannot copy tokens from the input sequence, which makes the model encounter the OOV problem. Therefore, a copy mechanism is used in the NamPat model to copy tokens from the input sequence, which enables the NamPat model to handle OOV tags; however, this is not the main reason for the superior performance of the NamPat model. To verify this point of view, the present invention removes the copy mechanism from NamPat by setting pgen in the model to 1, so that the NamPat model can only generate tokens from a fixed vocabulary, but not from the input sequence. The experiment of the present invention evaluates the performance of the same dataset, and the experimental results show that the performance of the model with the copy mechanism removed reaches 72.1% (Fscore), which is slightly lower than NamPat (72.8%) and much higher than MNire (67.1%) and Cognac (62.1%); in other words, the performance of NamPat mainly comes from the information provided by the name of the similar method, rather than the copy mechanism. Although the experiment of the present invention is conducted in a large training set, the OOV problem always appears in the tasks of the experiment of the present invention.

[0158] Therefore, it is reasonable for the present invention to add a replication mechanism to process OOV tokens. The above results show that introducing the name of the most similar method in the NamPat model NamPat makes it more effective than the existing methods, and the replication mechanism makes NamPat's performance better.

[0159] 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 a naming pattern, characterized in that: 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 NamPat, wherein 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, and the search engine retrieval module adopts Lucene; The method name generation module includes an encoder 1, an encoder 2 and a decoder, wherein the encoder 1 and the encoder 2 are both single-layer bidirectional neural networks, and the decoder is a single-layer unidirectional neural network; S300: Selecting a p-th training sample from P, wherein the p-th training sample includes a number of real word tokens; The pth training sample is used as the input of the naming pattern extraction module, and the naming pattern guide of the pth training sample is output through the search engine retrieval module; The context extraction module outputs the context sequence of the pth training sample; S400: using the naming pattern guide and 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: Set a loss threshold, calculate the loss of the p-th training sample, and when the loss of the p-th training sample is less than the loss threshold, obtain the trained NamPat model and execute the next step; Otherwise, update the NamPat model parameters in reverse order, set p=p+1, and return to S300; S600: Input the project method to be recommended into the trained NamPat model to obtain the recommended method name for the project method.

2. The method for recommending method names based on naming patterns according to claim 1, 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 naming pattern guide of the p-th training sample into the encoder 1 to obtain the pattern guide embedding vector x′, and input the context sequence of the p-th training sample into the encoder 2 to obtain the context embedding vector x, and the expressions are respectively expressed as follows: x′=(x′1,x′2,...,x′ n );(1) x=(x1,x2,...,x m );(2) Among them, x′ n represents the nth word token embedding vector in the pattern guide, x m represents the embedding vector of the mth word token in the context sequence, n represents the length of x′, and m represents the length of x; S420: Use the Bi-LSTM unit to process x′ and x, and calculate the context sequence hidden state and the pattern director hidden state. 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 actual physical meaning; Use formula (3), formula (4) and formula (5) to calculate the context sequence hidden state h respectively i and the pattern guide hidden state h′ j , specifically described as follows: When x a ∈{x1,x2,...,x m }, we get h i =(h1,h2...h m ), i=1,2,…,m; When x a ∈{x′1,x′2,...,x′ n }, we get h′ j =(h′1,h′2...h′ n ), j = 1, 2, ..., n; S430: 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 ); (6) Among them, s t-1 represents the hidden state before the decoder, y t-1 represents the embedding of a word token; S440: Calculate the weight of the hidden state of the context sequence and the weight of the hidden state of the pattern director respectively, and the calculation expression is as follows: a t =softmax(e t ); (8) 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 pattern guide sequence, that is, And the attention weight of the hidden state of the pattern guide sequence is denoted as α t =α′ t ,in, S450: Calculate the 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; Represents the mode director vector, and the calculation expression is as follows: in, represents the weight of the j-th word token at the t-th step, j = 1, 2, ..., n; S460: Calculate the probability p of generating a word token from the vocabulary gen , and its calculation formula is as follows: Among them, p gen represents the probability of a word token being marked from the vocabulary and p gen ∈[0,1],W hgc , W s , W z and b ptr represents the learnable parameters, σ represents the sigmoid function, s t represents the state of the decoder at the current time t, z t represents the input of the decoder at the current time t; S470: 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; S480: Select the word token corresponding to the maximum probability at time step t; S490: 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.

3. The method for recommending method names based on naming patterns 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 (14) 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.

4. The method for recommending method names based on naming patterns according to claim 1, characterized in that: The method name generation module in S200 may be a seq2seq attention model.

5. The method for recommending method names based on naming patterns according to claim 1, characterized in that: The public data set in S100 may also be used as a search corpus.

Citation Information

Patent Citations

  • Java method name recommendation method based on two-stage framework

    CN113408597A

  • Decision-based code annotation generation method fusing information retrieval and deep learning

    CN113743064A