Structured code block prediction method and device based on intermediate insertion

By converting the source code into an abstract syntax tree, deleting some branch structures and training the code completion model, the problem of incomplete code structure in code completion technology is solved, and the structural integrity and normal operation of code completion are achieved.

CN119225730APending Publication Date: 2024-12-31BEIJING GUIXIN TECHNOLOGY CO LTD
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202411123184.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Priority Date
2024-07-29
Filing Date
2024-08-15
Publication Date
2024-12-31

AI Technical Summary

Technical Problem

The existing code completion technology fails to ensure the complete code structure integrity, resulting in the code not being able to run normally.

Method used

By converting the source code into an abstract syntax tree, deleting part of the branch structure, training the code completion model to predict the deleted branch structure, and ensuring the integrity of the code structure through the structured code block prediction method inserted in the middle.

Benefits of technology

The structural integrity of code completion is achieved, ensuring that the completed source code can run normally, and the prediction accuracy is improved through model optimization.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119225730A_ABST
    Figure CN119225730A_ABST
Patent Text Reader

Abstract

The invention provides a structured code block prediction method and device based on intermediate insertion. The method comprises the following steps: acquiring a plurality of source codes; for any source code in the plurality of source codes, converting the source code into a corresponding first abstract syntax tree to obtain a plurality of first abstract syntax trees in one-to-one correspondence with the plurality of source codes; for any one first abstract syntax tree in the plurality of first abstract syntax trees, deleting any one branch structure in the first abstract syntax tree to obtain a second abstract syntax tree; training the initial model according to a plurality of second abstract syntax trees in one-to-one correspondence with the plurality of first abstract syntax trees to obtain a code completion model; the code completion model is used for predicting the deleted branch structure in the second abstract syntax tree. The code completion model obtained through training in the embodiment of the invention can ensure the structural integrity of code completion, so that the purpose that the completed source code can normally run is achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the technical field of large language models, and in particular to a structured code block prediction method and device based on intermediate insertion. Background Art

[0002] With the development of large language model technology, more and more people are beginning to use large models to solve code completion tasks in software development scenarios.

[0003] Most current code completion technical solutions do not consider the structural integrity of the completed code content. For example, the completed code structure does not have a right bracket corresponding to the previous left bracket, etc., which may cause the code to fail to run due to incomplete code. Summary of the invention

[0004] In view of this, the present disclosure proposes a structured code block prediction method and device based on intermediate insertion to solve the problem in the related art that the completed code is incomplete, resulting in the code being unable to run.

[0005] The first aspect of the present disclosure proposes a structured code block prediction method based on intermediate insertion, the method comprising:

[0006] Get multiple source codes;

[0007] For any one of the multiple source codes, convert the source code into a corresponding first abstract syntax tree to obtain a plurality of first abstract syntax trees corresponding to the multiple source codes one by one; any first abstract syntax tree includes a plurality of branch structures, each branch structure includes any one node in the first abstract syntax tree except a root node, and all child nodes of the any one node;

[0008] For any one of the plurality of first abstract syntax trees, delete any branch structure in the first abstract syntax tree to obtain a second abstract syntax tree;

[0009] The initial model is trained according to a plurality of second abstract syntax trees corresponding one-to-one to the plurality of first abstract syntax trees to obtain a code completion model, wherein the code completion model is used to predict the branch structure deleted in the second abstract syntax tree.

[0010] The disclosed embodiment deletes any branch structure in the first abstract syntax tree to obtain a second abstract syntax tree; trains an initial model according to multiple second abstract syntax trees corresponding one-to-one to the multiple first abstract syntax trees to obtain a code completion model. The code completion model obtained by this model training method can ensure the structural integrity of the code completion, thereby achieving the purpose of the normal operation of the completed source code.

[0011] In an embodiment of the present disclosure, the initial model is trained according to a plurality of second abstract syntax trees corresponding one-to-one to the plurality of first abstract syntax trees to obtain a code completion model, including:

[0012] For any second abstract syntax tree among the plurality of second abstract syntax trees, convert the second abstract syntax tree into a corresponding target character sequence;

[0013] Determine the deleted branch structure of the second abstract syntax tree at a target position of the target character sequence;

[0014] According to the target position, a first character sequence and a second character sequence are determined from the target character sequence; the first character sequence represents a character sequence of a preceding portion of the target position of the target character sequence, and the second character sequence represents a character sequence of a succeeding portion of the target position of the target character sequence;

[0015] The initial model is trained to predict the character sequence of the deleted branch structure based on the first character sequence and the second character sequence.

