Question and answer pair generation method and device for function code

By combining an intent classification model with code comments and structural information to generate functional code question-and-answer pairs, the problem that manually annotated question-and-answer pairs cannot reflect code details is solved, and more accurate code explanation is achieved.

CN120470099BActive Publication Date: 2025-10-21JINAN INSPUR DATA TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510964670.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-07-14
Publication Date
2025-10-21
Estimated Expiration
2045-07-14

AI Technical Summary

Technical Problem

In existing technologies, manually annotated function code Q&A pairs cannot effectively reflect the implementation details of the code, resulting in inaccurate Q&A pairs.

Method used

The intention classification model predicts the question type of the target annotation, and combines the component information of the target annotation with the structural information of the code segment to generate question-answer pairs. The model is trained using the distribution of syntactic units, data volume and standard question types of historical code segments to generate question-answer pairs that are more in line with the details of the code.

Benefits of technology

The generated question-and-answer pairs better reflect the implementation details of the functional code, improving the accuracy of the question-and-answer pairs and the coverage of code understanding.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120470099B_ABST
    Figure CN120470099B_ABST
Patent Text Reader

Abstract

The application discloses a question and answer pair generation method and device of function code, and relates to the technical field of computers, and comprises the following steps: obtaining a target generation request; obtaining a target annotation and a target code segment corresponding to the target annotation; predicting the question type of the target annotation by using an intention classification model to obtain a target type; and generating a target question and answer pair corresponding to the target type according to the target component information of the target annotation and the target structure information of the target code segment. By means of the application, the technical problem that the question and answer pair of the function code generated by the manual labeling mode cannot better reflect the implementation details of the function code is solved, and the technical effect that the question and answer pair of the function code which can better reflect the implementation details of the function code is generated is achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a method and device for generating question-answer pairs of function codes. Background Art

[0002] Question-and-answer pairs for function codes, as explanations of the code, can help programming beginners quickly understand the implementation details of the code and also serve as training data for strengthening large language models' ability to understand code. However, current question-and-answer pairs are typically generated by manual annotation. The coverage of code implementation details in these pairs depends heavily on the annotator's experience, and the generated pairs do not fully reflect the code implementation details. Summary of the Invention

[0003] The present application provides a method and device for generating question-answer pairs of function codes, so as to at least solve the problem that the question-answer pairs of function codes generated by manual annotation in the related art cannot well reflect the implementation details of the function codes.

[0004] The present application provides a method for generating a question-answer pair for a function code, comprising: obtaining a target generation request, wherein the target generation request is used to request generation of a question-answer pair for a target function code, the question-answer pair being used to indicate code implementation logic for the target function code;

[0005] In response to the target generation request, obtaining a target annotation in the target function code and a target code segment corresponding to the target annotation, wherein the target annotation is used to indicate a function implemented by the corresponding target code segment;

[0006] The target type is obtained by predicting the question type of the target annotation through the intent classification model. The intent classification model is trained based on the distribution of various grammatical units included in the code annotations of the historical code segments, the data volume of the historical code segments, the data volume of the code annotations, and the standard question types of the historical code segments.

[0007] A target question-answer pair corresponding to the target type of the target code segment is generated based on the target component information of the target annotation and the target structure information of the target code segment. The target component information is used to indicate the semantic components corresponding to the target type in the target annotation, and the target structure information is used to indicate the position and function of each code component in the target code segment in the context of the target functional code.

[0008] The present application also provides a device for generating a question-answer pair for a function code, comprising: a first acquisition module, configured to acquire a target generation request, wherein the target generation request is used to request generation of a question-answer pair for a target function code, the question-answer pair being used to indicate code implementation logic for the target function code;

[0009] A second acquisition module is configured to respond to a target generation request and acquire a target annotation in the target function code and a target code segment corresponding to the target annotation, wherein the target annotation is used to indicate a function implemented by the corresponding target code segment;

[0010] A prediction module is used to predict the question type of the target annotation using an intent classification model to obtain the target type. The intent classification model is trained based on the distribution of various grammatical units included in the code annotations of the historical code segments, the data volume of the historical code segments, the data volume of the code annotations, and the standard question types of the historical code segments.

[0011] A generation module is used to generate a target question-answer pair corresponding to the target type of the target code segment based on the target component information of the target annotation and the target structure information of the target code segment, wherein the target component information is used to indicate the semantic component corresponding to the target type in the target annotation, and the target structure information is used to indicate the position and function of each code component in the target code segment in the context of the target functional code.

[0012] The present application also provides an electronic device, comprising: a memory for storing a computer program; and a processor for implementing the steps of the method for generating question-answer pairs of any of the above-mentioned function codes when executing the computer program.

[0013] The present application also provides a computer-readable storage medium, in which a computer program is stored, wherein when the computer program is executed by a processor, the steps of the method for generating question-answer pairs of any of the above-mentioned function codes are implemented.

[0014] The present application also provides a computer program product, including a computer program, which, when executed by a processor, implements the steps of the method for generating question-answer pairs of any of the above-mentioned function codes.

[0015] Through this application, a target generation request is obtained, a target generation request is responded to, a target annotation in the target function code and a target code segment corresponding to the target annotation are obtained, the question type of the target annotation is predicted by the intent classification model, the target type is obtained, and a target question-answer pair that corresponds to the target type is generated according to the target component information of the target annotation and the target structure information of the target code segment. Since the intent classification model is trained based on the distribution of various grammatical units included in the code annotations of the historical code segment, the data volume of the historical code segment, the data volume of the code annotations, and the standard question types of the historical code segment, the intent classification model can better combine the performance of the target annotation in various grammatical directions to predict a question type that is more in line with the implementation details of the code segment. Then, the target type question-answer pair generated based on the component information of the target annotation and the structural information of the function code combines the target type obtained by the aforementioned prediction on the one hand and the structural information of the code on the other hand. Therefore, the generated question-answer pair is more in line with each code segment and can better reflect the implementation details of the code. Therefore, the technical problem that the question-answer pair of the function code generated by manual annotation cannot better reflect the implementation details of the function code can be solved, and the technical effect of generating a question-answer pair of the function code that can better reflect the implementation details of the function code can be achieved. BRIEF DESCRIPTION OF THE DRAWINGS

[0016] In order to more clearly illustrate the embodiments of the present application, the following is a brief introduction to the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.

[0017] Figure 1 This is a hardware structure block diagram of the method for generating question-answer pairs of function codes according to an embodiment of the present application;

[0018] Figure 2 is a flowchart of a method for generating question-answer pairs of function codes according to an embodiment of the present application;

[0019] Figure 3 is a schematic diagram of a method for generating question-answer data from inline code annotations based on annotation type classification, role semantic annotation, and integration of code structure information according to an embodiment of the present application;

[0020] Figure 4 This is a structural block diagram of a device for generating question-answer pairs of function codes according to an embodiment of the present application. DETAILED DESCRIPTION

[0021] The following will be combined with the accompanying drawings in the embodiments of this application to clearly and completely describe the technical solutions in the embodiments of this application. Obviously, the embodiments described are only part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.

[0022] It should be noted that, in the description of this application, the terms "comprises," "includes," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such process, method, article, or device. The terms "first," "second," etc., in this application are used to distinguish similar objects, and are not used to describe a particular order or sequence.

[0023] In order to enable those skilled in the art to better understand the present application, the present application is further described in detail below with reference to the accompanying drawings and specific implementation methods.

[0024] In combination with the question and answer of the function code, the specific application environment architecture or specific hardware architecture on which the execution of the generation method depends is described here.

[0025] The method embodiments provided in the embodiments of the present application can be executed in a server device or a similar computing device. Taking running on a server device as an example, Figure 1 This is a hardware structure diagram of the method for generating question-answer pairs of function codes according to an embodiment of the present application. Figure 1 As shown, the server device may include one or more ( Figure 1 Only one is shown) a processor 102 (the processor 102 may include but is not limited to a microprocessor MCU or a programmable logic device FPGA) and a memory 104 for storing data. The server device may also include a transmission device 106 and an input / output device 108 for communication functions. It will be understood by those skilled in the art that Figure 1 The structure shown is only for illustration and does not limit the structure of the above server device. Figure 1 More or fewer components than shown, or with Figure 1 Different configurations shown.

[0026] The memory 104 can be used to store computer programs, for example, software programs and modules of application software, such as the computer program corresponding to the question-answer pair generation method of the functional code in the embodiment of the present application. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, that is, implementing the above-mentioned method. The memory 104 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include a memory remotely located relative to the processor 102, and these remote memories may be connected to a server device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and a combination thereof.

