A software defect prediction method, system, terminal and storage medium

By extracting the syntax tree node type sequence, semantic features and static metric features of the source code, and using feature fusion strategy, the existing deep learning models are solved in the insufficient data and imbalance of categories, and the accuracy of software defect prediction is improved.

CN118245373BActive Publication Date: 2025-02-18SHENZHEN TECH UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202410378916.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-03-29
Publication Date
2025-02-18
Estimated Expiration
2044-03-29

AI Technical Summary

Technical Problem

The existing software defect prediction model built on deep learning methods performs poorly in terms of insufficient data sample size, imbalance in data sample categories and complexity in code text context, resulting in poor prediction results.

Method used

By obtaining the source code to be predicted, the syntax tree node type sequence, semantic features and static metric features are extracted, and the feature fusion strategy is used to fuse them into spliced ​​features, and defect prediction is used to use a multi-layer perceptron and output prediction model to perform defect prediction.

Benefits of technology

The software defect prediction model's ability to identify different types of defects is improved, and more effective information is provided to improve the prediction effect.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118245373B_ABST
    Figure CN118245373B_ABST
Patent Text Reader

Abstract

The present invention discloses a software defect prediction method, model, terminal and storage medium. The method obtains source code to be predicted, extracts syntax tree node type sequence according to code text sequence; extracts semantic features according to code text sequence by a first pre-training model, and extracts syntax features according to syntax tree node type sequence by a second pre-training model; obtains static metric features of source code, generates splicing features according to semantic features, grammatical features and static metric features; outputs defect prediction results according to splicing features by a prediction module, and the prediction module includes a multi-layer perceptron and an output prediction model. The present invention extracts grammatical features of code from a source code syntax tree, and uses the splicing features of semantic features, grammatical features and static metric features of source code to predict software defects, thereby providing more effective information for the model to predict and identify defects, thereby improving the model's ability to identify different types of defects.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer science and technology, and in particular to a software defect prediction method, system, terminal and storage medium. Background Art

[0002] Software defects refer to errors, problems, or situations that do not meet specifications, design documents, and user requirements. Software defect prediction can be performed at different module levels. In software defect prediction research, researchers mainly focus on three types of tasks according to different prediction goals: classification, regression, and ranking.

[0003] Currently, there are methods for predicting software defects based on machine learning / deep learning technology. Based on the existing historical source code samples, a software defect prediction model is constructed and trained using machine learning or deep learning methods, and the defects in new code modules are predicted using the trained software defect prediction model.

[0004] Compared with the software defect prediction model based on the deep learning method, the software defect prediction model based on the traditional machine learning method can accept more dimensional, more complex and larger data as input, and can effectively extract latent semantic feature information from it, with stronger expressiveness, thus improving the performance of software defect prediction. However, the current software defect prediction model based on the deep learning method still faces difficulties such as insufficient data sample size, unbalanced data sample categories, and complex context of code text, resulting in poor model prediction effect.

[0005] Therefore, the existing technology still needs to be improved and developed. Summary of the invention

[0006] The technical problem to be solved by the present invention is to provide a software defect prediction method, system, terminal and storage medium in response to the above-mentioned defects of the prior art, aiming to solve the problem of poor prediction effect of the software defect prediction model constructed based on the deep learning method in the prior art.

[0007] The technical solution adopted by the present invention to solve the problem is as follows:

[0008] In a first aspect, an embodiment of the present invention provides a software defect prediction method, the method comprising:

[0009] Obtaining a source code to be predicted, and extracting a syntax tree node type sequence according to a code text sequence of the source code;

[0010] Extracting semantic features according to a code text sequence of the source code by using a trained first pre-trained model, and extracting grammatical features according to a syntax tree node type sequence of the source code by using a trained second pre-trained model;

[0011] Obtaining static metric features of the source code, and generating splicing features according to semantic features, grammatical features and static metric features of the source code;

[0012] Outputting the defect prediction result of the source code according to the splicing features of the source code through a prediction module, wherein the prediction module includes a trained multi-layer perceptron and an output prediction model.

