A code readability evaluation method, system, device and medium

By combining the combined model of the feature extraction module, the base learner module and the code feature stacking module, taking into account code text and structural features, the problems of insufficient feature extraction and low model efficiency in the existing technology are solved, and code readability evaluation is achieved. high accuracy and stability.

CN118394661BActive Publication Date: 2024-09-10TRANSFAR PAY
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410815533.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-06-24
Publication Date
2024-09-10
Estimated Expiration
2044-06-24

AI Technical Summary

Technical Problem

Existing code readability evaluation technology has problems such as insufficient feature extraction, too many deep neural network model parameters, time-consuming training, and low operating efficiency.

Method used

A code readability evaluation method is adopted to construct a readability evaluation data set by obtaining the code fragments to be evaluated and the corresponding readability labels, and a combined model of the feature extraction module, the base learner module and the code feature stacking module is used to conduct Evaluation, considers code text features and structural features, adaptively learns the weights of different features, and outputs the final readability evaluation score.

Benefits of technology

The accuracy and stability of code readability evaluation are improved, and fast and accurate code readability evaluation is achieved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118394661B
    Figure CN118394661B
Patent Text Reader

Abstract

The present invention belongs to the technical field of code readability evaluation, and discloses a code readability evaluation method, system, device and medium, including: obtaining a code fragment to be evaluated and a corresponding readability label; constructing a readability evaluation data set based on the code fragment to be evaluated and the corresponding readability label; inputting the readability evaluation data set into a code readability evaluation model for evaluation prediction to obtain a readability evaluation score; wherein the code readability evaluation model includes a feature extraction module, a base learner module and a code feature stacking module connected in sequence. The technical solution of the present invention can fully extract features and improve the accuracy and stability of code readability evaluation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of code readability evaluation, and in particular relates to a code readability evaluation method, system, device and medium. Background Art

[0002] Code is mainly composed of natural language text, including identifiers and comments, and is a special form of text. Code readability refers to the ability to make the code clear and understandable to users (including programmers, testers, and maintainers).

[0003] Code readability is closely related to software quality, such as modifiability, understandability, reusability, and maintainability. Software developers read code all the time, and the first step of every software evolution and maintenance task is to read and understand the code carefully. This step is required even if the maintainer is the author of the code. Developers spend a lot of time reading code, much more than writing it from scratch. Therefore, if the code is readable, it is easy to start changing it and it can be reused in different modules or even similar new projects. On the contrary, unreadable code affects the understanding of the program and may lead to the introduction of bugs. This is why "readable code" is basic and very necessary at any stage of software maintenance and evolution.

[0004] Currently, code readability evaluation is mainly divided into two methods: manual evaluation and automated evaluation. Manual evaluation of code readability has limitations, and the effect depends on the level and experience of the evaluator, and is not suitable for situations where a large number of code readability evaluations are required. In recent years, machine learning technology has been applied to code readability evaluation. Machine learning frameworks can combine dozens or even thousands of features, can express complex "decision spaces", and can better capture the complex interactions between many variables. By combining rich code features with various machine learning algorithm frameworks into new models, code readability evaluation can be automated.

[0005] However, the existing evaluation technology combined with machine learning has the following disadvantages:

[0006] (1) The feature extraction of some existing technologies is not comprehensive enough. Most of them only consider common code structure features and cannot fully express the code readability.

[0007] (2) Existing technologies that use deep neural networks to evaluate code readability have too many model parameters, are time-consuming to train, and have low operating efficiency. Summary of the invention

[0008] The purpose of the present invention is to provide a code readability evaluation method, system, device and medium to solve the problems existing in the above-mentioned prior art.

[0009] To achieve the above object, the present invention provides a code readability evaluation method, comprising:

[0010] Get the code snippet to be evaluated and the corresponding readability label;

[0011] Constructing a readability evaluation dataset based on the code snippets to be evaluated and the corresponding readability labels;

[0012] The readability evaluation data set is input into a code readability evaluation model for evaluation prediction to obtain a readability evaluation score; wherein the code readability evaluation model includes a feature extraction module, a base learner module and a code feature stacking module connected in sequence.

[0013] Optionally, the training process of the code readability evaluation model includes:

