BiLSTM-CRF-based code formatting method and system

The source code is preprocessed and segmented through the BiLSTM-CRF model, and spaces, newlines and indented labels are predicted, solving the problems of niche language code formatting and low IDE efficiency, and achieving fast and accurate formats of codes in various languages and efficient prediction.

CN119088447APending Publication Date: 2024-12-06GUANGDONG POWER GRID CO LTD +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411128839.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-16
Publication Date
2024-12-06

AI Technical Summary

Technical Problem

The prior art cannot effectively format niche language code, and as the code size increases, the IDE runs slowly, affecting the efficiency of formatting tools.

Method used

Using BiLSTM-CRF-based code formatting method, preprocessing and segmenting source code, and using BiLSTM-CRF model to predict spaces, line breaks and indent tags, to achieve fast and accurate formatting of codes in various languages.

Benefits of technology

It realizes fast and accurate formatting of codes in various languages, improves the versatility and efficiency of formatting tools, reduces the input of BiLSTM-CRF model, and improves prediction accuracy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119088447A_ABST
    Figure CN119088447A_ABST
Patent Text Reader

Abstract

The invention discloses a BiLSTM-CRF-based code formatting method and system, and the method comprises the steps: obtaining a to-be-formatted source code, and carrying out the preprocessing of the source code, and obtaining a preprocessing result; segmenting the preprocessing result to obtain a plurality of units; inputting the plurality of units into a BiLSTM-CRF model to obtain a score of a corresponding label behind each unit, and determining a corresponding prediction label behind each unit based on the score; wherein the labels comprise space labels, line feed labels and indentation labels; and formatting the source code based on the corresponding prediction label behind each unit. According to the method and the device, various language codes can be quickly and accurately formatted, and the universality is high.
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 code formatting method and system based on BiLSTM-CRF. Background Art

[0002] As the scale of software continues to grow, code writers may intentionally or unintentionally disrupt the code structure of the program without affecting the program's functionality when writing software code, which greatly reduces the readability of the source code and in turn affects the subsequent development, maintenance, and iterative updates of the source code.

[0003] In the prior art, formatting tools integrated in an integrated development environment (IDE) are usually used to format code. However, formatting tools integrated in an IDE environment can only format common language codes, such as Python, Java, C++, etc., but cannot format niche language codes, and have poor versatility. At the same time, since formatting tools rely on the IDE environment, when the code volume increases, the IDE runs slowly, which in turn affects the efficiency of the formatting tools.

[0004] Application Contents

[0005] The present application provides a code formatting method and system based on BiLSTM-CRF, which can quickly and accurately format various language codes and has strong versatility.

[0006] In a first aspect, the present application provides a code formatting method based on BiLSTM-CRF, comprising:

[0007] Obtaining source code to be formatted, and preprocessing the source code to obtain a preprocessing result;

[0008] Segmenting the preprocessing result to obtain a plurality of units;

[0009] Inputting a plurality of the units into the BiLSTM-CRF model, obtaining scores of labels corresponding to the units, and determining predicted labels corresponding to the units based on the scores; wherein the labels include space labels, line break labels, and indentation labels;

[0010] The source code is formatted based on the corresponding predicted tags after each of the units.

[0011] The embodiment of the present application can delete blank lines and redundant spaces in the source code by preprocessing the source code, which is convenient for subsequent code segmentation, reduces the subsequent BiLSTM-CRF model input, and thus improves the accuracy of BiLSTM-CRF model prediction; by segmenting the preprocessing results, each unit in the code can be segmented out, which is convenient for the subsequent BiLSTM-CRF model to quickly and accurately predict the label after the unit; the BiLSTM-CRF model can calculate the score of the corresponding label after each unit to accurately predict the corresponding predicted label after each unit, and format the source code based on the corresponding predicted label after each unit, which can quickly and accurately format various language codes and has strong versatility.

[0012] Furthermore, the source code is preprocessed to obtain a preprocessing result, specifically:

[0013] Traversing the source code, deleting all blank lines in the source code;

[0014] Add a start sign at the beginning of each line of code in the source code;

[0015] Replace multiple consecutive spaces in the source code with a single space.

[0016] In this way, by preprocessing the source code, blank lines and extra spaces in the source code can be deleted, which facilitates subsequent code segmentation and reduces the subsequent BiLSTM-CRF model input, thereby improving the accuracy of BiLSTM-CRF model prediction.

[0017] Furthermore, the preprocessing result is segmented to obtain a plurality of units, specifically:

[0018] Traversing the preprocessing results, and detecting spaces or line breaks in the preprocessing results;

