Code quality evaluation method, evaluation device, storage medium and program product

By preprocessing and feature extraction of the original code data, combining machine learning models and abstract syntax trees, Word2Vec and other technologies, the problem of inaccurate code quality evaluation is solved, and the accuracy and recognition ability of evaluation are improved.

CN119292900APending Publication Date: 2025-01-10中国邮政储蓄银行股份有限公司
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411440282.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-15
Publication Date
2025-01-10

AI Technical Summary

Technical Problem

In the prior art, the code quality evaluation is inaccurate, and there are problems with low accuracy, data imbalance and complexity.

Method used

By obtaining the original code data for preprocessing and data expansion, feature vectors are extracted, and the code quality evaluation model trained through machine learning is evaluated using multiple sets of historical data, combining abstract syntax tree and Word2Vec algorithm for feature extraction, using Smote algorithm and Deep learning models perform data balance and feature extraction.

Benefits of technology

It improves the accuracy and recognition ability of code quality evaluation, solves the problem of inaccurate code quality evaluation, and realizes more detailed code structure and semantic information representation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119292900A_ABST
    Figure CN119292900A_ABST
Patent Text Reader

Abstract

The invention provides a code quality evaluation method and device, a storage medium and a program product. The method comprises the steps of obtaining original code data; preprocessing the original code data to obtain preprocessed code data, and performing data expansion processing on the preprocessed code data to obtain target code data; performing feature extraction processing on the target code data to obtain a first feature vector, and obtaining a first tag corresponding to the target code data; the to-be-evaluated code data is input into a code quality evaluation model, a second label corresponding to the to-be-evaluated code data is obtained, the code quality evaluation model is trained through machine learning by using multiple sets of historical data, and each set of data in the multiple sets of historical data comprises target code data and a first label corresponding to the target code data, the second label is used for representing a quality evaluation result of the to-be-evaluated code data. The method solves the technical problem of inaccurate code quality evaluation in the prior art.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a method for evaluating code quality, a device for evaluating code quality, a computer-readable storage medium, and a computer program product. Background Art

[0002] Current general-purpose large models can generate code, however, traditional methods may have problems such as low accuracy, data imbalance, complexity, and nonlinearity in code quality assessment.

[0003] Therefore, there is an urgent need for a method to solve the problem of inaccurate code quality assessment in the existing technology. Summary of the invention

[0004] The main purpose of the present application is to provide a code quality assessment method, a code quality assessment device, a computer-readable storage medium and a computer program product, so as to at least solve the problem of inaccurate code quality assessment in the prior art.

[0005] According to one aspect of the present application, a method for evaluating code quality is provided, comprising: obtaining original code data, wherein the original code data is generated based on a large model; preprocessing the original code data to obtain preprocessed code data, and performing data expansion processing on the preprocessed code data to obtain target code data; performing feature extraction processing on the target code data to obtain a first feature vector, and obtaining a first label corresponding to the target code data, wherein the first label is used to characterize a quality evaluation result of the target code data; inputting the code data to be evaluated into a code quality evaluation model to obtain a second label corresponding to the code data to be evaluated, wherein the code quality evaluation model is trained by machine learning using multiple groups of historical data, and each group of the multiple groups of historical data includes: the target code data and the first label corresponding to the target code data, and the second label is used to characterize the quality evaluation result of the code data to be evaluated.

[0006] Optionally, the original code data includes multiple original code fragments, and the original code data is preprocessed to obtain preprocessed code data, including: parsing each of the original code fragments to obtain multiple abstract syntax trees, the abstract syntax trees correspond one-to-one to the original code fragments, and the abstract syntax trees include multiple nodes; using the Word2Vec algorithm to encode each of the nodes to obtain multiple encoding vectors, wherein the encoding vectors correspond one-to-one to the nodes; aggregating or serializing each of the encoding vectors to obtain multiple second feature vectors, the second feature vectors correspond one-to-one to the original code fragments; and normalizing each of the second feature vectors to obtain the preprocessed code data.

[0007] Optionally, performing aggregation processing or serialization processing on each of the encoding vectors to obtain multiple second feature vectors includes: according to the formula Calculate the second eigenvector F1, where N h is the hth node, V h is the encoding vector corresponding to the hth node, · is used to represent the dot product of the vector, and n is the total number of nodes in the abstract syntax tree.

[0008] Optionally, normalizing each of the second feature vectors to obtain the preprocessed code data includes: according to the formula Calculate the mean μ of the second eigenvector, where X i is the i-th element of the second eigenvector, and m is the total number of elements in the second eigenvector; according to the formula Calculate the variance σ of the second eigenvector 2 According to the formula Calculate the preprocessed code data Among them, ∈ is a positive number close to 0.

[0009] Optionally, the preprocessed code data includes a first subset and a second subset, and the quality of any one of the preprocessed code data in the first subset is higher than the quality of any one of the preprocessed code data in the second subset. The preprocessed code data is subjected to data expansion processing to obtain target code data, including: performing oversampling processing on the preprocessed code data in the second subset using a sampling Smote algorithm to obtain oversampled data; inputting the oversampled data into a deep learning model to extract features of the oversampled data to obtain target features of the oversampled data; generating sample data based on the target features, and determining the sample data and the preprocessed code data to be the target code data.

[0010] Optionally, performing feature extraction processing on the target code data to obtain a first feature vector includes: obtaining a view corresponding to the target code data, a feature extraction function corresponding to the view, and a parameter set corresponding to the view, wherein the view includes at least one of a syntax view and a data flow view; according to formula f a =φ a (v a (c);θ a ),a=1,2,…,l, calculate the feature representation of the target code data on the ath view, where v a (c) is the ath view corresponding to the target code data c, φ a () is the feature extraction function corresponding to the a-th view, θ a is the parameter set corresponding to the ath view, l is the total number of views corresponding to the target code data; according to formula f ′ =A[F({f1,f2,…,f l}; Ψ); b], calculate the first preliminary eigenvector f ′ , where F() is the feature fusion function, Ψ is the parameter of the feature fusion function, A is the activation function, and b is the parameter of the activation function; according to the formula Calculate the second preliminary eigenvector f ″ , where w a is the first weight coefficient corresponding to the a-th first preliminary feature vector, and the first weight coefficient is used to characterize the weight of the feature extraction function; according to the formula Calculate the second weight coefficient d corresponding to the a-th view a , the second weight coefficient is used to characterize the weight of the corresponding view, where exp() is an exponential function, tanh() is a hyperbolic tangent activation function, and f j is the feature representation of the target code data in the jth view, e and g are the parameters of the fully connected layer; according to the formula The first eigenvector F2 is obtained by calculation.

[0011] Optionally, obtaining the first label corresponding to the target code data includes: obtaining the code amount of the target code data, the code vulnerabilities of the target code data, and the code logic of the target code data; determining a target set based on the code amount, the code vulnerabilities, and the code logic, wherein the target set includes a first element, a second element, and a third element, the first element is used to characterize the amount of the code, the second element is used to characterize the amount of the code vulnerabilities, and the third element is used to characterize the quality of the code logic.

[0012] According to another aspect of the present application, a code quality assessment device is provided, comprising: an acquisition unit, configured to acquire original code data, wherein the original code data is generated based on a large model; a first processing unit, configured to preprocess the original code data to obtain preprocessed code data, and perform data expansion processing on the preprocessed code data to obtain target code data; a second processing unit, configured to perform feature extraction processing on the target code data to obtain a first feature vector, and acquire a first label corresponding to the target code data, wherein the first label is used to characterize a quality assessment result of the target code data; an input unit, configured to input the code data to be evaluated into a code quality assessment model to obtain a second label corresponding to the code data to be evaluated, wherein the code quality assessment model is trained by machine learning using multiple sets of historical data, and each set of the multiple sets of historical data includes: the target code data and the first label corresponding to the target code data, and the second label is used to characterize the quality assessment result of the code data to be evaluated.