[0013] In one embodiment, the training method of the first pre-trained model, the second pre-trained model, and the multi-layer perceptron includes:

[0014] Obtain source code samples in a training set, and extract syntax tree node type sequences according to code text sequences of the source code samples;

[0015] Extracting semantic features according to a code text sequence of the source code sample using the first pre-trained model, and extracting grammatical features according to a syntax tree node type sequence of the source code sample using the second pre-trained model;

[0016] Obtaining static metric features of the source code sample, and generating splicing features according to semantic features, grammatical features and static metric features of the source code sample;

[0017] Generate initial prediction output data based on the splicing features of the source code sample through a multi-layer perceptron, and calculate a model loss function according to the initial prediction output data;

[0018] Parameters of the first pre-trained model, the second pre-trained model, and the multi-layer perceptron are updated according to the model loss function.

[0019] In one embodiment, the method further comprises:

[0020] Data enhancement is performed on the training set, wherein the data enhancement method includes randomly adding standard code snippets to positive samples of the training set.

[0021] In one embodiment, a method for generating a splicing feature includes:

[0022] According to the semantic features and grammatical features, dynamic features are obtained by splicing;

[0023] The dynamic features and the static metric features are spliced ​​to obtain the spliced ​​features.

[0024] In one embodiment, the method for screening the output prediction model comprises:

[0025] The output vector of the designated layer of the multi-layer perceptron is extracted as a fusion feature, and the output prediction model is selected from a plurality of candidate machine learning models according to the fusion feature.

[0026] In one embodiment, the designated layer is the second to last layer.

[0027] In one embodiment, the step of selecting an output prediction model from a plurality of candidate machine learning models according to the fusion features includes:

[0028] Inputting the fused features into several candidate machine learning models for verification;

[0029] By comparing the optimal verification results of each candidate machine learning model, the output prediction model is screened out.

[0030] In a second aspect, an embodiment of the present invention further provides a software defect prediction system, the software defect prediction system comprising:

[0031] A data acquisition module, used to acquire source code to be predicted;

[0032] A syntax extraction module, used for extracting a syntax tree node type sequence according to a code text sequence of the source code;

[0033] A trained first pre-trained model is used to extract semantic features according to a code text sequence of the source code;

[0034] A trained second pre-trained model, used for extracting grammatical features according to a syntax tree node type sequence of the source code;

[0035] A static feature extraction module, used to obtain static metric features of the source code;

[0036] A splicing module, used to generate splicing features according to the semantic features, grammatical features and static metric features of the source code;

[0037] The prediction module includes a trained multi-layer perceptron and an output prediction model, and is used to output a defect prediction result of the source code according to the splicing features of the source code.

[0038] In a third aspect, an embodiment of the present invention further provides a terminal, comprising a memory and one or more processors; the memory stores one or more programs; the program comprises instructions for executing any software defect prediction method as described above; and the processor is used to execute the program.

[0039] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium on which a plurality of instructions are stored, wherein the instructions are suitable for being loaded and executed by a processor to implement the steps of any of the above-mentioned software defect prediction methods.

[0040] Beneficial effects of the present invention: The embodiment of the present invention obtains the source code to be predicted, extracts the syntax tree node type sequence according to the code text sequence of the source code; extracts semantic features according to the code text sequence of the source code through the trained first pre-trained model, and extracts syntax features according to the syntax tree node type sequence of the source code through the trained second pre-trained model; obtains the static metric features of the source code, generates splicing features according to the semantic features, grammatical features and static metric features of the source code; outputs the defect prediction result of the source code according to the splicing features of the source code through the prediction module, wherein the prediction module includes a trained multi-layer perceptron and an output prediction model. The present invention extracts the grammatical features of the code from the source code syntax tree based on the standardized and highly structured characteristics of the software source code syntax. And adopts a feature fusion strategy to fuse the semantic features, grammatical features and static metric features of the source code for software defect prediction, thereby providing more effective information for the model to predict and identify defects, thereby improving the model's ability to identify different types of defects. BRIEF DESCRIPTION OF THE DRAWINGS

