A method for code snippet extraction and recombination

Through AST-based code parsing and reorganization strategies, the problems of inefficient code extraction and complex code reorganization are solved, and efficient and accurate code snippet extraction and reorganization are achieved to meet the diverse needs of users.

CN119806539BActive Publication Date: 2025-07-18TAIJI COMPUTER CORPORATION LIMITED
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510286611.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-03-12
Publication Date
2025-07-18
Estimated Expiration
2045-03-12

AI Technical Summary

Technical Problem

In the prior art, code extraction efficiency is low and abnormal occurrence is frequent, complex code reorganization is difficult to handle, and it is difficult to meet user needs.

Method used

By reading the source code based on AST and parsing it, it is converted into an abstract syntax tree (AST), and the source node annotation is performed according to the key structure and pattern, the code snippet recognition rules are set, the automatic extraction model is built, node extension recognition and extraction is performed, and the reorganization code snippets are generated in combination with the reorganization strategy, and the reorganization code snippets are optimized through preset testing strategies.

Benefits of technology

It realizes efficient and accurate code snippet extraction and reorganization, improves code utilization efficiency, and meets the diverse needs of users.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119806539B_ABST
    Figure CN119806539B_ABST
Patent Text Reader

Abstract

The present invention provides a method for code snippet extraction and recombination, which relates to the technical field of code processing. The method includes: reading the source code based on the AST and performing code parsing, converting the source code into an AST, and annotating the source nodes of the AST according to the key structures and patterns of the source code; obtaining the code snippet extraction requirements, setting the code snippet recognition rules, constructing an automatic code snippet extraction model, and performing node extension recognition and extraction on the AST based on the source node annotation results to obtain the extracted code snippets; setting the code snippet recombination strategy, calibrating the recombined code nodes in combination with the source node annotation results, and performing node extension recombination synthesis on the extracted code snippets based on the recombined code nodes to generate the recombined code snippets; performing code testing on the recombined code snippets according to the preset code testing strategy and performing iterative optimization on the node extension recognition extraction and the node extension recombination synthesis; effectively realizing the extraction and recombination of code snippets.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of code processing, and in particular to a method for code snippet extraction and recombination. Background Art

[0002] With the rapid development of the computer field, the number of codes is increasing continuously. To utilize codes efficiently, feature extraction of codes is required. The existing code extraction technology mainly extracts codes through AST, but AST often has the problems of low code extraction efficiency and abnormal extraction. In addition, as codes become more complex, to meet the usage requirements of users, code recombination processing is required. For simple codes, code recombination can be achieved through code recombination rules, while for complex codes, methods such as AST are needed for code recombination.

[0003] Therefore, the present invention provides a method for code snippet extraction and recombination. Summary of the Invention

[0004] The present invention provides a method for code snippet extraction and recombination. By reading the source code based on AST and performing code parsing, the source code is transformed into AST and source nodes are labeled for the AST according to the key structures and patterns of the source code. Obtain the requirements for code snippet extraction, set the code snippet recognition rules, construct an automatic code snippet extraction model, and perform node extension recognition and extraction on the AST based on the source node labeling results to obtain the extracted code snippets. Set the code snippet recombination strategy, calibrate the recombination code nodes in combination with the source node labeling results, and perform node extension recombination synthesis on the extracted code snippets based on the recombination code nodes to generate the recombined code snippets. Perform code testing on the recombined code snippets according to the preset code testing strategy and perform iterative optimization on the node extension recognition extraction and node extension recombination synthesis; effectively realizing the extraction and recombination of code snippets.

[0005] The present invention provides a method for code snippet extraction and recombination, including:

[0006] Step 1: Read the source code based on AST and perform code parsing, transform the source code into AST, and label the source nodes for the AST according to the key structures and patterns of the source code;

[0007] Step 2: Obtain the requirements for code snippet extraction, set the code snippet recognition rules, construct an automatic code snippet extraction model, and perform node extension recognition and extraction on the AST based on the source node labeling results to obtain the extracted code snippets;

[0008] Step 3: Set the code snippet recombination strategy, calibrate the recombination code nodes in combination with the source node labeling results, and perform node extension recombination synthesis on the extracted code snippets based on the recombination code nodes to generate the recombined code snippets;

[0009] Step 4: Conduct code testing on the recombined code snippets according to the preset code testing strategy, and perform iterative optimization on node extension recognition extraction and node extension recombination synthesis.

