Code snippet extraction and recombination method
Through AST-based code parsing and source node annotation, an automatic code snippet extraction model is built, identification rules and reorganization strategies are set, which solves the problem of inefficient code extraction in the existing technology, and realizes efficient extraction and reorganization of complex codes.
Patent Information
- Application Number
- CN202510286611.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-12
- Publication Date
- 2025-04-11
- Estimated Expiration
- 2045-03-12
AI Technical Summary
Existing code extraction technology is inefficient and difficult to effectively process complex code, which cannot meet users' efficient utilization needs.
Through AST-based code parsing, source node annotation, a code fragment automatic extraction model is built, identification rules and reorganization strategies are set, and code fragment extraction and reorganization are realized.
It effectively improves the efficiency and accuracy of code snippets, can handle complex code, and meets users' efficient utilization needs.
Smart Images

Figure CN119806539A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of code processing, and in particular to a method for extracting and reorganizing code fragments. Background Art
[0002] With the rapid development of the computer field, the amount of code is increasing. In order to efficiently utilize the code, it is necessary to extract features from the code. Existing code extraction technology mainly extracts code through AST, but AST often has low code extraction efficiency and extraction exceptions. In addition, the code is becoming more and more complex. In order to meet user needs, the code needs to be reorganized. For simple codes, code reorganization can be achieved through code reorganization rules, while for complex codes, AST and other methods are required for code reorganization.
[0003] Therefore, the present invention provides a code fragment extraction and reorganization method. Summary of the invention
[0004] The present invention provides a code snippet extraction and reorganization method, which reads source code based on AST and performs code parsing, converts source code into AST and performs source node annotation on AST according to key structures and patterns of source code; obtains code snippet extraction requirements, sets code snippet identification rules, builds a code snippet automatic extraction model and performs node extension identification and extraction on AST based on source node annotation results to obtain extracted code snippets; sets a code snippet reorganization strategy, calibrates reorganized code nodes in combination with source node annotation results and performs node extension, reorganization and synthesis on the extracted code snippets based on the reorganized code nodes to generate reorganized code snippets; performs code testing on the reorganized code snippets according to a preset code testing strategy and iteratively optimizes node extension identification extraction and node extension reorganization synthesis; effectively realizes the extraction and reorganization of code snippets.
[0005] The present invention provides a code fragment extraction and reorganization method, comprising: Step 1: Read the source code based on AST and perform code parsing, convert the source code into AST and annotate the source nodes of AST according to the key structure and pattern of the source code; Step 2: Obtain code snippet extraction requirements, set code snippet recognition rules, build a code snippet automatic extraction model, and perform node extension recognition and extraction on AST based on source node annotation results to obtain extracted code snippets; Step 3: Set the code fragment reorganization strategy, identify the reorganized code nodes based on the source node annotation results, and perform node expansion and reorganization synthesis on the extracted code fragments based on the reorganized code nodes to generate the reorganized code fragments; Step 4: Perform code testing on the reorganized code fragments according to the preset code testing strategy and iteratively optimize the node extension identification extraction and node extension reorganization synthesis.
[0006] According to a code snippet extraction and reorganization method provided by the present invention, source code is read and parsed based on AST, source code is converted into AST, and source nodes of AST are marked according to key structures and patterns of source code, including: Based on the source code, select the 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 AST; Recursively traverse the AST to obtain the key structure and pattern of the source code and mark the source nodes in the AST, wherein the source node marking includes: source node structure marking and source node pattern marking.
[0007] According to a code snippet extraction and reorganization method provided by the present invention, obtaining code snippet extraction requirements includes: Obtain code snippet extraction requirements, parse the code snippet extraction requirements, and clarify the code snippet extraction requirements; Setting a clear category for code snippet extraction, when the code snippet extraction requirement is a clear category for code snippet extraction, determining that the code extraction requirement is clearly completed, wherein the clear category for 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, and a code extraction requirement clarification option is generated according to the code snippet extraction clarification category, and the code snippet extraction requirement is clarified.
[0008] According to a code snippet extraction and reorganization method provided by the present invention, code snippet recognition rules are set, a code snippet automatic extraction model is constructed, and node extension recognition and extraction of AST is performed based on source node annotation results to obtain extracted code snippets, including: Obtain historical source code and historical code snippet extraction data, and set code snippet recognition rules; Optimizing the code snippet recognition rules based on the code snippet extraction requirements, wherein the optimization includes: rule selection optimization, rule adjustment optimization, and rule supplement optimization; Build a code snippet automatic extraction model based on historical source code, historical code snippet extraction data, and optimized code snippet recognition rules; Identify and extract AST according to the code snippet automatic extraction model to obtain a code snippet; Perform node expansion identification and extraction on AST according to the source node annotation results; According to the code snippet automatic extraction model, the AST at the source node annotation result is identified and extracted to obtain the node code snippet; According to the code snippet automatic extraction model, the AST other than the source node annotation results is identified and extracted to obtain the pending code snippet; Perform node expansion on the node code fragment according to the optimized code fragment identification rule, and perform fragment organization processing on the pending code fragment and the node code fragment, wherein the fragment organization processing includes: fragment merging organization processing, fragment conversion organization processing and fragment independent organization processing; According to the node code fragments, the pending code fragments are sequentially organized and processed. When all the code fragments are organized and processed, it is determined that the node extension identification and extraction is completed, and the secondary code fragments are obtained. If the secondary code fragment is consistent with the primary code fragment, the extracted code fragment is obtained according to the primary code fragment; If the secondary code fragment is inconsistent with the primary code fragment, the extracted code fragment is obtained according to the secondary code fragment.
[0009] According to a code snippet extraction and reorganization method provided by the present invention, AST is identified and extracted according to a code snippet automatic extraction model, and further includes: Obtain the recognition and extraction time of the code snippet automatic extraction model for recognizing and extracting AST, and record it as the current recognition and extraction time; The recognition and extraction time threshold is obtained based on the historical code fragment extraction data. If the current recognition and extraction time exceeds the recognition and extraction time threshold, the AST size is determined to be exceeded, and the AST is cut according to the source node annotation result. The AST after the segmentation process is input into the code snippet automatic extraction model in sequence for recognition and extraction, and the current recognition and extraction time is re-obtained. If the re-obtained current recognition and extraction time does not exceed the recognition and extraction time threshold, the segmentation process is determined to be effective. Otherwise, the AST is segmented according to the code fragment identification rules completed by the optimization process.
[0010] According to a code snippet extraction and reorganization method provided by the present invention, a code snippet reorganization strategy is set, a reorganized code node is calibrated in combination with a source node annotation result, and a node expansion and reorganization synthesis is performed on the extracted code snippet based on the reorganized code node to generate a reorganized code snippet, including: Setting a code snippet reorganization strategy according to the code snippet reorganization requirements, wherein the code snippet reorganization strategy includes: a code snippet reorganization type strategy, a code snippet reorganization target strategy, and a code snippet reorganization organization strategy; Mark the reorganized code nodes according to the code fragment reorganization strategy and the source node annotation results; Perform node expansion and reorganization synthesis on the extracted code fragments according to the code fragment reorganization strategy and the reorganized code nodes; Reorganize the extracted code fragments at the reorganized code nodes; Recombining the extracted code fragments outside the recombined code nodes and identifying the recombinations according to the code fragment recombinant type strategy; The extracted code fragments with effective reorganization recognition are extended and synthesized with the extracted code fragments at the reorganized code nodes to complete the node extension and reorganization synthesis and generate the reorganized code fragments.
[0011] According to a code snippet extraction and reorganization method provided by the present invention, code testing is performed on the reorganized code snippets according to a preset code testing strategy, and node extension identification extraction and node extension reorganization synthesis are iteratively optimized, including: Preset code testing strategy, and perform code testing on the reorganized code fragments according to the preset code testing strategy; Evaluate the code test results according to preset code test indicators to obtain a code test evaluation value, wherein the preset code test indicators include: a code test accuracy indicator, a code test performance indicator, and a code test readability indicator; When the code test evaluation value is not lower than the preset code test value, it is determined that the code fragment extraction and reorganization are effectively completed; When the code test evaluation value is lower than the preset code test value, the code snippet recognition rule is reset to extract the code snippet and the code snippet reorganization strategy is reset to reorganize the code snippet; According to the code snippet extraction process and the code snippet reorganization process, the node extension recognition extraction and the node extension reorganization synthesis are iteratively optimized.
[0012] A code snippet extraction and reorganization method provided by the present invention also includes: Visualize the code snippet extraction results and code snippet reorganization results.
[0013] Compared with the prior art, the present invention has the following beneficial effects: By reading the source code based on AST and performing code parsing, the source code is converted into AST and the source nodes of AST are annotated according to the key structure and pattern of the source code; the code snippet extraction requirements are obtained, the code snippet identification rules are set, the code snippet automatic extraction model is constructed, and the node extension identification and extraction of AST is performed based on the source node annotation results to obtain the extracted code snippets; the code snippet reorganization strategy is set, the reorganized code nodes are calibrated in combination with the source node annotation results, and the extracted code snippets are node-extended, reorganized and synthesized based on the reorganized code nodes to generate reorganized code snippets; the reorganized code snippets are code tested according to the preset code testing strategy, and the node extension identification extraction and node extension reorganization synthesis are iteratively optimized; the extraction and reorganization of code snippets are effectively realized.
[0014] Other features and advantages of the present invention will be described in the following description, and partly become apparent from the description, or understood by practicing the present invention. The purpose and other advantages of the present invention can be realized and obtained by the structures particularly pointed out in the written description and the accompanying drawings.
[0015] The technical solution of the present invention is further described in detail below through the accompanying drawings and embodiments. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] The accompanying drawings are used to provide a further understanding of the present invention and constitute a part of the specification. Together with the embodiments of the present invention, they are used to explain the present invention and do not constitute a limitation of the present invention. In the accompanying drawings: Figure 1 It is a flowchart of a code snippet extraction and reorganization method provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0017] The preferred embodiments of the present invention are described below in conjunction with the accompanying drawings. It should be understood that the preferred embodiments described herein are only used to illustrate and explain the present invention, and are not used to limit the present invention.
[0018] Embodiment 1: The embodiment of the present invention provides a method for extracting and reorganizing code fragments, such as Figure 1 As shown, including: Step 1: Read the source code based on AST and perform code parsing, convert the source code into AST and annotate the source nodes of AST according to the key structure and pattern of the source code; Step 2: Obtain code snippet extraction requirements, set code snippet recognition rules, build a code snippet automatic extraction model, and perform node extension recognition and extraction on AST based on source node annotation results to obtain extracted code snippets; Step 3: Set the code fragment reorganization strategy, identify the reorganized code nodes based on the source node annotation results, and perform node expansion and reorganization synthesis on the extracted code fragments based on the reorganized code nodes to generate the reorganized code fragments; Step 4: Perform code testing on the reorganized code fragments according to the preset code testing strategy and iteratively optimize the node extension identification extraction and node extension reorganization synthesis.
[0019] In this embodiment, AST refers to an abstract syntax tree.
[0020] In this embodiment, source code refers to code that needs to be extracted and reorganized into code fragments.
[0021] In this embodiment, source node annotation refers to annotation performed on AST according to key structures and patterns of source code, which is used to represent nodes.
[0022] In this embodiment, the code snippet extraction requirement refers to the requirement to extract snippets from source code.
[0023] In this embodiment, the code snippet identification rule refers to a rule for identifying a code snippet.
[0024] In this embodiment, the code snippet automatic extraction model refers to a model used to automatically extract code from source code.
[0025] In this embodiment, node extension identification extraction refers to the extraction processing of AST according to the source node annotation results.
[0026] In this embodiment, extracting code snippets refers to extracting code snippets from source code snippets to obtain code snippets.
[0027] In this embodiment, the code fragment reorganization strategy refers to a strategy for reorganizing the extracted code fragments.
[0028] In this embodiment, the reorganized code node refers to a node marked according to the code fragment reorganization strategy and the source node marking result.
[0029] In this embodiment, node expansion and reorganization synthesis refers to processing of the extracted code fragments according to the code fragment reorganization strategy and the reorganized code nodes.
[0030] In this embodiment, the reorganized code fragment refers to a code fragment obtained by reorganizing the extracted code fragment.
[0031] In this embodiment, the preset code testing strategy is a strategy preset for testing the code snippet extraction and reorganization results.
[0032] The working principle and beneficial effects of the above technical solution are: by reading the source code based on AST and performing code parsing, the source code is converted into AST and the source nodes are annotated on the AST according to the key structure and pattern of the source code; the code snippet extraction requirements are obtained, the code snippet recognition rules are set, the code snippet automatic extraction model is constructed, and the node extension recognition and extraction of AST is performed based on the source node marking results to obtain the extracted code snippets; the code snippet reorganization strategy is set, the reorganized code nodes are calibrated in combination with the source node marking results, and the extracted code snippets are node-extended, reorganized and synthesized based on the reorganized code nodes to generate reorganized code snippets; the reorganized code snippets are code tested according to the preset code testing strategy, and the node extension recognition extraction and node extension reorganization synthesis are iteratively optimized; the extraction and reorganization of code snippets are effectively realized.
[0033] Embodiment 2: The embodiment of the present invention provides a code snippet extraction and reorganization method, which reads source code based on AST and performs code parsing, converts the source code into AST and annotates source nodes of AST according to the key structure and mode of the source code, including: Based on the source code, select the 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 AST; Recursively traverse the AST to obtain the key structure and pattern of the source code and mark the source nodes in the AST, wherein the source node marking includes: source node structure marking and source node pattern marking.
[0034] In this embodiment, AST refers to an abstract syntax tree.
[0035] In this embodiment, the AST parsing library refers to a database used to parse source code.
[0036] In this embodiment, the syntax analysis tool refers to a tool for performing syntax analysis on source code to ensure that the code snippets obtained subsequently conform to the syntax and avoid errors in the code snippets.
[0037] In this embodiment, source code refers to code that needs to be extracted and reorganized into code fragments.
[0038] In this embodiment, the key structure is, for example, a connection structure.
[0039] In this embodiment, source node annotation refers to annotation performed on AST according to key structures and patterns of source code, which is used to represent nodes.
[0040] The working principle and beneficial effects of the above technical solution are: reading source code and performing code parsing based on AST, converting source code into AST and annotating source nodes of AST according to the key structure and pattern of the source code, laying the foundation for source node annotation for subsequent code fragment extraction and code fragment reorganization.
[0041] Embodiment 3: The embodiment of the present invention provides a code snippet extraction and reorganization method, which obtains code snippet extraction requirements, including: Obtain code snippet extraction requirements, parse the code snippet extraction requirements, and clarify the code snippet extraction requirements; Setting a clear category for code snippet extraction, when the code snippet extraction requirement is a clear category for code snippet extraction, determining that the code extraction requirement is clearly completed, wherein the clear category for 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, and a code extraction requirement clarification option is generated according to the code snippet extraction clarification category, and the code snippet extraction requirement is clarified.
[0042] In this embodiment, the code snippet extraction requirement refers to the requirement to extract snippets from source code, for example, extracting a1 type code to generate code snippets.
[0043] In this embodiment, the clear category of code snippet extraction refers to the category of specific code snippet extraction requirements, that is, the category of accurate code snippet extraction is implemented, and code extraction exceptions are not caused due to vague code snippet extraction requirements.
[0044] In this embodiment, a code extraction requirement clarification option, such as a code extraction type requirement clarification option, is used to clarify the code snippet extraction requirement.
[0045] The working principle and beneficial effect of the above technical solution are: obtaining the code snippet extraction requirements is conducive to the subsequent implementation of code snippet extraction.
[0046] Embodiment 4: The embodiment of the present invention provides a code snippet extraction and reorganization method, which sets code snippet recognition rules, builds a code snippet automatic extraction model, and performs node extension recognition and extraction on AST based on source node annotation results to obtain extracted code snippets, including: Obtain historical source code and historical code snippet extraction data, and set code snippet recognition rules; Optimizing the code snippet recognition rules based on the code snippet extraction requirements, wherein the optimization includes: rule selection optimization, rule adjustment optimization, and rule supplement optimization; Build a code snippet automatic extraction model based on historical source code, historical code snippet extraction data, and optimized code snippet recognition rules; Identify and extract AST according to the code snippet automatic extraction model to obtain a code snippet; Perform node expansion identification and extraction on AST according to the source node annotation results; According to the code snippet automatic extraction model, the AST at the source node annotation result is identified and extracted to obtain the node code snippet; According to the code snippet automatic extraction model, the AST other than the source node annotation results is identified and extracted to obtain the pending code snippet; Perform node expansion on the node code fragment according to the optimized code fragment identification rule, and perform fragment organization processing on the pending code fragment and the node code fragment, wherein the fragment organization processing includes: fragment merging organization processing, fragment conversion organization processing and fragment independent organization processing; According to the node code fragments, the pending code fragments are sequentially organized and processed. When all the code fragments are organized and processed, it is determined that the node extension identification and extraction is completed, and the secondary code fragments are obtained. If the secondary code fragment is consistent with the primary code fragment, the extracted code fragment is obtained according to the primary code fragment; If the secondary code fragment is inconsistent with the primary code fragment, the extracted code fragment is obtained according to the secondary code fragment.
[0047] In this embodiment, the historical source code refers to the source code of a historical period.
[0048] In this embodiment, the historical code snippet extraction data refers to the relevant data of code snippet extraction during the historical period.
[0049] In this embodiment, the code snippet identification rule refers to a rule for identifying a code snippet.
[0050] In this embodiment, the code snippet recognition rules are optimized based on the code snippet extraction requirements to facilitate meeting the code snippet extraction requirements and avoid invalid recognition.
[0051] In this embodiment, the code snippet automatic extraction model refers to a model used to automatically extract code from source code.
[0052] In this embodiment, a primary code snippet refers to a code snippet obtained by the automatic code snippet extraction model through identification and extraction of AST.
[0053] In this embodiment, node extension identification extraction refers to the extraction processing of AST according to the source node annotation results.
[0054] In this embodiment, the node code snippet refers to a code snippet obtained by identifying and extracting the AST at the source node annotation result according to the code snippet automatic extraction model.
[0055] In this embodiment, the pending code snippet refers to a code snippet obtained by identifying and extracting the AST other than the source node annotation result according to the code snippet automatic extraction model.
[0056] In this embodiment, node expansion refers to processing the node code fragment according to the code fragment identification rule completed by the optimization process, for example, merging the pending code fragment with the node code fragment.
[0057] In this embodiment, the fragment organization processing refers to the processing of the pending code fragments and the node code fragments to obtain the extracted code fragments.
[0058] In this embodiment, the secondary code fragment refers to a code fragment obtained by performing fragment organization processing on the pending code fragment and the node code fragment, that is, a code fragment obtained by performing node extension identification and extraction.
[0059] In this embodiment, extracting code snippets refers to extracting code snippets from source code snippets to obtain code snippets.
[0060] The working principle and beneficial effects of the above technical solution are: by setting code snippet recognition rules, building a code snippet automatic extraction model and performing node extension recognition and extraction on AST based on source node annotation results, the extracted code snippets are obtained, thereby effectively realizing the extraction of code snippets.
[0061] Embodiment 5: The embodiment of the present invention provides a code snippet extraction and reorganization method, which identifies and extracts AST according to a code snippet automatic extraction model, and also includes: Obtain the recognition and extraction time of the code snippet automatic extraction model for recognizing and extracting AST, and record it as the current recognition and extraction time; The recognition and extraction time threshold is obtained based on the historical code fragment extraction data. If the current recognition and extraction time exceeds the recognition and extraction time threshold, the AST size is determined to be exceeded, and the AST is cut according to the source node annotation result. The AST after the segmentation process is input into the code snippet automatic extraction model in sequence for recognition and extraction, and the current recognition and extraction time is re-obtained. If the re-obtained current recognition and extraction time does not exceed the recognition and extraction time threshold, the segmentation process is determined to be effective. Otherwise, the AST is segmented according to the code fragment identification rules completed by the optimization process.
[0062] In this embodiment, the current recognition and extraction time refers to the time when the automatic extraction model of the code snippet in the current period recognizes and extracts the AST.
[0063] In this embodiment, the identification extraction time threshold refers to a time threshold obtained by extracting data based on historical code snippets.
[0064] In this embodiment, it is determined that the AST size exceeds the limit, that is, the efficiency of the automatic code snippet extraction model in extracting code from the AST is low, and in addition, code extraction anomalies are prone to occur.
[0065] In this embodiment, the cutting process refers to the processing of the AST according to the source node labeling result, which is used to ensure that the size of the AST does not exceed the limit.
[0066] The working principle and beneficial effects of the above technical solution are: by obtaining the current recognition and extraction time, the AST is segmented according to the source node annotation results, thereby ensuring the efficiency of data fragment extraction and further realizing the extraction of code fragments.
[0067] Embodiment 6: The embodiment of the present invention provides a code snippet extraction and reorganization method, which sets a code snippet reorganization strategy, calibrates the reorganized code nodes in combination with the source node annotation results, and performs node expansion and reorganization synthesis on the extracted code snippets based on the reorganized code nodes to generate the reorganized code snippets, including: Setting a code snippet reorganization strategy according to the code snippet reorganization requirements, wherein the code snippet reorganization strategy includes: a code snippet reorganization type strategy, a code snippet reorganization target strategy, and a code snippet reorganization organization strategy; Mark the reorganized code nodes according to the code fragment reorganization strategy and the source node annotation results; Perform node expansion and reorganization synthesis on the extracted code fragments according to the code fragment reorganization strategy and the reorganized code nodes; Reorganize the extracted code fragments at the reorganized code nodes; Recombining the extracted code fragments outside the recombined code nodes and identifying the recombinations according to the code fragment recombinant type strategy; The extracted code fragments with effective reorganization recognition are extended and synthesized with the extracted code fragments at the reorganized code nodes to complete the node extension and reorganization synthesis and generate the reorganized code fragments.
[0068] In this embodiment, the code fragment reorganization demand refers to the demand for reorganizing the code fragments.
[0069] In this embodiment, the code fragment reorganization strategy refers to a strategy for reorganizing code fragments.
[0070] In this embodiment, the reorganized code node refers to a node marked according to the code fragment reorganization strategy and the source node marking result.
[0071] In this embodiment, node expansion and reorganization synthesis refers to processing the extracted code fragments according to the code fragment reorganization strategy and the reorganized code nodes, for example, performing code fragment merging processing on the extracted code fragments at the reorganized code nodes.
[0072] In this embodiment, the reorganized code fragment refers to a code fragment obtained by performing node expansion and reorganization synthesis on the extracted code fragment based on the reorganized code node.
[0073] The working principle and beneficial effects of the above technical solution are: by setting the code fragment reorganization strategy, combining the source node annotation results to calibrate the reorganized code nodes and based on the reorganized code nodes, the extracted code fragments are subjected to node expansion and reorganization synthesis to generate reorganized code fragments, thereby effectively realizing the reorganization of the code fragments.
[0074] Embodiment 7: The embodiment of the present invention provides a code snippet extraction and reorganization method, which performs code testing on the reorganized code snippets according to a preset code testing strategy and iteratively optimizes node extension identification extraction and node extension reorganization synthesis, including: Preset code testing strategy, and perform code testing on the reorganized code fragments according to the preset code testing strategy; Evaluate the code test results according to preset code test indicators to obtain a code test evaluation value, wherein the preset code test indicators include: a code test accuracy indicator, a code test performance indicator, and a code test readability indicator; When the code test evaluation value is not lower than the preset code test value, it is determined that the code fragment extraction and reorganization are effectively completed; When the code test evaluation value is lower than the preset code test value, the code snippet recognition rule is reset to extract the code snippet and the code snippet reorganization strategy is reset to reorganize the code snippet; According to the code snippet extraction process and the code snippet reorganization process, the node extension recognition extraction and the node extension reorganization synthesis are iteratively optimized.
[0075] In this embodiment, the preset code testing strategy refers to a strategy for performing code testing on the reorganized code fragments.
[0076] In this embodiment, the code test evaluation index refers to an index for evaluating the code test result.
[0077] In this embodiment, the code test evaluation value refers to a value obtained by evaluating the code test result according to a preset code test indicator.
[0078] In this embodiment, the preset code test value refers to a preset value used to determine whether the code segment extraction and reorganization are effectively completed.
[0079] The working principle and beneficial effect of the above technical solution are: by performing code testing on the reorganized code fragments according to the preset code testing strategy and iteratively optimizing the node extension identification extraction and node extension reorganization synthesis, it is beneficial to the subsequent extraction and reorganization of code fragments.
[0080] Embodiment 8: The embodiment of the present invention provides a code snippet extraction and reorganization method, further comprising: Visualize the code snippet extraction results and code snippet reorganization results.
[0081] The working principle and beneficial effect of the above technical solution are: by visualizing the code snippet extraction results and the code snippet reorganization results, the user experience is ensured.
[0082] 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 it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A code fragment extraction and reorganization method, characterized in that: include: Step 1: Read the source code based on AST and perform code parsing, convert the source code into AST and annotate the source nodes of AST according to the key structure and pattern of the source code; Step 2: Obtain code snippet extraction requirements, set code snippet recognition rules, build a code snippet automatic extraction model, and perform node extension recognition and extraction on AST based on source node annotation results to obtain extracted code snippets; Step 3: Set the code fragment reorganization strategy, identify the reorganized code nodes based on the source node annotation results, and perform node expansion and reorganization synthesis on the extracted code fragments based on the reorganized code nodes to generate the reorganized code fragments; Step 4: Perform code testing on the reorganized code fragments according to the preset code testing strategy and iteratively optimize the node extension identification extraction and node extension reorganization synthesis.
2. A code fragment extraction and reorganization method according to claim 1, characterized in that: Read source code based on AST and perform code parsing, convert source code into AST and annotate source nodes of AST according to key structures and patterns of source code, including: Based on the source code, select the 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 AST; Recursively traverse the AST to obtain the key structure and pattern of the source code and mark the source nodes in the AST, wherein the source node marking includes: source node structure marking and source node pattern marking.
3. A code fragment extraction and reorganization method according to claim 1, characterized in that: Obtain code snippet extraction requirements, including: Obtain code snippet extraction requirements, parse the code snippet extraction requirements, and clarify the code snippet extraction requirements; Setting a clear category for code snippet extraction, when the code snippet extraction requirement is a clear category for code snippet extraction, determining that the code extraction requirement is clearly completed, wherein the clear category for 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, and a code extraction requirement clarification option is generated according to the code snippet extraction clarification category, and the code snippet extraction requirement is clarified.
4. A code fragment extraction and reorganization method according to claim 3, characterized in that: Set the code snippet recognition rules, build the code snippet automatic extraction model, and perform node expansion recognition and extraction on AST based on the source node annotation results to obtain the extracted code snippets, including: Obtain historical source code and historical code snippet extraction data, and set code snippet recognition rules; Optimizing the code snippet recognition rules based on the code snippet extraction requirements, wherein the optimization includes: rule selection optimization, rule adjustment optimization, and rule supplement optimization; Build a code snippet automatic extraction model based on historical source code, historical code snippet extraction data, and optimized code snippet recognition rules; Identify and extract AST according to the code snippet automatic extraction model to obtain a code snippet; Perform node expansion identification and extraction on AST according to the source node annotation results; According to the code snippet automatic extraction model, the AST at the source node annotation result is identified and extracted to obtain the node code snippet; According to the code snippet automatic extraction model, the AST other than the source node annotation results is identified and extracted to obtain the pending code snippet; Perform node expansion on the node code fragment according to the optimized code fragment identification rule, and perform fragment organization processing on the pending code fragment and the node code fragment, wherein the fragment organization processing includes: fragment merging organization processing, fragment conversion organization processing and fragment independent organization processing; According to the node code fragments, the pending code fragments are sequentially organized and processed. When all the code fragments are organized and processed, it is determined that the node extension identification and extraction is completed, and the secondary code fragments are obtained. If the secondary code fragment is consistent with the primary code fragment, the extracted code fragment is obtained according to the primary code fragment; If the secondary code fragment is inconsistent with the primary code fragment, the extracted code fragment is obtained according to the secondary code fragment.
5. A code fragment extraction and reorganization method according to claim 4, characterized in that: Identify and extract AST based on the code snippet automatic extraction model, including: Obtain the recognition and extraction time of the code snippet automatic extraction model for recognizing and extracting AST, and record it as the current recognition and extraction time; The recognition and extraction time threshold is obtained based on the historical code fragment extraction data. If the current recognition and extraction time exceeds the recognition and extraction time threshold, the AST size is determined to be exceeded, and the AST is cut according to the source node annotation result. The AST after the segmentation process is input into the code snippet automatic extraction model in sequence for recognition and extraction, and the current recognition and extraction time is re-obtained. If the re-obtained current recognition and extraction time does not exceed the recognition and extraction time threshold, the segmentation process is determined to be effective. Otherwise, the AST is segmented according to the code fragment identification rules completed by the optimization process.
6. A code fragment extraction and reorganization method according to claim 1, characterized in that: Set the code snippet reorganization strategy, combine the source node annotation results to mark the reorganized code nodes, and perform node expansion and reorganization synthesis on the extracted code snippets based on the reorganized code nodes to generate reorganized code snippets, including: Setting a code snippet reorganization strategy according to the code snippet reorganization requirements, wherein the code snippet reorganization strategy includes: a code snippet reorganization type strategy, a code snippet reorganization target strategy, and a code snippet reorganization organization strategy; Mark the reorganized code nodes according to the code fragment reorganization strategy and the source node annotation results; Perform node expansion and reorganization synthesis on the extracted code fragments according to the code fragment reorganization strategy and the reorganized code nodes; Reorganize the extracted code fragments at the reorganized code nodes; Recombining the extracted code fragments outside the recombined code nodes and identifying the recombinations according to the code fragment recombinant type strategy; The extracted code fragments with effective reorganization recognition are extended and synthesized with the extracted code fragments at the reorganized code nodes to complete the node extension and reorganization synthesis and generate the reorganized code fragments.
7. A code fragment extraction and reorganization method according to claim 1, characterized in that: According to the preset code testing strategy, code testing is performed on the reorganized code fragments, and node extension identification extraction and node extension reorganization synthesis are iteratively optimized, including: Preset code testing strategy, and perform code testing on the reorganized code fragments according to the preset code testing strategy; Evaluate the code test results according to preset code test indicators to obtain a code test evaluation value, wherein the preset code test indicators include: a code test accuracy indicator, a code test performance indicator, and a code test readability indicator; When the code test evaluation value is not lower than the preset code test value, it is determined that the code fragment extraction and reorganization are effectively completed; When the code test evaluation value is lower than the preset code test value, the code snippet recognition rule is reset to extract the code snippet and the code snippet reorganization strategy is reset to reorganize the code snippet; According to the code snippet extraction process and the code snippet reorganization process, the node extension recognition extraction and the node extension reorganization synthesis are iteratively optimized.
8. A code fragment extraction and reorganization method according to claim 1, characterized in that: Also includes: Visualize the code snippet extraction results and code snippet reorganization results.
Citation Information
Patent Citations
Visual code generation method based on AST, rule base and knowledge base
CN113946326A
AST-based source code anomaly detection method and device
CN115146282A
Model source code vulnerability detection method based on signature
CN117540389A
AST-based code test analysis and correction method and system
CN118642942A
Cross-language code program classification method and device and classification model training method and device
CN118981335A