[0041] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0042] Figure 1 It is a flowchart of a software defect prediction method provided by an embodiment of the present invention.

[0043] Figure 2 It is a schematic diagram of the training process of the software defect prediction model provided by an embodiment of the present invention.

[0044] Figure 3 It is a schematic diagram of the internal functional modules of the software defect prediction model provided by an embodiment of the present invention.

[0045] Figure 4 It is a principle block diagram of a terminal provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0046] The present invention discloses a software defect prediction method, system, terminal and storage medium. In order to make the purpose, technical solution and effect of the present invention clearer and more specific, the present invention is further described in detail with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.

[0047] It will be understood by those skilled in the art that, unless expressly stated, the singular forms "one", "said", and "the" used herein may also include plural forms. It should be further understood that the term "comprising" used in the specification of the present invention refers to the presence of the features, integers, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or groups thereof. It should be understood that when we refer to an element as being "connected" or "coupled" to another element, it may be directly connected or coupled to the other element, or there may be intermediate elements. In addition, the "connection" or "coupling" used herein may include wireless connection or wireless coupling. The term "and / or" used herein includes all or any unit and all combinations of one or more associated listed items.

[0048] It will be understood by those skilled in the art that, unless otherwise defined, all terms (including technical and scientific terms) used herein have the same meaning as generally understood by those skilled in the art in the art to which the present invention belongs. It should also be understood that terms such as those defined in general dictionaries should be understood to have meanings consistent with the meanings in the context of the prior art, and will not be interpreted with idealized or overly formal meanings unless specifically defined as herein.

[0049] In view of the above defects of the prior art, the present invention provides a method for predicting software defects, which obtains source code to be predicted, extracts a syntax tree node type sequence according to the code text sequence of the source code; extracts semantic features according to the code text sequence of the source code through a trained first pre-training model, and extracts syntax features according to the syntax tree node type sequence of the source code through a trained second pre-training model; obtains static metric features of the source code, generates splicing features according to the semantic features, grammatical features and static metric features of the source code; outputs the defect prediction result of the source code according to the splicing features of the source code through a prediction module, wherein the prediction module includes a trained multi-layer perceptron and an output prediction model. The present invention extracts the grammatical features of the code from the source code syntax tree based on the standardized and highly structured characteristics of the software source code. And adopts a feature fusion strategy to fuse the semantic features, grammatical features and static metric features of the source code for software defect prediction, thereby providing more effective information for the model to predict and identify defects, thereby improving the model's ability to identify different types of defects.

[0050] like Figure 1 As shown, the method includes:

[0051] Step S100: Obtain source code to be predicted, and extract a syntax tree node type sequence according to a code text sequence of the source code.

[0052] Specifically, the source code to be predicted in this embodiment can come from any code text that needs to be used for software defect prediction. Software source code is usually written in a high-level programming language, follows a set of clear semantic definitions and grammatical rules, and is highly structured and standardized. In order to enable the model to make full use of the grammatical information of the source code, this embodiment will extract the syntax tree node type sequence of the source code module: the code text sequence of the source code is input into a preset syntax analysis tool to obtain an abstract syntax tree, and then the node type sequence is extracted from the abstract syntax tree. Among them, the node type refers to the syntax type corresponding to the syntax tree node word, such as class, method, operator, etc. In actual application scenarios, you can select a suitable static syntax analysis tool according to the programming language used in the software source code, obtain the abstract syntax tree of the code module, and extract the syntax tree node type sequence from the abstract syntax tree.

[0053] Step S200: extracting semantic features according to the code text sequence of the source code by using the trained first pre-trained model, and extracting grammatical features according to the syntax tree node type sequence of the source code by using the trained second pre-trained model.