[0027] Transmission device 106 is used to receive or transmit data via a network. A specific example of the aforementioned network may include a wireless network provided by a communication provider of the server device. In one embodiment, transmission device 106 includes a network interface controller (NIC), which can be connected to other network devices via a base station to enable communication with the Internet. In another embodiment, transmission device 106 may be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.

[0028] An embodiment of the present application provides a method for generating question-answer pairs of function codes, and the method is described in detail in conjunction with the execution process of the method for generating question-answer pairs of function codes.

[0029] In this embodiment, a method for generating question-answer pairs of function codes is provided. Figure 2 This is a flowchart of a method for generating question-answer pairs of function codes according to an embodiment of the present application. Figure 2 As shown, the method includes the following steps:

[0030] Step S202: Obtain a target generation request, wherein the target generation request is used to request generation of a question-answer pair for a target function code, and the question-answer pair is used to indicate code implementation logic for the target function code;

[0031] Step S204, in response to the target generation request, obtaining a target annotation in the target function code and a target code segment corresponding to the target annotation, wherein the target annotation is used to indicate the function implemented by the corresponding target code segment;

[0032] Step S206: Predicting the question type of the target comment using an intent classification model to obtain a target type, wherein the intent classification model is trained based on the distribution of various types of grammatical units included in the code comments of the historical code segments, the data volume of the historical code segments, the data volume of the code comments, and standard question types of the historical code segments;

[0033] Step S208, generating a target question-answer pair corresponding to the target type of the target code segment based on the target component information of the target annotation and the target structure information of the target code segment, wherein the target component information is used to indicate the semantic component corresponding to the target type in the target annotation, and the target structure information is used to indicate the position and function of each code component in the target code segment in the context of the target functional code.

[0034] Through the above steps, a target generation request is obtained, a target generation request is responded to, a target annotation in the target function code and a target code segment corresponding to the target annotation are obtained, the question type of the target annotation is predicted by the intent classification model, the target type is obtained, and a target question-answer pair that corresponds to the target type is generated according to the target component information of the target annotation and the target structure information of the target code segment. Since the intent classification model is trained based on the distribution of various grammatical units included in the code annotations of the historical code segment, the data volume of the historical code segment, the data volume of the code annotations, and the standard question types of the historical code segment, the intent classification model can better combine the performance of the target annotation in various grammatical directions to predict a question type that is more consistent with the implementation details of the code segment. Then, the target type question-answer pair generated based on the component information of the target annotation and the structural information of the function code combines the target type obtained by the above prediction on the one hand and the structural information of the code on the other hand. Therefore, the generated question-answer pair is more suitable for each code segment and can better reflect the implementation details of the code. Therefore, the technical problem that the question-answer pair of the function code generated by the manual annotation method cannot better reflect the implementation details of the function code can be solved, and the technical effect of generating the question-answer pair of the function code that can better reflect the implementation details of the function code can be achieved.

[0035] In the embodiment provided in step S202, the target function code may refer to, but is not limited to, code written during the software development process to implement a specific function. The target function code may be, but is not limited to, a module or a function used to complete a specific task or operation. The implementation logic of the target function code may be, but is not limited to, described in the form of question-and-answer pairs to facilitate better understanding and use of the code.

[0036] In the embodiment provided in step S204, the target comments may include, but are not limited to, inline comments and / or method header comments, wherein inline comments are comments written directly next to the code line, usually used to explain a specific part or operation of the code; method header comments are comments written before the method definition, usually used to describe the method's function, parameters, return value and possible exceptions thrown.

[0037] Optionally, in this embodiment, obtaining the target annotation in the target function code and the target code segment corresponding to the target annotation may include, but is not limited to, extracting all annotations in the target function code, then deleting invalid annotations to obtain the target annotation, and then obtaining the target code segment corresponding to the target annotation based on the target annotation.

[0038] Optionally, in this embodiment, obtaining the target annotation in the target function code and the target code segment corresponding to the target annotation can also include, but is not limited to, dividing the target function code into multiple code segments. First, the target function code is divided into multiple preliminary code segments according to the logical structure or functional module of the code. Then, the code segments containing annotations are screened out from these preliminary code segments. Finally, for these code segments containing annotations, the corresponding annotation content and the associated code portion are extracted. When dividing the preliminary code segments, it can be, but is not limited to, dividing according to the boundaries of functions or methods, or dividing according to the boundaries of code blocks, or dividing according to member variables and methods of classes, etc.

[0039] In the embodiment provided in step S206, predicting the problem type of the target comment through the intent classification model may include, but is not limited to, inputting the target comment into the intent classification model and receiving the target type output by the intent classification model, or inputting the target comment and the surrounding code context into the intent classification model and receiving the target type output by the intent classification model, wherein the contextual information can help the intent classification model better understand the specific meaning of the comment. For example, the comment may mention a variable or method, and the problem type can be predicted more accurately in combination with the context, or the target comment and the code structure information (such as function, class, code block, etc.) in which it is located can be input into the intent classification model and the target type output by the intent classification model, wherein the code structure information can help the intent classification model understand the semantic context of the comment. For example, if the comment is located in a function, the model can more accurately predict the problem type related to the function.

[0040] In the embodiment provided in step S208, generating a target question-answer pair that corresponds to the target type of the target code segment based on the target component information of the target annotation and the target structure information of the target code segment may include, but is not limited to, generating multiple candidate question-answer pairs of the target code segment based on the target component information and the component information and question template with corresponding relationships; extracting the target type of alternative question-answer pairs from the multiple candidate question-answer pairs; adjusting the alternative question-answer pairs based on the target structure information to obtain the target question-answer pair.

[0041] Optionally, in this embodiment, the target structure information may be used, but is not limited to, to indicate the position and function of each code component in the target code segment in the context of the target functional code. The target structure information may be used, but is not limited to, including the method name in the method signature, a list of parameter types, the names and types of core variables involved, key API (Application Programming Interface) call names, a simplified representation of an AST (Abstract Syntax Tree) node type sequence, and the like.

[0042] As an optional implementation, obtaining the target annotation in the target function code and the target code segment corresponding to the target annotation includes: extracting one or more code annotations from the target function code; determining invalid annotations in one or more code annotations based on the annotation information of each code annotation, wherein the annotation information is used to indicate the situation of the word elements included in the code annotation; deleting the invalid annotations from one or more code annotations to obtain the target annotation; matching the corresponding target code segment for the target annotation based on the annotation position information of the target annotation and the code position information of the target function code, wherein the annotation position information is used to indicate the position of the target annotation in the target function code, and the code position information is used to indicate the position of each code segment in the target function code in the target function code.

[0043] Optionally, in this embodiment, the present application may, but is not limited to, use JDT (Java Development Tools) to extract inline comments and corresponding code pairs located inside the method body from the target function code.

[0044] Optionally, in this embodiment, the code comments in the target function code may include, but are not limited to, invalid comments and valid comments. Invalid comments are comments that do not include implementation details of the code. Question and answer pairs generated based on invalid comments cannot reflect the code implementation logic of the target function code.

[0045] Through the above content, invalid comments are deleted from the numerous comments of the target function code, avoiding the generation of invalid question-answer pairs.

[0046] As an optional embodiment, determining an invalid annotation in one or more code annotations based on the annotation information of each code annotation includes at least one of the following: when a word element parameter of a first code annotation is less than or equal to a parameter threshold, determining the first code annotation as an invalid annotation, wherein the word element parameter is used to indicate the number of words included in the first code annotation, and the annotation information includes the word element parameter; when the word element information of a second code annotation is used to indicate that the second code annotation includes a reference word element, determining the second code annotation as an invalid annotation, wherein the word element information is used to indicate multiple words included in the second code annotation, and the annotation information includes the word element information; when a repetition parameter of a third code annotation is used to indicate that the third code annotation includes a code segment in a target function code, determining the third code annotation as an invalid annotation, wherein the repetition parameter is used to indicate the repetition of the third code annotation and the code segment in the function code, and the annotation information includes the repetition parameter.

[0047] Optionally, in this embodiment, the second code comment may include, but is not limited to, a comment indicating a function to be implemented and / or a comment without a specific descriptive meaning. Reference tokens may include, but are not limited to, "Todo," "Fixme," "Nothing," "Ignore," and the like.

[0048] Optionally, in this embodiment, to ensure the accuracy and effectiveness of the subsequently generated question-and-answer data, this application uses a series of heuristic rules to automatically pre-process and filter code comments, mainly including:

[0049] 1) Removal of short and invalid comments: Comments with a length of less than two words (i.e., first code comments) are deleted. Such comments usually lack sufficient semantic information, making it difficult to extract meaningful content.

[0050] 2) Temporary comment filtering: comments containing words such as "Todo" and "Fixme" (i.e., second-code comments) are automatically removed. These comments mainly record features to be developed or fixed, and have no practical significance for generating question-and-answer data, so they are removed.

[0051] 3) Delete meaningless comments (i.e., second code comments): For example, comments containing "Nothing", "Ignore", etc. that have no specific descriptive meaning will be removed.

[0052] 4) Remove comments containing code: If the comment content contains source code (usually commented out code segments) (i.e., third-code comments), these comments usually do not clearly reflect functional information and will also be removed.

[0053] Through the above content, annotations with too few words, annotations including reference words, and annotations including repeated codes are deleted, which can effectively limit the generation basis of question-answer pairs to annotations with better performance, thereby improving the quality of the generated question-answer pairs.

[0054] As an optional implementation, matching the target comment with the corresponding target code segment is performed based on the comment location information of the target comment and the code location information of the target function code, including: detecting the comment type of the target comment; when the comment type is an inline comment, determining the code segment with the smallest distance from the target comment in each code segment as the target code segment based on the comment location information and the code location information; when the comment type is a method header comment, determining the target function code as the target code segment.

[0055] Optionally, in this embodiment, when the annotation type is a method header annotation, the target function code may be determined as a target code segment, but is not limited to being determined as such.

[0056] Optionally, in this embodiment, if the comment type is an inline comment, the starting and ending line numbers of the inline comment in the target function code may be recorded, but is not limited to recording, to identify the code line or code block directly associated with the inline comment. Typically, an inline comment is located immediately before or after the code line it interprets or at the beginning of a logical code block (such as an if statement block (conditional statement block) or a loop body).

[0057] Through the above content, the corresponding code segments are accurately matched to the target comments, laying a good foundation for generating accurate questions and accurate answers to the questions.

[0058] As an optional implementation, the question type of the target annotation is predicted by the intent classification model to obtain the target type, including: inputting the target annotation into the intent classification model; and receiving the target type output by the intent classification model.

[0059] Optionally, in this embodiment, the intent classification model can be trained based on, but is not limited to, the distribution of various types of grammatical units included in the code comments of historical code segments, the data volume of historical code segments, the data volume of code comments, and the standard question types of historical code segments.

[0060] As an optional implementation, before predicting the question type of the target comment through the intent classification model and obtaining the target type, the method also includes: obtaining a code comment and a standard question type with a corresponding relationship; inputting the code comment into the initial language model to obtain an initial question type output by the initial language model, wherein the initial language model is used to extract language information of the code comment from the code comment when receiving the code comment, and the language information is used to indicate the distribution of various types of grammatical units included in the code comment, the data volume of the historical code segment and the data volume of the code comment; generating an initial question type for the code comment based on the language information; outputting the initial question type; adjusting the initial language model based on the first difference information between the initial question type and the standard question type corresponding to the code comment to obtain the intent classification model.

[0061] Optionally, in this embodiment, the code comments and standard question types having corresponding relationships may be, but are not limited to, obtained by experts in the field of software engineering through manual classification of a small number of randomly selected and screened inline comments.

[0062] Optionally, in this embodiment, the language information may be, but is not limited to, used to indicate the distribution of various types of grammatical units included in the code comments, the data volume of the historical code segments, and the data volume of the code comments. For example, the language information may be, but is not limited to, including TokenNum (number of words) (i.e., the data volume of the code comments), PrepNum (prepositions), ConjunNum (conjunctions), OnlySymbol (only symbols), Keywords (keywords), and Ratio (the ratio of the data volume of the historical code segments to the data volume of the code comments).

[0063] As an optional embodiment, the initial language model includes an initial encoder and an initial decoder. The initial encoder is used to extract a first vector, a second vector, a third vector and a fourth vector of the code annotation when receiving the code annotation, wherein the first vector is used to indicate the relationship between the ratio of the data volume of the code annotation to the data volume of the historical code segment and the ratio threshold, the second vector is used to indicate the prepositions included in the code annotation, the third vector is used to indicate the conjunctions included in the code annotation, and the fourth vector is used to indicate the ratio of symbols in the code annotation to the code annotation; the first vector, the second vector, the third vector and the fourth vector are output to the initial decoder; the initial decoder is used to receive the first vector, the second vector, the third vector and the fourth vector; generate an initial question type based on the first vector, the second vector, the third vector and the fourth vector; and output the initial question type.

[0064] Optionally, in this embodiment, after filtering the code comments, the application obtains high-quality <code, comment> pairs, which are then classified for intent. The specific implementation is as follows:

[0065] (a) Constructing an Annotated Dataset: Software engineering experts were organized to manually classify a small number of randomly selected (e.g., 5,000) filtered inline comments. These comments were annotated into predefined categories based on their primary purpose, primarily including: "What" (describing the function, purpose, or object of the code snippet), "How" (describing the implementation, algorithmic steps, or execution flow of the code snippet), and "Why" (explaining the reason for adopting a certain implementation, the basis for a design decision, or the rationale for a constraint). Specifically, this application proposes six features for automatic classification: TokenNum, PrepNum, ConjunNum, OnlySymbol, Keywords, and Ratio. Specifically, larger TokenNum and Ratio indicate a higher probability that a comment is an implementation detail or explanatory intent, i.e., a How or Why comment. PrepNum and ConjunNum indicate specific relationships (prepositions or conjunctions) within the comment, such as the word "because" indicating a Why comment. Keywords indicate specific words of a type, such as the word "via" indicating a How comment. OnlySymbol means that the comment contains only symbols, such as a code comment containing only "——", which cannot be classified into the What, How, and Why categories.

[0066] (b) Model Training: Select a powerful pre-trained language model (such as CodeBERT (Code Bidirectional Encoder Representations from Transformers)) and fine-tune it using the manually annotated dataset constructed in step (a) to train a text classification model (i.e., intent classification model). The training goal is to maximize the consistency between the model's predicted labels and the manually annotated labels. Experimental results show that the CodeBERT model achieves 90% precision and 90.1% recall, demonstrating excellent performance.

[0067] (c) Automatic classification: The trained text classification model is applied to all the screened high-quality inline annotations to predict and assign the most likely intent label (i.e., What / How / Why) to each annotation.

[0068] Through the above steps, <code, comment> data with precise intent labels is obtained, which provides key semantic guidance information for subsequent steps.

[0069] As an optional implementation, a target question-answer pair corresponding to the target type of the target code segment is generated based on the target component information of the target annotation and the target structure information of the target code segment, including: detecting the target component information and detecting the target structure information; inputting the target component information, target structure information, target type and target annotation into a question generation model, wherein the question generation model is used to generate a question based on the target component information, target structure information, target type and target annotation; receiving the target question output by the question generation model; extracting the answer to the target question from the target annotation to obtain a target question-answer pair.

[0070] Optionally, in this embodiment, the question generation model can be used, but is not limited to, to generate a target question in combination with target component information, target structure information, target type, and target annotation.

[0071] By detecting target component information and target structure information, we can more accurately understand the semantics and structure of target comments and code snippets. By feeding this information into the question generation model, the model can generate high-quality questions that better match the target type. Extracting answers from target comments ensures accuracy and relevance, thereby generating high-quality target question-answer pairs.

[0072] As an optional implementation, detecting target component information includes: identifying verb units in target annotations; identifying grammatical units associated with verb units in target annotations to obtain multiple other grammatical units; marking semantic components of other grammatical units to obtain multiple initial semantic components; extracting semantic components corresponding to the target type from the multiple initial semantic components according to a first correspondence relationship to obtain target component information, wherein the first correspondence relationship is a correspondence relationship between the question type and the semantic component.

[0073] Optionally, in this embodiment, the verb unit in the target annotation can be identified first, followed by the grammatical units associated with the verb unit, and finally the semantic components of the associated grammatical units are annotated to obtain the initial semantic components. By first identifying the verb unit, then identifying the grammatical units associated with the verb unit, and finally annotating the semantic components, this step-by-step refinement can more accurately capture the semantic information in the annotation. Each step can be independently verified and adjusted, thereby reducing errors.

[0074] Optionally, in this embodiment, after obtaining multiple initial semantic components, the present application will also extract semantic components corresponding to the target type from the multiple initial semantic components based on the first correspondence, and regard the extracted semantic components as target component information, which can reduce the operating burden and training burden of the question generation model.

