Class code code representation generation method and device, equipment and storage medium
By simplifying class-level source code files and integrating attention mechanisms, high-quality class-level code representations are generated, solving the problem of low representation generation quality in existing technologies for class-level code tasks and improving the model's processing capabilities.
Patent Information
- Application Number
- CN202410825735.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-25
- Publication Date
- 2025-12-12
- Estimated Expiration
- 2044-06-25
AI Technical Summary
In class-level code tasks, existing technologies produce code representations of low quality and poor effectiveness. Direct splicing or simple fusion methods result in excessively long input sequences, which affects model performance.
The source code files are simplified using a class-level code data simplification strategy to obtain a first sequence and a second sequence. The vector representation sequences are then fused using an attention-based representation fusion strategy to generate the code representation of the class-level source code files.
It improves the quality and effectiveness of code representation generation for class code, optimizes model performance, and is suitable for class-level code summarization tasks.
Smart Images

Figure CN118860404B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application belongs to the technical field of machine learning, and particularly relates to a class code code representation generation method and device, equipment and a storage medium. BACKGROUND
[0002] Code representation is to generate code representation that can be used for machine learning model processing from unlabeled source code, and is widely used in various code intelligent tasks such as code search, code summary generation, code defect detection, code generation, etc. Existing source code representation has token sequence based on code text, Abstract Syntax Tree (AST) based, data flow graph based, control flow graph based, etc. The mainstream code representation method mainly depends on code text or Abstract Syntax Tree. However, the code text based representation can capture the specific syntax and keywords of the code, but it is difficult to express the high-level semantic structure of the code. The Abstract Syntax Tree based representation can better reflect the hierarchical structure and logical relationship of the code, but it ignores the detailed syntax information in the code text.
[0003] Some methods that try to combine code text based representation and code AST based representation mainly generate fusion representation through simple methods such as direct splicing, addition and averaging, which are difficult to produce effective representation that can express the essence of the code. In addition, the existing code representation method mainly aims at method level code task, and few directly aims at class level code task. When used for class level code task processing, the task completion quality seriously declines, and even cannot be used, which is mainly because the data complexity faced by the same neural network when processing class level code task is much larger than when processing method level code task. Directly inputting complete class level code segment or corresponding Abstract Syntax Tree into neural network model is difficult to extract useful features from it, which also leads to excessive parameter quantity and high computational resource consumption. For example, the Abstract Syntax Tree sequence is directly connected after the code sequence by using the direct splicing method to form a new input sequence input into the model. This method in the method level code summary task, the code and the Abstract Syntax Tree are usually short, so direct splicing will not cause too long input sequence. However, in the class level code summary task, this method may cause the input length to increase significantly, thereby affecting the model performance. SUMMARY
[0004] The present application aims to provide a class code code representation generation method, device, equipment and storage medium, which aims to solve the problem of low quality and poor effectiveness of class code code representation generation caused by the prior art.
[0005] In one aspect, the present application provides a class code code representation generation method, which comprises the following steps:
[0006] The class-level source code file is subjected to data reduction by using a class-level code data reduction strategy, to obtain a first sequence and a second sequence after reduction.
[0007] The first vector representation sequence corresponding to the first sequence and the second vector representation sequence corresponding to the second sequence are fused by using a representation fusion strategy based on an attention mechanism, to obtain a code representation of the class-level source code file.
[0008] Preferably, the step of reducing the class-level source code file by using a class-level code data reduction strategy to obtain a first sequence and a second sequence after reduction comprises:
[0009] The class-level source code file is subjected to code reduction by using a class-level code text reduction algorithm, to obtain the first sequence.
[0010] The class-level source code file is subjected to node pruning by using an abstract syntax tree pruning algorithm, to obtain the second sequence.
[0011] Preferably, the step of reducing the class-level source code file by using a class-level code text reduction algorithm to obtain the first sequence comprises:
[0012] A code text structure tree of the class-level source code file is constructed.
[0013] The code text structure tree is subjected to depth-first traversal according to a preset to-be-deleted identifier, and codes corresponding to the to-be-deleted identifier are deleted from the code text structure tree.
[0014] The first sequence is obtained according to the code text structure tree after the traversal ends.
[0015] Preferably, the step of pruning the class-level source code file by using an abstract syntax tree pruning algorithm to obtain the second sequence comprises:
[0016] The class-level source code file is subjected to code parsing, to obtain a corresponding abstract syntax tree.
[0017] All nodes in the abstract syntax tree are traversed, and when a current node that is traversed meets a preset pruning condition, a corresponding pruning operation is performed on the abstract syntax tree according to the current node.
[0018] The second sequence is obtained according to the abstract syntax tree after the traversal ends.
[0019] Preferably, the pruning condition comprises a pruning condition, a replacement condition and a folding condition, and the step of performing, when a current node that is traversed meets a preset pruning condition, a corresponding pruning operation on the abstract syntax tree according to the current node comprises:
[0020] when the current node meets the pruning condition, performing a pruning operation on the abstract syntax tree according to the current node;
[0021] when the current node meets the replacement condition, performing a node replacement operation on the abstract syntax tree according to the current node and a preset replacement symbol;
[0022] when the current node meets the folding condition, performing a folding operation on the abstract syntax tree according to the current node.
[0023] Preferably, the step of adopting a representation fusion strategy based on an attention mechanism to fuse the first vector representation sequence corresponding to the first sequence and the second vector representation sequence corresponding to the second sequence to obtain the code representation of the class-level source code file comprises:
[0024] linearly mapping the first vector representation sequence and the second vector representation sequence respectively to obtain a first mapping embedding sequence and a second mapping embedding sequence corresponding thereto;
[0025] calculating an attention score between the first mapping embedding sequence and the second mapping embedding sequence to obtain an attention score matrix;
[0026] normalizing the attention score matrix to obtain an attention weight;
[0027] performing weighted summation on each vector representation in the second vector representation sequence according to the attention weight to obtain the code representation of the class-level source code file.
[0028] Preferably, before the step of linearly mapping the first vector representation sequence and the second vector representation sequence respectively, the method further comprises:
[0029] performing word embedding on the first sequence and the second sequence respectively to obtain the first vector representation sequence and the second vector representation sequence.
[0030] In another aspect, the present application provides a code representation generation device for class code, the device comprising:
[0031] a data simplification unit configured to perform data simplification on a class-level source code file by adopting a class-level code data simplification strategy to obtain a first sequence and a second sequence after simplification;
[0032] a representation fusion unit configured to fuse a first vector representation sequence corresponding to the first sequence and a second vector representation sequence corresponding to the second sequence by adopting a representation fusion strategy based on an attention mechanism to obtain a code representation of the class-level source code file.
[0033] In another aspect, the present application also provides a computing device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the class code code representation generation method as described above.
[0034] In another aspect, the present application also provides a computer readable storage medium storing a computer program, wherein the computer program is executable on a processor to implement the steps of the class code code representation generation method as described above.
[0035] The present application adopts a class code data reduction strategy to reduce the data of the class source code file, obtains a first sequence and a second sequence after reduction, adopts a representation fusion strategy based on an attention mechanism to fuse a first vector representation sequence corresponding to the first sequence and a second vector representation sequence corresponding to the second sequence, and obtains the code representation of the class source code file, thereby improving the generation quality and effectiveness of the class code representation and improving the model performance. BRIEF DESCRIPTION OF DRAWINGS
[0036] Figure 1 is an implementation flowchart of the class code code representation generation method provided by the first embodiment of the present application;
[0037] Figure 2 is an implementation flowchart of the class code code representation generation method provided by the second embodiment of the present application;
[0038] Figure 3 is an implementation flowchart of the class code code representation generation method provided by the third embodiment of the present application;
[0039] Figure 4 is a structural schematic diagram of the class code code representation generation device provided by the fourth embodiment of the present application;
[0040] Figure 5 is a structural schematic diagram of the computing device provided by the fifth embodiment of the present application. DETAILED DESCRIPTION
[0041] In order to make the objectives, technical solutions and advantages of the present application clearer, the present application is further described in detail below with reference to the drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and do not limit the present application.
[0042] The specific implementation of the present application is described in detail below with reference to specific embodiments:
[0043] Example One:
[0044] Figure 1The implementation flow of the code representation generation method of the class code provided by the embodiment of the present application is shown, for the convenience of illustration, only the part related to the embodiment of the present application is shown, and the details are described as follows:
[0045] In step S101, the class-level source code file is subjected to data reduction by using a class-level code data reduction strategy, to obtain a first sequence and a second sequence after reduction.
[0046] The embodiment of the present application is applicable to computing devices, such as personal computers, servers, etc. In the embodiment of the present application, the class-level source code file refers to a file obtained by storing source code defining one or more classes (i.e. Class) in the form of text, and the original class-level source code contains rich program structure, control logic and syntax details, such as method body, initialization block, etc. The code length is usually much longer than the method-level code. Based on this, the class-level source code file is subjected to data reduction by using a class-level code data reduction strategy, to obtain a first sequence and a second sequence after reduction, so as to directly reduce the complexity of input data from the data amount.
[0047] In a feasible embodiment, the class-level source code file is subjected to data reduction by the following steps, to obtain a first sequence and a second sequence after reduction:
[0048] (1) The class-level source code file is subjected to code reduction by using a class-level code text reduction algorithm, to obtain a first sequence;
[0049] In the embodiment of the present application, the input class-level source code file is received, the source code in the class-level source code file is subjected to reduction operation by using a class-level code text reduction algorithm, to obtain a class-level source code text after reduction, and then a class code text sequence is obtained from the class-level source code text after reduction. The class code text sequence is the first sequence. Thus, by directly reducing the length of the source code text of the class, the data complexity of the neural network processing class-level code task is simplified, unnecessary information load is reduced, and at the same time, the attention to the key elements in the code is emphasized, and the understanding and generalization ability of the neural network to the code structure are improved.
[0050] In a feasible embodiment, the class-level source code file is subjected to code reduction by using a class-level code text reduction algorithm, and the specific implementation of the first sequence is described in the following embodiment two, which is not repeated here.
[0051] (2) The class-level source code file is subjected to node pruning by using an abstract syntax tree pruning algorithm, to obtain a second sequence.
[0052] In the embodiment of the present application, the class-level source code file is received, the node pruning operation is performed on the abstract syntax tree of the class by using the abstract syntax tree pruning algorithm, and a pruned abstract syntax tree sequence of the class is obtained, which is the second sequence. Thus, by pruning the AST of the class, information related to the class-level code structure is selectively retained, while some details irrelevant to the code task are deleted, thereby directly reducing the class-level code structure data and optimizing the structure of the generated abstract syntax tree.
[0053] In a feasible embodiment, the class-level source code file is pruned by using the abstract syntax tree pruning algorithm, and the specific implementation of the second sequence is described in Embodiment III below, which is not repeated here.
[0054] Through the above steps (1) and (2), the class-level source code file is data-reduced to obtain the first sequence and the second sequence, thereby reducing the complexity of the class-level code data by data-reducing the class-level source code file from two aspects of the code text of the class and the code structure of the class.
[0055] In step S102, the first vector representation sequence corresponding to the first sequence and the second vector representation sequence corresponding to the second sequence are represented and fused by using the representation fusion strategy based on the attention mechanism to obtain the code representation of the class-level source code file.
[0056] In the embodiment of the present application, a neural network model based on the attention mechanism is constructed in advance. The class code text sequence and the AST sequence obtained after data reduction are received by the neural network model, and the first vector representation sequence corresponding to the class code text sequence and the second vector representation sequence corresponding to the AST sequence are represented and fused by using the representation fusion strategy based on the attention mechanism in the neural network model to obtain the mixed code representation output of the class, which is the final code representation of the class-level source code file. The neural network model based on the attention mechanism includes a full connection layer, an attention calculation layer, a softmax layer, and a weighted summation layer.
[0057] In a feasible embodiment, the first vector representation sequence corresponding to the first sequence and the second vector representation sequence corresponding to the second sequence are represented and fused by using the representation fusion strategy based on the attention mechanism to obtain the code representation of the class-level source code file through the following steps:
[0058] (1) The first vector representation sequence and the second vector representation sequence are linearly mapped to obtain the corresponding first mapping embedding sequence and the second mapping embedding sequence;
[0059] In the embodiment of the present application, the first vector representation sequence corresponding to the class code text sequence and the second vector representation sequence corresponding to the AST sequence are respectively linearly mapped by the full connection layer in the neural network model based on the attention mechanism, so as to respectively map the first vector representation sequence and the second vector representation sequence to a new representation space, and obtain the corresponding first mapping embedding sequence and the second mapping embedding sequence.
[0060] When the first vector representation sequence and the second vector representation sequence are respectively linearly mapped, specifically, in the full connection layer, the linear mapping of the first vector representation sequence and the second vector representation sequence is respectively implemented by mapping formulas m code = Linear(v code ) and m ast = Linear(v ast ), wherein v code is the first vector representation sequence, v ast is the second vector representation sequence, m code is the first mapping embedding sequence obtained after the linear mapping of v code , m ast is the second mapping embedding sequence obtained after the linear mapping of v ast , and Linear() represents a linear transformation implemented by matrix multiplication and a bias term, wherein the bias term allows the transformation to contain a translation, so that a new representation is obtained by learning, so that their information is more distinguishable in the new representation space, which helps the neural network model to learn to focus on different parts of the input sequence, thereby improving the expression ability and sensitivity of the model to the input sequence.
[0061] In a feasible embodiment, before the first vector representation sequence and the second vector representation sequence are respectively linearly mapped, the first sequence and the second sequence are respectively word embedded to obtain the first vector representation sequence and the second vector representation sequence, so that the index of each word is mapped to the corresponding embedding vector through the embedding process, and the class code text sequence and the abstract syntax tree sequence are endowed with vector representation, thereby providing more rich and continuous input representation for subsequent model learning.
[0062] When the first sequence and the second sequence are respectively word embedded, specifically, a word embedding conversion formula is used to respectively convert the class code text sequence (i.e. the first sequence) and the abstract syntax tree sequence (i.e. the second sequence) into vector representation, and obtain the corresponding first vector representation sequence and the second vector representation sequence, wherein w i represents the index of the i-th word in the vocabulary in the first sequence or the second sequence, and This represents the vector representation of the i-th word embedded in either the first or second sequence. The vector representations corresponding to all words in the first sequence constitute the first vector representation sequence, denoted as v. code The vector representations corresponding to all words in the second sequence constitute the second vector representation sequence, labeled as v. ast .
[0063] (2) Calculate the attention score between the first mapping embedding sequence and the second mapping embedding sequence to obtain the attention score matrix;
[0064] In this embodiment of the invention, the attention computation layer of the attention-based neural network model receives the first mapping embedding sequence m output by the fully connected layer. code Second mapping embedding sequence m ast And the attention score calculation formula is used in the attention calculation layer. Calculate m code and m ast The attention scores between the attention points are used to obtain the attention score matrix. scores Specifically, attention scores By m code and m ast transpose (i.e.) This is obtained through matrix multiplication, where attention... scores Each element in represents m code The corresponding position in the middle and m ast The degree of correlation between corresponding positions.
[0065] (3) Normalize the attention score matrix to obtain the attention weights;
[0066] In this embodiment of the invention, the softmax layer of the attention-based neural network model receives the attention output from the attention calculation layer. scores In the softmax layer, a normalized attention formula is used. weights =softmax(attention) scores Attention scores Normalization is performed to obtain the attention weights. weights Specifically, the attention mechanism is implemented using the softmax() function. scores Each attention score in the matrix is mapped to the range (0,1), and the sum of all weights is made equal to 1, to represent the sum of all attention scores given m. code In this case, the model should focus on m ast Which parts of m ultimately lead to... code to m astattention weights .
[0067] (4) according to the attention weight, weighted sum is performed on each vector representation in the second vector representation sequence to obtain the code representation of the class-level source code file.
[0068] In the embodiment of the application, the weighted sum layer of the neural network model based on the attention mechanism receives the attention weights In the weighted sum layer, the weighted sum formula v merge = matmul(attention weights , v ast ) is used to weight and sum the second vector representation sequence v ast The attention weights are applied to fuse the two feature representations to obtain the final code representation v merge .
[0069] Through the above steps (1)-(4), the attention mechanism is used to fuse the two feature representations, and the attention degree to different components is dynamically adjusted, and an effective representation of the class code is output, so that a high-quality vectorized representation of the class code is generated, which breaks through the limitation of the existing simple generation of fused representations by direct splicing, addition and averaging, and is particularly suitable for class code summary generation tasks and class code understanding.
[0070] In the embodiment of the application, a class code data reduction strategy is used to reduce the data of the class-level source code file to obtain a first sequence and a second sequence, and a representation fusion strategy based on the attention mechanism is used to fuse the first vector representation sequence corresponding to the first sequence and the second vector representation sequence corresponding to the second sequence to obtain the code representation of the class-level source code file, thereby improving the generation quality and effectiveness of the code representation of the class code and improving the model performance.
[0071] Example Two:
[0072] Figure 2 The implementation process of the code representation generation method of the class code provided in the second embodiment of the application is shown, only the parts related to the embodiments of the application are shown for the convenience of description, and the details are as follows:
[0073] The class-level source code file is code-reduced in step S101 of the embodiment one by using a class code text reduction algorithm to obtain a first sequence:
[0074] In step S201, a code text structure tree of the class-level source code file is constructed.
[0075] In the embodiment of the present application, the storage format of the class-level source code file is generally related to the programming language used, such as a file written in Java, Python, C++, etc., which is generally saved with an extension of.java,.py,.cpp, etc. Based on this, the class-level source code file is first converted into an XML (Extensible Markup Language) text with extensibility, and then the generated XML text is parsed into a tree structure to obtain a code text structure tree reflecting the hierarchical tree data structure of the class code.
[0076] In the construction of the code text structure tree of the class-level source code file, specifically, first, the class-level source code file is converted into an XML format using the SrcML tool to obtain the corresponding XML text, and then the etree.fromstring function is used to parse the XML string from the XML text to construct the XML tree structure of the class. The constructed XML tree is the code text structure tree of the class-level source code file.
[0077] In step S202, the code text structure tree is depth-first traversed according to the preset to-be-deleted identifier, and the code corresponding to the to-be-deleted identifier is deleted from the code text structure tree.
[0078] In the embodiment of the present application, the XML tree of the class is depth-first traversed, and when the preset to-be-deleted identifier is encountered, a pruning operation is performed to delete the corresponding part from the XML tree.
[0079] In a feasible embodiment, the preset to-be-deleted identifier is used to identify which code blocks, nodes or elements need to be deleted from the structure tree. These identifiers can be specific identifiers, tags, attributes or other mechanisms capable of uniquely or conditionally identifying code elements. Since the Block element identifies the method body in the class code and the Init element identifies the static initialization block or object initialization block in the class code, the to-be-deleted identifier includes but is not limited to the Block element (for example, Block, block, etc.) or the Init element (for example, Init, init, etc.), so that when the Block element or the Init element is encountered during traversal, the class code text is simplified by deleting the code blocks identified by these element identifiers in the XML tree of the class, and the key code reflecting the structure and encapsulation features of the class (such as the member variables and member methods of the class) is emphatically retained.
[0080] In yet another possible embodiment, when the code corresponding to the to-be-deleted identifier is deleted from the code text structure tree, specifically, when the Block element is encountered during the traversal, it is determined whether the parent node of the node corresponding to the current Block (referred to as Block node) is a Class node, if yes, the Block node is retained, otherwise, the Block node is removed from the parent node, that is, the code block corresponding to the Block is deleted, and when the Init element is encountered during the traversal, the code block corresponding to the Init is directly deleted.
[0081] In step S203, the first sequence is obtained according to the code text structure tree after the traversal ends.
[0082] In the embodiment of the present application, after the traversal ends, the XML tree of the refined class is obtained, the XML tree of the refined class is converted back to the code text form, so as to obtain the class-level source code text after the class is refined, then the class-level source code text after the class is refined is subjected to the lexical analysis, so as to decompose the class-level source code text into a series of tokens or tokens, and the tokens are the basic building blocks of the source code, such as keywords, identifiers, operators, delimiters, numbers, strings, etc., finally, the tokens obtained through the lexical analysis are arranged in the order of their appearance in the class-level source code text, so as to form a class code text sequence representing the class-level source code structure, and the class code text sequence is the first sequence.
[0083] When the XML tree of the refined class is converted back to the code text form, specifically, the element_to_text function is used to convert the simplified XML tree into text.
[0084] In the embodiment of the present application, the code text structure tree of the class-level source code file is first constructed, then the code text structure tree is subjected to the depth-first traversal according to the preset to-be-deleted identifier, and the code corresponding to the to-be-deleted identifier is deleted from the code text structure tree, finally, the first sequence is obtained according to the code text structure tree after the traversal ends, so that, by using the class-level code text refinement algorithm, the high-level structure information such as the member variables and the method signature of the class can be retained, the complexity of the neural network learning and processing the class code is reduced, and the concentration of the neural network learning and understanding the key information of the class is improved.
[0085] Example Three:
[0086] Figure 3 The implementation process of the class code text representation generation method provided in the third embodiment of the present application is shown, only the parts related to the embodiments of the present application are shown for the convenience of description, and the details are described as follows:
[0087] The node pruning of the class-level source code file in step S101 of the embodiment one is achieved by using the abstract syntax tree pruning algorithm through the following steps to obtain a second sequence:
[0088] In step S301, the class-level source code file is parsed to obtain a corresponding abstract syntax tree.
[0089] In the embodiment of the application, the AST pruning tool is used to parse the class-level source code file to obtain the abstract syntax tree of the corresponding class, wherein the AST pruning tool is modified from the Program Graph Extractor (PROGEX), and the PROGEX can be used to extract various representations such as CFG, PDG and AST from software source code, and the AST pruning tool is modified from the parsing process of the PROGEX to complete the pruning operation.
[0090] In step S302, all nodes in the abstract syntax tree are traversed, and when the current node traversed meets the preset pruning condition, the corresponding pruning operation is performed on the abstract syntax tree according to the current node.
[0091] In the embodiment of the application, all nodes in the abstract syntax tree are traversed, and when the current node traversed meets the preset pruning condition, the corresponding pruning operation is performed on the abstract syntax tree according to the current node, wherein the preset pruning condition includes a pruning condition, a replacement condition and a folding condition, and the corresponding pruning operation includes a pruning operation, a node replacement operation and a folding operation.
[0092] In a feasible embodiment, all nodes in the abstract syntax tree are traversed, and when the current node traversed meets the preset pruning condition, the corresponding pruning operation is performed on the abstract syntax tree according to the current node through the following steps:
[0093] ① When the current node meets the pruning condition, the pruning operation is performed on the abstract syntax tree according to the current node;
[0094] In the embodiment of the application, the pruning condition is to judge whether the current node is an irrelevant node or an unnecessary subtree in the AST, such as a comment, a blank line and a debugging statement, and if yes, the pruning condition is met, and if not, the pruning condition is not met, and when the current node is an irrelevant node or an unnecessary subtree, the code block corresponding to the current node is removed from the AST, so that the existence of redundant information and noise is reduced through the pruning operation, the key code structure and functional information of the class are retained, and a more pure input is provided for the subsequent class-level code summary task.
[0095] ② When the current node meets the replacement condition, the node replacement operation is performed on the abstract syntax tree according to the current node and the preset replacement symbol.
[0096] In the embodiments of the present application, the replacement condition is to determine whether the current node is a preset specific variable name or constant value, if yes, the replacement condition is met, otherwise, the replacement condition is not met, when the current node meets the replacement condition, a node replacement operation is performed on the abstract syntax tree according to the current node and the preset replacement symbol, specifically, the current node is replaced by a general identifier or a placeholder, so as to replace the specific implementation details in the AST with a more simplified or abstract node, so that the AST expression is more focused on the logical structure and function of a higher abstraction level of the code, thereby the node replacement operation helps the neural network to extract the core intention and function of the class code and learn a more accurate and concise summary.
[0097] ③ When the current node meets the folding condition, a folding operation is performed on the abstract syntax tree according to the current node.
[0098] In the embodiments of the present application, the folding condition is to determine whether there is a continuous same type node, if yes, the folding condition is met, otherwise, the folding condition is not met, when the current node meets the folding condition, that is, there is a node of the same type as the current node and continuous, at this time, a folding operation is performed on the abstract syntax tree according to the current node, specifically, a plurality of continuous same type nodes are folded into one node, so as to realize the combination or simplification of the redundant structure in the AST into a more compact form, thereby reducing the complexity of the AST, and further helping to reduce the processing difficulty of the neural network model for the redundant structure code and improving the learning and generalization effect of the class level code.
[0099] The pruning operation on the abstract syntax tree is realized through the above steps ①-③, so that the complete AST representation of the class is optimized to generate the refined AST of the class, thereby providing a more refined and efficient input for the neural network model for subsequent processing of the class level code summary task.
[0100] In step S303, a second sequence is obtained according to the abstract syntax tree after the traversal ends.
[0101] In the embodiments of the present application, when the traversal ends, the refined AST of the class is obtained, and then the nodes in the refined AST of the class are pre-ordered to obtain the AST sequence of the class, which is the second sequence.
[0102] In a feasible embodiment, when the second sequence is obtained according to the abstract syntax tree after the traversal ends, specifically, the abstract syntax tree obtained after the traversal ends is saved in a dot format file, then the dot file is parsed using the pydot library to obtain the dot file nodes, and then the dot file nodes are pre-ordered to obtain the AST sequence of the pruned class, which is the second sequence.
[0103] In the embodiment of the present application, the class-level source code file is first parsed to obtain the corresponding abstract syntax tree, then all nodes in the abstract syntax tree are traversed, when the current node traversed meets the preset pruning condition, the corresponding pruning operation is performed on the abstract syntax tree according to the current node, finally, the second sequence is obtained according to the abstract syntax tree after the traversal is completed, so that a more compact refined AST representation is obtained by using the proposed AST pruning algorithm, only the important nodes related to the properties, functions, behaviors and class structure of the class are included, and a more refined and efficient input is provided for the neural network processing the class-level code summary task, so that the neural network model processing the class code pays more attention to the high-level structure and semantics of the code, which is helpful to generate high-quality high-abstraction-level code summary.
[0104] Example Four:
[0105] Figure 4 The structure of the class code representation generation device provided in the fourth embodiment of the present application is shown, only the parts related to the embodiments of the present application are shown for convenience of description, which include:
[0106] The data refinement unit 41 is configured to perform data refinement on the class-level source code file by using a class-level code data refinement strategy to obtain the first sequence and the second sequence after data refinement.
[0107] The representation fusion unit 42 is configured to perform representation fusion on the first vector representation sequence corresponding to the first sequence and the second vector representation sequence corresponding to the second sequence by using a representation fusion strategy based on an attention mechanism to obtain the code representation of the class-level source code file.
[0108] Preferably, the data refinement unit 41 includes:
[0109] The code refinement unit is configured to perform code refinement on the class-level source code file by using a class-level code text refinement algorithm to obtain the first sequence.
[0110] The node pruning unit is configured to perform node pruning on the class-level source code file by using an abstract syntax tree pruning algorithm to obtain the second sequence.
[0111] The code refinement unit includes:
[0112] The structure tree construction unit is configured to construct the code text structure tree of the class-level source code file.
[0113] The code text deletion unit is configured to perform depth-first traversal on the code text structure tree according to the preset to-be-deleted identifier, and delete the code corresponding to the to-be-deleted identifier from the code text structure tree.
[0114] The first sequence obtaining unit is configured to obtain the first sequence according to the code text structure tree after the traversal is completed.
[0115] The node pruning unit comprises:
[0116] The code parsing unit is configured to perform code parsing on the class-level source code file to obtain a corresponding abstract syntax tree.
[0117] The node pruning unit is configured to traverse all nodes in the abstract syntax tree, and when a current node that is being traversed meets a preset pruning condition, perform a corresponding pruning operation on the abstract syntax tree according to the current node.
[0118] The second sequence obtaining unit is configured to obtain a second sequence according to the abstract syntax tree after the traversal ends.
[0119] The representation fusion unit 42 comprises:
[0120] The linear mapping unit is configured to perform linear mapping on the first vector representation sequence and the second vector representation sequence respectively to obtain corresponding first mapping embedding sequences and second mapping embedding sequences.
[0121] The score calculating unit is configured to calculate attention scores between the first mapping embedding sequences and the second mapping embedding sequences to obtain an attention score matrix.
[0122] The weight calculating unit is configured to normalize the attention score matrix to obtain attention weights.
[0123] The weighted sum unit is configured to perform weighted sum on each vector representation in the second vector representation sequence according to the attention weights to obtain the code representation of the class-level source code file.
[0124] The code representation generation apparatus for class code according to the embodiment of the application further comprises:
[0125] The sequence embedding unit is configured to perform word embedding on the first sequence and the second sequence respectively to obtain the first vector representation sequence and the second vector representation sequence.
[0126] In the embodiment of the application, each unit of the code representation generation apparatus for class code can be realized by a corresponding hardware or software unit, and each unit can be an independent software or hardware unit, or can be integrated into a software or hardware unit, which does not limit the application. Specifically, the implementation of each unit can refer to the description of the foregoing method embodiments, which will not be described here.
[0127] Example Five:
[0128] Figure 5 The structure of the computing device provided by the fifth embodiment of the application is shown, and only the parts related to the embodiments of the application are shown for ease of illustration.
[0129] The computing device 5 of the embodiment of the present application comprises a processor 50, a memory 51, and a computer program 52 stored in the memory 51 and executable on the processor 50. The processor 50 implements the steps in the code representation generation method embodiment of the class code when executing the computer program 52, for example Figure 1 the steps S101-S102 shown. Alternatively, the processor 50 implements the functions of the units in the above-mentioned device embodiments when executing the computer program 52, for example Figure 4 the functions of the units 41-42 shown.
[0130] In the embodiment of the present application, the class-level code data reduction strategy is used to reduce the data of the class-level source code file, to obtain the first sequence and the second sequence after reduction, and the representation fusion strategy based on the attention mechanism is used to fuse the first vector representation sequence corresponding to the first sequence and the second vector representation sequence corresponding to the second sequence, to obtain the code representation of the class-level source code file, thereby improving the generation quality and effectiveness of the code representation of the class code and improving the model performance.
[0131] The computing device of the embodiment of the present application can be a personal computer, a server. The steps implemented by the processor 50 in the computing device 5 when implementing the code representation generation method of the class code when executing the computer program 52 can refer to the description of the foregoing method embodiments, which will not be described here.
[0132] Example Six:
[0133] In the embodiment of the present application, a computer readable storage medium is provided, which stores a computer program. The computer program is executed by a processor to implement the steps in the code representation generation method embodiment of the class code, for example, Figure 1 the steps S101-S102 shown. Alternatively, the computer program is executed by a processor to implement the functions of the units in the above-mentioned device embodiments, for example Figure 4 the functions of the units 41-42 shown.
[0134] In the embodiment of the present application, the class-level code data reduction strategy is used to reduce the data of the class-level source code file, to obtain the first sequence and the second sequence after reduction, and the representation fusion strategy based on the attention mechanism is used to fuse the first vector representation sequence corresponding to the first sequence and the second vector representation sequence corresponding to the second sequence, to obtain the code representation of the class-level source code file, thereby improving the generation quality and effectiveness of the code representation of the class code and improving the model performance.
[0135] The computer readable storage medium of the embodiment of the present application can include any entity or device capable of carrying computer program code, recording medium, such as ROM / RAM, magnetic disk, optical disk, flash memory, etc.
[0136] The above descriptions are only the preferred embodiment of the application, not intended to limit the application and any modification, equivalent replacement and improvement made in the spirit and principle of the application shall be included in the protection scope of the application.
Claims
1. A method for generating code representations of class code, characterized in that, The method includes the following steps: A class-level code data simplification strategy is used to simplify the class-level source code files, resulting in a simplified first sequence and a second sequence. An attention-based representation fusion strategy is used to fuse the first vector representation sequence corresponding to the first sequence and the second vector representation sequence corresponding to the second sequence to obtain the code representation of the class-level source code file. The steps of using a class-level code data simplification strategy to simplify class-level source code files and obtain simplified first and second sequences include: Construct the code text structure tree of the class-level source code file, perform a depth-first traversal of the code text structure tree according to the preset deletion identifier, delete the code corresponding to the deletion identifier from the code text structure tree, and obtain the first sequence based on the code text structure tree after the traversal is completed; The class-level source code file is parsed to obtain the corresponding abstract syntax tree. All nodes in the abstract syntax tree are traversed. When the current node meets the preset pruning condition, the corresponding pruning operation is performed on the abstract syntax tree according to the current node. The second sequence is obtained according to the abstract syntax tree after the traversal. The step of fusing the first vector representation sequence corresponding to the first sequence and the second vector representation sequence corresponding to the second sequence using an attention-based representation fusion strategy to obtain the code representation of the class-level source code file includes: Linear mapping is performed on the first vector representation sequence and the second vector representation sequence respectively to obtain the corresponding first mapping embedding sequence and second mapping embedding sequence; Calculate the attention score between the first mapping embedding sequence and the second mapping embedding sequence to obtain the attention score matrix; The attention score matrix is normalized to obtain the attention weights; Based on the attention weights, a weighted sum is performed on each vector representation in the second vector representation sequence to obtain the code representation of the class-level source code file.
2. The method as described in claim 1, characterized in that, The pruning conditions include pruning conditions, replacement conditions, and folding conditions. When the current node encountered during traversal meets the preset pruning conditions, the steps for performing the corresponding pruning operation on the abstract syntax tree based on the current node include: When the current node meets the pruning condition, perform a pruning operation on the abstract syntax tree based on the current node; When the current node meets the replacement condition, a node replacement operation is performed on the abstract syntax tree according to the current node and the preset replacement symbol; When the current node meets the folding condition, the abstract syntax tree is folded according to the current node.
3. The method as described in claim 1, characterized in that, Before the step of performing linear mappings on the first vector representation sequence and the second vector representation sequence respectively, the method further includes: Word embedding is performed on the first sequence and the second sequence respectively to obtain the first vector representation sequence and the second vector representation sequence.
4. A code representation generation device for class code, characterized in that, The device includes: The data simplification element is used to simplify class-level source code files using a class-level code data simplification strategy, resulting in a simplified first sequence and a second sequence. The representation fusion unit is used to perform representation fusion on the first vector representation sequence corresponding to the first sequence and the second vector representation sequence corresponding to the second sequence using an attention-based representation fusion strategy to obtain the code representation of the class-level source code file. When the data simplification element uses a class-level code data simplification strategy to simplify class-level source code files and obtains the simplified first and second sequences, it includes: Construct the code text structure tree of the class-level source code file, perform a depth-first traversal of the code text structure tree according to the preset deletion identifier, delete the code corresponding to the deletion identifier from the code text structure tree, and obtain the first sequence based on the code text structure tree after the traversal is completed; The class-level source code file is parsed to obtain the corresponding abstract syntax tree. All nodes in the abstract syntax tree are traversed. When the current node meets the preset pruning condition, the corresponding pruning operation is performed on the abstract syntax tree according to the current node. The second sequence is obtained according to the abstract syntax tree after the traversal. The representation fusion unit, when employing an attention-based representation fusion strategy to fuse the first vector representation sequence corresponding to the first sequence and the second vector representation sequence corresponding to the second sequence to obtain the code representation of the class-level source code file, includes: Linear mapping is performed on the first vector representation sequence and the second vector representation sequence respectively to obtain the corresponding first mapping embedding sequence and second mapping embedding sequence; Calculate the attention score between the first mapping embedding sequence and the second mapping embedding sequence to obtain the attention score matrix; The attention score matrix is normalized to obtain the attention weights; Based on the attention weights, a weighted sum is performed on each vector representation in the second vector representation sequence to obtain the code representation of the class-level source code file.
5. The apparatus as described in claim 4, characterized in that, The pruning conditions include pruning conditions, replacement conditions, and folding conditions. When the current node encountered during traversal meets the preset pruning conditions, the data simplification element performs the corresponding pruning operation on the abstract syntax tree based on the current node, including: When the current node meets the pruning condition, perform a pruning operation on the abstract syntax tree based on the current node; When the current node meets the replacement condition, a node replacement operation is performed on the abstract syntax tree according to the current node and the preset replacement symbol; When the current node meets the folding condition, the abstract syntax tree is folded according to the current node.
6. The apparatus as claimed in claim 4, characterized in that, The device further includes: Word embedding is performed on the first sequence and the second sequence respectively to obtain the units of the first vector representation sequence and the second vector representation sequence.
7. A computing device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the steps of the method as described in any one of claims 1 to 3.
8. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method as described in any one of claims 1 to 3.
Citation Information
Patent Citations
Code searching method based on two-stage attention mechanism
CN112800172A
Code review method and device, electronic equipment and medium
CN117648931A