[0019] The preprocessing result is segmented based on the space or the line break to obtain a plurality of units.

[0020] In this way, by segmenting the preprocessing results, each unit in the code can be segmented, making it easier for the subsequent BiLSTM-CRF model to quickly and accurately predict the label behind the unit.

[0021] Furthermore, the source code is formatted based on the corresponding predicted tags after each unit, specifically:

[0022] Replace the space or line break after each unit in the source code with a corresponding predicted tag after each unit;

[0023] Delete the start mark at the beginning of each line of code.

[0024] In this way, by replacing the space or line break after each unit with a predicted label, various language codes can be formatted quickly and accurately with strong versatility.

[0025] Furthermore, the training process of the BiLSTM-CRF model is specifically as follows:

[0026] Obtaining a target format code, and marking and segmenting the target format code to obtain a plurality of target units and actual labels behind the target units;

[0027] Inputting a number of the target units into the BGE English model to obtain a word vector corresponding to each of the target units;

[0028] The word vectors corresponding to several of the target units are input into the BiLSTM-CRF model to obtain the scores of the labels corresponding to each of the target units, and the loss values ​​of the scores and the actual labels are calculated to continuously optimize the BiLSTM-CRF model based on the loss values.

[0029] In this way, the word vector corresponding to the target unit is input into the BiLSTM-CRF model for forward propagation to calculate the score of the label corresponding to each target unit, and the loss value of the score and the actual label is calculated to continuously optimize the BiLSTM-CRF model based on the loss value, thereby improving the accuracy of BiLSTM-CRF model prediction.

[0030] In a second aspect, the present application provides a code formatting system based on BiLSTM-CRF, including: a preprocessing module, a segmentation module, a prediction module and a formatting module;

[0031] The preprocessing module is used to obtain the source code to be formatted, and preprocess the source code to obtain a preprocessing result;

[0032] The segmentation module is used to segment the preprocessing result to obtain a plurality of units;

[0033] The prediction module is used to input a plurality of the units into the BiLSTM-CRF model, obtain the scores of the labels corresponding to the units, and determine the predicted labels corresponding to the units based on the scores; wherein the labels include space labels, line break labels and indentation labels;

[0034] The formatting module is used to format the source code based on the prediction tags corresponding to each of the units.

[0035] The embodiment of the present application can delete blank lines and redundant spaces in the source code by preprocessing the source code, which is convenient for subsequent code segmentation, reduces the subsequent BiLSTM-CRF model input, and thus improves the accuracy of BiLSTM-CRF model prediction; by segmenting the preprocessing results, each unit in the code can be segmented out, which is convenient for the subsequent BiLSTM-CRF model to quickly and accurately predict the label after the unit; the BiLSTM-CRF model can calculate the score of the corresponding label after each unit to accurately predict the corresponding predicted label after each unit, and format the source code based on the corresponding predicted label after each unit, which can quickly and accurately format various language codes and has strong versatility.

[0036] Further, the pre-processing module includes: a first deletion unit, an addition unit and a first replacement unit;

[0037] The first deleting unit is used to traverse the source code and delete all blank lines in the source code;

[0038] The adding unit is used to add a start mark at the beginning of each line of code in the source code;

[0039] The first replacement unit is used to replace multiple consecutive spaces in the source code with a single space.

[0040] Further, the segmentation module includes: a detection unit and a segmentation unit;

[0041] The detection unit is used to traverse the preprocessing result and detect spaces or line breaks in the preprocessing result;

[0042] The segmentation unit is used to segment the preprocessing result based on the space or the line break to obtain a plurality of units.

[0043] Further, the formatting module includes: a second replacement unit and a second deletion unit;

[0044] The second replacement unit is used to replace the space or the line break after each unit in the source code with a corresponding predicted label after each unit;

[0045] The second deleting unit is used to delete the start mark at the beginning of each line of code.

[0046] Furthermore, the training process of the BiLSTM-CRF model is specifically as follows:

[0047] Obtaining a target format code, and marking and segmenting the target format code to obtain a plurality of target units and actual labels behind the target units;

[0048] Inputting a number of the target units into the BGE English model to obtain a word vector corresponding to each of the target units;

[0049] The word vectors corresponding to several of the target units are input into the BiLSTM-CRF model to obtain the scores of the labels corresponding to each of the target units, and the loss values ​​of the scores and the actual labels are calculated to continuously optimize the BiLSTM-CRF model based on the loss values. BRIEF DESCRIPTION OF THE DRAWINGS

[0050] Figure 1 This is a flowchart of an embodiment of a code formatting method based on BiLSTM-CRF provided by the present application;

