Double-graph code duplicate checking method and system based on deep learning

Through deep learning, the double-graph code diversion check method is used to extract code structure and sequence features using GNN and RNN, combined with attention mechanism and triple loss function, the problem of low accuracy of code diversion check in the existing technology is solved, and the Effective identification and accurate detection of complex plagiarism patterns.

CN119292658AActive Publication Date: 2025-01-10SOUTHWEST PETROLEUM UNIV

Patent Information

Application Number
CN202411821895.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-11
Publication Date
2025-01-10
Estimated Expiration
2044-12-11

AI Technical Summary

Technical Problem

Existing code plagiarism checking techniques are difficult to accurately identify subtle differences in the code structure, resulting in low accuracy of plagiarism checking, especially when facing simple rearrangement of code snippets or complex structural changes, it is difficult to correctly identify plagiarism.

Method used

Using a dual-graph code plagiarism check method based on deep learning, the structure and sequential features of the code are extracted through graph neural network (GNN) and recurrent neural network (RNN), and multi-level embedding representation is performed in combination with attention mechanism, and triplets are used. The loss function optimizes the similarity discrimination and dynamically adjusts the similarity threshold.

Benefits of technology

Improve the ability to identify complex plagiarism patterns, enhance the ability to identify implicit plagiarism and reconstruct plagiarism, improve the accuracy and robustness of plagiarism check results, reduce the misjudgment rate, and adapt to different code sets and code styles. detection effect.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119292658A_ABST
    Figure CN119292658A_ABST
Patent Text Reader

Abstract

The invention discloses a double-graph code duplicate checking method and system based on deep learning. The double-graph code duplicate checking method comprises the following steps: constructing a network graph of two sections of source codes; inputting the network diagram into the trained GNN, and after k-layer message transmission, obtaining k-layer embedded vectors of each node; calculating structure embedding vectors of the source code to be detected and the target source code; inputting the network diagram into RNN, and generating a sequential embedding representation of the last node; according to the structure embedding vector and the sequence embedding representation, fusing the structure and the sequence of the source code through an attention mechanism to obtain a global vector; calculating the similarity between the source code to be detected and the target source code according to the global vector; and judging whether the similarity is smaller than a similarity threshold, if so, determining that the repetition degree of the two sections of source codes is high, otherwise, determining that the repetition degree of the two sections of source codes is low. By adopting the technology, the scheme has the advantages of improving the comprehensiveness of plagiarism mode recognition, improving the flexibility of plagiarism detection, improving the accuracy of plagiarism detection and the like.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to code duplication checking technology, and in particular to a dual-image code duplication checking method and system based on deep learning. Background Art

[0002] Code duplication detection, also known as code clone detection or code plagiarism detection, is a process used to identify and compare similar or identical code snippets in source code files. Its purpose is to ensure the originality of the code, prevent illegal copying or misuse of the code, and maintain intellectual property rights in software development. With the widespread application of computer programming in education, industry, and research, code duplication detection technology has become an important tool for ensuring code originality and maintaining intellectual property rights. The development of code duplication detection technology is closely related to the popularization of computer programming education. In an academic environment, teachers need to ensure that the code assignments submitted by students are original, while in the industrial world, companies need to protect the code of their software products from being illegally copied or misused. In addition, with the rise of the open source movement, code duplication detection has also become an important means to maintain the health of the open source community and promote code sharing.

[0003] Code duplication detection tools typically use a variety of techniques and algorithms to identify similar or duplicate code snippets, including text comparison, which directly compares the similarity of code text; abstract syntax tree (AST) analysis, which compares based on the structure and syntax of the code; program dependency graph (PDG), which analyzes the control flow and data flow of the code to identify logical structure; and machine learning, which uses technologies such as deep learning to identify code patterns and structures to discover deeper similarities.