[0075] Optionally, in this embodiment, after the data set cleaning is completed, the present application proposes a grammatical and semantic analysis method based on DependencyParsing (dependency analysis) and SRL (Semantic Role Labeling) to automatically extract rich and accurate semantic information from code comments (i.e., target comments) for the subsequent generation of high-quality code question-and-answer data. Specifically, the present application first uses a natural language processing tool to perform syntactic analysis on the filtered high-quality annotation data to extract a dependency tree that conforms to UD (Universal Dependencies). The dependency tree is a structured representation of syntactic analysis, which takes the verbs in the sentence as the core, and other grammatical units (such as subject, object, adverbial, etc.) are developed around the verbs with direct or indirect dependency relationships, thereby clearly expressing the grammatical structure and internal logical relationships of the sentence.

[0076] To better capture the semantic relationship between verbs and their associated grammatical units, this application further introduces semantic role labeling technology, constructs a semantic labeling model, and labels each grammatical unit according to the Proposition Bank (Semantic Role Labeling Library) semantic labeling specification to obtain multiple semantic roles (i.e., multiple initial semantic components). Semantic role labeling can more accurately define the semantic role played by each grammatical unit, such as subject, direct object, location, manner, temporal, direction, cause / purpose, and other role categories.

[0077] After obtaining multiple semantic roles, the semantic roles annotated by the SRL (for example, the text snippet for the "how" role) are aligned with the corresponding components in the dependency tree and specific code elements (i.e., the structural information of the code) (for example, the lines of code that implement the "how" or the APIs called). At the same time, the intent classification results (What / How / Why) of the annotations are considered to weight or prioritize certain semantic roles. For example, for "How" annotations, the ARGM-MNR (how) role information is analyzed and utilized; for "Why" annotations, the ARGM-PRP (purpose) or ARGM-CAU (cause) role information is analyzed.

[0078] As an optional implementation, detecting the target structure information includes: establishing an abstract syntax tree of the target function code; traversing each node of the abstract syntax tree starting from the root node of the abstract syntax tree until traversing to the target node corresponding to the target code segment; extracting the target node and the child nodes of the target node to obtain a sub-syntax tree corresponding to the target code segment, wherein the target structure information includes the sub-syntax tree.

[0079] Optionally, in this embodiment, the present application may, but is not limited to, use JDT (Java Development Tools) to extract inline comments and corresponding code pairs within method bodies from multiple code projects, and extract local code structure information (i.e., target structure information) strongly associated with the inline comments for subsequent neural network model feature learning. Specifically, this may, but is not limited to, include: parsing local variables and their types defined within the scope of the comment (e.g., within the method body or loop); identifying the control flow structure in which the comment resides, such as the if-else branch, loop body, or try-catch-finally block; and extracting the sequence of abstract syntax tree node types corresponding to the associated code statements, such as MethodInvocation (representing a method call), Assignment (representing a variable assignment operation), IfStatement (representing a conditional statement), as well as the key identifiers associated with these nodes (e.g., the name of the method being called, the name of the variable being assigned). Establishing a strong mapping between the inline comment text and these precise, local code structure elements lays the foundation for the subsequent generation of question-answer pairs that are tightly coupled with the code implementation.

[0080] As an optional implementation, the answer to the target question is extracted from the target annotation to obtain a target question-answer pair, including: determining the semantic component indicated by the target component information as the initial answer to the target question; detecting the matching parameters between the initial answer and the target question, wherein the matching parameters are used to indicate the coverage of the target question by the initial answer; when the matching parameters are less than the matching parameter threshold, generating the answer to the target question based on the target code segment and the target annotation; when the matching parameters are greater than or equal to the matching parameter threshold, determining the initial answer as the answer to the target question.

[0081] Optionally, in this embodiment, for each question generated by the deep learning model (i.e., the question generation model), a corresponding standard answer is found. Answers are preferentially extracted from the original inline annotations (i.e., the target annotations), typically the annotation clauses or snippets most relevant to the question focus (e.g., a specific semantic role or code element). For example, if the question is about "How," the portion of the annotation describing the ARGM-MNR (how) is extracted as the answer. When the annotation information is insufficient to fully answer the question, the answer can be supplemented or verified by combining it with associated code snippet information (i.e., the target code segment and the target annotation).

[0082] As an optional implementation, before inputting the target component information, target structure information, target type and target annotation into the question generation model, the method also includes: obtaining reference annotations and reference questions with corresponding relationships, wherein the reference question is generated based on the semantic components of each word included in the reference annotation and the question template corresponding to each semantic component; inputting the reference annotation, the reference component information of the reference annotation, the reference structure information of the reference annotation and the reference type of the reference annotation into the initial text-to-text transfer converter, wherein the reference component information is used to indicate the semantic component corresponding to the reference type in the reference annotation, the reference structure information is used to indicate the position and function of each code component in the reference code segment corresponding to the reference annotation in the reference function code context to which the reference code segment belongs, and the reference type is the question type of the reference annotation predicted by the intent classification model; adjusting the initial text-to-text transfer converter according to the second difference information between the initial question output by the initial text-to-text transfer converter and the reference question to obtain the question generation model.

[0083] Optionally, in this embodiment, reference questions may be generated based on, but not limited to, question templates corresponding to respective semantic components and question templates corresponding to respective dependency components.

[0084] Optionally, in this embodiment, Table 1 is example one of the question template according to the embodiment of the present application, Table 2 is example two of the question template according to the embodiment of the present application, and Table 3 is example three of the question template according to the embodiment of the present application. Reference questions corresponding to reference annotations can be generated according to, but are not limited to, the template shown in Table 1, or the template shown in Table 2 can be used to generate reference questions corresponding to reference annotations, or the template shown in Table 3 can be used to generate reference questions corresponding to reference annotations.

[0085] Table 1

[0086]

[0087] Table 2

[0088]

[0089] Table 3

[0090]

[0091] Among them, mainAux:main Auxiliary, i.e., the main auxiliary verb; otherAux:other Auxiliary, i.e., other auxiliary verbs; obj:object, i.e., the object; nsubj:subject, i.e., the subject. Italicized and underlined text indicates potential question targets.

[0092] Optionally, in this embodiment, the initial text-to-text transfer transformer may be, but is not limited to, a T5 (Text-to-Text Transfer Transformer) model.

[0093] As an optional implementation method, obtaining reference annotations and reference questions with corresponding relationships includes: marking the semantic components of each word included in the reference annotation to obtain multiple target semantic components; matching a corresponding target question template for each target semantic component from the semantic components and question templates with corresponding relationships; generating one or more candidate questions corresponding to the reference annotation based on the reference annotation, multiple target semantic components and the target question templates corresponding to each semantic component; screening out reference questions that meet the target conditions from the candidate questions to obtain reference annotations and reference questions with corresponding relationships.

[0094] Optionally, in this embodiment, a large number of <structured input sequence, target natural language question> pairs can be constructed, but is not limited to the following. Training data can be obtained from, but is not limited to, the following sources: First, a predefined heuristic rule generator based on annotation classification and SRL results (as a baseline approach) is used to generate an initial batch of question-answer pairs. Subsequently, these pairs are manually reviewed by experts to create a set of high-quality, diverse samples to further improve model performance and its ability to handle complex situations. As shown in Table 1, for example, when the target is the subject, the annotation: "The timer has disabled all timepoints." can generate the question "What has disabled all timepoints?" according to the template. As shown in Table 2, when the target is the method, the annotation: "By clicking on the button, the user can disable all timepoints." can generate the question "How can the user disable all timepoints?" according to the template. As shown in Table 3, when the target is time, the annotation: "After the process is started, we have subscriptions." can generate the question "When do we have subscriptions?" according to the template.

[0095] As an optional implementation, obtaining reference annotations and reference questions with corresponding relationships includes: when the reference annotation includes feature words, prepending the feature words in the reference annotation to obtain the reference question, wherein the feature words include linking verbs, modal verbs and auxiliary verbs; when the reference annotation does not include feature words, matching the corresponding target auxiliary verbs for the reference annotation; and adding the target auxiliary verbs to the head of the reference annotation to obtain the reference question.

[0096] Optionally, in this embodiment, for general judgment questions (questions beginning with "is," "does," "should," etc.), this application proposes a method for automatically generating questions based on analysis of predicate verb clauses. The specific implementation is as follows:

[0097] 1) Predicate Identification and Clause Analysis: This application first analyzes each finite predicate in the annotation text and generates questions based on the clause containing the predicate verb (i.e., the feature word). If an annotation contains multiple predicate verbs, this application generates a corresponding general judgment question for each predicate.