[0010] According to a method for code snippet extraction and recombination provided by the present invention, the source code is read based on the AST and code parsing is performed. The source code is transformed into an AST, and source node annotation is performed on the AST according to the key structures and patterns of the source code, including:

[0011] Based on the source code, select an AST parsing library and the built-in syntax analysis tool of the programming language to read and parse the source code, and transform the source code into an AST;

[0012] Perform recursive traversal on the AST to obtain the key structures and patterns of the source code and perform source node annotation in the AST. Among them, the source node annotation includes: source node structure annotation and source node pattern annotation.

[0013] According to a method for code snippet extraction and recombination provided by the present invention, obtain the code snippet extraction requirements, including:

[0014] Obtain the code snippet extraction requirements, parse the code snippet extraction requirements, and clarify the code snippet extraction requirements;

[0015] Set the clear category of code snippet extraction. When the code snippet extraction requirement is the clear category of code snippet extraction, it is determined that the code extraction requirement is clearly completed. Among them, the clear category of code snippet extraction includes: code snippet type, code category, code source, code function, and code arrangement;

[0016] Otherwise, it is determined that the code extraction requirement is not clear. Generate clear options for the code extraction requirement according to the clear category of code snippet extraction, and perform clear processing on the code snippet extraction requirement.

[0017] According to a method for code snippet extraction and recombination provided by the present invention, set the code snippet recognition rules, construct an automatic code snippet extraction model, and perform node extension recognition extraction on the AST based on the source node annotation results to obtain the extracted code snippets, including:

[0018] Obtain the historical source code and historical code snippet extraction data, and set the code snippet recognition rules;

[0019] Optimize the code snippet recognition rules based on the code snippet extraction requirements. Among them, the optimization processing includes: rule selection optimization processing, rule adjustment optimization processing, and rule supplement optimization processing;

[0020] Construct an automatic code snippet extraction model according to the historical source code, historical code snippet extraction data, and the code snippet recognition rules completed by the optimization processing;

[0021] The automatic extraction model for code snippets identifies and extracts the AST to obtain a code snippet once.

[0022] The AST is identified and extracted for node expansion according to the source node annotation result.

[0023] The automatic extraction model for code snippets identifies and extracts the AST at the source node annotation result to obtain a node code snippet.

[0024] The automatic extraction model for code snippets identifies and extracts the AST other than the source node annotation result to obtain a pending code snippet.

[0025] The node code snippet is expanded for nodes according to the code snippet recognition rule after optimization processing, and the pending code snippet and the node code snippet are processed for fragment organization. Among them, the fragment organization processing includes: fragment merging organization processing, fragment conversion organization processing, and fragment independent organization processing.

[0026] The pending code snippet is successively processed for fragment organization according to the node code snippet. When all code snippets have been processed for fragment organization, it is determined that the node expansion identification and extraction is completed, and a secondary code snippet is obtained.

[0027] If the secondary code snippet is the same as the primary code snippet, the extraction code snippet is obtained according to the primary code snippet.

[0028] If the secondary code snippet is different from the primary code snippet, the extraction code snippet is obtained according to the secondary code snippet.

[0029] According to a code snippet extraction and recombination method provided by the present invention, the automatic extraction model for code snippets identifies and extracts the AST, and further includes:

[0030] Obtain the recognition and extraction time when the automatic extraction model for code snippets identifies and extracts the AST, and record it as the current recognition and extraction time.

[0031] Obtain the recognition and extraction time threshold according to the historical code snippet extraction data. If the current recognition and extraction time exceeds the recognition and extraction time threshold, it is determined that the AST scale is exceeded, and the AST is cut according to the source node annotation result.

[0032] The cut AST is sequentially input into the automatic extraction model for code snippets for recognition and extraction, and the current recognition and extraction time is obtained again. If the current recognition and extraction time obtained again does not exceed the recognition and extraction time threshold, it is determined that the cut processing is effective.

[0033] Otherwise, the AST is cut according to the code snippet recognition rule after optimization processing.

[0034] A method for code snippet extraction and recombination provided by the present invention sets a code snippet recombination strategy, calibrates recombination code nodes in combination with source node annotation results, and performs node expansion recombination synthesis on the extracted code snippets based on the recombination code nodes to generate recombined code snippets, including:

[0035] Set a code snippet recombination strategy according to the code snippet recombination requirements, where the code snippet recombination strategy includes: code snippet recombination type strategy, code snippet recombination target strategy, and code snippet recombination organization strategy;