[0004] In the field of code duplication detection, although a variety of technologies have been used to identify similar or repeated fragments in source code, these technologies still have some significant limitations. Traditional code duplication detection systems often have difficulty capturing complex structural differences in code and slight changes in execution order. For example, a simple rearrangement of code fragments may prevent these systems from correctly identifying plagiarism. Although algorithms based on text similarity are simple and direct to operate, they are very sensitive to the format and comments of the code and are easily affected by surface text changes, which reduces the effectiveness of duplication detection. Such algorithms often ignore the execution logic information behind the code, limiting their ability to identify code fragments with similar structures but different text. Summary of the invention

[0005] In view of the above-mentioned deficiencies in the prior art, the deep learning-based dual-image code duplication checking method and system provided by the present invention solve the problem that the prior art is difficult to accurately identify subtle differences in code structure, resulting in low duplication checking accuracy.

[0006] In order to achieve the above-mentioned object of the invention, the technical solution adopted by the present invention is: In a first aspect, a dual-image code duplication checking method based on deep learning is provided, which comprises the steps of: S1. Input the source code to be detected and the target source code into the parser respectively and convert them into abstract syntax trees, and construct network graphs of the detection source code and the target source code respectively according to the abstract syntax trees; S2, assign an initial embedding vector to each node in the two network graphs, and input the initial embedding vector into the trained GNN. After k-layer message passing, the k-layer embedding vector of each node is obtained; S3, calculating the structural embedding vectors of the source code to be detected and the target source code according to the k-layer embedding vectors of all nodes corresponding to the source code to be detected and the target source code; S4, assign an initial vector to each node in the two network graphs, and input the initial vector into the trained RNN to generate the sequential embedding representation of the last node through recursive hidden layer state update; S5. According to the structural embedding vector and the sequential embedding representation, the structure and sequence of the source code are fused through the attention mechanism to obtain the global vector of the source code to be detected and the target source code; S6. Calculate the similarity between the source code to be detected and the target source code according to the global vectors of the source code to be detected and the target source code; S7. Determine whether the similarity between the source code to be detected and the target source code is less than a similarity threshold. If so, the two source code segments have a high degree of repetition; otherwise, the two source code segments have a low degree of repetition.

[0007] Furthermore, the calculation method of the similarity threshold includes: S71, selecting a source code to be detected or a target source code as an anchor point, calculating the similarity between the anchor point and all source codes in the code database, and sorting them in descending order; S72, selecting source code corresponding to a preset number of similarities ranked at the top as positive samples of anchor points, and selecting source code corresponding to a preset number of similarities ranked at the bottom as negative samples of anchor points; S73, input the anchor point, positive sample and negative sample into the neural network for training, and use the triple loss function BPR as the loss function of the neural network. After the training is completed, output the similarity threshold.

[0008] Furthermore, the expression of the triple loss function BPR is: in, is the loss value; is the similarity between the anchor point and the positive sample; is the similarity between the anchor point and the negative sample; d θis the similarity threshold; σ is the activation function, which is used to map the input value to a value between 0 and 1; λ is a hyperparameter; is the global vector of the anchor point; is the square of the L2 norm.

[0009] Furthermore, step S5 further includes: S51. According to the structural embedding vector and the sequential embedding representation, the structural weight and sequential weight of the source code to be detected and the target source code that weigh the structural information and sequential information are obtained through the attention mechanism: in, and are the structural weight and sequence weight of source code m, m=i and j respectively. When m=i, it indicates the source code to be detected, and when m=j, it indicates the target source code; is the weight matrix to be learned; For splicing operation; is the bias term; is the softmax activation function; and are the structural embedding vector and sequential embedding representation corresponding to the source code m respectively; S52. According to the structure embedding vector and the sequence embedding representation and the structure weight and the sequence weight, the structure and sequence information are weighted and fused to obtain the global vector of the source code to be detected and the target source code: in, is the global vector.

[0010] Furthermore, the expression for calculating the similarity between the source code to be detected and the target source code is: in, is the similarity between the source code to be detected and the target source code; is the global vector of the source code to be detected; is the global vector of the target source code; is the L2 norm.