[0014] Acquiring training data, wherein the training data includes a readability evaluation data training set and a corresponding readability evaluation score;

[0015] An initial code readability evaluation model is constructed, the training data is input into the initial code readability evaluation model for evaluation prediction, and training is performed with the goal of minimizing the loss between the initial training result after evaluation prediction and the readability evaluation score corresponding to the readability evaluation data training set, so as to obtain the code readability evaluation model.

[0016] Optionally, the processing of the code readability evaluation model includes:

[0017] Inputting the readability evaluation data set into the feature extraction module to extract features and obtain code feature data; wherein the code feature data includes text feature data and structure feature data;

[0018] Performing standardization processing on the text feature data and the structural feature data;

[0019] The standardized feature data are respectively input into the base learner module for feature learning and prediction, and the predicted output data corresponding to each feature data is obtained;

[0020] The predicted output data is input as input feature data into the code feature stacking module, and the code feature stacking module adaptively learns the weights of different input feature data and outputs a readability evaluation score.

[0021] Optionally, the text feature data includes comments and identifier consistency feature AIC, identifier integrity feature II, identifier narrow sense feature IN, identifier ambiguity feature IA and comment readability feature CR of the code snippet to be evaluated.

[0022] Optionally, the structural feature data includes a blank line ratio feature BLR, a bracket number feature BP, an identifier length feature IL, a code volume feature CV and a character entropy feature CE of the code fragment to be evaluated.

[0023] A code readability evaluation system, comprising:

[0024] A data collection module is used to obtain code snippets to be evaluated and corresponding readability labels; and to construct a readability evaluation data set based on the code snippets to be evaluated and the corresponding readability labels;

[0025] A code readability evaluation module is used to input the readability evaluation data set into a code readability evaluation model for evaluation prediction to obtain a readability evaluation score; wherein the code readability evaluation model includes a feature extraction module, a base learner module and a code feature stacking module connected in sequence.

[0026] An electronic device comprises a memory and a processor, wherein the memory is used to store a computer program, and the processor runs the computer program to enable the electronic device to execute a code readability evaluation method.

[0027] A computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method for evaluating code readability is implemented.

[0028] The technical effects of the present invention are:

[0029] The present invention considers both code text features and code structure features, and constructs a code feature stacking module by combining base learners that learn different code features. Two base learners that learn different code features and a feature stacking module are constructed, and the predicted outputs of the base learners that learn different code features are input into a code feature stacking module as new features. The code feature stacking module can adaptively learn the weights of different code features and output the final readability evaluation score. This method can improve the accuracy and stability of code readability evaluation. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0031] The drawings constituting a part of the present application are used to provide a further understanding of the present application. The illustrative 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:

[0032] Figure 1 A schematic diagram of an algorithm flow for evaluating code readability in an embodiment of the present invention;

[0033] Figure 2 is a schematic diagram of a hypernym tree of result in an embodiment of the present invention;

[0034] Figure 3 The present invention is a flowchart of the implementation of code readability evaluation in an embodiment of the present invention. DETAILED DESCRIPTION

[0035] Various exemplary embodiments of the present invention will now be described in detail. This detailed description should not be considered as limiting the present invention, but should be understood as a more detailed description of certain aspects, features, and embodiments of the present invention.

[0036] It should be understood that the terms described in the present invention are only for describing special embodiments and are not intended to limit the present invention. In addition, for the numerical range in the present invention, it should be understood that each intermediate value between the upper and lower limits of the scope is also specifically disclosed. Each smaller range between the intermediate value in any stated value or stated range and any other stated value or intermediate value in the described range is also included in the present invention. The upper and lower limits of these smaller ranges can be independently included or excluded in the scope.

[0037] Unless otherwise indicated, all technical and scientific terms used herein have the same meanings as those of ordinary skill in the art generally understood by the present invention. Although the present invention describes only preferred methods, any method similar or equivalent to that described herein may also be used in the implementation or testing of the present invention. All documents mentioned in this specification are incorporated by reference to disclose and describe the methods related to the documents. In the event of a conflict with any incorporated document, the content of this specification shall prevail.