[0051] Figure 2 It is a schematic diagram of the structure of the BiLSTM-CRF model provided in this application;

[0052] Figure 3 It is a structural diagram of an embodiment of a code formatting system based on BiLSTM-CRF provided by the present application. DETAILED DESCRIPTION

[0053] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions 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 are within the scope of protection of this application.

[0054] It should be understood that the step numbers used in this article are only for the convenience of description and are not intended to limit the order in which the steps are executed.

[0055] It should be understood that the terms used in the present specification are only for the purpose of describing specific embodiments and are not intended to limit the present invention. As used in the present specification and the appended claims, the singular forms "a", "an" and "the" are intended to include plural forms unless the context clearly indicates otherwise.

[0056] The terms “include” and “comprising” indicate the presence of described features, integers, steps, operations, elements and / or components, but do not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or combinations thereof.

[0057] The term "and / or" means and includes any and all possible combinations of one or more of the associated listed items.

[0058] The BiLSTM-CRF model, which is a combination of a bidirectional long short-term memory network (BiLSTM) and a conditional random field (CRF), is a sequence labeling model widely used in the field of natural language processing (NLP). The BiLSTM-CRF model includes a BiLSTM layer and a CRF layer. The BiLSTM layer is a special recurrent neural network (RNN) that extracts contextual features of the text and generates a vector representation (i.e., score) containing label information for each word or character; CRF (conditional random field) is a discriminant model for sequence labeling that takes into account the interactions and dependencies between labels and can find the label sequence with the highest prediction score as the final labeling result.

[0059] As the size of software grows, code writers may accidentally disrupt the code structure, which greatly reduces the readability of the source code and affects subsequent maintenance and updates. Currently, formatting is usually done by relying on formatting tools in IDEs, but these tools can only format common languages, such as Python, Java, and C++, and cannot format niche languages. In addition, as the code size increases, reliance on the IDE operating environment will also affect formatting efficiency.

[0060] Embodiment 1

[0061] Please refer to Figure 1 , Figure 1 It is a flowchart of an embodiment of a code formatting method based on BiLSTM-CRF provided by the present application, including steps S1 to S4;

[0062] Step S1, obtaining source code to be formatted, and preprocessing the source code to obtain a preprocessing result;

[0063] It is understandable that the programming language used in the source code can be assembly language, C language and script programming language, and also includes other types of languages, such as Rust, Scala and gsql. After obtaining the source code to be formatted, the source code needs to be preprocessed to facilitate the subsequent input into the BiLSTM-CRF model; specifically: traverse the source code through a computer program, find all blank lines in the source code and delete them, and then add a start mark Head to the beginning of each line of code in the source code; finally, traverse the source code again through a computer program, and replace multiple consecutive spaces in the source code with a single space.

[0064] In this way, by preprocessing the source code, blank lines and extra spaces in the source code can be deleted, which facilitates subsequent code segmentation and reduces the subsequent BiLSTM-CRF model input, thereby improving the accuracy of BiLSTM-CRF model prediction.

[0065] Step S2, segmenting the preprocessing result to obtain a plurality of units;

[0066] It is understandable that after the source code is preprocessed, the preprocessing result needs to be segmented to divide the source code into several units for the convenience of subsequent input into the BiLSTM-CRF model; specifically: the preprocessing result is traversed by a computer program to detect spaces or line breaks in the preprocessing result; and the preprocessing result is segmented based on the spaces or line breaks to obtain several units, so that the content after each unit is a space (SPACE) or a line break (WRAP). For example, such as the line of code "a=2", this line of code includes "a、SPACE、=、SPACE、2、WRAP". If this line of code is segmented, 3 units will be obtained, namely "a、=、2", and each unit is followed by labels such as space (SPACE) or line break (WRAP).

[0067] It should be noted that the unit can be a word or a symbol, etc., and this application does not limit the unit components.

[0068] In this way, by segmenting the preprocessing results, each unit in the code can be segmented, making it easier for the subsequent BiLSTM-CRF model to quickly and accurately predict the label behind the unit.

[0069] Step S3, inputting a plurality of the units into the BiLSTM-CRF model, obtaining scores of labels corresponding to the units, and determining predicted labels corresponding to the units based on the scores; wherein the labels include space labels, line break labels, and indentation labels;