[0016] The disclosed embodiment determines the first character sequence and the second character sequence from the target character sequence through the target position, and trains the initial model to predict the character sequence of the deleted branch structure based on the first character sequence and the second character sequence. This enables the model to consider the preceding and subsequent contexts of the code to better understand the logical flow and structure of the code, thereby optimizing the model performance and achieving the purpose of improving the prediction accuracy of the completion structure.

[0017] In an embodiment of the present disclosure, determining a first character sequence and a second character sequence from the target character sequence according to the target position includes:

[0018] taking a character sequence corresponding to a preset start character located at a front part of the target position in the target character sequence as the first character sequence;

[0019] The character sequence corresponding to the preset separator located at the post-order part of the target position in the target character sequence is used as the second character sequence.

[0020] In an embodiment of the present disclosure, the character sequence includes a plurality of characters, and the characters include at least one of the following: a keyword, an identifier, a constant, a string literal, and a special symbol.

[0021] In the embodiment of the present disclosure, after deleting any branch structure in the first abstract syntax tree to obtain the second abstract syntax tree, the method further includes:

[0022] Filtering out a first abstract syntax tree corresponding to the second abstract syntax tree from the plurality of first abstract syntax trees;

[0023] A branch structure in the first abstract syntax tree corresponding to the deleted branch structure in the second abstract syntax tree is marked.

[0024] The disclosed embodiment marks the branch structure in the first abstract syntax tree that corresponds to the deleted branch structure in the second abstract syntax tree, which helps to evaluate the completion structure prediction result of the initial model through the marked branch structure of the first abstract syntax tree after the initial model is trained.

[0025] In an embodiment of the present disclosure, after training the initial model to predict the character sequence of the deleted branch structure according to the first character sequence and the second character sequence, the method further includes:

[0026] Inputting a first character sequence and a second character sequence corresponding to any second abstract syntax tree into the initial model to obtain a character sequence of a completed branch structure output by the initial model;

[0027] If the character sequence of the completed branch structure is the same as the character sequence of the marked branch structure in the target first abstract syntax tree, the initial model is determined as the code completion model; wherein the target first abstract syntax tree represents the first abstract syntax tree among the multiple first abstract syntax trees corresponding to any one of the second abstract syntax trees.

[0028] In an embodiment of the present disclosure, the method further includes:

[0029] If the character sequence of the completed branch structure is different from the character sequence of the marked branch structure in the target first abstract syntax tree, the initial model is optimized according to a plurality of second abstract syntax trees corresponding one-to-one to the plurality of first abstract syntax trees.

[0030] The disclosed embodiment can effectively evaluate the code completion effect of the model by comparing the character sequence of the completed branch structure output by the initial model with the character sequence of the annotated branch structure in the target first abstract syntax tree to ensure the output accuracy of the completed code; and, under the condition that the character sequence of the completed branch structure output by the model is different from the character sequence of the annotated branch structure in the target first abstract syntax tree, the initial model is optimized to adjust the model parameters so that the model outputs the completed code that meets the expected requirements.

[0031] The second aspect of the present disclosure provides a programming method, including:

[0032] Convert the source code to be completed into an abstract syntax tree to be completed;

[0033] Determine a first character sequence and a second character sequence from a target character sequence corresponding to the abstract syntax tree to be completed; the first character sequence represents a character sequence of a pre-order part of a target position of the target character sequence, the second character sequence represents a character sequence of a post-order part of the target position of the target character sequence, and the target position represents a position of a branch structure to be completed of the abstract syntax tree to be completed in the target character sequence;

[0034] Inputting the first character sequence and the second character sequence into a code completion model of the embodiment of the first aspect, and outputting a third sequence of a completion branch structure; the completion branch structure corresponds to the branch structure to be completed;

[0035] The abstract syntax tree to be completed is completed based on the third sequence, and the completed abstract syntax tree is converted into executable source code.

[0036] The embodiment of the third aspect of the present disclosure provides a structured code block prediction device based on intermediate insertion, comprising:

[0037] A code acquisition module is used to acquire multiple source codes;

[0038] a syntax tree conversion module, configured to convert any one of the multiple source codes into a corresponding first abstract syntax tree, so as to obtain a plurality of first abstract syntax trees corresponding to the multiple source codes one by one; any first abstract syntax tree includes a plurality of branch structures, each branch structure includes any one node in the first abstract syntax tree except a root node, and all child nodes of the any one node;

[0039] A branch structure deletion module, configured to delete any branch structure in any first abstract syntax tree among the plurality of first abstract syntax trees, to obtain a second abstract syntax tree;

[0040] A model training module is used to train an initial model according to multiple second abstract syntax trees corresponding one by one to the multiple first abstract syntax trees to obtain a code completion model, wherein the code completion model is used to predict the branch structure deleted in the second abstract syntax tree.

