A Programming Pattern Mining Method and System Based on a Large Language Model

CN122311162APending Publication Date: 2026-06-30ZHEJIANG UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
ZHEJIANG UNIV
Filing Date
2026-04-24
Publication Date
2026-06-30

Smart Images

  • Figure CN122311162A_ABST
    Figure CN122311162A_ABST
Patent Text Reader

Abstract

This invention discloses a programming pattern mining method and system based on a large language model, belonging to the fields of software engineering and code analysis technology. The method includes: scanning and filtering target code repositories to select valid source files; constructing an abstract syntax tree using a static parser and extracting candidate code fragments at three granularities: function level, statement level, and interval level; vectorizing the code fragments using a code embedding model, assembling the AST structure and semantic vectors into meta-information; identifying high-frequency code patterns with semantic similarity using a density-based clustering algorithm; abstracting and generalizing variable elements through sliding window consistency analysis to generate generalized code templates; collaboratively determining the programming patterns of candidates using a multi-agent system, ultimately outputting a code template library; and constructing the code template library into a RAG retrieval knowledge base for use in software engineering tasks such as unit test generation, programming standard recognition, and code completion.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of software engineering and program analysis technology, specifically to a programming pattern mining method and system based on a large language model, which is particularly suitable for automatically discovering, abstracting and integrating reusable programming patterns from large-scale code repositories; the constructed code template library can serve as a retrieval knowledge base for RAG (Retrieval Enhanced Generation), providing retrieval enhancement support for software engineering tasks such as code understanding, intelligent programming assistance, test case generation, and programming standard recognition. Background Technology

[0002] In software development practice, programming patterns refer to widely adopted code designs or best practices that clearly express programming intent and have high reusability. These patterns typically contain rich semantic information, improving code readability, reducing comprehension costs, and promoting development efficiency and team collaboration. Programming patterns align with agile development principles, contributing to increased productivity and team cohesion.

[0003] However, existing technologies have the following shortcomings in identifying and mining programming patterns: First, methods based on predefined rules typically require enumerating pattern types beforehand and rely on the syntactic features of specific programming languages ​​for pattern matching. Such methods have limited scalability, are difficult to adapt to different languages ​​and emerging programming paradigms, and cannot discover pattern forms that are not predefined.

[0004] Second, while statistical or learning-based methods can automatically discover patterns in data, most of these methods focus on structural repetitive analysis and do not adequately characterize the semantics of the code, making it difficult to distinguish code segments with similar structures but different semantics.

[0005] Third, existing methods are relatively weak in terms of programming pattern abstraction and generalization. They often directly output raw code snippets and lack a unified abstraction of variable elements such as variable names, constants, and types, resulting in insufficient reusability and versatility of code templates.

[0006] Fourth, existing technologies lack effective automated means for judging the quality of programming patterns and the organic combination of multiple code snippets, making it difficult to form semantically coherent and logically complete composite code templates. Most methods use single model calls or simple rules, lacking a framework design for multi-agent collaboration, message routing, and runtime scheduling.

[0007] Fifth, in the application of RAG to software engineering tasks, existing technologies mostly use general documents or code snippets as retrieval libraries, lacking specialized knowledge base construction methods for programming patterns, which are high-value and highly reusable patterns; programming patterns, as semantically clear and structurally standardized code templates, are more suitable as retrieval sources for RAG.

[0008] Therefore, there is an urgent need for a method and system that can automatically explore, abstract, and synthesize high-quality programming patterns and code templates from large-scale codebases to support downstream applications such as code understanding, test generation, and specification identification. Summary of the Invention

[0009] To address the shortcomings of existing technologies, the present invention aims to provide a programming pattern mining method and system based on a large language model, thereby achieving fully automated processing from source code to the RAG retrieval knowledge base.