[0038] It will be apparent to those skilled in the art that various modifications and variations may be made to the specific embodiments of the present invention description without departing from the scope or spirit of the present invention. Other embodiments derived from the present invention description will be apparent to those skilled in the art. The present application description and examples are exemplary only.

[0039] The words “include,” “including,” “have,” “contain,” etc. used in this article are open-ended terms, meaning including but not limited to.

[0040] 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.

[0041] Embodiment 1

[0042] like Figure 1 - Figure 3 As shown, this embodiment provides a code readability evaluation method, including: obtaining a code snippet to be evaluated and a corresponding readability label; constructing a readability evaluation data set based on the code snippet to be evaluated and the corresponding readability label; inputting the readability evaluation data set into a code readability evaluation model for evaluation prediction to obtain a readability evaluation score; wherein the code readability evaluation model includes a feature extraction module, a base learner module and a code feature stacking module connected in sequence.

[0043] The technical problem solved by this embodiment is how to automatically and quickly complete the code readability evaluation and improve the efficiency and accuracy of the code readability evaluation. In this embodiment, the input code snippet first passes through the code feature extraction module in the model, and the text features and structural features are respectively input into the corresponding base learners, and then the predicted outputs of the two base learners for the corresponding code features are input into the code feature stacking module. After the module stacks the text features and structural features, it outputs the final readability evaluation score. In practical applications, this embodiment can quickly and accurately complete the code readability evaluation.

[0044] This embodiment discloses a code readability evaluation method, which is based on a code readability evaluation model (CREM) and utilizes the idea of ​​Feature Stacking in ensemble learning. The prediction outputs of base learners that combine and learn different code features are used as new features and input into a feature stacking module, thereby increasing the prediction accuracy and improving the robustness. The method includes code readability evaluation data set construction, a code feature extraction module, a base learner module, a code feature stacking module, and an application model for code readability evaluation.

[0045] This embodiment defines a set of code text features, including annotation and identifier consistency feature AIC, identifier integrity feature II, identifier narrow sense feature IN, identifier ambiguity feature IA, and annotation readability feature CR. At the same time, a set of code structure features are defined, including blank line ratio feature BLR, bracket number feature NP, identifier length feature IL, code volume feature CV, and character entropy feature CE.

[0046] In the code feature extraction stage, this embodiment performs CFU standardization on the code feature vector to avoid the impact of dimensional differences between code features on the model. This embodiment defines a code feature mapping function that can map the input code features to a higher-dimensional feature space, thereby making the data easier to be segmented by the base learner.

[0047] Through text feature extraction and structural feature extraction, the text features include not only individual identifiers and annotation features, but also their consistency features, and the structural features include features from character level to code snippet level, which can fully describe the code readability. Two base learners that learn different code features and a feature stacking module are constructed, and the predicted output of the base learners that learn different code features is input as a new feature into a code feature stacking module. The code feature stacking module can adaptively learn the weights of different code features and output the final readability evaluation score. This method can improve the accuracy and stability of code readability evaluation.

[0048] The technical solution flow chart of this embodiment is as follows Figure 1 shown.

[0049] The specific implementation process includes: Step 1: Construct a code readability evaluation dataset: Construct a code readability evaluation dataset, or use an existing public code readability evaluation dataset. Each data in the code readability evaluation dataset consists of a code snippet and a readability label. Indicates that is a code snippet, It is a code snippet The corresponding readability label. The value of is ,The larger the value, the higher the readability of the corresponding code snippet.

[0050] This example extracts code snippets of method implementation from the open source projects SpringBoot and Junit. The code length is within 50 lines (including comments). Then invite 10 computer science college students to read and evaluate each code snippet. A value is selected as the code readability label value. Finally, for each code snippet, the average value of 10 evaluations is taken as the final readability label value, thereby forming the code readability evaluation data set of this embodiment.

[0051] Step 2: Construct a code feature extraction module: The code feature extraction module performs text feature extraction and structural feature extraction on all code snippets in the data set to construct the corresponding base learner.

[0052] Step 2.1 Code text feature extraction: Extract 5 types of text features from the code: AIC (Annotation Identifier Consistency), II (Identifier Integrity), IN (Identifier Narrowness), IA (Identifier Ambiguity), and CR (Comment Readability). The specific steps are described as follows.