[0013] According to another aspect of the present application, a computer-readable storage medium is provided, wherein the computer-readable storage medium includes a stored program, wherein when the program is executed, the device where the computer-readable storage medium is located is controlled to execute any one of the methods described.

[0014] According to another aspect of the present application, a computer program product is provided, comprising a computer program, wherein when the computer program is executed by a processor, the steps of any one of the methods described are implemented.

[0015] The technical scheme of the present application is applied to provide a method for evaluating code quality. First, the original code data is obtained; then the original code data is preprocessed to obtain preprocessed code data, and the preprocessed code data is subjected to data expansion processing to obtain target code data; then, the target code data is subjected to feature extraction processing to obtain a first feature vector, and a first label corresponding to the target code data is obtained; finally, the code data to be evaluated is input into a code quality evaluation model to obtain a second label corresponding to the code data to be evaluated. The code quality evaluation model is trained by machine learning using multiple sets of historical data, and each set of data in the multiple sets of historical data includes: target code data and a first label corresponding to the target code data, and the second label is used to characterize the quality evaluation result of the code data to be evaluated. In this scheme, firstly, the data expansion method is adopted to balance the number of samples of different categories and improve the model's recognition ability of different quality codes. Secondly, feature extraction processing is used to extract features from the code, so as to more finely represent the code structure and semantic information, which can improve the accuracy of the evaluation and solve the technical problem of inaccurate code quality evaluation in the prior art. BRIEF DESCRIPTION OF THE DRAWINGS

[0016] The drawings constituting part of the present application are used to provide a further understanding of the present application. The exemplary embodiments and descriptions of the present application are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:

[0017] Figure 1 A hardware structure block diagram of a mobile terminal for executing a method for evaluating code quality provided in an embodiment of the present application is shown;

[0018] Figure 2 A schematic diagram of a flow chart of a method for evaluating code quality provided according to an embodiment of the present application is shown;

[0019] Figure 3 A specific flow chart of a code quality evaluation method provided according to an embodiment of the present application is shown;

[0020] Figure 4 A structural block diagram of a code quality evaluation device provided according to an embodiment of the present application is shown.

[0021] The above drawings include the following reference numerals:

[0022] 102, processor; 104, memory; 106, transmission device; 108, input and output devices. DETAILED DESCRIPTION

[0023] It should be noted that, in the absence of conflict, the embodiments and features in the embodiments of the present application can be combined with each other. The present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.

[0024] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present application.

[0025] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequential order. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present application described here. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0026] As introduced in the background technology, the code quality assessment in the prior art is inaccurate. To solve the above problem, the embodiments of the present application provide a code quality assessment method, a code quality assessment device, a computer-readable storage medium and a computer program product.

[0027] The technical solutions in the embodiments of the present invention will be described clearly and completely below in conjunction with the accompanying drawings in the embodiments of the present invention.

[0028] The method embodiments provided in the embodiments of the present application can be executed in a mobile terminal, a computer terminal or a similar computing device. Taking running on a mobile terminal as an example, Figure 1 1 is a hardware structure block diagram of a mobile terminal of a method for evaluating code quality according to an embodiment of the present invention. Figure 1 As shown, the mobile terminal may include one or more ( Figure 1 Only one is shown in the figure) a processor 102 (the processor 102 may include but is not limited to a processing device such as a microprocessor MCU or a programmable logic device FPGA) and a memory 104 for storing data, wherein the mobile terminal may also include a transmission device 106 and an input / output device 108 for communication functions. It can be understood by those skilled in the art that Figure 1 The structure shown is only for illustration and does not limit the structure of the mobile terminal. Figure 1 More or fewer components as shown, or with Figure 1 Different configurations are shown.