[0098] 2) Sentence reorganization and unit rearrangement: After determining the predicate clause, rearrange the other grammatical units in the predicate clause so that the generated question conforms to the general question expression habits of English.

[0099] 3) Do-support mechanism: When generating general judgment questions, this application proposes a do-support mechanism. This mechanism checks the original annotation for copulas (e.g., is, are), modal verbs (e.g., should, must), and auxiliary verbs (e.g., be, have, do). If so, the verbs (i.e., feature lemmas) in the original annotation are used directly. Otherwise, "do / does / did" are added as auxiliary verbs for support. For example, if the annotation "This operation needs to be completed." does not contain a copula or modal verb, this application automatically adds do-support, generating the question: "Does this operation need to be completed?" Through these steps, the general judgment questions automatically generated by this application not only conform to linguistic standards but also accurately express the core semantic information of the original annotation, effectively improving the quality and diversity of the dataset.

[0100] As an optional implementation, the reference annotations, the reference component information of the reference annotations, the reference structure information of the reference annotations, and the reference type of the reference annotations are input into an initial text-to-text transfer converter, including: splicing the reference annotations, the reference component information, the reference structure information, and the reference type according to the serialization template to obtain a reference sequence; and inputting the reference sequence into the initial text-to-text transfer converter.

[0101] Optionally, in this embodiment, when the reference annotations, the reference component information of the reference annotations, the reference structure information of the reference annotations, and the reference types of the reference annotations are input into the initial text-to-text transfer converter, the reference annotations, the reference component information, the reference structure information, and the reference types can be sorted in a fixed order, but are not limited to, and the sorted reference annotations, reference component information, reference structure information, and reference type sequence are input into the initial text-to-text transfer converter.

[0102] As an optional embodiment, the initial text-to-text transfer converter includes a reference encoder and a reference decoder, and inputting the reference sequence into the initial text-to-text transfer converter includes:

[0103] A reference sequence is input into a reference encoder, wherein the reference encoder is used to generate a context vector of the reference sequence when the reference sequence is received; the context vector is output to a reference decoder, and the reference decoder is used to generate multiple candidate sequences based on the context vector when the context vector is received, wherein each word in the candidate sequence is generated based on the context vector and the intermediate sequence corresponding to each word, and the intermediate sequence is obtained by splicing the previously generated intermediate words; the candidate sequence with the largest cumulative probability among the multiple candidate sequences is determined as the initial problem, wherein the cumulative probability is the product of the probabilities of each intermediate word included in each candidate sequence, and the probability of each intermediate word is used to indicate the conditional probability of each intermediate word as the next word in the intermediate sequence under the corresponding intermediate sequence and the context vector.

[0104] As an optional implementation, this application adopts a sequence-to-sequence model based on deep learning to achieve automatic and adaptive generation of high-quality natural language questions from rich contextual information.

[0105] Optionally, in this embodiment, an encoder-decoder architecture based on Transformer may be used, but is not limited to it. Specifically, a pre-trained T5 (Text-to-Text Transfer Transformer) model is selected as the base model and fine-tuned for the code question-answering generation task. The T5 model unifies all text processing tasks into a text-to-text conversion format, which is suitable for generation tasks. It is possible, but not limited to, to encode the multi-source information extracted from the previous steps (i.e., reference annotations, reference component information, reference structure information, and reference types) into a structured input sequence for the encoder of the T5 model. The specific input construction method may include, but is not limited to: converting the annotation text (i.e., reference annotations), intent classification labels (converted into special text tags, such as <what> , <how> , <why>) (i.e. reference type), key SRL roles (i.e. reference component information) (such as predicate, ARG0 (Argument 0, argument 0, usually represents the subject of the predicate), ARG1 (Argument 1, argument 1, usually represents the direct object of the predicate), and ARGM (Argument Modifier, argument modifier, modification information of the predicate or argument, such as time, place, method, reason, etc.) roles selected according to the annotation intention, expressed in the form of ROLE:text (i.e. role: text)), and key structural information of the associated code fragment (i.e. reference structural information) (such as the method name and parameter type list in the method signature; the core variable names and types involved; the key API call name; the simplified representation of the AST node type sequence) are serialized and spliced. Special separators are used to distinguish information from different sources. For example: classify: <how>comment: / / Sort the array using quicksort for efficiency. srl: predicate: Sort arg1: the array argm-mnr: using quicksortargm-prp: ​​for efficiency code_context: method: sortArray params: int[] dataapi_calls: Arrays.sort ast_nodes: MethodInvocation Assignment. The serialized text is fed into the encoder of the T5 model (i.e., the reference encoder). The encoder utilizes its self-attention mechanism to process the entire input sequence, capturing the complex dependencies between different pieces of information and generating contextualized embeddings (i.e., context vectors). Training is performed using a standard sequence-to-sequence learning paradigm. Cross-entropy loss is used as the optimization objective to minimize the difference (i.e., second difference information) between the token sequence generated by the model and the target question token sequence. Training employs learning rate warm-up and decay strategies, gradient clipping, and other techniques to ensure training stability and effectiveness. Monitor the loss on the validation set and the changes in automatic evaluation metrics to select the model checkpoint with the best performance. Specifically, after the model receives a new contextual input (including annotations, classifications, SRL, and code association information), it is encoded and passed through the trained T5 model's encoder. The model's decoder then generates tokens that constitute the question one by one using autoregressive generation. During decoding, the decoder retains multiple (for example, five) candidate sequences with the highest probability at each generation step, ultimately selecting the complete sequence with the highest overall probability (i.e., cumulative probability) as the generated question. This helps generate more fluent and reasonable natural language questions.The model ultimately outputs a complete natural language question text string, such as "How does this code sort the array for efficiency?" or "What is the purpose of the quicksort algorithm used here?". The model is able to adaptively adjust the question words, sentence structure, and focus of the generated question based on subtle changes in the input (such as whether the comment intention is How or Why, the specific role identified by SRL, and the associated code features).

[0106] In summary, this application can automatically, efficiently, and accurately generate large amounts of high-quality problem data from code comments. This method significantly reduces manual annotation costs and improves data consistency and scale, effectively improving the performance of code comprehension and code intelligence tasks based on large-scale pre-trained language models. It has broad application value and significant technical advantages.

[0107] As an optional implementation, the present application also provides a method for generating question-and-answer data from inline code comments based on comment type classification, role semantic annotation, and integration of code structure information. The method aims to automatically and efficiently extract accurate semantic information from large-scale code inline comment data, and generate high-quality code question-and-answer pairs based on neural networks, so as to enrich and enhance the training data resources of large-scale pre-trained language models and improve their performance in code comprehension and code question-and-answer tasks. Figure 3 is a schematic diagram of a method for generating question-answer data based on inline code annotations based on annotation type classification, role semantic annotation, and integration of code structure information according to an embodiment of the present application, such as Figure 3 As shown, this application first collects a large number of <code, comment> pairs from open source code repositories. Using the JDT tool, it parses the abstract syntax trees and other structural information of the code snippets to establish a mapping between inline comments and their adjacent or related specific code statements / code blocks. Using a variety of heuristic rules, it filters out short, invalid comments, Todo comments, meaningless comments, and comments containing source code, thereby selecting high-quality comments. This application uses machine learning methods to classify the intent of the cleaned inline comments. By fine-tuning a pre-trained language model, it trains an annotation classifier (i.e., an intent classification model) to automatically assign intent labels (e.g., What / How / Why) to the large-scale annotation data. This application uses natural language processing tools to perform grammatical analysis on these cleaned, high-quality code comments and construct a dependency tree. Furthermore, it further incorporates semantic role labeling technology to perform refined semantic annotation of each grammatical component in the annotation text according to PropBank specifications, clarifying the semantic role of each word, such as subject (agent), object (patient), time, place, manner, and reason, providing sufficient semantic basis for subsequent question generation. A neural network model then uses the code association information (i.e., target structure information / reference structure information), annotation intent (i.e., target type / reference type), and grammatical and semantic structure (i.e., target component information / reference component information) extracted from the aforementioned content as input to automatically generate highly contextually relevant natural language questions, covering both W-type questions and general judgment questions. Based on the generated questions, corresponding semantic roles or phrases are extracted from the original annotations as standard answers. The resulting question-and-answer data consists of <code snippet, question, standard answer> triplets, forming a large-scale dataset.