[0053] Step 2.1.1: Extract the annotation and identifier consistency feature AIC, which includes: firstly, process the code text and calculate the word set used in the annotation in the code snippet S and the set of words used by the identifier , and then calculate the value of AIC according to formula (1.1), and also consider Synonym set of the word (In the actual experiment, the synsets() function in WordNet is used to calculate the synonym set), because if and If there are two synonyms in the word (e.g. finish and done), they should be considered identical. In this case, the AIC is calculated as shown in formula (1.2), where The operation is to calculate the number of elements in the set, and the AIC value ranges from 0 to 1.

[0054] (1.1)

[0055] (1.2)

[0056] Identifiers refer to the names of variables, classes, and methods in the code. Comments are explanations and descriptions of the code content. The comment formats of different languages ​​are different. In Java, / / represents line comments, and / * * / represents block comments, that is, multiple lines are commented on at one time. There are many tools available for synonym calculation. This embodiment uses WordNet in NLTK to calculate synonyms. WordNet is a large vocabulary database. Nouns, verbs, adjectives, and adverbs are stored in this database in the form of synonym sets.

[0057] Calculate the AIC of the code snippet. Since the calculation of synonyms is complicated, this example calculates AIC according to formula (1.1). Assume that after the code snippet S is processed by the code text, the word set used in the comment in the code snippet is obtained. for , and the set of words used by identifiers for , we can get for ,Right now is 5, for ,Right now is 13, and finally according to formula (1.1) we get .

[0058] The above code text processing process includes:

[0059] (1) Remove non-text tokens, such as operators, special symbols, and programming language keywords (excluding comment characters, which are used to mark the location of comment lines).

[0060] (2) Split words by underscore or camelCase separators, for example, runTest is split into run and Test.

[0061] (3) Remove words from the stop word list, such as articles (a, an, the) and adverbs (when, where, why, how).

[0062] (4) Extract word stems from words, such as the word stem inline of inlined, the word stem limit of limitation, and the word stem finish of Finished.

[0063] In this embodiment, the stop word list is obtained through NLTK, and the Porter stem extractor in NLTK is used to extract stems. NLTK is a natural language processing toolkit and the most commonly used Python library in the field of NLP. It can be used to process corpora, classify texts, analyze language structures, etc.

[0064] How to get the stop word list: Import the stop word library from nltk.corpus and get the English stop word list;

[0065] Method for extracting stems: Import the Porter stemmer and extract stems using the stem method of the Porter stemmer.

[0066] Step 2.1.2: Extract identifier integrity feature II. The complete identifier index is more readable than its abbreviations idx and i, and the semantically complete identifier should appear in the dictionary. First, perform the code text processing in the same way as in Step 2.1.1, and then calculate II according to formula (1.3), where is a dictionary, in this case the English dictionary, express The number of times the word in appears in the English dictionary, with II values ​​ranging from 0 to 1.

[0067] (1.3)

[0068] Calculate the II of the code according to formula (1.3). Assume that the code snippet S is processed as in Step 2.1.1 to obtain the word set used by the identifier in the code snippet for ,Right now is 8, then we can get for ,Right now is 7, and finally according to formula (1.3) we get .

[0069] Step 2.1.3: Extract the identifier narrowness feature IN. The identifier in the readable code should be more narrow, that is, the hypernym tree of the words used by the identifier has a greater depth, and the words used by the identifier have more specific meanings and are easier to understand. First, perform the code text processing in the same way as in Step 2.1.1. , both use formula (1.4) to calculate the narrow characteristics of the identifier of the code line , and then calculate the average value to obtain the identifier narrowness feature of code snippet S , where It means calculation The depth of the hypernym tree (in the actual experiment, the hypernym_paths() function in WordNet is used to calculate the depth of the hypernym tree. For a given word, the hypernym_paths() function returns the hypernym path sequence of the word, and the depth of the hypernym tree is obtained by calculating the number of elements in the returned sequence), is the line of code The identifier in Figure 2 This is an example of a hypernym tree for "result".

[0070] (1.4)

