Code retrieval method and device based on natural language, equipment and storage medium

The code block sequence of the code retrieval system is optimized by double sorting, which solves the problems of incorrect code block sorting and missing business logic in the existing system, and improves the precision of code retrieval and the accuracy of responses.

CN120632065APending Publication Date: 2025-09-12KE COM (BEIJING) TECHNOLOGY CO LTD
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202510716762.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-29
Publication Date
2025-09-12

AI Technical Summary

Technical Problem

Existing natural language-based code retrieval systems may arrange more relevant code blocks after less relevant code blocks in the retrieval results, resulting in lower accuracy of the retrieval results. At the same time, some business logic is deleted during the index construction phase, making it impossible to provide accurate answers.

Method used

A double sorting method is used to reorder the initial code block sequence. First, the initial code block sequence is reordered using the reordering model or TF-IDF algorithm to generate the first reordered code block sequence. Then, candidate code blocks are obtained based on the original code text segmentation and reordered in a second way using different methods to generate the second reordered code block sequence, and finally the retrieval results are generated.

Benefits of technology

It improves the precision and accuracy of code retrieval, ensures that code blocks related to problem information are ranked first, avoids retrieval inaccuracies caused by deleting business logic during the indexing phase, and improves the accuracy of responses to complex logical problems.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120632065A_ABST
    Figure CN120632065A_ABST
Patent Text Reader

Abstract

The invention provides a code retrieval method and device based on a natural language, equipment and a storage medium, after an initial code block sequence is retrieved based on question information input by a user, the initial code block sequence is reordered by using different reordering modes, the probability of code block ordering errors is reduced, and the user experience is improved. According to the method, code blocks with high correlation with problem information can be ranked in front of code blocks with low correlation, the retrieval precision is improved, and after the first rearranged code block sequence is obtained, other code blocks in the original code text to which each code block belongs are further obtained and are reranked together with the first rearranged code block sequence, so that the retrieval efficiency is improved. The complete code text corresponding to the code block related to the problem information participates in code retrieval, and when code retrieval and reply are carried out on the problem related to the specific business logic of the code, the situation that retrieval is inaccurate due to the fact that the business logic in the original code text is deleted in the index establishment stage can be avoided; therefore, the question answering accuracy is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to artificial intelligence technology and natural language processing technology, and in particular to a natural language-based code retrieval method, apparatus, device, and storage medium. Background Art

[0002] During the software development process, developers often need to deal with existing code, such as taking over code left by others. Since this type of code usually lacks documentation and code files are usually named based on personal habits, it is difficult for other developers to understand the logic of the code. Therefore, using natural language for code retrieval and question-and-answer becomes particularly important, which can help developers quickly find the required information in a chaotic code environment.

[0003] Current natural language-based code retrieval systems typically convert user-entered natural language questions into vectors, calculate the similarity between these vectors and code blocks in the codebase, and thus retrieve and sort the code blocks. However, in practice, these retrieval methods may prioritize more relevant code blocks over less relevant ones, resulting in lower accuracy in retrieval results. Summary of the Invention

[0004] The embodiments of the present disclosure provide a natural language-based code retrieval method, apparatus, device, and storage medium, which can improve the code retrieval precision and the accuracy of the retrieval results.

[0005] One aspect of an embodiment of the present disclosure provides a natural language-based code retrieval method, comprising:

[0006] In response to detecting a question input operation, obtaining question information input by the question input operation;

[0007] Perform code retrieval based on the problem information to obtain an initial code block sequence;

[0008] reordering the code blocks in the initial code block sequence using a first reordering method to obtain a first reordered code block sequence, and determining candidate code blocks corresponding to the first reordered code block sequence, the candidate code blocks comprising code blocks obtained by segmenting original code texts to which the code blocks in the first reordered code block sequence belong;

[0009] Reordering the candidate code blocks using a second reordering method to obtain a second reordered code block sequence;

[0010] A retrieval result corresponding to the question information is generated based on the second rearranged code block sequence.

[0011] Optionally, reordering the code blocks in the initial code block sequence using a first reordering method to obtain a first reordered code block sequence includes:

[0012] Reordering the code blocks in the initial code block sequence using the first reordering method, and generating the first reordered code block sequence based on n code blocks with the highest similarity, where n is a positive integer;

[0013] The step of reordering the candidate code blocks corresponding to the first reordered code block sequence using a second reordering method to obtain a second reordered code block sequence includes:

[0014] The candidate code blocks are reordered using the second reordering method, and the second reordered code block sequence is generated based on the m candidate code blocks with the highest similarity, where m is a positive integer.

[0015] Optionally, reordering the code blocks in the initial code block sequence by using the first reordering method, and generating the first reordered code block sequence based on the n code blocks with the highest similarity, includes:

[0016] Inputting the initial code block sequence and the question information into a reordering model, and calculating the similarity between each code block and the question information by the reordering model to obtain a first similarity corresponding to each code block;

[0017] The n code blocks with the highest first similarities are arranged in descending order of first similarities to obtain the first rearranged code block sequence.

[0018] Optionally, reordering the candidate code blocks using the second reordering method and generating the second reordered code block sequence based on the m candidate code blocks with the highest similarity includes:

[0019] Calculate the second similarity between each candidate code block and the question information using the inverse document frequency (TF-IDF) algorithm;

[0020] The m candidate code blocks with the highest second similarity are arranged in descending order of second similarity to obtain the second rearranged code block sequence.

[0021] Optionally, determining a candidate code block corresponding to the first rearranged code block sequence includes:

[0022] Obtaining the original code text corresponding to each code block in the first rearranged code block sequence from a code library;