[0011] Furthermore, the expression of the k-layer embedding vector of a node is: in, is the k-layer embedding vector obtained by passing node q of source code m through the k-th layer; m=i and j, when m=i, it represents the source code to be detected, and when m=j, it represents the target source code; is the k-1 layer embedding vector of node q of source code m obtained by passing it through the k-1th pass; is the k-1 layer embedding vector obtained by passing the node p directly connected to the node q of the source code m through the k-1 layer; d q,m is the degree of node q of source code m; d p,m is the degree of node p of source code m; V p is the set of nodes directly connected to node q of source code m; The expression of the structure embedding vector is: Where, T is the total number of nodes included in the source code m; is the structural embedding vector of the source code m.

[0012] Furthermore, step S4 further comprises: Assign an initial vector to each node in the network graph: Among them, X m is the initial vector set of all nodes of source code m; m=i and j, when m=i, it represents the source code to be detected, and when m=j, it represents the target source code; , and are the initial vectors of the 1st, 2nd and Tth nodes of source code m respectively; Each initial vector in the initial vector set is sequentially input into the trained RNN, and the sequential embedding representation h of the last node is generated by recursive hidden layer state update. T ; The expression of the sequential embedding representation of each node is: in, is the sequential embedding representation of the t-th node of the source code m, 1≤t≤T; is the initial vector of the tth node of source code m; is the sequential embedding representation of the t-1th node of the source code m; W h and W x are the weight matrices of RNN for hidden layer state and input respectively; b is the bias term of RNN; Relu is the activation function.

[0013] Furthermore, the method for constructing the network graph of the detection source code and the target source code is: extracting functions, classes and control structures in the abstract syntax tree as nodes, and the calling relationships between the nodes as edges, to obtain the network graph corresponding to the detection source code and the target source code.

[0014] Furthermore, the neural network is a convolutional neural network. When the neural network is trained in step S73, the maximum number of training rounds is set to 150 rounds. The parameters are updated by back propagation in each round of training. When the change in the loss value ζ for 10 consecutive rounds of training is lower than a preset value, the training of the neural network is stopped.

[0015] In a second aspect, a system for a double-image code duplication checking method based on deep learning is provided, comprising: A network graph construction module is used to input the source code to be detected and the target source code into the parser to convert them into abstract syntax trees, and to construct network graphs of the detection source code and the target source code according to the abstract syntax trees; The embedding vector generation module is used to assign an initial embedding vector to each node in the two network graphs, and input the initial embedding vector into the trained GNN. After k-layer message passing, the k-layer embedding vector of each node is obtained; A structure embedding vector generation module, used to calculate the structure embedding vectors of the source code to be detected and the target source code according to the k-layer embedding vectors of all nodes corresponding to the source code to be detected and the target source code; The sequential embedding representation generation module is used to assign an initial vector to each node in the two network graphs, input the initial vector into the trained RNN, and generate the sequential embedding representation of the last node through recursive hidden layer state update; The global vector generation module is used to fuse the structure and order of the source code through the attention mechanism according to the structural embedding vector and the sequential embedding representation, and obtain the global vector of the source code to be detected and the target source code; A similarity calculation module is used to calculate the similarity between the source code to be detected and the target source code according to the global vectors of the source code to be detected and the target source code; The repetition judgment module is used to judge whether the similarity between the source code to be detected and the target source code is less than a similarity threshold. If so, the repetition between the two source codes is high, otherwise the repetition between the two source codes is low.

[0016] Compared with the prior art, the present invention has the following beneficial effects: 1. Improve the comprehensiveness of plagiarism pattern recognition: Existing duplicate detection systems can usually only capture the surface similarity or simple sequential features of the code, and it is difficult to deal with code snippets with complex structures or unclear dependencies. This solution introduces graph neural networks (GNNs) and recursive neural networks (RNNs), namely dual graphs, to extract structural and sequential features in the code respectively and generate multi-level code representations. GNNs can capture complex structural information such as dependencies, control flows, and call relationships between nodes in the code, while RNNs can retain the execution order and logical context of the code. Through this multi-angle embedded representation, complex plagiarism patterns can be more comprehensively identified, avoiding the limitations of single feature extraction. This achieves the effective capture of complex plagiarism patterns and improves the ability to identify implicit plagiarism and reconstruction plagiarism.