[0071] Calculate the IN of the code snippet according to formula (1.4). Assume that after the code snippet S is processed in the same way as in Step 2.1.1, the code lines containing identifiers in the code snippet are 2, 3, 5, 9, 11, and 15 respectively. The corresponding , , , , , , then according to formula (1.4) we can get .

[0072] Step 2.1.4: Extract identifier polysemy feature IA. The more meanings of identifiers in the code, the more difficult it is to understand. First, perform the same code text processing as in Step 2.1.1. , both use formula (4.5) to calculate the identifier ambiguity feature of the code line , and then calculate the average value to obtain the identifier polysemy feature of code snippet S , where It means calculation The number of meanings, is the line of code The identifier in .

[0073] (1.5)

[0074] Calculate the IA of the code according to formula (1.5). Assume that after the code fragment S is processed in the same way as in Step 2.1.1, the code lines containing identifiers in the code fragment are 2, 3, 5, 9, 11, and 15 respectively. , , , , , , then according to formula (1.5) we can get .

[0075] Step 2.1.5: Extract the annotation readability feature CR. The formula is shown in (4.6), where ASL is the average sentence length, which is equal to the number of words divided by the number of sentences, and ASW is the average number of syllables per word, which is equal to the number of syllables divided by the number of words. is a parameter that can be adjusted according to the actual situation. When calculating specifically, extract the comments in the code snippet, use a period to concatenate each line of comments into a paragraph of text, and calculate the paragraph of text according to formula (4.6) Value as a comment readability feature of code snippet S .

[0076] (4.6)

[0077] Calculate the CR of the code according to formula (1.6), let the parameter For code snippet S, assuming that each line of comment is concatenated into a text with a period, the number of words is 12, the number of sentences is 2, and the number of syllables is 24, then we can get , Finally, according to formula (1.6), we get .

[0078] Step 2.1.6: Use the five text features extracted from Step 2.1.1 to Step 2.1.5 as the text feature vector of each code snippet S , , and Perform CFU (Code Feature Unification) standardization. Specifically, first perform CFU standardization on the text feature vector. Each dimension of conduct The changes in , is the eigenvalue The mean of is the variance, then conduct The changes obtained ,make is the final eigenvalue.

[0079] Since the scales of different code features may vary greatly, if the code features are not CFU-standardized, some code features may have too much influence on the model, causing model deviation. Through standardization, the value ranges of different code features can be adjusted to similar scales, avoiding the impact of dimensional differences between code features on the model.

[0080] Step 2.2 Code structure feature extraction: Extract 5 structural features from the code: BLR (Blank Line Ratio), NP (Number of Parentheses), IL (Identifier Length), CV (Code Volume) and CE (Character Entropy). The specific steps are described as follows.

[0081] Step 2.2.1: Extract the blank line ratio feature BLR. Blank lines can increase the clarity of the code and separate the variable declaration and operation function. Calculate BLR according to formula (1.7), where is the total number of lines in the code snippet S, The number of blank lines.

[0082] (1.7)

[0083] Step 2.2.2: Extract the number of brackets feature NP. Brackets ensure that the code is logically and grammatically correct, which can improve the readability of the code. Equal to the number of curly braces "{" and "}" in code snippet S.

[0084] Step 2.2.3: Extract the identifier length feature IL. First, remove non-text tags and words in the stop word list to get the identifier set ,calculate The total length of the identifiers in the code snippet S is calculated, and the average value is calculated to obtain the identifier length feature of the code snippet S. , where calculate Length, is a set of identifiers The identifier in .

[0085] (4.8)

[0086] According to formula (1.8), the IL of the code is calculated. Assume that after the code snippet S removes non-text tags and words in the stop word list, the identifier set of the code snippet is obtained: for ,Right now is 4, then we can get Finally, according to formula (1.8), we get .

[0087] Step 2.2.4: Extract code volume feature CV. The formula is shown in (1.9), where is the hyperbolic tangent function, which is defined as shown in formula (1.10), is the total number of operators, is the total number of operations, is the number of operators, There are several types of operands, including reserved words, function calls, and operators, and operands including constants, constants, and variables.

[0088] (1.9)

[0089] (1.10)

