Code prediction method, apparatus, device, storage medium, and program product
By using code clone retrieval and abstract syntax tree analysis, combined with priority relationships to generate predictive hints, the problem of low code prediction accuracy in existing technologies is solved, achieving more efficient code generation.
Patent Information
- Application Number
- PCT/CN2025/080480
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2024-06-19
- Filing Date
- 2025-03-04
- Publication Date
- 2025-12-26
AI Technical Summary
Existing code prediction methods based on large models have low accuracy in predicting code during software development, which may result in generated code that does not meet the needs of programmers.
The code cloning retrieval technology is used to search for similar code segments from multiple dimensions (text, lexical, syntactic, and semantic), and the target code segment is determined by combining the abstract syntax tree. Prediction prompts are generated through priority relationships to guide the code prediction process.
It improves the accuracy of code prediction, ensuring that the generated code better meets the needs of programmers and improves software development efficiency.
Smart Images

Figure CN2025080480_26122025_PF_FP_ABST
Abstract
Description
Code prediction methods, apparatus, devices, storage media, and program products
[0001] This application claims priority to Chinese Patent Application No. 202410798903.2, filed on June 19, 2024, entitled "Code Prediction Method, Apparatus, Device, Storage Medium and Program Product", the entire contents of which are incorporated herein by reference. Technical Field
[0002] This application relates to the field of computer technology, and in particular to a code prediction method, apparatus, device, storage medium, and program product. Background Technology
[0003] In the context of computer software development, with the continuous development of large-parameter network models (also known as large models), AI-assisted R&D methods based on large models have gradually become popular, transforming the traditional software development process into an intelligent one. These AI-assisted R&D methods include code prediction methods, among others.
[0004] Taking code prediction methods based on large models as an example, during the code writing phase of software development, after programmers input some code, these methods can predict subsequent code, thereby reducing the amount of code programmers need to input and improving software development efficiency. However, the accuracy of the predicted code is not high, which means that the final generated code may not meet the programmer's needs. Summary of the Invention
[0005] This application provides a code prediction method, apparatus, device, storage medium, and computer program, which can solve the problem of low accuracy in code prediction results in related technologies. The technical solution is as follows:
[0006] Firstly, a code prediction method is provided, the method comprising: performing code clone retrieval on multiple candidate code segments based on a target code segment to obtain code clone retrieval results, wherein the candidate code segments include multiple code segments, the candidate code segments are fully defined, and the code clone retrieval results indicate whether there is a first type of code segment similar to the target code segment among the multiple candidate code segments; if the code clone retrieval results indicate that there is a first type of code segment among the multiple candidate code segments, then predicting the subsequent code of the target code segment based on the first type of code segment and the target code segment to obtain code prediction results.
[0007] Since code clone retrieval can retrieve similar code from multiple dimensions such as text, lexical, syntactic, and semantic, the similar code segments (i.e., the first type of code segments) identified from multiple candidate codes through code clone retrieval have little difference from the target code segment. Thus, it can effectively improve the accuracy of retrieving similar codes, thereby ensuring that the obtained code prediction results are highly accurate.
[0008] Optionally, based on the target code segment, code clone retrieval is performed on multiple candidate code segments to obtain code clone retrieval results.
[0009] Optionally, each candidate code segment is at the function level, meaning each candidate code segment indicates a fully defined function. In another possible implementation, each candidate code segment is at the code file level, meaning each candidate code segment indicates multiple fully defined functions.
[0010] Optionally, before performing code clone retrieval on multiple candidate code segments based on the target code segment and obtaining the code clone retrieval results, it is also necessary to obtain the target code segment.
[0011] Optionally, based on the predicted position, the target code segment is obtained from multiple code segments included in the code file, where the predicted position refers to the starting position of the code to be predicted in the code file.
[0012] Optionally, based on the multiple code segments included in the code file, an abstract syntax tree corresponding to the code file is determined, and based on the abstract syntax tree corresponding to the code file, a code segment for the function used to indicate the prediction position is determined, thus obtaining the target code segment.
[0013] Optionally, the abstract syntax tree corresponding to the code file includes multiple nodes, each with its type and position. A node indicates a code element within multiple code segments, and the position of a node refers to the location of the code element it indicates within the multiple code segments. These multiple nodes include the root node. Thus, the process of obtaining the code segment indicating the predicted position from multiple code segments based on the abstract syntax tree, as the target code segment, includes: determining the first node closest to the predicted position from the multiple nodes; determining the second node from the abstract syntax tree based on the first node, where the second node is the node of type function closest to the first node on the path from the first node to the root node; and determining the target code segment based on the code elements indicated by the second and third nodes, where the third node is the node connected to the second node and far from the root node.
[0014] It should be noted that the third node is any node that is directly or indirectly connected to the second node and is far away from the root node.
[0015] The process of determining the implementation of the target code segment based on the code elements indicated by the second and third nodes includes: determining all code segments composed of the code elements indicated by the second and third nodes, and determining the target code segment based on all code segments composed of the code elements indicated by the second and third nodes.
[0016] Optionally, the code segments corresponding to the code elements indicated by the second and third nodes in the code file are determined as all code segments composed of the code elements indicated by the second and third nodes.
[0017] The abstract syntax tree also indicates semantic rules between multiple nodes. In this case, intermediate code corresponding to the code elements indicated by the second and third nodes can be generated based on the code elements indicated by the second and third nodes, the semantic rules between the second and third nodes, and the syntax rules. The intermediate code includes multiple code segments, and the intermediate code is determined to be all code segments composed of the code elements indicated by the second and third nodes.
[0018] Because code segments in a code file may contain redundant elements unrelated to the code's function, such as comments, whitespace, punctuation marks, and delimiters, while intermediate code is functionally identical to the source code but removes many redundant elements from the source code (i.e., the multiple code segments included in the code file), intermediate code is more concise and refined than the source code. By identifying intermediate code as all code segments composed of code elements indicated by the second and third nodes, the efficiency and accuracy of subsequent retrieval can be effectively improved.
[0019] Optionally, all code segments consisting of the code elements indicated by the second and third nodes can be identified as the target code segment. Optionally, the N consecutive code segments preceding the prediction position in all code segments consisting of the code elements indicated by the second and third nodes can be identified as the target code segment, where N is an integer greater than or equal to 1.
[0020] Optionally, besides generating a tree through abstract syntax to obtain the target code segment from multiple code segments included in the code file, the target code segment can also be directly determined by identifying M consecutive code segments preceding the prediction position from the multiple code segments, where M is an integer greater than or equal to 1. Alternatively, if there is at least one code segment after the prediction position in the code file, the target code segment can be identified by identifying M1 consecutive code segments preceding the prediction position and M2 consecutive code segments following the prediction position from the multiple code segments included in the code file.
[0021] Based on the first type of code segment and the target code segment, prediction prompts are determined. These prompts are used to guide the code prediction process. The prediction prompts are then input into the code prediction model to obtain the code prediction result output by the model.
[0022] The following will describe the implementation process of determining the prediction prompt information based on the first type of code segment and the target code segment through steps (1)-(2).
[0023] Step (1): Based on the first type of code segment, determine the reference code segment.
[0024] Optionally, the first type of code segment can be designated as the reference code segment.
[0025] Optionally, before determining the reference code segment based on the first type of code segment, the electronic device may also perform a vectorized search on multiple candidate code segments based on the target code segment to obtain vectorized search results. The vectorized search results indicate whether there is a second type of code segment among the multiple candidate code segments that is similar to the target code segment. In this case, if the vectorized search results indicate that there is a second type of code segment among the multiple candidate code segments, then the reference code segment is determined based on the first type of code segment and the second type of code segment.
[0026] Optionally, the code clone retrieval results also include the similarity scores of each code segment in the first type of code segment, and the vectorized retrieval results also include the similarity scores of each code segment in the second type of code segment. The first type of code segment has a higher priority than the second type of code segment, and the similarity score indicates the degree of similarity between the corresponding code segment and the target code segment. In this case, the process of determining the implementation of the reference code segment based on the first type of code segment and the second type of code segment includes: filtering the first type of code segment and the second type of code segment based on the similarity scores of each code segment in the first type of code segment, the similarity scores of each code segment in the second type of code segment, and the priority relationship between the first type of code segment and the second type of code segment to obtain the reference code segment.
[0027] Since the first type of code segment is a code segment that is similar to the target code segment in multiple dimensions, while the second type of code segment is a code segment that is similar to the target code segment only in the dimension of implicit vector semantic similarity, the priority of each code segment in the first type of code segment is greater than the priority of each code segment in the second type of code segment.
[0028] Step (2): Determine the prediction prompt information based on the reference code segment and the target code segment.
[0029] Optionally, the prediction prompt information includes any of the following: reference code segment and target code segment; reference code segment, target code segment, and other code segments in the code file except for the target code segment; priority of reference code segment, target code segment, priority of reference code segment and priority of target code segment; priority of reference code segment, target code segment, priority of reference code segment, priority of target code segment, other code segments in the code file except for the target code segment and priority of other code segments.
[0030] Where the reference code segment includes at least one code segment, the priority of the target code segment is greater than the priority of each code segment within the at least one code segment, the priority of each code segment within the at least one code segment is greater than the priority of other code segments, and the priority of the at least one code segment is proportional to its corresponding similarity, where similarity indicates the degree of similarity between the corresponding code segment and the target code segment. If the reference code segment includes a first type of code segment and a second type of code segment, then the priority of any code segment in the first type of code segment is greater than the priority of any code segment in the second type of code segment.
[0031] This application incorporates the priority relationship between code segments as part of the prediction hint information, and then performs code prediction based on this prediction hint information. Compared with schemes that only use similar code and context code as prediction information, this application achieves accurate and comprehensive guidance for the code prediction process by carrying prediction hint information that includes the priority relationship between code segments, thereby making the final code prediction result more accurate.
[0032] Alternatively, the predicted prompt information can be determined based on the reference code segment, the target code segment, other code segments in the code file besides the target code segment, the priority relationship between the code segments, and the length of the prompt information. The priority relationship between the code segments indicates the priority relationship between the reference code segment, the target code segment, and other code segments, and the length of the prompt information indicates the upper limit of the length of the predicted prompt information.
[0033] The target code segment, reference code segment, and other code segments are concatenated in descending order of priority to obtain the concatenated code segment. If the length of the concatenated code segment is less than or equal to the length of the prompt message, the concatenated code segment is determined as the predicted prompt message. If the length of the concatenated code segment is greater than the length of the prompt message, the concatenated code segment is truncated to the length of the prompt message to obtain the truncated code segment, which is equal to the length of the prompt message.
[0034] Optionally, if the length of the concatenated code segment is less than or equal to the length of the hint message, the priority of each code segment in the concatenated code segment is also used as the predicted hint message. If the length of the concatenated code segment is greater than the length of the hint message, the priority of each code segment in the truncated code segment is also used as the predicted hint message.
[0035] This application concatenates code segments based on their priority relationships and the length of the hint information. This ensures that higher-priority code segments are less likely to be truncated, making them more likely to be used as prediction hints. Furthermore, the order of the code segments in the prediction hints also reflects the priority relationships between them. Compared to schemes that only use similar code and context code as prediction information, this application can provide accurate and comprehensive guidance for code prediction by determining the order of code segments in the prediction hints, resulting in more accurate code prediction results.
[0036] Optionally, if the code clone retrieval results indicate that there is no first-type code segment among the multiple candidate code segments, and the vectorization retrieval results indicate that there is a second-type code segment among the multiple candidate code segments, then the subsequent code of the target code segment is predicted based on the second-type code segment and the target code segment to obtain the code prediction result.
[0037] Optionally, if the code clone retrieval results indicate that there is no first type of code segment among the multiple candidate code segments, then a vectorized retrieval is performed on the multiple candidate code segments based on the target code segment to obtain a vectorized retrieval result. If the vectorized retrieval results indicate that there is a second type of code segment among the multiple candidate code segments, then the subsequent code of the target code segment is predicted based on the second type of code segment and the target code segment to obtain a code prediction result.
[0038] Secondly, a code prediction method is provided, applied to an electronic device. The method includes: performing code clone retrieval on multiple candidate code segments based on a target code segment to obtain code clone retrieval results, wherein the candidate code segments include multiple code segments, the candidate code segments are fully defined, and the code clone retrieval results indicate whether there is a first type of code segment similar to the target code segment among the multiple candidate code segments; sending the code clone retrieval results to a cloud platform, such that if the code clone retrieval results indicate the existence of the first type of code segment among the multiple candidate code segments, the cloud platform predicts the subsequent code of the target code segment based on the first type of code segment and the target code segment; or, sending prediction prompt information to the cloud platform, such that the cloud platform predicts the subsequent code of the target code segment based on the prediction prompt information, wherein the prediction prompt information is determined based on the first type of code segment and the target code segment present among the multiple candidate code segments.
[0039] Optionally, based on the target code segment, a vectorized search is performed on the plurality of candidate code segments to obtain a vectorized search result. The vectorized search result indicates whether there is a second type of code segment similar to the target code segment among the plurality of candidate code segments; the vectorized search result is then sent to the cloud platform.
[0040] Thirdly, a code prediction method is provided, applied to a cloud platform. The method includes: receiving code clone retrieval results or prediction prompts sent by an electronic device, wherein the code clone retrieval results are obtained by cloning multiple candidate code segments based on a target code segment, the candidate code segments include multiple code segments, the candidate code segments are fully defined, the code clone retrieval results indicate whether there is a first type of code segment similar to the target code segment among the multiple candidate code segments, and the prediction prompts are determined based on the first type of code segment present among the multiple candidate code segments and the target code segment; upon receiving the code clone retrieval results, if the code clone retrieval results indicate that there is a first type of code segment among the multiple candidate code segments, then based on the first type of code segment and the target code segment, predicting the subsequent code of the target code segment to obtain a code prediction result; upon receiving the prediction prompts, based on the prediction prompts, predicting the subsequent code of the target code segment to obtain the code prediction result.
[0041] Optionally, the step of predicting the subsequent code of the target code segment based on the first type of code segment and the target code segment to obtain a code prediction result includes: determining prediction prompt information based on the first type of code segment and the target code segment, wherein the prediction prompt information is used to guide the code prediction process; and inputting the prediction prompt information into the code prediction model to obtain the code prediction result output by the code prediction model.
[0042] Optionally, determining the prediction prompt information based on the first type of code segment and the target code segment includes: determining a reference code segment based on the first type of code segment; and determining the prediction prompt information based on the reference code segment and the target code segment.
[0043] Optionally, the method further includes: receiving a vectorized retrieval result sent by an electronic device, wherein the vectorized retrieval result is obtained by performing a vectorized retrieval on the plurality of candidate code segments based on the target code segment, and the vectorized retrieval result indicates whether there is a second type of code segment similar to the target code segment among the plurality of candidate code segments; the step of determining a reference code segment based on the first type of code segment includes: if the vectorized retrieval result indicates that there is a second type of code segment among the plurality of candidate code segments, then determining the reference code segment based on the first type of code segment and the second type of code segment.
[0044] Optionally, the method further includes: if the code clone retrieval result indicates that the first type of code segment does not exist among the plurality of candidate code segments, and the vectorization retrieval result indicates that the second type of code segment exists among the plurality of candidate code segments, then based on the second type of code segment and the target code segment, predicting the subsequent code of the target code segment to obtain a code prediction result.
[0045] Fourthly, a code prediction apparatus is provided, which has the function of implementing the code prediction method behavior described in the first aspect above. The code prediction apparatus includes at least one module for implementing the code prediction method provided in the first aspect above.
[0046] Fifthly, a code prediction apparatus is provided, which has the function of implementing the code prediction method behavior described in the second aspect above. The code prediction apparatus includes at least one module for implementing the code prediction method provided in the second aspect above.
[0047] Sixthly, a code prediction apparatus is provided, which has the function of implementing the code prediction method behavior described in the third aspect above. The code prediction apparatus includes at least one module for implementing the code prediction method provided in the third aspect above.
[0048] A seventh aspect provides an electronic device comprising a processor and a memory, the memory being configured to store a computer program for executing the code prediction method provided in the first aspect, or to store a computer program for executing the code prediction method provided in the second aspect. The processor is configured to execute the computer program stored in the memory to implement the code prediction method described in the first aspect, or to implement the code prediction method described in the second aspect.
[0049] Optionally, the electronic device may further include a communication bus for establishing a connection between the processor and the memory.
[0050] Eighthly, a computing device cluster is provided, the computing device cluster including at least one computing device, each computing device including a processor and a memory, wherein the processor of the at least one computing device is configured to execute instructions stored in the memory of the at least one computing device, such that the computing device cluster performs the code prediction method of the first aspect or performs the code prediction method of the third aspect.
[0051] Optionally, each computing device may also include a communication bus for establishing a connection between the processor and memory of each computing device.
[0052] Ninthly, a computer-readable storage medium is provided, wherein a computer program is stored therein, and when the computer program is run on a computer or processor, the computer or processor performs the steps of the code prediction method described in the first aspect, or performs the steps of the code prediction method described in the second aspect, or performs the steps of the code prediction method described in the third aspect.
[0053] In a tenth aspect, a computer program product is provided, the computer program product comprising computer instructions that, when executed on a computer or processor, cause the computer to perform the steps of the code prediction method described in the first aspect, or the steps of the code prediction method described in the second aspect, or the steps of the code prediction method described in the third aspect. Alternatively, a computer program is provided that, when executed on a computer or processor, causes the computer or processor to perform the steps of the code prediction method described in the first aspect, or the steps of the code prediction method described in the second aspect, or the steps of the code prediction method described in the third aspect.
[0054] The technical effects achieved by the second to tenth aspects mentioned above are similar to those achieved by the corresponding technical means in the first aspect, and will not be repeated here. Attached Figure Description
[0055] Figure 1 is a schematic diagram of a code prediction method based on RAG provided in an embodiment of this application;
[0056] Figure 2 is a schematic diagram of an implementation environment provided in an embodiment of this application;
[0057] Figure 3 is a schematic diagram of the structure of an electronic device provided in an embodiment of this application;
[0058] Figure 4 is a flowchart of a code prediction method provided in an embodiment of this application;
[0059] Figure 5 is a flowchart of another code prediction method provided in an embodiment of this application;
[0060] Figure 6 is a flowchart of another code prediction method provided in an embodiment of this application;
[0061] Figure 7 is a schematic diagram of the structure of a code prediction device provided in an embodiment of this application;
[0062] Figure 8 is a schematic diagram of another code prediction device provided in an embodiment of this application;
[0063] Figure 9 is a schematic diagram of another code prediction device provided in an embodiment of this application. Detailed Implementation
[0064] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the implementation methods of this application will be further described in detail below with reference to the accompanying drawings.
[0065] To facilitate understanding, before providing a detailed explanation of the code prediction method provided in the embodiments of this application, the terms, application scenarios, and implementation environments involved in the embodiments of this application will be introduced first.
[0066] First, the terms used in the embodiments of this application will be introduced.
[0067] Prompt: In large language models, prompt is an important concept. It refers to the text input to the model, which guides the model to generate the expected output. The definition and design of the prompt are crucial to the model's performance and output quality.
[0068] In some embodiments, the aforementioned large language model includes BERT (bidirectional encoder representations from transformers), GPT (generative pre-trained transformer), T5 (text-to-text transfer transformer), etc., and this application does not limit the specific model.
[0069] An integrated development environment (IDE) is an application that provides a program development environment. It typically includes a code editor, which is a tool for programmers to input and edit code.
[0070] In some embodiments, the IDE also includes tools such as a compiler, debugger, and graphical user interface.
[0071] In programming, a function is a well-organized, reusable block of code used to perform a single or related task. A function consists of a function name and a function body. The function name uniquely identifies a function, while the function body contains the code segment (also known as a code block) that performs a specific task or operation.
[0072] In some embodiments, a function may also include a parameter list, a return value, and so on. A parameter list is a comma-separated list of variables used to pass data to the function, and a return value is the value returned to the caller after the function has finished executing.
[0073] In programming, a token represents a code element. Tokens come in several types, including keywords, identifiers, literals, operators, separators, and more.
[0074] Keywords are words with fixed meanings defined by the programming language, such as `if`, `while`, and `return`. Identifiers are defined by the programmer and used to identify variables, functions, etc. Literals represent fixed values in code, such as numbers and strings. Operators are arithmetic operators, such as addition, subtraction, multiplication, and division. Separators are symbols used to separate different code elements, such as parentheses, curly braces, and commas.
[0075] An abstract syntax tree (AST) is a tree-like structure formed by combining a series of tokens after the source code has been analyzed and divided into a series of tokens, based on the syntax rules of the programming language. An AST can represent the syntactic structure of source code in a tree-like form, where each node represents a code element in the source code.
[0076] Retrieval Augmented Generation (RAG): RAG technology combines language modeling and information retrieval techniques. When the model needs to generate text or answer questions, it first retrieves relevant information from an external knowledge base. Then, the large language model uses the retrieved information to generate the results. In other words, RAG technology enhances the generated results of a large model by retrieving external knowledge.
[0077] Vectorized retrieval: In the programming field, vectorized retrieval refers to converting code segments into vectors, then calculating the similarity between the vectors corresponding to each code segment in the code library and the vector corresponding to the code segment to be queried (i.e., the target code segment). Based on the similarity, code segments that are implicitly the same as or similar to the target code segment are retrieved from the code library.
[0078] Text similarity retrieval: In the programming field, text similarity retrieval refers to retrieving code segments from a code library that are the same as or similar to the target code segment in terms of text (i.e., strings).
[0079] Code clone retrieval: In the programming field, code clone, also known as duplicate code or similar code, refers to two or more identical or similar source code segments. These similar code segments may be completely identical (i.e., full clone) or partially identical (i.e., partial clone). Code clone retrieval technology refers to retrieving code segments that are identical or similar to the target code segment from a code repository based on dimensions such as text, lexical, syntactic, and semantic analysis.
[0080] The application scenarios involved in the embodiments of this application will be introduced next.
[0081] In the context of computer software development, with the continuous development of large-parameter network models (also known as large models), AI-assisted R&D methods based on large models have gradually become popular, transforming the traditional software development process into an intelligent one. These AI-assisted R&D methods include code prediction methods, among others.
[0082] Taking code prediction methods based on large models as an example, during the code writing phase of software development, after a programmer inputs a portion of the code, these methods can predict subsequent code, thereby reducing the amount of code the programmer needs to input and improving software development efficiency. As another example, programmers develop software using an IDE, whose code editor has code prediction capabilities. If a programmer inputs part of the code in the code editor window, the editor can predict the code the programmer intends to input next. However, the accuracy of the predicted code using these methods is not high, meaning the final generated code may not meet the programmer's needs.
[0083] To improve the accuracy of code prediction, related technologies apply RAG to the field of code prediction. Please refer to Figure 1, which is a schematic diagram of a RAG-based code prediction method provided in an embodiment of this application. This method uses vectorized retrieval and / or text similarity retrieval to retrieve code segments from the code library that are identical or similar to the target code segment (for ease of description, code segments identical or similar to the target code segment will be referred to as similar code segments thereafter). The similar code and context code (i.e., the code entered by the programmer in the code editor window) are input as prompts into the model to obtain the code prediction results output by the model.
[0084] However, the aforementioned techniques determine similar code segments through vectorized retrieval or text similarity retrieval. Since vectorized retrieval can only retrieve similar codes from the dimension of implicit vector semantic similarity, and text similarity retrieval can only retrieve similar codes from the dimension of explicit text similarity, these two retrieval methods refer to fewer dimensions during retrieval, which makes the final retrieved similar codes potentially differ significantly from the target code segment, resulting in a lower accuracy of the final predicted code.
[0085] Based on this, the embodiments of this application provide a code prediction method. Since code clone retrieval can retrieve similar code from multiple dimensions such as text, lexical, syntactic and semantic, the difference between the similar code segment (i.e. the first type of code segment) determined from multiple candidate codes by code clone retrieval and the target code segment is small. Thus, the accuracy of retrieving similar code can be effectively improved, thereby ensuring that the accuracy of the obtained code prediction result is high.
[0086] The implementation environment involved in the embodiments of this application will be described next. Please refer to Figure 2, which is a schematic diagram of an implementation environment provided by an embodiment of this application. The implementation environment includes a retrieval unit 01 and a prediction unit 02.
[0087] The retrieval unit 01 is used to acquire the target code segment and perform code clone retrieval on multiple candidate code segments based on the target code segment to obtain the code clone retrieval result. The prediction unit 02 is used to predict the subsequent code of the target code segment based on the first type of code segment and the target code segment if the code clone retrieval result indicates that there is a first type of code segment among the multiple candidate code segments, so as to obtain the code prediction result.
[0088] In some embodiments, the code clone retrieval result indicates whether a first type of code segment similar to the target code segment exists among multiple candidate code segments. If the code clone retrieval result indicates that no first type of code segment similar to the target code segment exists among multiple candidate code segments, it means that no first type of code segment similar to the target code segment was retrieved from the multiple candidate code segments.
[0089] In some embodiments, the retrieval unit 01 can also perform vectorized retrieval on multiple candidate code segments based on the target code segment to obtain vectorized retrieval results.
[0090] In summary, the retrieval unit 01 is used to determine the retrieval results, which may include code clone retrieval results, or may include both code clone retrieval results and vectorized retrieval results.
[0091] In some embodiments, the prediction unit 02 can predict the subsequent code of the target code segment based on the retrieval results using a code prediction model to obtain the code prediction result.
[0092] In one possible implementation, prediction unit 02 includes an optimization subunit and a prediction subunit. The optimization subunit determines prediction suggestions based on the retrieval results. The prediction subunit inputs the prediction suggestions into the code prediction model to obtain the code prediction result output by the code prediction model.
[0093] For example, the optimization sub-unit determines a reference code segment based on the first type of code segment, and determines prediction prompt information based on the reference code segment and the target code segment.
[0094] It should be noted that the retrieval unit can be integrated as a functional module into the first execution entity, the optimization sub-unit can be integrated as a functional module into the second execution entity, and the prediction sub-unit can be integrated as a functional module into the third execution entity. The first, second, and third execution entities can be the same; alternatively, at least two of the first, second, and third execution entities can be different. In other words, the execution entities in this application embodiment can be one or more, and this application embodiment does not limit this. For example, the prediction unit includes the optimization sub-unit and the prediction sub-unit, as well as the retrieval unit, all integrated into execution entity 1. As another example, the optimization sub-unit and the retrieval unit are integrated into execution entity 1, and the prediction sub-unit is integrated into execution entity 2.
[0095] It should also be noted that when there are multiple execution entities in the embodiments of this application, data transmission is required between these multiple execution entities. For example, the optimization subunit and the retrieval unit are integrated in execution entity 1, and the prediction subunit is integrated in execution entity 2. In this case, execution entity 1 can also send the prediction prompt information to execution entity 2 after determining the prediction prompt information. Execution entity 2 can receive the prediction prompt information and, based on the prediction prompt information, predict the subsequent code of the target code segment using a code prediction model to obtain the code prediction result. As another example, the retrieval unit is integrated in execution entity 1, and the optimization subunit and the prediction subunit are jointly integrated in execution entity 2. In this case, execution entity 1 can also send the retrieval result to execution entity 2. Execution entity 2 can receive the data sent by execution entity 1 and, based on the data sent by execution entity 1, predict the subsequent code of the target code segment to obtain the code prediction result. After obtaining the code prediction result, execution entity 2 can also send the code prediction result back to execution entity 1.
[0096] The aforementioned first, second, and third executing entities can be electronic devices or cloud platforms, and this application embodiment does not limit this. The cloud platform can be a server cluster or distributed system composed of multiple physical servers, or it can be a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, content delivery networks (CDNs), and big data and artificial intelligence platforms, or a cloud computing service center.
[0097] In some embodiments, an electronic device integrating a retrieval unit has code editing capabilities, or a cloud platform integrating a retrieval unit can provide code editing services. For example, an electronic device integrating a retrieval unit may have an IDE deployed, allowing programmers to write code in the code editor included in the IDE. Alternatively, a cloud platform integrating a retrieval unit may provide a code editing page, allowing programmers to write code within that page.
[0098] It should be noted that the application scenarios and implementation environments described in the embodiments of this application are for the purpose of more clearly illustrating the technical solutions of the embodiments of this application, and do not constitute a limitation on the technical solutions provided in the embodiments of this application. As those skilled in the art will know, with the evolution of technology and the emergence of new application scenarios, the technical solutions provided in the embodiments of this application are also applicable to similar technical problems.
[0099] Please refer to Figure 3, which is a schematic diagram of the structure of an electronic device according to an embodiment of this application. The electronic device includes at least one processor 301, a communication bus 302, a memory 303, and at least one communication interface 304.
[0100] Processor 301 can be a general-purpose central processing unit (CPU), a network processor (NP), a microprocessor, or one or more integrated circuits for implementing the solutions of this application, such as application-specific integrated circuits (ASICs), programmable logic devices (PLDs), or combinations thereof. The aforementioned PLD can be a complex programmable logic device (CPLD), a field-programmable gate array (FPGA), generic array logic (GAL), or any combination thereof.
[0101] The communication bus 302 is used to transmit information between the aforementioned components. The communication bus 302 can be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, it is represented by only one thick line in the figure, but this does not indicate that there is only one bus or one type of bus.
[0102] The memory 303 may be a read-only memory (ROM), a random access memory (RAM), an electrically erasable programmable read-only memory (EEPROM), an optical disc (including a compact disc read-only memory (CD-ROM), a compressed optical disc, a laser disc, a digital versatile optical disc, a Blu-ray disc, etc.), a magnetic disk storage medium, or other magnetic storage devices, or any other medium capable of carrying or storing desired program code in the form of instructions or data structures that can be accessed by a computer, but not limited thereto. The memory 303 may exist independently and be connected to the processor 301 via a communication bus 302. Alternatively, the memory 303 may be integrated with the processor 301.
[0103] Communication interface 304 uses any transceiver-like device for communicating with other devices or communication networks. Communication interface 304 includes a wired communication interface and may also include a wireless communication interface. The wired communication interface may be, for example, an Ethernet interface. The Ethernet interface may be an optical interface, an electrical interface, or a combination thereof. The wireless communication interface may be a wireless local area network (WLAN) interface, a cellular network communication interface, or a combination thereof.
[0104] In a specific implementation, as one example, processor 301 may include one or more CPUs, such as CPU0 and CPU1 as shown in FIG3.
[0105] In a specific implementation, as one embodiment, the electronic device may include multiple processors, such as processor 301 and processor 305 as shown in FIG3. Each of these processors may be a single-core processor or a multi-core processor. Here, "processor" may refer to one or more devices, circuits, and / or processing cores for processing data (such as computer program instructions).
[0106] In a specific implementation, as one embodiment, the electronic device may further include an output device 306 and an input device 307. The output device 306 communicates with the processor 301 and can display information in various ways. For example, the output device 306 may be a liquid crystal display (LCD), a light-emitting diode (LED) display device, a cathode ray tube (CRT) display device, or a projector, etc. The input device 307 communicates with the processor 301 and can receive user input in various ways. For example, the input device 307 may be a mouse, a keyboard, a touchscreen device, or a sensing device, etc.
[0107] In some embodiments, memory 303 is used to store program code 310 for executing the scheme of this application, and processor 301 can execute program code 310 stored in memory 303. The program code 310 may include one or more software modules, and the electronic device can implement the code prediction method provided in the embodiment of FIG4 below through processor 301 and program code 310 in memory 303.
[0108] Based on the above description, the execution entity in the embodiments of this application can be one or more. When there is only one execution entity, it can be a cloud platform or an electronic device; when there are multiple execution entities, they can interact with each other. For example, the execution entities in the embodiments of this application can be a cloud platform and an electronic device, which can interact to implement the code prediction method provided in the embodiments of this application. The method provided in the embodiments of this application will be described in two separate embodiments.
[0109] Figure 4 is a flowchart of a code prediction method provided in an embodiment of this application. This embodiment uses an execution subject as an example, assuming the execution subject is an electronic device. Referring to Figure 4, the method includes the following steps.
[0110] Step 401: Based on the target code segment, perform code clone retrieval on multiple candidate code segments to obtain code clone retrieval results. The candidate code segment includes multiple code segments, and the definition of the candidate code segment is complete. The code clone retrieval results indicate whether there is a first-class code segment similar to the target code segment among the multiple candidate code segments. The target code segment includes at least one code segment.
[0111] In some embodiments, the electronic device stores multiple candidate codes in advance. In this case, it is possible to perform code clone retrieval on multiple candidate code segments based on the target code segment and obtain code clone retrieval results.
[0112] In one possible implementation, each candidate code segment is at the function level; that is, each candidate code segment indicates one fully defined function. In another possible implementation, each candidate code segment is at the code file level; that is, each candidate code segment indicates multiple fully defined functions. This application does not limit the granularity of the candidate code segments.
[0113] In some embodiments, during the code clone retrieval process, the electronic device can determine the length of the similar part between each candidate code segment and the target code segment, and then divide the length of the similar part by the length of the target code segment to obtain the similarity corresponding to each candidate code segment. Based on the similarity corresponding to multiple candidate code segments, the code clone retrieval result is determined.
[0114] In one possible implementation, if there is a candidate code segment among multiple candidate code segments with a similarity greater than a first similarity threshold, then that candidate code segment with a similarity greater than the first similarity threshold is identified as a first-class code segment. If there is no candidate code segment among multiple candidate code segments with a similarity greater than the first similarity threshold, then it is determined that there is no first-class code segment similar to the target code segment among the multiple candidate code segments, that is, no first-class code segment similar to the target code segment was retrieved from the multiple candidate code segments.
[0115] The first similarity threshold is preset and can be adjusted as needed under different circumstances. For further details regarding code clone retrieval, please refer to related technologies, such as SAGA code clone retrieval techniques; this application's embodiments will not elaborate on these aspects.
[0116] In some embodiments, before performing code clone retrieval on multiple candidate code segments based on the target code segment and obtaining the code clone retrieval results, the electronic device also needs to acquire the target code segment.
[0117] In one possible implementation, the target code segment is obtained from multiple code segments included in the code file based on the predicted position, which refers to the starting position of the code to be predicted in the code file.
[0118] In some embodiments, before retrieving the target code segment from multiple code segments included in the code file based on the predicted location, it is necessary to determine the predicted location and the code file. There are several ways to determine the predicted location and the code file; three of these methods will be described below.
[0119] In the first implementation, the electronic device has a code editing function, and the programmer writes code through the code editing function. In this case, the electronic device can periodically obtain the file of the code written by the programmer, and use the file as the code file. The position after and adjacent to the last character in the code file, or the position of the cursor in the code file, is used as the predicted position.
[0120] The second implementation responds to code prediction instructions, determining the code file and prediction location.
[0121] In some embodiments, the electronic device has a code editing function, through which a programmer writes code. In this case, the programmer can trigger a code prediction instruction, and the electronic device responds to the code prediction instruction triggered by the programmer by treating the file containing the code written by the programmer as a code file.
[0122] In some embodiments, the electronic device can determine the cursor position as the prediction position when the code prediction instruction is triggered. In other embodiments, after the programmer triggers the code prediction instruction, a position setting interface can be displayed. This position configuration interface is used to set the prediction position, and the programmer can trigger a setting completion instruction after setting the prediction position, thereby enabling the electronic device to determine the prediction position.
[0123] For example, a programmer can configure the location to set the start position of the 59th segment in the code file as the predicted position in the configuration interface.
[0124] In the third implementation, the programmer can upload the code file and the predicted location to the electronic device, which can then determine the code file and the predicted location.
[0125] In some embodiments, the process of obtaining the implementation of a target code segment from multiple code segments included in a code file based on the predicted location includes: determining the abstract syntax tree corresponding to the code file based on the multiple code segments included in the code file; determining the code segment of the function used to indicate the predicted location based on the abstract syntax tree corresponding to the code file; and obtaining the target code segment.
[0126] For a detailed explanation of the process of determining the abstract syntax tree corresponding to a code file based on the multiple code segments included in the code file, please refer to the relevant abstract syntax tree generation techniques. This application will not elaborate on this aspect in the embodiments.
[0127] In some embodiments, the abstract syntax tree corresponding to the code file includes multiple nodes and the type and position of each node. A node indicates a code element in multiple code segments, and the position of a node refers to the location of the code element it indicates within the multiple code segments. The multiple nodes include a root node. Thus, the process of obtaining the code segment indicating the predicted position from multiple code segments based on the abstract syntax tree, as the target code segment, includes: determining a first node closest to the predicted position from the multiple nodes; determining a second node from the abstract syntax tree based on the first node, where the second node is the node of type function that is closest to the first node on the path from the first node to the root node; and determining the target code segment based on the code element indicated by the second node and the third node, where the third node is the node connected to the second node and far from the root node.
[0128] It should be noted that the third node is any node that is directly or indirectly connected to the second node and is far away from the root node.
[0129] The process of determining the implementation of the target code segment based on the code elements indicated by the second and third nodes includes: determining all code segments composed of the code elements indicated by the second and third nodes, and determining the target code segment based on all code segments composed of the code elements indicated by the second and third nodes.
[0130] In some embodiments, the code segments corresponding to the code elements indicated by the second node and the third node in the code file are determined to be all code segments composed of the code elements indicated by the second node and the third node.
[0131] The abstract syntax tree also indicates semantic rules between multiple nodes. In this case, intermediate code corresponding to the code elements indicated by the second and third nodes can be generated based on the code elements indicated by the second and third nodes, the semantic rules between the second and third nodes, and the syntax rules. The intermediate code includes multiple code segments, and the intermediate code is determined to be all code segments composed of the code elements indicated by the second and third nodes.
[0132] Because code segments in a code file may contain redundant elements unrelated to the code's function, such as comments, whitespace, punctuation marks, and delimiters, while intermediate code is functionally identical to the source code but removes many redundant elements from the source code (i.e., the multiple code segments included in the code file), intermediate code is more concise and refined than the source code. By identifying intermediate code as all code segments composed of code elements indicated by the second and third nodes, the efficiency and accuracy of subsequent retrieval can be effectively improved.
[0133] For a detailed explanation of the process of generating intermediate code corresponding to the code elements indicated by the second and third nodes, based on the code elements indicated by the second and third nodes, the semantic rules between the second and third nodes, and the syntax rules, please refer to the relevant intermediate code generation technology. This application embodiment will not elaborate on this.
[0134] In some embodiments, all code segments consisting of the code elements indicated by the second and third nodes can be identified as the target code segment. In other embodiments, the target code segment is identified as the N consecutive code segments preceding the predicted position in all code segments consisting of the code elements indicated by the second and third nodes, where N is an integer greater than or equal to 1.
[0135] Of course, besides determining the target code segment through the two methods mentioned above, other methods can also be used to determine the target code segment, and this application embodiment does not limit this. For example, if there is at least one code segment after the predicted position in all code segments composed of the code elements indicated by the second node and the third node, the consecutive N1 code segments before the predicted position and the consecutive N2 code segments after the predicted position in all code segments composed of the code elements indicated by the second node and the third node can also be determined as the target code segment.
[0136] In some embodiments, besides obtaining the target code segment from multiple code segments included in the code file through an abstract syntax tree, the target code segment can also be directly determined from the M consecutive code segments preceding the prediction position, where M is an integer greater than or equal to 1. Of course, if there is at least one code segment after the prediction position in the code file, the target code segment can also be determined from the M1 consecutive code segments preceding the prediction position and the M2 consecutive code segments following the prediction position in the multiple code segments included in the code file; this embodiment does not limit this approach.
[0137] Among them, M, M1, M2, N, N1, and N2 are all preset, and can be adjusted according to different needs under different circumstances.
[0138] In some embodiments, besides obtaining the target code segment from multiple code segments included in the code file based on predicted location, the target code segment can also be obtained through other means, which are not limited in this application embodiment. For example, the electronic device can receive at least one piece of code uploaded by a programmer, and then determine the at least one piece of code as the target code segment. As another example, the electronic device has a code editing function, and the programmer can write at least one piece of code on the electronic device. In this case, the electronic device can directly use the written at least one piece of code as the target code segment.
[0139] Step 402: If the code clone retrieval results indicate that there is a first type of code segment among multiple candidate code segments, then based on the first type of code segment and the target code segment, predict the subsequent code of the target code segment to obtain the code prediction result.
[0140] Based on the first type of code segment and the target code segment, prediction prompts are determined. These prompts are used to guide the code prediction process. The prediction prompts are then input into the code prediction model to obtain the code prediction results output by the model.
[0141] The following steps (1)-(2) will be used to describe the process of determining the prediction prompt information based on the first type of code segment and the target code segment.
[0142] Step (1): Based on the first type of code segment, determine the reference code segment.
[0143] In some embodiments, the first type of code segment is identified as a reference code segment.
[0144] In other embodiments, before determining the reference code segment based on the first type of code segment, the electronic device may also perform a vectorized search on multiple candidate code segments based on the target code segment to obtain vectorized search results. The vectorized search results indicate whether there is a second type of code segment among the multiple candidate code segments that is similar to the target code segment. In this case, if the vectorized search results indicate that there is a second type of code segment among the multiple candidate code segments, the reference code segment is determined based on the first type of code segment and the second type of code segment.
[0145] Based on the target code segment, vectorized retrieval is performed on multiple candidate code segments. For detailed implementation methods of obtaining vectorized retrieval results, please refer to relevant technologies. This application embodiment will not elaborate on this aspect.
[0146] As an example, multiple candidate code segments and the target code segment can be vectorized and encoded using a vectorization model to obtain vectors corresponding to the multiple candidate code segments and the target code segment, respectively, as output by the vectorization model. Then, based on the vectors corresponding to the multiple candidate code segments and the target code segment, the similarity between the multiple candidate code segments and the target code segment can be determined according to relevant algorithms. Based on the similarity between the multiple candidate code segments and the target code segment, the vectorized retrieval result can be determined.
[0147] In some embodiments, the similarity between multiple candidate code segments and the target code segment can be determined based on the vectors corresponding to multiple candidate code segments and the vector corresponding to the target code segment, using similarity algorithms such as cosine similarity and Euclidean distance.
[0148] In one possible implementation, if there is a candidate code segment among multiple candidate code segments with a similarity greater than a second similarity threshold, then that candidate code segment with a similarity greater than the second similarity threshold is identified as a second type of code segment. If there is no candidate code segment among multiple candidate code segments with a similarity greater than the second similarity threshold, then it is determined that there is no second type of code segment similar to the target code segment among the multiple candidate code segments, that is, no second type of code segment similar to the target code segment was retrieved from the multiple candidate code segments.
[0149] The aforementioned vectorization models include, but are not limited to, the MiniLM model. The second similarity threshold is preset and can be adjusted as needed under different circumstances.
[0150] In some embodiments, the code clone retrieval results also include the similarity scores of each code segment in the first type of code segments, and the vectorized retrieval results also include the similarity scores of each code segment in the second type of code segments. The first type of code segments has a higher priority than the second type of code segments, and the similarity score indicates the degree of similarity between the corresponding code segment and the target code segment. In this case, the process of determining the implementation of the reference code segment based on the first type of code segments and the second type of code segments includes: filtering the first type of code segments and the second type of code segments based on the similarity scores of each code segment in the first type of code segments, the similarity scores of each code segment in the second type of code segments, and the priority relationship between the first type of code segments and the second type of code segments to obtain the reference code segment.
[0151] Since the first type of code segment is a code segment that is similar to the target code segment in multiple dimensions, while the second type of code segment is a code segment that is similar to the target code segment only in the dimension of implicit vector semantic similarity, the priority of each code segment in the first type of code segment is greater than the priority of each code segment in the second type of code segment.
[0152] Based on the similarity of each code segment in the first type of code segment, the code segments in the first type of code segment are sorted to obtain a first sorting result. Based on the similarity of each code segment in the second type of code segment, the code segments in the second type of code segment are sorted to obtain a second sorting result. Based on the priority relationship between the first type of code segment and the second type of code segment, the first sorting result and the second sorting result are concatenated. Based on the concatenated sorting result, the first type of code segment and the second type of code segment are filtered to obtain a reference code segment.
[0153] For example, the code segments in the first category are sorted in descending order of similarity to obtain a first sorting result. Of course, they can also be sorted in ascending order of similarity to obtain a first sorting result. Similarly, for the second category of code segments, they can be sorted in descending or ascending order of similarity to obtain a second sorting result. This embodiment of the application does not limit this approach.
[0154] In some embodiments, the sorting method of the first type of code segments is the same as that of the second type of code segments. If both the first sorting result and the second sorting result are obtained by sorting in descending order of similarity, the first sorting result with higher priority is concatenated before the second sorting result to obtain the concatenated sorting result. In the concatenated sorting result, the code segment with the lowest similarity in the first type of code segments is placed before the code segment with the highest similarity in the second type of code segments.
[0155] If both the first and second sorting results are obtained by sorting in ascending order of similarity, then the first sorting result with higher priority is appended to the second sorting result to obtain the appended sorting result. In the appended sorting result, the code segment with the highest similarity in the second category of code segments is placed before the code segment with the lowest similarity in the first category of code segments.
[0156] In some embodiments, if both the first and second sorting results are obtained by sorting in descending order of similarity, then the first X1 percent of code segments in the concatenated sorting result, or the first Y1 code segments in the concatenated sorting result, are used as reference code segments. If both the first and second sorting results are obtained by sorting in ascending order of similarity, then the last X1 percent of code segments in the concatenated sorting result, or the last Y1 code segments in the concatenated sorting result, are used as reference code segments.
[0157] In some embodiments, besides concatenating the first sorting result and the second sorting result to obtain the reference code segment, other implementation methods can be used to determine the reference code segment, and this application embodiment does not limit this. For example, the first type of code segment and the second type of code segment can be directly determined as the reference code segment. Another example is that, based on the first sorting result, the first type of code segment is filtered to obtain at least one first code segment, and based on the second sorting result, the second type of code segment is filtered to obtain at least one second code segment; both the at least one first code segment and the at least one second code segment are then determined as reference code segments.
[0158] If the first sorting result is obtained by sorting in descending order of similarity, then the top X2% of code segments in the first sorting result, or the top Y2% of code segments in the first sorting result, are determined as at least one first code segment. If the first sorting result is obtained by sorting in ascending order of similarity, then the bottom X2% of code segments in the first sorting result, or the bottom Y2% of code segments in the first sorting result, are determined as at least one first code segment.
[0159] If the second ranking result is obtained by sorting in descending order of similarity, then the top X3% or top Y3% code segments in the second ranking result are identified as at least one second code segment. If the second ranking result is obtained by sorting in ascending order of similarity, then the bottom X3% or bottom Y3% code segments in the second ranking result are identified as at least one second code segment.
[0160] Among them, X1, Y1, X2, Y2, X3, and Y3 are all preset, and can be adjusted according to different needs under different circumstances.
[0161] Step (2): Determine the prediction prompt information based on the reference code segment and the target code segment.
[0162] In some embodiments, the prediction prompt information includes any of the following: a reference code segment and a target code segment; a reference code segment, a target code segment, and other code segments in a code file except for the target code segment; the priority of the reference code segment, the target code segment, the priority of the reference code segment, and the priority of the target code segment; the priority of the reference code segment, the target code segment, the priority of the reference code segment, the priority of the target code segment, other code segments in a code file except for the target code segment, and the priority of other code segments.
[0163] Where the reference code segment includes at least one code segment, the priority of the target code segment is greater than the priority of each code segment within the at least one code segment, the priority of each code segment within the at least one code segment is greater than the priority of other code segments, and the priority of the at least one code segment is proportional to its corresponding similarity, where similarity indicates the degree of similarity between the corresponding code segment and the target code segment. If the reference code segment includes a first type of code segment and a second type of code segment, then the priority of any code segment in the first type of code segment is greater than the priority of any code segment in the second type of code segment.
[0164] This application embodiment incorporates the priority relationship between code segments as part of the prediction hint information, and then performs code prediction based on this prediction hint information. Compared with the scheme that only uses similar code and context code as prediction information, this application embodiment achieves accurate and comprehensive guidance for the code prediction process by carrying prediction hint information that includes the priority relationship between code segments, thereby making the final code prediction result more accurate.
[0165] In some embodiments, the predicted prompt information can also be determined based on the reference code segment, the target code segment, other code segments in the code file besides the target code segment, the priority relationship between the code segments, and the prompt information length. The priority relationship between the code segments indicates the priority relationship between the reference code segment, the target code segment, and other code segments, and the prompt information length indicates the upper limit of the predicted prompt information length.
[0166] The target code segment, reference code segment, and other code segments are concatenated in descending order of priority to obtain the concatenated code segment. If the length of the concatenated code segment is less than or equal to the length of the prompt message, the concatenated code segment is determined as the predicted prompt message. If the length of the concatenated code segment is greater than the length of the prompt message, the concatenated code segment is truncated to the length of the prompt message to obtain the truncated code segment, which is equal to the length of the prompt message.
[0167] In some embodiments, if the length of the concatenated code segment is less than or equal to the length of the prompt message, the priority of each code segment in the concatenated code segment is also used as the predicted prompt message. If the length of the concatenated code segment is greater than the length of the prompt message, the priority of each code segment in the truncated code segment is also used as the predicted prompt message.
[0168] This application embodiment concatenates code segments based on their priority relationships and the length of the hint information. This ensures that higher-priority code segments are less likely to be truncated, making them more likely to be used as prediction hint information. Furthermore, the order of the code segments in the prediction hint information also reflects the priority relationships between them. Compared to schemes that only use similar code and context code as prediction information, this application embodiment can reflect the priority relationships between code segments through the order of the code segments in the prediction hint information, thereby providing accurate and comprehensive guidance for code prediction and resulting in more accurate code prediction results.
[0169] In some embodiments, if the code clone retrieval result indicates that there is no first type of code segment among multiple candidate code segments, and the vectorization retrieval result indicates that there is a second type of code segment among multiple candidate code segments, then the subsequent code of the target code segment is predicted based on the second type of code segment and the target code segment to obtain the code prediction result.
[0170] In one possible implementation, if the code clone retrieval results indicate that there is no first-type code segment among the multiple candidate code segments, then a vectorized retrieval is performed on the multiple candidate code segments based on the target code segment to obtain a vectorized retrieval result. If the vectorized retrieval results indicate that there is a second-type code segment among the multiple candidate code segments, then the subsequent code of the target code segment is predicted based on the second-type code segment and the target code segment to obtain a code prediction result.
[0171] For details regarding vectorized retrieval, please refer to the relevant content in the embodiments of this application; it will not be repeated here.
[0172] In some embodiments, prediction prompts are determined based on the second type of code segment and the target code segment, and the prediction prompts are input into the code prediction model to obtain the code prediction results output by the code prediction model.
[0173] In some embodiments, the second type of code segment and the target code segment can be identified as prediction hints. In other embodiments, the second type of code segment, the target code segment, and other code segments in the code file besides the target code segment can also be identified as prediction hints. In other embodiments, the second type of code segment, the target code segment, the priority of the second type of code segment, and the priority of the target code segment are identified as prediction hints. In other embodiments, the second type of code segment, the target code segment, the priority of the second type of code segment, the priority of the target code segment, other code segments in the code file besides the target code segment, and the priorities of other code segments are identified as prediction hints.
[0174] It should be noted that the priority of each code segment in the second type of code segment mentioned above is proportional to its corresponding similarity, and the similarity indicates the degree of similarity between the corresponding code segment and the target code segment.
[0175] In some embodiments, the code prediction model described above can be a large language model, such as the BERT model, GPT model, T5 model, etc., and this application does not limit it.
[0176] Because code clone retrieval can search for similar code from multiple dimensions such as text, lexical, syntactic, and semantic dimensions, the similar code segments (i.e., first-class code segments) identified from multiple candidate codes through code clone retrieval have small differences from the target code segment. This effectively improves the accuracy of similar code retrieval, thereby ensuring a high accuracy of the obtained code prediction results. Furthermore, this embodiment incorporates the priority relationship between code segments as part of the prediction hint information, and then performs code prediction based on this hint information. Compared to schemes that only use similar code and context code as prediction information, this embodiment provides accurate and comprehensive guidance for the code prediction process by carrying prediction hint information that includes the priority relationship between code segments, thus making the final code prediction results more accurate. This application embodiment can also concatenate various code segments according to the priority relationship between code segments and the length of the prompt information, thereby ensuring that the higher priority code segment is less likely to be truncated, making the higher priority code segment more likely to be used as the prediction prompt information. Furthermore, the order of each code segment in the prediction prompt information can also reflect the priority relationship between code segments. Compared with the scheme that only uses similar code and context code as prediction information, this application embodiment can reflect the priority relationship between code segments by the order of code segments in the prediction prompt information, thereby achieving accurate and comprehensive guidance for code prediction, and making the final code prediction result more accurate.
[0177] Figure 5 is a flowchart of another code prediction method provided in an embodiment of this application. This embodiment uses two execution entities as an example, assuming the two execution entities are an electronic device and a cloud platform. Referring to Figure 5, the method includes the following steps.
[0178] Step 501: The electronic device performs code clone retrieval on multiple candidate code segments based on the target code segment to obtain code clone retrieval results. The candidate code segment includes multiple code segments and has a complete definition. The code clone retrieval results indicate whether there are first-type code segments similar to the target code segment among the multiple candidate code segments. The code clone retrieval results are sent to the cloud platform, or prediction prompt information is sent to the cloud platform. The prediction prompt information is determined based on the first-type code segments and the target code segment that exist among the multiple candidate code segments.
[0179] For a detailed explanation of the implementation process of obtaining the target code segment, the detailed implementation process of performing code clone retrieval on multiple candidate code segments based on the target code segment to obtain the code clone retrieval results, and the detailed implementation process of determining the prediction prompt information of the first type of code segment and the target code segment, please refer to the relevant content in the embodiment corresponding to Figure 4, which will not be repeated here.
[0180] In some embodiments, when an electronic device sends code clone retrieval results to a cloud platform, the electronic device can encapsulate the code clone retrieval results into JavaScript object notation (JSON) format and send it to the cloud platform.
[0181] In some embodiments, the electronic device can also encapsulate the target code segment into JSON format, encapsulate other code segments in the code file besides the target code segment into JSON format, and send them to the cloud platform.
[0182] In some embodiments, if the code clone retrieval results indicate that there is no first-type code segment among multiple candidate code segments, the electronic device may not send the code clone retrieval to the cloud platform.
[0183] In some embodiments, the electronic device can also perform vectorized retrieval on multiple candidate code segments based on the target code segment to obtain vectorized retrieval results. These vectorized retrieval results indicate whether there is a second type of code segment similar to the target code segment among the multiple candidate code segments, and send the vectorized retrieval results to the cloud platform.
[0184] Based on the target code segment, vectorized retrieval is performed on multiple candidate code segments to obtain the detailed implementation process of the vectorized retrieval results. Please refer to the relevant content in the embodiment corresponding to Figure 4, which will not be repeated here.
[0185] In some embodiments, after determining the vectorized search results, the electronic device can also encapsulate the vectorized search results into JSON format and send them to the cloud platform.
[0186] It should be noted that the code clone search results, vectorized search results, target code segment, and other code segments in the code file (excluding the target code segment) can be encapsulated separately to obtain multiple JSON format data pairs. Alternatively, at least two data pairs from the code clone search results, vectorized search results, target code segment, and other code segments can be encapsulated within the same JSON format data pair. This application does not limit this approach. If at least two data pairs from the code clone search results, vectorized search results, target code segment, and other code segments are encapsulated within the same JSON format data pair, these at least two data pairs can reside in different fields of the JSON format data pair.
[0187] In other embodiments, when the electronic device sends the prediction prompt information to the cloud platform, the electronic device can encapsulate the prediction prompt information into JSON format and send it to the cloud platform.
[0188] In some embodiments, the electronic device can send a code prediction request to a cloud platform, which instructs the cloud platform to predict subsequent code for a target code segment. For example, the electronic device can encapsulate the code prediction request in JSON format and send it to the cloud platform.
[0189] In one possible implementation, when the electronic device sends the code clone retrieval results to the cloud platform, the code prediction request may carry at least one of the following: the code clone retrieval results, the target code segment, the vectorized retrieval results, and other code segments in the code file besides the target code segment. When the electronic device sends prediction hints to the cloud platform, the code prediction request may also carry prediction hints.
[0190] Step 502: The cloud platform receives the code clone retrieval results or prediction prompts sent by the electronic device; if the code clone retrieval results indicate that there is a first type of code segment among multiple candidate code segments, then the subsequent code of the target code segment is predicted based on the first type of code segment and the target code segment to obtain the code prediction result; if the prediction prompts are received, the subsequent code of the target code segment is predicted based on the prediction prompts to obtain the code prediction result.
[0191] Upon receiving the code clone retrieval results, the cloud platform determines prediction prompts based on the first type of code segment and the target code segment. These prediction prompts guide the code prediction process. The prediction prompts are then input into the code prediction model to obtain the code prediction results output by the model.
[0192] In one possible implementation, the cloud platform determines a reference code segment based on the first type of code segment, and then determines the prediction prompt information based on the reference code segment and the target code segment.
[0193] In some embodiments, the cloud platform can also receive vectorized search results sent by electronic devices. If the vectorized search results indicate that a second type of code segment exists among multiple candidate code segments, a reference code segment is determined based on the first type of code segment and the second type of code segment.
[0194] For a detailed explanation of the implementation process of determining the reference code segment based on the first and second type of code segments, please refer to the relevant content in the embodiment corresponding to Figure 4, which will not be repeated here.
[0195] Upon receiving a prediction prompt, the cloud platform can input the prediction prompt into the code prediction model and obtain the code prediction result output by the code prediction model.
[0196] In some embodiments, if the code clone retrieval result indicates that there is no first type of code segment among multiple candidate code segments, and the vectorization retrieval result indicates that there is a second type of code segment among multiple candidate code segments, then the subsequent code of the target code segment is predicted based on the second type of code segment and the target code segment to obtain the code prediction result.
[0197] Based on the second type of code segment and the target code segment, the subsequent code of the target code segment is predicted to obtain the code prediction result. For the detailed implementation process, please refer to the relevant content in the embodiment corresponding to Figure 4, which will not be repeated here.
[0198] In some embodiments, after obtaining the code prediction result on the cloud platform, the code prediction result can also be sent to an electronic device, which can receive the code prediction result.
[0199] Next, the code prediction method provided in the embodiments of this application will be described again with reference to Figure 6.
[0200] Referring to Figure 6, this electronic device is equipped with an IDE, allowing programmers to write code in the code editor included in the IDE. In this scenario, the programmer writes code in the code editor and triggers a code prediction instruction. The electronic device responds to this instruction, using the file containing the programmer's code as the code file and determining the cursor position at the time the prediction instruction is triggered as the prediction location. Based on the multiple code segments within the code file, the electronic device determines the corresponding abstract syntax tree (AST) of the code file. Based on the AST, it identifies the code segment representing the function used to indicate the prediction location, thus obtaining the target code segment. Based on the target code segment, it performs code cloning retrieval on multiple candidate code segments, obtaining code cloning retrieval results. These candidate code segments are granular at the function level. Finally, based on the target code segment, it performs vectorized retrieval on the multiple candidate code segments, obtaining vectorized retrieval results. The electronic device encapsulates the code clone retrieval results, vectorized retrieval results, target code segment, and other code in the code file (excluding the target code segment) into JSON format and sends it to the cloud platform. The cloud platform receives the data from the electronic device and, based on the reference code segment, target code segment, other code segments, the priority relationships between code segments, and the length of the prompt message, determines the prediction prompt message. This prediction prompt message is then input into the code prediction model to obtain the code prediction result output by the model. After obtaining the code prediction result, the cloud platform can also send it back to the electronic device, which can receive and return the result to the code editor.
[0201] Because code clone retrieval can search for similar code from multiple dimensions such as text, lexical, syntactic, and semantic dimensions, the similar code segments (i.e., first-class code segments) identified from multiple candidate codes through code clone retrieval have small differences from the target code segment. This effectively improves the accuracy of similar code retrieval, thereby ensuring a high accuracy of the obtained code prediction results. Furthermore, this embodiment incorporates the priority relationship between code segments as part of the prediction hint information, and then performs code prediction based on this hint information. Compared to schemes that only use similar code and context code as prediction information, this embodiment provides accurate and comprehensive guidance for the code prediction process by carrying prediction hint information that includes the priority relationship between code segments, thus making the final code prediction results more accurate. This application embodiment can also concatenate various code segments according to the priority relationship between code segments and the length of the prompt information, thereby ensuring that the higher priority code segment is less likely to be truncated, making the higher priority code segment more likely to be used as the prediction prompt information. Furthermore, the order of each code segment in the prediction prompt information can also reflect the priority relationship between code segments. Compared with the scheme that only uses similar code and context code as prediction information, this application embodiment can reflect the priority relationship between code segments by the order of code segments in the prediction prompt information, thereby achieving accurate and comprehensive guidance for code prediction, and making the final code prediction result more accurate.
[0202] Figure 7 is a schematic diagram of a code prediction device provided in an embodiment of this application. This code prediction device can be implemented by software, hardware, or a combination of both, and can be part or all of the aforementioned cloud platform or electronic device. Referring to Figure 7, the device includes: a first retrieval module 701 and a first prediction module 702.
[0203] The first retrieval module 701 is used to perform code clone retrieval on multiple candidate code segments based on the target code segment, and obtain code clone retrieval results. The candidate code segment includes multiple code segments, the definition of the candidate code segment is complete, and the code clone retrieval results indicate whether there is a first-class code segment similar to the target code segment among the multiple candidate code segments. For detailed implementation process, please refer to the corresponding content in the above embodiments, which will not be repeated here.
[0204] The first prediction module 702 is used to predict the subsequent code of the target code segment based on the first type of code segment and the target code segment if the code clone retrieval result indicates that a first type of code segment exists among multiple candidate code segments, so as to obtain the code prediction result. For detailed implementation processes, please refer to the corresponding content in the above embodiments, which will not be repeated here.
[0205] In some embodiments, the first type of code segment refers to a code segment among multiple candidate code segments that is similar to the target code segment in the dimensions of text, lexical, syntactic and semantic dimensions.
[0206] In some embodiments, the first prediction module 702 is specifically used for:
[0207] Based on the first type of code segment and the target code segment, prediction prompts are determined, and these prompts are used to guide the code prediction process.
[0208] Input the prediction prompts into the code prediction model to obtain the code prediction results output by the code prediction model.
[0209] In some embodiments, the first prediction module 702 is specifically used for:
[0210] Based on the first type of code segment, determine the reference code segment;
[0211] Based on the reference code segment and the target code segment, the prediction prompt information is determined.
[0212] In some embodiments, the device further includes:
[0213] The second retrieval module is used to perform vectorized retrieval on multiple candidate code segments based on the target code segment, and obtain vectorized retrieval results. These vectorized retrieval results indicate whether there are second-type code segments similar to the target code segment among the multiple candidate code segments.
[0214] The first prediction module 702 is specifically used for:
[0215] If the vectorized retrieval results indicate the existence of a second type of code segment among multiple candidate code segments, then a reference code segment is determined based on the first and second type of code segments.
[0216] In some embodiments, the code clone retrieval results also include the similarity of each code segment in the first type of code segment, and the vectorized retrieval results also include the similarity of each code segment in the second type of code segment, wherein the first type of code segment has a higher priority than the second type of code segment, and the similarity indicates the degree of similarity between the corresponding code segment and the target code segment;
[0217] The first prediction module 702 is specifically used for:
[0218] Based on the similarity between each code segment in the first type of code segment, the similarity between each code segment in the second type of code segment, and the priority relationship between the first type of code segment and the second type of code segment, the first type of code segment and the second type of code segment are filtered to obtain reference code segments.
[0219] In some embodiments, the first prediction module 702 is specifically used for:
[0220] Based on the reference code segment, the target code segment, other code segments in the code file besides the target code segment, the priority relationship between code segments, and the length of the prompt message, the predicted prompt message is determined. The priority relationship between code segments indicates the priority relationship between the reference code segment, the target code segment, and other code segments, and the length of the prompt message indicates the upper limit of the predicted prompt message length.
[0221] In some embodiments, where the reference code segment includes at least one code segment, the priority of the target code segment is higher than the priority of each code segment in the at least one code segment, the priority of each code segment in the at least one code segment is higher than the priority of other code segments, and the priority of each code segment in the at least one code segment is proportional to its corresponding similarity, which indicates the degree of similarity between the corresponding code segment and the target code segment.
[0222] In some embodiments, the prediction prompt information includes any of the following:
[0223] Reference code segment and target code segment;
[0224] Reference code segment, target code segment, and other code segments in the code file besides the target code segment;
[0225] Reference code segment, target code segment, priority of reference code segment, and priority of target code segment;
[0226] Reference code segment, target code segment, priority of reference code segment, priority of target code segment, priority of other code segments in the code file other than the target code segment, and priority of other code segments.
[0227] In some embodiments, the device further includes:
[0228] The second prediction module is used to predict the subsequent code of the target code segment based on the second type of code segment and the target code segment if the code clone retrieval result indicates that there is no first type of code segment among multiple candidate code segments, and the vectorization retrieval result indicates that there is a second type of code segment among multiple candidate code segments, so as to obtain the code prediction result.
[0229] In some embodiments, the apparatus further includes:
[0230] The acquisition module is used to obtain the target code segment from multiple code segments included in the code file based on the predicted position. The predicted position refers to the starting position of the code to be predicted in the code file.
[0231] In some embodiments, the acquisition module is specifically used for:
[0232] Based on the multiple code segments included in the code file, determine the abstract syntax tree corresponding to the code file;
[0233] Based on the abstract syntax tree, the code segment used to indicate the prediction location is determined, and the target code segment is obtained.
[0234] In some embodiments, the abstract syntax tree includes multiple nodes and the type and position of each node, where a node indicates a code element in multiple code segments, and the position of a node refers to the location of the code element indicated by the node in the multiple code segments, and the multiple nodes include a root node;
[0235] The acquisition module is specifically used for:
[0236] Determine the first node closest to the predicted location from among multiple nodes;
[0237] Based on the first node, the second node is determined from the abstract syntax tree. The second node is the node that is closest to the first node and is of type function in the path from the first node to the root node.
[0238] The target code segment is determined based on the code elements indicated by the second and third nodes. The third node is a node that is connected to the second node and is far away from the root node.
[0239] In some embodiments, the acquisition module is specifically used for:
[0240] All code segments consisting of the code elements indicated by the second and third nodes are identified as the target code segments.
[0241] In some embodiments, the acquisition module is specifically used for:
[0242] The target code segment is determined from all code segments consisting of the code elements indicated by the second and third nodes, where N is an integer greater than or equal to 1.
[0243] In some embodiments, the acquisition module is specifically used for:
[0244] The target code segment is determined from the M consecutive code segments located before the prediction position in the multiple code segments, where M is an integer greater than or equal to 1.
[0245] Because code clone retrieval can search for similar code from multiple dimensions such as text, lexical, syntactic, and semantic dimensions, the similar code segments (i.e., first-class code segments) identified from multiple candidate codes through code clone retrieval have small differences from the target code segment. This effectively improves the accuracy of similar code retrieval, thereby ensuring a high accuracy of the obtained code prediction results. Furthermore, this embodiment incorporates the priority relationship between code segments as part of the prediction hint information, and then performs code prediction based on this hint information. Compared to schemes that only use similar code and context code as prediction information, this embodiment provides accurate and comprehensive guidance for the code prediction process by carrying prediction hint information that includes the priority relationship between code segments, thus making the final code prediction results more accurate. This application embodiment can also concatenate various code segments according to the priority relationship between code segments and the length of the prompt information, thereby ensuring that the higher priority code segment is less likely to be truncated, making the higher priority code segment more likely to be used as the prediction prompt information. Furthermore, the order of each code segment in the prediction prompt information can also reflect the priority relationship between code segments. Compared with the scheme that only uses similar code and context code as prediction information, this application embodiment can reflect the priority relationship between code segments by the order of code segments in the prediction prompt information, thereby achieving accurate and comprehensive guidance for code prediction, and making the final code prediction result more accurate.
[0246] It should be noted that the code prediction device provided in the above embodiments is only illustrated by the division of the above functional modules. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. In addition, the code prediction device and the code prediction method embodiments provided in the above embodiments belong to the same concept, and the specific implementation process can be found in the method embodiments, which will not be repeated here.
[0247] Figure 8 is a schematic diagram of a code prediction device provided in an embodiment of this application. This code prediction device can be implemented as part or all of the aforementioned electronic device by software, hardware, or a combination of both. Referring to Figure 8, the device includes: a first retrieval module 801 and a first sending module 802.
[0248] The first retrieval module 801 is used to perform code clone retrieval on multiple candidate code segments based on the target code segment, and obtain code clone retrieval results. The candidate code segment includes multiple code segments, the definition of the candidate code segment is complete, and the code clone retrieval results indicate whether there is a first-class code segment similar to the target code segment among the multiple candidate code segments. For detailed implementation process, please refer to the corresponding content in the above embodiments, which will not be repeated here.
[0249] The first sending module 802 is used to send the code clone retrieval results to the cloud platform, enabling the cloud platform to predict the subsequent code of the target code segment based on the first type of code segment and the target code segment when the code clone retrieval results indicate the presence of a first type of code segment among multiple candidate code segments; or, to send prediction prompt information to the cloud platform, enabling the cloud platform to predict the subsequent code of the target code segment based on the prediction prompt information, wherein the prediction prompt information is determined based on the first type of code segment and the target code segment present among multiple candidate code segments. Detailed implementation processes are described in the corresponding contents of the above embodiments and will not be repeated here.
[0250] In some embodiments, the device further includes:
[0251] The second retrieval module is used to perform vectorized retrieval on multiple candidate code segments based on the target code segment, and obtain vectorized retrieval results. These vectorized retrieval results indicate whether there are second-type code segments similar to the target code segment among the multiple candidate code segments.
[0252] The second sending module is used to send the vectorized retrieval results to the cloud platform.
[0253] Because code clone retrieval can search for similar code from multiple dimensions such as text, lexical, syntactic, and semantic dimensions, the similar code segments (i.e., first-class code segments) identified from multiple candidate codes through code clone retrieval have small differences from the target code segment. This effectively improves the accuracy of similar code retrieval, thereby ensuring a high accuracy of the obtained code prediction results. Furthermore, this embodiment incorporates the priority relationship between code segments as part of the prediction hint information, and then performs code prediction based on this hint information. Compared to schemes that only use similar code and context code as prediction information, this embodiment provides accurate and comprehensive guidance for the code prediction process by carrying prediction hint information that includes the priority relationship between code segments, thus making the final code prediction results more accurate. This application embodiment can also concatenate various code segments according to the priority relationship between code segments and the length of the prompt information, thereby ensuring that the higher priority code segment is less likely to be truncated, making the higher priority code segment more likely to be used as the prediction prompt information. Furthermore, the order of each code segment in the prediction prompt information can also reflect the priority relationship between code segments. Compared with the scheme that only uses similar code and context code as prediction information, this application embodiment can reflect the priority relationship between code segments by the order of code segments in the prediction prompt information, thereby achieving accurate and comprehensive guidance for code prediction, and making the final code prediction result more accurate.
[0254] It should be noted that the code prediction device provided in the above embodiments is only illustrated by the division of the above functional modules. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. In addition, the code prediction device and the code prediction method embodiments provided in the above embodiments belong to the same concept, and the specific implementation process can be found in the method embodiments, which will not be repeated here.
[0255] Figure 9 is a schematic diagram of a code prediction device provided in an embodiment of this application. This code prediction device can be implemented as part or all of a cloud platform by software, hardware, or a combination of both. Referring to Figure 9, the device includes: a first receiving module 901 and a first prediction module 902.
[0256] The first receiving module 901 is used to receive code clone search results or prediction prompts sent by an electronic device. The code clone search results are obtained by cloning multiple candidate code segments based on the target code segment. Each candidate code segment includes multiple code segments and has a complete definition. The code clone search results indicate whether there is a first-type code segment similar to the target code segment among the multiple candidate code segments. The prediction prompts are determined based on the first-type code segments present among the multiple candidate code segments and the target code segment. Detailed implementation processes are described in the corresponding contents of the above embodiments and will not be repeated here.
[0257] The first prediction module 902 is used to, upon receiving a code clone retrieval result, if the code clone retrieval result indicates that a first type of code segment exists among multiple candidate code segments, then predict the subsequent code of the target code segment based on the first type of code segment and the target code segment to obtain a code prediction result; and upon receiving prediction prompt information, predict the subsequent code of the target code segment based on the prediction prompt information to obtain a code prediction result. Detailed implementation processes are described in the corresponding contents of the above embodiments and will not be repeated here.
[0258] In some embodiments, the first prediction module 902 is specifically used for:
[0259] Based on the first type of code segment and the target code segment, prediction prompts are determined, and these prompts are used to guide the code prediction process.
[0260] Input the prediction prompts into the code prediction model to obtain the code prediction results output by the code prediction model.
[0261] In some embodiments, the first prediction module 902 is specifically used for:
[0262] Based on the first type of code segment, determine the reference code segment;
[0263] Based on the reference code segment and the target code segment, the prediction prompt information is determined.
[0264] In some embodiments, the apparatus further includes:
[0265] The second receiving module is used to receive the vectorized retrieval results sent by the electronic device. The vectorized retrieval results are obtained by performing vectorized retrieval on multiple candidate code segments based on the target code segment. The vectorized retrieval results indicate whether there is a second type of code segment similar to the target code segment among the multiple candidate code segments.
[0266] The first prediction module 902 is specifically used for:
[0267] If the vectorized retrieval results indicate the existence of a second type of code segment among multiple candidate code segments, then a reference code segment is determined based on the first and second type of code segments.
[0268] In some embodiments, the apparatus further includes:
[0269] The second prediction module is used to predict the subsequent code of the target code segment based on the second type of code segment and the target code segment if the code clone retrieval result indicates that there is no first type of code segment among multiple candidate code segments, and the vectorization retrieval result indicates that there is a second type of code segment among multiple candidate code segments, so as to obtain the code prediction result.
[0270] Because code clone retrieval can search for similar code from multiple dimensions such as text, lexical, syntactic, and semantic dimensions, the similar code segments (i.e., first-class code segments) identified from multiple candidate codes through code clone retrieval have small differences from the target code segment. This effectively improves the accuracy of similar code retrieval, thereby ensuring a high accuracy of the obtained code prediction results. Furthermore, this embodiment incorporates the priority relationship between code segments as part of the prediction hint information, and then performs code prediction based on this hint information. Compared to schemes that only use similar code and context code as prediction information, this embodiment provides accurate and comprehensive guidance for the code prediction process by carrying prediction hint information that includes the priority relationship between code segments, thus making the final code prediction results more accurate. This application embodiment can also concatenate various code segments according to the priority relationship between code segments and the length of the prompt information, thereby ensuring that the higher priority code segment is less likely to be truncated, making the higher priority code segment more likely to be used as the prediction prompt information. Furthermore, the order of each code segment in the prediction prompt information can also reflect the priority relationship between code segments. Compared with the scheme that only uses similar code and context code as prediction information, this application embodiment can reflect the priority relationship between code segments by the order of code segments in the prediction prompt information, thereby achieving accurate and comprehensive guidance for code prediction, and making the final code prediction result more accurate.
[0271] It should be noted that the code prediction device provided in the above embodiments is only illustrated by the division of the above functional modules. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. In addition, the code prediction device and the code prediction method embodiments provided in the above embodiments belong to the same concept, and the specific implementation process can be found in the method embodiments, which will not be repeated here.
[0272] This application also provides a computer-readable storage medium storing instructions that, when executed on a computer or processor, cause the computer or processor to perform the steps of the code prediction method described in the above embodiments.
[0273] This application also provides a computer program product containing instructions that, when executed on a computer or processor, cause the computer or processor to perform the steps of the code prediction method described in the above embodiments. Alternatively, a computer program is provided that, when executed on a computer or processor, causes the computer or processor to perform the steps of the code prediction method described in the above embodiments.
[0274] In the above embodiments, implementation can be achieved, in whole or in part, through software, hardware, firmware, or any combination thereof. When implemented in software, it can be implemented, in whole or in part, as a computer program product. The computer program product includes one or more computer instructions. When the computer instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of this application are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium accessible to a computer, or a data storage device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., digital versatile disc (DVD)), or a semiconductor medium (e.g., solid-state disk (SSD)). It is worth noting that the computer-readable storage medium mentioned in the embodiments of this application can be a non-volatile storage medium; in other words, it can be a non-transient storage medium.
[0275] It should be understood that "multiple" as mentioned herein refers to two or more. In the description of the embodiments of this application, unless otherwise stated, " / " means "or," for example, A / B can mean A or B; "and / or" in this document is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone. In addition, to facilitate a clear description of the technical solutions of the embodiments of this application, the terms "first," "second," etc., are used in the embodiments of this application to distinguish identical or similar items with substantially the same function and effect. Those skilled in the art will understand that the terms "first," "second," etc., do not limit the quantity or execution order, and the terms "first," "second," etc., do not necessarily imply that they are different.
[0276] It should be noted that the information (including but not limited to user device information, user personal information, etc.), data (including but not limited to data used for analysis, stored data, displayed data, etc.), and signals involved in the embodiments of this application are all authorized by the user or fully authorized by all parties, and the collection, use, and processing of related data must comply with the relevant laws, regulations, and standards of the relevant countries and regions. For example, the target code segment and multiple candidate code segments involved in the embodiments of this application were obtained under full authorization.
[0277] The above descriptions are embodiments provided in this application and are not intended to limit this application. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the protection scope of this application.
Claims
1. A code prediction method, characterized in that, The method includes: Based on the target code segment, code clone retrieval is performed on multiple candidate code segments to obtain code clone retrieval results. The candidate code segments include multiple code segments, and the definitions of the candidate code segments are complete. The code clone retrieval results indicate whether there are first-type code segments among the multiple candidate code segments that are similar to the target code segment. If the code clone retrieval result indicates that the first type of code segment exists among the multiple candidate code segments, then based on the first type of code segment and the target code segment, the subsequent code of the target code segment is predicted to obtain the code prediction result.
2. The method as described in claim 1, characterized in that, The first type of code segment refers to the code segment among the plurality of candidate codes that is similar to the target code segment in terms of text, lexical, syntactic and semantic dimensions.
3. The method as described in claim 1 or 2, characterized in that, The step of predicting the subsequent code of the target code segment based on the first type of code segment and the target code segment to obtain a code prediction result includes: Based on the first type of code segment and the target code segment, prediction prompt information is determined, which is used to guide the code prediction process; The predicted information is input into the code prediction model to obtain the code prediction result output by the code prediction model.
4. The method as described in claim 3, characterized in that, The step of determining the prediction prompt information based on the first type of code segment and the target code segment includes: Based on the first type of code segment, a reference code segment is determined; The prediction prompt information is determined based on the reference code segment and the target code segment.
5. The method as described in claim 4, characterized in that, The method further includes: Based on the target code segment, a vectorized retrieval is performed on the plurality of candidate code segments to obtain a vectorized retrieval result. The vectorized retrieval result indicates whether there is a second type of code segment similar to the target code segment among the plurality of candidate code segments. The step of determining the reference code segment based on the first type of code segment includes: If the vectorized retrieval result indicates that the second type of code segment exists among the plurality of candidate code segments, then the reference code segment is determined based on the first type of code segment and the second type of code segment.
6. The method as described in claim 5, characterized in that, The code clone retrieval results also include the similarity scores of each code segment in the first type of code segment, and the vectorized retrieval results also include the similarity scores of each code segment in the second type of code segment. The first type of code segment has a higher priority than the second type of code segment, and the similarity score indicates the degree of similarity between the corresponding code segment and the target code segment. The step of determining the reference code segment based on the first type of code segment and the second type of code segment includes: Based on the similarity between each code segment in the first type of code segment, the similarity between each code segment in the second type of code segment, and the priority relationship between the first type of code segment and the second type of code segment, the first type of code segment and the second type of code segment are filtered to obtain the reference code segment.
7. The method as described in claim 4, characterized in that, The step of determining the prediction prompt information based on the reference code segment and the target code segment includes: The predicted prompt information is determined based on the reference code segment, the target code segment, other code segments in the code file besides the target code segment, the priority relationship between the code segments, and the length of the prompt information. The priority relationship between the code segments indicates the priority relationship between the reference code segment, the target code segment, and the other code segments, and the length of the prompt information indicates the upper limit of the length of the predicted prompt information.
8. The method as described in claim 7, characterized in that, When the reference code segment includes at least one code segment, the priority of the target code segment is higher than the priority of each code segment in the at least one code segment, the priority of each code segment in the at least one code segment is higher than the priority of the other code segments, and the priority of each code segment in the at least one code segment is proportional to its corresponding similarity, the similarity indicating the degree of similarity between the corresponding code segment and the target code segment.
9. The method according to any one of claims 6-8, characterized in that, The prediction prompt information includes any of the following: The reference code segment and the target code segment; The reference code segment, the target code segment, and other code segments in the code file besides the target code segment; The reference code segment, the target code segment, the priority of the reference code segment, and the priority of the target code segment; The reference code segment, the target code segment, the priority of the reference code segment, the priority of the target code segment, the other code segments in the code file besides the target code segment, and the priorities of the other code segments.
10. The method as described in claim 5, characterized in that, The method further includes: If the code clone retrieval result indicates that the first type of code segment does not exist among the plurality of candidate code segments, and the vectorization retrieval result indicates that the second type of code segment exists among the plurality of candidate code segments, then based on the second type of code segment and the target code segment, the subsequent code of the target code segment is predicted to obtain the code prediction result.
11. The method according to any one of claims 1-10, characterized in that, The method further includes: Based on the predicted position, the target code segment is obtained from multiple code segments included in the code file, where the predicted position refers to the starting position of the code to be predicted in the code file.
12. The method as described in claim 11, characterized in that, The step of obtaining the target code segment from multiple code segments included in the code file based on the predicted location includes: Based on the multiple code segments included in the code file, determine the abstract syntax tree corresponding to the code file; Based on the abstract syntax tree, the code segment used to indicate the predicted position is determined, and the target code segment is obtained.
13. The method as described in claim 12, characterized in that, The abstract syntax tree includes multiple nodes and the type and position of each node. Each node indicates a code element in the multiple code segments. The position of the node refers to the position of the code element indicated by the node in the multiple code segments. The multiple nodes include a root node. The step of determining the code segment for the function indicating the predicted position based on the abstract syntax tree, to obtain the target code segment, includes: Determine the first node closest to the predicted location from the plurality of nodes; Based on the first node, a second node is determined from the abstract syntax tree. The second node is the node that is closest to the first node and is of type function in the path from the first node to the root node. The target code segment is determined based on the code elements indicated by the second node and the third node, wherein the third node refers to a node that is connected to the second node and is far from the root node.
14. The method as described in claim 13, characterized in that, Determining the target code segment based on the code elements indicated by the second and third nodes includes: The target code segment is determined by all the code segments composed of the code elements indicated by the second node and the third node.
15. The method as described in claim 13, characterized in that, Determining the target code segment based on the code elements indicated by the second and third nodes includes: The target code segment is determined from all code segments composed of the code elements indicated by the second node and the third node, where N is an integer greater than or equal to 1.
16. The method as described in claim 11, characterized in that, The step of obtaining the target code segment from multiple code segments included in the code file based on the predicted location includes: The M consecutive code segments preceding the predicted position in the multiple code segments are identified as the target code segment, where M is an integer greater than or equal to 1.
17. A code prediction method, characterized in that, Applied to electronic devices, the method includes: Based on the target code segment, code clone retrieval is performed on multiple candidate code segments to obtain code clone retrieval results. The candidate code segments include multiple code segments, and the definitions of the candidate code segments are complete. The code clone retrieval results indicate whether there are first-type code segments among the multiple candidate code segments that are similar to the target code segment. The code clone retrieval results are sent to the cloud platform, so that when the code clone retrieval results indicate that the first type of code segment exists among the plurality of candidate code segments, the cloud platform predicts the subsequent code of the target code segment based on the first type of code segment and the target code segment; or, prediction prompt information is sent to the cloud platform, so that the cloud platform predicts the subsequent code of the target code segment based on the prediction prompt information, wherein the prediction prompt information is determined based on the first type of code segment and the target code segment existing among the plurality of candidate code segments.
18. The method as described in claim 17, characterized in that, The method further includes: Based on the target code segment, a vectorized retrieval is performed on the plurality of candidate code segments to obtain a vectorized retrieval result. The vectorized retrieval result indicates whether there is a second type of code segment similar to the target code segment among the plurality of candidate code segments. The vectorized search results are sent to the cloud platform.
19. A code prediction method, characterized in that, Applied to a cloud platform, the method includes: The system receives code clone retrieval results or prediction prompts from an electronic device. The code clone retrieval results are obtained by cloning multiple candidate code segments based on a target code segment. Each candidate code segment includes multiple code segments and has a complete definition. The code clone retrieval results indicate whether there is a first type of code segment among the multiple candidate code segments that is similar to the target code segment. The prediction prompts are determined based on the first type of code segment and the target code segment that exist among the multiple candidate code segments. Upon receiving the code clone retrieval result, if the code clone retrieval result indicates that the first type of code segment exists among the plurality of candidate code segments, then based on the first type of code segment and the target code segment, the subsequent code of the target code segment is predicted to obtain a code prediction result; upon receiving the prediction prompt information, based on the prediction prompt information, the subsequent code of the target code segment is predicted to obtain the code prediction result.
20. The method as described in claim 19, characterized in that, The step of predicting the subsequent code of the target code segment based on the first type of code segment and the target code segment to obtain a code prediction result includes: Based on the first type of code segment and the target code segment, prediction prompt information is determined, which is used to guide the code prediction process; The predicted information is input into the code prediction model to obtain the code prediction result output by the code prediction model.
21. The method as described in claim 20, characterized in that, The step of determining the prediction prompt information based on the first type of code segment and the target code segment includes: Based on the first type of code segment, a reference code segment is determined; The prediction prompt information is determined based on the reference code segment and the target code segment.
22. The method as described in claim 21, characterized in that, The method further includes: The system receives vectorized retrieval results sent by an electronic device. These vectorized retrieval results are obtained by performing vectorized retrieval on a plurality of candidate code segments based on the target code segment. The vectorized retrieval results indicate whether there is a second type of code segment among the plurality of candidate code segments that is similar to the target code segment. The step of determining the reference code segment based on the first type of code segment includes: If the vectorized retrieval result indicates that the second type of code segment exists among the plurality of candidate code segments, then the reference code segment is determined based on the first type of code segment and the second type of code segment.
23. The method as described in claim 22, characterized in that, The method further includes: If the code clone retrieval result indicates that the first type of code segment does not exist among the plurality of candidate code segments, and the vectorization retrieval result indicates that the second type of code segment exists among the plurality of candidate code segments, then based on the second type of code segment and the target code segment, the subsequent code of the target code segment is predicted to obtain the code prediction result.
24. A code prediction device, characterized in that, The device includes: The first retrieval module is used to perform code clone retrieval on multiple candidate code segments based on the target code segment, and obtain code clone retrieval results. The candidate code segments include multiple code segments, the definitions of the candidate code segments are complete, and the code clone retrieval results indicate whether there is a first type of code segment similar to the target code segment among the multiple candidate code segments. The first prediction module is used to predict the subsequent code of the target code segment based on the first type of code segment and the target code segment if the code clone retrieval result indicates that the first type of code segment exists among the plurality of candidate code segments, so as to obtain the code prediction result.
25. A code prediction device, characterized in that, The device includes: The first retrieval module is used to perform code clone retrieval on multiple candidate code segments based on the target code segment, and obtain code clone retrieval results. The candidate code segments include multiple code segments, the definitions of the candidate code segments are complete, and the code clone retrieval results indicate whether there is a first type of code segment similar to the target code segment among the multiple candidate code segments. A first sending module is configured to send the code clone retrieval result to a cloud platform, so that when the code clone retrieval result indicates the presence of the first type of code segment among the plurality of candidate code segments, the cloud platform predicts the subsequent code of the target code segment based on the first type of code segment and the target code segment; or, send prediction prompt information to the cloud platform, so that the cloud platform predicts the subsequent code of the target code segment based on the prediction prompt information, wherein the prediction prompt information is determined based on the first type of code segment and the target code segment present among the plurality of candidate code segments.
26. A code prediction device, characterized in that, The device includes: The first receiving module is used to receive code clone retrieval results or prediction prompts sent by an electronic device. The code clone retrieval results are obtained by cloning multiple candidate code segments based on a target code segment. The candidate code segments include multiple code segments and have complete definitions. The code clone retrieval results indicate whether there is a first type of code segment similar to the target code segment among the multiple candidate code segments. The prediction prompts are determined based on the first type of code segment present among the multiple candidate code segments and the target code segment. The first prediction module is configured to, upon receiving the code clone retrieval result, if the code clone retrieval result indicates that the first type of code segment exists among the plurality of candidate code segments, then predict the subsequent code of the target code segment based on the first type of code segment and the target code segment to obtain a code prediction result; and upon receiving the prediction prompt information, predict the subsequent code of the target code segment based on the prediction prompt information to obtain the code prediction result.
27. An electronic device, characterized in that, The electronic device includes a memory and a processor; The memory is used to store computer programs; The processor is configured to execute the computer program to implement the steps of the method according to any one of claims 1-16, or to implement the steps of the method according to any one of claims 17-18.
28. A computing device cluster, characterized in that, The computing device cluster includes at least one computing device, and each computing device includes a processor and memory; The processor of the at least one computing device is configured to execute instructions stored in the memory of the at least one computing device to cause the cluster of computing devices to perform the method as described in any one of claims 1-16, or to perform the method as described in any one of claims 19-23.
29. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed on a computer or processor, causes the computer or processor to perform the method as described in any one of claims 1-16, or the method as described in any one of claims 17-18, or the method as described in any one of claims 19-23.
30. A computer program product, characterized in that, The computer program product includes computer instructions that, when executed by a computer or processor, cause the steps of the method as described in any one of claims 1-16 to be performed, or the steps of the method as described in any one of claims 17-18 to be performed, or the steps of the method as described in any one of claims 19-23 to be performed.
Citation Information
Patent Citations
Software reusing method based on code clone automatic detection and timely prompting
CN104077147A
Code recommendation method, device and system
CN113344023A
Retrieval-augmented code completion
US20230359441A1