[0017] 2. Adaptive fusion of features through the attention mechanism: This solution introduces the attention mechanism in the embedding fusion process of GNN and RNN to adaptively allocate the weights of structural features and sequential features. Compared with the traditional direct splicing or simple weighting method, the attention mechanism can dynamically adjust the fusion weight according to the characteristics of the input code to ensure that the focus on key features is optimized when facing different code snippets. This makes feature fusion more flexible and accurate, which helps to optimize model performance under different feature weight requirements, thereby improving the accuracy and robustness of duplicate checking results.

[0018] 3. Optimizing plagiarism discrimination based on contrastive learning of triplet loss: This solution optimizes the model through triplet loss (TripletLoss), selects "anchor point-positive sample-negative sample" triples for embedding learning, and enables the model to learn to distinguish similar and non-similar code fragments. Compared with traditional classification or distance measurement methods, triplet loss can automatically reduce the embedding distance of plagiarized code and increase the embedding distance of non-plagiarized code, thereby achieving precise similarity judgment. In addition, during the training process, the similarity threshold will be updated according to the triplet distance distribution to further improve the reliability of plagiarism judgment. This enables the model to adaptively improve the discrimination of the embedding vector by continuously optimizing the similarity distribution, making the plagiarism detection results more accurate and effectively reducing the misjudgment rate.

[0019] 4. Dynamic similarity threshold improves the flexibility of plagiarism detection: In actual plagiarism judgment, this solution uses the optimization process of triplet loss to set the dynamic similarity threshold. As the model embedding vector is optimized, the threshold will be automatically adjusted according to the training data to ensure that codes with different feature distributions can adapt to the optimal threshold. This method is more flexible than the traditional fixed threshold, and can adapt to the data distribution characteristics of different code sets, improving the model's duplication detection effect on diversified codes. The adaptive dynamic threshold setting enables this solution to ensure high detection accuracy and flexibility when processing different data sets or different code styles. BRIEF DESCRIPTION OF THE DRAWINGS

[0020] Figure 1 This is a flowchart of the dual-image code duplication checking method based on deep learning.

[0021] Figure 2 This is the principle block diagram of the dual-image code duplication checking system based on deep learning. DETAILED DESCRIPTION

[0022] The specific implementation modes of the present invention are described below so that those skilled in the art can understand the present invention. However, it should be clear that the present invention is not limited to the scope of the specific implementation modes. For those of ordinary skill in the art, as long as various changes are within the spirit and scope of the present invention as defined and determined by the attached claims, these changes are obvious, and all inventions and creations utilizing the concept of the present invention are protected.

[0023] refer to Figure 1 , Figure 1 A flowchart of a double-image code duplication checking method based on deep learning is shown; Figure 1 As shown, the method S includes steps S1 to S7.

[0024] In step S1, the source code to be tested and the target source code are respectively input into the parser and converted into an abstract syntax tree, and the network graphs of the test source code and the target source code are respectively constructed according to the abstract syntax tree; the abstract syntax tree not only captures the structure of the code, but also reveals the family relationship of the code. The target source code refers to the source code to be compared with the source code to be tested to determine whether the source code to be tested has the possibility of plagiarizing the target source code.

[0025] During implementation, this scheme preferably constructs a network graph of the detection source code and the target source code by extracting functions, classes and control structures in the abstract syntax tree as nodes, and the calling relationships between the nodes as edges, to obtain a network graph corresponding to the detection source code and the target source code.

[0026] By analyzing the relationship between elements such as functions, classes, and control structures, the system is able to grasp the high-level design and functional modules of the code. At the same time, since the same algorithm or design pattern may be implemented in a similar way in different projects, different code snippets may be similar in structure, but may have different functions if they are executed in different orders. To overcome these difficulties, this solution uses GNN to capture the structural information of the code and RNN to capture the code order information.