[0090] Calculate the CV of the code according to formula (1.9). For the code fragment S, assume that the operator of the code fragment is ,Right now , the operands are ,Right now , and then we get the operator table shown in Table 1 and the operand table shown in Table 2, then we can get , Finally, according to formula (1.9), we get .

[0091] Table 1 Operator table

[0092]

[0093] Table 2 Operand table

[0094]

[0095] Step 2.2.5: Extract character entropy feature CE. The formula is shown in (1.11), For code snippets The characters in for The probability of occurrence, , for Number of occurrences.

[0096] (1.11)

[0097] Calculate the CE of the code according to formula (1.11), ignoring the case of characters. For code fragment S, assume that the character set of the code fragment is , the character table is shown in Table 3, then the total number of characters in the code fragment is 111, and then the probability of the corresponding characters appearing is calculated, and finally according to formula (1.11) .

[0098] Table 3 Character table

[0099]

[0100] Step 2.2.6: Use the five structural features extracted from Step 2.2.1 to Step 2.2.5 as the structural feature vector of each code fragment S , , and Perform the same CFU standardization as Step 2.1.6.

[0101] Step 3 builds the base learner module, including:

[0102] Step 3.1 Construct a base learner for learning code text features: Construct a base learner for learning code text features. Its mathematical model is shown in formula (1.12), where For the data set The text feature vector of the code snippet, for The code text feature constraint coefficient, is the base learner bias value, is the code text feature mapping function, which maps the input code text features to a higher-dimensional feature space, making the data easier to be segmented by the base learner. Its formula is shown in (1.13), where , for and The square of the Euclidean distance between is the dimension of the code text feature dataset, the base learner loss function is shown in formula (1.14), the base learner receives the code text feature input , output the corresponding predicted value .

[0103]

[0104] and It is a parameter that will be continuously updated during the construction process.

[0105] Euclidean distance, also known as Euclidean distance, is the most common distance metric, measuring the absolute distance between two points in multidimensional space. The Euclidean distance formula in dimensional space is .

[0106] Step 3.1.1 Construction process of base learner for learning code text features:

[0107] Step 3.1.1.1: Construct the code text feature dataset , which is the text feature vector and readability label of the code snippet. Hyperparameters are determined by cross-validation , as well as , and set the parameters and The initial value is 0, and the maximum number of iterations is maxIter. is the regularization parameter, which controls the complexity of the base learner. The smaller the value, the stronger the regularization effect. is the error range, which defines the boundary size of the base learner loss function during the construction process. is the coefficient of the code text feature mapping function, The larger the value, the smaller the influence range of the mapping function, and the base learner focuses more on fitting local data points.

[0108] Step 3.1.1.2: Unless all data samples in the code text feature dataset All satisfy the BLC (Base Learner Convergence) condition shown in formula (1.15) or reach the maximum number of iterations maxIter, otherwise continue to execute Step3.1.1.2.1 to Step3.1.1.2.7:

[0109] (1.15)

[0110] The code text feature dataset Human-readable labels for data samples, for The predicted value after input into the base learner.

[0111] Step 3.1.1.2.1: Select two code text feature constraint coefficients and , the first code text feature constraint coefficient The second code text feature constraint coefficient is the one that violates the BLC condition shown in formula (1.15) to the greatest extent. choose The largest value.

[0112] Step 3.1.1.2.2: Update the step length of the code text feature constraint coefficient , code text feature vector sample similarity , code text feature constraint coefficient adjustment threshold .

[0113] Among them, the sample similarity Used to control the update amplitude. If The larger the value, the smaller the update amplitude, because the similarity between samples is low, and we do not want to update too quickly. The smaller it is, the larger the update amplitude is, because the similarity between samples is high and the optimal solution can be approached faster.

[0114] Step 3.1.1.2.3: Preliminary update of the two code text feature constraint coefficients, let , .

[0115] Step 3.1.1.2.4: If and Different symbols and and All are greater than or equal to the code text feature constraint coefficient adjustment threshold , in order to make the code text feature constraint coefficient adjust faster in the direction of reducing the loss function, let . is a symbolic function, defined as follows.

[0116] (1.16)