[0036] Calibrate recombination code nodes according to the code snippet recombination strategy and the source node annotation results;

[0037] Perform node expansion recombination synthesis on the extracted code snippets according to the code snippet recombination strategy and the recombination code nodes;

[0038] Recombine the extracted code snippets at the recombination code nodes;

[0039] Recombine the extracted code snippets other than the recombination code nodes and perform recombination recognition according to the code snippet recombination type strategy;

[0040] Expand and synthesize the extracted code snippets with valid recombination recognition and the extracted code snippets at the recombination code nodes to complete node expansion recombination synthesis and generate recombined code snippets.

[0041] A method for code snippet extraction and recombination provided by the present invention performs code testing on the recombined code snippets according to a preset code testing strategy and iteratively optimizes the node expansion recognition extraction and node expansion recombination synthesis, including:

[0042] Preset a code testing strategy and perform code testing on the recombined code snippets according to the preset code testing strategy;

[0043] Evaluate the code testing results according to the preset code testing metrics to obtain a code testing evaluation value, where the preset code testing metrics include: code testing accuracy metric, code testing performance metric, and code testing readability metric;

[0044] When the code testing evaluation value is not lower than the preset code testing value, it is determined that the code snippet extraction and recombination are effectively completed;

[0045] When the code testing evaluation value is lower than the preset code testing value, reset the code snippet recognition rules for code snippet extraction and reset the code snippet recombination strategy for code snippet recombination;

[0046] Iteratively optimize the node expansion recognition extraction and node expansion recombination synthesis according to the code snippet extraction process and the code snippet recombination process.

[0047] A method for code snippet extraction and recombination provided by the present invention further includes:

[0048] Visualize the code snippet extraction result and the code snippet recombination result.

[0049] Compared with the prior art, the beneficial effects of the present application are as follows:

[0050] By reading the source code based on the AST and performing code parsing, the source code is converted into an AST and source nodes are labeled for the AST according to the key structures and patterns of the source code; obtaining the code snippet extraction requirements, setting the code snippet recognition rules, constructing an automatic code snippet extraction model, and performing node expansion recognition and extraction on the AST based on the source node labeling results to obtain the extracted code snippets; setting the code snippet recombination strategy, calibrating the recombined code nodes in combination with the source node labeling results, and performing node expansion recombination synthesis on the extracted code snippets based on the recombined code nodes to generate recombined code snippets; performing code testing on the recombined code snippets according to the preset code testing strategy and iteratively optimizing the node expansion recognition extraction and node expansion recombination synthesis; effectively realizing the extraction and recombination of code snippets.

[0051] Other features and advantages of the present invention will be described in the subsequent specification, and, in part, will become apparent from the specification, or will be understood by implementing the present invention. The objectives and other advantages of the present invention can be achieved and obtained through the structures specifically pointed out in the written specification and the drawings.

[0052] The technical solution of the present invention will be further described in detail below through the drawings and embodiments. BRIEF DESCRIPTION OF THE DRAWINGS

[0053] The drawings are used to provide a further understanding of the present invention and constitute a part of the specification. They are used together with the embodiments of the present invention to explain the present invention and do not constitute a limitation to the present invention. In the drawings:

[0054] Figure 1 is a schematic flowchart of a method for code snippet extraction and recombination provided by an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0055] The following describes the preferred embodiments of the present invention with reference to the drawings. It should be understood that the preferred embodiments described herein are only for the purpose of illustrating and explaining the present invention and are not used to limit the present invention.

[0056] Embodiment 1:

[0057] An embodiment of the present invention provides a method for code snippet extraction and recombination, as Figure 1 shown, including:

[0058] Step 1: Read the source code based on the AST and perform code parsing, convert the source code into an AST, and annotate the source nodes of the AST according to the key structures and patterns of the source code;

[0059] Step 2: Obtain the code snippet extraction requirements, set the code snippet recognition rules, build an automatic code snippet extraction model, and perform node expansion recognition and extraction on the AST based on the source node annotation results to obtain the extracted code snippets;

[0060] Step 3: Set the code snippet recombination strategy, calibrate the recombined code nodes in combination with the source node annotation results, and perform node expansion recombination synthesis on the extracted code snippets based on the recombined code nodes to generate recombined code snippets;

[0061] Step 4: Perform code testing on the recombined code snippets according to the preset code testing strategy, and perform iterative optimization on the node expansion recognition extraction and node expansion recombination synthesis.