[0054] Specifically, after obtaining the syntax tree node type sequence, this embodiment uses a pre-trained first pre-trained model to extract the semantic features of the code text sequence, and uses a pre-trained second pre-trained model to extract the grammatical features of the syntax tree node type sequence. Further, in order to improve the accuracy and reliability of feature extraction, Figure 2 As shown in the figure, before extracting semantic features and grammatical features, the code text sequence and the syntax tree node type sequence can be tokenized and encoded according to the standard natural language processing (NLP) preprocessing process, that is, both the code text sequence and the syntax tree node type sequence need to go through the two processing steps of tokenization and encoding. The code text sequence and the syntax tree node type sequence are converted into numerical sequences through tokenization and encoding, which can facilitate subsequent model processing.

[0055] In one implementation, the first pre-trained model and the second pre-trained model can be constructed using a Transformer pre-trained model. The Transformer pre-trained model provides a corresponding tokenizer, which can be directly used to process the input data of the model.

[0056] Step S300: Obtain static metric features of the source code, and generate concatenation features according to semantic features, grammatical features and static metric features of the source code.

[0057] Specifically, static metric features refer to metric features of the code that can be obtained by statistics, including code metrics and process metrics, where code metrics include the number of lines, complexity, etc., and process metrics include development time, number of modifications, etc. Static metric features will not change with changes in model parameters. In actual application scenarios, the standardized static metric features of the current source code are obtained, and the three types of features, namely semantic features, grammatical features, and static metric features, are spliced ​​to obtain spliced ​​features, thereby providing more effective information for the model to predict and identify defects, thereby improving the model's ability to identify different types of defects. In one implementation, the three types of features, namely semantic features, grammatical features, and static metric features, can be in vector form.

[0058] Step S400: outputting a defect prediction result of the source code according to the splicing features of the source code through a prediction module, wherein the prediction module includes a trained multi-layer perceptron and an output prediction model.

[0059] Specifically, since the splicing feature combines three different features, namely semantic features, grammatical features and static metric features, it can provide more effective information for the model to predict and identify defects. Therefore, this embodiment uses splicing features to predict software defects. The steps of software defect prediction can be performed by a pre-trained multilayer perceptron (MLP) and a pre-selected output prediction model. The multilayer perceptron is used to analyze and process the splicing features, and the output prediction model is used to output the final defect prediction result. The type of information reflected by the defect prediction result can be determined based on a pre-set prediction task. For example, if the prediction task is a classification task, the defect prediction result can reflect whether the source code has defects; if the prediction task is a regression task, the defect prediction result can reflect the number of defects in the source code; if the prediction task is a sorting task, the defect prediction result can reflect the number of defects in the source code, and the predicted number of defects is sorted, so that data containing more defects can be tested first.

[0060] In one implementation, the first pre-trained model, the second pre-trained model, and the training method of the multi-layer perceptron include:

[0061] Obtain source code samples in a training set, and extract syntax tree node type sequences according to code text sequences of the source code samples;

[0062] Extracting semantic features according to a code text sequence of the source code sample using the first pre-trained model, and extracting grammatical features according to a syntax tree node type sequence of the source code sample using the second pre-trained model;

[0063] Obtaining static metric features of the source code sample, and generating splicing features according to semantic features, grammatical features and static metric features of the source code sample;

[0064] Generate initial prediction output data based on the splicing features of the source code sample through a multi-layer perceptron, and calculate a model loss function according to the initial prediction output data;

[0065] Parameters of the first pre-trained model, the second pre-trained model, and the multi-layer perceptron are updated according to the model loss function.