[0070] It can be understood that after the source code is divided into several units, it can be input into the BiLSTM-CRF model for prediction. By inputting several of the units into the BiLSTM-CRF model, the scores of the corresponding labels behind each unit are obtained, and the predicted labels corresponding to the following units are determined based on the scores; wherein the labels include space labels (SPACE), line break labels (WRAP) and indentation labels (T0, T1, T2, ..., TN). Exemplarily, by inputting the three units "a, =, 2" in the line of code "a = 2" into the BiLSTM-CRF model, the BiLSTM layer will output the scores of "a, =, 2" corresponding to the three "space labels, line break labels and indentation labels" respectively. The CRF layer can predict the most likely predicted label combination behind the three units "a, =, 2" according to the scores output by the BiLSTM layer, such as the most likely label behind "a" is "SPACE", the most likely label behind "=" is "SPACE", and the most likely label behind "2" is "SPACE".

[0071] It should be noted that when the BiLSTM-CRF model predicts that the predicted label after the unit is "line break label (WRAP)", it is necessary to add the starting mark Head at the beginning of the new code.

[0072] It should be noted that the BiLSTM-CRF model will also predict the number of indents after the start marker Head to output predicted indentation labels (T0, T1, T2, ..., TN). The "indentation label" after the start marker Head can be determined by judging the number of spaces, that is, 0 spaces are T0, 4 spaces are T1, 8 spaces are T2, ..., 4N spaces are TN. The specific prediction process of the number of indents after Head is similar to the above, so it will not be repeated here.

[0073] In this way, by replacing the space or line break after each unit with a predicted label, various language codes can be formatted quickly and accurately with strong versatility.

[0074] It can be understood that the training process of the BiLSTM-CRF model used in step S3 is specifically as follows: obtaining the target format code, and annotating and segmenting the target format code to obtain several target units and the actual labels behind the target units; inputting several of the target units into the BGE English model to obtain the word vectors corresponding to each of the target units; inputting the word vectors corresponding to several of the target units into the BiLSTM-CRF model to obtain the scores of the labels corresponding to each of the target units, and calculating the loss value of the scores and the actual labels to continuously optimize the BiLSTM-CRF model based on the loss value. Specifically, first, you need to collect language codes containing standard formats, where the number of target format codes should be as large as possible over 3,000 lines, and try to cover the grammatical features of the language. Of course, the target format code can also be the sample code of the official document; secondly, after collecting the code, add the starting mark Head to the beginning of each line of code, and mark each line of code with an "indentation label (T0, T1, T2, ..., TN)" according to the number of indents after the starting mark Head, that is, the number of T. If there is a blank line after the starting mark Head, the corresponding label to be marked is the "line break label (WRAP)", and the target code format is divided according to the space and line break, so as to divide the target format code into several units, each unit is followed by a space and a line break, and the corresponding "space label (SPACE)" and "line break label (WRAP)" are marked according to the space and line break after each unit. If there are multiple spaces, they are also processed as one space, that is, if there are multiple spaces, only one " Space label (SPACE)", wherein the "indentation label (T0, T1, T2, ..., TN)", "space label (SPACE)" and "line break label (WRAP)" marked on the target code are the actual labels behind the target unit; again, several of the target units are input into the BGE English model to obtain the word vectors corresponding to each of the target units; finally, the word vectors corresponding to several of the target units are input into the BiLSTM-CRF model, and the score of each target unit corresponding to the predicted label is obtained through the forward propagation of the BiLSTM-CRF model, and the loss value of the score and the actual label is calculated, and then the values ​​of the parameters of the BiLSTM-CRF model are updated through back propagation, and the loss value of each training is repeatedly calculated in this way, that is, the BiLSTM-CRF model can be continuously optimized based on the loss value until the loss value of the model reaches a satisfactory level or reaches a predetermined training cycle, and the training of the BiLSTM-CRF model can be completed.

[0075] It should be noted that the word vectors w0, w1, w2, w3, and w4 input to the BiLSTM-CRF model are word vectors containing word embeddings and character embeddings, where word embeddings are usually pre-trained, that is, word embeddings obtained through the BGE English model, and character embeddings are randomly initialized. All embeddings will be adjusted during the training iteration process. Exemplarily, assuming that all labels of the target code are "T0, T1, T2, T3, T4, T5, T6, T7, SPACE, WRAP", if the label corresponding to a unit is T3, that is, the value corresponding to T3 is 1, and the others are all 0, the input word vector w0 of the BiLSTM-CRF model is: [0,0,0,1,0,0,0,0,0,0], that is, 1 is the word embedding, and the remaining 0 is the randomly initialized word embedding; the BiLSTM layer can output the label score corresponding to each word vector w0, w1, w2, w3, w4, and the CRF layer can output the label corresponding to each unit in each line of code, wherein the structure diagram of the BiLSTM-CRF model is as follows: Figure 2 As shown by Figure 2 As we know, "T1, SPACE, SPACE, SPACE, WRAP" with the maximum probability of 0.9 will be used as the predicted label combination of several units for subsequent code formatting.