[0062] In this embodiment, the AST refers to the Abstract Syntax Tree.

[0063] In this embodiment, the source code refers to the code that needs to be extracted and recombined with code snippets.

[0064] In this embodiment, the source node annotation refers to the annotation performed on the AST according to the key structures and patterns of the source code, which is used to represent nodes.

[0065] In this embodiment, the code snippet extraction requirements refer to the requirements for extracting code snippets from the source code.

[0066] In this embodiment, the code snippet recognition rules refer to the rules used to recognize code snippets.

[0067] In this embodiment, the automatic code snippet extraction model refers to a model used to automatically extract code from the source code.

[0068] In this embodiment, the node expansion recognition extraction refers to the extraction process performed on the AST according to the source node annotation results.

[0069] In this embodiment, the extracted code snippets refer to the code snippets obtained by performing code snippet extraction on the source code snippets.

[0070] In this embodiment, the code snippet recombination strategy refers to the strategy for recombining the extracted code snippets.

[0071] In this embodiment, the recombined code nodes refer to the nodes calibrated according to the code snippet recombination strategy and the source node annotation results.

[0072] In this embodiment, the node expansion recombination synthesis refers to the process performed on the extracted code snippets according to the code snippet recombination strategy and the recombined code nodes.

[0073] In this embodiment, the recombined code segment refers to the code segment obtained by recombining the extracted code segment.

[0074] In this embodiment, the preset code test strategy is a strategy preset for testing the results of code segment extraction and recombination.

[0075] The working principle and beneficial effects of the above technical solution are as follows: By reading the source code based on the AST and performing code parsing, the source code is converted into an AST, and source node annotation is performed on the AST according to the key structures and patterns of the source code; obtaining the requirements for code segment extraction, setting the code segment recognition rules, constructing an automatic code segment extraction model, and performing node expansion recognition and extraction on the AST based on the source node annotation results to obtain the extracted code segments; setting the code segment recombination strategy, calibrating the recombined code nodes in combination with the source node annotation results, and performing node expansion recombination synthesis on the extracted code segments based on the recombined code nodes to generate recombined code segments; performing code testing on the recombined code segments according to the preset code test strategy and performing iterative optimization on the node expansion recognition extraction and node expansion recombination synthesis; effectively realizing the extraction and recombination of code segments.

[0076] Embodiment 2:

[0077] An embodiment of the present invention provides a method for code segment extraction and recombination. By reading the source code based on the AST and performing code parsing, the source code is converted into an AST, and source node annotation is performed on the AST according to the key structures and patterns of the source code, including:

[0078] Based on the source code, select an AST parsing library and the built-in syntax analysis tool of the programming language, read and parse the source code, and convert the source code into an AST;

[0079] Perform a recursive traversal of the AST, obtain the key structures and patterns of the source code, and perform source node annotation in the AST. Among them, the source node annotation includes: source node structure annotation and source node pattern annotation.

[0080] In this embodiment, the AST refers to the Abstract Syntax Tree.

[0081] In this embodiment, the AST parsing library refers to a database used for parsing the source code.

[0082] In this embodiment, the syntax analysis tool refers to a tool used for syntax parsing of the source code to ensure that the subsequent obtained code segments conform to the syntax and avoid code segment errors.

[0083] In this embodiment, the source code refers to the code that needs to be extracted and recombined for code segments.

[0084] In this embodiment, the key structures include, for example, the connection structure.

[0085] In this embodiment, the source node annotation refers to the annotation performed on the AST based on the key structures and patterns of the source code, which is used to represent nodes.

[0086] The working principle and beneficial effects of the above technical solution are as follows: Read the source code based on the AST and perform code parsing, convert the source code into the AST, and perform source node annotation on the AST according to the key structures and patterns of the source code, laying a foundation for subsequent code snippet extraction and code snippet recombination.

[0087] Embodiment 3:

[0088] The embodiment of the present invention provides a code snippet extraction and recombination method, which obtains the code snippet extraction requirements, including:

[0089] Obtain the code snippet extraction requirements, parse the code snippet extraction requirements, and clarify the code snippet extraction requirements;

[0090] Set the clear category of code snippet extraction. When the code snippet extraction requirement is the clear category of code snippet extraction, it is determined that the code extraction requirement is clearly completed, where the clear category of code snippet extraction includes: code snippet type, code category, code source, code function, and code arrangement;

