A method for detecting naming odor in object-oriented programming based on deep learning
Through the TBCNN+ bidirectional LSTM neural network model, combined with the method data set and abstract syntax tree information, the problem of low naming odor detection efficiency in the existing technology is solved, and more efficient naming odor detection and recommendation are achieved.
Patent Information
- Application Number
- CN202210059016.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-01-18
- Publication Date
- 2024-12-17
- Estimated Expiration
- 2042-01-18
AI Technical Summary
The existing deep learning methods have limited efficiency in code naming odor detection, and they fail to make full use of additional information outside the method body. In addition, the deep learning model has a lot of room for improvement in the final effect, making it difficult to accurately detect odors in method naming.
Using an object-oriented program method based on TBCNN+ bidirectional LSTM neural network model, the method data set, abstract syntax tree node type pre-trained set and word vector set are used, combined with the Encoder-Decoder architecture, convolutional layer, attention layer and bidirectional LSTM The layer, comprehensively considers the method name, method body and context information, and performs naming odor detection.
It improves the accuracy of naming odor detection, can automatically detect naming semantic information, recommends appropriate method name sequences, which have higher detection efficiency and accuracy than traditional methods.
Smart Images

Figure CN114398076B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer software code quality analysis, and in particular to a method for naming odor detection based on deep learning. Background Art
[0002] In the process of software development, developers usually focus on whether the code can run successfully, and often ignore a type of code that may cause software quality problems. Any symptom in the code that may cause deep-seated problems is called code smell. Almost all existing code smells have not been well solved, including confusing names, that is, identifier naming smells. Naming smells mainly refer to confusing or ambiguous names that appear in identifiers in the code, such as class names, method names, and variable names. The characteristic of this type of naming is that it cannot clearly summarize the role of modifying variables. According to relevant research, 70% of the source code of a software system is composed of identifiers, and in practice, most developers often ignore the writing of documents and have to read the source code or comments to understand the code. A good identifier can reduce the burden of programmers in software maintenance and reduce the cost of later software maintenance. Therefore, the correct naming of identifiers in the code has a certain promoting effect on the subsequent upgrade and maintenance of the software.
[0003] At present, researchers have proposed many processing methods for existing code naming smells. Traditional methods such as those based on logic / rules and context / feedback can only simply judge from their grammatical structure or use semi-automatic identification of naming smells. With the application of deep learning to this field, it has become possible to automatically detect the semantic information of naming. The differences between methods based on deep learning are concentrated on input information and models. In the input information, almost all methods take the method body into consideration. Some methods will try to add new information, such as class names, names of member variables in the class, etc., and have also achieved good results. However, on the whole, the information added by these methods is relatively fragmented, and none of them fully and effectively utilizes the additional information outside the method body. In terms of the structure of the input method body, most methods adopt the strategy of retaining the original order of the method body and ignore the structural information in the code. In terms of model design, the existing deep learning model still has a lot of room for improvement in the final effect. In summary, although the existing deep learning methods have been improved in various aspects compared with the past, their efficiency still has a lot of room for improvement. In order to more accurately detect the smell in method naming, the present invention proposes a naming smell detection method of an object-oriented program method based on a TBCNN+bidirectional LSTM neural network model. Summary of the invention
[0004] In order to improve the accuracy of method naming odor detection, the present invention proposes a naming odor detection method for object-oriented programs based on TBCNN+bidirectional LSTM neural network model. This method can improve the accuracy of recognition by predicting the method name.
[0005] In order to achieve the purpose of the present invention, the technical solution adopted is summarized as follows:
[0006] A naming smell detection method for object-oriented program methods based on TBCNN+bidirectional LSTM neural network model, whose input is code dataset and output is whether it contains smell results. Its main features include the following steps:
[0007] Step 1: Initialization
[0008] Initialize the parameters required by this method as follows:
[0009] M l : The maximum length of the method context, the default is 50, when the length is greater than max_len, truncation is performed;
[0010] M s : The maximum number of subtrees contained in the abstract syntax tree of a method, the default is 50;
[0011] M n : The maximum number of nodes contained in the subtree, the default value is 70;
[0012] M c : The maximum number of child nodes contained in a node in the subtree, the default is 10;
[0013] B s : The size of data read in at a time, i.e. the batch size, the default is 128;
[0014] N f : Indicates the dimension of the feature vector, the default is 100;
[0015] E s : When the training performance stops improving, stop training after a few rounds. The default value is 5.
[0016] E m : Maximum number of iterations, the default value is 100.
[0017] Initialize the data structure required by this method as follows:
[0018] (1) Construction method data set D, D = {d i |1≤i≤n}, n represents the total number of data samples, d i It can be expressed as a triplet of the i-th method (T i ,M i,A i ),T i Indicates d i The preprocessed method context word set can be expressed as T i ={t ij |1≤j≤t n}, where t ij Represents the i-th method context T i The jth word in t n Represents the context T i The number of words contained in M. i Indicates d i The preprocessed method body information set can be expressed as M i ={m j |1≤j≤t m}, where t m Indicates the number of abstract syntax tree subtrees corresponding to the i-th method, the maximum value is M l ,m j Represents a quad Each sub-item corresponds to the pre-order traversal node set, pre-order traversal child node set, level traversal node set and level traversal child node set of the j-th abstract syntax tree of the ith method. It can be expressed as in express The kth node name in n p express The number of nodes, the maximum value is M n ; It can be expressed as in The child node set corresponding to the kth node in the jth subtree of the pre-order traversal set in the i-th method can be expressed as Where n c express The number of child nodes contained, c m express The mth child node in the m The maximum value is M c . and The expression and and Similar. i Indicates d i The set of method nouns after preprocessing can be represented as A i ={a j |1≤j≤t a}, where a j represents the jth word in the method name of the i-th method, ta Indicates the number of words contained in the method name.
[0019] (2) Construct an abstract syntax tree node type pre-training set P, P = {p i |1≤i≤n p},n p represents the total number of pre-training samples, p i It is represented as the set of abstract syntax tree nodes corresponding to the i-th method, which can be represented as p i ={pn j |1≤j≤n n},n n Represented as the number of abstract syntax tree nodes, pn j Represents the jth node in the abstract syntax tree of the i-th method, which can be represented as a triple (pr j ,pp j ,PC j ),pr j Represents the name of the jth node in the abstract syntax tree of the i-th method, pp j Indicates the parent node name of the jth node in the abstract syntax tree of the i-th method, PC j Represents the set of child nodes of the j-th node in the abstract syntax tree of the i-th method.
[0020] (3) Construct a word vector set E, E = {e i |1≤i≤n},e i It can be expressed as a binary pair (w i ,v i ). i Represents a word, v i Represents w i The corresponding pre-trained word vector. Construct the vocabulary set V = {v i |1≤i≤n}, where n represents the number of words in the pre-trained vocabulary file, which is the same as the number of words in the word embedding file.
[0021] Step 2: Information Extraction
[0022] Step 2.1: Dataset information extraction
[0023] The dataset is processed initially, each class is traversed, and classes containing methods and whose method bodies are not empty are selected. The class names, class comments, and member variable names of the class are counted and represented as the context information of the class methods. The context information is then further processed: punctuation marks and special characters are removed, and single characters with special formats (such as camel case) are split. Finally, it is passed as the method context information parameter to each method in the class.
[0024] Traverse each method in the class, get the method name and method comment, remove punctuation and special characters and split according to the format, and then add the method comment to the current method context information T i In the above example, the processed method name set is marked as A. i . Parse the abstract syntax tree of the method body and first perform a pre-order traversal on it. If the current node is a statement node, then add it as the root node of the subtree to the list of the pre-order traversal of the subtree. Otherwise, continue to traverse the next node. Finally, we get the list of all subtrees representing the pre-order traversal of the method. The logic of the hierarchical traversal is similar to that of the pre-order traversal. The difference is that the subtrees in the subtree list of the hierarchical traversal are arranged in the order of the hierarchical traversal. Finally, save the pre-order and subtree hierarchical traversal lists of the currently obtained method context, method name, and method body.
[0025] Step 2.2: Abstract syntax tree node pre-training dataset pre-processing
[0026] Construct a pre-trained set P of abstract syntax tree node types, traverse each method in the dataset, and parse its abstract syntax tree. Traverse each node in the abstract syntax tree hierarchically, and the saved result of the jth node of the i-th method is represented as pn j , which records the name of the node pr j , parent node name pp j And the name set PC of its child nodes j The node set finally obtained by this method is represented as p i ={pn j |1≤j≤n n}. Then p i Add to P: P←P∪p i .
[0027] Step 3: Pre-training
[0028] Step 3.1: General vocabulary pre-training
[0029] The input of the regular vocabulary pre-training dataset includes method context and method body information. Before input, it is pre-processed: punctuation marks and special characters are removed, and single characters with special formats (such as camel case) are split. Construct a word2vec pre-processing model, input the regular vocabulary pre-training dataset, and after training, obtain the word vector table E and the vocabulary table V. The word vector table E = {e i |1≤i≤n}, where n represents the number of words in the pre-trained word embedding file, e i It can be expressed as a binary pair (w i ,v i ). i Represents a word, v i Represents wi The corresponding pre-trained word vector. Vocabulary V = {v i |1≤i≤n}, where n represents the number of words in the pre-trained vocabulary file, which is the same as the number of words in the word embedding file.
[0030] Step 3.2: Abstract syntax tree node pre-training
[0031] The steps for pre-training nodes in the abstract syntax tree are as follows.
[0032] Step 3.2.1: Count the types of abstract syntax tree nodes that appear in the data set and put all types into a node type list NL.
[0033] Step 3.2.2: Read data set P from B S batch data. For each node p i , get the subscript value corresponding to its node type and parent node type in NL, and then use it as the input value of the pre-trained model. In the pre-trained model, the vector of node x is represented as vec(x), where Where N f Represents the dimension of the feature vector, for each non-leaf node l and its direct child nodes: c1, c2, ..., c n , whose feature representations are vec(o),vec(c1),vec(c2),…,vec(c n ), W i c i N f ×N f The weight matrix is represented by c i The number of leaves under is weighted, denoted as W i The weight value l i , b is the bias value, num(x) represents the number of x, where vec(o) and l i The calculation formula is shown in formula (1) to (2):
[0034]
[0035]
[0036] Step 3.2.3: Construct hidden layer and fully connected layer for node category prediction, where the hidden layer weight W is defined h N f ×h two-dimensional matrix, N f is the number of node type features, h is the number of hidden layer nodes, and the fully connected layer weight W is defined s is h×l, where l is the number of NL nodes, b h ,b sare the biases of the hidden layer and the fully connected layer, W h and W s is a normally distributed random number, b h and b s is a randomly initialized parameter. tanh represents the hyperbolic tangent activation function, f is the activation function, and the final hidden layer is calculated as shown in formula (3), and the fully connected layer is calculated as shown in formula (4).
[0037] hidden = tanh(W h ·E1+b h )#(3)
[0038] logits=f(hidden·W s +b s )#(4)
[0039] Step 3.2.4: Use the cross entropy loss function to evaluate the training effect of the model, as shown in formulas (5) to (6), input the type logits predicted in the above steps and the true type E r , after the softmax layer, let j represent a node type in NL, logits ij It represents the output value of the i-th node of logits in the j-th node type. The range of j is the number of nodes in NL, that is, the number of node categories of classification. rij Indicates E ri At the output value of the jth node, when the loss value loss is E in the training process s The number of rounds will not be reduced or the model execution rounds have reached the upper limit E m , the model terminates. Otherwise, read the next batch of data and start again from step 3.2.2.
[0040]
[0041]
[0042] Step 3.3: Concatenate word embedding table and vocabulary table
[0043] Concatenate the embedding table and word list obtained in steps 3.1 and 3.2 to get the final embedding table E and word list V.
[0044] Step 4: Preprocessing
[0045] The data set in step 2.1 is further processed by traversing each method in it and calculating the method context T of the i-th method. i Intercept the front M lFor the method body, traverse the subtree pre-order traversal list and the level traversal subtree list respectively. First, traverse each subtree in the subtree pre-order traversal list level by level to obtain and Then perform a level traversal on each subtree in the level traversal subtree list to obtain and Then express it as a quaternion m j So for the ith method, its method body can be expressed as M i ,M i ={m j |1≤j≤t m},t m Indicates the number of abstract syntax tree subtrees corresponding to the i-th method. Finally, for the i-th method, it can be finally represented as a triple d i =(T i ,M i ,A i ), and then add it to the method data set D: D←D∪{d i}.
[0046] Step 5: Method name prediction model construction
[0047] The model is based on the Encoder-Decoder architecture. First, read the batch data in the dataset, and then perform the following operations for each row in the batch data:
[0048] Step 5.1: Construct the embedding matrix
[0049] Input data set D, for the method context set T i , method body information set M i and method name word set A i For each word, get the word position number in the dictionary V, and then get the word vector e from the word embedding table E according to this number. i , e i Replace the original word and get the final T i ,M i and A i In addition, since the size of the word embedding matrix is fixed, if the number of eigenvectors is less than the number of rows in the matrix, all subsequent rows are filled with 0; if the number of eigenvectors is too large, the eigenvectors that exceed the number of rows in the matrix are directly discarded.
[0050] Step 5.2: Build the Abstract Syntax Tree Embedding Model (TBCNN)
[0051] Step 5.2.1: Define the convolutional layer. Apply a fixed depth feature detector of size n, which contains the word vectors of n abstract syntax tree nodes, x1, x2, …, x n , the weight matrix of this layer is a two-dimensional matrix W of k×n conv , where k is the word embedding dimension. In order to handle different numbers of child nodes, three weight matrices are used, respectively. Where W conv,i is a linear combination of the above three weight matrices. Using the activation function tanh,b conv represents the bias term, and the final calculation is shown in formula (7).
[0052]
[0053] Step 5.2.2: Define the attention layer. The output of the convolutional layer is weighted and fused to generate the subtree representation vector c i , the calculation method is shown in formulas (8) to (10), where W a ,V u ,b a are randomly initialized parameters, tanh is the activation function, α i is the weight factor.
[0054]
[0055]
[0056] e i =a(y,v i )=v u tanh(W a y+b a )#(10)
[0057] Step 5.2.3: Using the hidden layer and softmax layer, define the weight W h and bias b h , the activation function uses tanh, and its calculation is shown in formula (11). The formula of the softmax layer is similar to formula (5), and the feature vector s representing a subtree is finally obtained h .
[0058] s h =tanh(c i ·W h +b h )#(11)
[0059] Step 5.3: Build a bidirectional LSTM layer
[0060] Step 5.3.1: Traverse the list vector of the subtree of the i-th method obtained after TBCNN in pre-order and subtree level traversal list vector vector is regarded as the concatenation of subtree vectors, where Respectively represent The last subtree row vector of . The context vector vec(T i ) is regarded as the concatenation of each word vector, where vec(t n ) represents the last feature word vector. The above description is shown in equations (12) to (14).
[0061]
[0062]
[0063]
[0064] Step 5.3.2: vec(T i ) are input into the cyclic unit in turn, and each row vector x is obtained. t The corresponding output vector h t The calculation process can be abstracted into formulas (15) to (20), where c t Indicates the current state of the loop unit, c t-1 represents the state at the previous moment, σ represents the Sigmoid activation function. Others such as W f , W i , W o , W c , b f , b i , b o , b c are randomly initialized parameters.
[0065] f t =σ(W f ·[h t-1 ,x t ]+b f )#(15)
[0066] i t =σ(W i ·[h t-1 ,x t ]+b i )#(16)
[0067] o t =σ(W o ·[h t-1 ,x t ]+b o )#(17)
[0068] q i =tanh(W c ·[h t-1 ,x t ]+b c )#(18)
[0069] c t =f t *c t-1 +i t *q i #(19)
[0070] h t =o t *tanh(c t )#(20)
[0071] Step 5.3.3: vec(T i Each row vector in the matrix is input into the cyclic unit in reverse order, and each reverse row vector x is obtained. i The corresponding output vector h i ',h i The calculation process of ' is the same as step 5.3.2.
[0072] Step 5.3.4: Combine step 5.3.2 with the output vector h at each moment in step 5.3.3 i With h i 'Splice into vector H i , for all H generated at all times i Weighted fusion forms a representation vector, which is the pre-order traversal method body vector Hierarchy traversal method body vector and method context information vector
[0073] Step 5.4: Build the attention layer
[0074] The vector obtained in step 5.3.4 and The representation vector F of the final method is formed by weighted fusion i , as shown in formulas (8) to (10).
[0075] Step 5.5: Constructing the recurrent layer
[0076] Step 5.5.1: Substitute the matrix F formed in step 5.4 i , viewed as a series of row vectors x i The splicing of t Represents the last row vector.
[0077]
[0078] Step 5.5.2: F i Each row vector x in the matrix i , and input them into the loop unit in turn to get each row vector x i The corresponding output vector h i , the calculation formulas are shown in (15)~(20).
[0079] Step 5.6: Iteration termination condition
[0080] The model uses the cross entropy loss function to evaluate the training effect of the model, where the method name i is the real method name, p is the prediction vector, and the formulas are shown in (5) to (6). s The number of rounds will not be reduced or the model execution rounds have reached the upper limit E m , the model terminates and executes step 5.7, otherwise reads the next batch of data and starts again from step 5.1.
[0081] Step 5.6: Model Evaluation
[0082] Let q represent the model number, evaluate model q on the validation set, obtain the accuracy, recall rate and F1 value of the model's prediction of the method name, and write them into the log file.
[0083] Step 5.7: Method name smell detection
[0084] Use the fully trained model to detect method name smell in a code file or folder. If the original method name is semantically similar to the predicted method name, then the current method name does not have code smell. Otherwise, the predicted method name is recommended as the original method name.
[0085] Compared with the prior art, the present invention has the following characteristics:
[0086] (1) The present invention can automatically detect the semantic information of naming based on deep learning and recommend appropriate method name sequences, while traditional methods can only simply judge from its grammatical structure or use semi-automatic methods to identify naming odors.
[0087] (2) The present invention comprehensively considers the dependency information of the method name, the method body and the method context, and proposes different processing methods for different dependency information. The method context adopts sequence, while the method body adopts an abstract syntax tree, and innovatively proposes an abstract syntax tree processing method that combines splitting, pre-order traversal and hierarchical traversal of subtrees. The existing method name prediction methods based on deep learning consider relatively simple information, and even if many factors are taken into account, different processing methods are not given according to the different characteristics of the information.
[0088] (3) This paper proposes a method name odor detection model based on deep learning. The input of the model is different representations of the method, and the output is a sequence of predicted method names. It includes the embedding of the abstract syntax tree (TBCNN), bidirectional LSTM, attention mechanism, etc., and comprehensively considers the characteristics of the input information. BRIEF DESCRIPTION OF THE DRAWINGS
[0089] Figure 1 It is an overall flow chart of the method of the present invention;
[0090] Figure 2 The present invention splits the abstract syntax tree and traverses the graph;
[0091] Figure 3 The method of the present invention based on deep learning is called odor detection model. DETAILED DESCRIPTION
[0092] The present invention proposes a method naming smell detection method based on deep learning, which can infer the method name from the input method body and method context information to detect whether the original method name has code smell.
[0093] The following is a detailed description of the method naming odor detection method based on deep learning proposed by the present invention in combination with specific implementation. For the convenience of explanation, this method simulates a sample data (because the space occupied by the method body will be relatively large, the method body information is not given in the following table) as shown in Table 1:
[0094]
[0095] Step 1: Initialization
[0096] Initialize parameters and data structures, such as the maximum value M of the subtree contained in the abstract syntax tree s is 50, the maximum number of nodes contained in the subtree is M n is 70, the maximum number of child nodes contained in a node in the subtree is M c is 10, and the data size B is read in at a time s is 128, the dimension of the feature vector is N fWhen the training performance is no longer improved, the training will be stopped after a few rounds. s 5, etc., data structures include method data set D, method pre-training set P, etc.
[0097] Step 2: Information Extraction
[0098] Step 2.1: Dataset information extraction
[0099] Dataset information extraction is mainly divided into two parts:
[0100] a) For method context information and method name information, remove special characters, convert all to lowercase, etc.;
[0101] b) For the method body information, it is represented as an abstract syntax tree and split according to the statement nodes. The subtree arrangement order is combined with the pre-order traversal and hierarchical traversal subtrees. At the same time, the abstract syntax tree node pre-training data set is preprocessed.
[0102] After method a), its T i ={check classpath for conflict predicate filecollection classpath a task for checking the classpath for conflictig},A i ={can conflict}, after b), the method body information is represented as {"pre-tree":[{"node":
[0103] "MethodDeclaration","children":[{"node":"Parameter","children":[{"node":"ClassOrInterfaceType","children":[]}]},{"node":"Parameter","child ren":[{"node":"ClassOrInterfaceType","children":[{"node":"ClassOrInte rfaceType","children":[{"node":"ClassOrInterfaceType","children":[]}]}]}]},{"node":"PrimitiveType","children":[{"node":"boolean","children":[]}]}],……}.
[0104] Step 2.2: Abstract syntax tree node pre-training dataset pre-processing
[0105] Construct a pre-trained set P of abstract syntax tree node types, traverse each method in the data set, and parse its abstract syntax tree. Traverse each node in the abstract syntax tree hierarchically, record the name of the node, the name of the parent node, and the name set of its child nodes, and finally obtain the node set p of the method i After the above steps, the method in the example i ={{"node":"MethodDeclaration","parent":null,"children":"Modifier,SimpleName,Parameter,VoidType,BlockStmt"},{"node":"Modifier","parent":"MethodDeclaration","children":""},……}
[0106] Step 3: Pre-training
[0107] Step 3.1: General vocabulary pre-training
[0108] Input method context information and method body into the word2vec model, and finally output word vector table and word table, where word embedding table E = {(final -6.2741876 1.5189061 2.5367842 -0.6484467 -1.84728428.301965……), (class -3.816071 -0.14647038 1.5672442 -3.8296738 -0.08557652 -0.8994526……), …}, word table V = {final, class, …}
[0109] Step 3.2: Abstract syntax tree node pre-training
[0110] Input the pre-training set P, count the node types that appear, and express them as NL = {'MethodDeclaration', 'null', 'Parameter', 'VoidType', 'BlockStmt', 'Class OrInterfaceType', 'ExpressionStmt', 'AssignExpr', 'FieldAccessExpr', 'NameExpr', 'ThisExpr', 'MarkerAnnotationExpr', ...}, then batch obtain the data in the pre-training set P, obtain the subscript values corresponding to its node type and parent node type in NL, and use them as the input of the pre-training model. For the node name MethodDeclaration, its mapping value in NL is 0. If the data in the training set is: {"node":"Modifier","parent":"MethodDeclaration","children":""}, then its input is 49 (Modifier mapping value) and 0 (MethodDeclaration mapping value). Then, 49 and 0 are represented by distributed embedding and input into the pre-training model for training. After the model training is completed, the embedding representation E of the node type is finally obtained, such as {(MethodDeclaration0.61046493 0.94142413 0.7241914 0.1924851 0.8261522 0.47195005 0.2165761…),(Parameter 0.70050824 0.7623019 0.44217777 0.9415456 0.63250136 0.040512920.50738966 0.8111124 0.80725145 0.1672548 0.70254636…)…}. The vocabulary table V is such as {MethodDeclaration,Parameter,VoidType,…}
[0111] Step 3.3: Abstract syntax tree node pre-training
[0112] Just concatenate the word vector table and vocabulary table obtained above.
[0113] Step 4: Preprocessing
[0114] The method body information needs to be processed again. The processing process is as follows: traverse each subtree hierarchically, store the traversed nodes in the node list, and record the child node list corresponding to each node under the list and store it in the child node list.
[0115] For this example, the final output node list is [['MethodDeclaration','Parameter','Parameter','PrimitiveType','ClassOrInterfaceType','ClassOrInterfaceType','boolean','ClassOrInterfaceType','ClassOrInterfaceType'], …], and the final output child node list is [[[1,2,3],[4,0,0],[5,0,0],[6,0,0],[0,0,0],[7,0,0],[0,0,0],[8,0,0],[0,0,0]], …].
[0116] Step 5: Build a method name prediction model
[0117] Step 5.1: Construct the embedding matrix
[0118] First, we build an embedding matrix. For each word in the method context {check classpath for conflictpredicate file collection classpath a task for checking the classpath forconflictig}, we get its position in the dictionary, and then find the corresponding word vector replacement in the word vector table. For example, the position of check in the dictionary is 287. We find the word vector in the word vector table [-1.82929340.4143431 2.0705438 -0.69411916 -1.3756107 2.1083343 -3.0396674 -2.699084 -2.0240371 1.2281425 2.3237095……], and add the vector to the embedding sequence of the current context. The final context sequence is expressed as {(-1.8292934 0.4143431 2.0705438 -0.69411916 -1.37561072.1083343 -3.0396674 -2.699084 -2.0240371 1.2281425 2.3237095……),(0.75084424 -0.7415062 -2.5481532 0.8562336 3.7556057 2.0467503 -0.7671489 -1.6196132 1.853755 -2.0131462 -1.0526531),……}.
[0119] For the method body list [['MethodDeclaration','Parameter','Parameter','PrimitiveType','ClassOrInterfaceType','ClassOrInterfaceType','boolean','ClassOrInterfaceType','ClassOrInterfaceType']……], we can find the corresponding word vector according to the above logic, and finally it can be represented as subTNode i_pre :[[0.61046493 0.94142413 0.72419140.1924851 0.8261522 0.47195005 0.2165761 0.011403203 0.6280153 0.68209030.58157563……],[0.70050824 0.7623019 0.44217777 0.9415456 0.632501360.04051292 0.50738966 0.8111124 0.80725145 0.1672548 0.70254636……],……].
[0120] Step 5.2: Build an Abstract Syntax Tree Embedding Model
[0121] This model is used to represent a subtree as a vector, input abstract syntax tree subtree pre-order traversal node list and a list of child nodes , and output the subtree vector information. For example, if we input [0.610464930.94142413 0.7241914 0.1924851 0.8261522 0.47195005 0.2165761 0.0114032030.6280153 0.6820903 0.58157563……] and [[1,2,3],[4,0,0],[5,0,0],[6,0,0],[0,0,0],[7,0,0],[0,0,0],[8,0,0],[0,0,0]], the final output is the vector representing the subtree: [0.80362740.07352376 0.67833805 0.11362302 0.08857036 0.60882676 0.6897093 0.327088950.67785764 0.043533087 0.5546485……]. When there are n subtrees in the pre-order traversal, the model will output n vectors representing each subtree, which are recorded as the subtree pre-order traversal vectors. Similarly, for the node list of the level traversal and a list of child nodes The embedding matrix is input into the model. Finally, n hierarchical traversal subtrees are input, and n vectors representing each subtree are output, which are recorded as subtree hierarchical traversal vectors.
[0122] Step 5.3: Build a bidirectional LSTM layer
[0123] The subtree pre-order traversal vector, level traversal vector, and method context vector are processed separately. Because the processing logic is the same, the pre-order traversal subtree matrix is taken as an example, and it is regarded as a series of subtree row vectors. They are input into the loop layer in sequence to obtain the output vector corresponding to each row vector, and then reversely input to obtain another output vector, which is finally weighted and fused to form a representation vector.
[0124] Step 5.4: Build the attention layer
[0125] The three vectors obtained in step 5.3 are weightedly fused to form the representation vector of the final method.
[0126] Step 5.5: Constructing the recurrent layer
[0127] The matrix formed in step 5.4 is regarded as a series of row vectors, and then input into the loop layer to generate a word vector sequence, and then find the corresponding words according to the word vectors, and finally output the predicted word sequence. For example, the method name sequence generated in the example method may have can conflict, is conflict, whether conflict, etc. Compare it with the real method name to determine the similarity between the two and give the result of whether the final method name contains odor.
[0128] In summary, the present invention proposes a method for detecting method naming odor based on deep learning. Based on the characteristics of deep learning, the dependency information of the method name is specifically analyzed, and different processing methods are applied according to the different structures. For example, the method context information uses sequential input, and the method body information uses an abstract syntax tree. On this basis, it is split, and its subtrees are traversed using pre-order traversal and hierarchical traversal to obtain the final preprocessing input structure. After that, a method naming prediction model based on deep learning is constructed. After training and optimizing it, the prediction function of the method name sequence can be completed. The preprocessing information processing is more comprehensive, and the model design also takes into account the characteristics of each input information.
[0129] The series of detailed descriptions listed above are only specific descriptions of feasible implementation schemes of the present invention. They are not used to limit the scope of protection of the present invention. All equivalent implementation schemes or changes that do not deviate from the spirit of the invention should be included in the scope of protection of the present invention.
Claims
1. A method for detecting naming odor based on deep learning object-oriented program methods, characterized by: The input is the code dataset and the output is the smell detection result; The following steps are involved: (1) Initialize the required parameters and construct the required related data structures; (2) Information extraction: Input the code dataset and obtain the context information of each method, including the class name, class comment, member variable name in the class, and method comment; extract the abstract syntax tree of each method, then split and traverse it, and preprocess the abstract syntax tree node pre-training dataset; (3) Pre-training: Pre-train the words in the method context and method body and the node types in the abstract syntax tree respectively; (4) Preprocessing: preprocessing the method context information and method body information respectively; (5) Method name prediction model construction; construct the Encoder-Decoder model and output the final detection result based on the input data; Step (1): Initialize the parameters required by the method and construct the relevant data structure required by the method: Step (2-1) Initialize the parameters required by the method, M l Indicates the maximum length of the method context; M s The maximum value of the subtree contained in the abstract syntax tree representing a method; M n Indicates the maximum number of nodes contained in the subtree; M c Indicates the maximum number of child nodes contained in a node in the subtree; B s Indicates the size of data read at a time, that is, the batch size; N f represents the dimension of the feature vector; E s When the training performance stops improving, the training will stop after a few rounds. Step (2-2) constructs the relevant data structure required by the method; Construction method data set D, D = {d i |1≤i≤n}, n represents the total number of data samples, d i Represented as the triplet of the i-th method (T i ,M i ,A i ),T i Indicates d i The preprocessed method context word set is denoted as T i ={t ij |1≤j≤t n }, where t ij Represents the i-th method context T i The jth word in t n Represents the context T i The number of words contained in M i Indicates d i The preprocessed method body information set is represented by M i ={m j |1≤j≤t m }, where t m Indicates the number of abstract syntax tree subtrees corresponding to the i-th method, the maximum value is M l ,m j Represents a quad Each sub-item corresponds to the pre-order traversal node set, pre-order traversal child node set, level traversal node set and level traversal child node set of the j-th abstract syntax tree of the ith method; It can be expressed as in express The kth node name in n p express The number of nodes, the maximum value is M n ; Expressed as in Represents the set of child nodes corresponding to the kth node in the jth subtree of the pre-order traversal set in the i-th method, expressed as where n c express The number of child nodes contained, c m express The mth child node in the m The maximum value is M c ; and The expression and and Similar; A i Indicates d i The set of method nouns after preprocessing, denoted as A i ={a j |1≤j≤t a }, where a j represents the jth word in the method name of the i-th method, t a Indicates the number of words contained in the method name; Construct an abstract syntax tree node type pre-training set P, P = {p i |1≤i≤n p },n p represents the total number of pre-training samples, p i Represented as the set of abstract syntax tree nodes corresponding to the i-th method, denoted as p i ={pn j |1≤j≤n n },n n Represented as the number of abstract syntax tree nodes, pn j Represents the jth node in the abstract syntax tree of the i-th method, represented as a triple (pr j ,pp j ,PC j ),pr j Represents the name of the jth node in the abstract syntax tree of the i-th method, pp j Indicates the parent node name of the jth node in the abstract syntax tree of the i-th method, PC j Represents the set of child nodes of the jth node in the abstract syntax tree of the i-th method; The step (3): pre-training: pre-training the words in the method context and the method body and the node types in the abstract syntax tree respectively: Step (3-1) conventional vocabulary pre-training; the conventional vocabulary pre-training data set input includes method context and method body information, which is first pre-processed before input: punctuation marks and special characters are removed, and single characters with special formats are split; a word2vec pre-processing model is constructed, and the conventional vocabulary pre-training data set is input. After the training is completed, a word vector table and a vocabulary table are obtained; Step (3-2) abstract syntax tree node pre-training; first, count the abstract syntax tree node types NL appearing in the data set P, and then obtain the subscript values corresponding to each node type and parent node type in NL; after inputting the model, represent it as a distributed embedding vector, construct a hidden layer and a fully connected layer to predict the node category, and use the cross entropy loss function to evaluate the training effect of the model. Finally, when the loss value loss no longer decreases during the training process or the model execution round number has reached the upper limit, the pre-trained word vector table and vocabulary are obtained; Step (3-3) concatenates the word vector table and vocabulary table obtained in the previous two steps.
2. The method for detecting odor in naming an object-oriented program method based on deep learning as claimed in claim 1, characterized in that: The step (2): information extraction: input the code data set, obtain the context information of each method, including the class name, class comment, member variable name in the class and method comment; extract the abstract syntax tree of each method, and then split and traverse it; and pre-process the abstract syntax tree node pre-training data set: Step (2-1) removes punctuation marks and special characters from the method context information and splits the single characters with special formats; represents the method body information as an abstract syntax tree and splits it according to the statement nodes, and adopts a combination of pre-order traversal and hierarchical traversal subtrees in terms of the subtree arrangement order; Step (2-2) preprocesses the abstract syntax tree node pre-training data set; constructs an abstract syntax tree node type pre-training set P, traverses each method in the data set, and parses its abstract syntax tree; Traverse each node in the abstract syntax tree hierarchically, record the name of the node, the name of the parent node and the name set of its child nodes, obtain the node set representation of a method, and then add the node set of this method to the pre-trained set.
3. The method for detecting odor in naming an object-oriented program method based on deep learning as claimed in claim 1, characterized in that: The step (4): preprocessing: preprocessing the method context information and the method body information respectively: The data set in step (2) is further processed by traversing each method and calculating the method context T of the i-th method. i Intercept the front M l words; for the method body, traverse the subtree pre-order traversal list and the level traversal subtree list respectively; first, traverse each subtree in the subtree pre-order traversal list level by level, and get and Then perform a level traversal on each subtree in the level traversal subtree list to obtain and Then express it as a quaternion m j ; So for the i-th method, its method body is represented by M i ,M i ={m j |1≤j≤t m },t m Indicates the number of abstract syntax tree subtrees corresponding to the i-th method; finally, for the i-th method, it is represented as a triple d i =(T i ,M i ,A i ), and then add it to the method data set D: D←D∪{d i }.
4. The method for detecting odor in naming an object-oriented program method based on deep learning as claimed in claim 1, characterized in that: In the step (5) of constructing the method name prediction model, an Encoder-Decoder model is constructed to output the final predicted method name sequence based on the input data: Step (5-1) constructs an embedding matrix. For each word in the method context, method body, and method name vectors, obtain the word position number in the dictionary, and then obtain the word vector from the word embedding table based on this number. Since the size of the word embedding matrix is fixed, if the number of feature vectors is less than the number of rows in the matrix, all subsequent rows are filled with 0. If there are too many eigenvectors, the eigenvectors that exceed the number of matrix rows are directly discarded; Step (5-2) establishes an abstract syntax tree embedding model TBCNN, and defines convolutional layer, attention layer, hidden layer and softmax layer to vectorize an abstract syntax tree subtree; Step (5-3) constructs a bidirectional LSTM and traverses the list vector of the subtree pre-order traversal of the i-th method obtained after TBCNN and subtree level traversal list vector As the concatenation of subtree vectors, the context vector vec(T i ) is regarded as the concatenation of each word vector; then vec(T i ) are input into the loop unit in order and reverse order respectively, and then the output vectors obtained in order and reverse order are concatenated to obtain the three fused vectors, which are the pre-order traversal method body vector Hierarchy traversal method body vector and method context information vector Step (5-4) constructs the attention layer and transforms the vector and The representation vector F of the final method is formed by weighted fusion i ; Step (5-5) constructs a recurrent layer and transforms the vector F i Treated as a series of row vectors x i Then each row vector is input into the circulation layer to obtain each row vector x i The corresponding output vector h i ; Step (5-6) is iterated and terminated; the cross entropy loss function is used to evaluate the training effect of the model. When the loss value no longer decreases during the training process or the number of model execution rounds has reached the upper limit, the training is terminated. Otherwise, the next batch of data is read in and step (5-1) is started. Step (5-7) Model evaluation: evaluate on the validation set to obtain the accuracy, recall rate and F1 value of the model for method name prediction and write them into the log file; Step (5-8) uses the fully trained model to detect method name smell in a certain code file or folder. If the original method name is semantically similar to the predicted method name, then the current method name does not have code smell. Otherwise, the predicted method name is recommended as the method name of the original method.
