Source code comment generation system and source code comment generation method
The system accurately generates source code comments by associating design document terms with source code scope units, addressing the inaccuracy of existing methods and enhancing comment relevance.
Patent Information
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Filing Date
- 2025-06-17
- Publication Date
- 2026-03-19
AI Technical Summary
Existing source code comment generation techniques using large language models often fail to accurately reflect the scope and intention of the source code, leading to inaccurate comments.
A system that utilizes a computer with a processor and memory to associate terms from a design document with their meanings, divides source code into predetermined scope units, and generates comments based on these associations using a large language model, considering the scope and structure of the source code.
Enables the generation of comments with high accuracy by aligning the comments with the scope and structure of the source code, improving the precision and relevance of the generated comments.
Smart Images

Figure JP2025021812_19032026_PF_FP_ABST
Abstract
Description
Source Code Comment Generation System, Source Code Comment Generation Method
[0001] The present invention relates to a comment generation technique for promoting the understanding of source code.
[0002] Conventionally, a technique has been proposed in which reference information is added to an input question sentence to generate a prompt, and the generated prompt is input into a large language model to generate an answer sentence. For example, in Patent Document 1, it is described in paragraph 0011 that "a prompt for input into a large language model is generated by adding a design document with a character count that does not exceed the character count limit that can be input to the input question sentence."
[0003] Japanese Patent No. 7325152
[0004] However, as described in Patent Document 1, simply adding a design document with a character count that does not exceed the character count limit that can be input to the input question sentence may not be sufficient. For example, the added design document may not necessarily be appropriate for the scope for which comment output is desired in the source code constituting the question sentence. In such a case, the source code comment generated by the large language model may be inaccurate or may not accurately reflect the intention of the question sentence. Thus, in the prior art, a technique capable of generating comments with high accuracy according to the scope of the source code has been demanded.
[0005] An object of the present invention is to provide a source code comment generation system and a source code comment generation method capable of generating comments with high accuracy according to the scope of the source code.
[0006] The source code comment generation system according to the present invention is a source code comment generation system that generates comments to source code using a computer having a processor and memory, wherein the source code comment generation system has term information that associates terms used in a design document input to the source code comment generation system with the meanings of those terms, and the processor divides the source code into source code of predetermined scope units, and generates commented source code including comments of predetermined scope units for the source code of predetermined scope units based on the meaning of the terms having a predetermined relationship with the meaning of the source code of predetermined scope units, the source code of predetermined scope units, and a predetermined language model.
[0007] According to the present invention, it is possible to generate comments with high accuracy according to the scope of the source code. Problems, configurations, and effects other than those described above will be clarified by the following description of embodiments.
[0008] This figure shows an example of the configuration of a source code comment generator. This figure shows an example of a computer overview. This is an example of a source code DB that stores source code, commented source code, update date and time, and updater ID. This is an example of a function source code DB that stores function source code, commented function source code, and the source code ID associated with the function source code. This is an example of a block source code DB that stores block source code, commented block source code, and the function source code ID associated with the block source code. This is an example of a row source code DB that stores row source code, commented row source code, and the block source code ID associated with the row source code. This shows an example of a design document DB that stores the document name, document content, and document vector of the design document. This shows an example of a design document term DB that stores the term names, meanings of the terms, and semantic vectors of the terms in the design document. This shows an example of an input unit that performs input of source code and design documents, source code analysis, and design document term dictionary construction. This figure shows an example of a display unit that displays a source code ID selection button and source code and commented source code. This figure shows an example of the processing flow of the design document preprocessing unit. This figure shows an example of the processing flow of the source code analysis unit. This figure shows an example of the processing flow of the design document term generation unit. This figure shows an example of the processing flow of the source code comment generation unit. This figure shows an example of the function comment generation process flow. This figure shows an example of the block comment generation process flow. This figure shows an example of the line comment generation process flow. This figure shows an example of the comment regeneration process flow. This figure shows an example of the results display screen for displaying the search results and cosine similarity calculation results in S1504 shown in Figure 15. This figure is for explaining a specific example of source code. This figure shows an example of comments generated on a function basis. This figure shows an example of comments generated on a block basis. This figure shows an example of comments generated on a line basis. This figure shows an example of comments regenerated on a line basis. This figure shows an example of comments regenerated on a block basis. This figure shows an example of comments regenerated on a function basis.
[0009] Embodiments of the present invention will be described below with reference to the drawings. The embodiments are illustrative examples for explaining the present invention, and have been omitted and simplified as appropriate for clarity of explanation. The present invention can also be carried out in various other forms. Unless otherwise specified, each component may be singular or plural. The position, size, shape, and range of each component shown in the drawings may not represent the actual position, size, shape, and range in order to facilitate understanding of the invention. Therefore, the present invention is not necessarily limited to the position, size, shape, and range disclosed in the drawings.
[0010] Examples of various types of information may be described using terms such as "table," "list," and "queue," but these types of information may also be represented by other data structures. For example, various types of information such as "XX table," "XX list," and "XX queue" may be referred to as "XX information." When describing identification information, terms such as "identification information," "identifier," "name," "ID," and "number" are used, but these terms are interchangeable.
[0011] When there are multiple components with the same or similar function, they may be described using the same symbol but with different subscripts. Furthermore, when it is not necessary to distinguish between these multiple components, the subscripts may be omitted in the description.
[0012] In the embodiments, the processes performed by executing a program may be described. Here, the computer executes the program using a processor (e.g., CPU, GPU) and performs the processing defined in the program using memory resources (e.g., memory) and interface devices (e.g., communication ports). Therefore, the main entity performing the processing by executing the program may be the processor. Similarly, the main entity performing the processing by executing the program may be a controller, device, system, computer, or node having a processor. The main entity performing the processing by executing the program may be an arithmetic unit, and may include a dedicated circuit that performs a specific processing. Here, a dedicated circuit is, for example, an FPGA (Field Programmable Gate Array), an ASIC (Application Specific Integrated Circuit), or a CPLD (Complex Programmable Logic Device).
[0013] The program may be installed on the computer from the program source. The program source may be, for example, a program distribution server or a storage medium readable by the computer. If the program source is a program distribution server, the program distribution server includes a processor and storage resources for storing the program to be distributed, and the processor of the program distribution server may distribute the program to other computers. In addition, in the embodiment, two or more programs may be implemented as one program, or one program may be implemented as two or more programs.
[0014] Figure 1A shows an example of the configuration of the source code comment generator 101 according to this embodiment. The source code comment generator 101 is a device that, in order to facilitate the developer's understanding of source code, accurately searches for design documents corresponding to the source code and outputs semantic source code comments using a large-scale language model device 105, while considering the scope units of the source code and the source code descriptions between those scopes. Scope refers to the range within which descriptions for executing source code, such as functions, code blocks, lines of code, methods, and files, are valid, depending on the hierarchy of the source code. In the following, as an example, the scope is explained using functions, code blocks, and lines of code as units of descriptions according to the hierarchy of the source code, but the same consideration may be applied to other descriptions as described above.
[0015] The source code comment generator 101 first inputs the design document 102 into the input unit 106, executes the design document preprocessing unit 111, and stores the output result in the design document DB 109e of the data holding unit 109. Next, the design document terminology DB generation unit 112 executes processing using the large-scale language model device 105 and stores the output result in the design document terminology DB 109f. The source code comment generator 101 also inputs the source code 103 into the input unit 106, executes the source code analysis unit 107, and stores the output result in the source code DB 109a. The source code comment generator 101 takes the source code from the source code DB 109a and the design document terminology DB 109f as input, executes the source code comment generation unit 108, and updates the source code DB 109a.
[0016] The source code comment generation unit 108 includes a function processing unit 108a, a block processing unit 108b, and a line processing unit 108c, and generates appropriate comments for each scope based on the scope structure of the source code. For example, the function processing unit 108a generates comments for the entire function, the block processing unit 108b generates comments for code blocks consisting of multiple lines, and the line processing unit 108c generates comments for individual lines.
[0017] The large-scale language model device 105 is a component that hosts machine learning models used to perform natural language processing tasks with high accuracy. For example, there is GPT-4 provided by OpenAI. GPT-4 is a transformer-based large-scale language model with billions of parameters, and it has the performance to pre-train on vast amounts of text data and handle a variety of natural language processing tasks.
[0018] The information acquired via the input unit 106 includes source code 103 and design document 102. The source code 103 may be source code in a different programming language, such as a Java® file or a Python® file. The design document 102 may be a document that includes diagrams and tables.
[0019] The display unit 110 displays DB information stored in the data storage unit 109, which is a typical storage device in terms of hardware. The display unit 110 also outputs commented source code 104. The display unit 110 may use a typical display device such as a display.
[0020] The source code comment generation device 101 shown in Figure 1A can be realized by a general computer 1600, which includes, for example, a CPU 1601, a memory 1602, an external storage device 1603 such as an HDD (Hard Disk Drive), a reader 1607 for reading and writing information to a portable storage medium 1608 such as a CD (Compact Disk) or USB memory, an input device 1606 for receiving various types of information such as a keyboard and mouse, an output device 1605 such as a display for outputting various types of information that have been input and used for processing, a communication device 1604 such as a NIC (Network Interface Card) for connecting to a communication network, and an internal communication line (referred to as a system bus) 1609 connecting these.
[0021] Furthermore, various data stored in the control device 3 or used for processing (for example, a database such as the source code DB 109a) can be realized by the CPU 1601 reading and using it from memory 1602 or external storage device 1603. In addition, each part of the control device 5 (for example, the source code analysis unit 107, the source code comment generation unit 108, the design document preprocessing unit 111, and the design document terminology DB generation unit 112) can be realized by the CPU 1601 loading a predetermined program stored in the external storage device 1603 into memory 1602 and executing it.
[0022] The aforementioned predetermined programs and data may be stored (downloaded) from the storage medium 1608 via the reading device 1607, or from the network via the communication device 1604, into the external storage device 1603, and then loaded onto the memory 1602 and executed by the CPU 1601. Alternatively, they may be loaded directly onto the memory 1602 via the reading device 1607 from the storage medium 1608, or from the network via the communication device 1604, and then executed by the CPU 1601.
[0023] In the following example, the control device 5 is described as being composed of a single computer, but all or part of these functions may be distributed across one or more computers, such as a cloud, and similar functions may be realized by them communicating with each other via a network.
[0024] The source code DB 109a will be explained using Figure 2. The source code DB 109a includes source code ID 201, source code file name 202, source code 203, commented source code 204, source code update date and time 205, source code updater ID 206, commented source code update date and time 207, and commented source code updater ID 208. The source code DB 109a is a database that stores source code output by the source code analysis unit 107 and commented source code to which source code comments have been added by the source code comment generation unit 108.
[0025] Source code ID 201 is an ID used to identify the source code. Source code file name 202 is the file name of the input source code. Source code 203 is the source code text. Commented source code 204 is the source code to which source code comments have been added by the source code comment generation unit 108 to source code 203. Source code update date and time 205 is the date and time when source code 203 was updated. Source code updater ID 206 is an ID that identifies the person who updated source code 203. Commented source code update date and time 207 is the date and time when commented source code 204 was updated. Commented source code updater ID 208 is an ID used to identify the person or AI that updated commented source code 204.
[0026] The function source code DB109b will be explained using Figure 3. The function source code DB109b includes function source code ID 301, function source code 302, function source code with comments 303, and source code ID 304. The function source code DB109b is a database that stores the scope in the source code, which is the range in which a function is valid, as a single scope unit.
[0027] Function source code ID 301 is an ID used by the function processing unit 108a to divide and identify the source code 203 in units of scope represented by a function. Function source code 302 is the text data of the function source code. Commented function source code 303 is the source code to which the source code comment generation unit 108 has added source code comments to the function source code 302. Source code ID 304 is source code ID 201 used to identify the source code 203, which includes the function source code 302.
[0028] In Figure 3, for example, the range 290 in which the function of the source code identified by source code ID "001" shown in Figure 2 is valid is stored as a single scope unit in the function source code 302 identified by function source code ID "001".
[0029] The block source code DB109c will be explained using Figure 4. The block source code DB109c includes block source code ID 401, block source code 402, commented block source code 403, and function source code ID 404. The block source code DB109c is a database that stores the range in which a code block, which is the scope in function source code, is valid, as a single scope unit.
[0030] Block source code ID 401 is an ID used to identify block source code, which is obtained by the block processing unit 108b by dividing the function source code 302 into scope units represented by code blocks. Block source code 402 is the text data of the block source code. Commented block source code 403 is the source code to which the source code comment generation unit 108 has added source code comments to the block source code 402. Function source code ID 404 is a function source code ID 301 used to identify the function source code 302, which includes the block source code 402.
[0031] In Figure 4, for example, a certain code block within the function source code identified by the function source code ID "003" shown in Figure 3, with a valid range 390, is stored as a single scope unit in the block source code 402 identified by the block source code ID "001".
[0032] The row source code DB109d is explained using Figure 5. The row source code DB109d includes row source code ID 501, row source code 502, commented row source code 503, and block source code ID 504. The row source code DB109d is a database that stores the range in which a code line, which is the scope in block source code, is valid, as a single scope unit.
[0033] Line source code ID 501 is an ID for identifying line source code obtained by the line processing unit 108c by dividing the block source code 402 into scope units represented by code lines. Line source code 502 is the text data of the line source code. Line source code with comments 503 is source code to which the source code comment generation unit 108 has added source code comments to the line source code 502. Block source code ID 504 is a block source code ID 401 for identifying the block source code 402 which includes the line source code 502.
[0034] In Figure 5, for example, a range 490 in which a certain line of code is valid, among the block source code identified by block source code ID "001" shown in Figure 4, is stored as a single scope unit in line source code 502 identified by line source code ID "001".
[0035] The design document DB 109e will be explained using Figure 6. The design document DB 109e includes document ID 601, document internal ID 602, document name 603, document content 604, document vector 605, update date and time 606, and document publication scope 607. The design document DB 109e is a database that stores the results executed by the design document preprocessing unit 111 on the input design document 102.
[0036] Document ID 601 is a unique ID used to identify the design document. Document ID 602 is an ID used for identification within the document and indicates the internal structure of the document. Document name 603 is the name of the design document and corresponds to the file name of the design document. Document content 604 is text data that shows the specific contents of the design document and contains information related to the source code corresponding to document ID 602. Document vector 605 is the document content 604 converted into a numerical vector and is used to search the contents of the document. Update date and time 606 indicates the date and time when the document ID 601 was last updated. Document access range 607 is an attribute that indicates the scope to which the design document is made public and the scope of users who can access it. For example, if the access range is Public, all users can access it, and if it is Project01, only members who have access rights to Project01 can access it. Design documents identified by document ID 601 are generally large in volume, and processing them once will result in a heavy load. Therefore, in Figure 6, the design document is divided and stored as one or more documents within a document, each identified by an internal document ID 602.
[0037] The design document terminology database 109f is explained using Figure 7. The design document terminology database 109f includes term ID 701, term name 702, term meaning 703, term meaning vector 704, document ID 705, update date and time 706, and updater ID 707. The design document terminology database 109f is a database that stores the meanings of terms obtained as a result of processing using the large-scale language model device 105.
[0038] Term ID 701 is a unique ID used to identify a term within a design document. Term name 702 is the name of the term used within the design document. Term meaning 703 is text data that shows the specific meaning of the term and contains a description of the term. Term meaning vector 704 is a numerical vector derived from term meaning 703 and is used to search for the meaning of a term within a design document. Document ID 705 is an ID that identifies the design document containing the term. Update date and time 706 indicates the date and time when the term ID 701 was last updated. Updater ID 707 is an ID used to identify the person or AI that updated the term.
[0039] The input unit 106 will be explained using Figure 8. The input unit 106 is an interface consisting of a screen that includes, for example, the following four buttons. The input unit 106 includes a source code input button 801, a design document input button 802, a source code analysis execution button 803, and a design document terminology dictionary construction execution button 804. The user selects the source code 103 using the source code input button 801 and the design document 102 using the design document input button 802. Then, the user starts the processing of the design document preprocessing unit 111 using the design document terminology dictionary construction execution button 804. Finally, the user starts the processing of the source code analysis unit 107 using the source code analysis execution button 803 and generates the commented source code 104.
[0040] The display unit 110 will be explained using Figure 9. The display unit 110 is an interface consisting of a screen for visually displaying information from the source code DB 109a and the commented source code 104. The display unit 110 includes a source code ID selection button 901, a source code display area 902, and a commented source code display area 903. When the source code ID selection button 901 is pressed, the source code 203 and the commented source code 204 corresponding to the source code ID 201 are displayed on the display unit 110. The display unit 110 also includes an external output button 904, and when this button is pressed, the commented source code 104 can be exported.
[0041] Using FIG. 10, the processing of the design document preprocessing unit 111 will be described. The design document preprocessing unit 111 is a component for performing preprocessing of the design document 102. An example of its processing flow is shown below.
[0042] S1001: The design document preprocessing unit 111 reads the file format of the design document 102 and extracts the text. For example, the design document preprocessing unit 111 obtains text data from files in PDF or Word format.
[0043] S1002: The design document preprocessing unit 111 removes parts other than text data such as blank lines and symbols from the text. For example, the design document preprocessing unit 111 deletes blank lines and filters special symbols such as paragraph symbols, line break codes, tab characters, and "\".
[0044] S1003: The design document preprocessing unit 111 divides the text into a certain number of characters. For example, the text is divided every 500 characters.
[0045] S1004: The design document preprocessing unit 111 vectorizes the text. For example, using natural language processing technology, each divided text data is converted into a numerical vector. S1005: The design document preprocessing unit 111 updates the document ID 602, document name 603, document content 604, document vector 605, and update date and time 606 in the design document DB 109e. For example, the design document preprocessing unit 111 generates a unique identifier as the document ID 602, sets the document name 603 to the input file name, sets the document content 604 to the extracted and processed text data, and sets the document vector 605 to the converted numerical vector. Furthermore, the design document preprocessing unit 111 sets the update date and time 606 to the current date and time and stores it in the design document DB 109e.
[0046] Using FIG. 11, the processing of the source code analysis unit 107 will be described. The source code analysis unit 107 is a component for analyzing the source code 103 and updating the source code DB 109a. An example of its processing flow is shown below.
[0047] S1101: The source code analysis unit 107 updates the source code file name 202, the source code 203, the source code update date and time 205, and the source code updater ID 206 in the source code DB 109a. For example, the source code analysis unit 107 sets the source code file name 202 for the newly input source code 103 and stores the source code 203. The source code analysis unit 107 sets the current date and time for the source code update date and time 205 and sets the ID identifying the person who updated it for the source code updater ID 206.
[0048] The process of the design document term DB generation unit 112 will be described using FIG. 12. This flowchart shows the process in which the design document term DB generation unit 112 acquires a design document from the design document DB 109e, extracts terms from the document using the large language model device 105, and vectorizes their meanings. In this process, it exemplifies the case where, for the design document with a certain document ID stored in the design document DB 109e, processing is performed for each in-document ID that was split in S1003 of FIG. 10 and stored in S1005, but the same processing is performed for other design documents as well.
[0049] S1201: This is the process in which the design document term DB generation unit 112 acquires a document from the design document DB 109e. In this process, the design document term DB generation unit 112 refers to the in-document ID 602 in the design document DB 109e and acquires the document content 604.
[0050] S1202: This is the process in which the design document term DB generation unit 112 extracts terms from the document. In this process, the design document term DB generation unit 112 extracts the terms in the design document based on the acquired document content 604. For example, the design document term DB generation unit 112 extracts the terms in the design document by executing a prompt in which the large language model device 105 describes an instruction for term extraction. The following processes of S1203 to S1205 are performed for each term extracted in S1202.
[0051] S1203: This is the process in which the design document terminology DB generation unit 112 outputs the meaning of the extracted terms from the document content 604 using the large-scale language model device 105. Alternatively, the input may be a combination of the document content 604 of document ID 601 instead of document ID 602, or all document content 604 present in the design document DB 109e.
[0052] S1204: The design document terminology database generation unit 112 performs a process of vectorizing the meaning of terms. The design document terminology database generation unit 112 converts the meaning of the extracted terms into numerical vectors, for example, using natural language processing technology.
[0053] S1205: This is the process by which the design document terminology DB generation unit 112 stores the term name 702, the term meaning 703, the term meaning vector 704, and the document ID 705 in the design document terminology DB 109f. The design document terminology DB generation unit 112 sets the term name extracted as the term name 702, the meaning generated by the large-scale language model device 105 and obtained from the large-scale language model device 105 as the term meaning 703, the vectorized meaning as the term meaning vector 704, and the document ID 602 as the document ID 705, and stores them in the design document terminology DB 109f.
[0054] The processing of the source code comment generation unit 108 will be explained using Figure 13. This flowchart shows the process of generating comments from source code for each specific scope unit (function, block, line), adding appropriate comments by referring to the design document corresponding to each scope unit, and regenerating for each specific scope unit (function, block, line) to improve accuracy. As will be explained in detail below, the source code comment generation unit 108 performs processing with functions as the scope, which are higher levels, then processes with blocks as the scope, which are middle levels, and then processes with lines as the scope, which are lower levels. Therefore, even if there is no matching description pattern in the translation dictionary, as in the conventional technology, the design document corresponding to the source code can be searched with high accuracy by changing the scope of the hierarchy in the source code being searched. Then, processing with blocks as the scope, which are middle levels, is performed by incorporating the processing results of the lower levels, and processing with functions as the scope, which are higher levels, is performed by incorporating the processing results of the middle levels.
[0055] In this way, the source code comment generation unit 108 performs processing corresponding to the scope of each level in the source code, such as the upper, middle, and lower levels, step by step toward the lower levels, and then recursively processes toward the original middle and upper levels. In other words, by recursively executing LLM on a source code scope basis and outputting source code comments, it becomes possible to add source code comments while considering the influence of the hierarchical units and structure between source code.
[0056] In the flowchart, the extracted scope unit is shown as a "function" as an example, but as mentioned above, it could also be a "class," "method," or "source code file."
[0057] S1301: This is the process by which the source code comment generation unit 108 extracts functions from the source code. In this process, the start and end positions of each function are identified from the source code 103, and each function is extracted as an independent scope unit. For example, in a Java program, the source code comment generation unit 108 detects functions from method declaration keywords such as "void" and "int", divides the start and end of each function into one block, and extracts the divided function block. At this time, the function processing unit 108a of the source code comment generation unit 108 registers the extracted function in the function source code 302 along with the function source code ID 301 and source code ID 304.
[0058] S1302: This is the process by which the source code comment generation unit 108 generates comments for each function, which is a single scope. This process refers to the design document 102 for each function and generates appropriate comments based on the content of the design document corresponding to the function source code 302. For example, if the extracted function performs database connection processing, the source code comment generation unit 108 refers to the explanation regarding database connection in the design document and generates comments for that function.
[0059] S1303: This is the process by which the source code comment generation unit 108 generates comments in units of code blocks, which constitute a single scope. The source code comment generation unit 108 extracts code blocks consisting of multiple lines within a function and generates comments for those blocks. In this process, the source code comment generation unit 108 refers to the relevant section of the design document and assigns appropriate comments to the multi-line processing that constitutes the code block. For example, if an error handling block exists, the source code comment generation unit 108 generates comments for that block by referring to the section of the design document related to error handling.
[0060] S1304: This is the process by which the source code comment generation unit 108 generates comments in units of code lines, which constitute a single scope. For each code line within a code block, the source code comment generation unit 108 refers to the design document and generates an appropriate comment for each line. For example, for a line that performs a specific calculation, the source code comment generation unit 108 refers to the algorithm description section of the design document and adds a comment explaining the purpose and meaning of that calculation.
[0061] S1305: This is the process by which the source code comment generation unit 108 regenerates the generated comments. In this process, the source code comment generation unit 108 re-examines the comments for a specific scope unit (function, block, line) that were initially generated and makes final adjustments to further improve accuracy and applicability. For example, the source code comment generation unit 108 re-examines the content of the comments for functions and blocks, verifies whether there are any inconsistencies or errors in the comments for functions, and corrects and regenerates the comments as necessary.
[0062] Figure 14 illustrates the process performed by the function processing unit 108a. This flowchart shows the process by which the function processing unit 108a receives function source code as input, outputs the meaning of that process, searches for the design document with the closest content in the design document DB 109e, generates appropriate comments, and adds them to the function source code.
[0063] S1401: This process involves the function processing unit 108a receiving the function source code 302 as input, querying the large-scale language model device 105 for the meaning of the process, and obtaining the result from the large-scale language model device 105. In this process, the large-scale language model device 105 analyzes the content of the function source code input from the function processing unit 108a and outputs the intended meaning of the process in natural language. For example, if the design document for calculating cosine similarity is in Japanese, the large-scale language model device 105 will output the meaning of the process of the function source code output by the function processing unit 108a and queried, in Japanese.
[0064] S1402: The function processing unit 108a performs the process of vectorizing the meaning of the function source code. The function processing unit 108a converts the meaning of the process output in S1401 into a numerical vector.
[0065] S1403: The function processing unit 108a searches the design document terminology database 109f for the closest meaning of the term 703 to the meaning of the function source code, and calculates the cosine similarity between the term and the meaning of the term.
[0066] S1404: The function processing unit 108a determines whether the cosine similarity calculated in S1403 is equal to or greater than a predetermined threshold.
[0067] S1405: If the cosine similarity is greater than or equal to a threshold (S1404; YES), the function processing unit 108a considers that the processing of the function source code and the meaning of the terms in the design document are sufficiently consistent, and inputs the function source code 302 and the meaning of the terms 703 to the large-scale language model device 105. The large-scale language model device 105 uses this input information to execute a natural language processing task, and the function processing unit 108a obtains commented function source code 303 in which the meaning of the terms obtained from the execution is embedded as a comment in the function source code.
[0068] S1406: This is the process in which the function processing unit 108a updates the function source code DB 109b with the commented function source code 303. The function processing unit 108a adds the generated comments to the function source code and updates the function source code DB 109b.
[0069] Figure 15 illustrates the process performed by the block processing unit 108b. This flowchart shows the process by which the block processing unit 108b extracts block source code from function source code, outputs the meaning of that process, searches for the design document with the closest content in the design document DB 109e, generates appropriate comments, and adds them to the block source code.
[0070] S1501: This is the process by which the block processing unit 108b extracts block source code 402 from function source code 302. In this process, the block processing unit 108b identifies the start and end positions of each code block from the function source code 302 and extracts each code block as an independent scope unit. For example, in a Java program, if there is one or more blank lines, the block processing unit 108b divides the code lines from the blank line before the blank line up to the blank line in question into one block and extracts the divided block. At this time, the block processing unit 108b of the source code comment generation unit 108 registers the extracted block in block source code 402 along with block source code ID 401 and function source code ID 404.
[0071] S1502: The block processing unit 108b takes block source code 402 as input, queries the large-scale language model device 105 for the meaning of the processing, and obtains the result from the large-scale language model device 105. In this process, the large-scale language model device 105 analyzes the contents of block source code 402 input from the block processing unit 108b and outputs the intended meaning of the processing in natural language.
[0072] S1503: The block processing unit 108b performs the process of vectorizing the meaning of the block source code 402. The block processing unit 108b converts the meaning of the process output in S1502 into a numerical vector.
[0073] S1504: The block processing unit 108b searches the design document terminology database 109f for the design document that most closely matches the meaning of the block source code, and calculates the cosine similarity between the meaning of the term and the design document. In this process, the block processing unit 108b compares the vectorized meaning with the contents of the design document terminology database 109f and identifies the design document with the highest similarity.
[0074] S1505: The block processing unit 108b determines whether the cosine similarity calculated in S1504 is equal to or greater than a predetermined threshold.
[0075] S1506: If the cosine similarity is greater than or equal to a threshold (S1505; YES), the block processing unit 108b inputs the block source code 402 and the term meanings 703 to the large-scale language model device 105. The large-scale language model device 105 uses this input information to perform a natural language processing task, and the block processing unit 108b obtains a commented block source code 403 in which the term meanings obtained from the execution are embedded as comments in the block source code.
[0076] S1507: This is the process in which the block processing unit 108b updates the block source code DB 109c with the commented block source code 403. The block processing unit 108b adds the generated comments to the block source code and updates the block source code DB 109c.
[0077] Figure 16 illustrates the process performed by the line processing unit 108c. This flowchart shows the process by which the line processing unit 108c extracts line source code from block source code, outputs the meaning of that process, searches for the design document with the closest content in the design document DB 109e, generates appropriate comments, and adds them to the line source code.
[0078] S1601: This is the process by which the line processing unit 108c extracts line source code 502 from block source code 402. In this process, the line processing unit 108c identifies the start and end positions of each line of code from block source code 402 and extracts each line as an independent scope unit. For example, in a Java program, the line processing unit 108c detects each line of code based on the semicolon ";" and newline character, divides it into individual lines of code, and extracts the block of the divided lines of code. At this time, the line processing unit 108c of the source code comment generation unit 108 registers the extracted lines of code in line source code 502 along with line source code ID 501 and block source code ID 504.
[0079] S1602: The line processing unit 108c takes line source code 502 as input, queries the large-scale language model device 105 for the meaning of the processing, and obtains the result from the large-scale language model device 105. In this process, the large-scale language model device 105 analyzes the content of line source code 502 input from the line processing unit 108c and outputs the intended meaning of the processing in natural language. For example, for lines that perform a specific calculation, the line processing unit 108c outputs the purpose and meaning of that calculation in natural language.
[0080] S1603: The line processing unit 108c performs the process of vectorizing the meaning of the processing in line source code 502. The line processing unit 108c converts the meaning of the processing output in S1602 into a numerical vector.
[0081] S1604: The line processing unit 108c searches the design document terminology database 109f for the design document that most closely matches the meaning of the line source code 502, and calculates the cosine similarity between the term and its meaning. In this process, the vectorized meaning is compared with the contents of the design document terminology database 109f to identify the design document with the highest similarity.
[0082] S1605: The row processing unit 108c determines whether the cosine similarity calculated in S1604 is equal to or greater than a predetermined threshold.
[0083] S1606: If the cosine similarity is greater than or equal to a threshold (S1605; YES), the line processing unit 108c inputs the line source code 502 and the term meanings 703 to the large-scale language model device 105. The large-scale language model device 105 uses this input information to perform a natural language processing task, and the line processing unit 108c obtains commented line source code 503 in which the term meanings obtained from the execution are embedded as comments in the line source code.
[0084] S1607: This is the process in which the row processing unit 108c updates the row source code DB 109d with the commented row source code 503. The row processing unit 108c adds the generated comments to the row source code and updates the row source code DB 109d.
[0085] Figure 17 illustrates the process performed by the comment regeneration processing unit 108d. This flowchart shows the process by which the comment regeneration processing unit 108d re-examines the commented source code generated in specific scope units (functions, blocks, lines) and makes final adjustments to further improve accuracy and applicability.
[0086] S1701: This is the process by which the comment regeneration processing unit 108d obtains the commented line source code 503 and the block source code ID 504 from the line source code DB 109d. In this process, the comment regeneration processing unit 108d obtains the commented line source code 503 for each code line and the block source code ID 504 corresponding to the commented line source code 503 from the line source code DB 109d.
[0087] S1702: This is the process by which the comment regeneration processing unit 108d obtains block source code 402 and function source code ID 404 from block source code DB 109c. In this process, the comment regeneration processing unit 108d obtains block source code 402 and function source code ID 404 corresponding to block source code ID 504 from block source code DB 109c based on the block source code ID 504 obtained in S1701.
[0088] S1703: This is the process by which the comment regeneration processing unit 108d obtains the function source code 302 from the function source code DB 109b. In this process, the comment regeneration processing unit 108d obtains the function source code 302 corresponding to the function source code ID 404 obtained in S1702 from the function source code DB 109b.
[0089] S1704: The comment regeneration processing unit 108d takes the commented function source code 303 and the commented block source code 403 as input and regenerates the comments of the commented line source code 503 using the large-scale language model device 105.
[0090] S1705: This is the process in which the comment regeneration processing unit 108d updates the row source code DB 109d with the commented row source code 503. The comment regeneration processing unit 108d adds the comments regenerated in S1704 to the row source code and updates the row source code DB 109d.
[0091] S1706: This is the process in which the comment regeneration processing unit 108d takes the commented line source code 503 and the commented function source code 303 as input and regenerates the comments of the commented block source code 403 using the large-scale language model device 105.
[0092] S1707: This is the process in which the comment regeneration processing unit 108d updates the block source code DB 109c with the commented block source code 403. The comment regeneration processing unit 108d adds the comments regenerated in S1706 to the block source code and updates the block source code DB 109c.
[0093] S1708: This is the process in which the comment regeneration processing unit 108d takes the commented line source code 503 and the commented block source code 403 as input and regenerates the comments of the commented function source code 303 using the large-scale language model device 105.
[0094] S1709: This is the process in which the comment regeneration processing unit 108d updates the function source code DB 109b with the commented function source code 303. The comment regeneration processing unit 108d adds the comments regenerated in S1708 to the function source code and updates the function source code DB 109b.
[0095] In the above explanation, in steps S1403 in Figure 14, S1504 in Figure 15, and S1604 in Figure 16, in order to expedite the processing, the obtained commented function source code, commented block source code, and commented line source code are not displayed on the screen before proceeding to the next step. However, as shown in Figure 18, for example, the block processing unit 108b may output the search results and the cosine similarity calculation results from step S1504 shown in Figure 15 to the display unit 110.
[0096] Figure 18 shows an example of a results display screen for displaying the search results and cosine similarity calculation results in S1504 shown in Figure 15.
[0097] As shown in Figure 18, the results display screen 1800 displays the following items in association with each other: the search result 1801, which shows the meaning of a term retrieved from the design document terminology DB 109f using the meaning of the block source code processing as a query; the term name 1802, which corresponds to the term shown in the search result; the document name 1803 of the design document containing the term name 1802; the update date and time of the document 1804; and the score 1805, which is the result of calculating the cosine similarity (degree of cosine similarity). For example, the results display screen 1800 shows that the score 1805 has a value of "0.2343", and the record 1806 with the highest cosine similarity is shown, which is the search result "The next ... NF9" for the term name "getParameter..." searched from the design document term DB 109f, "wiki...", which is the document name of the design document containing the term (document name 603 corresponding to document ID 705), and "Sun Oct 15 16:03:30 2023", which is the update date and time of the design document (update date and time 706).
[0098] The leftmost column is the search result ID used to identify each search result. In this example, the top 10 terms with the highest cosine similarity scores are displayed as search results. Figure 18 illustrates the display of the processing results of S1504 shown in Figure 15, but similar screens may be output for S1403 in Figure 14 and S1604 in Figure 16. By displaying such a results screen, the user can easily check the information that forms the basis of the commented source code 104 that is ultimately displayed on the display unit (for example, information such as the score).
[0099] The process of the source code comment generation unit 108 will be explained in more detail below, based on Figures 13 and 17. A specific example of the source code to be targeted is shown in Figure 19A.
[0100] First, in S1302, the source code comment generation unit 108 (function processing unit 108a) generates comments by referring to the source code and the design document terminology DB 109f for each function, which is a single scope. Figure 19B shows an example of comments initially generated for each function. In Figure 19B, it can be seen that the function-specific comments 1901 have been generated by the process in S1302.
[0101] Next, the source code comment generation unit 108 (block processing unit 108b) generates comments for the blocks within the function in S1303. Figure 19C shows that the comment 1902 for the blocks within the function is generated by the process in S1303.
[0102] Next, the source code comment generation unit 108 (line processing unit 108c) generates comments for each line in S1304. Figure 19D shows that comments 1903a, 1903b, and 1903c are generated for each line by the processing in S1304.
[0103] Next, the source code comment generation unit 108 (comment regeneration processing unit 108d) executes S1305. This will be explained in detail below.
[0104] 1. Regeneration process of commented line source code 503 (Figure 17 S1704) When the process in S1704 is performed, the regeneration result is obtained as line-by-line regeneration results, as shown in Figure 19E. In S1704, the date and text of the comments for the lines generated in S1304 are written in a more appropriate Japanese style by the large-scale language model device 105. That is, the comment regeneration processing unit 108d inputs the commented function source code generated in S1302, the commented block source code generated in S1303, and the terms used in these comments to the large-scale language model device 105, similar to the process in S1606. Then, the comment regeneration processing unit 108d updates the commented line source code 503 updated in S1607 to include new comments (comments 1904a, 1904b, 1904c) for each line obtained from the results of the natural language processing task performed by the large-scale language model device 105.
[0105] 2. Regeneration process of commented block source code 403 (Figure 17, S1706) When the process in S1706 is performed, the regeneration result is obtained as a block-level regeneration result, as shown in Figure 19F. In S1706, the date and text of the comments for the block generated in S1303 are written in a more appropriate Japanese style by the large-scale language model device 105. That is, the comment regeneration processing unit 108d inputs the commented function source code generated in S1302, the commented line source code generated in S1304, and the terms used in these comments to the large-scale language model device 105, in the same way as the process in S1506. Then, the comment regeneration processing unit 108d updates the commented block source code 403, which was updated in S1507, to include a new comment (comment 1905) for the block obtained from the results of the natural language processing task performed by the large-scale language model device 105.
[0106] 3. Regeneration process of commented function source code 303 (Figure 17, S1708) When the process in S1708 is performed, the regeneration result is obtained as a function-level regeneration result, as shown in Figure 19G. In S1708, the date and text of the comments for the function generated in S1302 are written in a more appropriate Japanese style by the large-scale language model device 105. That is, the comment regeneration processing unit 108d inputs the commented block source code generated in S1303, the commented line source code generated in S1304, and the terms used in these comments to the large-scale language model device 105, in the same way as the process in S1405. Then, the comment regeneration processing unit 108d updates the commented function source code 303 updated in S1406 to include a new comment (comment 1906) for the function obtained from the results of the natural language processing task executed by the large-scale language model device 105.
[0107] As described above, the source code comment generation unit 108 (comment regeneration processing unit 108d) inputs source code and terminology from a predetermined scope unit (e.g., function unit, block unit) at a different hierarchical level from the commented source code generated at a predetermined scope unit (e.g., line unit) into the large-scale language model device 105. The comment regeneration processing unit 108d then updates the commented source code generated at the predetermined scope unit (e.g., line unit) with new comments (e.g., comments reflecting function unit comments, block unit comments) obtained by analysis by the large-scale language model device 105. Through this process, the generated scope unit comments can be reviewed again, and final adjustments can be made to improve accuracy and applicability. As a result, it becomes possible to add source code comments that take into account the influence of scope between the design document and the source code with even greater accuracy.
[0108] The embodiment has been described above with reference to the drawings. In this embodiment, as described with reference to Figures 1, 13, etc., a source code comment generation system (for example, a source code comment generation device 101) that generates comments to source code using a computer having a processor and memory, the source code comment generation system has terminology information (for example, a design document terminology DB 109f) that associates terms used in a design document input to the source code comment generation system with the meaning of those terms, and the processor generates the source code into predetermined scope units (for example, function units, block units, line units) (for example, function source code DB 109b, block source code DB 109c, The source code is divided into rows (DB109d) (for example, processing S1301 in Figure 13, S1501 in Figure 15, and S1601 in Figure 16), and based on the above-mentioned terms with meanings that have a predetermined relationship (for example, a relationship where the cosine similarity is greater than or equal to a predetermined threshold) with the meaning of the source code in the above-mentioned scope unit, the above-mentioned source code in the above-mentioned scope unit, and a predetermined language model (for example, processing S1404 in Figure 14, processing S1505 in Figure 15, and processing S1605 in Figure 16), commented source code including the predetermined scope unit comments is generated for the above-mentioned scope unit source code (for example, processing S1302, S1303, and S1304 in Figure 13).
[0109] This process enables the generation of highly accurate comments tailored to the scope of the source code. For example, it becomes possible to generate comments that consider the impact of references in design documents on a per-source code scope basis. Specifically, according to this embodiment, even if there is no matching description pattern in the translation dictionary, as in the conventional technology, it is possible to accurately search for the corresponding design document by changing the source code scope being searched.
[0110] Furthermore, as explained in S1203 to S1205 of Figure 12, the processor inputs the terms used in the design document into a predetermined language model (for example, a language model similar to the one described above), associates the meaning of the input terms obtained from the predetermined language model with the input terms, and generates term information. This makes it possible to generate a term dictionary that includes highly accurate term meanings obtained from the execution results of various natural language processing tasks.
[0111] Furthermore, as explained in S1003 to S1005 of Figure 10, the processor divides the input text contained in the design document into predetermined units (for example, units of 500 characters), performs natural language processing on each of the divided texts, and stores them as design documents for generating the term information. As a result, the target of natural language processing is subdivided, making it possible to generate a term dictionary while reducing the processing load.
[0112] Furthermore, as explained in Figure 3-5, the processor divides the source code into predetermined scope units, in units of functions, blocks, and lines. This makes it possible to generate comments according to the various levels of the source code, particularly functions, code blocks, and lines of code.
[0113] Furthermore, as explained in Figures 10, 12, 14-16, etc., the processor converts the meaning of the terms obtained through the natural language processing into numerical vectors. This makes it possible to quantitatively and accurately grasp the semantic similarity between terms.
[0114] Furthermore, as explained in Figures 1, 14-16, etc., the processor inputs the source code in the scope unit into the predetermined language model (for example, the process in S1401 in Figure 14, the process in S1502 in Figure 15, and the process in S1602 in Figure 16), obtains the meaning of the source code in the scope unit analyzed by the predetermined language model, and determines the predetermined relationship between the obtained meaning of the source code in the scope unit and the meaning of the terms (for example, the process in S1404 in Figure 14, the process in S1505 in Figure 15, and the process in S1605 in Figure 16). This makes it possible to determine the predetermined relationship (for example, by determining cosine similarity) based on the highly accurate meaning of the source code obtained from the execution result of the natural language processing task.
[0115] Furthermore, as explained in Figures 1, 14, 16, etc., the processor inputs the scope-unit source code and the terms whose predetermined relationships have been determined into the predetermined language model, obtains commented source code including the scope-unit comments analyzed by the predetermined language model (for example, the process in S1404 in Figure 14, the process in S1505 in Figure 15, and the process in S1605 in Figure 16), and performs the above generation by updating the scope-unit source code with the obtained commented source code (S1406, S1507, S1607). As a result, for source code containing terms that satisfy the predetermined relationships, an even more accurate comment can be obtained by having the same language model perform a natural language processing task.
[0116] Furthermore, as explained in Figures 13 and 17, the processor inputs source code and terminology from a different hierarchical level of the predetermined scope unit than the commented source code generated in the predetermined scope unit into the predetermined language model, and updates the commented source code generated in the predetermined scope unit with new comments (for example, comments reflecting function-level comments or block-level comments) obtained by analysis by the predetermined language model. This makes it possible to generate source code that includes comments reflecting comments from other levels with even higher accuracy than the accuracy obtained by executing a natural language processing task using the same language model. As a result of recursively executing a large-scale language model in scope units of the source code structure and outputting source code comments, it becomes possible to add source code comments that take into account the influence of scope units and their structures between source code.
[0117] Furthermore, as explained in Figures 15 and 18, the processor outputs information that associates the meaning of a term having a predetermined relationship with the meaning of the source code in the scope unit, the term indicating that meaning, the design document containing that term, and a score indicating the degree of the predetermined relationship, to the display device screen (for example, the results display screen for displaying the search results and cosine similarity calculation results in S1504 shown in Figure 15). This allows users of the system to easily check the information that underlies the comments included in the commented source code obtained by the system (for example, the meaning of the term and the score value).
[0118] The present invention is not limited to the embodiments described above, and in the implementation stage, the components can be modified and implemented without departing from the gist of the invention, or the multiple components disclosed in the embodiments can be appropriately combined.
[0119] 101 Source code comment generator 102 Design document 103 Source code 104 Source code with comments 105 Large-scale language model device 106 Input unit 107 Source code analysis unit 108 Source code comment generation unit 108a Function processing unit 108b Block processing unit 108c Line processing unit 108d Comment regeneration 109 Data storage unit 109a Source code DB 109b Function source code DB 109c Block source code DB 109e Design document DB 109f Design document terminology DB 110 Display unit 111 Design document preprocessing unit 112 Design document terminology generation unit 201 Source code ID 202 Source code file name 203 Source code 204 Source code with comments 205 Source code update date and time 206 Source code updater ID 207 Source code with comments update date and time 208 Commented source code updater ID 301 Function source code ID 302 Function source code 303 Commented function source code 304 Source code ID 401 Block source code ID 402 Block source code 403 Commented block source code 404 Function source code ID 501 Line source code ID 502 Line source code 503 Commented line source code 504 Block source code ID 601 Document ID 602 Document ID 603 Document name 604 Document content 605 Document vector 606 Update date and time 607 Document publication scope 701 Term ID 702 Term name 703 Term meaning 704 Term meaning vector 705 Document ID 706 Update date and time 707 Updater ID 801 Source code input button802 Design document input button 803 Source code analysis execution button 804 Design document terminology dictionary construction execution button 901 Source code ID selection button 902 Source code display area 903 Source code display area with comments 904 External output
Claims
1. A source code comment generation system that generates comments to source code using a computer having a processor and memory, wherein the source code comment generation system has terminology information that associates terms used in a design document input to the source code comment generation system with the meanings of those terms, and the processor divides the source code into source code of predetermined scope units, and generates commented source code including comments of the predetermined scope unit for the source code of the scope unit, based on the meanings of the terms that have a predetermined relationship with the meanings of the source code of the scope unit, the source code of the scope unit, and a predetermined language model.
2. The source code comment generation system according to claim 1, characterized in that the processor inputs terms used in the design document into a predetermined language model, associates the meaning of the input terms obtained from the predetermined language model with the input terms, and generates term information.
3. The source code comment generation system according to claim 2, characterized in that the processor divides the text contained in the input design document into predetermined units, performs natural language processing on each of the divided texts, and stores them as a design document for generating the term information.
4. The source code comment generation system according to claim 1, characterized in that the processor performs the division of the predetermined scope units in units of functions, blocks, and lines that constitute the source code.
5. The source code comment generation system according to claim 3, characterized in that the processor converts the meaning of the term obtained by performing the natural language processing into a numerical vector.
6. The source code comment generation system according to claim 1, characterized in that the processor inputs the scope-unit source code into the predetermined language model, obtains the meaning of the scope-unit source code analyzed by the predetermined language model, and determines the predetermined relationship between the obtained meaning of the scope-unit source code and the meaning of the term.
7. The source code comment generation system according to claim 6, characterized in that the processor inputs the scope-unit source code and the term for which the predetermined relationship has been determined into the predetermined language model, obtains commented source code including the scope-unit comments analyzed by the predetermined language model, and performs the generation by updating the scope-unit source code with the obtained commented source code.
8. The source code comment generation system according to claim 1, characterized in that the processor inputs source code of a predetermined scope unit at a different hierarchical level from the commented source code generated in the predetermined scope unit, along with the term, into the predetermined language model, and updates the commented source code generated in the predetermined scope unit with new comments obtained by analysis by the predetermined language model.
9. The source code comment generation system according to claim 1, characterized in that the processor outputs to the screen of a display device information relating the meaning of a term that has a predetermined relationship with the meaning of the source code in the scope unit, the term that indicates said meaning, the design document that includes said term, and a score that indicates the degree of said predetermined relationship.
10. A source code comment generation method performed by a computer to generate comments for source code, characterized by: inputting term information that associates terms used in a design document input to the computer with the meanings of those terms; dividing the source code into source code of predetermined scope units; and generating commented source code that includes comments of the predetermined scope unit for the source code of the scope unit, based on the meanings of the terms that have a predetermined relationship with the meanings of the source code of the scope unit, the source code of the scope unit, and a predetermined language model.
Citation Information
Patent Citations
Method and device for generating comment sentence of computer program
JP2001005650A
Source code analysis system and source code analysis method
JP2021157316A
Analysis program, analysis method, and information processing apparatus
JP2022156575A