[0117] Step 3.1.1.2.5: If and Different symbols and and There is one less than the code text feature constraint coefficient adjustment threshold , then the step size needs to be updated using the code text feature constraint coefficient To limit the amplitude of multiplier update, so as to avoid the instability of base learner caused by too fast update, let . is a step function, defined as follows.

[0118] (1.17)

[0119] Step 3.1.1.2.6: Prune updated code text feature constraint coefficients and Keep it in Within the range, , , trim code text feature constraint coefficient , .

[0120] Step 3.1.1.2.7: Update the base learner bias value .

[0121] make , , and finally get .

[0122] Step 3.1.1.3: After the iteration is completed, the optimal code text feature constraint coefficient is obtained and bias value , is the number of training data samples. At this point, a base learner for learning text features is constructed. The base learner receives text feature input , output the corresponding predicted value .

[0123] Step 3.2 Construct a base learner for learning code structure features: Construct a base learner for learning code structure features. Its mathematical model is shown in formula (1.18), where For the data set The structural feature vector of the code snippet, for The code structure characteristic constraint coefficient, is the base learner bias value, is the code structure feature mapping function, which maps the input code structure features to a higher-dimensional feature space, making the data easier to be segmented by the base learner. Its formula is shown in (1.19), where , for and The square of the Euclidean distance between is the dimension of the code structure feature dataset, the base learner loss function is shown in formula (1.20), the base learner receives the code structure feature input , output the corresponding predicted value .

[0124]

[0125] Step 3.2.1 The construction process of the base learner for learning code structure features: The construction process of the base learner for learning code structure features only requires changing the corresponding text features in Step 3.1.1 to structural features. For example, the data set is changed to code structure feature data set. , that is, the structural feature vector and readability label of the code snippet. The rest is the same as Step 3.1.1 and will not be repeated here.

[0126] Step 4: Construct a code feature stacking module: Construct a code feature stacking module, whose mathematical model is shown in formula (1.21), where is the code text feature weight, is the code structure feature weight, and the loss function is shown in formula (1.22). The module uses the idea of ​​Feature Stacking in ensemble learning to receive the prediction output of two base learners. and As feature input, adaptively learn the weights of different features and output the final readability evaluation score .

[0127]

[0128] Step 4.1 Construction process of code feature stacking module:

[0129] Step 4.1.1: Divide the code readability evaluation dataset into k parts and put them into two base learners for k training. , the i-th dataset is used as the validation set, and the remaining k-1 datasets are used as the training set. After the i-th training is completed, the validation set is used for verification and the prediction output is saved. After k trainings are completed, the prediction output of k verifications is obtained as the code feature stacking dataset. , that is, the predicted output of the two base learners and the readability label of the code snippet. The above process is k-fold cross-validation. The reason for using k-fold cross-validation to build the code feature stacking dataset is that if the training set is used to predict on the base learner, the predicted output will be overfitted.

[0130] Determine the hyperparameter learning rate and feature stacking data batch size , and set the parameters , as well as The initial value is 0 and the maximum number of iterations is maxIter.

[0131] Step 4.1.2: Unless the loss function shown in formula (1.22) converges or reaches the maximum number of iterations maxIter, continue to execute Step 4.1.2.1 to Step 4.1.2.2.

[0132] Step 4.1.2.1: Random uniform sampling training data samples.

[0133] Step 4.1.2.2: Update code text feature weights , Code structure feature weight And the offset value .

[0134] make , , .

[0135] Step 4.1.3: After the iteration is completed, the optimal code text feature weight is obtained , Code structure feature weight and bias value , a code feature stacking module is constructed. The code feature stacking module receives the prediction output of the two base learners and As feature input, output the final readability evaluation score .

[0136] Step 5: Apply the model to evaluate code readability: After the code readability evaluation model CREM is built, CREM is applied to evaluate code readability. The code snippet that needs to be evaluated is input into CREM, and the code snippet is processed by the code feature extraction module, the base learner module, and the code feature stacking module to output the readability evaluation score.

[0137] A code readability evaluation system, comprising:

[0138] A data collection module is used to obtain code snippets to be evaluated and corresponding readability labels; and to construct a readability evaluation data set based on the code snippets to be evaluated and the corresponding readability labels;