[0091] Otherwise, it is determined that the code extraction requirement is not clear, generate clear options for the code extraction requirement according to the clear category of code snippet extraction, and perform clear processing on the code snippet extraction requirement.

[0092] In this embodiment, the code snippet extraction requirement refers to the requirement for extracting snippets from the source code. For example, extract the code of type a1 to generate code snippets.

[0093] In this embodiment, the clear category of code snippet extraction refers to the category of specific code snippet extraction requirements, that is, the category that precisely realizes code snippet extraction, and does not cause code extraction anomalies due to unclear code snippet extraction requirements.

[0094] In this embodiment, the clear option for code extraction requirement, such as the clear option for code extraction type requirement, is used to clarify the code snippet extraction requirement.

[0095] The working principle and beneficial effects of the above technical solution are as follows: Obtaining the code snippet extraction requirements is beneficial to subsequent code snippet extraction.

[0096] Embodiment 4:

[0097] An embodiment of the present invention provides a method for code snippet extraction and recombination. By setting code snippet recognition rules, constructing an automatic code snippet extraction model, and performing node expansion recognition and extraction on the AST based on the source node annotation results, the extracted code snippets are obtained, including:

[0098] Obtain historical source code and historical code snippet extraction data, and set code snippet recognition rules;

[0099] Optimize the code snippet recognition rules based on the code snippet extraction requirements. Among them, the optimization process includes: rule selection optimization, rule adjustment optimization, and rule supplementation optimization;

[0100] Construct an automatic code snippet extraction model according to the historical source code, historical code snippet extraction data, and the code snippet recognition rules completed by the optimization process;

[0101] Perform recognition and extraction on the AST according to the automatic code snippet extraction model to obtain the first code snippets;

[0102] Perform node expansion recognition and extraction on the AST according to the source node annotation results;

[0103] Perform recognition and extraction on the AST at the source node annotation results according to the automatic code snippet extraction model to obtain node code snippets;

[0104] Perform recognition and extraction on the AST other than the source node annotation results according to the automatic code snippet extraction model to obtain pending code snippets;

[0105] Perform node expansion on the node code snippets according to the code snippet recognition rules completed by the optimization process, and perform fragment organization processing on the pending code snippets and the node code snippets. Among them, the fragment organization processing includes: fragment merging organization processing, fragment conversion organization processing, and fragment independent organization processing;

[0106] Perform fragment organization processing on the pending code snippets one by one according to the node code snippets. When all code snippets have undergone fragment organization processing, it is determined that the node expansion recognition and extraction is completed, and the second code snippets are obtained;

[0107] If the second code snippets are the same as the first code snippets, obtain the extracted code snippets according to the first code snippets;

[0108] If the second code snippets are different from the first code snippets, obtain the extracted code snippets according to the second code snippets.

[0109] In this embodiment, the historical source code refers to the source code in a historical period.

[0110] In this embodiment, the historical code snippet extraction data refers to the relevant data for code snippet extraction in a historical period.

[0111] In this embodiment, the code snippet recognition rule refers to the rule for recognizing code snippets.

[0112] In this embodiment, the code snippet recognition rule is optimized based on the code snippet extraction requirement, which facilitates meeting the code snippet extraction requirement and avoids invalid recognition.

[0113] In this embodiment, the code snippet automatic extraction model refers to the model for automatically extracting code from source code.

[0114] In this embodiment, a primary code snippet refers to the code snippet obtained by the code snippet automatic extraction model through recognizing and extracting the AST.

[0115] In this embodiment, node extension recognition and extraction refers to the extraction process performed on the AST according to the source node annotation result.

[0116] In this embodiment, a node code snippet refers to the code snippet obtained by recognizing and extracting the AST at the source node annotation result according to the code snippet automatic extraction model.

[0117] In this embodiment, a pending code snippet refers to the code snippet obtained by recognizing and extracting the AST other than the source node annotation result according to the code snippet automatic extraction model.

[0118] In this embodiment, node extension refers to the process performed on the node code snippet according to the optimized code snippet recognition rule, for example, merging the pending code snippet with the node code snippet.

[0119] In this embodiment, snippet organization processing refers to the process performed on the pending code snippet and the node code snippet to obtain the extracted code snippet.

[0120] In this embodiment, a secondary code snippet refers to the code snippet obtained after completing the snippet organization processing on the pending code snippet and the node code snippet, that is, the code snippet obtained after completing the node extension recognition and extraction.