[0076] In this way, the word vector corresponding to the target unit is input into the BiLSTM-CRF model for forward propagation to calculate the score of the label corresponding to each target unit, and the loss value of the score and the actual label is calculated to continuously optimize the BiLSTM-CRF model based on the loss value, thereby improving the accuracy of BiLSTM-CRF model prediction.

[0077] Step S4: formatting the source code based on the corresponding predicted tags after each of the units.

[0078] It can be understood that after the predicted label corresponding to each unit is predicted through step S3, the source code can be formatted; specifically: the space or the line break after each unit in the source code is replaced with the predicted label corresponding to each unit; and the start mark at the beginning of each line of code is deleted. Exemplarily, still taking the line of code "a=2" as an example, in step S2, each unit in this line of code, namely "a,=, 2", is followed by "SPACE, SPACE, WRAP". In step S3, the BiLSTM-CRF model predicts that the most likely label after "a" is "SPACE", the most likely label after "=" is "SPACE", and the most likely label after "2" is "SPACE". The "SPACE, SPACE, WRAP" after the three units "a,=, 2" are replaced with the label combination "SPACE, SPACE, SPACE" predicted in step S3. At the same time, combined with the BiLSTM-CRF model, the indentation label predicted after the starting tag Head, if it is T3, the code can be "T3, a, SPACE,=, SPACE, 2, SPACE", that is, the indentation T in front of the line of code "a=2" is 3. By deleting the starting tag Head at the beginning of each line of code, the formatted code can be obtained.

[0079] In this way, by replacing the space or line break after each unit with a predicted label, various language codes can be formatted quickly and accurately with strong versatility.

[0080] It should be noted that the code formatting method based on BiLSTM-CRF in the embodiment of the present application can be applied to a terminal device, and the terminal device can be the execution end (user terminal) of the code or the compilation end (development terminal) of the code. The user terminal can be any electronic device capable of receiving the above-mentioned source code file, for example, a desktop computer, a laptop computer, a tablet computer, etc. The development terminal can be a compiler or any electronic device including a compiler. The compiler is used to compile source code, generate intermediate code and executable code. After obtaining the source code, the compiler compiles the source code into executable code, and then sends the executable code to the hardware that executes the code, such as a CPU (Central Processing Unit), and the CPU executes the target code to obtain the execution result. That is, the code formatting method based on BiLSTM-CRF provided in the embodiment of the present application can be performed during the compilation process of the code, or during the running process of the code.

[0081] The embodiment of the present application can delete blank lines and redundant spaces in the source code by preprocessing the source code, which is convenient for subsequent code segmentation, reduces the subsequent BiLSTM-CRF model input, and thus improves the accuracy of BiLSTM-CRF model prediction; by segmenting the preprocessing results, each unit in the code can be segmented out, which is convenient for the subsequent BiLSTM-CRF model to quickly and accurately predict the label after the unit; the BiLSTM-CRF model can calculate the score of the corresponding label after each unit to accurately predict the corresponding predicted label after each unit, and format the source code based on the corresponding predicted label after each unit, which can quickly and accurately format various language codes and has strong versatility.

[0082] Embodiment 2

[0083] Please refer to Figure 3 , Figure 3 It is a structural diagram of an embodiment of a code formatting system based on BiLSTM-CRF provided by the present application, including: a preprocessing module 01, a segmentation module 02, a prediction module 03 and a formatting module 04;

[0084] The preprocessing module 01 is used to obtain source code to be formatted, and preprocess the source code to obtain a preprocessing result;

[0085] The preprocessing module 01 includes: a first deleting unit, an adding unit and a first replacing unit; the first deleting unit is used to traverse the source code and delete all blank lines in the source code; the adding unit is used to add a start mark at the beginning of each line of code in the source code; the first replacing unit is used to replace multiple consecutive spaces in the source code with a single space.

[0086] It is understandable that the programming language used in the source code can be assembly language, C language and script programming language, and also includes other types of languages, such as Rust, Scala and gsql. After obtaining the source code to be formatted, the source code needs to be preprocessed to facilitate the subsequent input into the BiLSTM-CRF model; specifically: traverse the source code through a computer program, find all blank lines in the source code and delete them, and then add a start mark Head to the beginning of each line of code in the source code; finally, traverse the source code again through a computer program, and replace multiple consecutive spaces in the source code with a single space.

[0087] In this way, by preprocessing the source code, blank lines and extra spaces in the source code can be deleted, which facilitates subsequent code segmentation and reduces the subsequent BiLSTM-CRF model input, thereby improving the accuracy of BiLSTM-CRF model prediction.