[0108] In summary, this application performs intent classification on inline comments, integrates dependency syntax analysis, semantic role labeling technology, and code structure information, and uses a deep learning model for question generation. This allows for the flexible generation of the most valuable questions based on the different intentions of the comments (explaining functionality, implementation, or reasoning) and the specific code context. This significantly improves the accuracy, relevance, diversity, and effectiveness of question-and-answer data, overcoming the limitations of fixed templates. Compared to existing manual annotation methods or simple rule extraction methods based solely on text, this application generates higher-quality question-and-answer pairs, provides more targeted code understanding, uses larger data scale, and achieves a higher degree of process automation.

[0109] Training language models with high-quality, code-aware code question-and-answer data generated by this application can effectively improve the performance of large-scale pre-trained language models on code comprehension tasks. By leveraging inline comments and annotation classification information, the generated data (i.e., question-and-answer pairs) can guide the model to understand the fine-grained execution steps of the code, the role of local variables, and the principles behind specific implementations, which is crucial for understanding the internal logic of complex methods. This high-precision data enables large models to more deeply understand the different semantic roles, detailed logic, and implementation methods in the code, thereby enhancing their performance in practical application scenarios such as code comprehension, semantic retrieval, automatic code generation, and question answering.

[0110] The question-and-answer data generated by this application intuitively and clearly reflects the code implementation logic, details, and implementation principles, forming an interactive code documentation format that can effectively improve the readability and comprehension of the code. The generated question-and-answer pairs for inline comments can help developers quickly understand the details and reasons for specific snippets in the code, especially when reading complex logic written by others. Clear and unambiguous code question-and-answer data can greatly shorten the learning curve for developers, reduce the difficulty of understanding complex code, reduce ambiguity and misunderstandings, and improve development efficiency.

[0111] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus the necessary general hardware platform, and of course it can also be implemented by hardware, but in many cases the former is a better implementation method.

[0112] The embodiment of the present application also provides a device for generating question-answer pairs of function codes. Figure 4 This is a structural block diagram of a device for generating question-answer pairs of function codes according to an embodiment of the present application. Figure 4 As shown, the device includes:

[0113] A first acquisition module 402 is configured to acquire a target generation request, wherein the target generation request is used to request generation of a question-answer pair for a target function code, wherein the question-answer pair is used to indicate code implementation logic for the target function code;

[0114] The second acquisition module 404 is configured to respond to the target generation request and acquire a target annotation in the target function code and a target code segment corresponding to the target annotation, wherein the target annotation is used to indicate a function implemented by the corresponding target code segment;

[0115] Prediction module 406 is configured to predict the question type of the target annotation using an intent classification model to obtain a target type, wherein the intent classification model is trained based on the distribution of various grammatical units included in the code annotations of the historical code segments, the data volume of the historical code segments, the data volume of the code annotations, and standard question types of the historical code segments;

[0116] Generation module 408 is used to generate a target question-answer pair corresponding to the target type of the target code segment based on the target component information of the target annotation and the target structure information of the target code segment, wherein the target component information is used to indicate the semantic component corresponding to the target type in the target annotation, and the target structure information is used to indicate the position and function of each code component in the target code segment in the context of the target functional code.

[0117] Through the above device, a target generation request is obtained, a target generation request is responded to, a target annotation in the target function code and a target code segment corresponding to the target annotation are obtained, the question type of the target annotation is predicted by the intent classification model, the target type is obtained, and the target question and answer pair corresponding to the target code segment is generated according to the target component information of the target annotation and the target structure information of the target code segment. Since the intent classification model is trained based on the distribution of various grammatical units included in the code annotations of the historical code segment, the data volume of the historical code segment, the data volume of the code annotation and the standard question type of the historical code segment, the intent classification model can better combine the performance of the target annotation in various grammatical directions to predict the question type that is more in line with the implementation details of the code segment. Then, the target type question and answer pair generated based on the component information of the target annotation and the structural information of the function code combines the target type obtained by the above prediction on the one hand and the structural information of the code on the other hand, so that the generated question and answer pair is more in line with each code segment and can better reflect the implementation details of the code. Therefore, the technical problem that the question and answer pair of the function code generated by the manual annotation method cannot better reflect the implementation details of the function code can be solved, and the technical effect of generating the question and answer pair of the function code that can better reflect the implementation details of the function code can be achieved.

[0118] Optionally, the second acquisition module includes: a first extraction unit for extracting one or more code comments from the target function code; a determination unit for determining invalid comments in one or more code comments based on the comment information of each code comment, wherein the comment information is used to indicate the situation of the word elements included in the code comment; a deletion unit for deleting invalid comments from one or more code comments to obtain the target comment; a matching unit for matching the target comment with the corresponding target code segment based on the comment position information of the target comment and the code position information of the target function code, wherein the comment position information is used to indicate the position of the target comment in the target function code, and the code position information is used to indicate the position of each code segment in the target function code in the target function code.

[0119] Optionally, the determination unit is further used to: when a word element parameter of the first code annotation is less than or equal to a parameter threshold, determine the first code annotation as an invalid annotation, wherein the word element parameter is used to indicate the number of words included in the first code annotation, and the annotation information includes the word element parameter; when the word element information of the second code annotation is used to indicate that the second code annotation includes a reference word element, determine the second code annotation as an invalid annotation, wherein the word element information is used to indicate multiple words included in the second code annotation, and the annotation information includes the word element information; when a repetition parameter of the third code annotation is used to indicate that the third code annotation includes a code segment in the target function code, determine the third code annotation as an invalid annotation, wherein the repetition parameter is used to indicate the repetition of the third code annotation and the code segment in the function code, and the annotation information includes the repetition parameter.

[0120] Optionally, the matching unit is also used to: detect the comment type of the target comment; when the comment type is an inline comment, determine the code segment with the smallest distance from the target comment in each code segment as the target code segment based on the comment position information and the code position information; when the comment type is a method header comment, determine the target function code as the target code segment.

[0121] Optionally, the prediction module includes: a first input unit for inputting target annotations into the intent classification model; and a first receiving unit for receiving the target type output by the intent classification model.

[0122] Optionally, the device for generating question-answer pairs for functional codes further includes: a third acquisition module for acquiring code comments and standard question types having a corresponding relationship; an input module for inputting the code comments into the initial language model to obtain the initial question type output by the initial language model, wherein the initial language model is used to extract language information of the code comments from the code comments when the code comments are received, and the language information is used to indicate the distribution of various types of grammatical units included in the code comments, the data volume of historical code segments, and the data volume of the code comments; generating the initial question type of the code comments based on the language information; outputting the initial question type; and an adjustment module for adjusting the initial language model based on the first difference information between the initial question type and the standard question type corresponding to the code comments to obtain an intent classification model.

[0123] Optionally, the initial language model includes an initial encoder and an initial decoder, the initial encoder being used to extract a first vector, a second vector, a third vector and a fourth vector of the code annotation when receiving the code annotation, wherein the first vector is used to indicate the size relationship between the ratio of the data volume of the code annotation to the data volume of the historical code segment and a ratio threshold, the second vector is used to indicate the prepositions included in the code annotation, the third vector is used to indicate the conjunctions included in the code annotation, and the fourth vector is used to indicate the ratio of symbols in the code annotation to the code annotation; the first vector, the second vector, the third vector and the fourth vector are output to the initial decoder; the initial decoder is used to receive the first vector, the second vector, the third vector and the fourth vector; generate an initial question type based on the first vector, the second vector, the third vector and the fourth vector; and output the initial question type.

[0124] Optionally, the generation module includes: a detection unit for detecting target component information and detecting target structure information; a second input unit for inputting target component information, target structure information, target type and target annotation into a question generation model, wherein the question generation model is used to generate questions based on the target component information, target structure information, target type and target annotation; a second receiving unit for receiving the target question output by the question generation model; and a second extraction unit for extracting the answer to the target question from the target annotation to obtain a target question-answer pair.

[0125] Optionally, the detection unit is also used to: identify verb units in the target annotation; identify grammatical units associated with the verb units in the target annotation to obtain multiple other grammatical units; label the semantic components of other grammatical units to obtain multiple initial semantic components; extract semantic components corresponding to the target type from the multiple initial semantic components according to a first correspondence relationship to obtain target component information, wherein the first correspondence relationship is the correspondence relationship between the question type and the semantic component.

[0126] Optionally, the detection unit is also used to: establish an abstract syntax tree of the target function code; traverse each node of the abstract syntax tree starting from the root node of the abstract syntax tree until traversing to the target node corresponding to the target code segment; extract the target node and the child nodes of the target node to obtain a sub-syntax tree corresponding to the target code segment, wherein the target structure information includes the sub-syntax tree.

