Model-based question answering methods and devices that generate context based on grammatical structure
By locating code blocks and their ancestor nodes from the abstract syntax tree, generating target code snippets, and splicing them with context, the problem that question-answering models cannot simultaneously obtain code details and locations is solved, thus improving the question-answering effect.
Patent Information
- Application Number
- CN202511851043.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-10
- Publication Date
- 2026-03-06
- Estimated Expiration
- 2045-12-10
AI Technical Summary
Existing question-answering models cannot simultaneously obtain code details and determine the code's location within the overall program when dealing with large codebases, resulting in poor question-answering performance.
By locating the first code block node corresponding to the initial code fragment and all its ancestor nodes from the abstract syntax tree of the source code file, code structure information is extracted, target code fragments are generated, and they are concatenated into a target context input question answering model.
It improves the question-answering model's understanding of code context, preserves necessary code details and structural definitions, and significantly enhances question-answering performance.
Smart Images

Figure CN121303363B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, specifically to a model question-answering method and apparatus based on generating context from grammatical structures. Background Technology
[0002] When using Large Language Models (LLMs) to process large codebases, providing the entire codebase directly to the model is impractical due to the limited context window size of LLMs. Therefore, code selection and abstraction are necessary. However, previous methods have significant shortcomings: either providing continuous raw code snippets, allowing the model to see details but not their position within the overall program; or providing summaries or generalizations of the code, enabling the model to understand the global location but losing specific implementation details. This results in models either lacking key implementation details or failing to understand the role of code snippets within the overall architecture, leading to poor question-answering performance. Summary of the Invention
[0003] In view of this, this application proposes a model question answering method and apparatus based on generating context from syntactic structure, in order to solve the problem that existing question answering models cannot simultaneously obtain code details and determine the position of the code in the overall program, resulting in poor model question answering performance.
[0004] The first aspect of this application proposes a model question-answering method based on generating context from grammatical structure, the method comprising:
[0005] In response to the user's problem information, multiple initial code snippets related to the problem information are selected from the target code library;
[0006] For any one of the plurality of initial code fragments, obtain the source code file containing the initial code fragment;
[0007] Locate the first code block node corresponding to the initial code fragment from the abstract syntax tree of the source code file;
[0008] Locate all ancestor nodes of the first code block node from the abstract syntax tree, and extract the code structure information of each ancestor node;
[0009] Generate a target code fragment based on the code structure information of the first code block node and all ancestor nodes;
[0010] The target context is obtained by concatenating multiple target code fragments that correspond one-to-one with the multiple initial code fragments;
[0011] The target context is input into the question-answering model so that the question-answering model outputs answer information based on the target context.
[0012] This application embodiment locates the first code block node corresponding to the initial code fragment from the abstract syntax tree of the source code file, and locates all ancestor nodes of the first code block node from the abstract syntax tree. Based on the code structure information of the first code block node and all ancestor nodes, the target code fragment is generated. This enables effective extraction and organization of the code context, so that the extracted code context not only retains the necessary code details, but also retains the structural definition part to provide structural clues (classes, function signatures, and hierarchical indentation, etc.), thereby greatly improving the question answering effect of the model.
[0013] In this embodiment of the application, locating the first code block node corresponding to the initial code fragment from the abstract syntax tree of the source code file includes:
[0014] Locate the leaf node corresponding to the initial code fragment from the abstract syntax tree;
[0015] Tracing back upwards from the leaf node, the first code block node that is furthest from the leaf node and whose code length is not greater than a preset length threshold is selected.
[0016] In this embodiment, the ancestor node includes a definition node and a control node; the code structure information of each ancestor node is extracted, including:
[0017] For any ancestor node, if the ancestor node is a definition node, then the first code structure information of the definition node is extracted; the first code structure information includes the signature information of the definition node; the definition node includes class definition nodes and method definition nodes;
[0018] If the ancestor node is a control node, then the second code structure information of the control node is extracted; the second code structure information includes the condition information of the control node; the control node includes loop control nodes and branch control nodes.
[0019] In this embodiment of the application, generating a target code fragment based on the code structure information of the first code block node and all ancestor nodes includes:
[0020] Locate the second code block node from the abstract syntax tree that shares the same ancestor node as the first code block node;
[0021] Extract the code structure information of the second code block node;
[0022] The target code fragment is obtained by concatenating the code snippet of the first code block node, the code structure information of the second code block node, and the code structure information of all ancestor nodes.
[0023] In this embodiment of the application, the target code segment is obtained by concatenating the code fragment of the first code block node, the code structure information of the second code block node, and the code structure information of all ancestor nodes, including:
[0024] Determine the file path information of the source code file containing the initial code fragment;
[0025] The code concatenation result is obtained by concatenating the code structure information of the initial code fragment, the second code block node, and the code structure information of all ancestor nodes.
[0026] The target code fragment is generated using the file path information and the code concatenation result.
[0027] In this embodiment of the application, each target code segment contains the filename information of the corresponding source code file; multiple target code segments that correspond one-to-one with the multiple initial code segments are concatenated to obtain a target context, including:
[0028] For any target code segment, the sorting position of the target code segment is determined based on the file path information contained in the target code segment and a preset sorting relationship; the preset sorting relationship includes multiple file path information and a sorting position corresponding to each file path information.
[0029] The target context is obtained by concatenating the multiple target code fragments according to their respective order.
[0030] In this embodiment of the application, after obtaining the source code file containing the initial code fragment, the method further includes:
[0031] An abstract syntax tree is constructed based on the source code file; each node in the abstract syntax tree represents a structural element in the source code file, and the hierarchical relationship between nodes is used to characterize the syntactic nesting structure of the source code; the structural elements include class declarations, function definitions, control statements, and expressions.
[0032] An embodiment of the second aspect of this application provides a model question-answering device based on grammatical structure-generated context, comprising:
[0033] The initial code snippet filtering module is used to filter out multiple initial code snippets related to the problem information from the target code library in response to the problem information from the user's terminal.
[0034] The source code file acquisition module is used to acquire a source code file containing any one of the plurality of initial code fragments.
[0035] The first code block node localization module is used to locate the first code block node corresponding to the initial code fragment from the abstract syntax tree of the source code file;
[0036] Ancestor node location module, used to locate all ancestor nodes of the first code block node from the abstract syntax tree, and extract the code structure information of each ancestor node;
[0037] The target code fragment generation module is used to generate a target code fragment based on the code structure information of the first code block node and all ancestor nodes.
[0038] The target context generation module is used to concatenate multiple target code segments that correspond one-to-one with the multiple initial code segments to obtain a target context;
[0039] The model question answering module is used to input the target context into the question answering model so that the question answering model can output answer information based on the target context.
[0040] An embodiment of the third aspect of this application provides a computer device including a memory and a processor, the memory and the processor being communicatively connected to each other, the memory storing computer instructions, and the processor executing the computer instructions to perform the model question-answering method based on syntax structure to generate context as described in the first aspect.
[0041] An embodiment of the fourth aspect of this application provides a computer-readable storage medium storing computer instructions for causing a computer to execute the model question-answering method based on syntax structure to generate context as described in the first aspect above.
[0042] Additional aspects and advantages of this application will be set forth in part in the description which follows, and in part will be obvious from the description, or may be learned by practice of this application. Attached Figure Description
[0043] Various other advantages and benefits will become apparent to those skilled in the art upon reading the following detailed description of preferred embodiments. The accompanying drawings are for illustrative purposes only and are not intended to limit the scope of this application. Furthermore, the same reference numerals denote the same parts throughout the drawings. In the drawings:
[0044] Figure 1The diagram illustrates a flowchart of a model question-answering method based on grammatical structure to generate context, according to an embodiment of this application.
[0045] Figure 2 A schematic diagram of an object code segment provided in an embodiment of this application is shown;
[0046] Figure 3 This illustration shows a schematic diagram of the structure of a model question-answering device based on grammatical structure to generate context, according to an embodiment of this application.
[0047] Figure 4 This illustration shows a schematic diagram of the structure of a computer device according to an embodiment of this application;
[0048] Figure 5 A schematic diagram of a storage medium provided in one embodiment of this application is shown. Detailed Implementation
[0049] Exemplary embodiments of this application will now be described in more detail with reference to the accompanying drawings. While exemplary embodiments of this application are shown in the drawings, it should be understood that this application may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided to enable a more thorough understanding of this application and to fully convey the scope of this application to those skilled in the art.
[0050] It should be noted that, unless otherwise stated, the technical or scientific terms used in this application shall have the ordinary meaning as understood by one of ordinary skill in the art to which this application pertains.
[0051] The technical scenarios involved in the embodiments of this application are described below.
[0052] As large language models are increasingly used in tasks such as code generation, completion, review, and transfer, constructing appropriate "context" for the model has become a performance bottleneck. One major challenge is the limited context window. Common methods for constructing program code as context include: 1. Treating consecutive lines of the original code as code segments and independently recalling these segments; 2. Summarizing and generalizing the original code and recalling these summaries and generalizations. These two methods struggle to simultaneously allow the model to understand both the code details and the code's position within the overall program. This invention aims to provide a context construction mechanism based on program syntax structure and code abstraction. While preserving code details at specific locations, it provides information about the code's position on the syntax tree, allowing the question-answering model to refer to both the details and this positional information when generating the answer information corresponding to the question.
[0053] According to an embodiment of this application, a model question-answering method based on grammatical structure to generate context is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.
[0054] This embodiment provides a model question-answering method based on generating context from grammatical structure. Figure 1 This is a flowchart of a model question-answering method based on syntax structure to generate context according to an embodiment of this application, such as... Figure 1 As shown, the process includes the following steps:
[0055] Step S101: In response to the problem information from the user terminal, select multiple initial code fragments related to the problem information from the target code library.
[0056] Specifically, the aforementioned initial code snippets are the code snippets that the question-answering model needs to focus on. These code snippets are strongly related to the current question or task, such as lines that may cause errors or function snippets that need to generate code.
[0057] Step S102: For any one of the plurality of initial code segments, obtain the source code file containing the initial code segment.
[0058] Specifically, the target code library contains multiple source code files, and multiple source code files that correspond one-to-one with multiple initial code snippets are selected from the target code library.
[0059] In some specific embodiments, after obtaining multiple source code files, the method further includes: sorting the multiple source code files according to the file paths in the target code library, and sequentially performing the following steps S103-S105 on the sorted multiple source code files to generate a target code fragment; the target code fragment retains the code details at a specific location (i.e., the first code block node corresponding to the initial code fragment), and also provides information on its position on the syntax tree (locating all ancestor nodes of the first code block node from the abstract syntax tree and extracting the code structure information of each ancestor node), which can greatly improve the question answering performance of the model.
[0060] In some specific embodiments, after step S102, the method further includes: constructing an abstract syntax tree based on the source code file. Each node in the abstract syntax tree represents a structural element in the source code file, and the hierarchical tree relationship between nodes is used to characterize the syntactic nesting structure of the source code; the structural elements include class declarations, function definitions, control statements, and expressions.
[0061] In this embodiment, the source code in each source code file is parsed to obtain an Abstract Syntax Tree (AST). A syntax tree is a structured representation of code, where each node represents a structural element in the code (e.g., class declaration, function definition, control statement, expression, etc.), and the nodes present a hierarchical tree-like relationship. This step can be completed using existing parser tools (such as Tree-sitter), with the aim of accurately identifying the code structure subsequently.
[0062] Step S103: Locate the first code block node corresponding to the initial code fragment from the abstract syntax tree of the source code file.
[0063] The first code block node is used to represent the first code block node that is farthest from the leaf node and whose code length is not greater than a preset length threshold, for example... Figure 2 As shown: When the leaf node corresponding to the initial code fragment is "x+=n", the first code block node corresponding to that leaf node has the following structure:
[0064] void foo(int n) {
[0065] int x = 1;
[0066] x += n; / / Focus on the line of code details, keep the full details
[0067] System.out.println(x);
[0068] }
[0069] In some specific embodiments, step S103 above includes steps S1031-S1032:
[0070] Step S1031: Locate the leaf node corresponding to the initial code fragment from the abstract syntax tree.
[0071] Specifically, in an abstract syntax tree, a leaf node is a node that no longer has any child nodes; that is, a leaf node does not have a field that points to the next level node.
[0072] Step S1032: Backtrack upwards from the leaf node to select the first code block node that is furthest from the leaf node and whose code length is not greater than a preset length threshold.
[0073] Specifically, the preset length threshold can be set according to the actual situation, and no specific limitation is made here. This preset length threshold is used to limit the size of the selected code block to avoid the block exceeding the model's context window due to being too large. Taking the above example: when the initial code fragment is "x+=n", when searching upwards, the first code block node that is farthest from the leaf node and whose length does not exceed the preset length threshold can be found.
[0074] This embodiment of the application, by locating leaf nodes and determining the first code block node containing the initial code fragment based on a preset length threshold, ensures that the selected code block both contains as much of the context as possible for the code of interest and is not too large. For each such code block node found, its complete source code text is preserved. In other words, the small code segment containing the line of interest will be fully presented in the context.
[0075] Step S104: Locate all ancestor nodes of the first code block node from the abstract syntax tree, and extract the code structure information of each ancestor node.
[0076] Specifically, ancestor nodes include, but are not limited to, defining nodes and control nodes.
[0077] More specifically, for each first code block node, this step requires looking further up in the syntax tree to see all its ancestor nodes (for example, the block might belong to a function definition node, which in turn belongs to a class definition node, etc.). For these ancestor nodes, their entire code content is not retained, but their structural definitions are preserved to provide clues to their hierarchical position.
[0078] In some specific embodiments, step S104 includes steps S1041-S1042:
[0079] Step S1041: For any ancestor node, if the ancestor node is a definition node, extract the first code structure information of the definition node. The first code structure information includes the signature information of the definition node; the definition node includes class definition nodes and method definition nodes.
[0080] Specifically, if the ancestor node is a signed definition node such as a class, method, or function, then its signature is retained. The signature refers to the definition header, such as the class name or function name and parameter list. For example... Figure 2 As shown: The first-line signature information, such as "classMyClass {", is retained. Through this information, the model can determine the specific location of the current code block, i.e., which class and function it belongs to.
[0081] Step S1042: If the ancestor node is a control node, then extract the second code structure information of the control node. The second code structure information includes the condition information of the control node; the control node includes loop control nodes and branch control nodes.
[0082] Specifically, if the ancestor node is a control structure such as a loop or branch, it generally starts with a condition or keyword and contains a statement block. The outer part of these structures is preserved, such as the line "if (condition) {" or "for (...) {", without expanding the internal specific statements.
[0083] In some specific implementations, for other types of ancestor structures (such as general code blocks or structures that are syntactically only one line), the first line of content is retained to indicate the start of the structure.
[0084] In this embodiment, to ensure the integrity of the code structure, wherever a start symbol (such as "{", "(", etc.) is retained, the corresponding end symbol (such as a matching "}" or ")" is also retained. This ensures that the output code snippets appear in pairs in terms of bracket pairing, preventing confusion for the reader (model or human).
[0085] Step S105: Generate a target code fragment based on the code structure information of the first code block node and all ancestor nodes.
[0086] In some specific embodiments, step S105 above includes steps S1051-S1053:
[0087] Step S1051: Locate the second code block node in the abstract syntax tree that belongs to the same ancestor node as the first code block node.
[0088] Specifically, such as Figure 2 As shown in the specific example: when the first code block node is "void foo(int n){...}", the second code block nodes that belong to the same ancestor node (i.e. "class MyClass{") include "void bar(Strings){...}" and "void baz(){...}".
[0089] Step S1052: Extract the code structure information of the second code block node.
[0090] Specifically, when extracting the code structure information of each second code block node, only its signature is retained, and the subject is omitted.
[0091] In some specific embodiments, for the first code block node mentioned above, there may be other sibling nodes at the same level (i.e., the second code block node mentioned above). If these sibling nodes are also independently identifiable structures such as classes, functions, and methods, their existence can be mentioned in the context, but their content is not elaborated; only their signatures are retained, and their bodies are omitted using ellipses. For example, if the block of interest is within a method of a class, other methods in that class will retain their method names and parameters (or brief signatures) in the form of "void otherMethod(...) { ...}", with "..." within curly braces indicating that implementation details are omitted. In this way, the model can know which other methods are defined in the same class without having to read the specific implementation. Sibling nodes that are not part of the main structure or irrelevant content can be directly omitted.
[0092] Step S1053: The code fragment of the first code block node, the code structure information of the second code block node, and the code structure information of all ancestor nodes are concatenated to obtain the target code fragment.
[0093] In some specific embodiments, step S1053 above includes steps a1-a3:
[0094] Step a1: Determine the file path information of the source code file containing the initial code fragment.
[0095] Step a2: Obtain the code concatenation result by concatenating the code structure information of the initial code fragment, the second code block node, and the code structure information of all ancestor nodes;
[0096] Step a3: Generate the target code fragment using the file path information and the code concatenation result.
[0097] For example, the steps a1-a3 above will be explained:
[0098] First, determine the file path information of the source code file containing the initial code snippet (i.e., "x += n") as "MyClass.java";
[0099] Next, obtain the code snippet corresponding to the first code block node of the initial code snippet, for example, the following:
[0100] void foo(int n) {
[0101] int x = 1;
[0102] x += n; / / Focus on the line of code details, keep the full details
[0103] System.out.println(x);
[0104] }
[0105] Finally, after obtaining the code fragment of the first code block node, it is concatenated with the code structure information of the second code block node (e.g., "void bar(String s){...}" and "void baz(){...}") and the code structure information of the ancestor node (e.g., "class MyClass{") to obtain the target code fragment (e.g. Figure 2 (As shown).
[0106] In the above Figure 2 In the example, `MyClass.java` is the file path, `class MyClass { ...}` indicates that the class definition is preserved, the `foo(int n)` method fully presents the key implementation details (`x += n;` and context), while the `bar` and `baz` methods only retain their signatures and use `...` to indicate that their content is not expanded, and other irrelevant content in the class is also replaced with `...`. Thus, the model can see what happens inside the `foo` method, know that this code is located in the `MyClass` class, and that there are other methods that exist but are not detailed. This method of providing context has high information density and a clear hierarchy.
[0107] Step S106: Concatenate multiple target code segments that correspond one-to-one with the multiple initial code segments to obtain the target context.
[0108] Specifically, each target code snippet contains the filename information of the corresponding source code file, for example... Figure 2 The "MyClass.java" file in the file.
[0109] In some specific embodiments, step S106 above includes steps S1061-S1062:
[0110] Step S1061: For any target code segment, determine the sorting position of the target code segment based on the file path information contained in the target code segment and the preset sorting relationship.
[0111] Specifically, the preset sorting relationship includes multiple file path information and the sorting position corresponding to each file path information. The specific location of the target code fragment within the target context can be determined by the file path information contained within the target code fragment.
[0112] Step S1062: The multiple target code fragments are concatenated according to their respective sorting order to obtain the target context.
[0113] Specifically, the target context is obtained by splicing and merging multiple target code fragments according to the specific location of each target code fragment in the target context.
[0114] In the embodiments of this application, the above steps can convert each source code file into a concise context fragment that contains key code and a structural framework, thereby helping to improve the question-answering performance of the model.
[0115] Step S107: Input the target context into the question-answering model so that the question-answering model outputs answer information based on the target context.
[0116] In this embodiment, the target context can be provided to the question-answering model for subsequent tasks. In this way, the model input includes detailed information about the code requiring attention, as well as its organizational structure within the project (filename, class, and function, etc.). When reading the context, the model can simultaneously refer to both details and structure: it can access key lines of source code and understand the global location and module relationships through class names / function names, thereby gaining a more accurate understanding of the question.
[0117] The embodiments of this application have the following technical effects:
[0118] This application's technical solution, starting from the code's syntactic structure, cleverly achieves effective extraction and organization of the code context even with a limited context window. Compared to existing solutions, this method preserves necessary code details (using syntax trees to locate relevant blocks for complete presentation), while providing structural clues (classes, function signatures, and hierarchical indentation, etc.), and avoids redundant content occupying space through ellipses. This method, as a low-level tool, supports various high-level code intelligence applications. Verification shows it significantly improves LLM's understanding and processing of complex codebases, solving the problem of existing technologies struggling to balance details and the overall picture, and possesses high practical value.
[0119] Corresponding to the above implementation of the model question answering method based on generating context from grammatical structure, this application embodiment also provides a model question answering device based on generating context from grammatical structure, used to execute the model question answering method based on generating context from grammatical structure described in the above embodiments. Figure 3 As shown, this model question-answering device, which generates context based on grammatical structure, includes:
[0120] The initial code snippet filtering module is used to filter out multiple initial code snippets related to the problem information from the target code library in response to the problem information from the user's terminal.
[0121] The source code file acquisition module is used to acquire a source code file containing any one of the plurality of initial code fragments.
[0122] The first code block node localization module is used to locate the first code block node corresponding to the initial code fragment from the abstract syntax tree of the source code file;
[0123] Ancestor node location module, used to locate all ancestor nodes of the first code block node from the abstract syntax tree, and extract the code structure information of each ancestor node;
[0124] The target code fragment generation module is used to generate a target code fragment based on the code structure information of the first code block node and all ancestor nodes.
[0125] The target context generation module is used to concatenate multiple target code segments that correspond one-to-one with the multiple initial code segments to obtain a target context;
[0126] The model question answering module is used to input the target context into the question answering model so that the question answering model can output answer information based on the target context.
[0127] Optionally, the first code block node positioning module is further configured to locate the leaf node corresponding to the initial code fragment from the abstract syntax tree; and backtrack upwards from the leaf node to select the first code block node that is farthest from the leaf node and whose code length is not greater than a preset length threshold.
[0128] Optionally, the ancestor node location module is further configured to, for any ancestor node, if the ancestor node is a definition node, extract the first code structure information of the definition node; the first code structure information includes the signature information of the definition node; the definition node includes class definition nodes and method definition nodes; if the ancestor node is a control node, extract the second code structure information of the control node; the second code structure information includes the condition information of the control node; the control node includes loop control nodes and branch control nodes.
[0129] Optionally, the target code fragment generation module is further configured to locate a second code block node that belongs to the same ancestor node as the first code block node from the abstract syntax tree; extract the code structure information of the second code block node; and concatenate the code fragment of the first code block node, the code structure information of the second code block node, and the code structure information of all ancestor nodes to obtain the target code fragment.
[0130] Optionally, the target code fragment generation module is further configured to determine the file path information of the source code file containing the initial code fragment; obtain a code concatenation result by concatenating the initial code fragment, the code structure information of the second code block node, and the code structure information of all ancestor nodes; and generate the target code fragment using the file path information and the code concatenation result.
[0131] Optionally, the target context generation module is further configured to, for any target code segment, determine the sorting position of the target code segment based on the file path information contained within the target code segment and a preset sorting relationship; the preset sorting relationship includes multiple file path information and a sorting position corresponding to each file path information; and concatenate the multiple target code segments according to their respective sorting positions to obtain the target context.
[0132] Optionally, the apparatus further includes: an abstract syntax tree construction module, used to construct an abstract syntax tree based on the source code file; each node in the abstract syntax tree represents each structural element in the source code file, and the tree-like hierarchical relationship between nodes is used to characterize the syntactic nesting structure of the source code; the structural elements include class declarations, function definitions, control statements, and expressions.
[0133] The model question answering device based on syntax structure to generate context provided in the above embodiments of this application and the model question answering method based on syntax structure to generate context provided in the embodiments of this application are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by the applications they store.
[0134] This application also provides a computer device for executing the above-described model question-answering method based on syntactic structure-based context generation. Please refer to... Figure 4 This illustrates a schematic diagram of a computer device provided by some embodiments of this application. For example... Figure 4 As shown, the computer device 4 includes a processor 400, a memory 401, a bus 402, and a communication interface 403. The processor 400, the communication interface 403, and the memory 401 are connected via the bus 402. The memory 401 stores a computer program that can run on the processor 400. When the processor 400 runs the computer program, it executes the model question-answering method based on syntax structure to generate context provided in the foregoing embodiments of this application.
[0135] The memory 401 may include high-speed random access memory (RAM) or non-volatile memory, such as at least one disk storage device. Communication between this system network element and at least one other network element is achieved through at least one communication interface 403 (which can be wired or wireless), such as the Internet, wide area network, local area network, or metropolitan area network.
[0136] Bus 402 can be an ISA bus, PCI bus, or EISA bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. Memory 401 is used to store programs. After receiving an execution instruction, processor 400 executes the program. The model question-answering method based on syntax structure to generate context disclosed in the foregoing embodiments can be applied to processor 400, or implemented by processor 400.
[0137] The processor 400 may be an integrated circuit chip with signal processing capabilities. In implementation, each step of the above method can be completed by the integrated logic circuitry in the hardware of the processor 400 or by instructions in software form. The processor 400 may be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it may also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), an off-the-shelf programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. It can implement or execute the methods, steps, and logic block diagrams disclosed in the embodiments of this application. The general-purpose processor may be a microprocessor or any conventional processor. The steps of the methods disclosed in the embodiments of this application can be directly embodied in the execution of a hardware decoding processor, or executed by a combination of hardware and software modules in the decoding processor. The software modules may reside in random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, or other mature storage media in the art. The storage medium is located in memory 401. The processor 400 reads the information in memory 401 and, in conjunction with its hardware, completes the steps of the above method.
[0138] The computer device provided in this application embodiment and the model question answering method based on syntax structure to generate context provided in this application embodiment are based on the same inventive concept and have the same beneficial effects as the methods they adopt, run or implement.
[0139] This application also provides a computer-readable storage medium corresponding to the model question-answering method based on syntax structure to generate context provided in the foregoing embodiments. Please refer to [link / reference]. Figure 5 The computer-readable storage medium shown is an optical disc 30, on which a computer program (i.e., a program product) is stored. When the computer program is run by a processor, it executes the model question-answering method based on the syntax structure to generate context provided in any of the foregoing embodiments.
[0140] It should be noted that examples of the computer-readable storage medium may also include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other optical and magnetic storage media, which will not be elaborated here.
[0141] The computer-readable storage medium provided in the above embodiments of this application and the model question-answering method based on syntax structure to generate context provided in the embodiments of this application are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by the applications stored therein.
[0142] It should be noted that:
[0143] Numerous specific details are set forth in the specification provided herein. However, it will be understood that embodiments of this application may be practiced without these specific details. In some instances, well-known structures and techniques have not been shown in detail so as not to obscure the understanding of this specification.
[0144] Similarly, it should be understood that, for the sake of brevity and to aid in understanding one or more of the various inventive aspects, in the above description of exemplary embodiments of this application, various features of this application are sometimes grouped together in a single embodiment, figure, or description thereof. However, this disclosure should not be construed as reflecting a schematic diagram in which the claimed application requires more features than expressly recited in each claim. Rather, as reflected in the following claims, inventive aspects lie in fewer than all features of a single foregoing disclosed embodiment. Therefore, the claims following the detailed description are hereby expressly incorporated into that detailed description, wherein each claim itself is a separate embodiment of this application.
[0145] Furthermore, those skilled in the art will understand that although some embodiments described herein include certain features but not others included in other embodiments, combinations of features from different embodiments are intended to be within the scope of this application and form different embodiments. For example, in the following claims, any of the claimed embodiments can be used in any combination.
[0146] The above description is merely a preferred embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A model question and answer method based on generating context according to syntax structure, characterized in that, The method comprises: in response to the question information of the user terminal, screening a plurality of initial code segments related to the question information from the target code library; for any initial code segment in the plurality of initial code segments, obtaining a source code file containing the initial code segment; locating a first code block node corresponding to the initial code segment from an abstract syntax tree of the source code file; locating all ancestor nodes of the first code block node from the abstract syntax tree and extracting code structure information of each ancestor node; generating a target code segment according to the first code block node and the code structure information of all the ancestor nodes; splicing a plurality of target code segments corresponding one by one to the plurality of initial code segments to obtain a target context; inputting the target context into the question and answer model, so that the question and answer model outputs answer information according to the target context; locating a first code block node corresponding to the initial code segment from an abstract syntax tree of the source code file, comprising: locating a leaf node corresponding to the initial code segment from the abstract syntax tree; backtracking from the leaf node upwards to screen a first code block node farthest from the leaf node and having a code length not greater than a preset length threshold; generating a target code segment according to the first code block node and the code structure information of all the ancestor nodes, comprising: locating a second code block node belonging to the same ancestor node as the first code block node from the abstract syntax tree; extracting code structure information of the second code block node; splicing the code segment of the first code block node, the code structure information of the second code block node and the code structure information of all the ancestor nodes to obtain the target code segment.
2. The method of claim 1, wherein, The ancestor nodes include definition nodes and control nodes; extracting the code structure information of each ancestor node, comprising: for any ancestor node, if the ancestor node is a definition node, extracting first code structure information of the definition node; the first code structure information contains signature information of the definition node; the definition node includes class definition nodes and method definition nodes; if the ancestor node is a control node, extracting second code structure information of the control node; the second code structure information contains condition information of the control node; the control node includes loop control nodes and branch control nodes.
3. The method of claim 1, wherein, splicing the code segment of the first code block node, the code structure information of the second code block node and the code structure information of all the ancestor nodes to obtain the target code segment, comprising: determining file path information of the source code file containing the initial code segment; splicing the initial code segment, the code structure information of the second code block node and the code structure information of all the ancestor nodes to obtain a code splicing result; generating the target code segment through the file path information and the code splicing result.
4. The method of claim 3, wherein, Each target code snippet contains file name information of a corresponding source code file; a plurality of target code snippets corresponding to the plurality of initial code snippets are spliced to obtain a target context, including: For any target code snippet, the sorting bit of the target code snippet is determined according to the file path information contained in the target code snippet and a preset sorting relationship; the preset sorting relationship includes a plurality of file path information and a sorting bit corresponding to each file path information; Splice the plurality of target code snippets according to their respective sorting bits to obtain the target context.
5. The method of claim 1, wherein, After obtaining the source code file containing the initial code snippet, the method further includes: An abstract syntax tree is constructed according to the source code file; each node in the abstract syntax tree represents each structural element in the source code file, and the tree hierarchical relationship between the nodes is used to represent the syntax nesting structure of the source code; the structural elements include class declaration, function definition, control statement and expression. 6.A model question and answer device generating a context based on a syntax structure, characterized by, The device includes: An initial code snippet screening module is configured to screen a plurality of initial code snippets related to the problem information from a target code library in response to the problem information of the user end; A source code file acquisition module is configured to acquire a source code file containing any initial code snippet in the plurality of initial code snippets; A first code block node positioning module is configured to locate a first code block node corresponding to the initial code snippet from an abstract syntax tree of the source code file; An ancestor node positioning module is configured to locate all ancestor nodes of the first code block node from the abstract syntax tree and extract code structure information of each ancestor node; A target code snippet generation module is configured to generate a target code snippet according to the first code block node and the code structure information of all the ancestor nodes; A target context generation module is configured to splice a plurality of target code snippets corresponding to the plurality of initial code snippets to obtain a target context; A model question and answer module is configured to input the target context into a question and answer model, so that the question and answer model outputs answer information according to the target context; Locating a first code block node corresponding to the initial code snippet from an abstract syntax tree of the source code file includes: Locating a leaf node corresponding to the initial code snippet from the abstract syntax tree; Backtracking from the leaf node to filter out a first code block node farthest from the leaf node and having a code length not greater than a preset length threshold; Generating a target code snippet according to the first code block node and the code structure information of all the ancestor nodes includes: Locating a second code block node belonging to the same ancestor node as the first code block node from the abstract syntax tree; Extracting code structure information of the second code block node; Splicing the code snippet of the first code block node, the code structure information of the second code block node and the code structure information of all the ancestor nodes to obtain the target code snippet.
7. A computer device, comprising: It includes: A memory and a processor, which are connected in communication with each other, the memory storing computer instructions, and the processor executing the computer instructions to perform the model question answering method based on the syntax structure to generate the context according to any one of claims 1 to 5.
8. A computer-readable storage medium, characterized in that, The computer readable storage medium stores computer instructions for causing a computer to perform the model question answering method based on the syntax structure to generate the context according to any one of claims 1 to 5.
Citation Information
Patent Citations
Data processing method and device, equipment, storage medium and computer program product
CN113971028A
Modeling method, device and equipment for prompt information, storage medium and program product
CN121008851A