[0023] Based on the start and end code lines corresponding to each method in the original code text, the original code text is segmented to obtain the candidate code blocks, each candidate code block corresponding to a method.

[0024] Optionally, performing code retrieval based on the problem information to obtain an initial code block sequence includes:

[0025] Optimizing the question information using a preset optimization method to obtain a search text, wherein the search text includes at least one of the optimized question information and a search keyword, and the preset optimization method includes at least one of keyword expansion, text error correction, and redundant information removal;

[0026] Performing vectorization processing on the search text to obtain a search vector;

[0027] Code retrieval is performed using the retrieval vector to obtain the initial code block sequence.

[0028] Another aspect of the present disclosure provides a natural language-based code search device, comprising:

[0029] An acquisition module, configured to acquire question information input by the question input operation in response to detecting the question input operation;

[0030] A retrieval module, configured to perform code retrieval based on the problem information to obtain an initial code block sequence;

[0031] a first reordering module, configured to reorder the code blocks in the initial code block sequence using a first reordering method to obtain a first reordered code block sequence, and determine candidate code blocks corresponding to the first reordered code block sequence, the candidate code blocks comprising code blocks obtained by segmenting original code texts to which the code blocks in the first reordered code block sequence belong;

[0032] A second reordering module, configured to reorder the candidate code blocks using a second reordering method to obtain a second reordered code block sequence;

[0033] A generating module is used to generate a search result corresponding to the question information based on the second rearranged code block sequence.

[0034] Another aspect of the present disclosure provides an electronic device, including:

[0035] memory for storing computer programs;

[0036] The processor is configured to execute the computer program stored in the memory, and when the computer program is executed, the method described in the above aspects is implemented.

[0037] Another aspect of the embodiments of the present disclosure provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, the method described in the above aspects is implemented.

[0038] Another aspect of the embodiments of the present disclosure provides a computer program, comprising computer program instructions, which implement the method described in the above aspects when executed by a processor.

[0039] Based on the embodiment of the present disclosure, after an initial code block sequence is retrieved based on the question information input by the user, the initial code block sequence is first reordered using a first reordering method to obtain a first reordered code block sequence, and then the candidate code blocks corresponding to the first reordered code block sequence are reordered using a second reordering method to obtain a second reordered code block sequence. A retrieval result is generated based on the second reordered code block sequence, that is, the initial code block sequence is reordered using different reordering methods, which reduces the probability of code block sorting errors and can place code blocks with high relevance to the question information before code blocks with low relevance, thereby improving retrieval accuracy. Moreover, after obtaining the first reordered code block sequence, candidate code blocks obtained by segmenting the original code text to which each code block belongs are further obtained and reordered together with the first reordered code block sequence, so that the complete code text corresponding to the code block related to the question information participates in the code retrieval. When performing code retrieval and answers for questions related to the specific business logic of the code, it is possible to avoid retrieval inaccuracies caused by deleting the business logic in the original code text during the indexing phase, thereby improving the accuracy of question answers.

[0040] The technical solution of the present disclosure is further described in detail below through the accompanying drawings and examples. BRIEF DESCRIPTION OF THE DRAWINGS

[0041] The accompanying drawings, which are incorporated in and constitute a part of the specification, illustrate embodiments of the present disclosure and, together with the description, serve to explain the principles of the present disclosure.

[0042] The present disclosure can be more clearly understood from the following detailed description with reference to the accompanying drawings, in which:

[0043] Figure 1 This is a flowchart of an embodiment of the natural language-based code retrieval method disclosed herein;

[0044] Figure 2 This is a flowchart of another embodiment of the natural language-based code retrieval method disclosed herein;

[0045] Figure 3 This is a flowchart of another embodiment of the natural language-based code retrieval method disclosed herein;

[0046] Figure 4This is a schematic structural diagram of an embodiment of a natural language-based code retrieval device disclosed herein;

[0047] Figure 5 The figure is a schematic structural diagram of an application embodiment of the electronic device disclosed herein. DETAILED DESCRIPTION

[0048] Various exemplary embodiments of the present disclosure will now be described in detail with reference to the accompanying drawings. It should be noted that unless otherwise specifically stated, the relative arrangement of components and steps, numerical expressions and numerical values ​​set forth in these embodiments do not limit the scope of the present disclosure.

[0049] Those skilled in the art will understand that the terms "first" and "second" in the embodiments of the present disclosure are only used to distinguish different steps, devices or modules, and do not represent any specific technical meanings, nor do they indicate a necessary logical order between them.

[0050] It should also be understood that in the embodiments of the present disclosure, “a plurality of” may refer to two or more than two, and “at least one” may refer to one, two, or more than two.

[0051] It should also be understood that any component, data or structure mentioned in the embodiments of the present disclosure can generally be understood as one or more, unless explicitly limited or otherwise indicated in the context.

[0052] In addition, the term "and / or" in this disclosure is merely a description of the association relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent three situations: A exists alone, A and B exist simultaneously, and B exists alone. In addition, the character " / " in this disclosure generally indicates that the related objects are in an "or" relationship.

[0053] It should also be understood that the description of the various embodiments in this disclosure focuses on the differences between the various embodiments, and the same or similar aspects thereof can be referenced with each other. For the sake of brevity, they will not be described one by one.

[0054] At the same time, it should be understood that for the convenience of description, the sizes of the various parts shown in the drawings are not drawn according to the actual proportional relationship.

[0055] The following description of at least one exemplary embodiment is merely illustrative in nature and is in no way intended to limit the present disclosure, its application, or uses.

[0056] Technologies, methods, and equipment known to ordinary technicians in the relevant art may not be discussed in detail, but where appropriate, the technologies, methods, and equipment should be considered part of the specification.

