The invention discloses a code similarity detection method and
system based on a large
language model, and the method comprises the steps: obtaining a to-be-detected
source code pair, and marking the to-be-detected
source code pair as a
source code A and a source code B; performing code cleaning and format
standardization on the source code A and the source code B, and mapping a variable name and a function name which are customized by a user into a uniform placeholder; analyzing the source codes A and B based on the
abstract syntax tree, respectively replacing variable names and function names in the source codes A and B with unified serialized placeholders, and maintaining a mapping table; meanwhile, expanding a lexical dictionary of the pre-training large
language model, and inserting a special identifier; splicing the replaced code snippets with special identifiers, and constructing a structure sensing input sequence; inputting the structure
perception input sequence into a pre-trained large
language model backbone network for
feature coding to obtain a high-dimensional
semantic feature vector containing global context information; connecting a multi-task prediction head behind the large language model
backbone network, inputting the
feature vector into the multi-task prediction head, outputting probability distribution of
code clone types through a classification task head, and respectively outputting a row level similarity
score and a lexical element level similarity
score through a regression task head; and according to the classification probability and the regression
score, performing comprehensive judgment by combining a preset threshold, and generating a detection report. According to the method, similar codes after variable renaming, statement rearrangement or
control flow transformation can be accurately recognized, and the accuracy and robustness of code similarity detection are improved.