[0121] In this embodiment, an extracted code snippet refers to the code snippet obtained by extracting code from the source code snippet.

[0122] The working principle and beneficial effects of the above technical solution are as follows: By setting the code snippet recognition rule, constructing the code snippet automatic extraction model and performing node extension recognition and extraction on the AST based on the source node annotation result, the extracted code snippet is obtained, effectively realizing the extraction of code snippets.

[0123] Embodiment 5:

[0124] An embodiment of the present invention provides a method for code snippet extraction and recombination. The code snippet automatic extraction model is used to identify and extract the AST, and it further includes:

[0125] Obtain the identification and extraction time of the AST by the code snippet automatic extraction model, and record it as the current identification and extraction time;

[0126] Obtain the identification and extraction time threshold according to the historical code snippet extraction data. If the current identification and extraction time exceeds the identification and extraction time threshold, it is determined that the AST scale exceeds the limit, and the AST is cut according to the source node annotation result;

[0127] Input the AST after the cutting process into the code snippet automatic extraction model in sequence for identification and extraction, and re-obtain the current identification and extraction time. If the re-obtained current identification and extraction time does not exceed the identification and extraction time threshold, it is determined that the cutting process is effective;

[0128] Otherwise, cut the AST according to the code snippet recognition rule after the optimization process.

[0129] In this embodiment, the current identification and extraction time refers to the time when the code snippet automatic extraction model identifies and extracts the AST in the current period.

[0130] In this embodiment, the identification and extraction time threshold refers to the time threshold obtained according to the historical code snippet extraction data.

[0131] In this embodiment, determining that the AST scale exceeds the limit means that the efficiency of the code snippet automatic extraction model for code extraction from the AST is low, and in addition, code extraction anomalies are likely to occur.

[0132] In this embodiment, the cutting process refers to the process of processing the AST according to the source node annotation result, which is used to ensure that the AST scale does not exceed the limit.

[0133] The working principle and beneficial effects of the above technical solution are: By obtaining the current identification and extraction time and cutting the AST according to the source node annotation result, the efficiency of data snippet extraction is ensured, and the extraction of code snippets is further realized.

[0134] Example 6:

[0135] An embodiment of the present invention provides a method for code snippet extraction and recombination. Set the code snippet recombination strategy, calibrate the recombination code nodes in combination with the source node annotation result, and perform node expansion and recombination synthesis on the extracted code snippets based on the recombination code nodes to generate the recombined code snippets, including:

[0136] Set the code snippet recombination strategy according to the code snippet recombination requirements, where the code snippet recombination strategy includes: code snippet recombination type strategy, code snippet recombination target strategy, and code snippet recombination organization strategy;

[0137] Calibrate the recombined code nodes according to the code snippet recombination strategy and the source node annotation results;

[0138] Perform node extension recombination synthesis on the extracted code snippets according to the code snippet recombination strategy and the recombined code nodes;

[0139] Recombine the extracted code snippets at the recombined code nodes;

[0140] Recombine the extracted code snippets other than the recombined code nodes and perform recombination recognition according to the code snippet recombination type strategy;

[0141] Perform extended synthesis on the effectively recognized extracted code snippets for recombination and the extracted code snippets at the recombined code nodes to complete node extension recombination synthesis and generate recombined code snippets.

[0142] In this embodiment, the code snippet recombination requirement refers to the requirement for recombining code snippets.

[0143] In this embodiment, the code snippet recombination strategy refers to the strategy for recombining code snippets.

[0144] In this embodiment, the recombined code node refers to the node calibrated according to the code snippet recombination strategy and the source node annotation results.

[0145] In this embodiment, node extension recombination synthesis refers to the processing of the extracted code snippets according to the code snippet recombination strategy and the recombined code nodes. For example, perform code snippet merging processing on the extracted code snippets at the recombined code nodes.

[0146] In this embodiment, the recombined code snippet refers to the code snippet obtained by performing node extension recombination synthesis on the extracted code snippets based on the recombined code nodes.

[0147] The working principle and beneficial effects of the above technical solution are: By setting the code snippet recombination strategy, calibrating the recombined code nodes in combination with the source node annotation results, and performing node extension recombination synthesis on the extracted code snippets based on the recombined code nodes to generate recombined code snippets, the recombination of code snippets is effectively realized.

[0148] Embodiment 7:

[0149] The embodiment of the present invention provides a method for code snippet extraction and recombination, which performs code testing on the recombined code snippets according to a preset code testing strategy and iteratively optimizes the node extension recognition extraction and node extension recombination synthesis, including:

[0150] A preset code testing strategy is used to perform code testing on the recombined code snippets according to the preset code testing strategy;

[0151] Evaluate the code testing results according to the preset code testing metrics to obtain a code testing evaluation value. Among them, the preset code testing metrics include: code testing accuracy metric, code testing performance metric, and code testing readability metric;

[0152] When the code testing evaluation value is not lower than the preset code testing value, it is determined that the extraction and recombination of the code snippets are effectively completed;

[0153] When the code testing evaluation value is lower than the preset code testing value, re-set the code snippet recognition rule for code snippet extraction and re-set the code snippet recombination strategy for code snippet recombination;

[0154] Iteratively optimize the node expansion recognition extraction and node expansion recombination synthesis according to the code snippet extraction process and the code snippet recombination process.

[0155] In this embodiment, the preset code testing strategy refers to the strategy for performing code testing on the recombined code snippets.

[0156] In this embodiment, the code testing evaluation metric refers to the metric for evaluating the code testing results.

[0157] In this embodiment, the code testing evaluation value refers to the value obtained by evaluating the code testing results according to the preset code testing metrics.

[0158] In this embodiment, the preset code testing value refers to the preset value used to determine whether the extraction and recombination of the code snippets are effectively completed.

[0159] The working principle and beneficial effects of the above technical solution are: By performing code testing on the recombined code snippets according to the preset code testing strategy and iteratively optimizing the node expansion recognition extraction and node expansion recombination synthesis, it is beneficial to perform code snippet extraction and recombination subsequently.

[0160] Embodiment 8:

[0161] The embodiment of the present invention provides a method for code snippet extraction and recombination, which further includes:

[0162] Visualize the code snippet extraction result and the code snippet recombination result.

[0163] The working principle and beneficial effects of the above technical solution are: By visualizing the code snippet extraction result and the code snippet recombination result, the user experience is ensured.

[0164] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions recorded in the foregoing embodiments, or perform equivalent replacements for some of the technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the various embodiments of the present invention.

Claims

1. A method for code snippet extraction and recombination, characterized in that Including: Step 1: Read the source code based on the AST and perform code parsing, convert the source code into an AST, and perform source node annotation on the AST according to the key structures and patterns of the source code; Step 2: Obtain the code snippet extraction requirements, set the code snippet recognition rules, build an automatic code snippet extraction model, and perform node expansion recognition extraction on the AST based on the source node annotation results to obtain the extracted code snippets; Step 3: Set the code snippet recombination strategy, calibrate the recombined code nodes in combination with the source node annotation results, and perform node expansion recombination synthesis on the extracted code snippets based on the recombined code nodes to generate recombined code snippets; Step 4: Perform code testing on the recombined code snippets according to the preset code testing strategy and perform iterative optimization on the node expansion recognition extraction and node expansion recombination synthesis; Among them, Step 2 includes: Obtain the historical source code and historical code snippet extraction data, and set the code snippet recognition rules; Perform optimization processing on the code snippet recognition rules based on the code snippet extraction requirements, where the optimization processing includes: rule selection optimization processing, rule adjustment optimization processing, and rule supplement optimization processing; Build an automatic code snippet extraction model according to the historical source code, historical code snippet extraction data, and the code snippet recognition rules after optimization processing; Perform recognition extraction on the AST according to the automatic code snippet extraction model to obtain the primary code snippets; Perform node expansion recognition extraction on the AST according to the source node annotation results; Perform recognition extraction on the AST at the source node annotation results according to the automatic code snippet extraction model to obtain the node code snippets; Perform recognition extraction on the AST other than the source node annotation results according to the automatic code snippet extraction model to obtain the pending code snippets; Perform node expansion on the node code snippets according to the code snippet recognition rules after optimization processing, and perform snippet organization processing on the pending code snippets and the node code snippets, where the snippet organization processing includes: snippet merging organization processing, snippet conversion organization processing, and snippet independent organization processing; Perform snippet organization processing on the pending code snippets one by one according to the node code snippets. When all code snippets have been subjected to snippet organization processing, it is determined that the node expansion recognition extraction is completed, and the secondary code snippets are obtained; If the secondary code snippets are the same as the primary code snippets, obtain the extracted code snippets according to the primary code snippets; If the secondary code snippets are different from the primary code snippets, obtain the extracted code snippets according to the secondary code snippets.