[0066] Specifically, the model training process is similar to the steps of the actual application process of the model, and both involve steps such as feature extraction, splicing, and prediction. The training data used in the model training process is a source code sample with a real label. Taking the data processing process of a source code sample as an example: a syntax tree node type sequence is obtained through the code text sequence of the source code sample, the semantic features of the code text sequence are extracted using the first pre-training model, and the syntax features of the syntax tree node type sequence are extracted using the second pre-training model. Further, in order to improve the accuracy and reliability of feature extraction, before extracting semantic features and grammatical features, the code text sequence and the syntax tree node type sequence can be segmented and encoded. Then the semantic features, grammatical features and static metric features are spliced, wherein the static metric features can be standardized static metric features. The spliced ​​features are input into the multi-layer perceptron to generate a preliminary prediction output, namely the initial prediction output data. The loss function (loss) of the initial prediction output data and the label is calculated, and the parameters of the multi-layer perceptron and the two pre-training models (i.e., the first pre-training model and the second pre-training model) are updated through reverse gradient propagation, thereby achieving fine-tuning of the pre-training model. This embodiment adopts a pre-trained model fine-tuning strategy to transfer the general feature representation learned from a large amount of data from other tasks to defect prediction, thereby enhancing the generalization ability of the model and alleviating the overfitting problem caused by insufficient data samples. When the Transformer model is used to build a pre-trained model, the Transformer model's self-attention mechanism can capture long-distance dependencies, which enables the prediction model to better handle the complex contextual relationships of the source code.

[0067] In one implementation, a pre-trained model based on a Transformer structure, such as the LongCoder model (a model proposed by Microsoft for code completion tasks, containing 150M parameters), may be used to construct the first pre-trained model and the second pre-trained model in this embodiment.

[0068] In another implementation, pre-trained models with different parameter scales or types may be used to construct the first pre-trained model / second pre-trained model in this embodiment, such as LSTM, CNN, or Large Language Model (LLM).

[0069] In one implementation, the method further includes:

[0070] Data enhancement is performed on the training set, wherein the data enhancement method includes randomly adding standard code snippets to positive samples of the training set.

[0071] Specifically, for actual software codes, defective modules are often far less than defect-free modules, and modules with a large number of defects are often less than modules with a small number of defects. Therefore, the training data used for software defect prediction usually has the problem of class imbalance. Taking defective modules as positive samples and defect-free modules as negative samples, the positive samples are usually far less than the negative samples. Therefore, this embodiment will first perform data preprocessing on the source code samples, and realize data enhancement by randomly adding standard code snippets to the positive samples of the training set, thereby solving the problem of sample class imbalance. Among them, the standard code snippet refers to a code snippet that will not cause errors after being added to the appropriate position of the source code. In addition to randomly adding standard code snippets to the positive samples of the training set, other different data enhancement methods can also be used in actual application scenarios.

[0072] For example, in order to achieve data enhancement, the data preprocessing is performed on the positive samples of the training set: first, a non-negative integer is randomly generated to represent the number of standard code snippets to be sampled; then, the list of standard code snippets is randomly sampled with replacement according to the number of standard code snippets, and the sampled standard code snippets are inserted into the preset position (such as the beginning) of the source code sample. Take the data enhancement of the Java project as a specific example: a standard code snippet is formed based on the core class library of Java (such as from java.io import *), and a code snippet or a combination of snippets imported from the core class library is randomly added to the beginning of the source code. In this way, the number of positive samples is effectively expanded, and the scale of the expanded samples can be controlled by adjusting the number of standard code snippets to be selected and the combination of snippets, so as to achieve the purpose of keeping the sample categories balanced.

[0073] In one implementation, a method for generating a splicing feature includes:

[0074] According to the semantic features and grammatical features, dynamic features are obtained by splicing;

[0075] The dynamic features and the static metric features are spliced ​​to obtain the spliced ​​features.

[0076] Specifically, the feature fusion strategy in this embodiment mainly involves feature fusion at two levels. The first level is the fusion of semantic features and grammatical features: the semantic features and the extracted grammatical features are fused to obtain dynamic features, and the grammatical information can be used to enhance the model's ability to recognize different types of defects. Among them, dynamic features refer to source code features extracted by a deep neural network model, and dynamic features will change dynamically due to changes in model parameters. The second level is the fusion of dynamic features and static features: the fusion of dynamic features with static metric features can provide more effective information for the model to predict and identify defects. Among them, static metric features refer to metric features of the code that can be obtained by statistics, and static metric features will not change with changes in model parameters. Furthermore, since in actual situations, the orders of magnitude between different static features of the code may vary greatly, standardization can be performed before feature fusion. This embodiment illustrates a method of standardization processing: first, the maximum value of each static feature in the training sample is counted, and then in the data preprocessing stage of all samples including the test set, each static feature is divided by its own preset multiple (for example, 1.1 times) of the maximum value of the training set, so as to constrain the numerical range of the static feature to between 0 and 1 while ensuring that the feature distribution is not changed.