[0127] Optionally, the second extraction unit is further used to: determine the semantic component indicated by the target component information as the initial answer to the target question; detect the matching parameters between the initial answer and the target question, wherein the matching parameters are used to indicate the coverage of the target question by the initial answer; when the matching parameters are less than the matching parameter threshold, generate the answer to the target question based on the target code segment and the target annotation; when the matching parameters are greater than or equal to the matching parameter threshold, determine the initial answer as the answer to the target question.

[0128] Optionally, the generation module also includes: an acquisition unit, used to acquire reference annotations and reference questions with corresponding relationships, wherein the reference questions are generated based on the semantic components of each word included in the reference annotations and the question templates corresponding to each semantic component; a third input unit, used to input the reference annotations, the reference component information of the reference annotations, the reference structure information of the reference annotations, and the reference type of the reference annotations into the initial text-to-text transfer converter, wherein the reference component information is used to indicate the semantic components corresponding to the reference type in the reference annotations, the reference structure information is used to indicate the position and function of each code component in the reference code segment corresponding to the reference annotation in the reference function code context to which the reference code segment belongs, and the reference type is the question type of the reference annotation predicted by the intent classification model; an adjustment unit, used to adjust the initial text-to-text transfer converter based on the second difference information between the initial question output by the initial text-to-text transfer converter and the reference question, to obtain a question generation model.

[0129] Optionally, the acquisition unit is also used to: mark the semantic components of each word included in the reference annotation to obtain multiple target semantic components; match a corresponding target question template for each target semantic component from the semantic components and question templates with corresponding relationships; generate one or more candidate questions corresponding to the reference annotation based on the reference annotation, multiple target semantic components and the target question templates corresponding to each semantic component; filter out reference questions that meet the target conditions from the candidate questions to obtain reference annotations and reference questions with corresponding relationships.

[0130] Optionally, the acquisition unit is also used to: when the reference annotation includes feature words, prepend the feature words in the reference annotation to obtain a reference question, wherein the feature words include linking verbs, modal verbs and auxiliary verbs; when the reference annotation does not include feature words, match the corresponding target auxiliary verbs for the reference annotation; and add the target auxiliary verbs to the head of the reference annotation to obtain a reference question.

[0131] Optionally, the third input unit is further used to: obtain a reference sequence by splicing reference annotations, reference component information, reference structure information and reference type according to the serialization template; and input the reference sequence into the initial text-to-text transfer converter.

[0132] Optionally, the initial text-to-text transfer converter includes a reference encoder and a reference decoder, and the third input unit is further used to: input a reference sequence into the reference encoder, wherein the reference encoder is used to generate a context vector of the reference sequence when the reference sequence is received; output the context vector to the reference decoder, and the reference decoder is used to generate multiple candidate sequences based on the context vector when the context vector is received, wherein each word in the candidate sequence is generated based on the context vector and the intermediate sequence corresponding to each word, and the intermediate sequence is obtained by splicing the previously generated intermediate words; determine the candidate sequence with the largest cumulative probability among the multiple candidate sequences as the initial problem, wherein the cumulative probability is the product of the probabilities of each intermediate word included in each candidate sequence, and the probability of each intermediate word is used to indicate the conditional probability of each intermediate word as the next word in the intermediate sequence under the corresponding intermediate sequence and context vector.

[0133] For the description of the features in the embodiment corresponding to the device for generating question and answer pairs of function codes, please refer to the relevant description of the embodiment corresponding to the method for generating question and answer pairs of function codes, which will not be repeated here.

[0134] An embodiment of the present application also provides an electronic device, including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to execute the steps in any of the above-mentioned question-answer pair generation method embodiments of the function code.

[0135] An embodiment of the present application further provides a computer-readable storage medium, in which a computer program is stored, wherein the computer program is configured to execute the steps in the embodiment of the question-answer pair generation method of any of the above-mentioned function codes when running.

[0136] In an exemplary embodiment, the computer-readable storage medium may include, but is not limited to, various media that can store computer programs, such as a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk, or an optical disk.

[0137] An embodiment of the present application further provides a computer program product, which includes a computer program. When the computer program is executed by a processor, it implements the steps in the embodiment of the method for generating question and answer pairs of any of the above-mentioned function codes.

[0138] An embodiment of the present application also provides another computer program product, including a non-volatile computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, it implements the steps in the embodiment of the question-answer pair generation method for any of the above-mentioned function codes.

[0139] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the above description has generally described the components and steps of each example according to their functions. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0140] The above is a detailed introduction to the method and device for generating question-answer pairs of a function code provided by this application. This article uses specific examples to illustrate the principles and implementation methods of this application. The description of the above embodiments is only used to help understand the method and core idea of ​​this application. It should be pointed out that for ordinary technicians in this technical field, without departing from the principles of this application, several improvements and modifications can be made to this application, and these improvements and modifications also fall within the scope of protection of the claims of this application.< / how> < / why> < / how> < / what>

Claims

1. A method for generating question-answer pairs of function codes, characterized in that: include: Obtaining a target generation request, wherein the target generation request is used to request generation of a question-answer pair for a target function code, the question-answer pair being used to indicate code implementation logic for the target function code; In response to the target generation request, obtaining a target annotation in the target function code and a target code segment corresponding to the target annotation, wherein the target annotation is used to indicate a function implemented by the corresponding target code segment; Predicting the question type of the target annotation using an intent classification model to obtain a target type, wherein the intent classification model is trained based on the distribution of various types of grammatical units included in the code annotations of the historical code segment, the data volume of the historical code segment, the data volume of the code annotations, and standard question types of the historical code segment; A target question-answer pair corresponding to the target code segment and conforming to the target type is generated based on the target component information of the target annotation and the target structure information of the target code segment, wherein the target component information is used to indicate the semantic component corresponding to the target type in the target annotation, and the target structure information is used to indicate the position and function of each code component in the target code segment in the context of the target functional code.

2. The method for generating question-answer pairs of function codes according to claim 1, wherein: The acquiring of the target annotation in the target function code and the target code segment corresponding to the target annotation comprises: Extracting one or more code comments from the target function code; Determining an invalid annotation among one or more code annotations according to annotation information of each code annotation, wherein the annotation information is used to indicate a condition of a word element included in the code annotation; Deleting the invalid comment from one or more code comments to obtain the target comment; The target code segment corresponding to the target annotation is matched according to the comment position information of the target annotation and the code position information of the target function code, wherein the comment position information is used to indicate the position of the target annotation in the target function code, and the code position information is used to indicate the position of each code segment in the target function code in the target function code.

3. The method for generating question-answer pairs of function codes according to claim 2, wherein: The determining, based on the annotation information of each of the code annotations, an invalid annotation in one or more of the code annotations includes at least one of the following: If a word-gram parameter of a first code annotation is less than or equal to a parameter threshold, determining the first code annotation as the invalid annotation, wherein the word-gram parameter is used to indicate the number of words included in the first code annotation, and the annotation information includes the word-gram parameter; determining the second code annotation as the invalid annotation when word-gram information of the second code annotation is used to indicate that the second code annotation includes a reference word-gram, wherein the word-gram information is used to indicate a plurality of word-grams included in the second code annotation, and the annotation information includes the word-gram information; In the case where the repetition parameter of the third code annotation is used to indicate that the third code annotation includes the code segment in the target function code, the third code annotation is determined as the invalid annotation, wherein the repetition parameter is used to indicate the repetition of the third code annotation and the code segment in the function code, and the annotation information includes the repetition parameter.

4. The method for generating question-answer pairs of function codes according to claim 2, wherein: The matching of the target code segment corresponding to the target annotation according to the annotation position information of the target annotation and the code position information of the target function code includes: Detecting the annotation type of the target annotation; In a case where the comment type is an inline comment, determining, according to the comment position information and the code position information, the code segment with the smallest distance from the target comment among the code segments as the target code segment; In a case where the annotation type is a method header annotation, the target function code is determined as the target code segment.

5. The method for generating question-answer pairs of function codes according to claim 1, wherein: The method of predicting the question type of the target annotation by the intent classification model to obtain the target type includes: inputting the target annotation into the intent classification model; Receive the target type output by the intent classification model.