[0027] In step S2, an initial embedding vector is assigned to each node in the two network graphs, and the initial embedding vector is input into the trained GNN (graph neural network). After k-layer message passing, the k-layer embedding vector of each node is obtained; During implementation, the expression of the k-layer embedding vector of the preferred node of this scheme is: in, is the k-layer embedding vector obtained by passing node q of source code m through the k-th layer; m=i and j, when m=i, it represents the source code to be detected, and when m=j, it represents the target source code; is the k-1 layer embedding vector of node q of source code m obtained by passing it through the k-1th pass; is the k-1 layer embedding vector obtained by passing the node p directly connected to the node q of the source code m through the k-1 layer; d q,m is the degree of node q of source code m (the degree includes in-degree and out-degree, that is, the number of nodes adjacent to node q); d p,m is the degree of node p of source code m; V p is the set of nodes directly connected to node q of source code m.

[0028] In this scheme, in each layer of GNN, nodes receive information from their neighbor nodes, which may be syntactically adjacent nodes (such as function calls and their parameters) or nodes in data dependencies (such as variable declarations and usage). Each node updates its embedding vector representation based on the received neighbor node information and its own features. After each aggregation, the embedding vector representation of the node better reflects its relationship with the surrounding neighbor nodes; the embedding representation of each node after k layers of message passing reflects the local information of the code snippet.

[0029] In step S3, the structural embedding vectors of the source code to be detected and the target source code are calculated according to the k-layer embedding vectors of all nodes corresponding to the source code to be detected and the target source code; the expression of the structural embedding vector is: Where, T is the total number of nodes included in the source code m; is the structural embedding vector of the source code m.

[0030] In step S4, an initial vector is assigned to each node in the two network graphs, and the initial vector is input into the trained RNN (recurrent neural network), and the sequential embedding representation of the last node is generated by recursive hidden layer state update; During implementation, the preferred step S4 of this solution further includes: Assign an initial vector to each node in the network graph: Among them, X mis the initial vector set of all nodes of source code m; m=i and j, when m=i, it represents the source code to be detected, and when m=j, it represents the target source code; , and are the initial vectors of the 1st, 2nd and Tth nodes of source code m respectively; Each initial vector in the initial vector set is sequentially input into the trained RNN, and the sequential embedding representation h of the last node is generated by recursive hidden layer state update. T ; The expression of the sequential embedding representation of each node is: in, is the sequential embedding representation of the t-th node of the source code m, 1≤t≤T; is the initial vector of the tth node of source code m; is the sequential embedding representation of the t-1th node of the source code m; W h and W x are the weight matrices of RNN for hidden layer state and input respectively; b is the bias term of RNN; Relu is the activation function.

[0031] The recursive neural network of this solution can capture the changes in these sequential information and learn the temporal dependencies by gradually processing each operation or statement of the code. By capturing the sequential features in the code that do not depend on the structure, RNN can detect plagiarism that circumvents duplication detection by simply reordering code fragments or changing the position of statements.

[0032] In step S5, according to the structural embedding vector and the sequential embedding representation, the structure and sequence of the source code are fused through the attention mechanism to obtain the global vectors of the source code to be detected and the target source code; In the multi-level representation of the code, the embedding vector generated by the graph neural network (GNN) reflects the structural information of the code, while the embedding representation generated by the recurrent neural network (RNN) reflects the sequential information of the code. Through the attention mechanism, the model can selectively emphasize the more important parts of the two types of embeddings when fusing them for plagiarism detection.