[0057] It should be noted that like reference numerals and letters refer to like items in the following figures, and therefore, once an item is defined in one figure, it need not be further discussed in subsequent figures.

[0058] Current natural language code retrieval systems typically employ the Retrieval-Augmented Generation (RAG) system architecture. RAG technology is a natural language processing (NLP) technique that combines information retrieval and generative models, aiming to improve the accuracy and richness of the results generated by generative models. By combining the generation task with the retrieval mechanism of an external knowledge base, the RAG model can dynamically retrieve relevant information from the external knowledge base and integrate the retrieved information into the generated results, thereby improving the accuracy and coverage of the generated results. The application of RAG technology primarily involves two phases: the retrieval phase and the generation phase. In the retrieval phase, given a user input (query), the retrieval module extracts information related to the user input from an external knowledge base (such as a search engine or a specific database). In the generation phase, the generative model combines the retrieved information, the user input, and the context to generate a corresponding answer. RAG technology converts natural language questions into vectors and calculates the similarity between the question vector and the code block vectors in the code base to recall code blocks. However, in practical code retrieval applications, RAG systems suffer from the following problems during the retrieval phase:

[0059] 1. Code block sorting issue: In some cases, the system will sort the code block that is actually most relevant to the user's input question behind the code blocks with less relevance, affecting the accuracy of the search results;

[0060] 2. Missing business logic: To prevent excessive business logic characters from interfering with vector matching, some business logic is often deleted during the code index construction phase. This results in the system being unable to provide accurate answers to questions related to specific methods and code logic details.

[0061] To address these issues, the present invention provides a natural language-based code retrieval method. This method can be applied to RAG systems to optimize the retrieval phase of the system. After the RAG system initially retrieves code blocks, the method provided by the present invention is used to reorder the retrieved code blocks and generate retrieval results. Figure 1 A flowchart of a natural language-based code retrieval method provided for an exemplary embodiment of the present disclosure. The natural language-based code retrieval method of the embodiment of the present disclosure can be implemented by an electronic device equipped with a natural language question-answering system.

[0062] like Figure 1 As shown, the method includes the following steps:

[0063] Step 101: In response to detecting a question input operation, obtain question information input by the question input operation.

[0064] In one possible implementation, a user's question input operation may be received through an intelligent question-answering dialog box in an application or webpage, and question information input by the question input operation may be obtained. The question information may include at least one of the following types: text information, image information, and video information.

[0065] Illustratively, a user inputs the text "What properties and methods are included in entity class A?" through the smart question and answer dialog box, and the text content can be determined as question information input for the question input operation.

[0066] Step 102: Perform code retrieval based on the problem information to obtain an initial code block sequence.

[0067] In one possible implementation, the question information may be vectorized to obtain a question vector, and the similarity between the question vector and the code vectors of each code block in the code library may be calculated respectively, and the relevant code blocks may be preliminarily recalled to obtain an initial code block sequence.

[0068] Optionally, code blocks are generated by segmenting the original code text according to a specific code structure. For example, considering that class declarations, variable declarations, and method definitions reflect the main structure of the code to be stored, the code blocks corresponding to the class declarations, variable declarations, and method definitions can be extracted. Each code block is converted to a vector, and the corresponding vector is used as an index and stored in the code library in association with the code block for system retrieval.

[0069] Optionally, the initial code block sequence is a code block sequence obtained by sorting the code blocks in the code library from high to low according to their similarity with the problem information. Code blocks in the code library with a similarity higher than a similarity threshold can be added to the initial code block sequence, or a preset number of code blocks with the highest similarity can be selected and added to the initial code block sequence.

[0070] Step 103 : reorder the code blocks in the initial code block sequence using a first reordering method to obtain a first reordered code block sequence, and determine candidate code blocks corresponding to the first reordered code block sequence.

[0071] Among them, the candidate code blocks include code blocks obtained by segmenting the original code texts to which each code block in the first rearranged code block sequence belongs, that is, the candidate code blocks include each code block in the first rearranged code block sequence, and also include other code blocks that belong to the same original code text as each code block in the first rearranged code block sequence.

[0072] Optionally, the first re-ranking method may include but is not limited to at least one of re-ranking using a re-ranking model and re-ranking using a Term Frequency-Inverse Document Frequency (TF-IDF) algorithm.

[0073] Optionally, a first reordering method may be used to calculate similarity between the code blocks in the initial code block sequence, and a first reordered code block sequence may be determined in descending order of similarity to the question information. The first reordered code block sequence may include all code blocks in the initial code block sequence, or may include a predetermined number of code blocks with the highest similarity.

[0074] Step 104: reorder the candidate code blocks using a second reordering method to obtain a second reordered code block sequence.

[0075] Optionally, the second reranking method may include but is not limited to at least one of reranking using a reranking model and reranking using a term frequency-inverse document frequency (TF-IDF) algorithm, and the second reranking method is different from the first reranking method.