[0010] To achieve the above objectives, the present invention adopts the following technical solution: a programming pattern mining method based on a large language model, comprising the following steps: S1. Code Repository Scanning and Filtering: Traverse the target code repository, filter valid source files based on language configuration, and exclude test directories, hidden directories, and irrelevant files containing keywords such as "test"; identify each subdirectory as an independent project and obtain a list of project names and a list of source file paths; S2. Static code parsing and abstract syntax tree construction: Parse the valid source file into an abstract syntax tree, extract node information such as node type, position range and byte offset, and skip the file if the parsing fails. S3. Multi-granularity programming pattern representation and identification: Candidate code fragments are extracted from the abstract syntax tree according to three granularity levels: function level, statement level, and interval level. Among them, function level and block level are directly extracted, while statement level requires nesting checks to extract only the outermost statement. Filtering conditions are set to form a three-dimensional data structure of "project-file-function". S4. Code representation vectorization and meta-information assembly: Semantic embedding encoding is performed on the candidate code fragments to obtain semantic vectors, and the abstract syntax tree structure (including depth, extent, kind, ast_num, etc.) is assembled with the semantic vectors to form code meta-information; S5. Semantic similarity code clustering: The semantic vectors are clustered using a density-based clustering algorithm. Based on the code meta-information, representative code segments are selected from each cluster, and candidate programming patterns are determined based on the representative code segments. At the same time, meta-information such as cluster size and position label is recorded. S6. Variable element identification and abstract generalization: The candidate programming pattern is lexicalized, and variable elements are identified based on the consistency analysis within the cluster. The variable elements are replaced with abstract placeholders to generate a generalized code template. S7. Multi-Agent Collaborative Programming Pattern Determination and Code Template Synthesis: The generalized code templates are grouped according to the same source region; for the candidate code segments in each group, the first agent (semantic clarity agent) and the second agent (syntax logic agent) are called in parallel to evaluate the semantic clarity and syntactic logic of the candidate code segments respectively, and the evaluation results are input to the third agent (comprehensive judgment agent) for comprehensive judgment, and the code segments judged as valid programming patterns are retained as the current code segments; the fourth agent (planning and synthesis agent) reads the code segments in this region that have passed the judgment except for the current code segment, and determines the number of code segments to be read at one time and the stopping condition; the fifth agent (code assembly agent) merges the code segments read by the fourth agent with the current code segment; the merged code segments are input again into the first agent, the second agent and the third agent for validity judgment, and multiple merging iterations are performed until the stopping condition is reached, and the valid code templates of each source region are output to form a code template library; S8. Construction and output of RAG retrieval knowledge base: Construct the code template library into a RAG retrieval knowledge base.

[0011] Further, in step S3, the granularity level is specifically: function-level granularity, used to extract the abstract syntax tree node corresponding to the complete function definition; statement-level granularity, used to extract the abstract syntax tree node corresponding to a single statement; and interval-level granularity, used to extract the continuous syntax block corresponding to the control structure, loop body, or exception handling block.

[0012] Furthermore, in step S4, the semantic embedding encoding is implemented using a pre-trained code language model, which is an encoder based on the Transformer architecture.

[0013] Furthermore, in step S5, the density-based clustering algorithm is the DBSCAN algorithm, and the representative code segment is the code segment in each cluster that is closest to the centroid of the cluster.

[0014] Furthermore, in step S6, the consistency analysis uses a sliding window to compare the word sequences of each code segment within the cluster. When the matching ratio of a certain word in the cluster is higher than a preset threshold, it is retained as a common element; otherwise, it is replaced with an abstract placeholder. The types of the abstract placeholder include: string type, constant type, type identifier type, variable type, and function call type.

[0015] Furthermore, in step S7, the code merging determination conditions for merging the code segment read by the fourth agent with the current code segment through the fifth agent include: the code segments originate from the same abstract syntax tree, share context variables, have similar structural features, or have a co-occurrence relationship.

[0016] Further, step S8 includes: storing the code fragments, semantic vectors, and meta-information of the code template library in a structured manner to form a knowledge base that can be retrieved by RAG.

[0017] Furthermore, the RAG retrieval knowledge base is used for retrieval enhancement generation in software engineering tasks, including unit test generation, programming standard identification, and code completion.

