Code abstract generation method fusing item-level code context
By integrating project-level code context and utilizing static analysis and large language models to generate code summaries, the problem of insufficient accuracy and readability in existing code summaries is solved, achieving high-quality code summary generation.
Patent Information
- Application Number
- CN202511179921.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-22
- Publication Date
- 2026-01-06
AI Technical Summary
Existing code summarization methods fail to fully utilize project-level context information, resulting in inaccurate and unreadable summaries. Furthermore, the application of large models fails to fully leverage natural language processing capabilities.
By constructing a code digest generation method based on project-level code context, the method utilizes static analysis to obtain the context of the code repository, combines it with a large language model to generate code digests, including abstract syntax tree analysis, class dependency graph construction and feature clustering, uses Transformer for weighted concatenation and context strategy recognition, and finally generates code digests in the large language model.
It improves the accuracy and readability of code summaries by constructing a standardized context dataset and a hierarchical summary aggregation, and by leveraging the language processing capabilities of a large model to generate high-quality code summaries with complete information.
Smart Images

Figure CN121277553A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of code digest generation, and specifically relates to a code digest generation method that integrates project-level code context. Background Technology
[0002] With the development of the internet, modern software development often involves millions of lines of code, encompassing numerous modules and components. Developers face greater challenges in understanding structured project code. Research shows that software developers spend 58% of their development time on program understanding, with direct understanding through code editors being the primary method. Code summaries, or natural language descriptions of code, are the most intuitive interpretations of the code. Developers can quickly understand the specific meaning of code segments and thus the entire code project through the textual content of the code summary; its importance in the code understanding process is self-evident. However, because high-quality code summaries require significant manpower and time, their scarcity is one of the problems in software engineering coding.
[0003] With the development of AI technology, deep learning has been widely applied in numerous research fields, including software code. Researchers have proposed a large number of different models to support the automated generation of code summaries. Based on the types of underlying methods, their sophistication, and the underlying principles of the models, they can be roughly divided into three categories.
[0004] The first method is to infer code summaries from source code using deep learning. These methods typically take code as input, introduce classic deep learning models such as RNNs, LSTMs, and Transformers to represent the code, and compare the generated code summary fragments with the original code summary to update the network weights.
[0005] The second type is code summarization generation methods assisted by code context. These methods still tend to use deep learning models, but the difference is that they add "code context" defined by their respective research methods to supplement the semantics lacking in the source code. Some methods define the context as class names, while others define the context as random fragments of all files in the project.
[0006] The third approach is to use large models to understand source code. These methods typically design a framework or prompt template to support tasks such as understanding code snippets and generating summaries. This type of method performs better when the code snippets are short and generates more readable summaries.
[0007] However, these research methods have several shortcomings. Specifically, for the first type of research method, the drawback is that they are almost all end-to-end approaches, failing to fully integrate the strengths of various models, and their performance is limited to the design of the basic network, making them difficult to apply universally. For the second type of research method, the definition of "context" is relatively superficial, scattered, and non-standardized, lacking the construction and application of project-level context, and also lacking effective code-context-summary data pairs for training. For the third type of research method, there is a dependence on large models, but it fails to fully utilize the natural language processing capabilities of large models, and it is prone to context loss due to excessively long code snippets; the selection of prompt words is also a major challenge.
[0008] Therefore, there is an urgent need for a large-scale enhanced code summarization method that integrates project-level context into the code summarization process to address the shortcomings of previous research methods. Summary of the Invention
[0009] To address the problems in the prior art, this invention provides a code digest generation method that integrates project-level code context.
[0010] The technical solution of the present invention is as follows:
[0011] This invention discloses a code digest generation method that integrates project-level code context, comprising:
[0012] 1) Obtain the Java codebase to be digested, obtain the context, context, and abstract syntax tree language fragments of each class code segment in the codebase; obtain the metadata of each type of code and the third-party libraries referenced, and use the documentation descriptions of the third-party libraries as the context of the corresponding class code to expand the context of the class code.
[0013] 2) Input the member variable codes of various code types into the trained member variable summary generation model to obtain member variable vectors; input the method codes of various code types into the trained method summary generation model to obtain method vectors; concatenate all member variable vectors in each class code segment and perform average pooling to obtain member variable embedding vectors; concatenate all method vectors in each class code segment and perform average pooling to obtain method embedding vectors; weight the member variable embedding vectors and method embedding vectors of the same class code segment based on the trained attention mechanism, concatenate the weighted vectors to obtain class vectors, and then input the class vectors into the trained Transformer-based decoding layer to obtain basic summaries of various code types;
[0014] 3) Extract feature data of various types of code, cluster the code based on the feature data, and divide the code into multiple categories; use the common features of all code in each category as the category features of that category, input the category features into the trained context selection strategy recognition model to obtain the context selection strategy of the code in each category; obtain the context required by the code from the context of the code based on the context selection strategy of each code segment; fill the basic summary, abstract syntax tree language fragment, required context and metadata of each code segment into the preset prompt template, and then input the prompt template after filling in the information into the large language model to obtain the code summary of all code in the code library.
[0015] Further, in step 1), obtaining the context, background, and abstract syntax tree language fragments of each class code segment in the code library includes:
[0016] An abstract syntax tree (AST) is constructed for each class code segment in the codebase using static analysis. Then, based on the complete AST, the call relationships between various types of code in the codebase are analyzed to construct a class dependency graph. Based on the class dependency graph, the context of each type of code is obtained. The preceding text of a class code is the code that references and declares libraries, classes, interfaces, and variables. The following text of a class code is the code snippet in other classes that references the class code when it is applied to them. Finally, the AST of each class code segment is serialized to obtain a language fragment that uniquely describes the structure of the AST, i.e., the AST language fragment.
[0017] Compared with the prior art, the beneficial effects of the present invention are as follows:
[0018] 1. It provides a standardized scheme for constructing context-inclusive code summarization datasets, offering a standardized solution for the preparation phase of context-dependent code summarization research.
[0019] 2. Based on the concept of hierarchical summary aggregation, the lower limit of code summary generation effect is guaranteed.
[0020] 3. Make full use of project-level contextual information and train based on context optimization strategies to obtain the most suitable contextual addition scheme for the current context.
[0021] 4. Leveraging the language processing capabilities of large models, code summarization is enhanced by using customized prompt word templates based on different contextual strategies, further improving the quality of the generated summaries. Attached Figure Description
[0022] Figure 1 This is a flowchart of the code digest generation method that integrates project-level code context according to the present invention;
[0023] Figure 2This is a flowchart of the first step in the code digest generation method of the present invention;
[0024] Figure 3 This is a flowchart of the second step in the code digest generation method of the present invention;
[0025] Figure 4 This is a flowchart of the third step in the code digest generation method of the present invention. Detailed Implementation
[0026] The present invention will be further described and illustrated below with reference to specific embodiments. The embodiments described are merely examples of the content of this disclosure and do not limit the scope of the invention. The technical features of each embodiment in the present invention can be combined accordingly, provided that there is no mutual conflict.
[0027] This invention designs and implements a large-scale enhanced code summarization method that integrates project-level context with code summarization generation, addressing the shortcomings of previous methods. Based on a fundamental model for end-to-end code summarization from source code, this invention effectively utilizes project-level code context and selects the most suitable context to supplement semantics based on an optimal context selection strategy. Finally, leveraging the language-related task processing capabilities of the large-scale model, the code summarization is enhanced, resulting in a more complete, readable, and accurate code summary.
[0028] The present invention will now be described with reference to the accompanying drawings.
[0029] like Figure 1 The code digest generation method of the present invention, which integrates project-level code context, includes the following steps:
[0030] Step 1: Dataset Construction
[0031] like Figure 2 As shown, constructing the dataset first requires selecting multiple Java code repositories for training models from open-source communities (such as GitHub) based on criteria such as activity and popularity, cloning them locally, parsing the source code in these repositories from multiple dimensions, and finally compiling them into a JSON dataset. Specifically:
[0032] In order to describe the characteristics of the code, metadata such as the number of lines of code, the fully qualified class name (the complete name used to uniquely identify a class), and the number of methods need to be parsed and aggregated into a JSON dataset.
[0033] To extract code context, we first define the "preceding text" of a class code as the source code of the libraries / classes / interfaces / variables that reference that class, and the "following text" as the code snippets in other classes that reference that class code when it is used. Then, we parse each code snippet using static analysis to obtain an Abstract Syntax Tree (AST) for each type of code. We then extract the code elements from each AST as the basis for constructing relationships between class codes, thereby building a dependency graph for each codebase. Figure 2 The class dependency graph is then used. Finally, based on the class dependency graph of each codebase, the context of each class code segment in each codebase is obtained, and the extracted class code context is added to the JSON dataset. Then, the abstract syntax tree of each class code segment is serialized to obtain language fragments that uniquely describe the structure of the abstract syntax tree. Similarly, the language fragments of each class code segment are also added to the JSON dataset.
[0034] To supplement the missing code context of third-party libraries, we collected the missing third-party libraries for each class code segment from each codebase and expanded the context of the corresponding class code using the official documentation description of the third-party library; at the same time, we added the context of the class code to the JSON dataset.
[0035] To obtain a complete code digest, regular expression matching needs to be applied to code snippets at different levels to extract member variable code, method code, and the class code's code digest, forming a multi-level code digest. Specifically, regular expression matching is used to obtain the code digest of each class code segment in each code repository, as well as the digests of member variable code and method code within each class code segment in each code repository. The class code digest, the member variable code digest, and the method code digest constitute the multi-level code digest of that class code. Finally, all the multi-level code digests of the class code are aggregated into a JSON dataset.
[0036] Of course, it is also necessary to import each class code segment from each code repository into the JSON dataset.
[0037] Based on the above methods, a JSON dataset consisting of JSON objects organized by codebase and corresponding one-to-one with class code was obtained.
[0038] Step 2: Basic Summary Generation
[0039] Basic summaries refer to summaries generated independently of context. This invention proposes a Hierarchical Summary Aggregation Model (HSAM) suitable for the Java language. It regards code snippets at different levels in the JSON dataset as the basic source of summary semantics, extracts their internal semantics independently through encoder-decoder training, and then unifies them into a class-level summary vector through an aggregation mechanism, thereby improving the model's ability to extract local semantics and organize overall semantics.
[0040] like Figure 3 As shown, the complete steps for generating the basic summary include:
[0041] Construct a member variable summary generation model and a method summary generation model; both the member variable summary generation model and the method summary generation model include an encoding layer and a decoding layer.
[0042] Then, the member variable codes of each class code segment in the JSON dataset are input into the member variable summary generation model to obtain the predicted summary of the member variable codes. Then, the summary of each member variable code in the JSON dataset is used as the label, and the member variable summary generation model is trained based on the cross-entropy loss function.
[0043] The method code of each class code in the JSON dataset is input into the method summary generation model to obtain the predicted summary of the method code. The summary of each method code in the JSON dataset is used as the label, and the method summary generation model is trained based on the cross-entropy loss function.
[0044] The member variable codes of each class in the JSON dataset are input into the encoding layer of a pre-trained member variable summarization generation model to obtain member variable vectors for each type of code. Similarly, the method codes of each class in the JSON dataset are input into the encoding layer of a pre-trained method summarization generation model to obtain method vectors for each type of code.
[0045] The member variable vectors from each class code segment are concatenated and average pooled to obtain a member variable embedding vector; similarly, the method vectors from each class code segment are concatenated and average pooled to obtain a method embedding vector; the member variable embedding vector and method embedding vector have the same dimension. Then, based on an attention mechanism, the member variable embedding vector and method embedding vector of the same class code segment are weighted and concatenated to obtain a preliminary class vector. This preliminary class vector is then input into a Transformer-based decoding layer to obtain a predicted summary of the class code. Using the summaries of various code types in the JSON dataset as labels, the Transformer-based decoding layer and attention mechanism are trained using the cross-entropy loss function.
[0046] Based on a trained attention mechanism, the member variable embedding vector and method embedding vector of the same class code are weighted, and then concatenated to obtain a class vector. The class vector is then input into a trained Transformer-based decoding layer to obtain a basic summary of various types of code in the JSON dataset.
[0047] In a specific embodiment of the present invention, both the member variable summary generation model and the method summary generation model are GPT models or DeepCom models.
[0048] Step 3: Context-Driven Code Digestion Enhancement
[0049] Context-driven code summarization enhancement includes training a context selection strategy recognition model and basic code summarization enhancement based on a large model.
[0050] like Figure 4 As shown, during the model training phase, feature data for each class code segment in the JSON dataset is first extracted. This feature data includes code characteristics such as class structural complexity, class granularity, context density, and dependency depth. Then, cluster analysis is performed on the class code based on its feature data, dividing the class code into multiple categories. Next, case analysis is conducted on the class code within each category to obtain the category features for each category. This involves analyzing the common features of all class code within each category and using these as the category features for that category.
[0051] The categorical features of different categories are used as input to the context selection strategy recognition model, which is a multilayer perceptron neural network. The model outputs the predicted context selection strategy. Then, the same context introduction method is set for all class codes in each category and used as a label. The context introduction method can be to introduce only the preceding context, only the following context, the three most relevant preceding contexts, or all preceding contexts, etc. Finally, the context selection strategy recognition model is trained based on the cross-entropy loss function.
[0052] During training, the performance improvement of different strategies on the input of the large model to the basic summary is evaluated. Finally, a strategy recognition model is obtained that can select the most appropriate context enhancement method according to different types of code. This strategy recognition mechanism is used to solve the problem of different structural classes having different degrees of dependence on context information in summary generation, and to ensure that the context introduction method is optimal.
[0053] Finally, in the basic summary enhancement stage, the basic code summary generated in the second step can be selected as input. Based on the trained context selection strategy recognition model, the context selection strategy of the class code is obtained. Based on the context selection strategy of each class code segment, the context required by the class code is obtained from the context of the class code. A predefined prompt word template is selected, and the basic summary, the required context, and metadata are filled in. Finally, the prompt word template with the filled information is input into the large language model (e.g., ChatGPT; all experiments in this invention are conducted under ChatGPT 3.5-Turbo), which can obtain the code summary of all class codes in each code base used to train the model.
[0054] Leveraging the language generation and semantic integration capabilities of large models, the basic summary is enhanced for contextual consistency and refined for language, resulting in a more readable, accurate, and context-consistent best summary.
[0055] Step 4: Generate a code digest of the codebase to be used for code digest generation.
[0056] First, obtain the Java codebase for code digest generation. Obtain the context, context, and language fragments used to uniquely describe the abstract syntax tree structure for each class code segment in the codebase. Obtain the metadata of each type of code and the third-party libraries referenced. Use the documentation of the third-party libraries as the context of the corresponding class code to expand the context of the class code.
[0057] Then, the member variable codes of various types of code are input into a trained member variable summary generation model to obtain member variable vectors; the method codes of various types of code are input into a trained method summary generation model to obtain method vectors; all member variable vectors in each class code segment are concatenated and average pooled to obtain member variable embedding vectors; all method vectors in each class code segment are concatenated and average pooled to obtain method embedding vectors; based on a trained attention mechanism, the member variable embedding vectors and method embedding vectors of the same class code segment are weighted, and the weighted vectors are concatenated to obtain class vectors. The class vectors are then input into a trained Transformer-based decoding layer to obtain basic summaries of various types of code.
[0058] Next, feature data of various types of code is extracted, and the code classes are clustered based on the feature data to divide the code classes into multiple categories. The common features of all code classes in each category are used as the category features of that category. The category features are input into the trained context selection strategy recognition model to obtain the context selection strategy of the code classes in each category. Based on the context selection strategy of each code class, the context required by the code class is obtained from the context of the code class. The basic summary, language fragment, required context, and metadata of each code class are filled into a preset prompt word template. Then, the prompt word template with the filled information is input into the large language model to obtain the code summary of all code classes in the code library.
[0059] Results Evaluation: The enhanced summary was evaluated using multi-dimensional metrics to assess its performance gain compared to the basic summary, thus verifying the positive effect of context introduction on code summarization quality. Specifically, three context-dependent code summarization models—CocoSum (Code ContextSummary), Context-PCS (Context-Precise Contextual Summary), and HRCE (HybridRetrieval-Context Enhancement)—were selected as comparison models, while the unenhanced HSAM model was selected as the ablation experiment object. The improvements achieved by this invention compared to other models are shown in the table below.
[0060]
[0061]
[0062] Due to the introduction of a large model, the model of this invention has a significant advantage in readability. Thanks to accurate context definition and the role of the policy generator, the model of this invention shows significant advantages and considerable improvements in BLEU (Bilingual Evaluation Understudy) and METEOR (Metric for Evaluation of Translation with Explicit ORdering) scores.
[0063] The above-described embodiments are merely illustrative of several implementations of the present invention, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of the invention. Those skilled in the art can make various modifications and improvements without departing from the concept of the present invention, and these modifications and improvements all fall within the scope of protection of the present invention.
Claims
1. A method of code summarization that fuses project-level code context, characterized by, The method comprises the following steps: 1) obtaining a code library to be summarized in Java language, obtaining the context, the context and the abstract syntax tree language segment of each class code in the code library, obtaining the metadata of each class code and the third-party library referenced, and taking the document description of the third-party library as the context of the corresponding class code, and expanding the context of the class code; 2) inputting the member variable code of each class code into the trained member variable summary generation model to obtain a member variable vector; inputting the method code of each class code into the trained method summary generation model to obtain a method vector; splicing and average pooling all member variable vectors in each class code to obtain a member variable embedding vector; splicing and average pooling all method vectors in each class code to obtain a method embedding vector; weighting the member variable embedding vector and the method embedding vector of the same class code based on the trained attention mechanism, splicing after weighting to obtain a class vector, and then inputting the class vector into the trained decoding layer based on the Transformer to obtain a basic summary of each class code; 3) extracting feature data of each class code, clustering the class codes based on the feature data, and dividing the class codes into multiple categories; taking the common features of all class codes in each category as the category features of the category, and inputting the category features into the trained context selection strategy recognition model to obtain the context selection strategy of the class codes in each category; obtaining the required context of each class code from the context of the class code based on the context selection strategy of each class code; filling the basic summary, the abstract syntax tree language segment, the required context and the metadata of each class code into a preset prompt template, and then inputting the prompt template after filling the information into a large language model to obtain the code summary of all class codes in the code library.
2. The code digest generation method according to claim 1, characterized by, In step 1), the context, the context and the abstract syntax tree language segment of each class code in the code library are obtained, which comprises: constructing an abstract syntax tree of each class code in the code library based on a static analysis method, constructing a class dependency graph of the code library based on the calling relationship between the class codes in the code library, obtaining the context of each class code based on the class dependency graph of the code library, the context of the class code is the code of the library, class, interface and variable declared by the class code, and the context of the class code is the code segment of the other class code which references the class code when the class code is applied to the other class code; finally, the abstract syntax tree of each class code is serialized to obtain a language segment for uniquely describing the structure information of the abstract syntax tree, i.e. the abstract syntax tree language segment.
3. The code digest generation method according to claim 1, characterized by, The metadata in step 1) includes the total number of lines of the class code, the class fully qualified name and the number of methods.
4. The code digest generation method according to claim 1, characterized by, In step 2), the method for obtaining the trained member variable summary generation model and the trained method summary generation model comprises: obtain the context and language fragment for uniquely describing the structure information of the abstract syntax tree of each class code based on the abstract syntax tree; obtain the metadata of each class code and the third-party library referenced by the class code, and take the document description of the third-party library as the context of the corresponding class code; obtain the code summary of each class code, and obtain the summary of the member variable code and the summary of the method code in each class code to construct a multi-level code summary of the corresponding class code; input the member variable code of each class code in the data set into a member variable summary generation model to obtain the predicted summary of the member variable code, take the summary of each member variable code in the data set as the label, and train the member variable summary generation model based on the cross-entropy loss function; 5. The code digest generation method according to claim 4, characterized by, input the method code of each class code in the data set into a method summary generation model to obtain the predicted summary of the method code, take the summary of each method code in the data set as the label, and train the method summary generation model based on the cross-entropy loss function.
6. The code digest generation method according to claim 4, characterized by, The member variable summary generation model and the method summary generation model both include an encoding layer and a decoding layer.
7. The code digest generation method according to claim 4, wherein The code summary of each class code in each code library is obtained by a regular expression matching method, and the summary of the member variable code and the summary of the method code in each class code in each code library are obtained. The member variable summary generation model and the method summary generation model are both GPT models or DeepCom models. The method for obtaining the trained attention mechanism and the trained decoding layer based on the Transformer includes: input the member variable code of each class code in the data set into the encoding layer of the trained member variable summary generation model to obtain a member variable vector; input the method code of each class code in the data set into the encoding layer of the trained method summary generation model to obtain a method vector; 8. The code digest generation method according to claim 7, characterized by, splice and average pool all the member variable vectors in each class code to obtain a member variable embedding vector, splice and average pool all the method vectors in each class code to obtain a method embedding vector, weight the member variable embedding vector and the method embedding vector of the same class code based on the attention mechanism, splice the weighted vectors to obtain a preliminary class vector, and input the preliminary class vector into a decoding layer based on the Transformer to obtain the predicted summary of the class code, take the summary of each class code in the data set as the label, and train the decoding layer based on the Transformer and the attention mechanism based on the cross-entropy loss function. The dimensions of the member variable embedding vector and the method embedding vector are the same. In step 3), the method for obtaining the trained context selection strategy recognition model includes: obtain the basic summary of each class code in the data set by using the trained attention mechanism and the decoding layer based on the Transformer; Feature data of each type of code in the data set is extracted, the class codes are clustered based on the feature data of the class codes, and the class codes are divided into multiple categories; common features of all class codes in each category are analyzed as category features of the category, and the category features are input into a context selection strategy recognition model to obtain a predicted context selection strategy; a same context introduction mode is set for all class codes in each category and used as a label, and the context selection strategy recognition model is trained based on a cross-entropy loss function.
9. The code digest generation method according to claim 8, characterized by, The feature data includes structural complexity, class granularity, context density, and dependency depth of the class codes.
10. The code digest generation method according to claim 8, characterized by, The context selection strategy recognition model is a multilayer perception neural network.