[0029] The memory 104 can be used to store computer programs, for example, software programs and modules of application software, such as the computer program corresponding to the code quality evaluation method in the embodiment of the present invention. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, that is, the above method is implemented. The memory 104 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include a memory remotely arranged relative to the processor 102, and these remote memories may be connected to the mobile terminal via a network. Examples of the above-mentioned networks include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and a combination thereof. The transmission device 106 is used to receive or send data via a network. The above-mentioned specific network examples may include a wireless network provided by a communication provider of the mobile terminal. In one example, the transmission device 106 includes a network adapter (Network Interface Controller, referred to as NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device 106 may be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.

[0030] In this embodiment, a method for evaluating the quality of code running on a mobile terminal, a computer terminal or a similar computing device is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in an order different from that shown here.

[0031] Figure 2 : is a flowchart of a method for evaluating code quality according to an embodiment of the present application. Figure 2 As shown, the method comprises the following steps:

[0032] Step S201, obtaining original code data, wherein the original code data is generated based on a large model;

[0033] Specifically, the general large model is used as the data source, and the generated code data is obtained using the API interface. The code data is usually stored in the form of plain text, and each record includes the following two fields: code and the corresponding generated model.

[0034] Step S202, preprocessing the original code data to obtain preprocessed code data, and performing data expansion processing on the preprocessed code data to obtain target code data;

[0035] Specifically, the above data preprocessing is to convert the original code data into a form suitable for deep learning algorithm training. Data augmentation is the process of enhancing the original code data set to improve the generalization ability of the model and prevent overfitting. For the code quality assessment task, due to the serious imbalance between the number of codes with good quality and poor quality, directly training the model may lead to insufficient recognition of poor quality code.

[0036] Step S203, performing feature extraction processing on the target code data to obtain a first feature vector, and obtaining a first label corresponding to the target code data, wherein the first label is used to characterize a quality assessment result of the target code data;

[0037] Specifically, the purpose of data feature extraction is to extract features that are highly relevant to code quality from the code to achieve more accurate code quality assessment.

[0038] Step S204: input the code data to be evaluated into a code quality evaluation model to obtain a second label corresponding to the code data to be evaluated, wherein the code quality evaluation model is trained by machine learning using multiple sets of historical data, and each set of the multiple sets of historical data includes: the target code data and the first label corresponding to the target code data, and the second label is used to characterize the quality evaluation result of the code data to be evaluated.

[0039] Specifically, the code quality assessment model may be a classifier based on a support vector machine (Support Vector Machine, SVM for short) and an improved evolutionary algorithm.

[0040] Through this embodiment, a method for evaluating code quality is provided. First, the original code data is obtained; then, the original code data is preprocessed to obtain preprocessed code data, and the preprocessed code data is data expanded to obtain target code data; then, the target code data is feature extracted to obtain a first feature vector, and the first label corresponding to the target code data is obtained; finally, the code data to be evaluated is input into the code quality evaluation model to obtain the second label corresponding to the code data to be evaluated. The code quality evaluation model is trained by machine learning using multiple sets of historical data, and each set of data in the multiple sets of historical data includes: target code data and the first label corresponding to the target code data, and the second label is used to characterize the quality evaluation result of the code data to be evaluated. In this scheme, first, the data expansion method is adopted to balance the number of samples of different categories and improve the model's recognition ability of different quality codes. Secondly, feature extraction is used to extract features from the code, so as to more finely represent the code structure and semantic information, which can improve the accuracy of the evaluation and solve the technical problem of inaccurate code quality evaluation in the prior art.

[0041] In the specific implementation process, the above original code data includes multiple original code fragments, and the above step S202 can be implemented by the following steps: step S2021, respectively parse each of the above original code fragments to obtain multiple abstract syntax trees, the above abstract syntax trees correspond to the above original code fragments one by one, and the above abstract syntax trees include multiple nodes; step S2022, use the Word2Vec algorithm to encode each of the above nodes to obtain multiple encoding vectors, wherein the above encoding vectors correspond to the above nodes one by one; step S2023, aggregate or serialize each of the above encoding vectors to obtain multiple second feature vectors, the above second feature vectors correspond to the above original code fragments one by one; step S2024, normalize each of the above second feature vectors to obtain the above preprocessed code data. The use of deep learning algorithms can further better capture the semantics and contextual information of the code, and can more accurately evaluate the code quality than traditional methods. At the same time, using technologies such as abstract syntax trees and Word2Vec to extract features from the code, so as to more finely represent the code structure and semantic information, the accuracy of the evaluation can be further improved. At the same time, processing data through techniques such as batch normalization can further improve the stability and training effect of the model, which helps to accurately evaluate the code quality.

[0042] Specifically, the Abstract Syntax Tree (AST) is a tree data structure used to represent the structure and semantics of source code. It converts various programming elements (such as expressions, statements, functions, etc.) in the source code into a tree structure, where nodes represent programming elements and edges represent the relationship between these elements. The abstract syntax tree can be divided into the following steps: split the source code string into a series of morphemes, such as keywords, identifiers, constants, operators, etc.; build an abstract syntax tree based on the morphemes. This step uses the results of lexical analysis to organize the morphemes into a tree structure according to grammatical rules. Each node represents a grammatical element, and the relationship between nodes represents the grammatical structure; based on the abstract syntax tree, check the semantic correctness of the program. This step includes type checking, scope resolution, etc. to ensure that the program is logically correct; convert the abstract syntax tree into executable code. This step may involve target code generation, intermediate code optimization, etc. Word2Vec is an unsupervised machine learning algorithm for generating word vectors, which can map words to vectors in high-dimensional space. The goal of this algorithm is to make semantically similar words close in the vector space. There are two main implementations of the Word2Vec algorithm: Continuous Bag-of-Words (CBOW) and Skip-Gram. The purpose of CBOW is to predict the context words of the current word. It trains a model so that given the context of a word (for example, the two words before and after), the model can predict the current word. Skip-Gram is the opposite of CBOW. The goal of Skip-Gram is to predict the context of the current word. Given a word, the Skip-Gram model will try to predict the context words around it.

[0043] In order to further quickly calculate the second eigenvector, the above step S2023 of the present application can be implemented by the following steps: Step S20231, according to the formula Calculate the second eigenvector F1, where N h is the hth node, V h is the encoding vector corresponding to the hth node, · is used to represent the dot product of the vector, and n is the total number of the above nodes in the above abstract syntax tree.

[0044] Specifically, the code standardization process is as follows: Assume there is a code fragment C, which contains N nodes N i By transforming the encoding vector V of each node i Perform weighted summation to obtain the feature vector representation F1 of the code snippet.

[0045] The above step S2024 can also be implemented by other methods, for example: Step S20241, according to the formula Calculate the mean μ of the second eigenvector above, where X i is the i-th element of the second eigenvector, and m is the total number of elements in the second eigenvector; Step S20242, according to the formula Calculate the variance σ of the second eigenvector above 2 ; Step S20243, according to the formula Calculate the above preprocessed code data Where ∈ is a positive number approaching 0. This method can further solve the problem of internal covariate shift in deep learning models.

[0046] In some embodiments, the above step S202 can be specifically implemented by the following steps: step S2025, the sampling Smote algorithm performs oversampling processing on the preprocessed code data in the second subset to obtain oversampled data; step S2026, the oversampled data is input into the deep learning model to extract the features of the oversampled data to obtain the target features of the oversampled data; step S2027, based on the target features, sample data is generated, and the sample data and the preprocessed code data are determined to be the target code data. Using the Smote algorithm and the data expansion method based on deep learning to oversample the code samples with poor quality can further balance the number of samples of different categories.

[0047] Specifically, for each code sample, we first use the deep learning model to extract its deep features, and then generate samples based on the deep features. The deep feature extraction process can be described by the following formula: i =DL(X i ,θ), where f i For sample X i The deep features of X are represented by DL, θ is the model parameter, and θ is the model parameter. Based on the deep features, the generation process of new samples can be described by the following formula: new =X i +α×(f j -f i ), where X new is a newly generated sample, X i and X j are two randomly selected samples, α is a randomly generated number between 0 and 1, and f i and f j For sample X i and X j Further, for each sample generation, the parameters of the deep learning model are dynamically adjusted according to the distribution of the sample. The parameter update process can be described by the following formula: t+1 =θ t -ηt *g t , where θ t and θ t+1 is the parameter of the tth and t+1th times, η t is the learning rate for the tth time, g t is the gradient of the tth time. The learning rate update process can be described by the following formula: t+1 =η t *exp(-β*l t ), where η t and η t+1 is the learning rate of the tth and t+1th time, β is the adaptive parameter, l t is the t-th loss.

[0048] The above step S203 can be specifically implemented by the following steps: Step S2031, obtaining the view corresponding to the above target code data, the feature extraction function corresponding to the above view, and the parameter set corresponding to the above view, wherein the above view includes at least one of a syntax view and a data flow view; Step S2032, according to formula f a =φ a (v a (c);θ a ),a=1,2,…,l, calculate the feature representation of the above target code data on the ath view, where v a (c) is the ath view corresponding to the target code data c, φ a () is the feature extraction function corresponding to the a-th view, θ a is the parameter set corresponding to the ath view, l is the total number of views corresponding to the above target code data; step S2033, according to formula f ′ =A[F({f1,f2,…,f l}; Ψ); b], calculate the first preliminary eigenvector f ′ , where F() is the feature fusion function, Ψ is the parameter of the feature fusion function, A is the activation function, and b is the parameter of the activation function; Step S2034, according to the formula Calculate the second preliminary eigenvector f ″ , where w a is the first weight coefficient corresponding to the a-th first preliminary feature vector, and the first weight coefficient is used to characterize the weight of the feature extraction function; step S2035, according to the formula Calculate the second weight coefficient d corresponding to the a-th view a , the above second weight coefficient is used to characterize the weight of the corresponding view, where exp() is an exponential function, tanh() is a hyperbolic tangent activation function, and f jis the feature representation of the target code data in the jth view, e and g are the parameters of the fully connected layer; step S2036, according to the formula The first feature vector F2 is calculated. This method uses a multi-branch neural network structure to extract features on different code views (e.g., syntax view, data flow view, etc.), further comprehensively capture the complexity and nonlinear characteristics of the code, and further extract more comprehensive and representative code features. At the same time, the introduction of an improved evolutionary algorithm to optimize the parameters of the neural network helps to better adapt to different types of code and quality characteristics, and further improve the performance of the model.

[0049] Specifically, the calculation method of the above activation function A can be expressed as: f(x) = a*(1 / (1+exp(-bx))), where b is the parameter of the activation function, which is used to control the curvature and slope of the function in the x-axis direction. Furthermore, a dynamic weight adjustment mechanism is used to further improve the accuracy of code quality assessment. This mechanism will dynamically adjust the weight of each branch feature based on the model training process to better adapt to various types of code and their quality characteristics. First, define the weight of each branch feature as w = {w1, w2, …, w n}. At the beginning of training, the weights of all branch features are initialized to 1, that is, w i =1,i=1,2,…,n. Then, according to the performance of the model on the validation set, the weight of each branch feature is dynamically adjusted. Specifically, if the model performs better on the features of branch i during a certain validation process, the weight of the branch is increased; conversely, if the performance is poor, the weight of the branch is reduced. Let ΔP i is the performance improvement of the model on branch i (if the performance decreases, it is a negative number), η is the learning rate, then the weight update formula for each branch feature is as follows: i =w i +η·ΔP i ,i=1,2,…,n. Finally, each branch feature is weighted and summed according to its weight to obtain the final feature representation of the code, that is, the second preliminary feature vector. In addition, the attention mechanism can be used to determine which view features are more important among multiple code view features, or in other words, in specific cases, more "attention" should be given. Let α={α1,α2,…,α n} is the attention weight, where each α i Represents the importance of the corresponding i-th view feature. α can be learned through a fully connected neural network layer with a softmax activation function. Then, the learned attention weight α is used to perform weighted summation on the multi-view features to obtain the final code feature representation, i.e., the first eigenvector.

[0050] The training process of the code quality assessment model is as follows: The code quality assessment model uses a classifier based on SVM and an improved evolutionary algorithm. SVM is a binary classification model. Its basic model is defined as a linear classifier with the largest interval in the feature space. Its learning strategy is to maximize the interval. It can be expressed as follows: Given a training sample set, (x1, y1), (x2, y2), ..., (x n ,y n ), where x i ∈R d is the eigenvector, y i ∈{+1,-1} is the corresponding category label. SVM obtains the classification hyperplane by solving the following optimization problem: The constraints are: i ((w·x i )+b)≥1-ξ i ,ξ i ≥0,i=1,2,...,n, where w is the normal vector, which determines the direction of the hyperplane; b is the intercept term, which determines the distance between the hyperplane and the origin; C>0 is a penalty parameter used to control the trade-off between maximizing the margin and ensuring that the function margin of most points is less than 1.0; ξ i is a slack variable used to deal with linear inseparable situations. In order to deal with the nonlinearity and complexity of data, the kernel function technology is introduced on the basis of SVM to map the original feature space to a higher-dimensional feature space, so that the nonlinear problem becomes linearly separable. The Gaussian radial basis function (RBF) is used as the kernel function. Specifically, the Gaussian kernel function can be expressed as: K(x i ,x j ) = exp(-γ||x i -x j || 2 ), where x i and x j are two samples, ||x i -x j || 2 is the square of the Euclidean distance between them, γ>0 is the parameter of the kernel function, which controls the complexity of the feature space after mapping. Furthermore, the improved evolutionary algorithm is used to optimize the extreme learning machine. Specifically, the improved evolutionary algorithm not only updates according to the local information of the current solution at each iteration, but also adjusts the update direction and step size according to the difference between the global optimal solution and the individual. Assuming that the current solution is x and the global optimal solution is g, the update strategy can be expressed as: new =x+α×(gx)+β×(rand(0,1)×((x best-x), where α is the learning rate, β is the random perturbation coefficient, and x best is the optimal solution of the current solution in the population, and rand(0,1) is a random number between 0 and 1. In the specific implementation, an improved evolutionary algorithm is used to optimize the parameters of SVM, including the weight vector w, the intercept term b and the penalty parameter C, so as to obtain a better classifier. Furthermore, in terms of training method, the present invention adopts an integrated learning strategy based on collaborative computing. By training multiple SVM models optimized based on the improved evolutionary algorithm, and then integrating their prediction results, the final classification decision is obtained. Specifically, assuming that M SVM models are trained, for a given sample x, the prediction result of the mth SVM model is expressed as f m (x), m = 1, 2, ..., M. The ensemble is performed by voting, that is, the final prediction result is the category with the most occurrences, which can be expressed as: Where I(·) is an indicator function, which takes the value of 1 when the condition in the brackets is met, otherwise it takes the value of 0. arg max means taking the value of y that maximizes the following expression. For binary classification problems, the average method is used for integration, that is, the final prediction result is the symbol of the average value of the prediction results of each model, which can be expressed as: Where sign(·) is a sign function, which takes the value +1 when the expression in the brackets is greater than 0, and -1 otherwise.

[0051] After the model is trained and optimized, model inference is performed to evaluate the quality of the newly input code using the trained model. Initial input: Let C be the code to be evaluated for quality. The code first undergoes a preprocessing step and is converted into a feature vector V through feature extraction. Model prediction: Input the feature vector V into the trained classifier to obtain the prediction result P. Assuming that the model is a multi-classification model, that is, each quality level is regarded as a category, then P will be a vector, and each element corresponds to the predicted probability of a category. Let P = [p1, p2, ..., p n ], where n is the number of categories, p i is the probability that code C is judged as the i-th category. Result interpretation: Based on the prediction result P, various interpretations can be performed, such as directly selecting the category with the highest probability as the predicted category, that is, predicting the quality level of code C to be arg max i p i In order to increase the interpretability of the model, the present invention proposes a probability allocation strategy based on weights. That is, the comprehensive quality score S of the code C is calculated as follows:

[0052] The above step S203 can be specifically implemented through other steps: step S2037, obtaining the code amount of the above target code data, the code loopholes of the above target code data and the code logic of the above target code data; step S2038, determining the above target set according to the above code amount, the above code loopholes and the above code logic, wherein the above target set includes a first element, a second element and a third element, the above first element is used to characterize the amount of the above code, the above second element is used to characterize the amount of the above code loopholes, and the above third element is used to characterize the quality of the above code logic. This method can further quickly obtain the above first label.

[0053] Specifically, the annotations of code quantity, code vulnerability, and code logic are used as target values ​​for subsequent model learning and are completed manually or using existing code quality assessment tools. The annotation results can be expressed as a triple (q, v, l), where q represents code quantity, v represents code vulnerability, and l represents code logic. The value range of each element is [0, 1], and the larger the value, the better the quality in that aspect.

[0054] In order to enable those skilled in the art to more clearly understand the technical solution of the present application, the implementation process of the code quality evaluation method of the present application will be described in detail below in combination with specific embodiments.

[0055] This embodiment relates to a specific code quality evaluation method, such as Figure 3 As shown, the following steps are included:

[0056] Step S1: Data collection and annotation, using the general large model as the data source, and using the API interface to obtain the generated code data. The code data is stored in the form of plain text, and each record usually includes the following two fields: code and generated model;

[0057] Step S2: Data preprocessing: Data preprocessing is to convert the original data into a form suitable for deep learning algorithm training. The collected code is modeled by deep learning algorithm, and it is used as the main step to evaluate the code quality;

[0058] Step S3: Data augmentation, using the Smote algorithm to oversample the poor quality code and generate new code samples. For each poor quality code sample, the original Smote algorithm first randomly selects a sample from its k nearest neighbors, and then randomly selects a point on the line between the two as a new sample;

[0059] Step S4: data feature extraction, using a multi-branch neural network structure, which can integrate the representation of source code in multiple different views (e.g., syntax view, data flow view, etc.);

[0060] Step S5: training the classifier, and proposing a classifier based on support vector machine and improved evolutionary algorithm;

[0061] Step S6: Model reasoning, using the trained model to evaluate the quality of the newly input code.

[0062] The embodiment of the present application also provides a device for evaluating code quality. It should be noted that the device for evaluating code quality of the embodiment of the present application can be used to perform the method for evaluating code quality provided by the embodiment of the present application. The device is used to implement the above-mentioned embodiments and preferred implementation modes, and those that have been described will not be repeated. As used below, the term "module" can implement a combination of software and / or hardware of a predetermined function. Although the device described in the following embodiments is preferably implemented in software, the implementation of hardware, or a combination of software and hardware is also possible and conceived.

[0063] The following is an introduction to the code quality evaluation device provided in an embodiment of the present application.

[0064] Figure 4 Schematic diagram of a device for evaluating code quality according to an embodiment of the present application. Figure 4 As shown, the device comprises:

[0065] An acquisition unit 10 is used to acquire original code data, wherein the original code data is generated based on a large model;

[0066] Specifically, the general large model is used as the data source, and the generated code data is obtained using the API interface. The code data is usually stored in the form of plain text, and each record includes the following two fields: code and the corresponding generated model.

[0067] The first processing unit 20 is used to preprocess the original code data to obtain preprocessed code data, and perform data expansion processing on the preprocessed code data to obtain target code data;

[0068] Specifically, the above data preprocessing is to convert the original code data into a form suitable for deep learning algorithm training. Data augmentation is the process of enhancing the original code data set to improve the generalization ability of the model and prevent overfitting. For the code quality assessment task, due to the serious imbalance between the number of codes with good quality and poor quality, directly training the model may lead to insufficient recognition of poor quality code.

[0069] The second processing unit 30 is used to perform feature extraction processing on the target code data to obtain a first feature vector and obtain a first label corresponding to the target code data, wherein the first label is used to characterize a quality assessment result of the target code data;

[0070] Specifically, the purpose of data feature extraction is to extract features that are highly relevant to code quality from the code to achieve more accurate code quality assessment.

[0071] The input unit 40 is used to input the code data to be evaluated into the code quality evaluation model to obtain a second label corresponding to the above-mentioned code data to be evaluated, wherein the above-mentioned code quality evaluation model is trained by machine learning using multiple groups of historical data, and each group of data in the multiple groups of the above-mentioned historical data includes: the above-mentioned target code data and the above-mentioned first label corresponding to the above-mentioned target code data, and the above-mentioned second label is used to characterize the quality evaluation result of the above-mentioned code data to be evaluated.

[0072] Specifically, the code quality assessment model may be a classifier based on a support vector machine (Support Vector Machine, SVM for short) and an improved evolutionary algorithm.

[0073] Through this embodiment, a code quality evaluation device is provided, wherein an acquisition unit acquires original code data; a first processing unit preprocesses the original code data to obtain preprocessed code data, and performs data expansion processing on the preprocessed code data to obtain target code data; a second processing unit performs feature extraction processing on the target code data to obtain a first feature vector, and obtains a first label corresponding to the target code data; an input unit inputs the code data to be evaluated into a code quality evaluation model to obtain a second label corresponding to the code data to be evaluated, and the code quality evaluation model is trained by machine learning using multiple sets of historical data, and each set of data in the multiple sets of historical data includes: target code data and a first label corresponding to the target code data, and the second label is used to characterize the quality evaluation result of the code data to be evaluated. In this scheme, firstly, the data expansion method is adopted to balance the number of samples of different categories and improve the model's recognition ability of different quality codes. Secondly, feature extraction processing is used to extract features from the code, so as to more finely represent the code structure and semantic information, which can improve the accuracy of the evaluation and solve the technical problem of inaccurate code quality evaluation in the prior art.

[0074] In the specific implementation process, the original code data includes multiple original code fragments, and the first processing unit includes a first processing module, a second processing module, a third processing module and a fourth processing module, wherein the first processing module is used to parse each of the original code fragments to obtain multiple abstract syntax trees, the abstract syntax trees correspond to the original code fragments one by one, and the abstract syntax trees include multiple nodes; the second processing module is used to encode each of the above nodes using the Word2Vec algorithm to obtain multiple encoding vectors, wherein the encoding vectors correspond to the above nodes one by one; the third processing module is used to aggregate or serialize each of the above encoding vectors to obtain multiple second feature vectors, the second feature vectors correspond to the original code fragments one by one; the fourth processing module is used to normalize each of the above second feature vectors to obtain the preprocessed code data. The use of deep learning algorithms can further better capture the semantics and contextual information of the code, and can more accurately evaluate the code quality than traditional methods. At the same time, the use of abstract syntax trees and Word2Vec and other technologies to extract features from the code can more carefully represent the code structure and semantic information, which can further improve the accuracy of the evaluation. At the same time, processing data through techniques such as batch normalization can further improve the stability and training effect of the model, which helps to accurately evaluate the code quality.

[0075] Specifically, the Abstract Syntax Tree (AST) is a tree data structure used to represent the structure and semantics of source code. It converts various programming elements (such as expressions, statements, functions, etc.) in the source code into a tree structure, where nodes represent programming elements and edges represent the relationship between these elements. The abstract syntax tree can be divided into the following steps: split the source code string into a series of morphemes, such as keywords, identifiers, constants, operators, etc.; build an abstract syntax tree based on the morphemes. This step uses the results of lexical analysis to organize the morphemes into a tree structure according to grammatical rules. Each node represents a grammatical element, and the relationship between nodes represents the grammatical structure; based on the abstract syntax tree, check the semantic correctness of the program. This step includes type checking, scope resolution, etc. to ensure that the program is logically correct; convert the abstract syntax tree into executable code. This step may involve target code generation, intermediate code optimization, etc. Word2Vec is an unsupervised machine learning algorithm for generating word vectors, which can map words to vectors in high-dimensional space. The goal of this algorithm is to make semantically similar words close in the vector space. There are two main implementations of the Word2Vec algorithm: Continuous Bag-of-Words (CBOW) and Skip-Gram. The purpose of CBOW is to predict the context words of the current word. It trains a model so that given the context of a word (for example, the two words before and after), the model can predict the current word. Skip-Gram is the opposite of CBOW. The goal of Skip-Gram is to predict the context of the current word. Given a word, the Skip-Gram model will try to predict the context words around it.

[0076] In order to further quickly calculate the second eigenvector, the third processing module of the present application includes a first calculation submodule for calculating the second eigenvector according to the formula Calculate the second eigenvector F1, where N h is the hth node, V h is the encoding vector corresponding to the hth node, · is used to represent the dot product of the vector, and n is the total number of the above nodes in the above abstract syntax tree.

[0077] Specifically, the code standardization process is as follows: Assume there is a code fragment C, which contains N nodes N i By transforming the encoding vector V of each node i Perform weighted summation to obtain the feature vector representation F1 of the code snippet.

[0078] The fourth processing module includes a second calculation submodule, a third calculation submodule and a fourth calculation submodule, wherein the second calculation submodule is used to calculate the Calculate the mean μ of the second eigenvector above, where X i is the i-th element of the second eigenvector, and m is the total number of elements in the second eigenvector; the third calculation submodule is used to calculate the value according to the formula Calculate the variance σ of the second eigenvector above 2 ; The fourth calculation submodule is used according to the formula Calculate the above preprocessed code data Wherein, ∈ is a positive number approaching 0. The device can further solve the internal covariate shift problem in the deep learning model.

[0079] In some embodiments, the first processing unit includes a fifth processing module, an input module, and a generation module, wherein the fifth processing module is used to sample the Smote algorithm to perform oversampling processing on the preprocessed code data in the second subset to obtain oversampled data; the input module is used to input the oversampled data into a deep learning model to extract features of the oversampled data to obtain target features of the oversampled data; the generation module is used to generate sample data based on the target features, and determine that the sample data and the preprocessed code data are the target code data. Using the Smote algorithm and the data expansion method based on deep learning to oversample poor quality code samples can further balance the number of samples of different categories.

[0080] Specifically, for each code sample, we first use the deep learning model to extract its deep features, and then generate samples based on the deep features. The deep feature extraction process can be described by the following formula: i =DL(X i ,θ), where f i For sample X i The deep features of X are represented by DL, θ is the model parameter, and θ is the model parameter. Based on the deep features, the generation process of new samples can be described by the following formula: new =X i +α×(f j -f i ), where X new is a newly generated sample, X i and X j are two randomly selected samples, α is a randomly generated number between 0 and 1, and f i and f j For sample X i and X j Further, for each sample generation, the parameters of the deep learning model are dynamically adjusted according to the distribution of the sample. The parameter update process can be described by the following formula: t+1 =θ t -ηt *g t , where θ t and θ t+1 is the parameter of the tth and t+1th times, η t is the learning rate for the tth time, g t is the gradient of the tth time. The learning rate update process can be described by the following formula: t+1 =η t ×exp(-β×l t ), where η t and η t+1 is the learning rate of the tth and t+1th time, β is the adaptive parameter, l t is the t-th loss.

[0081] The second processing unit includes a first acquisition module, a first calculation module, a second calculation module, a third calculation module, a fourth calculation module and a fifth calculation module, wherein the first acquisition module is used to acquire a view corresponding to the target code data, a feature extraction function corresponding to the view and a parameter set corresponding to the view, wherein the view includes at least one of a syntax view and a data flow view; the first calculation module is used to calculate the target code data according to the formula f a =φ a (v a (c);θ a ),a=1,2,…,l, calculate the feature representation of the above target code data on the ath view, where v a (c) is the ath view corresponding to the target code data c, φ a () is the feature extraction function corresponding to the a-th view, θ a is the parameter set corresponding to the ath view, l is the total number of views corresponding to the above target code data; the second calculation module is used to calculate the value of the target code data according to the formula f ′ =A[F({f1,f2,…,f l}; Ψ); b], calculate the first preliminary eigenvector f ′ , where F() is the feature fusion function, Ψ is the parameter of the feature fusion function, A is the activation function, and b is the parameter of the activation function; the third calculation module is used to calculate the value of the feature fusion function according to the formula Calculate the second preliminary eigenvector f ″ , where w a is the first weight coefficient corresponding to the a-th first preliminary feature vector, and the first weight coefficient is used to characterize the weight of the feature extraction function; the fourth calculation module is used to calculate the weight of the feature extraction function according to the formula Calculate the second weight coefficient d corresponding to the a-th view a, the above second weight coefficient is used to characterize the weight of the corresponding view, where exp() is an exponential function, tanh() is a hyperbolic tangent activation function, and f j is the feature representation of the above target code data in the jth view, e and g are the parameters of the fully connected layer; the fifth calculation module is used to calculate according to the formula The first feature vector F2 is calculated. The device uses a multi-branch neural network structure to extract features on different code views (e.g., syntax view, data flow view, etc.), further comprehensively capture the complexity and nonlinear characteristics of the code, and further extract more comprehensive and representative code features. At the same time, the introduction of an improved evolutionary algorithm to optimize the parameters of the neural network helps to better adapt to different types of code and quality characteristics, and further improve the performance of the model.

[0082] Specifically, the calculation method of the above activation function A can be expressed as: f(x) = a*(1 / (1+exp(-bx))), where b is the parameter of the activation function, which is used to control the curvature and slope of the function in the x-axis direction. Furthermore, a dynamic weight adjustment mechanism is used to further improve the accuracy of code quality assessment. This mechanism will dynamically adjust the weight of each branch feature based on the model training process to better adapt to various types of code and their quality characteristics. First, define the weight of each branch feature as w = {w1, w2, …, w n}. At the beginning of training, the weights of all branch features are initialized to 1, that is, w i =1,i=1,2,…,n. Then, according to the performance of the model on the validation set, the weight of each branch feature is dynamically adjusted. Specifically, if the model performs better on the features of branch i during a certain validation process, the weight of the branch is increased; conversely, if the performance is poor, the weight of the branch is reduced. Let ΔP i is the performance improvement of the model on branch i (if the performance decreases, it is a negative number), η is the learning rate, then the weight update formula for each branch feature is as follows: i =w i +η·ΔP i ,i=1,2,…,n. Finally, each branch feature is weighted and summed according to its weight to obtain the final feature representation of the code, that is, the second preliminary feature vector. In addition, the attention mechanism can be used to determine which view features are more important among multiple code view features, or in other words, in specific cases, more "attention" should be given. Let α={α1,α2,…,α n} is the attention weight, where each α iRepresents the importance of the corresponding i-th view feature. α can be learned through a fully connected neural network layer with a softmax activation function. Then, the learned attention weight α is used to perform weighted summation on the multi-view features to obtain the final code feature representation, i.e., the first eigenvector.

[0083] The training process of the code quality assessment model is as follows: The code quality assessment model uses a classifier based on SVM and an improved evolutionary algorithm. SVM is a binary classification model. Its basic model is defined as a linear classifier with the largest interval in the feature space. Its learning strategy is to maximize the interval. It can be expressed as follows: Given a training sample set, (x1, y1), (x2, y2), ..., (x n ,y n ), where x i ∈R d is the eigenvector, y i ∈{+1,-1} is the corresponding category label. SVM obtains the classification hyperplane by solving the following optimization problem: The constraints are: i ((w·x i )+b)≥1-ξ i ,ξ i ≥0,i=1,2,...,n, where w is the normal vector, which determines the direction of the hyperplane; b is the intercept term, which determines the distance between the hyperplane and the origin; C>0 is a penalty parameter used to control the trade-off between maximizing the margin and ensuring that the function margin of most points is less than 1.0; ξ i is a slack variable used to deal with linear inseparable situations. In order to deal with the nonlinearity and complexity of data, the kernel function technology is introduced on the basis of SVM to map the original feature space to a higher-dimensional feature space, so that the nonlinear problem becomes linearly separable. The Gaussian radial basis function (RBF) is used as the kernel function. Specifically, the Gaussian kernel function can be expressed as: K(x i ,x j ) = exp(-γ||x i -x j || 2 ), where x i and x j are two samples, ||x i -x j || 2is the square of the Euclidean distance between them, γ>0 is the parameter of the kernel function, which controls the complexity of the feature space after mapping. Furthermore, the improved evolutionary algorithm is used to optimize the extreme learning machine. Specifically, the improved evolutionary algorithm not only updates according to the local information of the current solution at each iteration, but also adjusts the update direction and step size according to the difference between the global optimal solution and the individual. Assuming that the current solution is x and the global optimal solution is g, the update strategy can be expressed as: new =x+α×(gx)+β×(rand(0,1)×((x best -x), where α is the learning rate, β is the random perturbation coefficient, and x best is the optimal solution of the current solution in the population, and rand(0,1) is a random number between 0 and 1. In the specific implementation, an improved evolutionary algorithm is used to optimize the parameters of SVM, including the weight vector w, the intercept term b and the penalty parameter C, so as to obtain a better classifier. Furthermore, in terms of training method, the present invention adopts an integrated learning strategy based on collaborative computing. By training multiple SVM models optimized based on the improved evolutionary algorithm, and then integrating their prediction results, the final classification decision is obtained. Specifically, assuming that M SVM models are trained, for a given sample x, the prediction result of the mth SVM model is expressed as f m (x), m = 1, 2, ..., M. The ensemble is performed by voting, that is, the final prediction result is the category with the most occurrences, which can be expressed as: Where I(·) is an indicator function, which takes the value of 1 when the condition in the brackets is met, otherwise it takes the value of 0. arg max means taking the value of y that maximizes the following expression. For binary classification problems, the average method is used for integration, that is, the final prediction result is the symbol of the average value of the prediction results of each model, which can be expressed as: Where sign(·) is a sign function, which takes the value +1 when the expression in the brackets is greater than 0, and -1 otherwise.

[0084] After the model is trained and optimized, model inference is performed to evaluate the quality of the newly input code using the trained model. Initial input: Let C be the code to be evaluated for quality. The code first undergoes a preprocessing step and is converted into a feature vector V through feature extraction. Model prediction: Input the feature vector V into the trained classifier to obtain the prediction result P. Assuming that the model is a multi-classification model, that is, each quality level is regarded as a category, then P will be a vector, and each element corresponds to the predicted probability of a category. Let P = [p1, p2, ..., p n ], where n is the number of categories, p iis the probability that code C is judged as the i-th category. Result interpretation: Based on the prediction result P, various interpretations can be performed, such as directly selecting the category with the highest probability as the predicted category, that is, predicting the quality level of code C to be arg max i p i In order to increase the interpretability of the model, the present invention proposes a probability allocation strategy based on weights. That is, the comprehensive quality score S of the code C is calculated as follows:

[0085] The second processing unit includes a second acquisition module and a determination module, wherein the second acquisition module is used to acquire the code volume of the target code data, the code loopholes of the target code data, and the code logic of the target code data; the determination module is used to determine the target set according to the code volume, the code loopholes, and the code logic, wherein the target set includes a first element, a second element, and a third element, wherein the first element is used to characterize the amount of the code volume, the second element is used to characterize the amount of the code loopholes, and the third element is used to characterize the quality of the code logic. The device can further quickly acquire the first label.

[0086] Specifically, the annotations of code quantity, code vulnerability, and code logic are used as target values ​​for subsequent model learning and are completed manually or using existing code quality assessment tools. The annotation results can be expressed as a triple (q, v, l), where q represents code quantity, v represents code vulnerability, and l represents code logic. The value range of each element is [0, 1], and the larger the value, the better the quality in that aspect.

[0087] The above-mentioned code quality evaluation device includes a processor and a memory, and the above-mentioned acquisition unit, the first processing unit, the second processing unit, and the input unit are all stored in the memory as program units, and the processor executes the above-mentioned program units stored in the memory to realize corresponding functions. The above-mentioned modules are all located in the same processor; or, the above-mentioned modules are located in different processors in any combination.

[0088] The processor contains a kernel, which calls the corresponding program unit from the memory. One or more kernels can be set, and the code quality can be evaluated by adjusting the kernel parameters.

[0089] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip.

[0090] An embodiment of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium includes a stored program, wherein when the program is executed, the device where the computer-readable storage medium is located is controlled to execute the code quality evaluation method.

[0091] Specifically, the code quality assessment methods include:

[0092] Step S201, obtaining original code data, wherein the original code data is generated based on a large model;

[0093] Specifically, the general large model is used as the data source, and the generated code data is obtained using the API interface. The code data is usually stored in the form of plain text, and each record includes the following two fields: code and the corresponding generated model.

[0094] Step S202, preprocessing the original code data to obtain preprocessed code data, and performing data expansion processing on the preprocessed code data to obtain target code data;

[0095] Specifically, the above data preprocessing is to convert the original code data into a form suitable for deep learning algorithm training. Data augmentation is the process of enhancing the original code data set to improve the generalization ability of the model and prevent overfitting. For the code quality assessment task, due to the serious imbalance between the number of codes with good quality and poor quality, directly training the model may lead to insufficient recognition of poor quality code.

[0096] Step S203, performing feature extraction processing on the target code data to obtain a first feature vector, and obtaining a first label corresponding to the target code data, wherein the first label is used to characterize a quality assessment result of the target code data;

[0097] Specifically, the purpose of data feature extraction is to extract features that are highly relevant to code quality from the code to achieve more accurate code quality assessment.

[0098] Step S204: input the code data to be evaluated into a code quality evaluation model to obtain a second label corresponding to the code data to be evaluated, wherein the code quality evaluation model is trained by machine learning using multiple sets of historical data, and each set of the multiple sets of historical data includes: the target code data and the first label corresponding to the target code data, and the second label is used to characterize the quality evaluation result of the code data to be evaluated.

[0099] Specifically, the code quality assessment model may be a classifier based on a support vector machine (Support Vector Machine, SVM for short) and an improved evolutionary algorithm.

[0100] An embodiment of the present invention provides a processor, which is used to run a program, wherein the code quality assessment method is executed when the program is running.

[0101] Specifically, the code quality assessment methods include:

[0102] Step S201, obtaining original code data, wherein the original code data is generated based on a large model;

[0103] Specifically, the general large model is used as the data source, and the generated code data is obtained using the API interface. The code data is usually stored in the form of plain text, and each record includes the following two fields: code and the corresponding generated model.

[0104] Step S202, preprocessing the original code data to obtain preprocessed code data, and performing data expansion processing on the preprocessed code data to obtain target code data;

[0105] Specifically, the above data preprocessing is to convert the original code data into a form suitable for deep learning algorithm training. Data augmentation is the process of enhancing the original code data set to improve the generalization ability of the model and prevent overfitting. For the code quality assessment task, due to the serious imbalance between the number of codes with good quality and poor quality, directly training the model may lead to insufficient recognition of poor quality code.

[0106] Step S203, performing feature extraction processing on the target code data to obtain a first feature vector, and obtaining a first label corresponding to the target code data, wherein the first label is used to characterize a quality assessment result of the target code data;

[0107] Specifically, the purpose of data feature extraction is to extract features that are highly relevant to code quality from the code to achieve more accurate code quality assessment.

[0108] Step S204: input the code data to be evaluated into a code quality evaluation model to obtain a second label corresponding to the code data to be evaluated, wherein the code quality evaluation model is trained by machine learning using multiple sets of historical data, and each set of the multiple sets of historical data includes: the target code data and the first label corresponding to the target code data, and the second label is used to characterize the quality evaluation result of the code data to be evaluated.

[0109] Specifically, the code quality assessment model may be a classifier based on a support vector machine (Support Vector Machine, SVM for short) and an improved evolutionary algorithm.

[0110] An embodiment of the present invention provides a device, the device including a processor, a memory, and a program stored in the memory and executable on the processor, and when the processor executes the program, at least the following steps are implemented:

[0111] Step S201, obtaining original code data, wherein the original code data is generated based on a large model;

[0112] Step S202, preprocessing the original code data to obtain preprocessed code data, and performing data expansion processing on the preprocessed code data to obtain target code data;

[0113] Step S203, performing feature extraction processing on the target code data to obtain a first feature vector, and obtaining a first label corresponding to the target code data, wherein the first label is used to characterize a quality assessment result of the target code data;

[0114] Step S204: input the code data to be evaluated into a code quality evaluation model to obtain a second label corresponding to the code data to be evaluated, wherein the code quality evaluation model is trained by machine learning using multiple sets of historical data, and each set of the multiple sets of historical data includes: the target code data and the first label corresponding to the target code data, and the second label is used to characterize the quality evaluation result of the code data to be evaluated.

[0115] The devices in this article can be servers, PCs, PADs, mobile phones, etc.

[0116] The present application also provides a computer program product, which, when executed on a data processing device, is suitable for executing a program for initializing at least the following method steps:

[0117] Step S201, obtaining original code data, wherein the original code data is generated based on a large model;

[0118] Step S202, preprocessing the original code data to obtain preprocessed code data, and performing data expansion processing on the preprocessed code data to obtain target code data;

[0119] Step S203, performing feature extraction processing on the target code data to obtain a first feature vector, and obtaining a first label corresponding to the target code data, wherein the first label is used to characterize a quality assessment result of the target code data;

[0120] Step S204: input the code data to be evaluated into a code quality evaluation model to obtain a second label corresponding to the code data to be evaluated, wherein the code quality evaluation model is trained by machine learning using multiple sets of historical data, and each set of the multiple sets of historical data includes: the target code data and the first label corresponding to the target code data, and the second label is used to characterize the quality evaluation result of the code data to be evaluated.

[0121] Obviously, those skilled in the art should understand that the above modules or steps of the present invention can be implemented by a general computing device, they can be concentrated on a single computing device, or distributed on a network composed of multiple computing devices, they can be implemented by a program code executable by a computing device, so that they can be stored in a storage device and executed by the computing device, and in some cases, the steps shown or described can be executed in a different order than here, or they can be made into individual integrated circuit modules, or multiple modules or steps therein can be made into a single integrated circuit module for implementation. Thus, the present invention is not limited to any specific combination of hardware and software.

[0122] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.

[0123] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0124] These computer program instructions may also be stored in a computer readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0125] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0126] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.

[0127] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.

[0128] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.

[0129] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.

[0130] From the above description, it can be seen that the above embodiments of the present application achieve the following technical effects:

[0131] 1) The code quality evaluation method of the present application, first, obtain the original code data; then pre-process the original code data to obtain the pre-processed code data, and perform data expansion processing on the pre-processed code data to obtain the target code data; then, perform feature extraction processing on the target code data to obtain the first feature vector, and obtain the first label corresponding to the target code data; finally, input the code data to be evaluated into the code quality evaluation model to obtain the second label corresponding to the code data to be evaluated, and the code quality evaluation model is trained by machine learning using multiple sets of historical data, and each set of data in the multiple sets of historical data includes: the target code data and the first label corresponding to the target code data, and the second label is used to characterize the quality evaluation result of the code data to be evaluated. In this scheme, firstly, the data expansion method is adopted to balance the number of samples of different categories and improve the model's recognition ability of different quality codes. Secondly, feature extraction processing is used to extract features from the code, so as to more finely represent the code structure and semantic information, which can improve the accuracy of the evaluation and solve the technical problem of inaccurate code quality evaluation in the prior art.

[0132] 2) The code quality evaluation device of the present application, the acquisition unit acquires the original code data; the first processing unit preprocesses the original code data to obtain the preprocessed code data, and performs data expansion processing on the preprocessed code data to obtain the target code data; the second processing unit performs feature extraction processing on the target code data to obtain the first feature vector, and obtains the first label corresponding to the target code data; the input unit inputs the code data to be evaluated into the code quality evaluation model to obtain the second label corresponding to the code data to be evaluated, and the code quality evaluation model is trained by machine learning using multiple sets of historical data, and each set of data in the multiple sets of historical data includes: the target code data and the first label corresponding to the target code data, and the second label is used to characterize the quality evaluation result of the code data to be evaluated. In this scheme, firstly, the data expansion method is adopted to balance the number of samples of different categories and improve the model's recognition ability of different quality codes. Secondly, feature extraction processing is used to extract features from the code, so as to more finely represent the code structure and semantic information, which can improve the accuracy of the evaluation and solve the technical problem of inaccurate code quality evaluation in the prior art.