[0077] In another implementation, the present embodiment may also adopt a feature fusion method other than feature splicing. For example, for multiple features that need to be fused, the feature types may be changed so that the types of different features are unified before fusion.

[0078] In one implementation, the method for screening the output prediction model includes:

[0079] The output vector of the designated layer of the multi-layer perceptron is extracted as a fusion feature, and the output prediction model is selected from a plurality of candidate machine learning models according to the fusion feature.

[0080] Specifically, this embodiment uses a traditional machine learning model as the final output prediction model. In order to screen out a suitable output prediction model, this embodiment uses the output vector of a specified layer of a multi-layer perceptron as a fusion feature, and inputs the fusion feature into different candidate machine learning models. By analyzing the prediction results of each candidate machine learning model based on the fusion feature output, the best model with the best average performance is selected as the final output prediction model. The last linear output layer of the multi-layer perceptron is replaced by the output prediction model (machine learning model). In one implementation, the number of layers of the multi-layer perceptron can be three.

[0081] In one implementation, the designated layer is the second to last layer.

[0082] Specifically, the linear output layer of the multilayer perceptron is essentially a linear regression model or a logistic regression model, so this embodiment replaces it with other machine learning models to obtain better prediction performance. In view of the huge difference in the implementation principles of the deep neural network model and the machine learning algorithm model, it is impossible to directly replace the output layer of the multilayer perceptron with a machine learning model. Therefore, this embodiment takes out the output vector of the second-to-last layer of the multilayer perceptron as a fusion feature and uses it as the input of the machine learning model, thereby screening out a suitable output prediction model to replace the linear output layer of the multilayer perceptron.

[0083] In one implementation, the step of selecting an output prediction model from a plurality of candidate machine learning models according to the fusion features includes:

[0084] Inputting the fused features into several candidate machine learning models for verification;

[0085] By comparing the optimal verification results of each candidate machine learning model, the output prediction model is screened out.

[0086] Specifically, the fusion features are input into the candidate machine learning models, and the best output prediction model is automatically selected from the candidate machine learning models by comparing the best verification results of each candidate machine learning model. For example, the model screening can be performed by cross-validation. In addition, the fusion features can also be used for the training of subsequent output prediction models.

[0087] For example, all machine learning algorithm models that need to be tried and the corresponding hyperparameter combinations to be tried are listed in advance, and several candidate machine learning models are obtained. The output vector of the specified layer (e.g., the second to last layer) of the previous multi-layer perceptron is used as the input of each candidate machine learning model, and the k-fold cross-validation of the training set is used to find the optimal hyperparameter combination of each candidate machine learning model in turn. By comparing the optimal verification results of each candidate machine learning model, the best model is automatically selected as the final output prediction model.

[0088] In another implementation, in addition to using cross-validation to screen machine learning models, other validation methods can also be used to screen machine learning models; in addition to screening machine learning models, machine learning models can also be replaced with other types of models, such as CNN and LSTM models.

[0089] The present invention focuses on the comprehensive prediction ability of the model for classification, regression and sorting tasks in the context of file-level cross-project defect prediction. Among them, file-level defect prediction refers to the data samples used in source code files; cross-project defect prediction refers to the samples used in the training set and test set containing samples from different projects. Based on the software defect prediction method, the present invention utilizes feature fusion strategies, adaptive output prediction model screening mechanisms, and positive sample code data enhancement methods and mechanisms to obtain good comprehensive defect prediction performance for classification, regression and sorting tasks.