[0041] An embodiment of a fourth aspect of the present disclosure provides a programming device, including:

[0042] A syntax tree conversion module is used to convert the source code to be completed into an abstract syntax tree to be completed;

[0043] A sequence determination module, configured to determine a first character sequence and a second character sequence from a target character sequence corresponding to the abstract syntax tree to be completed; the first character sequence represents a character sequence of a pre-order part of a target position of the target character sequence, the second character sequence represents a character sequence of a post-order part of the target position of the target character sequence, and the target position represents a position of a branch structure to be completed of the abstract syntax tree to be completed in the target character sequence;

[0044] a third sequence output module, configured to input the first character sequence and the second character sequence into a code completion model of the embodiment of the first aspect, and output a third sequence of a completion branch structure; the completion branch structure corresponds to the branch structure to be completed;

[0045] A completion module is used to complete the abstract syntax tree to be completed based on the third sequence, and convert the completed abstract syntax tree into executable source code.

[0046] An embodiment of the fifth aspect of the present disclosure provides an electronic device, which includes a memory and a processor, wherein the memory and the processor are communicatively connected to each other, the memory stores computer instructions, and the processor executes the structured code block prediction method based on intermediate insertion described in the first aspect by executing the computer instructions.

[0047] An embodiment of the sixth aspect of the present disclosure provides a computer-readable storage medium, on which computer instructions are stored, and the computer instructions are used to enable a computer to execute the structured code block prediction method based on intermediate insertion described in the first aspect above.

[0048] Additional aspects and advantages of the present disclosure will be given in part in the following description and in part will become apparent from the following description or be learned through practice of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS

[0049] By reading the detailed description of the preferred embodiment below, various other advantages and benefits will become clear to those of ordinary skill in the art. The accompanying drawings are only used for the purpose of illustrating the preferred embodiment and are not considered to be limitations of the present disclosure. In addition, the same reference symbols are used to represent the same components throughout the accompanying drawings.

[0050] In the attached picture:

[0051] Figure 1 A schematic flow chart of a structured code block prediction method based on intermediate insertion provided by an embodiment of the present disclosure is shown;

[0052] Figure 2 A schematic diagram showing a branch structure of an abstract syntax tree provided by an embodiment of the present disclosure;

[0053] Figure 3 A schematic flow chart of another structured code block prediction method based on intermediate insertion provided by an embodiment of the present disclosure is shown;

[0054] Figure 4 Shows Figure 1 A flowchart of a specific implementation of step S104 in the embodiment;

[0055] Figure 5 A schematic flow chart of another structured code block prediction method based on intermediate insertion provided by an embodiment of the present disclosure is shown;

[0056] Figure 6 A schematic diagram of the structure of a structured code block prediction device based on intermediate insertion provided by an embodiment of the present disclosure is shown;

[0057] Figure 7 A schematic diagram of the structure of an electronic device provided by an embodiment of the present disclosure is shown;

[0058] Figure 8 A schematic diagram of a storage medium provided by an embodiment of the present disclosure is shown. DETAILED DESCRIPTION

[0059] The exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments described herein. On the contrary, these embodiments are provided in order to enable a more thorough understanding of the present disclosure and to fully convey the scope of the present disclosure to those skilled in the art.

[0060] It should be noted that, unless otherwise specified, the technical terms or scientific terms used in the present disclosure should have the common meanings understood by those skilled in the art to which the present disclosure belongs.

[0061] According to an embodiment of the present disclosure, an embodiment of a structured code block prediction method based on intermediate insertion is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.

[0062] In this embodiment, a structured code block prediction method based on intermediate insertion is provided. Figure 1 is a flow chart of a structured code block prediction method based on intermediate insertion according to an embodiment of the present disclosure, such as Figure 1 As shown, the process includes the following steps:

[0063] Step S101, obtaining multiple source codes.

[0064] Specifically, the multiple source codes obtained are not specifically limited here, and the corresponding source codes can be selected according to actual needs. For example: the multiple source codes obtained can come from different technical fields or application scenarios, respectively, so that the code completion model finally trained has universality, that is: the code completion model can perform code completion operations on source codes to be completed in different technical fields or application scenarios. For another example: the multiple source codes obtained can belong to the same technical field or application scenario, so that the code completion model finally trained has domain specificity, that is: the code completion model can better understand and predict code patterns and structures in a specific technical field, and improve the accuracy of code completion.

[0065] Step S102: for any one of the multiple source codes, convert the source code into a corresponding first abstract syntax tree, so as to obtain multiple first abstract syntax trees corresponding to the multiple source codes one by one.