[0033] In one embodiment of the present invention, step S5 further comprises: S51. According to the structural embedding vector and the sequential embedding representation, the structural weight and sequential weight of the source code to be detected and the target source code that weigh the structural information and sequential information are obtained through the attention mechanism: in, and are the structural weight and sequence weight of source code m, m=i and j respectively. When m=i, it indicates the source code to be detected, and when m=j, it indicates the target source code; is the weight matrix to be learned; For splicing operation; is the bias term; is the softmax activation function; and are the structural embedding vector and sequential embedding representation corresponding to the source code m respectively; S52. According to the structure embedding vector and the sequence embedding representation and the structure weight and the sequence weight, the structure and sequence information are weighted and fused to obtain the global vector of the source code to be detected and the target source code: in, is the global vector.

[0034] By effectively integrating the structural information captured by the graph neural network and the sequential information captured by the recurrent neural network through the attention mechanism, we can more comprehensively understand the multi-dimensional features of the code. Compared with simple vector concatenation, the attention mechanism gives the model stronger adaptability, enabling it to flexibly handle different types of plagiarism patterns and improve the accuracy of detection.

[0035] In step S6, the similarity between the source code to be detected and the target source code is calculated based on the global vectors of the source code to be detected and the target source code: in, is the similarity between the source code to be detected and the target source code; is the global vector of the source code to be detected; is the global vector of the target source code; is the L2 norm.

[0036] In step S7, it is determined whether the similarity between the source code to be detected and the target source code is less than a similarity threshold. If so, the two source code segments have a high degree of repetition; otherwise, the two source code segments have a low degree of repetition.

[0037] In one embodiment of the present invention, a method for calculating a similarity threshold includes: S71, selecting a source code to be detected or a target source code as an anchor point, calculating the similarity between the anchor point and all source codes in the code database, and sorting them in descending order; S72, selecting source code corresponding to a preset number of similarities ranked at the top as positive samples of anchor points, and selecting source code corresponding to a preset number of similarities ranked at the bottom as negative samples of anchor points; S73, input the anchor point, positive sample and negative sample into the neural network for training, and use the triple loss function BPR as the loss function of the neural network. After the training is completed, the similarity threshold is output. The expression of the triple loss function BPR is: in, is the loss value; is the similarity between the anchor point and the positive sample; is the similarity between the anchor point and the negative sample; d θ is the similarity threshold; σ is the activation function, which is used to map the input value to a value between 0 and 1; λ is a hyperparameter; is the global vector of the anchor point; is the square of the L2 norm.

[0038] The preferred neural network in this scheme is a convolutional neural network. When the neural network is trained in step S73, the maximum number of training rounds is set to 150 rounds. The parameters are updated by back propagation in each round of training. When the change in the loss value ζ after 10 consecutive rounds of training is lower than the preset value, the training of the neural network is stopped.

[0039] like Figure 2 As shown, the present solution also provides a system for a double-image code duplication checking method based on deep learning, which includes: A network graph construction module is used to input the source code to be detected and the target source code into the parser to convert them into abstract syntax trees, and to construct network graphs of the detection source code and the target source code according to the abstract syntax trees; The embedding vector generation module is used to assign an initial embedding vector to each node in the two network graphs, and input the initial embedding vector into the trained GNN. After k-layer message passing, the k-layer embedding vector of each node is obtained; A structure embedding vector generation module, used to calculate the structure embedding vectors of the source code to be detected and the target source code according to the k-layer embedding vectors of all nodes corresponding to the source code to be detected and the target source code; The sequential embedding representation generation module is used to assign an initial vector to each node in the two network graphs, input the initial vector into the trained RNN, and generate the sequential embedding representation of the last node through recursive hidden layer state update; The global vector generation module is used to fuse the structure and order of the source code through the attention mechanism according to the structural embedding vector and the sequential embedding representation, and obtain the global vector of the source code to be detected and the target source code; A similarity calculation module is used to calculate the similarity between the source code to be detected and the target source code according to the global vectors of the source code to be detected and the target source code; The repetition judgment module is used to judge whether the similarity between the source code to be detected and the target source code is less than a similarity threshold. If so, the repetition between the two source codes is high, otherwise the repetition between the two source codes is low.