[0090] Based on the above embodiments, the present invention also provides a software defect prediction system, such as Figure 3 As shown, the software defect prediction system includes:

[0091] A data acquisition module, used to acquire source code to be predicted;

[0092] A syntax extraction module, used for extracting a syntax tree node type sequence according to a code text sequence of the source code;

[0093] A trained first pre-trained model is used to extract semantic features according to a code text sequence of the source code;

[0094] A trained second pre-trained model, used for extracting grammatical features according to a syntax tree node type sequence of the source code;

[0095] A static feature extraction module, used to obtain static metric features of the source code;

[0096] A splicing module, used to generate splicing features according to the semantic features, grammatical features and static metric features of the source code;

[0097] The prediction module includes a trained multi-layer perceptron and an output prediction model, and is used to output a defect prediction result of the source code according to the splicing features of the source code.

[0098] In short, the software defect model used by the software defect system in this embodiment is a combination of a deep neural network model (i.e., the first pre-trained model, the second pre-trained model, and the multi-layer perceptron) and a selected machine learning model (i.e., the output prediction model). The aforementioned explanation of the software defect prediction method embodiment is also applicable to the software defect system of this embodiment, and will not be repeated here. The software defect prediction model can be evaluated using test set samples to obtain model prediction performance.

[0099] Based on the above embodiment, the present invention further provides a terminal, whose principle block diagram can be shown as follows: Figure 4As shown. The terminal includes a processor, a memory, a network interface, and a display screen connected through a system bus. Among them, the processor of the terminal is used to provide computing and control capabilities. The memory of the terminal includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the terminal is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a software defect prediction method is implemented. The display screen of the terminal can be a liquid crystal display screen or an electronic ink display screen.

[0100] Those skilled in the art will understand that Figure 4 The principle block diagram shown in the figure is only a block diagram of a partial structure related to the scheme of the present invention, and does not constitute a limitation on the terminal to which the scheme of the present invention is applied. The specific terminal may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0101] In one implementation, the memory of the terminal stores one or more programs, and is configured to be executed by one or more processors. The one or more programs include instructions for performing a software defect prediction method.

[0102] A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment method can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided by the present invention can include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).

[0103] In summary, the present invention discloses a method, system, terminal and storage medium for predicting software defects. The method obtains source code to be predicted, extracts a syntax tree node type sequence according to the code text sequence of the source code; extracts semantic features according to the code text sequence of the source code through a trained first pre-training model, and extracts syntax features according to the syntax tree node type sequence of the source code through a trained second pre-training model; obtains static metric features of the source code, generates splicing features according to the semantic features, grammatical features and static metric features of the source code; outputs the defect prediction result of the source code according to the splicing features of the source code through a prediction module, wherein the prediction module includes a trained multi-layer perceptron and an output prediction model. The present invention extracts the syntax features of the code from the source code syntax tree based on the standardized and highly structured characteristics of the software source code. And adopts a feature fusion strategy to fuse the semantic features, grammatical features and static metric features of the source code for software defect prediction, thereby providing more effective information for the model to predict and identify defects, thereby improving the model's ability to identify different types of defects.

[0104] It should be understood that the application of the present invention is not limited to the above examples. For ordinary technicians in this field, improvements or changes can be made based on the above description. All these improvements and changes should fall within the scope of protection of the claims attached to the present invention.

Claims