[0018] This invention also provides a programming pattern mining system based on a large language model, comprising: The scanning and filtering module is used to traverse the target code repository and filter valid source files based on the language configuration; The static parsing module is used to parse the valid source file into an abstract syntax tree and extract attributes such as node type, position range, and byte offset. The representation recognition module is used to extract candidate code fragments from the abstract syntax tree according to three granularity levels: function level, statement level, and interval level. The vectorization and assembly module is used to perform semantic embedding encoding on the candidate code fragments to obtain semantic vectors, and to assemble the abstract syntax tree structure and the semantic vectors into code meta-information. The clustering module is used to cluster the semantic vectors using a density-based clustering algorithm, select representative code segments from each cluster based on the code meta-information, and determine candidate programming patterns based on the representative code segments. The abstract generalization module is used to perform lexical processing on the candidate programming patterns, identify variable elements based on consistency analysis within clusters, replace the variable elements with abstract placeholders, and generate a generalized code template. The determination and synthesis module is used to perform programming pattern determination and code template synthesis based on a multi-agent architecture. The module includes: grouping the generalized code templates according to the same source region; for each group of candidate code segments, simultaneously calling the first agent and the second agent to evaluate the semantic clarity and syntactic logic of the candidate code segments, and inputting the evaluation results to the third agent for comprehensive determination, retaining the code segments determined as valid programming patterns as the current code segment; reading the determined code segments in the same region (excluding the current code segment) through the fourth agent, determining the number of code segments to be read at once and the stopping condition; merging the code segments read by the fourth agent with the current code segment through the fifth agent; inputting the merged code segment back into the first agent, second agent, and third agent for validity determination, performing multiple merging iterations until the stopping condition is met, and outputting the valid code templates for each source region, thus forming a code template library. The RAG knowledge base construction module is used to build the code template library into a RAG retrieval knowledge base.

[0019] Furthermore, the representation recognition module supports static analysis tools for multiple programming languages ​​and achieves multilingual extension by configuring language-specific node type mappings.

[0020] The beneficial effects of this invention are as follows: (1) An eight-stage modular pipeline design is adopted, which clearly separates the code repository filtering, static parsing, representation recognition, vectorization assembly, clustering, abstract generalization, decision synthesis, RAG construction, etc., with clear responsibilities, which facilitates expansion and maintenance; (2) By vectorizing code representations and assembling meta-information, the AST structure and semantic vectors are managed in a unified manner, providing a complete data foundation for clustering and RAG retrieval; (3) A multi-Agent architecture is used for programming pattern determination and code template synthesis: the semantic clarity agent and the syntax logic agent make parallel determinations to improve efficiency, and the comprehensive determination agent summarizes the results; the planning synthesis agent dynamically reads the code segment in this area through tool calls and decides the synthesis strategy, the code assembly agent performs the merging, and the merging result is determined again by the three agents, supporting multiple rounds of iteration until the planning synthesis agent decides to stop. (4) The constructed code template library can be directly used as a RAG retrieval knowledge base to achieve enhanced retrieval generation in tasks such as unit test generation, programming standard recognition, and code completion; (5) By identifying and abstracting variable elements, variable parts are unified into abstract placeholders, thereby improving the generality and reusability of code templates. Attached Figure Description

[0021] Figure 1 This is a schematic diagram of the overall eight-stage process provided in the embodiments of the present invention; Figure 2 This is a flowchart illustrating the multi-Agent collaborative programming mode determination and code template synthesis process of the present invention; Figure 3 This is a schematic diagram of the structure of the device of the present invention; Figure 4 This is a schematic diagram of the electronic device of the present invention. Detailed Implementation

[0022] The present invention will now be described in further detail with reference to the accompanying drawings and specific embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.

[0023] like Figure 1 As shown, this embodiment of the invention provides a programming pattern mining method based on a large language model, including the following steps: Step 1: Code Repository Scanning and Filtering: Traverse the target code repository, filter valid source files based on language configuration, and exclude test directories, hidden directories, and irrelevant files containing keywords such as "test"; identify each subdirectory as an independent project and obtain a list of project names and a list of source file paths.

[0024] Step 2, Static code parsing and abstract syntax tree construction: Parse the valid source file into an abstract syntax tree, extracting node information such as node type, position range, and byte offset. If parsing fails, skip the file.

[0025] Step 3: Multi-granularity programming pattern representation and identification: Extract candidate code fragments from the abstract syntax tree according to three granularity levels: function level, statement level, and interval level; among them, function level and block level are directly extracted, while statement level requires nesting checks to extract only the outermost statement; set filtering conditions to form a three-dimensional data structure of "project-file-function".

[0026] Step 4: Code representation vectorization and meta-information assembly: Semantic embedding encoding is performed on the candidate code fragments to obtain semantic vectors, and the abstract syntax tree structure (including depth, extent, kind, ast_num, etc.) is assembled with the semantic vectors to form code meta-information.

[0027] Step 5: Semantic similarity code clustering: The semantic vectors are clustered using a density-based clustering algorithm. Based on the code metadata, representative code segments are selected from each cluster, and candidate programming patterns are determined based on the representative code segments. At the same time, metadata such as cluster size and position labels are recorded.