2. The method for code segment extraction and recombination according to claim 1, characterized in that Read the source code based on the AST and perform code parsing, convert the source code into an AST, and perform source node annotation on the AST according to the key structures and patterns of the source code, including: Select an AST parsing library and the built-in syntax analysis tool of the programming language based on the source code, read and perform code parsing on the source code, and convert the source code into an AST; Perform recursive traversal on the AST, obtain the key structures and patterns of the source code, and perform source node annotation in the AST, where the source node annotation includes: source node structure annotation and source node pattern annotation.

3. A method for code snippet extraction and recombination according to claim 1, characterized in that Obtain the code snippet extraction requirements, including: Obtain the code snippet extraction requirements, parse the code snippet extraction requirements, and clarify the code snippet extraction requirements; Set the clear category of code snippet extraction. When the code snippet extraction requirement is the clear category of code snippet extraction, it is determined that the code extraction requirement is clearly completed. Among them, the clear category of code snippet extraction includes: code snippet type, code category, code source, code function, and code arrangement; Otherwise, it is determined that the code extraction requirement is not clear. Generate clear options for the code extraction requirement according to the clear category of code snippet extraction, and perform clear processing on the code snippet extraction requirement.

4. A method for code snippet extraction and recombination according to claim 1, characterized in that Perform recognition and extraction on the AST according to the code snippet automatic extraction model, and also include: Obtain the recognition and extraction time when the code snippet automatic extraction model performs recognition and extraction on the AST, and record it as the current recognition and extraction time; Obtain the recognition and extraction time threshold according to the historical code snippet extraction data. If the current recognition and extraction time exceeds the recognition and extraction time threshold, it is determined that the AST scale is exceeded, and the AST is cut according to the source node annotation result; Input the AST after the cutting process into the code snippet automatic extraction model in sequence for recognition and extraction, and re-obtain the current recognition and extraction time. If the re-obtained current recognition and extraction time does not exceed the recognition and extraction time threshold, it is determined that the cutting process is effective; Otherwise, cut the AST according to the code snippet recognition rule after the optimization process.

5. A method for code snippet extraction and recombination according to claim 1, characterized in that Set the code snippet recombination strategy, calibrate the recombined code nodes in combination with the source node annotation result, and perform node extension recombination synthesis on the extracted code snippets based on the recombined code nodes to generate recombined code snippets, including: Set the code snippet recombination strategy according to the code snippet recombination requirement. Among them, the code snippet recombination strategy includes: code snippet recombination type strategy, code snippet recombination target strategy, and code snippet recombination organization strategy; Calibrate the recombined code nodes according to the code snippet recombination strategy and the source node annotation result; Perform node extension recombination synthesis on the extracted code snippets according to the code snippet recombination strategy and the recombined code nodes; Recombine the extracted code snippets at the recombined code nodes; Recombine the extracted code snippets other than the recombined code nodes and perform recombination recognition according to the code snippet recombination type strategy; Perform extended synthesis on the extracted code snippets with effective recombination recognition and the extracted code snippets at the recombined code nodes to complete node extension recombination synthesis and generate recombined code snippets.

6. A method for code snippet extraction and recombination according to claim 1, characterized in that Perform code testing on the recombined code snippets according to the preset code testing strategy and perform iterative optimization on node extension recognition extraction and node extension recombination synthesis, including: Preset the code testing strategy, and perform code testing on the recombined code snippets according to the preset code testing strategy; Evaluate the code testing result according to the preset code testing index, and obtain the code testing evaluation value. Among them, the preset code testing index includes: code testing accuracy index, code testing performance index, and code testing readability index; When the code testing evaluation value is not lower than the preset code testing value, it is determined that the code snippet extraction and recombination are effectively completed; When the code test evaluation value is lower than the preset code test value, reset the code snippet recognition rule for code snippet extraction and reset the code snippet recombination strategy for code snippet recombination; Iteratively optimize the node expansion recognition extraction and node expansion recombination synthesis according to the code snippet extraction process and the code snippet recombination process.

7. A method for code snippet extraction and recombination according to claim 1, characterized in that It also includes: Visualize the code snippet extraction result and the code snippet recombination result.

Citation Information

Patent Citations

  • AST-based code test analysis and correction method and system

    CN118642942A

  • Code peculiar smell detection method and system based on AST code measurement and code semantics

    CN119248277A