[0133] The above description is only the preferred embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.

Claims

1. A method for evaluating code quality, characterized in that: include: Acquire original code data, wherein the original code data is generated based on a large model; Preprocessing the original code data to obtain preprocessed code data, and performing data expansion processing on the preprocessed code data to obtain target code data; Performing feature extraction processing on the target code data to obtain a first feature vector, and acquiring a first label corresponding to the target code data, wherein the first label is used to characterize a quality assessment result of the target code data; The code data to be evaluated is input into a code quality evaluation model to obtain a second label corresponding to the code data to be evaluated, wherein the code quality evaluation model is trained by machine learning using multiple groups of historical data, each group of the multiple groups of historical data includes: the target code data and the first label corresponding to the target code data, and the second label is used to characterize the quality evaluation result of the code data to be evaluated.

2. The method according to claim 1, characterized in that The original code data includes a plurality of original code segments, and the original code data is preprocessed to obtain preprocessed code data, including: Parsing each of the original code fragments respectively to obtain a plurality of abstract syntax trees, wherein the abstract syntax trees correspond to the original code fragments one by one and the abstract syntax trees include a plurality of nodes; Using the Word2Vec algorithm to encode each of the nodes respectively to obtain a plurality of encoding vectors, wherein the encoding vectors correspond to the nodes one by one; Aggregate or serialize the encoding vectors to obtain a plurality of second feature vectors, where the second feature vectors correspond one-to-one to the original code fragments; Normalization is performed on each of the second feature vectors to obtain the preprocessed code data.

