Code document generation method and device, electronic equipment and storage medium
By converting source code into an abstract syntax tree and performing depth-first traversal and node analysis, the problems of low accuracy and poor coherence in code documentation generation are solved, achieving efficient and standardized code documentation generation and optimization.
Patent Information
- Application Number
- CN202511069239.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-31
- Publication Date
- 2025-11-07
AI Technical Summary
When faced with complex code structures and business logic, existing technologies suffer from low accuracy and poor consistency in code documentation generation, and lack the ability to understand the deep semantics of the code and explain design decisions.
By converting source code into an abstract syntax tree, deep traversal and node analysis are used to extract key structural features, perform hierarchical extraction and context analysis, generate code documentation and score it, and use incremental adjustments to optimize the documentation quality.
It improves the accuracy and consistency of code documentation generation, reduces the cost of manual writing and optimization, and makes the documentation easier to understand and maintain.
Smart Images

Figure CN120909642A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of intelligent decision-making, and in particular to a code document generation method and device, electronic equipment and a storage medium. BACKGROUND
[0002] Code document generation is a technology that extracts and converts key information such as functions, modules and routes in the code into structured documents by parsing the source code, aiming to improve the readability and practicality of the documents. This method not only simplifies the document writing process, but also ensures the synchronous update of document content and code, reducing human error and information lag.
[0003] In the field of financial technology, the technical document of the code document generation transaction system enables developers to quickly grasp the architecture and business logic of the system, thereby significantly improving development efficiency and system stability, ensuring the safety and reliability of financial transactions.
[0004] In the field of medical health, code document generation also plays an important role. It can generate technical documents for electronic health record systems, helping medical professionals accurately understand and use the system, thereby improving the quality and efficiency of medical services and ensuring the safety and privacy protection of patient data.
[0005] In summary, although the code document generation technology has significant advantages in automatically generating technical documents, it still has deficiencies in semantic understanding and context association when facing complex code structures and business logic. Existing technologies mainly rely on static analysis of source code and template generation rules, lack of understanding of deep semantic of code and explanation ability of design decisions, resulting in unstable document quality and lack of context association.
[0006] Therefore, the code document generation in the prior art has the problems of low accuracy and poor coherence, which need to be solved. SUMMARY
[0007] The present application provides a code document generation method, device, electronic equipment and storage medium to solve the problem of low accuracy and poor coherence of code document generation in complex scenarios, thereby improving the efficiency of code document generation.
[0008] In a first aspect, a code document generation method is provided, comprising: obtaining source code and converting the source code into an abstract syntax tree; extracting key structural features of the abstract syntax tree through deep traversal and node analysis; performing hierarchical extraction and context analysis on the key structural features to obtain code feature information; fill the code feature information into a preset technical document template to generate a code document, score the code document to obtain a score result; When the score result is less than a preset threshold, the code document is optimized through incremental adjustment to generate an optimized code document.
[0009] In a second aspect, a code document generation apparatus is provided, comprising: a conversion module configured to obtain source code and convert the source code into an abstract syntax tree; a traversal module configured to extract key structural features of the abstract syntax tree through deep traversal and node analysis; an analysis module configured to perform hierarchical extraction and context analysis on the key structural features to obtain code feature information; a scoring module configured to fill the code feature information into a preset technical document template to generate a code document, score the code document to obtain a score result; an optimization module configured to, when the score result is less than a preset threshold, optimize the code document through incremental adjustment to generate an optimized code document.
[0010] In a third aspect, a computer device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the above code document generation method when executing the computer program.
[0011] In a fourth aspect, a computer readable storage medium is provided, which stores a computer program, wherein the computer program is executable by a processor to implement the steps of the above code document generation method.
[0012] In the above code document generation method, apparatus, electronic device, and storage medium, the source code is converted into an abstract syntax tree, which can accurately capture the syntax structure and logical relationship of the code and lay a foundation for subsequent analysis; the key structural features are extracted through deep traversal and node analysis, which can focus on core information and avoid redundancy; the code feature information is obtained by performing hierarchical extraction and context analysis on the key structural features, which can comprehensively and systematically present the essence of the code; the feature information is filled into a preset template to generate a code document, which can ensure that the document format is standardized, the content is unified, and the generation efficiency is improved; the document is scored and optimized through incremental adjustment when it does not meet the requirements, which can continuously improve the quality of the document, meet different scene requirements, and overall process takes into account accuracy, efficiency, and quality, greatly reducing the cost of manually writing and optimizing the document, and making the code document easier to understand and maintain. BRIEF DESCRIPTION OF DRAWINGS
[0013] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following will briefly introduce the drawings needed to be used in the description of the embodiments of the present application. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those of ordinary skill in the art without any creative effort based on these drawings.
[0014] Figure 1 is an application environment schematic diagram of a code document generation method in an embodiment of the present application; Figure 2 is a flow schematic diagram of a code document generation method in an embodiment of the present application; Figure 3 is Figure 2 is a specific implementation flow schematic diagram of step S1 in the embodiment; Figure 4 is Figure 2 is a specific implementation flow schematic diagram of step S3 in the embodiment; Figure 5 is a structure schematic diagram of a code document generation device in an embodiment of the present application; Figure 6 is a structure schematic diagram of a computer device in an embodiment of the present application; Figure 7 is another structure schematic diagram of a computer device in an embodiment of the present application. DETAILED DESCRIPTION
[0015] The technical solutions of the embodiments of the present application will be described clearly and completely in the following with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are some embodiments of the present application, but not all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without any creative effort belong to the scope of protection of the present application.
[0016] The code document generation method provided in the embodiments of the present application can be applied in, for example, Figure 1In an application environment of the application, a client communicates with a server through a network. The server can obtain source code, convert the source code into an abstract syntax tree, extract key structural features of the abstract syntax tree through deep traversal and node analysis, perform hierarchical extraction and context analysis on the key structural features to obtain code feature information, fill the code feature information into a preset technical document template to generate a code document, score the code document to obtain a score result, and when the score result is less than a preset threshold, optimize the code document through incremental adjustment to generate an optimized code document. The application provides a code document generation device. For a target result business, the abstract syntax tree analysis technology is used to convert source code, extract key features, and perform hierarchical analysis, so that the essence of the code can be accurately captured and redundancy can be avoided. The template filling and document generation can ensure the format specification and improve the efficiency. The scoring and incremental optimization can continuously improve the document quality. The overall process considers accuracy and efficiency, greatly reduces the labor cost, and makes the code document easier to understand and maintain. The client can be, but is not limited to, various personal computers, notebook computers, smart phones, tablet computers, and portable wearable devices. The server can be implemented by an independent server or a server cluster composed of multiple servers. The application will be described in detail through specific embodiments.
[0017] Please refer to Figure 2 , as shown in the figure, Figure 2 A flowchart of a code document generation method provided by an embodiment of the application is shown in the figure, and includes the following steps: S1, obtain source code and convert the source code into an abstract syntax tree.
[0018] In the example of the application, the abstract syntax tree is an abstract representation form of the syntax structure of the source code. It presents the syntax units of the code in a tree structure, and each node represents a syntax structure (such as an expression, a statement, a declaration, etc.). Some details that are irrelevant to semantic analysis (such as parentheses, spaces, etc.) are omitted.
[0019] In the example of the application, as shown in the figure, Figure 3 , the obtaining of the source code and the conversion of the source code into the abstract syntax tree includes: S11, decompose the source code into a token sequence by using lexical analysis; S12, construct an initial abstract syntax tree from the token sequence by using syntax analysis; S13, map the initial abstract syntax tree to a unified format by using a standardization algorithm to obtain an abstract syntax tree.
[0020] In the examples of the present application, the source code is sequentially read as a character stream, and a lexical unit with independent semantics is identified according to a predefined lexical rule (such as a regular expression), each lexical unit including a type (such as an identifier, a keyword, or a constant) and a value (such as a variable name or a literal), the characters are scanned through a sliding window, the current lexical unit is truncated when a blank or a separator is encountered, if the combination of characters matches the lexical rule, a corresponding lexical unit is generated and the subsequent characters are processed, and finally the entire source code is converted into an ordered token sequence.
[0021] In the examples of the present application, the token sequence is constructed into an initial abstract syntax tree through syntax analysis, including: The first token of the token sequence is obtained, and a shift operation is performed on the first token and the first token is pushed into a pre-constructed analysis stack; The top token in the analysis stack is subjected to a reduction check, and a reduction check result is generated; When the reduction check result is matched, the top token is subjected to a reduction operation, and a local sub-tree is generated; All tokens in the token sequence are sequentially obtained, and the shift-reduce operation is performed on all tokens, and each local sub-tree of the abstract syntax tree is gradually constructed until the token sequence is processed, and the abstract syntax tree is obtained.
[0022] Specifically, the first element in the token sequence is read, which is the first token, the data structure (including token type, value, etc.) of the token is copied or referenced, and then the push operation of the stack is called to place the first token at the top of the pre-initialized analysis stack, and the top pointer or stack length information is updated to realize the process of moving the token from the sequence to the stack for storage, and the shift operation of the token is completed.
[0023] In detail, the sequence of a plurality of continuous tokens at the top of the analysis stack (the number of tokens is determined according to the maximum length of the right part symbols in the grammar rules) is read from the analysis stack, and then the sequence is compared with the right part of the context-free grammar rule one by one, the matching process is realized by traversing the rule list, and the sequence of tokens at the top of the stack is compared with the right part symbols of each rule one by one to determine whether the type and order are completely consistent. If a rule is found that matches, the reduction check result is marked as a match, and the corresponding grammar rule is recorded for subsequent reduction operation; if all rules do not match, the reduction check result is not matched.
[0024] The context-free grammar rule is a formal syntax description method, which is composed of a set of productions, each production includes a single non-terminal as the left part and a symbol string composed of terminal and non-terminal as the right part, which represents that the non-terminal can be replaced by the symbol string of the right part, and the entire grammar recursively defines the legal symbol sequence in the language through these rules.
[0025] Further, when the reduction check result is a match, first, the number of stack top tokens required for replacement is determined according to the successfully matched grammar rule, then the tokens corresponding to the right part of the rule and their associated sub-node data are popped from the analysis stack, then a new data structure of a non-terminal node is created, the node type corresponds to the left symbol of the grammar rule, then the sub-nodes corresponding to the just popped tokens are linked as the sub-nodes of the new node in turn, forming a local sub-tree structure. Finally, the newly constructed non-terminal node is pushed to the top of the analysis stack, the stack state information is updated, thus completing a reduction operation and generating a local sub-tree.
[0026] Specifically, when the reduction check result is a mismatch, the program directly skips the reduction branch: neither pops any stack token nor allocates a new non-terminal node, only keeps the current analysis stack top pointer unchanged, and immediately returns the control flow to the shift-in logic to continue reading the next token and pushing it to the stack, waiting for the next reduction check.
[0027] In detail, by traversing each token in the token sequence, a shift-in operation is performed for the current token, that is, it is pushed into the pre-initialized analysis stack. After each shift-in operation is completed, a sequence of top tokens is read from the analysis stack, and a matching check is performed with the right part of the context-free grammar rule to determine whether there is a matching reduction rule. When there is a matching rule, the program pops the corresponding number of stack top tokens and their associated sub-tree nodes according to the rule, constructs a new non-terminal node, forms a local sub-tree with the popped nodes as its sub-nodes, and then pushes the non-terminal node back to the analysis stack. The shift-in and reduction process continues alternately until all tokens in the token sequence are processed in turn. Each round of reduction operation will combine the lower layer sub-tree into a higher layer node, and the local sub-tree will be merged upwards, and finally a complete abstract syntax tree is formed.
[0028] In the example of the present application, the initial abstract syntax tree is mapped to a unified format by a standardization algorithm to obtain an abstract syntax tree. Each node of the initial abstract syntax tree is traversed. For each node, the syntax structure of its specific language is converted into a unified format according to the predefined mapping rule. For example, nodes representing variable declarations in different languages are uniformly mapped to a "variable declaration" node, and key information such as variable name and type is extracted and stored in the node attributes of the unified format. Starting from the leaf node, recursively process layer by layer upwards until the root node. Finally, the entire initial abstract syntax tree is converted into an abstract syntax tree conforming to the unified format, where the type and structure of each node conform to the standardized semantic representation.
[0029] The predefined mapping rules are a set of explicit conversion instructions for mapping the abstract syntax tree node types and structures of different programming languages to a unified semantic representation. These rules define how to convert the syntax elements of a specific language (such as variable declaration, function definition, expression, etc.) into a standardized node form.
[0030] In the examples of the present application, the advantage of converting source code into an abstract syntax tree is that the abstract syntax tree can provide a structured representation of the source code, facilitating code analysis and conversion.
[0031] In the field of financial technology, abstract syntax tree technology can be used for core code analysis of insurance claim settlement systems, quickly identifying and optimizing claim settlement logic, ensuring the accuracy of claim calculation, and detecting potential vulnerabilities or abnormal behavior to improve system reliability and stability.
[0032] In the field of medical health, insurance-related medical expense reimbursement systems can analyze code through abstract syntax tree parsing technology, optimize the logic of reimbursement rules, ensure that expense calculation complies with insurance clauses, and improve system transparency and maintainability.
[0033] S2, extract the key structural features of the abstract syntax tree through deep traversal and node analysis.
[0034] In the examples of the present application, the key structural features refer to important features extracted from the abstract syntax tree that can reflect the core logic and organization of the program, including class hierarchy, function call relationship, variable scope, and control flow information.
[0035] In the examples of the present application, the key structural features of the abstract syntax tree are extracted through deep traversal and node analysis, including: extracting the class hierarchy and function call relationship of the abstract syntax tree through function parsing; extracting the variable scope of the abstract syntax tree through scope parsing; extracting the conditional branches and loop structures of the abstract syntax tree through control flow parsing; semantically integrating the class hierarchy, function call relationship, variable scope, conditional branches, and loop structures to obtain key structural features.
[0036] In the examples of the present application, the class hierarchy and function call relationship of the abstract syntax tree are extracted by function resolution. Each node of the abstract syntax tree is accessed by a depth-first traversal algorithm. After a class definition node is identified, the class name and parent class name specified by the inheritance keyword are recorded. For each class definition node, the class name is used as a key and the parent class name is used as a value, which are stored in a hash table, thereby constructing the class hierarchy. By depth-first traversal, when a function definition node is located, the function name and parameter list are recorded. When a function call node is encountered, the called function name and the context information (such as the code block or expression where the call is located) of the call point are extracted. For each function call, the location of the call point and the called function name are stored as an entry in another hash table or list, thereby establishing the function call relationship.
[0037] In the examples of the present application, the variable scope of the abstract syntax tree is extracted by scope resolution. The abstract syntax tree is traversed. When a variable declaration node is identified, the variable name, the code block (such as a function body, a loop body, or a global range) where the declaration is located, and the position information are recorded. A scope stack is maintained, and the current code block is pushed into the stack as the scope boundary of the variable. During the traversal process, each new code block (such as a function definition or a loop structure) is pushed into the scope stack, and is popped out when exiting. When a variable use node is encountered, the nearest variable declaration with the same name is found from the top of the scope stack downwards, the effective scope range of the variable is determined, and finally the variable name, the corresponding scope boundary, and the use position are stored in association, forming a variable scope table.
[0038] In the examples of the present application, the conditional branch and loop structure of the abstract syntax tree are extracted by control flow resolution. The abstract syntax tree is traversed, and conditional branch nodes (such as if, else, and switch) and loop structure nodes (such as for, while, and do-while) are identified. For a conditional branch node, the condition expression and the corresponding true / false branch code block position are extracted, and a mapping relationship between the branch node and the child code block is established. For a loop structure node, the loop initialization expression, the termination condition, and the loop body code block position are extracted, and the loop entry and exit conditions are recorded. During the traversal process, a control flow stack is maintained. Each time a new condition or loop node is entered, it is pushed into the stack, and is popped out when exiting. At the same time, a unique identifier is generated for each node. Finally, the identifier of the conditional / loop node, the condition expression, and the branch / loop body position information are stored in association, forming a control flow path table.
[0039] In the examples of the present application, the semantic integration of the class hierarchy, function call relationship, variable scope, conditional branching and loop structure is achieved by assigning unique identifiers to classes, functions, variables and control flow nodes, and establishing a mapping table between identifiers and nodes. Then, the function call relationship table is traversed, and the context class name in it is mapped to the identifier of the corresponding class node through the class hierarchy, establishing a class-function call association; then, taking the identifier of the variable declaration node as the index, the conditional branching or loop body where the variable appears is found in the control flow path table, and the inclusion relationship between the variable scope and the control flow boundary is recorded.
[0040] Then, the parameter list of the function call is verified for type matching of the actual parameters through the variable scope table, establishing a parameter passing link; then the control flow conditional expression is disassembled into a variable reference sequence, which is matched to the identifier of the corresponding variable declaration node through the scope stack, establishing the reference relationship between the expression and the variable; finally, these class inheritance, function call, variable scope and control flow path relationships associated by identifiers are stored as a graph structure composed of nodes (classes, functions, variables, etc.) and edges (inheritance, call, reference, control, etc.), forming a multi-dimensional associated key structure feature.
[0041] In the examples of the present application, the key structure features of the abstract syntax tree are extracted through deep traversal and node analysis, which can accurately reveal the internal logic and organizational architecture of the code, providing a solid foundation for code quality assessment and code documentation generation.
[0042] In the field of financial technology, this technology can be used to analyze transaction system code, extract class hierarchy and function call relationship, quickly locate potential performance bottlenecks and security vulnerabilities, such as detecting whether there are unauthorized fund transfer paths or abnormal transaction logic branches.
[0043] In the field of medical health, by analyzing the code of medical device software, extracting variable scope and control flow structure, ensuring the stability and accuracy of the device under various conditional branching and loop operations.
[0044] S3, extracting and context analyzing the key structure features to obtain code feature information.
[0045] In the examples of the present application, the code feature information refers to the data extracted from the source code about program structure, function and behavior. It covers key elements such as syntax structure, semantic meaning, variable scope, function call relationship, etc., helping to understand the code logic and architecture.
[0046] In the field of financial technology, code feature information is used to generate technical documents for automated trading systems, ensuring the transparency and compliance of trading algorithms.
[0047] In the medical health field, it helps to create code documents of electronic health record systems, and guarantees the accuracy of data processing.
[0048] In the examples of the present application, referring to Figure 4 As shown, the hierarchical extraction and context analysis of the key structural features obtain code feature information, including: S31, according to the pre-defined granularity level rule, extracting the code segment corresponding to the abstract syntax tree; S32, according to the key structural features, establishing the context relationship of the code segment; S33, performing semantic mapping on the code segment and the context relationship to obtain code feature information.
[0049] In the examples of the present application, according to the pre-defined granularity level rule, the code segment corresponding to the abstract syntax tree is extracted, that is, the extraction boundary condition is set according to the pre-defined granularity level rule (such as project level, module level, class level, function level), the abstract syntax tree node is traversed by depth-first, the node type (such as class declaration, function definition, code block) conforming to the granularity rule is identified, the source code interval corresponding to the node is extracted, and the node context information (such as parent node type, variable scope) is recorded, and finally the code segment is obtained.
[0050] In the examples of the present application, according to the key structural features, the context relationship of the code segment is established, that is, the abstract syntax tree nodes corresponding to the extracted code segment are matched one by one with the nodes in the key structural features through the node identifiers in the key structural features, then the parent node identifiers, variable scope information and control flow boundaries of these nodes are searched, the code segment and its direct parent node, adjacent sibling nodes, and related nodes in the variable scope chain and control flow path are associated in turn, the caller and callee identifiers of the function nodes in the function call relationship involved in the segment are combined, the multiple relationship mappings of the code segment in the inheritance structure, call relationship, scope level and control flow path are formed, and finally these associated information is saved in the form of structured data, forming the context relationship between the code segments.
[0051] In the examples of the present application, the semantic mapping of the code snippet and the context relationship to obtain code feature information is based on a predefined term mapping table to standardize the conversion of function names, class names, and variable names in the code snippet (such as mapping "handleLogin" to "user login processing"), and to generate structured descriptions of structural information such as calling relationship and scope level in the context relationship through template matching (such as "the function calls the authentication service, checks the username and password, and generates a token"), and to supplement conditional branch logic by combining control flow information (such as "if the verification fails, return error code 401"), and finally to convert the code elements and their associated relationships into a set of natural language descriptions that meet the specifications of technical documents.
[0052] In the examples of the present application, hierarchical extraction and context analysis of key structural features are performed to obtain code feature information, which can improve the readability and maintainability of the code and help developers quickly understand the code logic and architecture.
[0053] In the field of medical health, it can guarantee the code quality of electronic health record systems, for example, by analyzing the context of patient data processing functions, it can ensure that data processing meets medical specifications and privacy protection requirements.
[0054] S4, filling the code feature information into a preset technical document template to generate a code document, scoring the code document to obtain a score result.
[0055] In the examples of the present application, the preset technical document template is a set of standardized document structure and format specifications for organizing and displaying technical information. It includes project overview, architecture design, functional modules, interfaces, and deployment and operation, etc.
[0056] In the examples of the present application, the filling of the code feature information into the preset technical document template to generate the code document includes: calculating the semantic similarity between the code feature information and the preset technical document template; structurally mapping the code feature information into the preset technical document template according to the semantic similarity to obtain an initial code document; reorganizing the hierarchical structure of the initial code document to obtain a code document.
[0057] In the examples of the present application, the calculation of the semantic similarity between the code feature information and the preset technical document template is first lexical analysis of the code feature information and the preset technical document template to split their respective vocabulary sets, then conversion of the vocabulary to numerical vectors through text vectorization technology, and then calculation of the distance (such as cosine distance) between the vectors, the smaller the distance, the higher the semantic similarity, so as to quantify the semantic matching degree of the two, and complete the similarity calculation.
[0058] In the examples of the present application, the structuring mapping of the code feature information into the preset technical document template according to the semantic similarity to obtain the initial code document is to determine the matching relationship of the code feature information and each chapter of the preset technical document template according to the semantic similarity, then extract the content (such as function description, parameter list, etc.) of the corresponding matching chapter in the code feature information according to the hierarchical structure of the template (such as project overview layer, architecture design layer, etc.), fill these contents into the specified position of the template (such as put the function code segment into the function description area corresponding to the interface layer), while retaining the structured association of the code feature information (such as call relationship, dependency information according to the template format), thereby generating the initial code document containing the code feature information and conforming to the template structure.
[0059] In the examples of the present application, the hierarchical structure reorganization of the initial code document to obtain the code document is to parse the existing hierarchical structure of the initial code document, extract each part of the title, content and their nesting relationship; then according to the hierarchical specification of the preset technical document template (such as the order of project overview→architecture design→function module), identify the unreasonable part of the hierarchical structure in the initial document (such as the function module content is incorrectly nested in the architecture design). Then reorganize the content of the code document by adjusting the title level and moving the content block, so that each part (such as module division, data flow description) corresponds to the level specified by the template (such as module division and data flow in the architecture design layer, interface function in the function module layer), and finally obtain the code document with a hierarchical structure conforming to the specification.
[0060] In the examples of the present application, the scoring of the code document to obtain the scoring result includes: completeness evaluation of the code document to obtain a completeness score; accuracy evaluation of the code document to obtain an accuracy score; understandability evaluation of the code document to obtain an understandability score; consistency evaluation of the code document to obtain a consistency score; weighted fusion of the completeness score, the accuracy score, the understandability score and the consistency score to obtain the scoring result.
[0061] In the examples of the present application, the completeness evaluation of the code document to obtain the completeness score is to extract all the lists of key code elements (such as code feature information points that need to be covered in each level such as project overview, architecture design, etc.) from the preset technical document template as the evaluation benchmark; then traverse the generated code document to check whether each key code element exists one by one; count the number of existing key code elements and calculate the proportion of the number in the total number of key code elements, which is the completeness score.
[0062] In the examples of the present application, the accuracy evaluation on the code document is performed to obtain an accuracy score by extracting content (such as function call relationship and variable scope description) describing code structure and logic from the code document, comparing the content with corresponding information in an abstract syntax tree and a code relationship mapping diagram of the original code, counting the number of consistent information items, and calculating the proportion of the number of consistent information items in the total number of comparison items as the accuracy score.
[0063] In the examples of the present application, the understandability evaluation on the code document is performed to obtain an understandability score by extracting language expression specifications (such as sentence length upper limit and professional term explanation requirement) and structural clarity standards (such as title level continuity rule and content block correlation index) from a preset technical document template, traversing the code document to extract all sentences, title levels and reference relationships between content blocks, counting the number of sentences exceeding the upper limit, the number of occurrences of unexplained professional terms, the number of title level jumps and the proportion of content blocks without reference correlation, comparing the statistical results with the specification standards to calculate deviation values, converting the deviation values into deduction values according to preset weights, and obtaining the understandability score by subtracting the deduction values from the full score of the understandability evaluation.
[0064] In the examples of the present application, the consistency evaluation on the code document is performed to obtain a consistency score by extracting code snippets, function descriptions, parameter descriptions, call relationships and other content from the code document, comparing the content with original code feature information (including code snippets and context relationships), counting the number of matching items of function descriptions and code logic, the number of matching items of parameter descriptions and function definitions, and the number of matching items of call relationships and code relationship mapping diagrams, and calculating the proportion of the total number of matching items in the total number of comparison items as the consistency score.
[0065] In the examples of the present application, the integrity score, the accuracy score, the understandability score and the consistency score are fused by weighting to obtain a score result, which is obtained by weighting the integrity score, the accuracy score, the understandability score and the consistency score according to preset weight values (such as integrity 25%, accuracy 20%, understandability 25% and consistency 30%) to obtain a total sum.
[0066] In the examples of the present application, the code feature information is filled into the preset technical document template to generate a code document and score, which can ensure that the document conforms to the specification and the quality is controllable, intuitively reflects the performance of the document in the integrity, accuracy and other dimensions through the score, facilitates targeted optimization, and improves the practicality and reliability of the document.
[0067] In the field of financial technology, the risk control system document can cover all compliance requirement modules and accurately describe the verification logic, and the score result can help quickly locate document omissions to meet regulatory review.
[0068] S5. When the score result is less than a preset threshold, the code document is optimized by incremental adjustment to generate an optimized code document.
[0069] In this invention, the preset threshold is a pre-defined scoring or performance standard used to determine whether the output result meets the expected quality or performance requirements. When the scoring result of the code document or other content generated by the system is lower than this threshold, the system will trigger a corresponding optimization or improvement process to ensure that the final output meets the predetermined quality standard.
[0070] Specifically, the incremental adjustment is a gradual optimization process that improves document quality through continuous small updates and adjustments. Each improvement is based on the results of the previous one, targeting specific problems or deficiencies for targeted optimization until the expected standard is reached.
[0071] In this invention example, when the scoring result is less than a preset threshold, the code document is optimized through incremental adjustment to generate an optimized code document, including: Extract the local text with low scores from the rating results; The local text is supplemented with information to generate an updated code document; The updated code document is scored to obtain an update score result; When the update score is less than a preset threshold, the updated code document is optimized through incremental adjustment until the update score reaches the preset threshold, and an optimized code document is generated.
[0072] Specifically, based on the scoring results, specific issues (such as completeness, accuracy, etc.) are identified. Then, the contribution of each segment to the overall score is calculated (e.g., quantified by gradient values), and segments below a threshold are filtered out. Next, based on contextual relationships (e.g., the association between variable references and functions), related low-contribution segments are clustered. Finally, the data is mapped back to the code documentation, the issue text is extracted, and the reasons for deductions are labeled (e.g., "inconsistent types"), resulting in local text.
[0073] Furthermore, the problem text annotating the reasons for the deduction (such as "missing parameter type") is parsed, and the missing information (such as obtaining the type from the parameter table) is retrieved based on the contextual relationships in key structural features (such as function call chains and variable scope). Then, supplementary text is generated according to a predefined template, and the supplementary text is accurately inserted into the corresponding position in the original document through a text similarity matching algorithm to generate a structurally coherent updated code document.
[0074] The predefined template is a set of standardized text format rules for guiding the supplement of missing information of the code document.
[0075] In detail, the integrity score, the accuracy score, the understandability score and the consistency score of the updated code document are obtained, and the integrity score, the accuracy score, the understandability score and the consistency score are weighted and fused to obtain an update score result.
[0076] Further, if the update score result is still less than the preset threshold, the local text with a low score in the update score result is extracted, the local text is supplemented with information, the updated code document is generated, and then the updated code document is scored to obtain an update score result, until the update score result reaches the preset threshold, and an optimized code document is generated.
[0077] If the score result is not less than the preset threshold, it indicates that the document quality meets the standard, and there is no need for further optimization, and the current document is directly output as the optimized code document.
[0078] In the examples of the present application, the incremental adjustment of the optimized code document ensures that the document quality always meets the preset standard, improves the accuracy and reliability of the document, and helps to reduce misunderstandings and errors.
[0079] In the field of financial technology, for the payment system document, the division of the fund flow module of the architecture design layer can be incrementally supplemented, and the transaction verification logic description of the function module layer can be repaired to meet the compliance and development and maintenance requirements.
[0080] In the field of medical health, for the electronic medical record system document, the data encryption description of the function module layer can be incrementally supplemented, and the permission configuration guide of the deployment and operation layer can be supplemented to help medical data security management and system stable operation and maintenance.
[0081] As can be seen, in the above solution, for the target business result, the use of abstract syntax tree parsing technology to convert the source code into an abstract syntax tree can accurately capture the syntactic structure and logical relationships of the code, laying the foundation for subsequent analysis; extracting key structural features through depth traversal and node analysis can focus on core information and avoid redundancy; hierarchical extraction and contextual analysis of key structural features to obtain code feature information can comprehensively and systematically present the essence of the code; filling the feature information into a preset template to generate code documentation can ensure standardized document format and consistent content, improving generation efficiency; scoring the documentation and making incremental adjustments and optimizations when it does not meet the standards can continuously improve document quality, meet the needs of different scenarios, and the overall process takes into account accuracy, efficiency and quality, significantly reducing the cost of manually writing and optimizing documentation, while making the code documentation easier to understand and maintain.
[0082] It should be understood that the sequence number of each step in the above embodiments does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0083] In one embodiment, a code documentation generation apparatus is provided, which corresponds one-to-one with the code documentation generation method described in the above embodiments. For example... Figure 5 As shown, the code documentation generation device 100 includes a conversion module 101, a traversal module 102, an analysis module 103, a scoring module 104, and an optimization module 105. Detailed descriptions of each functional module are as follows: Conversion module 101 is used to obtain source code and convert the source code into an abstract syntax tree; Traversal module 102 is used to extract key structural features of the abstract syntax tree through depth traversal and node analysis; Analysis module 103 is used to perform hierarchical extraction and contextual analysis on the key structural features to obtain code feature information; The scoring module 104 is used to fill the code feature information into a preset technical document template, generate a code document, score the code document, and obtain a scoring result. The optimization module 105 is used to optimize the code document by incremental adjustment when the scoring result is less than a preset threshold, and generate an optimized code document.
[0084] In one embodiment, the conversion module 101, upon acquiring source code, converts the source code into an abstract syntax tree, for the following purposes: Lexical analysis is used to decompose the source code into a sequence of tokens. The token sequence is used to construct an initial abstract syntax tree using syntax analysis; mapping the initial abstract syntax tree to a uniform format by a standardization algorithm to obtain an abstract syntax tree.
[0085] In an embodiment, the conversion module 101, in converting the source code into an abstract syntax tree after obtaining the source code, is further configured to: obtain a first token of the token sequence, perform a shift operation on the first token and push the first token into a pre-built analysis stack; perform a reduction check on a top token in the analysis stack to generate a reduction check result; when the reduction check result is matched, perform a reduction operation on the top token to generate a local sub-tree; obtain all tokens in the token sequence in sequence, perform a shift-reduce operation on the all tokens to gradually build each local sub-tree of an abstract syntax tree, until the token sequence is processed to obtain the abstract syntax tree.
[0086] In an embodiment, the traversal module 102, in extracting key structural features of the abstract syntax tree by depth-first traversal and node analysis, is configured to: extract a class hierarchy and function call relationship of the abstract syntax tree by function parsing; extract a variable scope of the abstract syntax tree by scope parsing; extract a conditional branch and loop structure of the abstract syntax tree by control flow parsing; semantically integrate the class hierarchy, the function call relationship, the variable scope, the conditional branch and the loop structure to obtain the key structural features.
[0087] In an embodiment, the analysis module 103, in performing hierarchical extraction and context analysis on the key structural features to obtain code feature information, is configured to: extract a code snippet corresponding to the abstract syntax tree according to a predefined granularity level rule; establish a context relationship of the code snippet according to the key structural features; semantically map the code snippet and the context relationship to obtain the code feature information.
[0088] In an embodiment, the scoring module 104, in filling the code feature information into a preset technical document template to generate a code document, scoring the code document to obtain a scoring result, is configured to: calculate a semantic similarity between the code feature information and the preset technical document template; structurally map the code feature information into the preset technical document template according to the semantic similarity to obtain an initial code document; The initial code document is hierarchically reorganized to obtain a code document.
[0089] In an embodiment, the scoring module 104 fills the code feature information into a preset technical document template, generates a code document, scores the code document, and obtains a score result, which is used for: completeness evaluation of the code document to obtain a completeness score; accuracy evaluation of the code document to obtain an accuracy score; understandability evaluation of the code document to obtain an understandability score; consistency evaluation of the code document to obtain a consistency score; The completeness score, the accuracy score, the understandability score, and the consistency score are weighted and fused to obtain the score result.
[0090] In an embodiment, the optimization module 105, when the score result is less than a preset threshold, optimizes the code document through incremental adjustment to generate an optimized code document, which is used for: extracting a local text with a low score in the score result; supplementing information of the local text to generate an updated code document; scoring the updated code document to obtain an updated score result; When the updated score result is less than a preset threshold, the updated code document is optimized through incremental adjustment until the updated score result reaches the preset threshold to generate an optimized code document.
[0091] The present application provides a code document generation device, which can accurately capture the essence of the code by using abstract syntax tree parsing technology to convert source code, extract key features, and perform hierarchical analysis, and avoid redundancy. The filling template can generate a document with a standard format and improved efficiency. The scoring and incremental optimization can continuously improve the quality of the document. The overall process balances accuracy and efficiency, significantly reduces labor costs, and makes the code document easier to understand and maintain.
[0092] The specific limitations of the code document generation device can be referred to the limitations of the code document generation method described above, and will not be repeated here. Each module in the code document generation device described above can be realized by software, hardware, and their combinations. The above modules can be embedded in or independent of the processor in the computer device in hardware form, or stored in the memory in the computer device in software form, so as to be called and executed by the processor to perform the operations corresponding to the above modules.
[0093] In one embodiment, a computer device is provided, which can be a server, and an internal structure diagram thereof can be as shown in Figure 6 The computer device includes a processor, a memory, a network interface and a database connected through a system bus. The processor of the computer device is configured to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for running the operating system and the computer program in the non-volatile storage medium. The network interface of the computer device is configured to communicate with an external client through a network connection. The computer program, when executed by the processor, implements the functions or steps of the server side of the code document generation method.
[0094] In one embodiment, a computer device is provided, which can be a client, and an internal structure diagram thereof can be as shown in Figure 7 The computer device includes a processor, a memory, a network interface, a display screen and an input device connected through a system bus. The processor of the computer device is configured to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for running the operating system and the computer program in the non-volatile storage medium. The network interface of the computer device is configured to communicate with an external server through a network connection. The computer program, when executed by the processor, implements the functions or steps of the client side of the code document generation method In one embodiment, a computer device is provided, which includes a memory, a processor and a computer program stored in the memory and executable on the processor, and the processor implements the following steps when executing the computer program: Obtaining source code, and converting the source code into an abstract syntax tree; Extracting key structural features of the abstract syntax tree through deep traversal and node analysis; Performing hierarchical extraction and context analysis on the key structural features to obtain code feature information; Filling the code feature information into a preset technical document template to generate a code document, scoring the code document to obtain a score result; When the score result is less than a preset threshold, optimizing the code document by incremental adjustment to generate an optimized code document.
[0095] In one embodiment, a computer readable storage medium is provided, which stores a computer program executable by a processor, and the processor implements the following steps when executing the computer program: Obtaining source code, and converting the source code into an abstract syntax tree; extracting key structural features of the abstract syntax tree through depth-first traversal and node analysis; performing hierarchical extraction and context analysis on the key structural features to obtain code feature information; filling the code feature information into a preset technical document template to generate a code document, scoring the code document to obtain a score result; when the score result is less than a preset threshold, optimizing the code document through incremental adjustment to generate an optimized code document.
[0096] It should be noted that the functions or steps described above with respect to the computer-readable storage medium or the computer device can correspond to the related descriptions of the server side and the client side in the foregoing method embodiments. To avoid repetition, they will not be described one by one here.
[0097] Those skilled in the art can understand that all or part of the processes in the foregoing embodiment methods can be completed by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer readable storage medium. When the computer program is executed, it can include the processes of the foregoing embodiments. In the embodiments provided in the present application, any reference to a memory, storage, database or other medium can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. As an illustration but not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDR SDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), Rambus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM) and memory bus dynamic RAM (RDRAM).
[0098] Those skilled in the art can clearly understand that, for the convenience and brevity of description, only the division of the above functional units and modules is exemplified. In actual applications, the above functions can be completed by different functional units or modules according to needs, that is, the internal structure of the device is divided into different functional units or modules to complete all or part of the functions described above.
[0099] It should be noted that if non-company software tools or components appear in the embodiments of the present application, they are only used for example introduction and do not represent actual use.
[0100] The above-described embodiments are only used to illustrate the technical solutions of the present application, rather than limit them; although the foregoing embodiments of the present application have been described in detail, those skilled in the art should understand that they can still modify the technical solutions recorded in the foregoing embodiments, or make equivalent replacement for some technical features therein; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present application, and should be included in the protection scope of the present application.
Claims
1. A code document generation method characterized by, The method comprises the following steps: obtaining source code, and converting the source code into an abstract syntax tree; extracting key structural features of the abstract syntax tree through deep traversal and node analysis; performing hierarchical extraction and context analysis on the key structural features to obtain code feature information; filling the code feature information into a preset technical document template to generate a code document, and scoring the code document to obtain a score result; when the score result is less than a preset threshold, optimizing the code document through incremental adjustment to generate an optimized code document.
2. The code document generation method of claim 1, wherein, The method of obtaining source code and converting the source code into an abstract syntax tree comprises the following steps: decomposing the source code into a token sequence by using lexical analysis; constructing an initial abstract syntax tree from the token sequence by using syntax analysis; mapping the initial abstract syntax tree to a unified format by using a standardization algorithm to obtain an abstract syntax tree.
3. The code document generation method of claim 2, wherein, The method of constructing an initial abstract syntax tree from the token sequence by using syntax analysis comprises the following steps: obtaining a first token of the token sequence, performing a shift operation on the first token and pushing the first token into a pre-constructed analysis stack; performing a reduction check on a top token in the analysis stack to generate a reduction check result; when the reduction check result is matched, performing a reduction operation on the top token to generate a local sub-tree; obtaining all tokens in the token sequence in sequence, performing a shift-reduction operation on the all tokens to gradually construct each local sub-tree of the abstract syntax tree, until the token sequence is processed to obtain an abstract syntax tree.
4. The code document generation method of claim 1, wherein, The method of extracting key structural features of the abstract syntax tree through deep traversal and node analysis comprises the following steps: extracting a class hierarchy and function call relationship of the abstract syntax tree through function analysis; extracting a variable scope of the abstract syntax tree through scope analysis; extracting a conditional branch and a loop structure of the abstract syntax tree through control flow analysis; performing semantic integration on the class hierarchy, the function call relationship, the variable scope, the conditional branch and the loop structure to obtain key structural features.
5. The code document generation method of claim 1, wherein, The method of performing hierarchical extraction and context analysis on the key structural features to obtain code feature information comprises the following steps: extracting a code snippet corresponding to the abstract syntax tree according to a predefined granularity level rule; establishing a context relationship of the code snippet according to the key structural features; performing semantic mapping on the code snippet and the context relationship to obtain code feature information.
6. The code document generation method of claim 1, wherein, The method of filling the code feature information into a preset technical document template to generate a code document comprises the following steps: calculating a semantic similarity between the code feature information and the preset technical document template; structurally mapping the code feature information into the preset technical document template according to the semantic similarity to obtain an initial code document; performing hierarchical structure reorganization on the initial code document to obtain a code document.
7. The code document generation method of claim 1, wherein, The method of scoring the code document to obtain a score result comprises the following steps: performing integrity evaluation on the code document to obtain an integrity score; performing accuracy evaluation on the code document to obtain an accuracy score; performing understandability evaluation on the code document to obtain an understandability score; performing consistency evaluation on the code document to obtain a consistency score; performing weighted fusion on the integrity score, the accuracy score, the understandability score and the consistency score to obtain a score result.
8. A code document generating apparatus characterized by comprising: comprising: a conversion module configured to obtain source code and convert the source code into an abstract syntax tree; a traversal module configured to extract key structural features of the abstract syntax tree through deep traversal and node analysis; an analysis module configured to perform hierarchical extraction and context analysis on the key structural features to obtain code feature information; a scoring module configured to fill the code feature information into a preset technical document template, generate a code document, perform scoring on the code document, and obtain a score result; an optimization module configured to perform optimization on the code document through incremental adjustment when the score result is less than a preset threshold to generate an optimized code document.
9. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, The processor implements the code document generation method according to any one of claims 1-7 when executing the computer program.
10. A computer-readable storage medium storing a computer program, the computer program comprising instructions that, when executed by a computer, cause the computer to perform the method of any one of claims 1 to 9. The computer program implements the code document generation method according to any one of claims 1-7 when executed by the processor.
Citation Information
Cited By
Agentic AI-based document generation method and device, electronic equipment and storage medium
CN121902769A