[0066] In some specific embodiments, in the process of converting the source code into the corresponding first abstract syntax tree, the source code can be first decomposed into multiple characters, such as keywords, identifiers, operators, etc., and a character sequence is formed by multiple characters; then, through a syntax analyzer, the character sequence is converted into a nested syntax structure, namely, an abstract syntax tree (AST), wherein the syntax analyzer constructs the AST according to the syntax rules of the programming language.

[0067] In some specific embodiments, each first abstract syntax tree includes multiple branch structures, and each branch structure includes any node except the root node in the first abstract syntax tree, and all child nodes of any node.

[0068] Step S103: for any first abstract syntax tree among the plurality of first abstract syntax trees, delete any branch structure in the first abstract syntax tree to obtain a second abstract syntax tree.

[0069] Specifically, the first abstract syntax tree refers to the abstract syntax tree corresponding to the source code that is complete and does not require code completion, and the second syntax tree refers to the abstract syntax tree corresponding to the source code that is incomplete and requires code completion, and the abstract syntax tree lacks any branch structure, such as Figure 2 The "201" or "202" in the

[0070] Through the above steps S101 to S103, the disclosed embodiment can obtain multiple first abstract syntax trees and multiple second abstract syntax trees, which are in a one-to-one correspondence, for example: the first abstract syntax tree 1 corresponds to the second abstract syntax tree 1, the first abstract syntax tree 2 corresponds to the second abstract syntax tree 2, and the first abstract syntax tree 3 corresponds to the second abstract syntax tree 3. Among them, there is a marked branch structure in each second abstract syntax tree, and the deleted branch structure in the first abstract syntax tree corresponding to the above second abstract syntax tree corresponds to the marked branch structure. For example: the first abstract syntax tree 1 corresponds to the second abstract syntax tree 1, wherein the deleted branch structure N in the first abstract syntax tree 1 corresponds to the marked branch structure N in the second abstract syntax tree 1.

[0071] In some specific embodiments, Figure 3 As shown, after the above step S103, the method further includes steps 301-302:

[0072] Step 301: Filter out a first abstract syntax tree corresponding to the second abstract syntax tree from the multiple first abstract syntax trees.

[0073] Step 302: Mark the branch structure in the first abstract syntax tree that corresponds to the deleted branch structure in the second abstract syntax tree.

[0074] In the above steps 301-302, for any second abstract syntax tree, it is necessary to screen out a first abstract syntax tree corresponding to the second abstract syntax tree from multiple first abstract syntax trees; then, according to the location of the deleted branch structure in the second abstract syntax tree, the corresponding branch structure in the above first abstract syntax tree is annotated. The present application annotates the branch structure corresponding to the deleted branch structure in the first abstract syntax tree, which helps to evaluate the output result of the code completion model through the annotated branch structure of the first abstract syntax tree after model training, so as to evaluate the completion effect of the code completion model.

[0075] Step S104: training the initial model according to the plurality of second abstract syntax trees corresponding one-to-one to the plurality of first abstract syntax trees to obtain a code completion model, wherein the code completion model is used to predict the branch structure deleted in the second abstract syntax tree.

[0076] In the disclosed embodiment, the deleted branch structure in the second abstract syntax tree predicted by the code completion model can be understood as an intermediately inserted structured code block, and the second abstract syntax tree is obtained by deleting any branch structure in the first abstract syntax tree; and the character sequence corresponding to the deleted branch structure is predicted based on the first character sequence of the preceding part and the second character sequence of the succeeding part of the deleted branch structure in the second abstract syntax tree, thereby predicting the deleted branch structure in the second abstract syntax tree.

[0077] In some specific embodiments, Figure 4 As shown, the above step S104 includes steps S1041 to S1044:

[0078] Step S1041: for any second abstract syntax tree among the plurality of second abstract syntax trees, convert the second abstract syntax tree into a corresponding target character sequence.

[0079] Specifically, the tree structure can be converted into a character sequence using a corresponding preset algorithm according to the structure of the syntax tree. For example, if the syntax tree represents an expression, the conversion algorithm will generate a corresponding mathematical expression string.

[0080] In some specific embodiments, each character sequence includes a plurality of characters, and the characters include at least one of the following: a keyword, an identifier, a constant, a string literal, and a special symbol.

[0081] Step S1042: Determine the target position of the deleted branch structure of the second abstract syntax tree in the target character sequence.

[0082] Step S1043: determining a first character sequence and a second character sequence from the target character sequence according to the target position.

[0083] Specifically, the first character sequence represents a character sequence of a preceding part of a target position of the target character sequence, and the second character sequence represents a character sequence of a succeeding part of the target position of the target character sequence.

[0084] In some specific embodiments, the above step S1043 includes steps b1 and b2:

[0085] Step b1, taking the character sequence corresponding to the preset start character located at the front part of the target position in the target character sequence as the first character sequence.

[0086] Specifically, the preset start symbol may be understood as the start symbol that is closest to the target position and located before the target position, and the first character sequence may be understood as the character sequence between the preset start symbol and the target position.

[0087] Step b2: taking the character sequence corresponding to the preset separator located at the post-order part of the target position in the target character sequence as the second character sequence.

[0088] Specifically, the preset separator can be understood to be selected according to actual needs, for example, the Nth separator located after the target position is selected, and the second character sequence can be understood to be the character sequence between the preset separator and the target position.

[0089] Step S1044: training the initial model to predict the character sequence of the deleted branch structure based on the first character sequence and the second character sequence.

[0090] Specifically, the initial model can select a large language model suitable for sequence-to-sequence tasks, such as a Transformer-based model, such as GPT (Generative Pre-trained Transformer), BERT (Bidirectional Encoder Representations from Transformers), etc.

[0091] In some specific embodiments, during the model training process, a validation set can be used to evaluate the performance of the model using appropriate evaluation indicators, such as BLEU score, ROUGE score, or a custom evaluation indicator. The model can also be fine-tuned based on the evaluation results, which may include adjusting the network structure, optimizing the training process, or using more data for training.

[0092] In some specific embodiments, Figure 5 As shown, after the above step S1044, the method further includes steps S501 to S503:

[0093] Step S501: input a first character sequence and a second character sequence corresponding to any second abstract syntax tree into the initial model to obtain a character sequence of a completed branch structure output by the initial model.

[0094] Step S502: If the character sequence of the completion branch structure is the same as the character sequence of the marked branch structure in the target first abstract syntax tree, the initial model is determined as the code completion model.

[0095] Specifically, the target first abstract syntax tree represents a first abstract syntax tree among the multiple first abstract syntax trees corresponding to any one of the second abstract syntax trees.

[0096] Step S503: If the character sequence of the completed branch structure is different from the character sequence of the marked branch structure in the target first abstract syntax tree, the initial model is optimized according to a plurality of second abstract syntax trees corresponding one-to-one to the plurality of first abstract syntax trees.

[0097] The disclosed embodiment can effectively evaluate the code completion effect of the model by comparing the character sequence of the completed branch structure output by the initial model with the character sequence of the annotated branch structure in the target first abstract syntax tree to ensure the output accuracy of the completed code; and, under the condition that the character sequence of the completed branch structure output by the model is different from the character sequence of the annotated branch structure in the target first abstract syntax tree, the model parameters are adjusted through an optimization process so that the model outputs the completed code that meets the expected requirements.

[0098] Corresponding to the implementation of the above structured code block prediction method based on intermediate insertion, the embodiment of the present disclosure further provides a programming method, including:

[0099] Convert the source code to be completed into an abstract syntax tree to be completed;

[0100] Determine a first character sequence and a second character sequence from a target character sequence corresponding to the abstract syntax tree to be completed; the first character sequence represents a character sequence of a pre-order part of a target position of the target character sequence, the second character sequence represents a character sequence of a post-order part of the target position of the target character sequence, and the target position represents a position of a branch structure to be completed of the abstract syntax tree to be completed in the target character sequence;

[0101] Inputting the first character sequence and the second character sequence into the code completion model of the structured code block prediction method based on intermediate insertion described in the above embodiment, and outputting a third sequence of completion branch structures; the completion branch structure corresponds to the branch structure to be completed;

[0102] The abstract syntax tree to be completed is completed based on the third sequence, and the completed abstract syntax tree is converted into executable source code.

[0103] Corresponding to the implementation of the above structured code block prediction method based on intermediate insertion, the embodiment of the present disclosure also provides a structured code block prediction device based on intermediate insertion, which is used to perform the above Figures 1 to 5 The structured code block prediction method based on intermediate insertion described in any of the illustrated embodiments. Figure 6As shown, the structured code block prediction device based on intermediate insertion includes:

[0104] A code acquisition module is used to acquire multiple source codes;

[0105] a syntax tree conversion module, configured to convert any one of the multiple source codes into a corresponding first abstract syntax tree, so as to obtain a plurality of first abstract syntax trees corresponding to the multiple source codes one by one; any first abstract syntax tree includes a plurality of branch structures, each branch structure includes any one node in the first abstract syntax tree except a root node, and all child nodes of the any one node;

[0106] A branch structure deletion module, configured to delete any branch structure in any first abstract syntax tree among the plurality of first abstract syntax trees, to obtain a second abstract syntax tree;

[0107] A model training module is used to train an initial model according to multiple second abstract syntax trees corresponding one by one to the multiple first abstract syntax trees to obtain a code completion model, wherein the code completion model is used to predict the branch structure deleted in the second abstract syntax tree.