3. The method according to claim 2, characterized in that Aggregate or serialize the encoding vectors to obtain multiple second feature vectors, including: According to the formula Calculate the second eigenvector F1, where N h is the hth node, V h is the encoding vector corresponding to the hth node, · is used to represent the dot product of the vector, and n is the total number of nodes in the abstract syntax tree.

4. The method according to claim 3, characterized in that Normalizing each of the second feature vectors to obtain the preprocessed code data includes: According to the formula Calculate the mean μ of the second eigenvector, where X i is the i-th element of the second eigenvector, and m is the total number of elements in the second eigenvector; According to the formula Calculate the variance σ of the second eigenvector 2 ; According to the formula Calculate the preprocessed code data Among them, ∈ is a positive number close to 0.

5. The method according to claim 1, characterized in that The preprocessed code data includes a first subset and a second subset, the quality of any one of the preprocessed code data in the first subset is higher than the quality of any one of the preprocessed code data in the second subset, and the preprocessed code data is subjected to data expansion processing to obtain target code data, including: The sampling Smote algorithm performs oversampling processing on the preprocessed code data in the second subset to obtain oversampled data; Inputting the oversampled data into a deep learning model to extract features of the oversampled data and obtain target features of the oversampled data; Based on the target feature, sample data is generated, and the sample data and the preprocessed code data are determined to be the target code data.