[0139] A code readability evaluation module is used to input the readability evaluation data set into a code readability evaluation model for evaluation prediction to obtain a readability evaluation score; wherein the code readability evaluation model includes a feature extraction module, a base learner module and a code feature stacking module connected in sequence.

[0140] An electronic device comprises a memory and a processor, wherein the memory is used to store a computer program, and the processor runs the computer program to enable the electronic device to execute a code readability evaluation method.

[0141] A computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method for evaluating code readability is implemented.

[0142] The above is only a preferred specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed in the present application should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.

Claims

1. A code readability evaluation method, It is characterized in that include: Get the code snippet to be evaluated and the corresponding readability label; Constructing a readability evaluation dataset based on the code snippets to be evaluated and the corresponding readability labels; Inputting the readability evaluation data set into a code readability evaluation model for evaluation prediction to obtain a readability evaluation score; wherein the code readability evaluation model comprises a feature extraction module, a base learner module and a code feature stacking module connected in sequence; The processing process of the code readability evaluation model includes: Inputting the readability evaluation data set into the feature extraction module to extract features and obtain code feature data; wherein the code feature data includes text feature data and structure feature data; Performing standardization processing on the text feature data and the structural feature data; The standardized feature data are respectively input into the base learner module for feature learning and prediction, and the predicted output data corresponding to each feature data is obtained; Inputting the predicted output data as input feature data into the code feature stacking module, the code feature stacking module adaptively learns the weights of different input feature data and outputs a readability evaluation score; The text feature data includes the annotation and identifier consistency feature AIC, identifier integrity feature II, identifier narrow sense feature IN, identifier ambiguity feature IA and annotation readability feature CR of the code fragment to be evaluated; The structural feature data includes a blank line ratio feature BLR, a bracket number feature BP, an identifier length feature IL, a code volume feature CV and a character entropy feature CE of the code fragment to be evaluated.

2. A code readability evaluation method according to claim 1, It is characterized in that The training process of the code readability evaluation model includes: Acquiring training data, wherein the training data includes a readability evaluation data training set and a corresponding readability evaluation score; An initial code readability evaluation model is constructed, the training data is input into the initial code readability evaluation model for evaluation prediction, and training is performed with the goal of minimizing the loss between the initial training result after evaluation prediction and the readability evaluation score corresponding to the readability evaluation data training set, so as to obtain the code readability evaluation model.

3. A code readability evaluation system, It is characterized in that include: Data collection module, used to obtain the code snippets to be evaluated and the corresponding readability labels; Constructing a readability evaluation dataset based on the code snippets to be evaluated and the corresponding readability labels; A code readability evaluation module, used for inputting the readability evaluation data set into a code readability evaluation model for evaluation prediction to obtain a readability evaluation score; wherein the code readability evaluation model comprises a feature extraction module, a base learner module and a code feature stacking module connected in sequence; The processing process of the code readability evaluation model includes: Inputting the readability evaluation data set into the feature extraction module to extract features and obtain code feature data; wherein the code feature data includes text feature data and structure feature data; Performing standardization processing on the text feature data and the structural feature data; The standardized feature data are respectively input into the base learner module for feature learning and prediction, and the predicted output data corresponding to each feature data is obtained; Inputting the predicted output data as input feature data into the code feature stacking module, the code feature stacking module adaptively learns the weights of different input feature data and outputs a readability evaluation score; The text feature data includes the annotation and identifier consistency feature AIC, identifier integrity feature II, identifier narrow sense feature IN, identifier ambiguity feature IA and annotation readability feature CR of the code fragment to be evaluated; The structural feature data includes a blank line ratio feature BLR, a bracket number feature BP, an identifier length feature IL, a code volume feature CV and a character entropy feature CE of the code fragment to be evaluated.

4. An electronic device, It is characterized in that It comprises a memory and a processor, wherein the memory is used to store a computer program, and the processor runs the computer program to enable the electronic device to execute a code readability evaluation method according to any one of claims 1-2.

5. A computer-readable storage medium, It is characterized in that It stores a computer program, which, when executed by a processor, implements a code readability evaluation method as described in any one of claims 1-2.