[0088] The segmentation module 02 is used to segment the preprocessing result to obtain a plurality of units;

[0089] The segmentation module 02 includes: a detection unit and a segmentation unit; the detection unit is used to traverse the preprocessing results and detect spaces or line breaks in the preprocessing results; the segmentation unit is used to segment the preprocessing results based on the spaces or the line breaks to obtain a plurality of units.

[0090] It is understandable that after the source code is preprocessed, the preprocessing result needs to be segmented to divide the source code into several units for the convenience of subsequent input into the BiLSTM-CRF model; specifically: the preprocessing result is traversed by a computer program to detect spaces or line breaks in the preprocessing result; and the preprocessing result is segmented based on the spaces or line breaks to obtain several units, so that the content after each unit is a space (SPACE) or a line break (WRAP). For example, such as the line of code "a=2", this line of code includes "a、SPACE、=、SPACE、2、WRAP". If this line of code is segmented, 3 units will be obtained, namely "a、=、2", and each unit is followed by labels such as space (SPACE) or line break (WRAP).

[0091] It should be noted that the unit can be a word or a symbol, etc., and this application does not limit the unit components.

[0092] In this way, by segmenting the preprocessing results, each unit in the code can be segmented, making it easier for the subsequent BiLSTM-CRF model to quickly and accurately predict the label behind the unit.

[0093] The prediction module 03 is used to input a plurality of the units into the BiLSTM-CRF model, obtain the scores of the labels corresponding to the units, and determine the predicted labels corresponding to the units based on the scores; wherein the labels include space labels, line break labels and indentation labels;

[0094] It can be understood that after the source code is divided into several units, it can be input into the BiLSTM-CRF model for prediction. By inputting several of the units into the BiLSTM-CRF model, the scores of the corresponding labels behind each unit are obtained, and the predicted labels corresponding to the following units are determined based on the scores; wherein the labels include space labels (SPACE), line break labels (WRAP) and indentation labels (T0, T1, T2, ..., TN). Exemplarily, by inputting the three units "a, =, 2" in the line of code "a = 2" into the BiLSTM-CRF model, the BiLSTM layer will output the scores of "a, =, 2" corresponding to the three "space labels, line break labels and indentation labels" respectively. The CRF layer can predict the most likely predicted label combination behind the three units "a, =, 2" according to the scores output by the BiLSTM layer, such as the most likely label behind "a" is "SPACE", the most likely label behind "=" is "SPACE", and the most likely label behind "2" is "SPACE".

[0095] It should be noted that when the BiLSTM-CRF model predicts that the predicted label after the unit is "line break label (WRAP)", it is necessary to add the starting mark Head at the beginning of the new code.

[0096] It should be noted that the BiLSTM-CRF model will also predict the number of indents after the start marker Head to output predicted indentation labels (T0, T1, T2, ..., TN). The "indentation label" after the start marker Head can be determined by judging the number of spaces, that is, 0 spaces are T0, 4 spaces are T1, 8 spaces are T2, ..., 4N spaces are TN. The specific prediction process of the number of indents after Head is similar to the above, so it will not be repeated here.

[0097] In this way, by replacing the space or line break after each unit with a predicted label, various language codes can be formatted quickly and accurately with strong versatility.

[0098] It can be understood that the training process of the BiLSTM-CRF model used in step S3 is specifically as follows: obtaining the target format code, and annotating and segmenting the target format code to obtain several target units and the actual labels behind the target units; inputting several of the target units into the BGE English model to obtain the word vectors corresponding to each of the target units; inputting the word vectors corresponding to several of the target units into the BiLSTM-CRF model to obtain the scores of the labels corresponding to each of the target units, and calculating the loss value of the scores and the actual labels to continuously optimize the BiLSTM-CRF model based on the loss value. Specifically, first, you need to collect language codes containing standard formats, where the number of target format codes should be as large as possible over 3,000 lines, and try to cover the grammatical features of the language. Of course, the target format code can also be the sample code of the official document; secondly, after collecting the code, add the starting mark Head to the beginning of each line of code, and mark each line of code with an "indentation label (T0, T1, T2, ..., TN)" according to the number of indents after the starting mark Head, that is, the number of T. If there is a blank line after the starting mark Head, the corresponding label to be marked is the "line break label (WRAP)", and the target code format is divided according to the space and line break, so as to divide the target format code into several units, each unit is followed by a space and a line break, and the corresponding "space label (SPACE)" and "line break label (WRAP)" are marked according to the space and line break after each unit. If there are multiple spaces, they are also processed as one space, that is, if there are multiple spaces, only one " Space label (SPACE)", wherein the "indentation label (T0, T1, T2, ..., TN)", "space label (SPACE)" and "line break label (WRAP)" marked on the target code are the actual labels behind the target unit; again, several of the target units are input into the BGE English model to obtain the word vectors corresponding to each of the target units; finally, the word vectors corresponding to several of the target units are input into the BiLSTM-CRF model, and the score of each target unit corresponding to the predicted label is obtained through the forward propagation of the BiLSTM-CRF model, and the loss value of the score and the actual label is calculated, and then the values ​​of the parameters of the BiLSTM-CRF model are updated through back propagation, and the loss value of each training is repeatedly calculated in this way, that is, the BiLSTM-CRF model can be continuously optimized based on the loss value until the loss value of the model reaches a satisfactory level or reaches a predetermined training cycle, and the training of the BiLSTM-CRF model can be completed.