1. A software defect prediction method, characterized in that: The method comprises: Obtaining a source code to be predicted, and extracting a syntax tree node type sequence according to a code text sequence of the source code; the node type refers to a syntax type corresponding to a syntax tree node token; Extracting semantic features according to a code text sequence of the source code by using a trained first pre-trained model, and extracting grammatical features according to a syntax tree node type sequence of the source code by using a trained second pre-trained model; Obtain static measurement features of the source code, and generate splicing features according to semantic features, grammatical features and static measurement features of the source code; the method for generating splicing features includes: splicing according to semantic features and grammatical features to obtain dynamic features; splicing according to dynamic features and static measurement features to obtain splicing features; dynamic features refer to source code features extracted by a deep neural network model, and dynamic features will change dynamically due to changes in model parameters; static measurement features refer to measurement features of the code that can be obtained by statistics, and static measurement features will not change with changes in model parameters; Outputting a defect prediction result of the source code according to the splicing features of the source code through a prediction module, wherein the prediction module includes a trained multi-layer perceptron and an output prediction model; replacing the last linear output layer of the multi-layer perceptron with the output prediction model; The screening method of the output prediction model comprises: Extracting an output vector of a specified layer of the multi-layer perceptron as a fusion feature; the specified layer is the penultimate layer; Inputting the fused features into several candidate machine learning models for verification; By comparing the optimal verification results of each candidate machine learning model, the output prediction model is screened out.

2. The software defect prediction method according to claim 1, characterized in that: The training method of the first pre-trained model, the second pre-trained model and the multi-layer perceptron includes: Obtain source code samples in a training set, and extract syntax tree node type sequences according to code text sequences of the source code samples; Extracting semantic features according to a code text sequence of the source code sample using the first pre-trained model, and extracting grammatical features according to a syntax tree node type sequence of the source code sample using the second pre-trained model; Obtaining static metric features of the source code sample, and generating splicing features according to semantic features, grammatical features and static metric features of the source code sample; Generate initial prediction output data based on the splicing features of the source code sample through a multi-layer perceptron, and calculate a model loss function according to the initial prediction output data; Parameters of the first pre-trained model, the second pre-trained model, and the multi-layer perceptron are updated according to the model loss function.

3. The software defect prediction method according to claim 2, characterized in that: The method further comprises: Data enhancement is performed on the training set, wherein the data enhancement method includes randomly adding standard code snippets to positive samples of the training set.

4. A software defect prediction system, characterized in that: The software defect prediction system comprises: A data acquisition module, used to acquire source code to be predicted; A syntax extraction module, used to extract a syntax tree node type sequence according to a code text sequence of the source code; the node type refers to a syntax type corresponding to a syntax tree node token; A trained first pre-trained model is used to extract semantic features according to a code text sequence of the source code; A trained second pre-trained model, used for extracting grammatical features according to a syntax tree node type sequence of the source code; A static feature extraction module, used to obtain static metric features of the source code; A splicing module is used to generate splicing features according to the semantic features, grammatical features and static measurement features of the source code; the method for generating splicing features includes: splicing according to the semantic features and grammatical features to obtain dynamic features; splicing according to the dynamic features and static measurement features to obtain splicing features; dynamic features refer to source code features extracted by a deep neural network model, and dynamic features will change dynamically due to changes in model parameters; static measurement features refer to measurement features of the code that can be obtained by statistics, and static measurement features will not change with changes in model parameters; A prediction module, including a trained multi-layer perceptron and an output prediction model, is used to output a defect prediction result of the source code according to the splicing features of the source code; and the last linear output layer of the multi-layer perceptron is replaced by the output prediction model; The screening method of the output prediction model comprises: Extracting an output vector of a specified layer of the multi-layer perceptron as a fusion feature; the specified layer is the penultimate layer; Inputting the fused features into several candidate machine learning models for verification; By comparing the optimal verification results of each candidate machine learning model, the output prediction model is screened out.

5. A terminal, characterized in that: The terminal includes a memory and one or more processors; the memory stores one or more programs; the program contains instructions for executing the software defect prediction method as described in any one of claims 1-3; and the processor is used to execute the program.

6. A computer-readable storage medium having a plurality of instructions stored thereon, characterized in that: The instructions are suitable for being loaded and executed by a processor to implement the steps of the software defect prediction method described in any one of claims 1 to 3 above.

Citation Information

Patent Citations

  • Intelligent contract vulnerability detection method and system based on Tree-LSTM and BiLSTM

    CN117195220A

  • XSS attack detection method and device, computer equipment and storage medium

    CN117728995A