[0040] In summary, this scheme solves the limitations of existing code duplication detection systems in plagiarism pattern recognition, feature adaptive fusion, similarity discrimination, etc. through multi-level code embedding representation, feature fusion of attention mechanism, contrastive learning of triplet loss, and dynamic similarity threshold, greatly improving the accuracy, flexibility and robustness of detection.

Claims

1. A dual-image code duplication checking method based on deep learning, characterized in that: Includes steps: S1. Input the source code to be detected and the target source code into the parser respectively and convert them into abstract syntax trees, and construct network graphs of the detection source code and the target source code respectively according to the abstract syntax trees; S2, assign an initial embedding vector to each node in the two network graphs, and input the initial embedding vector into the trained GNN. After k-layer message passing, the k-layer embedding vector of each node is obtained; S3, calculating the structural embedding vectors of the source code to be detected and the target source code according to the k-layer embedding vectors of all nodes corresponding to the source code to be detected and the target source code; S4, assign an initial vector to each node in the two network graphs, and input the initial vector into the trained RNN to generate the sequential embedding representation of the last node through recursive hidden layer state update; S5. According to the structural embedding vector and the sequential embedding representation, the structure and sequence of the source code are fused through the attention mechanism to obtain the global vector of the source code to be detected and the target source code; S6. Calculate the similarity between the source code to be detected and the target source code according to the global vectors of the source code to be detected and the target source code; S7. Determine whether the similarity between the source code to be detected and the target source code is less than a similarity threshold. If so, the two source code segments have a high degree of repetition; otherwise, the two source code segments have a low degree of repetition.

2. The double-image code duplication checking method based on deep learning according to claim 1 is characterized in that: The calculation method of the similarity threshold includes: S71, selecting a source code to be detected or a target source code as an anchor point, calculating the similarity between the anchor point and all source codes in the code database, and sorting them in descending order; S72, selecting source code corresponding to a preset number of similarities ranked at the top as positive samples of anchor points, and selecting source code corresponding to a preset number of similarities ranked at the bottom as negative samples of anchor points; S73, input the anchor point, positive sample and negative sample into the neural network for training, and use the triple loss function BPR as the loss function of the neural network. After the training is completed, output the similarity threshold.

3. The double-image code duplication checking method based on deep learning according to claim 2 is characterized in that: The expression of the triplet loss function BPR is: in, is the loss value; is the similarity between the anchor point and the positive sample; is the similarity between the anchor point and the negative sample; d θ is the similarity threshold; σ is the activation function, which is used to map the input value to a value between 0 and 1; λ is a hyperparameter; is the global vector of the anchor point; is the square of the L2 norm.

4. The double-image code duplication checking method based on deep learning according to claim 1 is characterized in that: Step S5 further comprises: S51. According to the structural embedding vector and the sequential embedding representation, the structural weight and sequential weight of the source code to be detected and the target source code that weigh the structural information and sequential information are obtained through the attention mechanism: in, and are the structural weight and sequence weight of source code m, m=i and j respectively. When m=i, it indicates the source code to be detected, and when m=j, it indicates the target source code; is the weight matrix to be learned; For splicing operation; is the bias term; is the softmax activation function; and are the structural embedding vector and sequential embedding representation corresponding to the source code m respectively; S52. According to the structure embedding vector and the sequence embedding representation and the structure weight and the sequence weight, the structure and sequence information are weighted and fused to obtain the global vector of the source code to be detected and the target source code: in, is the global vector.

5. The double-image code duplication checking method based on deep learning according to claim 4 is characterized in that: The expression for calculating the similarity between the source code to be detected and the target source code is: in, is the similarity between the source code to be detected and the target source code; is the global vector of the source code to be detected; is the global vector of the target source code; is the L2 norm.