6. The method for generating question-answer pairs of function codes according to claim 1, wherein: Before predicting the question type of the target annotation by the intent classification model to obtain the target type, the method further includes: Obtaining the code comments and the standard question types having corresponding relationships; Inputting the code annotation into an initial language model to obtain an initial question type output by the initial language model, wherein the initial language model is configured to extract language information of the code annotation from the code annotation upon receiving the code annotation, the language information being configured to indicate the distribution of various types of grammatical units included in the code annotation, the data volume of the historical code segment, and the data volume of the code annotation; generating the initial question type for the code annotation based on the language information; and outputting the initial question type; The initial language model is adjusted according to first difference information between the initial question type and the standard question type corresponding to the code comment to obtain the intent classification model.

7. The method for generating question-answer pairs of function codes according to claim 6, wherein: The initial language model includes an initial encoder and an initial decoder, The initial encoder is configured to, upon receiving the code annotation, extract a first vector, a second vector, a third vector, and a fourth vector of the code annotation, wherein the first vector is configured to indicate a magnitude relationship between a ratio of a data volume of the code annotation to a data volume of the historical code segment and a ratio threshold, the second vector is configured to indicate a number of prepositions included in the code annotation, the third vector is configured to indicate a number of conjunctions included in the code annotation, and the fourth vector is configured to indicate a ratio of a symbol in the code annotation to a symbol in the code annotation; and output the first vector, the second vector, the third vector, and the fourth vector to the initial decoder; The initial decoder is configured to receive the first vector, the second vector, the third vector, and the fourth vector; generate the initial question type according to the first vector, the second vector, the third vector, and the fourth vector; and output the initial question type.

8. The method for generating question-answer pairs of function codes according to claim 1, wherein: Generating a target question-answer pair corresponding to the target code segment and conforming to the target type according to the target component information of the target annotation and the target structure information of the target code segment includes: detecting the target component information and detecting the target structure information; Inputting the target component information, the target structure information, the target type, and the target annotation into a question generation model, wherein the question generation model is used to generate a question based on the target component information, the target structure information, the target type, and the target annotation; Receiving a target question output by the question generation model; The answer to the target question is extracted from the target annotation to obtain the target question-answer pair.

9. The method for generating question-answer pairs of function codes according to claim 8, wherein: The detecting the target component information includes: identifying verb units in the target annotation; identifying a grammatical unit associated with a verb unit in the target annotation to obtain a plurality of other grammatical units; marking the semantic components of the other grammatical units to obtain a plurality of initial semantic components; The semantic component corresponding to the target type is extracted from the multiple initial semantic components according to a first corresponding relationship to obtain the target component information, wherein the first corresponding relationship is a corresponding relationship between the question type and the semantic component.

10. The method for generating question-answer pairs of function codes according to claim 8, wherein: The detecting the target structure information includes: Establishing an abstract syntax tree of the target function code; Traversing each node of the abstract syntax tree starting from the root node of the abstract syntax tree until traversing to a target node corresponding to the target code segment; The target node and child nodes of the target node are extracted to obtain a sub-syntax tree corresponding to the target code segment, wherein the target structure information includes the sub-syntax tree.

11. The method for generating question-answer pairs of function codes according to claim 8, wherein: Extracting the answer to the target question from the target annotation to obtain the target question-answer pair includes: determining the semantic component indicated by the target component information as an initial answer to the target question; Detecting a matching parameter between the initial answer and the target question, wherein the matching parameter is used to indicate the degree to which the initial answer covers the target question; When the matching parameter is less than a matching parameter threshold, generating an answer to the target question according to the target code segment and the target annotation; When the matching parameter is greater than or equal to the matching parameter threshold, the initial answer is determined as the answer to the target question.

12. The method for generating question-answer pairs of function codes according to claim 8, wherein: Before inputting the target component information, the target structure information, the target type, and the target annotation into the question generation model, the method further includes: Obtaining reference annotations and reference questions having corresponding relationships, wherein the reference questions are generated based on semantic components of each word included in the reference annotations and question templates corresponding to each semantic component; Inputting the reference annotation, reference component information of the reference annotation, reference structure information of the reference annotation, and reference type of the reference annotation into an initial text-to-text transfer converter, wherein the reference component information is used to indicate the semantic component corresponding to the reference type in the reference annotation, the reference structure information is used to indicate the position and function of each code component in the reference code segment corresponding to the reference annotation in the context of the reference function code to which the reference code segment belongs, and the reference type is the problem type of the reference annotation predicted by the intent classification model; The initial text-to-text transfer converter is adjusted according to second difference information between the initial question output by the initial text-to-text transfer converter and the reference question to obtain the question generation model.

13. The method for generating question-answer pairs of function codes according to claim 12, wherein: The obtaining of reference annotations and reference questions having corresponding relationships includes: Annotating the semantic components of each word included in the reference annotation to obtain multiple target semantic components; Matching a corresponding target question template for each target semantic component from semantic components and question templates having a corresponding relationship; Generate one or more candidate questions corresponding to the reference annotations according to the reference annotations, the multiple target semantic components, and the target question templates corresponding to the respective semantic components; The reference questions that meet the target conditions are screened out from the candidate questions, and the reference annotations and the reference questions with corresponding relationships are obtained.

14. The method for generating question-answer pairs of function codes according to claim 12, wherein: The obtaining of reference annotations and reference questions having corresponding relationships includes: In the case where the reference annotation includes a characteristic word, the characteristic word in the reference annotation is prepended to obtain the reference question, wherein the characteristic word includes a linking verb, a modal verb, and an auxiliary verb; In the case that the reference annotation does not include a feature word, a corresponding target auxiliary verb is matched for the reference annotation; and the target auxiliary verb is added to the head of the reference annotation to obtain the reference question.

15. The method for generating question-answer pairs of function codes according to claim 12, wherein: The step of inputting the reference annotation, the reference component information of the reference annotation, the reference structure information of the reference annotation, and the reference type of the reference annotation into an initial text-to-text transfer converter comprises: Concatenate the reference annotation, the reference component information, the reference structure information, and the reference type according to a sequence template to obtain a reference sequence; The reference sequence is input into the initial text-to-text transfer converter.

16. The method for generating question-answer pairs of function codes according to claim 15, wherein: The initial text-to-text transfer converter includes a reference encoder and a reference decoder, and inputting the reference sequence into the initial text-to-text transfer converter includes: The reference sequence is input into the reference encoder, wherein the reference encoder is used to generate a context vector of the reference sequence when the reference sequence is received; the context vector is output to the reference decoder, and the reference decoder is used to generate multiple candidate sequences based on the context vector when the context vector is received, wherein each word in the candidate sequence is generated based on the context vector and the intermediate sequence corresponding to each word, and the intermediate sequence is obtained by splicing the previously generated intermediate words; the candidate sequence with the largest cumulative probability among the multiple candidate sequences is determined as the initial problem, wherein the cumulative probability is the product of the probabilities of each intermediate word included in each candidate sequence, and the probability of each intermediate word is used to indicate the conditional probability of each intermediate word as the next word of the intermediate sequence under the corresponding intermediate sequence and the context vector.

17. A device for generating question-answer pairs of function codes, characterized in that: include: A first acquisition module is configured to acquire a target generation request, wherein the target generation request is used to request generation of a question-answer pair of a target function code, and the question-answer pair is used to indicate code implementation logic of the target function code; a second acquisition module, configured to respond to the target generation request and acquire a target annotation in the target function code and a target code segment corresponding to the target annotation, wherein the target annotation is used to indicate a function implemented by the corresponding target code segment; a prediction module, configured to predict the question type of the target annotation using an intent classification model to obtain a target type, wherein the intent classification model is trained based on the distribution of various types of grammatical units included in the code annotations of the historical code segments, the data volume of the historical code segments, the data volume of the code annotations, and standard question types of the historical code segments; A generation module is used to generate a target question-answer pair corresponding to the target code segment that conforms to the target type based on the target component information of the target annotation and the target structure information of the target code segment, wherein the target component information is used to indicate the semantic component corresponding to the target type in the target annotation, and the target structure information is used to indicate the position and function of each code component in the target code segment in the context of the target functional code.

18. An electronic device, characterized in that: include: memory for storing computer programs; A processor, configured to implement the steps of the method for generating question-answer pairs of function codes as claimed in any one of claims 1 to 16 when executing the computer program.

19. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, wherein when the computer program is executed by a processor, the steps of the method for generating question-answer pairs of function codes according to any one of claims 1 to 16 are implemented.

20. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method for generating question-answer pairs of function codes as claimed in any one of claims 1 to 16 are implemented.

Citation Information

Patent Citations

  • Method and system for generating API mark of tutorial clips and and computer equipment

    CN112181389A

  • Code analysis method, device and system, electronic equipment and storage medium

    CN118796191A