[0028] Step Six: Variable Element Identification and Abstract Generalization: The candidate programming patterns are lexicalized, and variable elements are identified based on consistency analysis within the clusters. The variable elements are replaced with abstract placeholders to generate a generalized code template.

[0029] Step 7: Multi-Agent Collaborative Programming Pattern Determination and Code Template Synthesis: Group the generalized code templates according to the same source region; for the candidate code segments in each group, the first agent (semantic clarity agent) and the second agent (syntax logic agent) are called in parallel to evaluate the semantic clarity and syntactic logic of the candidate code segments respectively, and the evaluation results are input to the third agent (comprehensive judgment agent) for comprehensive judgment, and the code segments judged as valid programming patterns are retained as the current code segments; the fourth agent (planning and synthesis agent) reads the code segments in this region that have passed the judgment, except for the current code segment, and determines the number of code segments to be read at one time and the stopping condition; the fifth agent (code assembly agent) merges the code segments read by the fourth agent with the current code segment; the merged code segments are input again into the first agent, the second agent and the third agent for validity judgment, and multiple merging iterations are performed until the stopping condition is reached, and the valid code templates of each source region are output to form a code template library.

[0030] Step 8: Construction and output of RAG retrieval knowledge base: Construct the code template library into a RAG retrieval knowledge base.

[0031] The step of selecting representative code snippets from each cluster and determining candidate programming patterns based on these representative code snippets essentially involves treating each cluster as a candidate programming pattern and using the central code snippet of that cluster as a typical implementation of that candidate programming pattern.

[0032] Example 1: Code Repository Scanning and Filtering. This example details the technical implementation of step one, code repository scanning and filtering.

[0033] A file scanner is used to traverse the target code repository. Appropriate file extension mappings are configured for different programming languages: C++ supports .cpp, .cc, .cxx, .hpp, .h, and .hxx; Python supports .py; Java supports .java; and JavaScript supports .js, .jsx, .ts, and .tsx. During scanning, test directories and hidden directories (such as __pycache__, .git, and .svn) are skipped; for non-Python languages, files containing "test" are skipped. Within the base path, each subdirectory is identified as an independent project, and a list of project names and source file paths for each project are obtained. The list of valid source file paths output in this step serves as input for step two.

[0034] Example 2: Static Code Analysis and Abstract Syntax Tree Construction. This example details the technical implementation of step two: static code analysis and abstract syntax tree construction.

[0035] The source file is parsed into an Abstract Syntax Tree (AST) using a tree-sitter. The corresponding Language object is loaded based on the language parameters; a Parser is created and bound to the Language; the source file is read in byte mode, and `parser.parse(source_bytes)` is called to obtain the Tree. The root node of the Tree is the root node, and each node has attributes such as type, start_point, end_point, start_byte, end_byte, and children. `start_point` and `end_point` are (row, column) tuples; `start_byte` and `end_byte` are byte offsets used to extract code from the source file slice. If parsing throws an exception or returns None, the file is skipped.

[0036] Preferably, it supports automatic detection of multiple encodings (UTF-16BOM, UTF-8, Latin-1, etc.) to ensure that source files with different encodings can be correctly parsed.

[0037] Example 3: Multi-granularity programming pattern representation recognition. This example details the technical implementation of step three, multi-granularity programming pattern representation recognition.

[0038] 3.1. Function Node Extraction A depth-first traversal is performed on the AST to identify function definition nodes. Based on the language, a set of function-level node types is configured: C++ includes `function_definition`, `function_declarator`, `method_definition`, `class_specifier`, `template_declaration`, and `template_function`; Python includes `function_definition` and `class_definition`; Java includes `method_declaration`, `class_declaration`, `interface_declaration`, and `constructor_declaration`; JavaScript includes `function_declaration`, `function`, `method_definition`, `class_declaration`, and `arrow_function`. To avoid duplicate counting of nested functions, the line number range of processed functions is recorded, and nodes falling within this range are excluded.

[0039] 3.2. Node Information Extraction and Granularity Division For each function node, a depth-first traversal is performed to extract a list of node information, including depth, position range (extent), node type, code snippet, and number of direct child nodes (ast_num). Based on node type, three granularities are defined: function-level and block-level (func_kind, block_kind) are extracted directly; statement-level (stmt_kind) requires nesting checks, and only the outermost statement is extracted. After code snippet extraction, comments and redundant line breaks are removed to obtain the cleaned text.