[0076] Because when building a code base index, in order to avoid excessive business logic characters interfering with vector matching, some business logic is usually deleted during the code index construction phase. Therefore, when users need to query related questions involving specific methods and code logic, the system cannot provide accurate answers due to missing information. Therefore, in one possible implementation, after obtaining the first rearranged code block sequence, the candidate code blocks corresponding to the first rearranged code block sequence are further determined based on the original code text to which each code block in the first rearranged code block sequence belongs. The candidate code blocks include code blocks in the first rearranged code block sequence, as well as code blocks that belong to the same original code text as the code blocks in the first rearranged code block sequence (such as code blocks in the business logic part of the original code text). For example, a certain original code text can be roughly divided into code block a, code block b, and code block c, where code block a is the code block related to class declarations and method definitions, and code blocks b and code blocks c are code blocks related to business logic. When the original code text is added to the code library, in order to avoid excessive business logic characters interfering with vector matching, the index of the original code text is generated based only on code block a. During a code search, if the first rearranged code block sequence includes code block a, the original code text is code segmented to obtain code blocks a, code blocks b, and code blocks c. The candidate code blocks corresponding to the first rearranged code block sequence include code blocks a, code blocks b, and code blocks c. The candidate code blocks are used to participate in subsequent similarity calculations and reordering, so that accurate answers to such questions can be generated without constructing an index corresponding to the code in the business logic portion of the original code text.

[0077] Optionally, a second reordering method different from the first reordering method is used to reorder the candidate code blocks corresponding to the first reordered code block sequence to obtain a second reordered code block sequence. This can avoid the situation where a single reordering method causes some code blocks with higher relevance to be placed behind code blocks with lower relevance. That is, it can avoid code block sorting errors, ensure that the code blocks most relevant to the problem information are placed in the front, and improve the accuracy of natural language retrieval.

[0078] Optionally, the second reordered code block sequence can be determined by combining the reordering results of the first reordering method and the reordering results of the second reordering method. For example, the x code blocks with the highest similarity determined based on the first reordering method and the y candidate code blocks with the highest similarity determined based on the second reordering method can be determined as the second reordered code block sequence. For another example, the m (e.g., 5) code blocks with the highest similarity determined based on the first reordering method can be determined as the first reordered code block sequence. After obtaining candidate code blocks based on the first reordered code block sequence, the n (e.g., 7) candidate code blocks with the highest similarity determined based on the second reordering method can be determined as the second reordered code block sequence.

[0079] Step 105: Generate a search result corresponding to the question information based on the second rearranged code block sequence.

[0080] Optionally, a search result corresponding to the question information may be generated based on a preset number of candidate code blocks with the highest similarity in the second rearranged code block sequence, where the similarity is used to represent the degree of similarity between the candidate code blocks and the question information.

[0081] Schematically, the initial code block sequence includes 5 code blocks. After obtaining the original code text of the code block and performing code segmentation, a total of 25 candidate code blocks are obtained and reordered for the second time to obtain a second reordered code block sequence. A preset number (for example, 5) of candidate code blocks with the highest similarity can be determined as the code blocks most relevant to the question information, and a retrieval result corresponding to the question information is generated.

[0082] Based on the embodiment of the present disclosure, after an initial code block sequence is retrieved based on the question information input by the user, the initial code block sequence is first reordered using a first reordering method to obtain a first reordered code block sequence, and then the candidate code blocks corresponding to the first reordered code block sequence are reordered using a second reordering method to obtain a second reordered code block sequence. A retrieval result is generated based on the second reordered code block sequence, that is, the initial code block sequence is reordered using different reordering methods, which reduces the probability of code block sorting errors and can place code blocks with high relevance to the question information before code blocks with low relevance, thereby improving retrieval accuracy. Moreover, after obtaining the first reordered code block sequence, candidate code blocks obtained by segmenting the original code text to which each code block belongs are further obtained and reordered together with the first reordered code block sequence, so that the complete code text corresponding to the code block related to the question information participates in the code retrieval. When performing code retrieval and answers for questions related to the specific business logic of the code, it is possible to avoid retrieval inaccuracies caused by deleting the business logic in the original code text during the indexing phase, thereby improving the accuracy of question answers.

[0083] In one possible implementation, Figure 2 As shown, the above step 103 may specifically include the following step 201, and step 104 may specifically include the following step 202:

[0084] Step 201 : reorder code blocks in an initial code block sequence using a first reordering method, and generate a first reordered code block sequence based on n code blocks with the highest similarity.

[0085] Here, the value of n is a positive integer.

[0086] Among them, the first reordering method can calculate the similarity between each code block and the problem information (hereinafter referred to as the first similarity), and then sort the code blocks in the initial code block sequence according to a certain order (for example, the order of the first similarity from high to low), and output the reordered first reordered code block sequence.

[0087] In another possible implementation, the code blocks in the initial code block sequence may be reordered using at least two reordering methods. For example, the code blocks may be sorted based on a weighted average of the first similarities calculated using the at least two reordering methods, and a reordered first reordered code block sequence may be output.

[0088] Optionally, the first reordering method is to use a reordering model for reordering. Step 201 may specifically include the following steps:

[0089] In step 201a, the initial code block sequence and question information are input into a rerank model. The rerank model calculates the similarity between each code block and the question information to obtain a first similarity corresponding to each code block.

[0090] In principle, the reranking model can be implemented using dedicated reranking models such as Bocha Semantic Reranker, bge-reranker, and Cohere rerank, or it can be implemented using a large language model (LLM), such as the bidirectional encoder model (Bidirectional Encoder Representations from Transformers, BERT), the generative pre-trained transformer model (GPT), etc.

[0091] Step 201b: Arrange the n code blocks with the highest first similarity in descending order of first similarity to obtain a first rearranged code block sequence.

[0092] Optionally, the reordering model in step 201a can directly output a code block sequence sorted from high to low according to the first similarity, and then the first n (for example, the first 5) code blocks in the code block sequence are selected as the first reordered code block sequence. Alternatively, the reordering model in step 201a can output the first similarity corresponding to each code block, and then the n code blocks with the highest first similarity are selected and arranged in order of the first similarity from high to low to obtain the first reordered code block sequence.

[0093] Step 202: reorder the candidate code blocks using a second reordering method, and generate a second reordered code block sequence based on the m candidate code blocks with the highest similarity.