6. The method according to claim 1, characterized in that Performing feature extraction processing on the target code data to obtain a first feature vector includes: Acquire a view corresponding to the target code data, a feature extraction function corresponding to the view, and a parameter set corresponding to the view, wherein the view includes at least one of a syntax view and a data flow view; According to the formula f a =φ a (v a (c);θ a ),a=1,2,…,l, calculate the feature representation of the target code data on the ath view, where v a (c) is the ath view corresponding to the target code data c, φ a () is the feature extraction function corresponding to the a-th view, θ a is the parameter set corresponding to the ath view, and l is the total number of views corresponding to the target code data; According to the formula f ′ =A[F({f1,f2,…,f l }; Ψ); b], calculate the first preliminary eigenvector f ′ , where F() is the feature fusion function, Ψ is the parameter of the feature fusion function, A is the activation function, and b is the parameter of the activation function; According to the formula Calculate the second preliminary eigenvector f ″ , where w a is a first weight coefficient corresponding to the a-th first preliminary feature vector, wherein the first weight coefficient is used to represent the weight of the feature extraction function; According to the formula Calculate the second weight coefficient d corresponding to the a-th view a , the second weight coefficient is used to characterize the weight of the corresponding view, where exp() is an exponential function, tanh() is a hyperbolic tangent activation function, and f j is the feature representation of the target code data in the jth view, e and g are the parameters of the fully connected layer; According to the formula The first eigenvector F2 is obtained by calculation.