[0099] It should be noted that the word vectors w0, w1, w2, w3, and w4 input to the BiLSTM-CRF model are word vectors containing word embeddings and character embeddings, where word embeddings are usually pre-trained, that is, word embeddings obtained through the BGE English model, and character embeddings are randomly initialized. All embeddings will be adjusted during the training iteration process. Exemplarily, assuming that all labels of the target code are "T0, T1, T2, T3, T4, T5, T6, T7, SPACE, WRAP", if the label corresponding to a unit is T3, that is, the value corresponding to T3 is 1, and the others are all 0, the input word vector w0 of the BiLSTM-CRF model is: [0,0,0,1,0,0,0,0,0,0], that is, 1 is the word embedding, and the remaining 0 is the randomly initialized word embedding; the BiLSTM layer can output the label score corresponding to each word vector w0, w1, w2, w3, w4, and the CRF layer can output the label corresponding to each unit in each line of code, wherein the structure diagram of the BiLSTM-CRF model is as follows: Figure 2 As shown by Figure 2 As we know, "T1, SPACE, SPACE, SPACE, WRAP" with the maximum probability of 0.9 will be used as the predicted label combination of several units for subsequent code formatting.

[0100] In this way, the word vector corresponding to the target unit is input into the BiLSTM-CRF model for forward propagation to calculate the score of the label corresponding to each target unit, and the loss value of the score and the actual label is calculated to continuously optimize the BiLSTM-CRF model based on the loss value, thereby improving the accuracy of BiLSTM-CRF model prediction.

[0101] The formatting module 04 is used to format the source code based on the corresponding prediction tags after each of the units.

[0102] The formatting module 04 includes: a second replacement unit and a second deletion unit; the second replacement unit is used to replace the space or the line break after each unit in the source code with the corresponding predicted label after each unit; the second deletion unit is used to delete the start mark at the beginning of each line of code.

[0103] It can be understood that after the predicted label corresponding to each unit is predicted through step S3, the source code can be formatted; specifically: the space or the line break after each unit in the source code is replaced with the predicted label corresponding to each unit; and the start mark at the beginning of each line of code is deleted. Exemplarily, still taking the line of code "a=2" as an example, in step S2, each unit in this line of code, namely "a,=, 2", is followed by "SPACE, SPACE, WRAP". In step S3, the BiLSTM-CRF model predicts that the most likely label after "a" is "SPACE", the most likely label after "=" is "SPACE", and the most likely label after "2" is "SPACE". The "SPACE, SPACE, WRAP" after the three units "a,=, 2" are replaced with the label combination "SPACE, SPACE, SPACE" predicted in step S3. At the same time, combined with the BiLSTM-CRF model, the indentation label predicted after the starting tag Head, if it is T3, the code can be "T3, a, SPACE,=, SPACE, 2, SPACE", that is, the indentation T in front of the line of code "a=2" is 3. By deleting the starting tag Head at the beginning of each line of code, the formatted code can be obtained.

[0104] In this way, by replacing the space or line break after each unit with a predicted label, various language codes can be formatted quickly and accurately with strong versatility.

[0105] The embodiment of the present application can delete blank lines and redundant spaces in the source code by preprocessing the source code, which is convenient for subsequent code segmentation, reduces the subsequent BiLSTM-CRF model input, and thus improves the accuracy of BiLSTM-CRF model prediction; by segmenting the preprocessing results, each unit in the code can be segmented out, which is convenient for the subsequent BiLSTM-CRF model to quickly and accurately predict the label after the unit; the BiLSTM-CRF model can calculate the score of the corresponding label after each unit to accurately predict the corresponding predicted label after each unit, and format the source code based on the corresponding predicted label after each unit, which can quickly and accurately format various language codes and has strong versatility.