6. The double-image code duplication checking method based on deep learning according to claim 1 is characterized in that: The expression of the k-layer embedding vector of a node is: in, is the k-layer embedding vector obtained by passing node q of source code m through the k-th layer; m=i and j, when m=i, it represents the source code to be detected, and when m=j, it represents the target source code; is the k-1 layer embedding vector of node q of source code m obtained by passing it through the k-1th pass; is the k-1 layer embedding vector obtained by passing the node p directly connected to the node q of the source code m through the k-1 layer; d q,m is the degree of node q of source code m; d p,m is the degree of node p of source code m; V p is the set of nodes directly connected to node q of source code m; The expression of the structure embedding vector is: Where, T is the total number of nodes included in the source code m; is the structural embedding vector of the source code m.

7. The deep learning-based double-image code duplication checking method according to claim 1, characterized in that: Step S4 further comprises: Assign an initial vector to each node in the network graph: Among them, X m is the initial vector set of all nodes of source code m; m=i and j, when m=i, it represents the source code to be detected, and when m=j, it represents the target source code; , and are the initial vectors of the 1st, 2nd and Tth nodes of source code m respectively; Each initial vector in the initial vector set is sequentially input into the trained RNN, and the sequential embedding representation h of the last node is generated by recursive hidden layer state update. T ; The expression of the sequential embedding representation of each node is: in, is the sequential embedding representation of the t-th node of the source code m, 1≤t≤T; is the initial vector of the tth node of source code m; is the sequential embedding representation of the t-1th node of the source code m; W h and W x are the weight matrices of RNN for hidden layer state and input respectively; b is the bias term of RNN; Relu is the activation function.

8. The deep learning-based double-image code duplication checking method according to claim 1, characterized in that: The method for constructing the network graph of the detection source code and the target source code is as follows: extracting functions, classes and control structures in the abstract syntax tree as nodes, and the calling relationships between the nodes as edges, to obtain the network graph corresponding to the detection source code and the target source code.

9. The double-image code duplication checking method based on deep learning according to claim 2 or 3, characterized in that: The neural network is a convolutional neural network. When the neural network is trained in step S73, the maximum number of training rounds is set to 150 rounds. The parameters are updated by back propagation in each round of training. When the change in the loss value ζ of 10 consecutive rounds of training is lower than the preset value, the training of the neural network is stopped.

10. A system applied to the deep learning-based double-image code duplication checking method according to any one of claims 1 to 9, characterized in that: include: A network graph construction module is used to input the source code to be detected and the target source code into the parser to convert them into abstract syntax trees, and to construct network graphs of the detection source code and the target source code according to the abstract syntax trees; The embedding vector generation module is used to assign an initial embedding vector to each node in the two network graphs, and input the initial embedding vector into the trained GNN. After k-layer message passing, the k-layer embedding vector of each node is obtained; A structure embedding vector generation module, used to calculate the structure embedding vectors of the source code to be detected and the target source code according to the k-layer embedding vectors of all nodes corresponding to the source code to be detected and the target source code; The sequential embedding representation generation module is used to assign an initial vector to each node in the two network graphs, input the initial vector into the trained RNN, and generate the sequential embedding representation of the last node through recursive hidden layer state update; The global vector generation module is used to fuse the structure and order of the source code through the attention mechanism according to the structural embedding vector and the sequential embedding representation, and obtain the global vector of the source code to be detected and the target source code; A similarity calculation module is used to calculate the similarity between the source code to be detected and the target source code according to the global vectors of the source code to be detected and the target source code; The repetition judgment module is used to judge whether the similarity between the source code to be detected and the target source code is less than a similarity threshold. If so, the repetition between the two source codes is high, otherwise the repetition between the two source codes is low.

Citation Information

Patent Citations

  • Cross-architecture binary function similarity detection method and system based on neural network

    CN112308210A

  • Code detection method, system and equipment based on open source component and storage medium

    CN114201406A

  • Code similarity detection method based on code attribute graph

    CN115438709A

  • Software clone detection method based on plagiarism-detector confrontation

    CN116578336A

  • Deep learning code plagiarism detection method based on multi-head attention mechanism

    CN116738963A

Cited By

  • An article plagiarism checking method for preventing paraphrased generation

    CN120429422B