7. The method according to claim 1, characterized in that The first tag includes a target set, and obtaining the first tag corresponding to the target code data includes: Acquire the code amount of the target code data, the code loopholes of the target code data, and the code logic of the target code data; The target set is determined based on the amount of code, the code vulnerabilities and the code logic, wherein the target set includes a first element, a second element and a third element, the first element is used to characterize the amount of code, the second element is used to characterize the amount of code vulnerabilities, and the third element is used to characterize the quality of the code logic.

8. A device for evaluating code quality, characterized in that: include: An acquisition unit, used for acquiring original code data, wherein the original code data is generated based on a large model; A first processing unit is used to preprocess the original code data to obtain preprocessed code data, and perform data expansion processing on the preprocessed code data to obtain target code data; a second processing unit, configured to perform feature extraction processing on the target code data to obtain a first feature vector, and obtain a first label corresponding to the target code data, wherein the first label is used to characterize a quality assessment result of the target code data; An input unit is used to input the code data to be evaluated into a code quality evaluation model to obtain a second label corresponding to the code data to be evaluated, wherein the code quality evaluation model is trained by machine learning using multiple groups of historical data, and each group of the multiple groups of historical data includes: the target code data and the first label corresponding to the target code data, and the second label is used to characterize the quality evaluation result of the code data to be evaluated.

9. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored program, wherein when the program is executed, the device where the computer-readable storage medium is located is controlled to execute the method according to any one of claims 1 to 7.

10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.