[0040] 3.3. Filtering and Data Storage Set filtering criteria: minimum number of nodes in a function, minimum number of child nodes in a node, and minimum number of code snippets in a project. Save the parsed results as structured data, including project name, file path list, function AST list, and function source code list, forming a three-dimensional data structure of "project-file-function" for use in step four.

[0041] Example 4: Code Representation Vectorization and Meta-Information Assembly. This example details the technical implementation of step four, code representation vectorization and meta-information assembly.

[0042] 4.1. Embedding Model A pre-trained code embedding model is used to map code snippets into fixed-dimensional semantic vectors. Optional models include CodeLlama-7B, UniXcoder, CodeBERT, etc., employing a Transformer architecture. Each code snippet is tokenized and fed into the model, where average pooling is used to obtain the embedding vector.

[0043] 4.2. Meta-information Assembly Each embedded code snippet is associated with an info record in the format [pro_name, file_name, extent_root, node_info]. pro_name is the project name; file_name is the current file path; extent_root is the root node extent of the function to which the snippet belongs; and node_info contains complete information about the current node (including depth, extent, kind, code_snippet, ast_num, etc.). The semantic vectors are assembled with the above AST structure information into unified code metadata, outputting a data structure containing columns such as pros_name, pros_src, pros_emb, and pros_info, for use in step five, clustering.

[0044] Example 5: Semantic Similarity Code Clustering. This example details the technical implementation of step five, semantic similarity code clustering.

[0045] DBSCAN is used to cluster semantic vectors, with parameters including eps and min_samples, and cosine distance is used. After clustering, samples with a label of -1 are considered noise points; other samples with the same label belong to the same cluster. For each non-noise cluster, the centroid is calculated, and the code segment closest to the centroid is selected as the representative of the cluster (center_point). The else_point, cluster_size, center_point_info, infos, and loc_label are recorded. loc_label is the concatenation of the first three items of center_point_info, in the format "project name-file name-extent_root". The clustering results are saved as clusters.pkl for use in steps six and seven.

[0046] Example 6: Variable Element Identification and Abstraction Generalization. This example details the technical implementation of step six, variable element identification and abstraction generalization.

[0047] Lexicalization is performed on each code fragment within a cluster, and positional consistency comparison is conducted using the cluster representative (center_point) as a reference. A sliding window (e.g., window size 2) is used to analyze the lexical sequence; those with a matching ratio higher than a threshold (e.g., 0.8) are retained as common elements; otherwise, they are replaced with abstract placeholders based on semantic type. <str>(string) <const>(constant), <type>(type), <var>(variable), <fun>(Function call). After abstraction, a general code template that can be reused across scenarios is obtained for use in step seven.

[0048] Example 7: Programming Pattern Determination and Code Template Synthesis for Multi-Agent Collaboration. This example details the technical implementation of step seven, the programming pattern determination and code template synthesis for multi-agent collaboration, such as... Figure 2 As shown.

[0049] 7.1. Grouping by source region All candidate code segments in the general code template are grouped by location label (loc_label, in the format "project name-file name-extent_root"). Code segments under the same loc_label originate from the same source region (the same function or the same code block), have contextual relevance, and facilitate subsequent determination and synthesis within the same context.

[0050] 7.2. Programming Pattern Determination: Three-Agent Determination System (Semantic and Syntactic Parallel) For each group of candidate code segments, the programming pattern is first determined. Three agents are established: semantic_agent (semantic clarity agent), syntax_agent (syntax logic agent), and judge_agent (comprehensive judgment agent).

[0051] The Semantic Clarity Agent evaluates naming quality, intent clarity, and understandability, scoring from 0 to 100. The Syntax and Logic Agent evaluates syntactic correctness, logical flow, control flow, and exception handling, also scoring from 0 to 100. Both the Semantic Clarity Agent and the Syntax and Logic Agent receive the same code segment input in parallel, and both can simultaneously invoke a large language model, improving decision-making efficiency.

[0052] The outputs of both (SemanticClarityResult and SyntaxLogicResult) are aggregated and input into the comprehensive judgment agent. The comprehensive judgment agent determines whether the code conforms to the programming pattern standard based on the scores from both dimensions; if both dimensions are ≥80, it is directly judged as a programming pattern; if either dimension is ≥60, it may be a programming pattern but requires further analysis and judgment using a large language model; otherwise, it is judged as a non-programming pattern. All code segments judged as valid programming patterns within this region are retained for subsequent synthesis.

