Method and equipment for generating code map for project and storage medium
By dividing the code files in the code base into code snippets and generating vector representations, and combining large language models to generate UML diagrams, it solves the problem that programmers have difficulty in quickly understanding the structure of the code project, and achieves rapid and intuitive code understanding and improvement of development efficiency.
Patent Information
- Application Number
- CN202411736746.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-29
- Publication Date
- 2025-02-25
AI Technical Summary
During the programming process, programmers need to spend a lot of time understanding the overall logic and details of new code projects. Traditional methods lack inter-file interaction diagrams, resulting in time-consuming and easy omission of key logic or modules.
By dividing the code files in the code base into code snippets and generating vector representations for each code snippet, it is stored in the vector database. Use a large language model to extract the project structure tree, generate UML generation plans, and generate the UML diagram of the current project based on this. Display the UML diagram through the rendering engine and search in vector matching in response to user clicks to generate a detailed view.
It enables developers to quickly and intuitively understand the connection between the complete structure of the project and the functional modules, lowers the threshold for code understanding, and improves development efficiency.
Smart Images

Figure CN119512531A_ABST
Abstract
Description
Technical Field
[0001] This application belongs to the field of software engineering technology, and specifically relates to a method, device and storage medium for generating code maps for a project. Background Technology
[0002] During the programming process, programmers typically need to spend a significant amount of time understanding the overall logic and details of a new code project. Traditional methods mainly rely on examining the structure of code files, lacking a diagram of the interaction relationships between files. Programmers need to read through each code file and manually analyze the call relationships, which is not only time-consuming but also prone to overlooking critical logic or modules.
[0003] Therefore, how to enable developers to quickly and intuitively understand the complete structure of a project and the relationships between its functional modules is a topic worthy of study by those skilled in the art. Summary of the Invention
[0004] Based on the above analysis, the embodiments of the present invention aim to provide a method, device, and storage medium for generating code maps for projects, so that developers can quickly and intuitively understand the complete structure of the project and the relationships between functional modules.
[0005] A first aspect of this application provides a method for generating code maps for a project, comprising:
[0006] The code files in the code library are divided into code fragments, a corresponding vector representation is generated for each code fragment, and the code fragments and their vector representations are stored in a vector database;
[0007] Extract the project structure tree and use a large language model to generate a UML generation plan based on the project structure tree;
[0008] Based on the UML generation plan and the code in the code library, a large language model is used to generate the UML diagram of the current project;
[0009] The rendering engine displays the UML diagram of the current project on the graphical interface; the nodes in the UML diagram are associated with the vector representations in the code library.
[0010] In response to the user's instruction to select a node in the global UML diagram, the system searches the vector database using vector matching, returns a code snippet related to the selected node, and uses the structural relationships in the code snippet to generate a detailed view related to the selected node, which is then displayed on the graphical user interface.
[0011] Optionally, the code snippet related to the selected node is returned, and then:
[0012] Locate the code snippet within the project.
[0013] Optionally, the code snippet related to the selected node is returned, and then:
[0014] Analyze the dependencies between the selected node and other nodes in the project;
[0015] A large language model is used to extract information from the code snippets and dependencies, generating semantic explanations of natural language expressions.
[0016] In response to a user's instruction to interpret a selected node, the semantic interpretation information is displayed on the graphical user interface.
[0017] Optionally, it also includes:
[0018] In response to a user's input command to select a specific function, the code segment corresponding to the specific function is retrieved from the code library;
[0019] Based on the characteristics of the code segment, determine the matching UML diagram type;
[0020] Generate a code segment for the corresponding UML diagram type.
[0021] Optionally, the step of dividing the code files in the code library into code fragments and generating a corresponding vector representation for each code fragment includes:
[0022] Abstracting classes, methods, and related dependencies in a codebase using Abstraction Abstraction (AST);
[0023] Divide the codebase into modules or functional blocks, and each input model contains only one module and its direct dependencies;
[0024] The code embedding model is used to generate a corresponding vector representation for each code fragment.
[0025] Optionally, the generation plan for generating UML using a large language model based on the project structure tree includes:
[0026] The large language model is used to determine the modules to be presented, the UML diagram process, and the type of UML diagram to be presented for the project based on the project structure tree.
[0027] Optionally, the large language model can be used to determine the modules to be presented based on the project structure tree, including:
[0028] The large language model is used to extract functional descriptions from module names, method names, and comments;
[0029] The core modules are selected based on preset rules as the modules to be presented.
[0030] Optionally, the type of UML diagram used to represent the project can be determined based on the project structure tree, including:
[0031] The system receives user input requests and uses a large language model to perform semantic parsing on the user requests to generate request information.
[0032] Based on the characteristics and requirements of the modules, determine the type of UML diagram to present the project.
[0033] A second aspect of this application provides an apparatus for generating code maps for a project, including a memory and a processor, the memory storing a computer program that, when executed by the processor, implements the method for generating code maps for a project according to any of the above-described methods.
[0034] A third aspect of this application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method for generating a code map for a project according to any of the above-described methods.
[0035] The method for generating code maps for projects provided in this application involves segmenting code files in a code repository into code fragments, generating a corresponding vector representation for each code fragment, and storing the code fragments and their vector representations in a vector database. The method then extracts the project structure tree and uses a large language model to generate a UML generation plan based on the project structure tree. Based on the UML generation plan and the code in the code repository, a large language model is used to generate the UML diagram for the current project. A rendering engine displays the UML diagram of the current project on a graphical user interface. In response to a user's command to select a node in the global UML diagram, the method searches the vector database using vector matching, returns code fragments related to the selected node, and uses the structural relationships within the code fragments to generate a detailed view related to the selected node, which is then displayed on the graphical user interface. Based on the code map provided in this application, a UML diagram of the entire project can be generated with a single click, allowing users to quickly and intuitively understand the complete structure of the project and the relationships between its functional modules.
[0036] In addition, this application also provides a device and storage medium for generating code maps for projects with the above-mentioned technical effects. Attached Figure Description
[0037] To more clearly illustrate the technical solutions in the embodiments of this specification or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the embodiments of this specification. For those skilled in the art, other drawings can be obtained based on these drawings.
[0038] Figure 1 A flowchart illustrating a specific implementation of the method for generating code maps for a project provided in this application;
[0039] Figure 2 A schematic diagram illustrating the method for generating code maps for a project provided in this application;
[0040] Figure 3 A structural block diagram of the device for generating code maps for a project, provided in this application. Detailed Implementation
[0041] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. It should be noted that, unless otherwise specified, the implementation methods and features in the implementation methods in this disclosure can be combined, separated, interchanged, and / or rearranged. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0042] The terminology used herein is for the purpose of describing particular embodiments and is not intended to be limiting. As used herein, unless the context clearly indicates otherwise, the singular forms “a” and “the” are intended to include the plural forms as well. Furthermore, when the terms “comprising” and / or “including” and variations thereof are used in this specification, it indicates the presence of the stated features, integrals, steps, operations, parts, components, and / or groups thereof, but does not exclude the presence or addition of one or more other features, integrals, steps, operations, parts, components, and / or groups thereof. It should also be noted that, as used herein, the terms “substantially,” “about,” and other similar terms are used as approximate terms rather than as terms of degree, thus explaining the inherent biases in measurements, calculated values, and / or provided values that would be recognized by one of ordinary skill in the art.
[0043] A flowchart of a specific implementation of the method for generating code maps for projects provided in this application is shown below. Figure 1 As shown, the method specifically includes:
[0044] S101: Divide the code files in the code library into code fragments, generate a corresponding vector representation for each code fragment, and store the code fragments and their vector representations in a vector database.
[0045] For code files in the codebase, vectorization is required. First, the code files are segmented into code snippets. A code embedding model is then used to generate a vector representation for each code snippet. These vector representations transform the code snippets into semantic embeddings, facilitating rapid association through semantic retrieval during subsequent analysis. The code embedding model converts the code snippets into vectors and stores the generated vectors in a vector database, supporting subsequent retrieval.
[0046] For complex codebases, this application embodiment uses an Abstract Syntax Tree (AST) to abstract the classes, methods, and related dependencies of the codebase; it divides the codebase into modules or functional blocks, with each input model containing only one module and its direct dependencies; and it uses a code embedding model to generate a corresponding vector representation for each code fragment. This avoids the input context from exceeding the model's input token limit.
[0047] Large projects may contain hundreds of files and tens of thousands of lines of code; directly inputting the entire codebase into the language model would exceed the token limit. The token limit defines the total amount of input and output content that the model can process at one time (e.g., 4k, 16k, 128k tokens).
[0048] An Abstract Syntax Tree (AST) is a structured representation of program source code, containing the hierarchical relationships of classes, methods, functions, variables, etc. An AST abstracts the codebase into a hierarchical syntax tree, extracting key structures and dependencies. The information extracted from the AST is used to generate the project's structure tree, providing a foundation for subsequent module partitioning and vectorization. Modular decomposition divides the codebase by module or function, inputting only one module and its related content at a time to avoid the token capping issue.
[0049] S102: Extract the project structure tree and use a large language model to generate a UML generation plan based on the project structure tree.
[0050] A project structure tree is a hierarchical representation of the internal organization of a code project. It displays the files, modules, classes, methods, and their hierarchical relationships and dependencies in a tree structure. The project structure tree provides developers or tools with a static view of the project, helping them understand how the code is organized and the relationships between modules.
[0051] The project structure tree is extracted, and a large language model is used to generate a UML generation plan based on the project structure tree. In the plan, the large language model outputs the modules to be presented in the UML diagram, the UML diagram flow, and the type of UML diagram representing the project. Traditional ASTs can only generate nodes at the code structure level (classes, methods, variables, etc.), lacking a semantic understanding of business logic and developer intent. This application, however, uses a large language model that can automatically abstract nodes with business semantics through a deep understanding of the code content.
[0052] The process of using a large language model to determine the modules to be presented based on the project structure tree includes: extracting functional descriptions from module names, method names, and comments using the large language model; and selecting core modules as the modules to be presented based on preset rules. Specifically, the large language model can identify key modules based on dependencies, functional descriptions, and user specifications, and filter out irrelevant modules to obtain the modules to be presented in the UML diagram. Here, dependencies refer to core modules called by multiple modules, functional descriptions refer to modules that identify those associated with the main business logic, and user specifications refer to modules corresponding to the range of user input.
[0053] The method of using a large language model to determine the type of UML diagram to present the project based on the project structure tree includes: receiving user input requirements, using a large language model to perform semantic parsing of the user requirements to generate requirement information; and determining the type of UML diagram to present the project based on the characteristics of the modules and the requirement information. Different UML diagram types may include, but are not limited to, sequence diagrams, class diagrams, flowcharts, state diagrams, etc., suitable for different scenarios in different projects.
[0054] One specific implementation method is as follows: if the dependencies between modules are significant, then the UML diagram type for presenting the project is determined to be a class diagram. If the call chain logic is clear, then the UML diagram type for presenting the project is determined to be a sequence diagram. If the user specifies the functionality, then the UML diagram type for presenting the project is determined to be a flowchart. Pre-generating the plan allows the generated UML diagram to avoid being overly granular and is also easier for developers to understand.
[0055] S103: Based on the generation plan of the UML and the code in the code library, generate the UML diagram of the current project using the large language model.
[0056] After generating the plan in the previous step, a UML diagram can be generated. Specifically, based on the complete code or abstracted complete code from the model codebase and the generated plan, the UML diagram of the current project is presented by writing Mermaid code.
[0057] S104: Display the UML diagram of the current project on the graphical interface through the rendering engine; where the nodes in the UML diagram are associated with the vector representations in the code library.
[0058] Specifically, the extracted code structure (classes, methods, relationships) can be mapped to Mermaid code format. The converted Mermaid description is then output as text. A Mermaid rendering tool transforms the text code into a graphical representation. By providing a clear and intuitive graphical representation, it helps developers quickly understand the project structure and logic.
[0059] S105: In response to the user's instruction to select a node in the global UML diagram, the vector database is searched using vector matching to return a code snippet related to the selected node. Using the structural relationships in the code snippet, a detailed view related to the selected node is generated and displayed on the graphical interactive interface.
[0060] After Mermaid is rendered as an SVG image, each node can be parsed from the image, and users can click on the nodes. Since the codebase was vectorized in the previous steps, code related to different nodes can be retrieved separately using vector matching.
[0061] The process includes returning the code snippet associated with the selected node, and locating the code snippet within the project.
[0062] Through vector matching, this application embodiment can quickly retrieve code related to nodes and locate the file and line number where the code is located, saving developers time in manually searching for code.
[0063] Large language models can understand user input via natural language and dynamically adjust the fine-grainedness of UML diagrams. It is understood that, in this embodiment, the fine-grainedness can be divided into: Global view: corresponding to low fine-grainedness, only showing the dependencies between modules; Internal module view: corresponding to high fine-grainedness, showing the structure of classes and methods.
[0064] Furthermore, this application can generate more detailed sub-diagrams for any node in the UML diagram, progressively revealing the node's internal structure or calling relationships. Only global module relationships are displayed in the global UML diagram. Clicking on a node generates a detailed view of that module's internal structure. Understandably, users can delve deeper, from the global view to modules, then to classes, methods, and finally to specific code details. Each generated sub-diagram or code explanation becomes a new entry point for further user operations, forming a closed loop.
[0065] After returning the code snippet related to the selected node, the method further includes: parsing the dependency relationship between the selected node and other nodes in the project; using a large language model to extract information from the code snippet and dependency relationship to generate semantic explanation information in natural language; and responding to the user's input instruction to explain the selected node by displaying the semantic explanation information on the graphical user interface.
[0066] This application leverages the semantic understanding capabilities of large-scale models to generate natural language explanations for UML graph nodes that users do not understand. It can automatically analyze the semantics of nodes and generate clear, easy-to-understand explanations. Users can obtain information about specific nodes by asking questions in natural language.
[0067] Furthermore, the method provided in this application may further include: responding to a user input instruction to select a specific function, retrieving a code segment from a code library corresponding to the specific function; determining a matching UML diagram type based on the characteristics of the code segment; and generating a code segment generation diagram corresponding to the UML diagram type.
[0068] A specific function can be a module or logical unit within a project. A code segment is a specific scope of code, such as a class, a function, or a block of code.
[0069] Users can specify a function using natural language, and the system responds to user input by understanding the description and locating the corresponding code segment. Alternatively, users can directly select a code file or a specific code segment.
[0070] Based on the characteristics of the code segment, the matching UML diagram type is determined; a code segment generation diagram of the corresponding UML diagram type is then generated. The process of generating a diagram for a specific function or code segment can be understood as a visualization operation that focuses on a specific scope from the overall project. This application embodiment, by limiting the scope and generating UML diagram types corresponding to a specific code scope, can quickly and intuitively display the structure and logic of the function or code segment, significantly improving code understanding and collaboration efficiency.
[0071] Combined with attachment Figure 2 This application provides a schematic diagram of the method for generating code maps for projects. The method provided in this application will be further explained in detail below.
[0072] Table 1 shows the implementation method for generating code maps and the context parameters required.
[0073] Table 1
[0074] Summary_plan tree, type (optional) Uml_summary code, tree, summary_plan Detail_plan detail_code, tree, connection, target, type (optional) Uml_detail detail_code, tree, detail_plan Node_explain detail_code,tree,target,connection
[0075] The process begins by generating a UML summary plan based on the project's tree structure (type, tree). The tree describes the codebase's directory structure, file hierarchy, and module relationships. The type (optional) specifies the diagram type, such as a class diagram, sequence diagram, or flowchart. If not specified, a suitable diagram type will be automatically recommended based on the code logic.
[0076] Based on the code, tree structure, and summary plan, generate a UML diagram summary (Uml_summary). Here, code represents the complete codebase. If the code is too long, the input can be reduced through abstraction (e.g., extracting only class and method definitions).
[0077] The generated UML diagram will be viewed by the user, serving as a global diagram illustrating the relationships between modules. Users can click on a node to generate a detailed plan (Detail_plan). Based on the plan, a more granular UML diagram (Uml_detail) is generated. Detail_code is the retrieved code related to the current node. Tree is the code structure tree, helping to resolve the dependencies of the current node. Connection is the node directly related to the current node (nodes with a dependency distance of 1). Target is the name of the target node clicked by the user. type (optional) specifies the subgraph type (e.g., class diagram or sequence diagram); if not specified, a recommended subgraph type will be automatically generated.
[0078] When a user clicks on or queries the function or logic of a specific node, a node explanation (Node_explain) is generated. While explaining the node's functionality, the corresponding code file and line number are located, and a code snippet is provided for developers to view. Here, `detail_code` is the code snippet for the target node, facilitating explanation generation. `tree` is used to analyze the node's context information. `Target` is the name of the target node clicked by the user. `Connection` are the connection nodes directly related to the target node, helping to supplement the context.
[0079] Users can repeatedly click on new nodes or return to the previous view to generate new plans and fine-grained charts. It supports progressive exploration from the global view to local views and then to the code. Each newly generated chart or explanation can further trigger subsequent operations, forming a dynamic interactive loop.
[0080] In summary, this application combines the natural language processing capabilities, code semantic analysis capabilities, and graph generation and interactive extension capabilities of a large language model. By adjusting the granularity and natural language interpretation, it ensures that UML diagrams remain intuitive even in complex projects. Users can gradually delve from a macro perspective to specific implementation details, forming a hierarchical understanding. Users can ask questions about nodes they don't understand and quickly and accurately obtain corresponding explanations and code locations. The method provided in this application helps users quickly and efficiently understand code structure and logic in complex projects, lowering the barrier to code comprehension and improving development efficiency and user experience.
[0081] In addition, this application also provides a device for generating code maps for projects, such as Figure 3 The structural block diagram of the device for generating code maps for a project provided in this application shows that the device specifically includes a memory 31 and a processor 32. The memory 31 stores a computer program, which, when executed by the processor 32, implements the method for generating code maps for a project according to any of the above-described methods.
[0082] In addition, this application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method for generating a code map for a project according to any of the above-described methods.
[0083] Computer-readable storage media include both permanent and non-permanent, removable and non-removable media that can store information by any method or technology. Information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic magnetic disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable media does not include transient computer-readable media, such as modulated data signals and carrier waves.
[0084] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0085] The steps of the methods or algorithms described in conjunction with the embodiments disclosed herein can be implemented in hardware, a software module executed by a processor, or a combination of both. The software module can be located in random access memory (RAM), main memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, removable disk, CD-ROM, or any other form of storage medium known in the art.
[0086] The specific embodiments described above further illustrate the purpose, technical solution, and beneficial effects of this application. It should be understood that the above description is only a specific embodiment of this application and is not intended to limit the scope of protection of this application. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of protection of this application.
Claims
1. A method for generating a code map for a project, characterized in that: include: Divide the code files in the code library into code fragments, generate a corresponding vector representation for each of the code fragments, and store the code fragments and their vector representations in a vector database; Extract the project structure tree and use the large language model to generate the UML generation plan based on the project structure tree; Based on the UML generation plan and the code in the code base, a UML diagram of the current project is generated using a large language model; The UML diagram of the current project is displayed on a graphical interactive interface through a rendering engine; wherein the nodes in the UML diagram are associated with the vector representation in the code base; In response to a user clicking on a selected node in a global UML diagram, a search is performed in the vector database by means of vector matching, and a code snippet related to the selected node is returned. A detailed view related to the selected node is generated by using the structural relationship in the code snippet and displayed on the graphical interactive interface.
2. The method for generating a code map for a project according to claim 1, characterized in that: After returning the code snippet related to the selected node, the method further includes: Navigate to the location of the code snippet in the project.
3. The method for generating a code map for a project according to claim 2, characterized in that: After returning the code snippet related to the selected node, the method further includes: Resolving the dependencies of the selected node with other nodes in the project; Using a large language model to extract information from the code snippets and dependencies, and generating semantic interpretation information expressed in natural language; In response to a user input instruction to interpret the selected node, the semantic interpretation information is displayed on the graphical interaction interface.
4. The method for generating a code map for a project according to claim 1, characterized in that: Also includes: In response to an instruction input by a user to select a specific function, a code segment corresponding to the specific function is retrieved from a code library; Based on the characteristics of the code segment, determining a matching UML diagram type; Generates code snippet diagrams of corresponding UML diagram types.
5. The method for generating a code map for a project according to any one of claims 1 to 4, characterized in that: The dividing the code files in the code library into code fragments and generating a corresponding vector representation for each of the code fragments includes: Abstract the classes, methods, and related dependencies of the code library through AST; Divide the code base into modules or functions, so that each input model only contains one module and its direct dependencies. A code embedding model is used to generate a corresponding vector representation for each of the code snippets.
6. The method for generating a code map for a project according to claim 5, characterized in that: The generation plan of generating UML according to the project structure tree using the large language model includes: A large language model is used to determine the modules, UML diagram flow and UML diagram type to be presented according to the project structure tree.
7. The method for generating a code map for a project according to claim 6, characterized in that: The modules that need to be presented are determined by using a large language model according to the project structure tree, including: Use a large language model to extract functional descriptions from module names, method names, and comments; Combine preset rules to filter core modules as the modules that need to be presented.
8. The method for generating a code map for a project according to claim 7, characterized in that: The UML diagram types used to present the project according to the project structure tree using the large language model include: Receive user requirements input by users, perform semantic analysis on the user requirements using a large language model, and generate requirement information; Determine the type of UML diagram to present the project based on the module's characteristics and requirement information.
9. A device for generating a code map for a project, characterized in that: The method comprises a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the method for generating a code map for a project according to any one of claims 1 to 8 is implemented.
10. A computer-readable storage medium, characterized in that: A computer program is stored thereon, and when the computer program is executed by a processor, the method for generating a code map for a project according to any one of claims 1-8 is implemented.
Citation Information
Cited By
Function feature-oriented code map construction method and system
CN119718276A
Method for visual presentation of software project and related device
WO2026184221A1