[0094] Here, the value of m is a positive integer.

[0095] The second reordering method may calculate the second similarity between each candidate code block and the question information, and then sort the candidate code blocks in a certain order (eg, order of second similarity from high to low), and output a reordered code block sequence.

[0096] In another possible implementation, the candidate code blocks corresponding to the first reordered code block sequence may be reordered using at least two reordering methods. For example, the candidate code blocks may be sorted based on a weighted average of the second similarities calculated using at least two reordering methods, and the reordered code block sequence may be output.

[0097] Optionally, the second reordering method is to use the TF-IDF algorithm for reordering. Step 203 may specifically include the following steps:

[0098] Step 203a: Calculate the second similarity between each candidate code block and the question information using the TF-IDF algorithm.

[0099] Optionally, the similarity between each candidate code block and the question information (hereinafter referred to as the second similarity) can be calculated using the Term Frequency-Inverse Document Frequency (TF-IDF) algorithm. TF-IDF is used to evaluate the importance of a word to a document set or one of the documents in a corpus. The importance of a word increases in direct proportion to the number of times it appears in the document, but at the same time decreases in inverse proportion to the frequency of its appearance in the corpus. The TF-IDF algorithm can be used to compare each candidate code block with the question information to obtain the second similarity between each candidate code block and the question information. Specifically, the question information can be split first to obtain the phrases contained in the question information. For each candidate code block, the term frequency-inverse document frequency of each phrase in the candidate code block is calculated, that is, the ratio of the number of times the phrase appears in the candidate code block to the total number of times the phrase appears in the code base. Then, the second similarity of each candidate code block is calculated. For example, the second similarity can be the sum of the term frequency-inverse document frequency of each phrase in the candidate code block.

[0100] Step 203b: Arrange the m candidate code blocks with the highest second similarity in descending order of the second similarity to obtain a second rearranged code block sequence.

[0101] Optionally, the candidate code blocks may be arranged in descending order according to the second similarity, and then the first m (eg, first 5) candidate code blocks in the sequence are selected as the second rearranged code block sequence.

[0102] Based on the embodiments of the present disclosure, by combining at least two methods, a reordering model and a TF-IDF algorithm, to reorder the retrieved code blocks, the probability of sorting errors can be reduced and the accuracy of the retrieval results can be improved. Furthermore, after reordering the initial code block sequence using a first reordering method, the n code blocks with the highest similarity are selected to generate a first reordered code block sequence, candidate code blocks corresponding to the first reordered code block sequence are obtained, and the candidate code blocks are reordered using a second reordering method. This can improve the accuracy of the retrieval results for problem information related to specific methods and code logic details, allowing the complete code text corresponding to the code blocks related to the problem information to participate in code retrieval, thereby making the retrieval system perform better when handling complex logic problems.

[0103] In a possible implementation, after obtaining the first rearranged code block sequence, candidate code blocks corresponding to the first rearranged code block sequence may be obtained.

[0104] After reordering the initial code block sequence using the first reordering method and screening out code blocks with high similarity to the problem information, considering that the retrieved code blocks are usually architectural information such as class declarations, variable declarations, and method definitions, but lack relevant code for specific business logic such as specific methods and functions, candidate code blocks corresponding to the first reordered code block sequence are further obtained to address issues related to specific methods and code logic details.

[0105] Optionally, determining the candidate code blocks corresponding to the first rearranged code block sequence may specifically include the following steps:

[0106] Step a: obtaining the original code text corresponding to each code block in the first rearranged code block sequence from the code library.

[0107] In one possible implementation, each code block in the code library is stored in association with its corresponding original code text or the storage address of the original code text, and the original code text corresponding to each initial code block can be directly obtained through the code library. For example, if the first rearranged code block sequence includes five initial code blocks, two of which belong to the same original code text, then four original code texts can be obtained.

[0108] Step b: based on the start and end code lines corresponding to each method in the original code text, the original code text is segmented to obtain candidate code blocks.

[0109] Each candidate code block corresponds to at least one method (i.e., may contain code corresponding to one or more abstract methods). Optionally, the original code text may be segmented using a syntax tree. For example, the original code text may be parsed using a syntax tree to determine the start and end positions of each method in the original code text. The original code text may then be segmented based on the start and end positions of each method to obtain at least one candidate code block.

[0110] The syntax tree is an abstract representation of the grammatical structure of the source code, which shows the syntax structure of the programming language in a tree-like form, and each node on the tree represents a structure in the source code. Different programming languages ​​have different grammatical features and code structure characteristics. For example, the definition of a class and the implementation of a class in the Java programming language need to be put together, while the definition of a class and the implementation of a class in the C++ programming language need to be written separately, and the specific statements of the class definition are also different. Optionally, the target programming language corresponding to the code to be stored can be identified, and based on the grammatical features of the target programming language, the syntax tree of the code to be stored can be parsed to obtain the code structure of the code to be stored. This code structure is used to characterize the corresponding start and end positions of each method or function (each part).

[0111] Illustratively, the starting and ending positions (including the starting and ending code lines) of each method in the original code text can be determined based on the code structure. When the number of methods is greater than one, for example, the original code text includes call codes for five methods, the original code text can be divided into five code blocks based on the starting and ending positions of each method call code, with each code block containing the call code for one method.

[0112] In one possible implementation, during the intelligent question-answering process, the natural language question information entered by the user may have problems such as unclear question expression, unclear intention, or excessive length. If the search strategy is selected directly based on the question information entered by the user, the search results may be inaccurate. Therefore, it is necessary to optimize the question information before selecting the search strategy. Figure 3 As shown, the above step 102 may specifically include the following steps:

[0113] Step 301: Optimize the question information using a preset optimization method to obtain a search text.

[0114] The search text may include, for example, at least one of optimization question information and search keywords. The preset optimization method includes at least one of keyword expansion, text error correction, and redundant information removal. The optimization question information is directly obtained by optimizing the question information using the preset optimization method, and the search keywords are obtained by keyword extraction from the optimization question information.

[0115] Optionally, a language model can be used to optimize the text of the question information. For example, the question information and question optimization prompts can be input into the language model. The question optimization prompts include descriptions of the preset optimization methods and may also include characteristics and examples corresponding to each preset optimization method. The preset optimization prompts allow the language model to clarify the question information and appropriately expand relevant keywords to improve search results.

[0116] Illustratively, for the question information "How to quickly sort", the language model can perform problem optimization to obtain the optimized question information "How to implement the quick sort algorithm?", and based on the optimized question information, filter out keywords such as "code implementation of quick sort" and "logic for implementing quick sort".

[0117] Step 302: vectorize the search text to obtain a search vector.

[0118] In one possible implementation, an embedding network is provided in the language model, through which the search keywords and optimization question information can be converted into keyword vectors and question vectors, respectively. The search vector includes at least one of the keyword vector and the question vector.

[0119] Step 303: Use the search vector to perform code search to obtain an initial code block sequence.

[0120] Optionally, the similarity between the keyword vector and the question vector and each code block index in the code library can be calculated respectively, the z code blocks with the highest similarity can be selected, and the initial code block sequence can be obtained by arranging them in descending order of similarity, where z is a positive integer and the value of z is greater than or equal to the value of m.

[0121] Based on the embodiments of the present disclosure, the question information is optimized through at least one preset optimization method to obtain clear and complete optimized question information and more comprehensive search keywords. Searching based on the search text including at least one of the optimized question information and search keywords can improve the accuracy of information retrieval, thereby improving the accuracy of reply information.

[0122] Optionally, the method provided by the embodiment of the present disclosure can be applied to a variety of question-and-answer scenarios, such as intelligent assistants for electronic products, intelligent customer service for shopping platforms, and the like. The conversation topics and user needs for reply information in different question-and-answer scenarios may be different, so it is possible to design a retrieval library, a preset optimization method, and a suitable reordering method corresponding to the current question-and-answer scenario. Schematically, the current question-and-answer scenario includes a conversation scenario in an IDE. For example, it can be applied to a conversation chat work module of an IDE, which can assist users in obtaining programming information through natural language interaction and help users solve various problems encountered in programming.

[0123] Figure 4 The following is a block diagram of a code search device based on natural language provided by an exemplary embodiment of the present disclosure. The code search device based on natural language includes:

[0124] An acquisition module 401 is configured to acquire question information input by the question input operation in response to detecting the question input operation;

[0125] A retrieval module 402 is configured to perform code retrieval based on the problem information obtained by the acquisition module 401 to obtain an initial code block sequence;

[0126] A first reordering module 403 is configured to reorder the code blocks in the initial code block sequence retrieved by the retrieval module 402 using a first reordering method to obtain a first reordered code block sequence, and determine candidate code blocks corresponding to the first reordered code block sequence, wherein the candidate code blocks include code blocks obtained by segmenting the original code text to which each code block in the first reordered code block sequence belongs;

[0127] A second reordering module 404 is configured to reorder the candidate code blocks obtained by the first reordering module 403 using a second reordering method to obtain a second reordered code block sequence;

[0128] The generating module 405 is configured to generate a search result corresponding to the question information based on the second rearranged code block sequence obtained by the second rearrangement module 404 .

[0129] Optionally, in a possible implementation manner, the first reordering module 403 may also be configured to:

[0130] Reordering the code blocks in the initial code block sequence using a first reordering method, generating a first reordered code block sequence based on n code blocks with the highest similarity, where n is a positive integer;

[0131] The second reordering module 404 may also be used to:

[0132] The candidate code blocks are reordered using a second reordering method, and a second reordered code block sequence is generated based on the m candidate code blocks with the highest similarity, where m is a positive integer.

[0133] Optionally, in a possible implementation manner, the first reordering module 403 may also be configured to:

[0134] Inputting the initial code block sequence and question information into the reordering model, the reordering model calculates the similarity between each code block and the question information, and obtains a first similarity corresponding to each code block;

[0135] The n code blocks with the highest first similarities are arranged in descending order of the first similarities to obtain a first rearranged code block sequence.

[0136] Optionally, in a possible implementation manner, the second reordering module 404 may also be used to:

[0137] Using the TF-IDF algorithm, calculate the second similarity between each candidate code block and the question information;

[0138] The m candidate code blocks with the highest second similarity are arranged in descending order of the second similarity to obtain a second rearranged code block sequence.

[0139] Optionally, in a possible implementation manner, the first reordering module 403 may be further configured to:

[0140] Obtaining original code text corresponding to each code block in the first rearranged code block sequence from a code library;

[0141] Based on the start and end code lines corresponding to each method in the original code text, the original code text is segmented to obtain the candidate code blocks, each candidate code block corresponding to a method.

[0142] Optionally, in a possible implementation, the retrieval module 402 may also be used to:

[0143] Optimizing the question information using a preset optimization method to obtain a search text, the search text including at least one of the optimized question information and search keywords, the preset optimization method including at least one of keyword expansion, text error correction, and redundant information removal;

[0144] Perform vectorization processing on the search text to obtain the search vector;

[0145] Use the retrieval vector to perform code retrieval and obtain the initial code block sequence.