[0053] 7.3. Code Template Synthesis: Planning the Synthesis Agent and the Code Assembly Agent After the programming pattern is determined, the code segments that pass the determination within each group are synthesized. Two synthesis-related agents are established: (1) Planning Synthesis Agent: Responsible for planning and iterative control of the synthesis strategy. This agent attempts to read other code segments within the region determined by the programming pattern through tool calls. The capabilities of the tool calls include: querying the list of other valid code segments within the region (within the same loc_label) based on the current code segment identifier; and obtaining the content and metadata of a specified code segment. The planning synthesis agent needs to decide: the number of other code segments to read at one time (e.g., 1, 2, or more); and when to stop (e.g., no mergeable code segments within the region, invalid after merging, reaching the maximum number of iterations, or the plan considers a complete template to have been formed). Once the planning synthesis agent finds a suitable code segment, it passes the selected code segment identifier and the current code segment identifier to the code assembly agent.

[0054] (2) Code Assembly Agent (code_assembly_agent): Responsible for performing the actual code merging. It receives the code segment selected by the planning and merging agent and the current code segment. Based on semantic relationships, common collocations (such as try-finally, init-cleanup, open-close), shared variables, and other rules, it merges the selected code segment with the current code segment into a syntactically correct and logically coherent code block. Merging rules include: maintaining logical order and dependencies; removing redundant declarations; ensuring self-containment and executableness. After merging, the merged code segment is output.

[0055] 7.4. Re-evaluation and Iteration of Merged Results The merged code segment output by the code assembly agent is input again into the semantic clarity agent, syntax logic agent, and comprehensive judgment agent, and the same judgment process as in 7.2 is performed. If the merged code segment is determined to be a valid programming pattern, the merge result is retained; the planning synthesis agent decides whether to continue trying the next round of merging based on the current status (such as the remaining mergeable code segments in this area, the number of merges already performed, the size of the merged code, etc.). If the merged code segment is determined to be invalid, or the planning synthesis agent decides to stop, the synthesis process of the current code segment ends, and the finally valid code segment (which may be the original single segment or a composite segment after multiple merges) is added to the code template list.

[0056] Since the entire process may require multiple mergings, the decision to stop planning the synthesis agent is crucial. This can be achieved based on the preset maximum number of iterations, the upper limit of the size of the merged code segment, or the judgment of the large language model on "whether a complete template has been formed".

[0057] 7.5. Output and Storage For each source region, after the above judgment and multiple rounds of synthesis, the final retained valid code template (including the original single segment and the synthesized composite segment) is serialized and saved as {repo}_idiom_syn.pkl by project for use in step eight to build the RAG knowledge base.

[0058] Example 8: Construction and Output of RAG Retrieval Knowledge Base. This example details the technical implementation of step eight, the construction and output of the RAG retrieval knowledge base.

[0059] The determined and synthesized code template library is stored in a structured manner. The stored content includes: code snippets (center_point), semantic vectors (which can reuse the embedding or re-encoding from step four), and metadata (info, cnt, avg_ast_num, loc_label, etc.). A knowledge base for RAG retrieval is constructed: the query vector is obtained by encoding the user task or context; the similarity between the query vector and the code template vector is calculated, and the Top-K code templates are retrieved based on similarity; the retrieval results are injected into the contextual prompts of the large language model; the large language model generates output based on the injected code templates. This knowledge base can be applied to software engineering tasks such as unit test generation, programming style recognition, and code completion, realizing a closed loop of "retrieval—injection—generation" in RAG.

[0060] Example 9: System Implementation and Module Integration. This invention also provides a programming pattern mining system based on a large language model, comprising: Scanning and filtering module: Integrates a file scanner, supports multi-language file extension configuration, and enables code repository traversal and filtering.

[0061] Static parsing module: Integrates parsers such as tree-sitter to parse source files into abstract syntax trees.

[0062] Representation recognition module: Configures language-specific node type mapping tables (func, block, stmt) to achieve code snippet extraction at three granularities.

[0063] Vectorization and Assembly Module: Integrates code embedding models (CodeLlama, UniXcoder, CodeBERT) to assemble semantic vectors and AST structures into code meta-information.