[0108] Optionally, the model training module is also used to: for any one of the multiple second abstract syntax trees, convert the second abstract syntax tree into a corresponding target character sequence; determine the target position of the deleted branch structure of the second abstract syntax tree in the target character sequence; according to the target position, determine a first character sequence and a second character sequence from the target character sequence; the first character sequence represents a character sequence of a preceding part of the target position of the target character sequence, and the second character sequence represents a character sequence of a post-order part of the target position of the target character sequence; train the initial model to predict the character sequence of the deleted branch structure based on the first character sequence and the second character sequence.

[0109] Optionally, the model training module is also used to: use the character sequence corresponding to the preset start character in the target character sequence located in the front part of the target position as the first character sequence; use the character sequence corresponding to the preset spacer in the target character sequence located in the back part of the target position as the second character sequence.

[0110] Optionally, the character sequence includes multiple characters, and the characters include at least one of the following: keywords, identifiers, constants, string literals, and special symbols.

[0111] Optionally, the device also includes: a marking module, used to filter out a first abstract syntax tree corresponding to the second abstract syntax tree from the multiple first abstract syntax trees; and mark the branch structure in the first abstract syntax tree corresponding to the deleted branch structure of the second abstract syntax tree.

[0112] Optionally, the device also includes: a model testing module used to: input the first character sequence and the second character sequence corresponding to any second abstract syntax tree into the initial model to obtain the character sequence of the completed branch structure output by the initial model; if the character sequence of the completed branch structure is the same as the character sequence of the marked branch structure in the target first abstract syntax tree, then determine the initial model as the code completion model; wherein the target first abstract syntax tree represents the first abstract syntax tree among the multiple first abstract syntax trees corresponding to the any second abstract syntax tree; if the character sequence of the completed branch structure is different from the character sequence of the marked branch structure in the target first abstract syntax tree, then optimize the initial model according to the multiple second abstract syntax trees that correspond one-to-one to the multiple first abstract syntax trees.

[0113] The structured code block prediction device based on intermediate insertion provided in the above-mentioned embodiment of the present disclosure and the structured code block prediction method based on intermediate insertion provided in the embodiment of the present disclosure are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by the application programs stored therein.

[0114] Corresponding to the implementation of the above programming method, the embodiment of the present disclosure further provides a programming device, including:

[0115] A syntax tree conversion module is used to convert the source code to be completed into an abstract syntax tree to be completed;

[0116] A sequence determination module, configured to determine a first character sequence and a second character sequence from a target character sequence corresponding to the abstract syntax tree to be completed; the first character sequence represents a character sequence of a pre-order part of a target position of the target character sequence, the second character sequence represents a character sequence of a post-order part of the target position of the target character sequence, and the target position represents a position of a branch structure to be completed of the abstract syntax tree to be completed in the target character sequence;

[0117] A third sequence output module is used to input the first character sequence and the second character sequence into the code completion model of the structured code block prediction method based on intermediate insertion described in the above embodiment, and output a third sequence of a completed branch structure; the completed branch structure corresponds to the branch structure to be completed;

[0118] A completion module is used to complete the abstract syntax tree to be completed based on the third sequence, and convert the completed abstract syntax tree into executable source code.

[0119] The disclosed embodiment also provides an electronic device to perform the above structured code block prediction method based on intermediate insertion. Figure 7 , which shows a schematic diagram of an electronic device provided by some embodiments of the present disclosure. Figure 7 As shown, the electronic device 7 includes: a processor 700, a memory 701, a bus 702 and a communication interface 703, wherein the processor 700, the communication interface 703 and the memory 701 are connected via the bus 702; the memory 701 stores a computer program that can be run on the processor 700, and the processor 700 executes the aforementioned Figures 1 to 5 Any of the illustrated embodiments provides a structured code block prediction method based on intermediate insertion.

[0120] The memory 701 may include a high-speed random access memory (RAM), and may also include a non-volatile memory (non-volatile memory), such as at least one disk storage. The communication connection between the system network element and at least one other network element is realized through at least one communication interface 703 (which may be wired or wireless), and the Internet, wide area network, local area network, metropolitan area network, etc. may be used.

[0121] The bus 702 may be an ISA bus, a PCI bus, or an EISA bus. The bus may be divided into an address bus, a data bus, a control bus, etc. The memory 701 is used to store programs. After receiving the execution instruction, the processor 700 executes the program. Figures 1 to 5 The structured code block prediction method based on intermediate insertion disclosed in any of the illustrated embodiments may be applied to the processor 700 or implemented by the processor 700 .