[0146] Each embodiment in this specification is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The parts of the embodiments that are the same, similar, or corresponding can be referred to each other. Since the method, device, system, and equipment embodiments are basically corresponding, the relevant parts can be referred to the description of the corresponding parts. The methods, devices, systems, and equipment of the embodiments of the present disclosure also correspond to each other in terms of specific implementation methods and beneficial technical effects. The relevant contents can be referenced to each other and will not be repeated here.

[0147] In addition, an embodiment of the present disclosure further provides an electronic device, including:

[0148] memory for storing computer programs;

[0149] The processor is configured to execute the computer program stored in the memory, and when the computer program is executed, implements the natural language-based code retrieval method described in any of the above embodiments of the present disclosure.

[0150] Figure 5 This is a schematic diagram of the structure of an application embodiment of the electronic device disclosed in the present invention. Figure 5 The electronic device according to the embodiment of the present disclosure is described. The electronic device may be either or both of the first device and the second device, or a standalone device independent of them, and the standalone device may communicate with the first device and the second device to receive collected input signals from them.

[0151] like Figure 5 As shown, the electronic device includes one or more processors and memory.

[0152] The processor may be a central processing unit (CPU) or other forms of processing units having data processing capabilities and / or instruction execution capabilities, and may control other components in the electronic device to perform desired functions.

[0153] The memory may include one or more computer program products, which may include various forms of computer-readable storage media, such as volatile memory and / or non-volatile memory. The volatile memory may include, for example, random access memory (RAM) and / or cache memory. The non-volatile memory may include, for example, read-only memory (ROM), a hard disk, a flash memory, etc. One or more computer program instructions may be stored on the computer-readable storage medium, and the processor may execute the program instructions to implement the natural language-based code retrieval method of each embodiment of the present disclosure described above and / or other desired functions.

[0154] In one example, the electronic device may further include an input device and an output device, and these components are interconnected via a bus system and / or other forms of connection mechanisms (not shown).

[0155] In addition, the input device may also include, for example, a keyboard, a mouse, and the like.

[0156] The output device can output various information to the outside, including determined distance information, direction information, etc. The output device can include, for example, a display, a speaker, a printer, a communication network and a remote output device connected thereto, and the like.

[0157] Of course, to simplify, Figure 5Only some of the components related to the present disclosure in the electronic device are shown, and components such as a bus, an input / output interface, etc. are omitted. In addition, the electronic device may further include any other appropriate components according to specific application scenarios.

[0158] In addition to the above-mentioned methods and devices, an embodiment of the present disclosure may also be a computer program product, which includes computer program instructions, which, when executed by a processor, enable the processor to perform the steps of the natural language-based code retrieval method according to various embodiments of the present disclosure described in the above part of this specification.

[0159] The computer program product may be written in any combination of one or more programming languages ​​to implement the operations of the disclosed embodiments, including object-oriented programming languages ​​such as Java, C++, and conventional procedural programming languages ​​such as C or similar programming languages. The program code may be executed entirely on the user's computing device, partially on the user's computing device, as a standalone software package, partially on the user's computing device and partially on a remote computing device, or entirely on a remote computing device or server.

[0160] In addition, an embodiment of the present disclosure may also be a computer-readable storage medium having computer program instructions stored thereon, which, when executed by a processor, causes the processor to execute the steps of the natural language-based code retrieval method according to various embodiments of the present disclosure described in the above part of this specification.

[0161] The computer-readable storage medium can adopt any combination of one or more readable media. The readable medium can be a readable signal medium or a readable storage medium. The readable storage medium can, for example, include but is not limited to a system, device or component of electricity, magnetism, light, electromagnetic, infrared, or semiconductor, or any combination thereof. More specific examples (non-exhaustive list) of readable storage media include: an electrical connection with one or more wires, a portable disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof.

[0162] Those skilled in the art will understand that all or part of the steps of implementing the above-mentioned method embodiment can be completed by hardware related to program instructions, and the aforementioned program can be stored in a computer-readable storage medium. When the program is executed, it executes the steps of the above-mentioned method embodiment; and the aforementioned storage medium includes: ROM, RAM, disk or optical disk, etc. Various media that can store program codes.

[0163] The basic principles of the present disclosure have been described above in conjunction with specific embodiments. However, it should be noted that the advantages, strengths, and effects mentioned in this disclosure are merely illustrative and not restrictive, and should not be construed as necessarily possessed by each embodiment of the present disclosure. Furthermore, the specific details disclosed above are provided for illustrative purposes and to facilitate understanding, rather than as limitations. These details do not limit the present disclosure to necessarily being implemented using these specific details.

[0164] Each embodiment in this specification is described in a progressive manner, with each embodiment focusing on its differences from the other embodiments. References to the same or similar parts between the various embodiments are sufficient. For system embodiments, since they largely correspond to method embodiments, their description is relatively simple. For relevant parts, references to the description of the method embodiments are sufficient.

[0165] The block diagrams of the devices, devices, equipment, and systems involved in this disclosure are merely illustrative examples and are not intended to require or imply that they must be connected, arranged, or configured in the manner shown in the block diagrams. As will be appreciated by those skilled in the art, these devices, devices, equipment, and systems can be connected, arranged, or configured in any manner. Words such as "include," "comprise," "have," and the like are open-ended words, meaning "including but not limited to," and can be used interchangeably therewith. The words "or" and "and" used herein refer to the words "and / or" and can be used interchangeably therewith, unless the context clearly indicates otherwise. The word "such as" used herein refers to the phrase "such as but not limited to," and can be used interchangeably therewith.