[0064] Clustering module: Integrates DBSCAN clustering, supports parameter configuration and optional Bayesian optimization.

[0065] Abstract generalization module: Implements lexicalization, sliding window consistency analysis, and abstract placeholder replacement.

[0066] The decision synthesis module integrates five types of agents based on a multi-agent architecture: semantic clarity agent and syntactic logic agent (working in parallel), comprehensive decision agent, planning synthesis agent (reading the code segment in this area through tool calls and deciding on the synthesis strategy and stopping conditions), and code assembly agent (performing code merging). The merged code segment is verified again by the three decision agents, supporting multiple rounds of iteration; it also supports switching between multiple large language models.

[0067] RAG Knowledge Base Building Module: Stores the code template library in a structured manner and outputs it as a RAG retrieval knowledge base.

[0068] The modules are connected through standardized data formats to form an end-to-end eight-stage pipeline.

[0069] Example 10: Programming Pattern Mining Device, Electronic Device, and Storage Medium Based on Large Language Model Corresponding to the aforementioned embodiments of the programming pattern mining method based on a large language model, this embodiment of the invention also provides a programming pattern mining apparatus based on a large language model. See also Figure 3 The device may include one or more processors for executing the steps in the above method embodiments to achieve programming pattern mining, code template synthesis, and related processing functions. The device can be deployed in any device with data processing capabilities, such as a computer, server, workstation, or other electronic device. The device can be implemented in software, hardware, or a combination of both. In software implementation, the processor in the device reads and executes computer program instructions stored in non-volatile memory and runs them in memory to form corresponding functional modules; in hardware implementation, corresponding hardware units collaboratively complete the above functions. Figure 3 In addition to the processor, memory, network interface, and non-volatile memory shown, the device may include other hardware structures as needed for actual applications, which will not be elaborated here. The functions, roles, and specific implementation processes of each unit in the above device embodiments can be found in the relevant descriptions in the foregoing method embodiments, and will not be repeated here. It should be understood that the above device embodiments are merely illustrative. The functional units may be physically independent, integrated into the same physical entity, or distributed across multiple physical entities. Those skilled in the art can configure them accordingly based on actual needs.

[0070] Corresponding to the foregoing method embodiments, this application also provides an electronic device, including: one or more processors, and a memory for storing one or more programs; when the one or more programs are executed by the one or more processors, the one or more processors implement the method steps of the above-described programming pattern mining method based on a large language model. See also Figure 4 , Figure 4 This illustration shows a hardware structure diagram of any electronic device with data processing capabilities according to an embodiment of this application. The electronic device may include a processor, an internal bus, a network interface, and a memory, etc., and the various hardware modules can interact with each other via the internal bus. Depending on the actual application scenario and functional requirements, the electronic device may also include other hardware modules, which will not be elaborated here.

[0071] Corresponding to the foregoing method embodiments, this invention also provides a computer-readable storage medium storing a computer program thereon. When executed by a processor, the computer program is used to implement the steps of the above-described programming pattern mining method based on a large language model. The computer-readable storage medium can be an internal storage unit in the device described in any of the foregoing embodiments, such as a hard disk or memory; it can also be an external storage device, such as a plug-in hard disk, smart memory card, SD card, or flash memory card; or it can include both internal storage units and external storage devices. The computer-readable storage medium can be used to store the computer program and other programs and data required for the operation of the device, and can also be used to temporarily store data that has been output or will be output.

[0072] The above description is merely a preferred embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, or alterations made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.< / fun> < / var> < / type> < / const> < / str>

Claims

1. A method for mining programming patterns based on a large language model, characterized in that, Includes the following steps: S1. Traverse the target code repository and filter valid source files based on the language configuration; S2. Parse the valid source file into an abstract syntax tree and extract node information; S3. Extract candidate code fragments from the abstract syntax tree according to three granularity levels: function level, statement level, and interval level. S4. Semantic embedding encoding is performed on the candidate code fragments to obtain semantic vectors, and the abstract syntax tree structure and the semantic vectors are assembled into code meta-information; S5. The semantic vectors are clustered using a density-based clustering algorithm. Based on the code meta-information, representative code segments are selected from each cluster, and candidate programming patterns are determined based on the representative code segments. S6. Perform lexical processing on the candidate programming pattern, identify variable elements based on consistency analysis within the cluster, replace the variable elements with abstract placeholders, and generate a general code template. S7. Group the generalized code templates according to the same source region; For each candidate code segment within a group, the first and second agents are invoked in parallel to evaluate the semantic clarity and syntactic logic of the candidate code segments, respectively. The evaluation results are then input to the third agent for comprehensive judgment, and the code segments judged as valid programming patterns are retained as the current code segment. The fourth agent reads the code segments in this region that have passed the judgment, excluding the current code segment, and determines the number of code segments to be read at one time and the stopping condition. The fifth agent merges the code segments read by the fourth agent with the current code segment. The merged code segment is then input into the first, second, and third agents again for validity judgment, and multiple merging iterations are performed until the stopping condition is reached. Valid code templates for each source region are output, forming a code template library. S8. Construct the code template library into a RAG retrieval knowledge base.