[0122] The processor 700 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the hardware integrated logic circuit in the processor 700 or the instruction in the form of software. The above processor 700 can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present disclosure can be implemented or executed. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor. The steps of the method disclosed in conjunction with the embodiments of the present disclosure can be directly embodied as a hardware decoding processor to be executed, or the hardware and software modules in the decoding processor can be executed. The software module can be located in a mature storage medium in the field such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory 701, and the processor 700 reads the information in the memory 701 and completes the steps of the above method in combination with its hardware.

[0123] The electronic device provided by the embodiment of the present disclosure and the structured code block prediction method based on intermediate insertion provided by the embodiment of the present disclosure are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by them.

[0124] The present disclosure also provides a computer-readable storage medium corresponding to the structured code block prediction method based on intermediate insertion provided in the above-mentioned embodiment. Figure 8 The computer-readable storage medium shown is a CD 30 on which a computer program (ie, a program product) is stored. When the computer program is executed by a processor, the structured code block prediction method based on intermediate insertion provided in any of the aforementioned embodiments will be executed.

[0125] It should be noted that examples of the computer-readable storage medium may also include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other optical or magnetic storage media, which are not listed here one by one.

[0126] The computer-readable storage medium provided by the above-mentioned embodiment of the present disclosure and the structured code block prediction method based on intermediate insertion provided by the embodiment of the present disclosure are based on the same inventive concept, and have the same beneficial effects as the method adopted, run or implemented by the application program stored therein.

[0127] It should be noted that:

[0128] In the description provided herein, a large number of specific details are described. However, it is understood that the embodiments of the present disclosure can be practiced without these specific details. In some instances, well-known structures and technologies are not shown in detail so as not to obscure the understanding of this description.

[0129] Similarly, it should be understood that in order to streamline the present disclosure and aid in understanding one or more of the various inventive aspects, in the above description of the exemplary embodiments of the present disclosure, the various features of the present disclosure are sometimes grouped together into a single embodiment, figure, or description thereof. However, this disclosed method should not be interpreted as reflecting that the claimed disclosure requires more features than those explicitly recited in each claim. More specifically, as reflected in the claims below, the inventive aspects lie in less than all the features of the individual embodiments disclosed above. Therefore, the claims that follow the specific embodiment are hereby expressly incorporated into the specific embodiment, with each claim itself serving as a separate embodiment of the present disclosure.

[0130] In addition, those skilled in the art will appreciate that, although some embodiments described herein include certain features included in other embodiments but not other features, the combination of features of different embodiments is meant to be within the scope of this disclosure and form different embodiments. For example, in the claims below, any one of the claimed embodiments may be used in any combination.

[0131] The above is only a preferred specific implementation of the present disclosure, but the protection scope of the present disclosure is not limited thereto. Any changes or substitutions that can be easily thought of by any technician familiar with the technical field within the technical scope disclosed in the present disclosure should be included in the protection scope of the present disclosure. Therefore, the protection scope of the present disclosure should be based on the protection scope of the claims.

Claims

1. A structured code block prediction method based on intermediate insertion, characterized in that: The method comprises: Get multiple source codes; For any one of the multiple source codes, convert the source code into a corresponding first abstract syntax tree to obtain a plurality of first abstract syntax trees corresponding to the multiple source codes one by one; any first abstract syntax tree includes a plurality of branch structures, each branch structure includes any one node in the first abstract syntax tree except a root node, and all child nodes of the any one node; For any first abstract syntax tree among the multiple first abstract syntax trees, delete any branch structure in the first abstract syntax tree to obtain a second abstract syntax tree; The initial model is trained according to a plurality of second abstract syntax trees corresponding one-to-one to the plurality of first abstract syntax trees to obtain a code completion model, wherein the code completion model is used to predict the branch structure deleted in the second abstract syntax tree.

2. The method according to claim 1, characterized in that Training the initial model according to the plurality of second abstract syntax trees corresponding one-to-one to the plurality of first abstract syntax trees to obtain a code completion model includes: For any second abstract syntax tree among the plurality of second abstract syntax trees, convert the second abstract syntax tree into a corresponding target character sequence; Determine the deleted branch structure of the second abstract syntax tree at a target position of the target character sequence; According to the target position, a first character sequence and a second character sequence are determined from the target character sequence; the first character sequence represents a character sequence of a preceding portion of the target position of the target character sequence, and the second character sequence represents a character sequence of a succeeding portion of the target position of the target character sequence; The initial model is trained to predict the character sequence of the deleted branch structure based on the first character sequence and the second character sequence.