[0166] The methods and apparatus of the present disclosure may be implemented in many ways. For example, the methods and apparatus of the present disclosure may be implemented by software, hardware, firmware, or any combination of software, hardware, and firmware. The above order of steps for the method is for illustration only, and the steps of the method of the present disclosure are not limited to the order specifically described above unless otherwise specified. In addition, in some embodiments, the present disclosure may also be implemented as programs recorded in a recording medium, which include machine-readable instructions for implementing the methods according to the present disclosure. Thus, the present disclosure also covers recording media that store programs for executing the methods according to the present disclosure.

[0167] It should also be noted that in the apparatus, device, and method of the present disclosure, each component or each step can be decomposed and / or recombined. Such decomposition and / or recombination should be regarded as equivalent solutions of the present disclosure.

[0168] The above description of the disclosed aspects is provided to enable any person skilled in the art to make or use the present disclosure. Various modifications to these aspects will be readily apparent to those skilled in the art, and the general principles defined herein may be applied to other aspects without departing from the scope of the present disclosure. Therefore, the present disclosure is not intended to be limited to the aspects shown herein, but rather to be accorded the widest scope consistent with the principles and novel features disclosed herein.

[0169] The above description has been provided for the purpose of illustration and description. In addition, this description is not intended to limit the embodiments of the present disclosure to the forms disclosed herein. Although a number of example aspects and embodiments have been discussed above, those skilled in the art will recognize certain variations, modifications, alterations, additions, and sub-combinations thereof.

Claims

1. A code retrieval method based on natural language, characterized in that: include: In response to detecting a question input operation, obtaining question information input by the question input operation; Perform code retrieval based on the problem information to obtain an initial code block sequence; reordering the code blocks in the initial code block sequence using a first reordering method to obtain a first reordered code block sequence, and determining candidate code blocks corresponding to the first reordered code block sequence, the candidate code blocks comprising code blocks obtained by segmenting original code texts to which the code blocks in the first reordered code block sequence belong; Reordering the candidate code blocks using a second reordering method to obtain a second reordered code block sequence; A retrieval result corresponding to the question information is generated based on the second rearranged code block sequence.

2. The method according to claim 1, characterized in that The step of reordering the code blocks in the initial code block sequence using a first reordering method to obtain a first reordered code block sequence includes: Reordering the code blocks in the initial code block sequence using the first reordering method, and generating the first reordered code block sequence based on n code blocks with the highest similarity, where n is a positive integer; The step of reordering the candidate code blocks corresponding to the first reordered code block sequence using a second reordering method to obtain a second reordered code block sequence includes: The candidate code blocks are reordered using the second reordering method, and the second reordered code block sequence is generated based on the m candidate code blocks with the highest similarity, where m is a positive integer.

3. The method according to claim 2, characterized in that The step of reordering the code blocks in the initial code block sequence by using the first reordering method and generating the first reordered code block sequence based on the n code blocks with the highest similarity includes: Inputting the initial code block sequence and the question information into a reordering model, and calculating the similarity between each code block and the question information by the reordering model to obtain a first similarity corresponding to each code block; The n code blocks with the highest first similarities are arranged in descending order of first similarities to obtain the first rearranged code block sequence.

4. The method according to claim 2, characterized in that The step of reordering the candidate code blocks using the second reordering method and generating the second reordered code block sequence based on the m candidate code blocks with the highest similarity includes: Calculate the second similarity between each candidate code block and the question information using the TF-IDF algorithm; The m candidate code blocks with the highest second similarity are arranged in descending order of second similarity to obtain the second rearranged code block sequence.

5. The method according to any one of claims 1 to 4, characterized in that: The determining of the candidate code blocks corresponding to the first rearranged code block sequence includes: Obtaining the original code text corresponding to each code block in the first rearranged code block sequence from a code library; Based on the start and end code lines corresponding to each method in the original code text, the original code text is segmented to obtain the candidate code blocks, each candidate code block corresponding to at least one method.

6. The method according to any one of claims 1 to 4, characterized in that: The code retrieval based on the problem information to obtain an initial code block sequence includes: Optimizing the question information using a preset optimization method to obtain a search text, wherein the search text includes at least one of the optimized question information and a search keyword, and the preset optimization method includes at least one of keyword expansion, text error correction, and redundant information removal; Performing vectorization processing on the search text to obtain a search vector; Code retrieval is performed using the retrieval vector to obtain the initial code block sequence.

7. A code retrieval device based on natural language, characterized in that: include: An acquisition module, configured to acquire question information input by the question input operation in response to detecting the question input operation; A retrieval module, configured to perform code retrieval based on the problem information to obtain an initial code block sequence; a first reordering module, configured to reorder the code blocks in the initial code block sequence using a first reordering method to obtain a first reordered code block sequence, and determine candidate code blocks corresponding to the first reordered code block sequence, the candidate code blocks comprising code blocks obtained by segmenting original code texts to which the code blocks in the first reordered code block sequence belong; A second reordering module, configured to reorder the candidate code blocks using a second reordering method to obtain a second reordered code block sequence; A generating module is used to generate a search result corresponding to the question information based on the second rearranged code block sequence.

8. An electronic device, characterized in that: include: memory for storing computer programs; A processor is configured to execute a computer program stored in the memory, and when the computer program is executed, implements the method described in any one of claims 1 to 6.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.

10. A computer program comprising computer program instructions, characterized in that When the computer program instructions are executed by a processor, the method according to any one of claims 1 to 6 is implemented.

Citation Information

Patent Citations

  • Code search method and system

    CN106294786A

  • Index building method, device, equipment and storage medium

    CN119782446A

  • Retrieval code reordering method and device based on natural language

    CN119829750A

  • Code retrieval method and apparatus based on large language model

    US20240403344A1