2. The method of claim 1, wherein, In step S3, the granularity level is specifically: function-level granularity, used to extract the abstract syntax tree node corresponding to the complete function definition; statement-level granularity, used to extract the abstract syntax tree node corresponding to a single statement. Interval-level granularity is used to extract consecutive syntax blocks corresponding to control structures, loop bodies, or exception handling blocks.

3. The method of claim 1, wherein, In step S4, the semantic embedding encoding is implemented using a pre-trained code language model, which is an encoder based on the Transformer architecture.

4. The method of claim 1, wherein, In step S5, the density-based clustering algorithm is the DBSCAN algorithm, and the representative code segment is the code segment in each cluster that is closest to the centroid of the cluster.

5. The method of claim 1, wherein, In step S6, the consistency analysis uses a sliding window to compare the word sequences of each code segment within the cluster. When the matching ratio of a word in the cluster is higher than a preset threshold, it is retained as a common element; otherwise, it is replaced with an abstract placeholder. The types of the abstract placeholder include: string type, constant type, type identifier type, variable type, and function call type.

6. The method of claim 1, wherein, In step S7, the code merging determination conditions for merging the code segment read by the fourth agent with the current code segment through the fifth agent include: the code segments originate from the same abstract syntax tree, share context variables, have similar structural features, or have a co-occurrence relationship.

7. The method of claim 1, wherein, Step S8 includes: storing the code fragments, semantic vectors, and meta-information of the code template library in a structured manner to form a knowledge base that can be retrieved by RAG.

8. The method according to claim 1, characterized in that, The RAG retrieval knowledge base is used for retrieval enhancement generation in software engineering tasks, including unit test generation, programming standard identification, and code completion.

9. A programming pattern mining system based on a large language model, characterized in that, include: The scanning and filtering module is used to traverse the target code repository and filter valid source files based on the language configuration; The static parsing module is used to parse the valid source file into an abstract syntax tree and extract node information; The representation recognition module is used to extract candidate code fragments from the abstract syntax tree according to three granularity levels: function level, statement level, and interval level. The vectorization and assembly module is used to perform semantic embedding encoding on the candidate code fragments to obtain semantic vectors, and to assemble the abstract syntax tree structure and the semantic vectors into code meta-information. The clustering module is used to cluster the semantic vectors using a density-based clustering algorithm, select representative code segments from each cluster based on the code meta-information, and determine candidate programming patterns based on the representative code segments. The abstract generalization module is used to perform lexical processing on the candidate programming patterns, identify variable elements based on consistency analysis within clusters, replace the variable elements with abstract placeholders, and generate a generalized code template. The determination and synthesis module is used to perform programming pattern determination and code template synthesis based on a multi-agent architecture. The module includes: grouping the generalized code templates according to the same source region; for each group of candidate code segments, simultaneously calling the first agent and the second agent to evaluate the semantic clarity and syntactic logic of the candidate code segments, and inputting the evaluation results to the third agent for comprehensive determination, retaining the code segments determined as valid programming patterns as the current code segment; reading the determined code segments in the same region (excluding the current code segment) through the fourth agent, determining the number of code segments to be read at once and the stopping condition; merging the code segments read by the fourth agent with the current code segment through the fifth agent; inputting the merged code segment back into the first agent, second agent, and third agent for validity determination, performing multiple merging iterations until the stopping condition is met, and outputting the valid code templates for each source region, thus forming a code template library. The RAG knowledge base construction module is used to build the code template library into a RAG retrieval knowledge base.

10. The system according to claim 9, characterized in that, The representation recognition module supports static analysis tools for multiple programming languages ​​and enables multilingual extensions by configuring language-specific node type mappings.