[0106] The specific embodiments described above further describe the purpose, technical solutions and beneficial effects of the present application in detail. It should be understood that the above description is only a specific embodiment of the present application and is not intended to limit the scope of protection of the present application. It is particularly pointed out that for those skilled in the art, any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the scope of protection of the present application.

Claims

1. A code formatting method based on BiLSTM-CRF, characterized in that: include: Obtaining source code to be formatted, and preprocessing the source code to obtain a preprocessing result; Segmenting the preprocessing result to obtain a plurality of units; Inputting a plurality of the units into the BiLSTM-CRF model, obtaining scores of labels corresponding to the units, and determining predicted labels corresponding to the units based on the scores; wherein the labels include space labels, line break labels, and indentation labels; The source code is formatted based on the corresponding predicted tags after each of the units.

2. The code formatting method based on BiLSTM-CRF according to claim 1, characterized in that The source code is preprocessed to obtain a preprocessing result, which is specifically: Traversing the source code, deleting all blank lines in the source code; Add a start sign at the beginning of each line of code in the source code; Replace multiple consecutive spaces in the source code with a single space.

3. The code formatting method based on BiLSTM-CRF according to claim 2, characterized in that The preprocessing result is segmented to obtain a plurality of units, specifically: Traversing the preprocessing results, and detecting spaces or line breaks in the preprocessing results; The preprocessing result is segmented based on the space or the line break to obtain a plurality of units.

4. The code formatting method based on BiLSTM-CRF according to claim 3, characterized in that The source code is formatted based on the corresponding predicted tags after each unit, specifically: Replace the space or line break after each unit in the source code with a corresponding predicted tag after each unit; Delete the start mark at the beginning of each line of code.

5. The code formatting method based on BiLSTM-CRF according to claim 1, characterized in that: The training process of the BiLSTM-CRF model is specifically as follows: Obtaining a target format code, and marking and segmenting the target format code to obtain a plurality of target units and actual labels behind the target units; Inputting a number of the target units into the BGE English model to obtain a word vector corresponding to each of the target units; The word vectors corresponding to several of the target units are input into the BiLSTM-CRF model to obtain the scores of the labels corresponding to each of the target units, and the loss values ​​of the scores and the actual labels are calculated to continuously optimize the BiLSTM-CRF model based on the loss values.

6. A code formatting system based on BiLSTM-CRF, characterized in that: include: Preprocessing module, segmentation module, prediction module and formatting module; The preprocessing module is used to obtain the source code to be formatted, and preprocess the source code to obtain a preprocessing result; The segmentation module is used to segment the preprocessing result to obtain a plurality of units; The prediction module is used to input a plurality of the units into the BiLSTM-CRF model, obtain the scores of the labels corresponding to the units, and determine the predicted labels corresponding to the units based on the scores; wherein the labels include space labels, line break labels and indentation labels; The formatting module is used to format the source code based on the prediction tags corresponding to each of the units.

7. The code formatting system based on BiLSTM-CRF according to claim 6, characterized in that: The pre-processing module includes: a first deletion unit, an addition unit and a first replacement unit; The first deleting unit is used to traverse the source code and delete all blank lines in the source code; The adding unit is used to add a start mark at the beginning of each line of code in the source code; The first replacement unit is used to replace multiple consecutive spaces in the source code with a single space.

8. The BiLSTM-CRF-based code formatting system according to claim 7, characterized in that: The segmentation module includes: a detection unit and a segmentation unit; The detection unit is used to traverse the preprocessing result and detect spaces or line breaks in the preprocessing result; The segmentation unit is used to segment the preprocessing result based on the space or the line break to obtain a plurality of units.

9. The BiLSTM-CRF-based code formatting system according to claim 8, characterized in that: The formatting module includes: a second replacement unit and a second deletion unit; The second replacement unit is used to replace the space or the line break after each unit in the source code with a corresponding predicted label after each unit; The second deleting unit is used to delete the start mark at the beginning of each line of code.

10. The BiLSTM-CRF-based code formatting system according to claim 6, characterized in that: The training process of the BiLSTM-CRF model is specifically as follows: Obtaining a target format code, and marking and segmenting the target format code to obtain a plurality of target units and actual labels behind the target units; Inputting a number of the target units into the BGE English model to obtain a word vector corresponding to each of the target units; The word vectors corresponding to several of the target units are input into the BiLSTM-CRF model to obtain the scores of the labels corresponding to each of the target units, and the loss values ​​of the scores and the actual labels are calculated to continuously optimize the BiLSTM-CRF model based on the loss values.