3. The method according to claim 2, characterized in that Determining a first character sequence and a second character sequence from the target character sequence according to the target position includes: taking a character sequence corresponding to a preset start character located at a front part of the target position in the target character sequence as the first character sequence; The character sequence corresponding to the preset separator located at the post-order part of the target position in the target character sequence is used as the second character sequence.

4. The method according to claim 2 or 3, characterized in that: The character sequence includes a plurality of characters, and the characters include at least one of the following: a keyword, an identifier, a constant, a string literal, and a special symbol.

5. The method according to claim 1 or 2, characterized in that: After deleting any branch structure in the first abstract syntax tree to obtain a second abstract syntax tree, the method further includes: Filtering out a first abstract syntax tree corresponding to the second abstract syntax tree from the plurality of first abstract syntax trees; A branch structure in the first abstract syntax tree corresponding to the deleted branch structure in the second abstract syntax tree is marked.

6. The method according to claim 2 or 3, characterized in that: After training the initial model to predict the character sequence of the deleted branch structure according to the first character sequence and the second character sequence, the method further includes: Inputting a first character sequence and a second character sequence corresponding to any second abstract syntax tree into the initial model to obtain a character sequence of a completed branch structure output by the initial model; If the character sequence of the completed branch structure is the same as the character sequence of the marked branch structure in the target first abstract syntax tree, the initial model is determined as the code completion model; wherein the target first abstract syntax tree represents the first abstract syntax tree among the multiple first abstract syntax trees corresponding to any one of the second abstract syntax trees.

7. The method according to claim 6, characterized in that The method further comprises: If the character sequence of the completed branch structure is different from the character sequence of the marked branch structure in the target first abstract syntax tree, the initial model is optimized according to a plurality of second abstract syntax trees corresponding one-to-one to the plurality of first abstract syntax trees.

8. A programming method, characterized in that: The method comprises: Convert the source code to be completed into an abstract syntax tree to be completed; Determine a first character sequence and a second character sequence from a target character sequence corresponding to the abstract syntax tree to be completed; the first character sequence represents a character sequence of a pre-order part of a target position of the target character sequence, the second character sequence represents a character sequence of a post-order part of the target position of the target character sequence, and the target position represents a position of a branch structure to be completed of the abstract syntax tree to be completed in the target character sequence; Inputting the first character sequence and the second character sequence into a code completion model of the structured code block prediction method based on intermediate insertion according to any one of claims 1 to 7, and outputting a third sequence of completion branch structures; the completion branch structure corresponds to the branch structure to be completed; The abstract syntax tree to be completed is completed based on the third sequence, and the completed abstract syntax tree is converted into executable source code.

9. A structured code block prediction device based on intermediate insertion, characterized in that: The device comprises: A code acquisition module is used to acquire multiple source codes; a syntax tree conversion module, configured to convert any one of the multiple source codes into a corresponding first abstract syntax tree, so as to obtain a plurality of first abstract syntax trees corresponding to the multiple source codes one by one; any first abstract syntax tree includes a plurality of branch structures, each branch structure includes any one node in the first abstract syntax tree except a root node, and all child nodes of the any one node; A branch structure deletion module, configured to delete any branch structure in any first abstract syntax tree among the plurality of first abstract syntax trees, to obtain a second abstract syntax tree; A model training module is used to train an initial model according to multiple second abstract syntax trees corresponding one by one to the multiple first abstract syntax trees to obtain a code completion model, wherein the code completion model is used to predict the branch structure deleted in the second abstract syntax tree.

10. A programming device, characterized in that: The device comprises: A syntax tree conversion module is used to convert the source code to be completed into an abstract syntax tree to be completed; A sequence determination module, configured to determine a first character sequence and a second character sequence from a target character sequence corresponding to the abstract syntax tree to be completed; the first character sequence represents a character sequence of a pre-order part of a target position of the target character sequence, the second character sequence represents a character sequence of a post-order part of the target position of the target character sequence, and the target position represents a position of a branch structure to be completed of the abstract syntax tree to be completed in the target character sequence; A third sequence output module, configured to input the first character sequence and the second character sequence into a code completion model of the structured code block prediction method based on intermediate insertion according to any one of claims 1 to 7, and output a third sequence of a completion branch structure; the completion branch structure corresponds to the branch structure to be completed; A completion module is used to complete the abstract syntax tree to be completed based on the third sequence, and convert the completed abstract syntax tree into executable source code.

Citation Information

Patent Citations

  • Code completion method and device, computer device and storage medium

    CN110688121A

  • Code completion method based on abstract syntax tree augmented graph model

    CN113064586A

  • Chinese text automatic error correction method and device

    CN114372441A

  • Code completion method, device and equipment

    CN115291854A

  • Task processing method, code completion method, code processing model training method, information processing method based on code processing model and model training platform

    CN118363601A