Object code determination method, code completion method and computer program product
By using a sliding window and an optimized Jaccard similarity calculation method, the problem of low accuracy of similar code fragments in multi-file encoding scenarios is solved, achieving more efficient and accurate code retrieval and completion.
Patent Information
- Application Number
- PCT/CN2025/076886
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2024-05-20
- Filing Date
- 2025-02-11
- Publication Date
- 2025-11-27
Smart Images

Figure CN2025076886_27112025_PF_FP_ABST
Abstract
Description
Target code determination method, code completion method, computer program product
[0001] The present application claims priority to the Chinese patent application No. 202410627533.6, filed on May 20, 2024, and entitled "Target code determination method, code completion method, computer program product", the content of which is incorporated herein by reference in its entirety. TECHNICAL FIELD
[0002] Embodiments of the present specification relate to the technical field of computer technology, in particular to a target code determination method, a code completion method, and a computer program product. BACKGROUND
[0003] In today's code generation tools, although the rise of large models has greatly promoted the development of automatic coding technology, the limitations of computing resources still constrain the boundaries of the context processing capabilities of large models. For code generation quality, the relevance of context content is crucial. Introducing inappropriate or irrelevant content into the prompt will not only weaken the accuracy of the generated code, but also weaken the user's trust in the code completion plug-in.
[0004] In the complex scenario of multi-file coding, there is often some degree of logical connection between the code being written by the user and the previously edited or browsed files. However, there is a conflict between the limited nature of local computing resources and the high demand for immediate feedback, and the accuracy of the determined similar code fragments is often low, so there is an urgent need for a target code determination method that can quickly and accurately anchor similar code segments similar to the code being written by the user. SUMMARY
[0005] Therefore, the embodiments of the present specification provide a target code determination method. One or more embodiments of the present specification also relate to a target code determination apparatus, a code completion method, a computing device, a computer-readable storage medium, and a computer program product to solve the technical defects in the prior art that the accuracy of the searched similar code fragments is low when searching for similar code fragments.
[0006] According to a first aspect of the embodiments of the present specification, a target code determination method is provided, comprising:
[0007] determining a target processing code and a target associated code associated with the target processing code;
[0008] sliding a window on the target processing code using a preset sliding window and a preset sliding step to obtain a plurality of sub-processing codes, and sliding a window on the target associated code to obtain a plurality of sub-associated codes;
[0009] perform similarity calculation on each sub-processing code and each sub-association code to obtain a similarity result between the each sub-processing code and the each sub-association code;
[0010] determine, according to the similarity result, a target sub-processing code and a target sub-association code from the plurality of sub-processing codes and the plurality of sub-association codes.
[0011] According to a second aspect of the embodiments of the present specification, a target code determination apparatus is provided, comprising:
[0012] a first determination module configured to determine a target processing code and a target association code associated with the target processing code;
[0013] a code obtaining module configured to perform window sliding on the target processing code by using a preset sliding window and a preset sliding step to obtain a plurality of sub-processing codes, and perform window sliding on the target association code to obtain a plurality of sub-association codes;
[0014] a result obtaining module configured to perform similarity calculation on each sub-processing code and each sub-association code to obtain a similarity result between the each sub-processing code and the each sub-association code;
[0015] a second determination module configured to determine, according to the similarity result, a target sub-processing code and a target sub-association code from the plurality of sub-processing codes and the plurality of sub-association codes.
[0016] According to a third aspect of the embodiments of the present specification, a code completion method is provided, comprising:
[0017] receiving a code completion request sent by a client, and determining a target processing code according to the code completion request, wherein the code completion request is triggered by an interaction operation of a user on a user interaction interface of the client;
[0018] determining a target sub-association code according to the target processing code, wherein the target sub-association code is determined by the target code determination method described above;
[0019] inputting the target processing code and the target sub-association code into a code completion model, obtaining a completion code by using the code completion model, and returning the completion code to the client to enable the client to display the completion code to the user on the user interaction interface.
[0020] According to a fourth aspect of the embodiments of the present specification, a code completion apparatus is provided, comprising:
[0021] The receiving module is configured to receive a code completion request sent by a client, and determine target processing code according to the code completion request, wherein the code completion request is triggered by an interaction operation of a user on a user interaction interface of the client;
[0022] The determining module is configured to determine target sub-association code according to the target processing code, wherein the target sub-association code is determined by the target code determination method;
[0023] The obtaining module is configured to input the target processing code and the target sub-association code into a code completion model, obtain a completed code by using the code completion model, and return the completed code to the client, so that the client displays the completed code to the user on the user interaction interface.
[0024] According to a fifth aspect of an embodiment of the present specification, a computing device is provided, comprising:
[0025] a memory and a processor;
[0026] The memory is configured to store computer programs / instructions, and the processor is configured to execute the computer programs / instructions, which implement the steps of the target code determination method and the code completion method.
[0027] According to a sixth aspect of an embodiment of the present specification, a computer readable storage medium is provided, which stores computer programs / instructions, which implement the steps of the target code determination method and the code completion method when executed by a processor.
[0028] According to a seventh aspect of an embodiment of the present specification, a computer program product is provided, comprising computer programs / instructions, which implement the steps of the target code determination method and the code completion method when executed by a processor.
[0029] The target code determination method provided by one embodiment of the present specification can introduce target association code associated with target processing code and capable of providing a global code context view, and obtain a plurality of sub-processing codes corresponding to the target processing code and a plurality of sub-association codes corresponding to the target association code by means of window sliding, so as to obtain a more comprehensive similarity result between the codes, and obtain more accurate target sub-processing code and target sub-association code based on the similarity result, thereby improving the accuracy and efficiency of code retrieval. BRIEF DESCRIPTION OF DRAWINGS
[0030] FIG. 1 is a schematic diagram of a scenario of a target code determination method according to an embodiment of the present specification;
[0031] FIG. 2 is a flowchart of a target code determination method according to an embodiment of the present specification;
[0032] FIG. 3 is a flowchart of a processing procedure of a target code determination method according to an embodiment of the present specification;
[0033] FIG. 4 is a schematic diagram of a preset sliding window according to an embodiment of the present specification;
[0034] FIG. 5 is a schematic diagram of window sliding of a preset sliding window according to an embodiment of the present specification;
[0035] FIG. 6 is a flowchart of a code completion method according to an embodiment of the present specification;
[0036] FIG. 7 is a schematic diagram of a structure of a target code determination apparatus according to an embodiment of the present specification;
[0037] FIG. 8 is a schematic diagram of a structure of a code completion apparatus according to an embodiment of the present specification;
[0038] FIG. 9 is a structural block diagram of a computing device according to an embodiment of the present specification. DETAILED DESCRIPTION
[0039] In the following description, numerous specific details are set forth in order to provide a thorough understanding of the present specification. However, the present specification can be practiced without the specific details, which are not described in the present specification, and it is understood that the scope of the present specification is not limited to the details below.
[0040] The terms used in one or more embodiments of the present specification are merely for the purpose of describing a specific embodiment, and are not intended to limit one or more embodiments of the present specification. The singular forms "a," "an," and "the" used in one or more embodiments of the present specification and the appended claims are also intended to include the plural forms, unless the context clearly indicates otherwise. It should also be understood that the term "and / or" used in one or more embodiments of the present specification means and includes any or all possible combinations of one or more associated listed items.
[0041] It should be understood that, although the terms first, second, etc. can be employed in describing various information in one or more embodiments of the present specification, the information should not be limited to such terms. These terms are only used to differentiate one piece of information from another piece of information of the same type. For example, without departing from the scope of one or more embodiments of the present specification, first can also be referred to as second, and similarly, second can also be referred to as first. Depending on the context, the word "if' as used herein can be interpreted as "when" or "upon" or "in response to determining".
[0042] In addition, it should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in one or more embodiments of the present specification are all information and data authorized by the user or authorized by all parties, and the collection, use and processing of related data need to comply with relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation portal for user to choose authorization or refusal.
[0043] First, the terms involved in one or more embodiments of the present specification are explained.
[0044] Code generation tool: a tool used in software development to automatically generate code suggestions, which usually utilizes machine learning models such as natural language processing (NLP) models to predict and suggest the next possible tokens based on the code already written by the developer (input tokens).
[0045] Code completion plugin: an auxiliary tool integrated into the development environment that can provide programming syntax and structure correct code completion suggestions in real time based on the current written code.
[0046] Multi-file development: a common scenario in software development where developers work on multiple interdependent source code files, which requires development tools to provide effective search and reference between these files.
[0047] Token (in the field of large models): refers to the smallest unit in the text sequence processed by the natural language processing model; in the context of code completion, token can be variable name, function name, keyword, etc. Basic components of code.
[0048] Jaccard similarity: a statistical measure used to measure the similarity between two data sets (such as two sets of tokens), achieved by calculating the proportion of the intersection and union of the two data sets.
[0049] Multiset: In a multiset, the same element can appear multiple times; for example, {1, 2, 3} is a set, while {1, 1, 1, 2, 2, 3} is not a set, but a multiset; where the multiplicity of element 1 is 3, the multiplicity of element 2 is 2, and the multiplicity of element 3 is 1.
[0050] For the problem of cross-file code similarity calculation in code generation and completion tools, there are some solutions in the existing, but all have more or less shortcomings.
[0051] For example, traditional code completion tools: Many code completion tools use rule-based or heuristic-based methods to provide code suggestions, which may include simple string matching and syntax tree-based search; but these methods often rely on local context rather than global project perspective, and they may not provide deep semantic understanding when dealing with cross-file associations, making it difficult to capture code patterns scattered in multiple files.
[0052] Deep learning-based code completion models: For example, GitHub Copilot uses OpenAI's Codex model, which is a powerful deep learning algorithm that can generate code suggestions based on patterns learned from a large open source code base; although these models perform well in semantic understanding, they have limited context length, so they may not fully utilize all relevant code in the user's current project. In addition, deep learning models usually require a large amount of computing resources and may not run in real-time in a local environment.
[0053] Code search engines: Code search tools can provide code search functionality, allowing users to search for relevant code snippets in the entire code base; although these search engines are very useful, they are usually designed as independent tools rather than integrated into real-time completion systems during code editing, and users may need to explicitly perform searches, which is not as seamless and convenient as automatic code suggestions.
[0054] Therefore, to overcome the above problems, the embodiment of the present specification provides a method for quickly and accurately determining target code based on a bidirectional sliding window mechanism and an optimized Jaccard similarity.
[0055] In the present specification, a target code determination method is provided, and the present specification also relates to a target code determination device, a code completion method, a code completion device, a computing device, a computer-readable storage medium, and a computer program product, which are described in detail one by one in the following embodiments.
[0056] Referring to FIG. 1, FIG. 1 shows a scene schematic diagram of a target code determination method according to an embodiment of the present specification.
[0057] The target code determination method can be applied to various scenarios. For example, when the target code determination method is applied to a repeated code detection scenario, the target code determination method searches for target sub-associated codes that are highly similar or even consistent with the target processing code, and is used to identify and report the same or highly similar code segments in the code, so as to perform code refactoring and optimization. When the target code determination method is applied to a code completion scenario, the target code determination method determines target sub-associated codes that are highly similar to the target processing code, and inputs the target sub-associated codes into a code completion model in a code completion tool as a prompt, thereby providing the target processing code with prompt content that has a high context association degree, and enabling the code completion model to generate more accurate completion codes based on the target sub-associated codes. The target code determination method can also be applied to a code search scenario.
[0058] The following describes the target code determination method provided by the embodiments of the present specification in the case of being applied to a code search scenario.
[0059] Specifically, the target code determination method is implemented by the end-side device 102 and the server 104. The end-side device 102 is configured to send a target processing code to the server 104. In actual application, the end-side device 102 can send a code search request to the server 104, and the code search request carries the target processing code. The target processing code can be understood as a current editing code. The code search request is used to search for similar codes related to the current editing code. The code search request can be triggered by a user's interaction operation on a user interaction interface of the end-side device 102.
[0060] The code search tool 106 is deployed in the server 104. The code search tool 106 can save a code library or can obtain codes in a current workspace. Thus, when the server 104 receives the target processing code sent by the end-side device 102, the target processing code is input into the code search tool 106, and the code search tool 106 determines target associated codes associated with the target processing code, such as codes of other files in the current workspace. Thus, the target processing code is window-slid by using a preset sliding window and a preset sliding step, and a plurality of sub-processing codes are obtained. The target associated codes are window-slid, and a plurality of sub-associated codes are obtained. Similarity calculation is performed on each sub-processing code and each sub-associated code, and similarity results between each sub-processing code and each sub-associated code are obtained. According to the similarity results, target sub-processing codes and target sub-associated codes are determined from the plurality of sub-processing codes and the plurality of sub-associated codes. The target sub-processing codes, the target sub-associated codes, and the similarity results between the target sub-processing codes and the target sub-associated codes can be returned to the end-side device 102, which is not limited herein.
[0061] The terminal-side device 102 can include a browser, an APP (Application), or a web application such as an H5 (Hyper Text Markup Language 5) application, or a light application (also known as a small program, a lightweight application program), or a cloud application, and the like. The terminal-side device can be developed based on a software development kit (SDK) of a corresponding service provided by the server, such as a real-time communication (RTC) SDK, and the like. The terminal-side device can be deployed in an electronic device, and needs to be run in dependence on a device or an APP in the device, and the like. The electronic device can have a display screen and support information browsing, and the like, and can be a personal mobile terminal such as a mobile phone, a tablet computer, a personal computer, and the like. Various other types of applications can also be configured in the electronic device, such as human-computer dialogue applications, model training applications, text processing applications, web browser applications, shopping applications, search applications, instant communication tools, mailbox clients, social platform software, and the like.
[0062] The server 104 can be understood as a server providing various services, including a physical server, a cloud server, for example, a server providing communication services for multiple clients, for example, a server for background training supporting a model used on a client, for example, a server processing data sent by a client, and the like. It should be noted that the server 104 can be implemented as a distributed server cluster composed of multiple servers, or as a single server. The server 104 can also be a server of a distributed system, or a server combined with a blockchain. The server 104 can also be a cloud server of 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, and the like. The server 104 can also be an intelligent cloud computing server or an intelligent cloud host with artificial intelligence technology.
[0063] It should be noted that the target code determination method provided in the embodiments of the present specification can be executed by the server 104. In other embodiments of the present specification, the code search tool can be deployed in the terminal-side device 102, so that the terminal-side device 102 also has similar functions as the server 104, thereby executing the target code determination method provided in the embodiments of the present specification. In other embodiments, the target code determination method provided in the embodiments of the present specification can also be executed by the terminal-side device 102 and the server 104 together.
[0064] The target code determination method provided by one embodiment of the present specification can introduce target associated code associated with the target processing code and capable of providing a global code context view, and obtain a plurality of sub-processing codes corresponding to the target processing code and a plurality of sub-associated codes corresponding to the target associated code in a window sliding manner, so as to obtain a more comprehensive similarity result between codes, and obtain more accurate target sub-processing code and target sub-associated code based on the similarity result, thereby improving the accuracy and efficiency of code retrieval.
[0065] Referring to FIG. 2, FIG. 2 shows a flowchart of a target code determination method according to one embodiment of the present specification, which specifically includes the following steps.
[0066] Step 202: determining a target processing code and a target associated code associated with the target processing code.
[0067] The target processing code can be understood as the code in the file being edited by the user, and the target associated code can be understood as the code in the associated file related to the file being edited by the user. In consideration of the close association between code files and the global project perspective, the associated file can be understood as the file in the current workspace, which can include the following files: other files in addition to the file being edited, which are opened by the user in the integrated development environment, files edited by the user, files opened by the user, and files submitted by the user when managing the project. However, this is not limited.
[0068] Specifically, the code in the file being edited by the user and the code in the associated file related to the file being edited are determined, so that the similar part of the code in the associated file to the part of the code being edited by the user is determined, which can consider the content of the global project and provide more comprehensive relevance analysis.
[0069] In one or more embodiments of the present specification, when there are a plurality of associated files, the codes in the associated files can be determined as target associated codes in sequence, so as to determine the codes similar to the target processing code from the plurality of associated files. The specific implementation manner is as follows:
[0070] The determination of the target processing code and the target associated code associated with the target processing code includes:
[0071] determining the target processing code and a plurality of initial associated codes associated with the target processing code;
[0072] The plurality of initial association codes are sequentially determined as the target association code.
[0073] The initial association code can be understood as a code in each association file, and one initial association code comes from the same file.
[0074] Specifically, there can be multiple association files in the user workspace, and the initial association codes of the multiple association files are sequentially determined as the target association code, so as to realize comparison between the target processing code and the initial association codes in each association file.
[0075] In actual application, the priority of the multiple association files is other files except the file being edited, the file edited by the user, the file opened by the user, and the file historically submitted by the user when managing the project; therefore, the initial association codes of the association files can be sequentially determined as the target association code according to the priority.
[0076] The target code determination method provided by the embodiments of the present specification can expand the search range of the initial association code as much as possible while ensuring performance, and can improve the possibility of finding a highly similar segment of the target processing code by increasing the number of codes considered.
[0077] In one or more embodiments of the present specification, the code can be converted into a token sequence, so as to realize more efficient and more accurate target code determination. The specific implementation manner is as follows:
[0078] After determining the target processing code and the target association code associated with the target processing code, the method further includes:
[0079] performing tokenization on the target processing code, and converting the tokenized target processing code into a target processing code token sequence;
[0080] performing tokenization on the target association code, and converting the tokenized target association code into a target association code token sequence.
[0081] The target processing code token sequence can be understood as a target processing code token sequence composed of the tokenized target processing code after tokenization of the target processing code; and the target association code token sequence can be understood as a target processing code token sequence composed of the tokenized target association code after tokenization of the target association code.
[0082] Specifically, in the case of tokenizing the target processing code and the target associated code into one token, the token sequence corresponding to the target processing code and the target associated code can be obtained, and the token in the token sequence can represent the basic constituent part of the code such as the function name, the variable name, and the keyword.
[0083] The target code determination method provided by the embodiments of the present specification can quickly compare the similarity between the target processing code and the target associated code by tokenization, and accelerate the code search process by converting the target processing code and the target associated code into the target processing code token sequence and the target associated code token sequence.
[0084] Step 204: window sliding is performed on the target processing code by using a preset sliding window and a preset sliding step to obtain a plurality of sub-processing codes, and window sliding is performed on the target associated code to obtain a plurality of sub-associated codes.
[0085] The preset sliding window can be understood as a fixed-length window, which can capture continuous code segments in the target processing code and the target associated code by sliding the window. The window size (i.e., the number of code units contained, such as the number of characters or the number of words) of the preset sliding window can be pre-set according to actual conditions, and the window size determines the size of the sub-code segment captured each time.
[0086] The sub-processing code can be understood as a code segment of a preset sliding window size captured in the target processing code by the preset sliding window, and the sub-associated code can be understood as a code segment of a preset sliding window size captured in the target associated code by the preset sliding window.
[0087] Specifically, the preset sliding window is sequentially slid on the target processing code and the target associated code according to the preset sliding step, and a code in the preset sliding window is captured as a sub-processing code or a sub-associated code each time the sliding is performed. By dividing the originally continuous code into a series of ordered and uniform code segments, the subsequent comparison of the obtained sub-processing codes and sub-associated codes is facilitated.
[0088] In one or more embodiments of the present specification, the starting position and the ending position of the preset sliding window in the target processing code and the target associated code are determined to accurately obtain a plurality of sub-processing codes corresponding to the target processing code and a plurality of sub-associated codes corresponding to the target associated code. The specific implementation is as follows:
[0089] The target processing code is window-slid by using the preset sliding window and the preset sliding step to obtain a plurality of sub-processing codes, and the target association code is window-slid to obtain a plurality of sub-association codes, including:
[0090] The preset sliding window and the preset sliding step are determined.
[0091] The preset sliding window is used to start window sliding from the starting position of the target processing code according to the preset sliding step until the window sliding is ended by sliding to the ending position of the target processing code, so as to obtain the sub-processing code corresponding to the preset sliding window in each window sliding.
[0092] The preset sliding window is used to start window sliding from the starting position of the target association code according to the preset sliding step until the window sliding is ended by sliding to the ending position of the target association code, so as to obtain the sub-association code corresponding to the preset sliding window in each window sliding.
[0093] Specifically, in the case where the target processing code is not segmented, the starting position of the preset sliding window is determined as the starting position of the target processing code, i.e., the first character of the target processing code, and the initial position of the preset sliding window in the target processing code is determined according to the window size of the preset sliding window.
[0094] For example, in the case where the window size of the preset sliding window is 60 characters, the initial position of the preset sliding window in the target processing code is the position covering the first character to the 60th character, and the sub-processing code corresponding to the preset sliding window at the initial position is obtained; and at this time, the unit of the preset sliding step is also a character, for example, the preset sliding step is 1 character; in the case where the target processing code contains 1000 characters, when the preset sliding window is slid according to the preset sliding step, the ending position of the preset sliding window reaches the 1000th character, i.e., the ending position of the target processing code, the window sliding is ended, and the sub-processing code corresponding to the preset sliding window in each window sliding is obtained.
[0095] In actual application, the implementation of obtaining the sub-association code is similar to the implementation of obtaining the sub-processing code, which will not be described here.
[0096] In one or more embodiments of the present specification, by determining the starting position and the ending position of the preset sliding window in the target processing code segmentation sequence and the target association code segmentation sequence, a plurality of sub-processing code segmentation sequences corresponding to the target processing code and a plurality of sub-association code segmentation sequences corresponding to the target association code are accurately obtained. The specific implementation manner is as follows:
[0097] The target processing code is window-slid by using the preset sliding window and the preset sliding step to obtain a plurality of sub-processing codes, and the target association code is window-slid to obtain a plurality of sub-association codes, including:
[0098] The preset sliding window and the preset sliding step are determined.
[0099] The preset sliding window is used to start window sliding from the starting position of the target processing code token sequence according to the preset sliding step until the window sliding is ended when the preset sliding window is slid to the end position of the target processing code token sequence, so as to obtain the sub-processing code token sequence corresponding to the preset sliding window in each window sliding.
[0100] The preset sliding window is used to start window sliding from the starting position of the target association code token sequence according to the preset sliding step until the window sliding is ended when the preset sliding window is slid to the end position of the target association code token sequence, so as to obtain the sub-association code token sequence corresponding to the preset sliding window in each window sliding.
[0101] Specifically, in the case of tokenization for the target processing code, the unit of the window size of the preset sliding window is token, and the unit of the preset sliding step is also token, so that the starting position of the preset sliding window is determined as the first token of the target processing code token sequence, and the initial position of the preset sliding window in the target processing code token sequence is determined according to the window size of the preset sliding window.
[0102] For example, in the case of the window size of the preset sliding window being 60 tokens, the initial position of the preset sliding window in the target processing code token sequence is the position covering the first token to the 60th token, and the sub-processing code token sequence corresponding to the preset sliding window at the initial position is obtained; the preset sliding step can be determined as 1 token, and in the case of the target processing code token sequence containing 1000 tokens, the preset sliding window is slid according to the preset sliding step, so that the end position of the preset sliding window reaches the 1000th token, i.e. the end position of the target processing code token sequence, and the window sliding is ended, so as to obtain the sub-processing code token sequence corresponding to the preset sliding window in each window sliding.
[0103] Correspondingly, the implementation of obtaining the sub-association code token sequence is similar to the implementation of obtaining the sub-processing code token sequence, which will not be described here.
[0104] The target code determination method provided by the embodiments of the present specification can accurately determine the sliding range of the preset sliding window by accurately determining the start position and the end position of the preset sliding window, and can subsequently obtain different parts of the code meeting the requirements according to different actual requirements by adjusting the start position and the end position of the preset sliding window.
[0105] In one or more embodiments of the present specification, in order to improve the efficiency of obtaining the sub-processing code and the sub-association code, two preset sliding windows, i.e., a first preset sliding window and a second preset sliding window, can be determined, so that the two preset sliding windows are used to obtain a plurality of sub-processing codes from the target processing code and a plurality of sub-association codes from the target association code. The specific implementation is as follows:
[0106] The window sliding of the target processing code by using the preset sliding window and the preset sliding step to obtain a plurality of sub-processing codes, and the window sliding of the target association code by using the preset sliding window and the preset sliding step to obtain a plurality of sub-association codes, includes:
[0107] determining a first preset sliding window, a second preset sliding window, and the preset sliding step, wherein the window size of the first preset sliding window is the same as the window size of the second preset sliding window;
[0108] using the first preset sliding window to start window sliding from the start position of the target processing code according to the preset sliding step until ending window sliding at the end position of the target processing code, to obtain the sub-processing code corresponding to the preset sliding window in each window sliding; and
[0109] using the second preset sliding window to start window sliding from the start position of the target association code according to the preset sliding step until ending window sliding at the end position of the target association code, to obtain the sub-association code corresponding to the preset sliding window in each window sliding.
[0110] Specifically, the specific implementation of obtaining a plurality of sub-processing codes and a plurality of sub-association codes by using the preset sliding window is similar to the above, and will not be described here.
[0111] The target code determination method provided by the embodiments of the present specification can improve the efficiency of obtaining the sub-processing code and the sub-association code by setting two preset sliding windows, and can perform real-time similar result calculation on the obtained sub-processing code and the sub-association code by using the two preset sliding windows.
[0112] The target processing code is window-slid by using the preset sliding window and the preset sliding step to obtain a plurality of sub-processing codes, and the target associated code is window-slid to obtain a plurality of sub-associated codes, including:
[0113] The first preset sliding window, the second preset sliding window, and the preset sliding step are determined.
[0114] The first preset sliding window is used to start window sliding from the starting position of the target processing code word sequence according to the preset sliding step until the window sliding ends at the ending position of the target processing code word sequence, to obtain a sub-processing code word sequence corresponding to the preset sliding window in each window sliding.
[0115] The second preset sliding window is used to start window sliding from the starting position of the target associated code word sequence according to the preset sliding step until the window sliding ends at the ending position of the target associated code word sequence, to obtain a sub-associated code word sequence corresponding to the preset sliding window in each window sliding.
[0116] Specifically, the specific implementation of obtaining the sub-processing code word sequence and the sub-associated code word sequence by using the preset sliding window is similar to the above, and will not be described here.
[0117] The target code determination method provided by the embodiments of the present specification improves the efficiency of obtaining the sub-processing code word sequence and the sub-associated code word sequence by setting two preset sliding windows, and can calculate similar results in real time for the obtained sub-processing code word sequence and the sub-associated code word sequence by using the two preset sliding windows.
[0118] In one or more embodiments of the present specification, in the case where the code fragment similar to the code part currently being edited by the user is to be determined, the first preset sliding window includes the code part being edited by the user in the sub-processing code obtained by window sliding the target processing code. The specific implementation is as follows:
[0119] The first preset sliding window is used to start window sliding from the starting position of the target processing code according to the preset sliding step until the window sliding ends at the ending position of the target processing code, including:
[0120] The starting position and the ending position of the edited code in the target processing code are determined.
[0121] The first preset sliding window is used to start window sliding from a first target position of the target processing code according to the preset sliding step, and end window sliding until a second target position of the target processing code is reached, wherein the first target position is a position at the end of the first preset sliding window and aligned with the end position of the edited code, and the second target position is a position at the start of the first preset sliding window and aligned with the start position of the edited code.
[0122] The edited code can be understood as a code part being edited by the user in the target processing code.
[0123] For example, the user is editing the 100th line of file A, and the 100th line can be regarded as the edited code. In the case that the 100th line includes 20 characters, the start position of the edited code in the target processing code is determined to be the position of the 500th character, and the end position of the edited code in the target processing code is determined to be the position of the 520th character.
[0124] The window size of the first preset sliding window is determined to be w, and w needs to be greater than or equal to the length of the edited code. In the case that the end position of the first preset sliding window is aligned with the end position of the edited code, the start position of the first preset sliding window when starting window sliding is the 520-wth character, that is, the first target position is the position of the 520-wth character.
[0125] In the case that the start position of the first preset sliding window is aligned with the start position of the edited code, the start position of the first preset sliding window when ending window sliding is the 500th character, that is, the second target position is the position of the 500th character.
[0126] The target code determination method provided by the embodiments of the present specification can extract sub-processing sequences closely related to the edited code from the target processing code by setting the window sliding range of the first preset sliding window, and these sub-processing sequences contain important context information related to the edited code.
[0127] In one or more embodiments of the present specification, in the case that a code segment similar to the code part currently being edited by the user is to be determined, the first preset sliding window is caused to include the code part being edited by the user in the sub-processing code token sequence obtained by window sliding in the target processing code token sequence. The specific implementation is as follows:
[0128] The first preset sliding window is used to start window sliding from a first target position of the target processing code according to the preset sliding step, and end window sliding until a second target position of the target processing code is reached, wherein the first target position is a position at the end of the first preset sliding window and aligned with the end position of the edited code, and the second target position is a position at the start of the first preset sliding window and aligned with the start position of the edited code.
[0129] determining a start position and an end position of the edited code token sequence in the target processing code token sequence;
[0130] sliding the first preset sliding window from a first target position of the target processing code token sequence according to the preset sliding step, until sliding to a second target position of the target processing code token sequence, wherein the first target position is a position at which an end position of the first preset sliding window aligns with the end position of the edited code token sequence, and the second target position is a position at which a start position of the first preset sliding window aligns with the start position of the edited code token sequence.
[0131] Here, the unit of the first preset sliding window is token. The sub-processing code token sequence related to the edited code is obtained near the edited code, and the specific implementation is similar to the above and will not be described here.
[0132] The target code determination method provided by the embodiments of the present specification introduces a bidirectional sliding window mechanism, which can expand the search area in the forward and backward directions around the edited code, thereby providing a global and continuous code context view, and enabling more comprehensive cross-file code similarity calculation, thereby helping to find and suggest more accurate code completion options when applied in the code completion scenario.
[0133] Step 206: Perform similarity calculation on each sub-processing code and each sub-associated code to obtain a similarity result between each sub-processing code and each sub-associated code.
[0134] In one or more embodiments of the present specification, the similarity result between each sub-processing code and each sub-associated code is determined by calculating the number of overlapping code elements contained in each sub-processing code and each sub-associated code. The specific implementation is as follows:
[0135] The similarity calculation on each sub-processing code and each sub-associated code to obtain a similarity result between each sub-processing code and each sub-associated code includes:
[0136] determining the number of overlapping code elements contained in each sub-processing code and each sub-associated code;
[0137] obtaining a similarity result between each sub-processing code and each sub-associated code according to the number of overlapping code elements.
[0138] Wherein, the overlapping code element can be understood as the element contained in the intersection after the intersection operation on the sub-processing code and the sub-associated code, and the code element can be understood as the element in character units.
[0139] Specifically, the similarity result between each sub-processing code and each sub-correlation code can be obtained by calculating the Jaccard similarity, but in the case of using multiple sets for set union operation and presetting a fixed window size of the sliding window, the denominator of the Jaccard similarity calculation (the sum of the elements of the sub-processing code and the sub-correlation code) is constant, so it can be ignored; the similarity result is obtained by comparing the size of the intersection, that is, the number of overlapping code elements.
[0140] And by pruning optimization, when the sub-processing code (or the sub-correlation code) does not have the code element in the sub-correlation code (or the sub-processing code), the calculation is skipped.
[0141] The target code determination method provided by the embodiments of the present specification adopts optimized Jaccard similarity calculation, improves the traditional algorithm to adapt to the structure and semantic characteristics of the code, simplifies the calculation process, especially directly compares the size of the intersection and ignores the fixed denominator, which can significantly improve the calculation efficiency, and through the pruning optimization technology, it can quickly exclude the elements irrelevant to the reference set in the similarity calculation process, reduce invalid calculation, and speed up the overall calculation process.
[0142] In one or more embodiments of the present specification, in the case of segmenting the target processing code and the target correlation code, similarity calculation is performed on each sub-processing code segmentation sequence and each sub-correlation code segmentation sequence to obtain the similarity result between each sub-processing code segmentation sequence and each sub-correlation code segmentation sequence. The specific implementation is as follows:
[0143] The similarity calculation of each sub-processing code and each sub-correlation code obtains the similarity result between each sub-processing code and each sub-correlation code, including:
[0144] The similarity calculation of each sub-processing code segmentation sequence and each sub-correlation code segmentation sequence obtains the similarity result between each sub-processing code segmentation sequence and each sub-correlation code segmentation sequence.
[0145] The similarity calculation of each sub-processing code segmentation sequence and each sub-correlation code segmentation sequence obtains the similarity result between each sub-processing code segmentation sequence and each sub-correlation code segmentation sequence, including:
[0146] Determine the number of overlapping sequence elements contained in each sub-processing code segmentation sequence and each sub-correlation code segmentation sequence.
[0147] According to the number of overlapping sequence elements, the similarity result between each sub-processing code segmentation sequence and each sub-correlation code segmentation sequence is obtained.
[0148] The implementation can refer to the above-mentioned embodiments, which will not be described here.
[0149] In one or more embodiments of the present specification, when the similarity result between each sub-processing code word sequence and each sub-associated code word sequence is obtained, the last obtained similarity result can be updated to obtain the similarity result between the current sub-processing code word sequence and the sub-associated code word sequence. The specific implementation is as follows:
[0150] The similarity between each sub-processing code word sequence and each sub-associated code word sequence is calculated, and the similarity result between each sub-processing code word sequence and each sub-associated code word sequence is obtained, comprising:
[0151] The first sub-processing code word sequence in the plurality of sub-processing code word sequences is determined as a first target sub-processing code word sequence;
[0152] The sub-processing code word sequences other than the first sub-processing code word sequence in the plurality of sub-processing code word sequences are determined as second target sub-processing code word sequences;
[0153] The first target sub-processing code word sequence and each sub-associated code word sequence are calculated for similarity, and the first similarity result between the first target sub-processing code word sequence and each sub-associated code word sequence is obtained;
[0154] According to the first similarity result, the second similarity result between the second target sub-processing code word sequence and each sub-associated code word sequence is calculated;
[0155] According to the first similarity result and the second similarity result, the similarity result between each sub-processing code word sequence and each sub-associated code word sequence is obtained.
[0156] The first target sub-processing code word sequence can be understood as the first sub-processing code word sequence in the plurality of sub-processing code word sequences, and in the case that each sub-processing code word sequence is sequentially windowed according to the first preset sliding window, each sub-processing code word sequence has a sequence, i.e. the sub-processing code word sequence corresponding to the initial position of the first preset sliding window is the first sub-processing code word sequence.
[0157] The first similarity result can be understood as a reference result, and the subsequent second similarity result is determined based on the first similarity result.
[0158] The second target sub-processing code word segmentation sequence can be understood as a sub-processing code word segmentation sequence other than the first sub-processing code word segmentation sequence in the plurality of sub-processing code word segmentation sequences.
[0159] Specifically, by calculating the similarity between the first target sub-processing code word segmentation sequence and each sub-associated code word segmentation sequence, a first similarity result between the first target sub-processing code word segmentation sequence and the sub-associated code word segmentation sequence is obtained, and in the case of calculating the first similarity result, the calculation of the second similarity result can be realized by updating the first similarity result.
[0160] In one or more embodiments of the present specification, the similarity calculation between each sub-processing code and each sub-associated code to obtain the similarity result between each sub-processing code and each sub-associated code comprises:
[0161] The first sub-processing code in the plurality of sub-processing codes is determined as the first target sub-processing code.
[0162] The sub-processing code other than the first sub-processing code in the plurality of sub-processing codes is determined as the second target sub-processing code.
[0163] The first target sub-processing code is calculated with the similarity of each sub-associated code to obtain the first similarity result between the first target sub-processing code and each sub-associated code.
[0164] According to the first similarity result, the second similarity result between the second target sub-processing code and each sub-associated code is calculated.
[0165] According to the first similarity result and the second similarity result, the similarity result between each sub-processing code and each sub-associated code is obtained.
[0166] For specific implementation, please refer to the above-mentioned embodiments, which will not be repeated here.
[0167] In one or more embodiments of the present specification, the similarity calculation between the first target sub-processing code word segmentation sequence and each sub-associated code word segmentation sequence to obtain the first similarity result between the first target sub-processing code word segmentation sequence and each sub-associated code word segmentation sequence comprises:
[0168] The number of overlapping sequence elements between the first target sub-processing code word segmentation sequence and each sub-associated code word segmentation sequence is calculated.
[0169] According to the number of overlapping sequence elements, the first similarity result between the first target sub-processing code word segmentation sequence and each sub-associated code word segmentation sequence is obtained.
[0170] The coincident sequence elements can be understood as elements contained in the intersection of the sub-processing code token sequence and the sub-correlation code token sequence after the intersection operation.
[0171] The specific implementation can refer to the above-mentioned embodiments, which will not be described here.
[0172] In one or more embodiments of the present specification, the second similarity result between the second target sub-processing code token sequence and each sub-correlation code token sequence is calculated according to the first similarity result in a loop iteration manner. The specific implementation is as follows:
[0173] The second target sub-processing code token sequence is n, and the plurality of sub-correlation code token sequences is m, and n and m are both positive integers;
[0174] The second similarity result between the second target sub-processing code token sequence and each sub-correlation code token sequence is calculated according to the first similarity result, comprising:
[0175] S2, the i-th second target sub-processing code token sequence in the n second target sub-processing code token sequences is determined as a target sub-processing code token sequence, wherein i is a positive integer, and i starts from 1;
[0176] S4, the j-th sub-correlation code token sequence in the m sub-correlation code token sequences is determined as a target sub-correlation code token sequence, wherein j is a positive integer, and j starts from 1;
[0177] S6, according to the target sub-processing code token sequence and the target sub-processing code token sequence adjacent to the previous sub-processing code token sequence, the deleted sequence element and the added sequence element are determined;
[0178] S8, according to the deleted sequence element, the added sequence element and the target sub-correlation code token sequence, the first similarity result is updated, and the updated first similarity result is determined as the second similarity result between the target sub-processing code token sequence and the target sub-correlation code token sequence;
[0179] S10, judge whether i is less than or equal to n,
[0180] If yes, i is incremented by 1, the second similarity result is determined as the first similarity result, and step S2 is continued to be executed,
[0181] If not, in the case that j is less than or equal to m, j is incremented by 1, and step S2 is continued to be executed.
[0182] Specifically, starting from the second target sub-processing code token sequence and the sub-correlation code token sequence, a pair-by-pair similarity calculation is performed through a double-loop manner, one loop traversing the second target sub-processing code token sequence (index i) and the other loop traversing the sub-correlation code token sequence (index j), ensuring that all combinations are compared.
[0183] At the beginning of each loop, a second target sub-processing code token sequence (index i) and a sub-correlation code token sequence (index j) are selected as the current comparison pair, and based on the comparison of the current target sub-processing code token sequence and its previous sub-processing code token sequence, it is determined which elements are deleted and which elements are newly added. According to the determined deletion sequence and the newly added sequence elements, the first similarity result is updated based on the target sub-correlation code token sequence to obtain a second similarity result. In the case of continuing the loop, the second similarity result is determined to be the first similarity result, so that the first similarity result is always updated.
[0184] It is judged whether there are still second target sub-processing code token sequences that have not been traversed (i < n). If yes, the next second target sub-processing code token sequence is determined to continue the calculation. Otherwise, in the case that there are still sub-correlation code token sequences that have not been compared (j < m), the next sub-correlation code token sequence is switched to continue the loop.
[0185] For example, the first similarity result between the first target sub-processing code token sequence and the first sub-correlation code token sequence is calculated to be 1. When the second target sub-processing code token sequence is the second sub-processing code token sequence, since the second sub-processing code token sequence has a overlapping part with the first sub-processing code token sequence, according to the first sub-processing code token sequence and the second sub-processing code token sequence, it can be determined that the second sub-processing code token sequence has deleted sequence elements and newly added sequence elements compared with the first sub-processing code token sequence. Therefore, it is judged whether the deleted sequence elements exist in the first sub-correlation code token sequence. If yes, 1 is subtracted from the first similarity result. If not, the first similarity result is not affected. It is also judged whether the newly added sequence elements exist in the first sub-correlation code token sequence. If yes, 1 is added to the first similarity result. If not, the first similarity result is not affected.
[0186] In this way, the second similarity result between the second target sub-processing code token sequence and each sub-correlation code token sequence is calculated according to the first similarity result. Thus, the similarity result between each sub-processing code token sequence and each sub-correlation code token sequence is obtained according to the first similarity result and the second similarity result.
[0187] Of course, in actual application, the first preset sliding window can be fixed first, the second preset sliding window is slid to realize the traversal of each sub-associated code token sequence, then the first preset sliding window is moved, the second preset sliding window is continuously slid to realize the traversal of each sub-associated code token sequence, until the traversal of each sub-processing code token sequence and each sub-associated code token sequence is completed; or the second preset sliding window can be fixed first, the first preset sliding window is slid to realize the traversal of each sub-processing code token sequence, then the second preset sliding window is moved, the second preset sliding window is continuously slid to realize the traversal of each sub-associated code token sequence, until the traversal of each sub-processing code token sequence and each sub-associated code token sequence is completed.
[0188] That is, the similarity result 1 of the first sub-processing code token sequence and the first sub-associated code token sequence is obtained, so that when the similarity result of the sub-processing code token sequence except the first sub-processing code token sequence and the first sub-associated code token sequence is calculated, the similarity result 1 is obtained; in the case that the similarity result of each sub-processing code token sequence and the first sub-associated code token sequence is calculated, the second preset sliding window is slid back one step, the similarity result 2 of the first sub-processing code token sequence and the second sub-associated code token sequence is calculated, so that when the similarity result of the sub-processing code token sequence except the first sub-processing code token sequence and the second sub-associated code token sequence is calculated, the similarity result 2 is obtained, and so on, until the similarity result between each sub-processing code token sequence and each sub-associated code token sequence is obtained.
[0189] Or the similarity result 1 of the first sub-processing code token sequence and the first sub-associated code token sequence can be obtained, so that when the similarity result of the sub-associated code token sequence except the first sub-associated code token sequence and the first sub-processing code token sequence is calculated, the similarity result 1 is obtained; in the case that the similarity result of each sub-associated code token sequence and the first sub-processing code token sequence is calculated, the first preset sliding window is slid back one step, the similarity result 2 of the second sub-processing code token sequence and the first sub-associated code token sequence is calculated, so that when the similarity result of the sub-associated code token sequence except the first sub-associated code token sequence and the second sub-processing code token sequence is calculated, the similarity result 2 is obtained, and so on, until the similarity result between each sub-processing code token sequence and each sub-associated code token sequence is obtained.
[0190] The target code determination method provided by the embodiment of the present specification can update the elements at the edge of the preset sliding window through the rolling array update technology when the preset sliding window moves, that is, according to the changes before and after the movement of the preset sliding window, the calculation of the intersection is adjusted, a large amount of repeated calculation work is reduced, the updated intersection result can be quickly obtained, and similar results can be obtained.
[0191] Step 208: determining target sub-processing code and target sub-association code from the plurality of sub-processing codes and the plurality of sub-association codes according to the similar results.
[0192] In one or more embodiments of the present specification, the target similar result is quickly obtained by sorting the obtained similar results, so as to accurately determine the sub-processing code and the sub-association code corresponding to the target similar result. The specific implementation is as follows:
[0193] The target sub-processing code and the target sub-association code are determined from the plurality of sub-processing codes and the plurality of sub-association codes according to the similar results, including:
[0194] The obtained similar results are sorted, and the target similar result is determined from the similar results according to the sorting result;
[0195] The sub-processing code and the sub-association code corresponding to the target similar result are determined, and the sub-processing code and the sub-association code corresponding to the target similar result are determined as the target sub-processing code and the target sub-association code.
[0196] Specifically, all the calculated similarity results are sorted, and the sorting standard can be a similarity value. The higher the similarity value, the stronger the similarity or correlation between the corresponding sub-processing code and the sub-association code. The similarity values can be sequentially sorted from large to small to obtain a sorting result, the first similar result in the sorting result is determined as a target similar result, and the sub-processing code and the sub-association code corresponding to the target similar result are determined as the target sub-processing code and the target sub-association code.
[0197] The target code determination method provided by the embodiment of the present specification can quickly determine the target similar result from the similar results by sorting and screening the obtained similar results, locate the corresponding sub-processing code and sub-association code according to the target similar result, and thus efficiently identify the target sub-association code most relevant to the target sub-processing code.
[0198] In one or more embodiments of the present specification, the target sub-processing code and the target sub-association code are determined from the plurality of sub-processing codes and the plurality of sub-association codes according to the similar results, including:
[0199] rank the obtained similar results, and determine a target similar result from the similar results according to a ranking result;
[0200] determine a sub-processing code token sequence and a sub-correlation code token sequence corresponding to the target similar result, and determine the sub-correlation code token sequence and the sub-correlation code token sequence corresponding to the target similar result as the target sub-correlation code token sequence and the target sub-correlation code token sequence.
[0201] For details, refer to the above embodiments, which will not be repeated here.
[0202] By using the above target code determination method, the similarity comparison time of the code between two files is optimized to the original 1 / (n*w) (n is the number of similar files, w is the size of the sliding window, and the current is 60 tokens). In the case of 1000 tokens and a window size of 60, the time is optimized from about 1800 milliseconds to 30 milliseconds.
[0203] The target code determination method provided by the embodiments of the present specification, through the bidirectional sliding window mechanism, not only considers the local context, but also captures the code before and after the current editing point, providing more comprehensive context information. This helps to find more potential correlation code patterns in related files; through the optimized similarity calculation method, it can maintain low computational consumption while ensuring very high computational accuracy and real-time response capability.
[0204] Referring to FIG. 3, FIG. 3 shows a processing process flowchart of a target code determination method according to an embodiment of the present specification, which specifically includes the following steps.
[0205] Step 302: data preprocessing.
[0206] determine a target processing code and other codes in the current workspace, wherein the target processing code can be understood as the code in the file being edited by the user in the integrated development environment; the other codes include the codes of other files in the current workspace, and the other files can be obtained in the following priority: other files opened by the user in the integrated development environment except the file being edited, files once edited by the user, files once opened by the user, and files historically submitted by the user when managing the project.
[0207] In this case, the codes of multiple other files can be obtained, and the codes of the multiple other files are sequentially determined as target correlation codes to realize the calculation of the similar results with the target processing code.
[0208] In actual applications, in a case where the target processing code and the target association code are determined, token sequence A corresponding to the target processing code (i.e., the target processing code token sequence in the above embodiment) and token sequence B corresponding to the target association code (i.e., the target association code token sequence in the above embodiment) are obtained by performing tokenization processing on the target processing code and the target association code. The tokens in the token sequence A and the token sequence B can be variable names, function names, keywords, and the like, which are basic components of codes.
[0209] Step 304: initialization of the sliding window.
[0210] The window size and the position of the first preset sliding window and the second preset sliding window are set. Specifically, the window size of the first preset sliding window and the second preset sliding window is the same, for example, 60 tokens in the token sequence can be included in the first preset sliding window and the second preset sliding window.
[0211] In a case where similar codes need to be searched in a file being edited by the user or codes being edited by the user, the initial position of the first preset sliding window is set at a position of a token corresponding to the code being edited in the token sequence, so that the end position of the first preset sliding window is aligned with the end position (cursor position) of the token corresponding to the code being edited. For example, after 10 tokens are obtained by performing tokenization on the code before the cursor in the 100th line after the user edits the 100th line, the cursor position is taken as the end position of the first preset sliding window in a case where the window size of the first preset sliding window is 60 tokens, that is, the first preset sliding window at the initial position covers the 10 tokens corresponding to the 100th line and 50 tokens before the 10 tokens in the token sequence.
[0212] The start position of the token sequence B is determined as the start position of the second preset sliding window, so that the initial position of the second preset sliding window can be determined in a case where the window size of the second preset sliding window is determined.
[0213] Step 306: sliding window traversal.
[0214] The first preset sliding window is slid for the token sequence A, the second preset sliding window is slid for the token sequence B, and for each window sliding, the first preset sliding window corresponding subtoken sequence A is obtained, and the second preset sliding window corresponding subtoken sequence B is obtained. Specifically, the token sequence A and the token sequence B can be traversed in a double loop manner.
[0215] For example, after the initial positions of the first preset sliding window and the second preset sliding window are determined, the first preset sliding window can be fixed, and the second preset sliding window can be slid backward by using a preset sliding step, so that the second preset sliding window corresponding subtoken sequence B obtained by each window sliding can be compared with the first preset sliding window corresponding subtoken sequence A. When the second preset sliding window slides to the end position of the token sequence B, the first preset sliding window is slid backward according to the preset sliding step, the current first preset sliding window corresponding subtoken sequence A is obtained, and then each subtoken sequence B obtained by sliding the second preset sliding window in the token sequence B is compared.
[0216] In the above example, the first preset sliding window is slid to the start position of the token corresponding to the code being edited in the token sequence, and then the sliding is ended, that is, when the start position of the first preset sliding window is aligned with the start position of the 10 tokens corresponding to the 100th line, it is determined that the first preset sliding window slides to the end position, and the window sliding is ended. That is, when the similar code is determined for the code being edited by the user, the window sliding is performed to traverse the token sequence, and it is necessary to ensure that the subtoken sequence in the first preset sliding window includes the token corresponding to the code being edited by the user, so that when each subtoken sequence A of the token sequence A is compared with each subtoken sequence B of the token sequence B, the most similar code segment covering the token interval of the code being edited by the user can be determined according to the similarity result.
[0217] Step 308: Similarity result calculation.
[0218] Specifically, in the case of calculating the similarity between each subtoken sequence A and each subtoken sequence B, the similarity between each subtoken sequence A and each subtoken sequence B is obtained by calculating the Jaccard similarity (the Jaccard similarity can be understood as a measure for measuring the similarity between two sets, which is defined as the ratio between the size of the intersection of the two sets and the size of the union of the two sets, and the value of the ratio ranges from 0 to 1, where 1 indicates that the two sets are completely identical, and 0 indicates that the two sets have no intersection).
[0219] In practical applications, when calculating the similarity, the repetition of tokens in the subtoken sequence is considered, and the multiset is used to calculate the similarity.
[0220] As shown in FIG. 4, an embodiment of the present specification provides a schematic diagram of a preset sliding window.
[0221] One cell in the token sequence A and the token sequence B represents one token element. When the first preset sliding window corresponding to the token sequence A slides to the position in FIG. 4, the subtoken sequence A is obtained as [1, 3, 3, 4], and when the second preset sliding window corresponding to the token sequence B slides to the position in FIG. 4, the subtoken sequence B is obtained as [2, 3, 1, 4]. When the Jaccard similarity is used to calculate the similarity between the subtoken sequence A and the subtoken sequence B, the intersection of the subtoken sequence A and the subtoken sequence B is determined as (3, 1, 4), and the union is (1, 3, 3, 4, 2, 3, 1, 4). The ratio of the intersection to the union is 3 / 8, and thus the similarity between the subtoken sequence A and the subtoken sequence B is 3 / 8.
[0222] Step 310: optimizing the Jaccard algorithm.
[0223] Specifically, when calculating, since the window size of the first preset sliding window and the second preset sliding window is fixed, both are 60 tokens, the denominator of the similarity calculation (the sum of the elements of the two sets) is a constant, that is, the size of the union of the subtoken sequence A and the subtoken sequence B is 60+60, and thus can be omitted. The similarity is evaluated by calculating the intersection size of the subtoken sequence A and the subtoken sequence B to obtain the similarity result.
[0224] By improving the similarity calculation method, the code structure can be adapted, such as considering the repetition of tokens and ignoring the fixed denominator to directly compare the size of the intersection, which improves the accuracy of the algorithm and makes the similarity calculation result more consistent with the actual situation of code similarity.
[0225] Step 312: Pruning optimization.
[0226] When calculating the similarity, if the subtoken sequence B contains a token that does not exist in the target processing code, the calculation can be skipped.
[0227] For example, the token sequence A corresponding to the target processing code is [1, 2, 3], and the token sequence B of the target association code is [5, 2, 3, 4]. When comparing the intersection number of the subtoken sequence B [5, 2, 3] and the subtoken sequence A [1, 2, 3], the number of token element 5 in the subtoken sequence A can be skipped, and the number of token element 2 in the subtoken sequence A can be directly calculated.
[0228] Step 314: Rolling update intersection.
[0229] When moving the second preset sliding window corresponding to the token sequence B, only the tokens at both ends of the second preset sliding window are updated, and the middle part remains unchanged. According to the changes before and after the window movement, the intersection calculation is adjusted to reduce unnecessary repeated calculation.
[0230] For example, in the case where the preset sliding step is 1 token, FIG. 5 shows a schematic diagram of window sliding of a preset sliding window according to an embodiment of the present specification.
[0231] When the subtoken sequence A is [1, 3, 3, 4] and the subtoken sequence B is [2, 2, 3, 1], the similarity calculation result is (3, 1) / (1, 3, 3, 4, 2, 2, 3, 1), i.e. 2 / 8; when the second preset sliding window moves one token to the right, the deleted token element is 2, and the deleted token element 2 does not exist in the above similarity calculation intersection, so the number of elements in the intersection is not affected. For the newly added token element 4, it exists in the subtoken sequence A, so the number of elements in the intersection is increased by 1, i.e. the similarity calculation result at this time is (2+1) / 8=3 / 8. It is not necessary to perform complete calculation of the moved subtoken sequence B and the subtoken sequence A. On the basis of the previous calculation result, the intersection calculation is adjusted, i.e. the repeated token elements in the second preset sliding window before and after the movement are not repeatedly calculated, and the intersection calculation is adjusted according to the changed token elements, thereby reducing unnecessary repeated calculation.
[0232] Step 316: Target similarity result determination.
[0233] In a case where a preset end condition (for example, a preset time of 100 milliseconds) is reached, the window sliding is ended, the similarity results of the token sequence A and the token sequence B are obtained, and after the similarity results of the token sequence A and the token sequence B are obtained, the similarity results can be sorted in descending order, so that according to the sorting result, the similarity result in the first place is determined as a target similarity result, and the subtoken sequence A and the subtoken sequence B corresponding to the target similarity result are determined.
[0234] The target similarity result and the subtoken sequence A and the subtoken sequence B corresponding thereto can be output, so as to be subsequently applied in subsequent prompt or code completion.
[0235] The target code determination method provided by the embodiments of the present specification can not only consider local context, but also find more potential associated code patterns in the code of related files without being limited by a single file, can search for relevant code segments forward (preceding) and backward (following) in the context of the currently edited code, provide more comprehensive context information and obtain more comprehensive relevance analysis, and through the pruning and rolling update manner, reduce repeated and unnecessary calculation, improve the efficiency of the calculation process, and also enable the method to be applicable to running in a local environment with limited resources.
[0236] Referring to FIG. 6, FIG. 6 shows a flowchart of a code completion method according to an embodiment of the present specification, which specifically includes the following steps.
[0237] Step 602: receiving a code completion request sent by a client, and determining a target processing code according to the code completion request, wherein the code completion request is triggered by an interaction operation of a user on a user interaction interface of the client.
[0238] Specifically, when the user inputs code in the user interaction interface of the client (for example, an IDE (an integrated development environment), a code editor, etc.) and triggers the code completion function, the client collects the context information of the currently edited code, including the part of the code (that is, the target processing code) that has been typed by the user, the cursor position, etc., and determines as the code completion request, which is sent by the client to a server, and the server receives the code completion request sent by the client, so as to determine the target processing code according to the code completion request.
[0239] Step 604: determining a target sub-associated code according to the target processing code, wherein the target sub-associated code is determined by the target code determination method.
[0240] Specifically, a code generation tool can be deployed in the server, which includes a code search service and a code completion model. The code search service can implement the above-mentioned target code determination method. Thus, after the server receives a code completion request, the target processing code is parsed according to the code completion request, and the target sub-associated code is obtained by using the above-mentioned target code determination method. The target sub-associated code has a high similarity with the target processing code.
[0241] Step 606: input the target processing code and the target sub-associated code into the code completion model, obtain the completion code by using the code completion model, and return the completion code to the client, so that the client displays the completion code to the user on the user interaction interface.
[0242] After the target processing code and the associated target sub-associated code are determined, the two pieces of information are input into the code completion model. The code completion model can be a deep learning model, such as a model composed of a Transformer architecture. The code completion model is trained by a large amount of high-quality code data, and can understand and generate code segments that conform to grammar specifications and logical coherence. The code completion model can predict the most likely subsequent code segment, i.e., the completion code, according to the input information. The completion code can include multiple.
[0243] After the prediction is completed, the completion code can be encapsulated in a response message and returned to the client. When the client receives the completion code, the completion code can be displayed on the user interaction interface. The user can intuitively view and select the appropriate completion option, thereby accelerating the programming process and improving the code quality.
[0244] The code completion method provided by the embodiments of the present specification can search for relevant code segments forward (above) and backward (below) in the context of the current editing code, determine target sub-associated code, provide more comprehensive relevance analysis, improve the ability of the code generation tool in processing cross-file association, and thus greatly improve the accuracy of code automatic generation. Moreover, the method can be directly integrated into a code editor or an integrated development environment as part of real-time code completion, provide seamless code suggestions, improve the accuracy and real-time performance of the code completion tool in cross-file code association retrieval, and effectively improve the efficiency and quality of software development.
[0245] Corresponding to the above method embodiments, the present specification also provides target code determination device embodiments. FIG. 7 shows a structural schematic diagram of a target code determination device according to an embodiment of the present specification. As shown in FIG. 7, the device includes:
[0246] The first determining module 702 is configured to determine a target processing code and a target association code associated with the target processing code.
[0247] The code obtaining module 704 is configured to slide a preset sliding window and a preset sliding step on the target processing code to obtain a plurality of sub-processing codes, and slide the preset sliding window on the target association code to obtain a plurality of sub-association codes.
[0248] The result obtaining module 706 is configured to perform similarity calculation on each sub-processing code and each sub-association code to obtain a similarity result between the each sub-processing code and the each sub-association code.
[0249] The second determining module 708 is configured to determine a target sub-processing code and a target sub-association code from the plurality of sub-processing codes and the plurality of sub-association codes according to the similarity result.
[0250] Optionally, the code obtaining module 704 is further configured to:
[0251] determine the preset sliding window and the preset sliding step;
[0252] slide the preset sliding window on the target processing code from a starting position of the target processing code according to the preset sliding step to obtain a sub-processing code corresponding to the preset sliding window in each window sliding; and
[0253] slide the preset sliding window on the target association code from a starting position of the target association code according to the preset sliding step to obtain a sub-association code corresponding to the preset sliding window in each window sliding.
[0254] Optionally, the code obtaining module 704 is further configured to:
[0255] determine a first preset sliding window, a second preset sliding window and the preset sliding step, wherein a window size of the first preset sliding window is the same as a window size of the second preset sliding window;
[0256] slide the first preset sliding window on the target processing code from a starting position of the target processing code according to the preset sliding step to obtain a sub-processing code corresponding to the preset sliding window in each window sliding; and
[0257] obtaining, by using the second preset sliding window, a sub-correlation code corresponding to each window sliding according to the preset sliding step, from a start position of the target correlation code to an end position of the target correlation code.
[0258] Optionally, the code obtaining module 704 is further configured to:
[0259] determining a start position and an end position of the edit code in the target processing code;
[0260] obtaining, by using the first preset sliding window, a sub-processing code corresponding to each window sliding according to the preset sliding step, from a first target position of the target processing code to a second target position of the target processing code, wherein the first target position is a position at which an end position of the first preset sliding window is aligned with the end position of the edit code, and the second target position is a position at which a start position of the first preset sliding window is aligned with the start position of the edit code.
[0261] Optionally, the result obtaining module 706 is further configured to:
[0262] determining a number of coincident code elements contained in the sub-processing codes and the sub-correlation codes;
[0263] obtaining a similarity result between the sub-processing codes and the sub-correlation codes according to the number of the coincident code elements.
[0264] Optionally, the result obtaining module 706 is further configured to:
[0265] determining a first sub-processing code in the plurality of sub-processing codes as a first target sub-processing code;
[0266] determining sub-processing codes other than the first sub-processing code in the plurality of sub-processing codes as second target sub-processing codes;
[0267] performing similarity calculation on the first target sub-processing code and the sub-correlation codes to obtain a first similarity result between the first target sub-processing code and the sub-correlation codes;
[0268] calculating a second similarity result between the second target sub-processing codes and the sub-correlation codes according to the first similarity result;
[0269] obtaining a similarity result between the sub-processing codes and the sub-correlation codes according to the first similarity result and the second similarity result.
[0270] Optionally, the second determining module 708 is further configured to:
[0271] rank the obtained similar results, and determine a target similar result from the similar results according to a ranking result;
[0272] determine a sub-processing code and a sub-correlation code corresponding to the target similar result, and determine the sub-processing code and the sub-correlation code corresponding to the target similar result as the target sub-processing code and the target sub-correlation code.
[0273] The apparatus further includes:
[0274] a word segmentation module configured to perform word segmentation on the target processing code, convert the word segmented target processing code into a target processing code word sequence, and perform word segmentation on the target correlation code, convert the word segmented target correlation code into a target correlation code word sequence.
[0275] Optionally, the code obtaining module 704 is further configured to:
[0276] determine the preset sliding window and the preset sliding step;
[0277] slide the window from a starting position of the target processing code word sequence according to the preset sliding step, until the window is slid to an ending position of the target processing code word sequence, obtain a sub-processing code word sequence corresponding to the preset sliding window at each window sliding, and
[0278] slide the window from a starting position of the target correlation code word sequence according to the preset sliding step, until the window is slid to an ending position of the target correlation code word sequence, obtain a sub-correlation code word sequence corresponding to the preset sliding window at each window sliding.
[0279] Optionally, the code obtaining module 704 is further configured to:
[0280] determine a first preset sliding window, a second preset sliding window, and the preset sliding step;
[0281] slide the window from a starting position of the target processing code word sequence according to the preset sliding step, until the window is slid to an ending position of the target processing code word sequence, obtain a sub-processing code word sequence corresponding to the preset sliding window at each window sliding, and
[0282] The second preset sliding window is used to slide the window from the start position of the target associated code word sequence according to the preset sliding step, until the window is slid to the end position of the target associated code word sequence, to obtain a sub-associated code word sequence corresponding to the preset sliding window in each window sliding.
[0283] Optionally, the result obtaining module 706 is further configured to:
[0284] The similarity between each sub-processing code word sequence and each sub-associated code word sequence is calculated to obtain a similarity result between each sub-processing code word sequence and each sub-associated code word sequence.
[0285] Optionally, the result obtaining module 706 is further configured to:
[0286] The number of coincident sequence elements contained in each sub-processing code word sequence and each sub-associated code word sequence is determined.
[0287] The similarity result between each sub-processing code word sequence and each sub-associated code word sequence is obtained according to the number of coincident sequence elements.
[0288] Optionally, the result obtaining module 706 is further configured to:
[0289] The first sub-processing code word sequence in the plurality of sub-processing code word sequences is determined as a first target sub-processing code word sequence.
[0290] The sub-processing code word sequences other than the first sub-processing code word sequence in the plurality of sub-processing code word sequences are determined as second target sub-processing code word sequences.
[0291] The similarity between the first target sub-processing code word sequence and each sub-associated code word sequence is calculated to obtain a first similarity result between the first target sub-processing code word sequence and each sub-associated code word sequence.
[0292] The second similarity result between the second target sub-processing code word sequence and each sub-associated code word sequence is calculated according to the first similarity result.
[0293] The similarity result between each sub-processing code word sequence and each sub-associated code word sequence is obtained according to the first similarity result and the second similarity result.
[0294] Optionally, the result obtaining module 706 is further configured to:
[0295] counting the number of coincident sequence elements between the first target sub-processing code word sequence and each sub-correlation code word sequence;
[0296] obtaining a first similarity result between the first target sub-processing code word sequence and each sub-correlation code word sequence according to the number of coincident sequence elements.
[0297] Optionally, the result obtaining module 706 is further configured to:
[0298] S2, determining an i-th second target sub-processing code word sequence in the n second target sub-processing code word sequences as a target sub-processing code word sequence, where i is a positive integer, and i starts from 1;
[0299] S4, determining a j-th sub-correlation code word sequence in the m sub-correlation code word sequences as a target sub-correlation code word sequence, where j is a positive integer, and j starts from 1;
[0300] S6, determining a deleted sequence element and an added sequence element according to the target sub-processing code word sequence and a previous sub-processing code word sequence adjacent to the target sub-processing code word sequence;
[0301] S8, updating the first similarity result according to the deleted sequence element, the added sequence element and the target sub-correlation code word sequence, and determining an updated first similarity result as a second similarity result between the target sub-processing code word sequence and the target sub-correlation code word sequence;
[0302] S10, judging whether i is less than or equal to n,
[0303] If yes, increasing i by 1, determining the second similarity result as the first similarity result, and continuing to execute step S2,
[0304] If no, increasing j by 1 when j is less than or equal to m, and continuing to execute step S2.
[0305] Optionally, the second determining module 708 is further configured to:
[0306] sorting the obtained similarity results, and determining a target similarity result from the similarity results according to a sorting result;
[0307] determining a sub-processing code word sequence and a sub-correlation code word sequence corresponding to the target similarity result, and determining the sub-correlation code word sequence and the sub-correlation code word sequence corresponding to the target similarity result as the target sub-correlation code word sequence and the target sub-correlation code word sequence.
[0308] Optionally, the first determining module 702 is further configured to:
[0309] determine the target processing code and a plurality of initial associated codes associated with the target processing code;
[0310] determine the plurality of initial associated codes as the target associated codes in sequence.
[0311] One embodiment of the present specification provides a target code determining apparatus. By determining the target processing code and the target associated code associated with the target processing code, the target associated code associated with the target processing code and capable of providing a global code context view can be introduced, and by means of window sliding, the plurality of sub-processing codes corresponding to the target processing code and the plurality of sub-associated codes corresponding to the target associated code can be obtained. Therefore, in the case of calculating the similarity between each sub-processing code and each sub-associated code, a more comprehensive similarity result between codes can be obtained, and based on the similarity result, more accurate target sub-processing codes and target sub-associated codes can be obtained, thereby improving the accuracy and efficiency of code retrieval.
[0312] The above is a schematic scheme of the target code determining apparatus of the present embodiment. It should be noted that the technical scheme of the target code determining apparatus and the technical scheme of the target code determining method described above belong to the same concept, and the details of the technical scheme of the target code determining apparatus which are not described in detail can be referred to the description of the technical scheme of the target code determining method.
[0313] Corresponding to the method embodiments described above, the present specification also provides code completion apparatus embodiments. FIG. 8 shows a structural schematic diagram of a code completion apparatus according to one embodiment of the present specification. As shown in FIG. 8, the apparatus comprises:
[0314] The receiving module 802 is configured to receive a code completion request sent by a client, and determine a target processing code according to the code completion request, wherein the code completion request is triggered by an interaction operation of a user on a user interaction interface of the client;
[0315] The determining module 804 is configured to determine a target sub-associated code according to the target processing code, wherein the target sub-associated code is determined by the target code determining method described above;
[0316] The obtaining module 806 is configured to input the target processing code and the target sub-associated code into a code completion model, obtain a completion code by using the code completion model, and return the completion code to the client, so that the client displays the completion code to the user on the user interaction interface.
[0317] The embodiment of the present specification provides a code completion device, which can search for relevant code segments in the context of the current edited code, forward (above) and backward (below), determine target sub-association code, and provide more comprehensive relevance analysis. The method can be directly integrated into a code editor or an integrated development environment as part of real-time code completion, providing seamless code suggestions.
[0318] The above is a schematic scheme of a code completion device according to the embodiment. It should be noted that the technical scheme of the code completion device belongs to the same concept as the technical scheme of the code completion method described above. The technical scheme of the code completion device is not described in detail. The details can be referred to the description of the technical scheme of the code completion method.
[0319] FIG. 9 shows a structural block diagram of a computing device 900 according to an embodiment of the present specification. The components of the computing device 900 include but are not limited to a memory 910 and a processor 920. The processor 920 is connected to the memory 910 through a bus 930, and a database 950 is used to save data.
[0320] The computing device 900 also includes an access device 940, which enables the computing device 900 to communicate via one or more networks 960. Examples of these networks include a public switched telephone network (PSTN), a local area network (LAN), a wide area network (WAN), a personal area network (PAN), or a combination of communication networks such as the Internet. The access device 940 can include one or more of any type of network interface (for example, a network interface card (NIC)), such as an IEEE 802.11 wireless local area network (WLAN) wireless interface, a worldwide interoperability for microwave access (Wi-MAX) interface, an Ethernet interface, a universal serial bus (USB) interface, a cellular network interface, a Bluetooth interface, near field communication (NFC).
[0321] In one embodiment of the present specification, the above-mentioned components of the computing device 900 and other components not shown in FIG. 9 can also be connected to each other, for example, through a bus. It should be understood that the computing device structure block diagram shown in FIG. 9 is merely for the purpose of example, and is not a limitation on the scope of the present specification. Other components can be added or replaced as needed by those skilled in the art.
[0322] The computing device 900 can be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (e.g., a tablet computer, a personal digital assistant, a laptop computer, a notebook computer, a netbook, etc.), a mobile phone (e.g., a smartphone), a wearable computing device (e.g., a smartwatch, smart glasses, etc.), or other types of mobile devices, or a stationary computing device such as a desktop computer or a personal computer (PC). The computing device 900 can also be a mobile or stationary server.
[0323] The processor 920 is configured to execute computer programs / instructions that implement the steps of the above-mentioned target code determination method and code completion method.
[0324] Each of the embodiments in the present specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other. Each of the embodiments focuses on the difference from other embodiments. In particular, for the computing device embodiment, since it is basically similar to the target code determination method and code completion method embodiment, the description is relatively simple, and the relevant parts can be referred to the part of the description of the target code determination method and code completion method embodiment.
[0325] An embodiment of the present specification also provides a computer-readable storage medium storing computer programs / instructions that implement the steps of the above-mentioned target code determination method and code completion method.
[0326] Each of the embodiments in the present specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other. Each of the embodiments focuses on the difference from other embodiments. In particular, for the computer-readable storage medium embodiment, since it is basically similar to the target code determination method and code completion method embodiment, the description is relatively simple, and the relevant parts can be referred to the part of the description of the target code determination method and code completion method embodiment.
[0327] An embodiment of the present specification also provides a computer program product comprising computer programs / instructions that implement the steps of the above-mentioned target code determination method and code completion method.
[0328] The above is a schematic solution of a computer program product of the embodiment. It should be noted that the technical solution of the computer program product and the technical solutions of the target code determination method and the code completion method belong to the same concept, and the technical solution of the computer program product is not described in detail. The contents can be seen from the description of the technical solutions of the target code determination method and the code completion method.
[0329] The above describes specific embodiments of the present specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in an order different than the order in which they are recited in the embodiments and still achieve desirable results. In addition, the processes depicted in the figures do not necessarily require the particular order shown, or sequential order, to achieve the desired results. In certain implementations, multitasking and parallel processing can be advantageous or necessary.
[0330] The computer instructions include computer program code, which can be in the form of source code, object code, executable code, or some intermediate form. The computer-readable medium can include any entity or device capable of carrying the computer program code, recording medium, U disk, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signal, telecommunication signal, and software distribution medium, etc. It should be noted that the contents contained in the computer-readable medium can be appropriately increased or decreased according to the requirements of patent practice, for example, in some regions, according to the patent practice, the computer-readable medium does not include electrical carrier signals and telecommunication signals.
[0331] It should be noted that for the foregoing method embodiments, in order to facilitate description, they are all expressed as a combination of a series of actions, but those skilled in the art should know that the embodiments of the present specification are not limited by the order of the described actions, because according to the embodiments of the present specification, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should know that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily all necessary for the embodiments of the present specification.
[0332] In the above embodiments, the description of each embodiment is focused on, and the parts not described in detail in a certain embodiment can be seen from the related description of other embodiments.
[0333] The preferred embodiments of the present specification disclosed above are only used to help explain the present specification. Alternative embodiments do not describe all the details and limit the present application to the specific embodiments described. Obviously, many modifications and changes can be made according to the content of the embodiments of the present specification. The present specification selects and specifically describes these embodiments in order to better explain the principles and practical applications of the embodiments of the present specification, so that those skilled in the art can well understand and utilize the present specification. The present specification is limited only by the claims and their full scope and equivalents.
Claims
1. A target code determining method, comprising: determining a target processing code and a target association code associated with the target processing code; window-sliding the target processing code using a preset window and a preset sliding step to obtain a plurality of sub-processing codes, and window-sliding the target association code using the preset window and the preset sliding step to obtain a plurality of sub-association codes; calculating the similarity between each sub-processing code and each sub-association code to obtain a similarity result between the each sub-processing code and the each sub-association code; determining a target sub-processing code and a target sub-association code from the plurality of sub-processing codes and the plurality of sub-association codes according to the similarity result. 2.The target code determining method of claim 1, after the determining a target processing code and a target association code associated with the target processing code, further comprising: segmenting the target processing code, and converting the segmented target processing code into a target processing code segmentation sequence; segmenting the target association code, and converting the segmented target association code into a target association code segmentation sequence. 3.The target code determining method of claim 2, the window-sliding the target processing code using a preset window and a preset sliding step to obtain a plurality of sub-processing codes, and window-sliding the target association code using the preset window and the preset sliding step to obtain a plurality of sub-association codes, comprising: determining a first preset window, a second preset window and the preset sliding step; starting window-sliding from a start position of the target processing code segmentation sequence according to the preset sliding step using the first preset window, and ending window-sliding at an end position of the target processing code segmentation sequence, to obtain a sub-processing code segmentation sequence corresponding to the preset window of each window-sliding; and starting window-sliding from a start position of the target association code segmentation sequence according to the preset sliding step using the second preset window, and ending window-sliding at an end position of the target association code segmentation sequence, to obtain a sub-association code segmentation sequence corresponding to the preset window of each window-sliding. 4.The target code determining method of claim 3, the starting window-sliding from a start position of the target processing code segmentation sequence according to the preset sliding step using the first preset window, and ending window-sliding at an end position of the target processing code segmentation sequence, comprising: determining a start position and an end position of an edit code segmentation sequence in the target processing code segmentation sequence; The first preset sliding window is used to start window sliding from a first target position of the target processing code token sequence according to the preset sliding step until ending window sliding at a second target position of the target processing code token sequence, wherein the first target position is a position at the end of the first preset sliding window and aligned with a terminal position of the edited code token sequence, and the second target position is a position at the start of the first preset sliding window and aligned with a start position of the edited code token sequence.
5. The target code determining method of claim 3, wherein the similarity calculation between each sub-processing code and each sub-associated code to obtain a similarity result between the each sub-processing code and the each sub-associated code comprises: determining a number of overlapping sequence elements contained in the each sub-processing code token sequence and the each sub-associated code token sequence; and obtaining the similarity result between the each sub-processing code token sequence and the each sub-associated code token sequence according to the number of overlapping sequence elements.
6. The target code determining method of claim 3, wherein the similarity calculation between each sub-processing code and each sub-associated code to obtain a similarity result between the each sub-processing code and the each sub-associated code comprises: determining a first sub-processing code token sequence in the plurality of sub-processing code token sequences as a first target sub-processing code token sequence; determining other sub-processing code token sequences in the plurality of sub-processing code token sequences except the first sub-processing code token sequence as second target sub-processing code token sequences; performing similarity calculation between the first target sub-processing code token sequence and the each sub-associated code token sequence to obtain a first similarity result between the first target sub-processing code token sequence and the each sub-associated code token sequence; calculating a second similarity result between the second target sub-processing code token sequence and the each sub-associated code token sequence according to the first similarity result; and obtaining the similarity result between the each sub-processing code token sequence and the each sub-associated code token sequence according to the first similarity result and the second similarity result.
7. The target code determining method of claim 6, wherein the second target sub-processing code token sequence is n, the plurality of sub-associated code token sequences is m, and n and m are positive integers; and wherein the calculating of the second similarity result between the second target sub-processing code token sequence and the each sub-associated code token sequence according to the first similarity result comprises: S2, determining an i-th second target sub-processing code token sequence in the n second target sub-processing code token sequences as a target sub-processing code token sequence, wherein i is a positive integer and i starts from 1; and S4, determining a j-th sub-associated code token sequence in the m sub-associated code token sequences as a target sub-associated code token sequence, wherein j is a positive integer and j starts from 1. S6, determining a deletion sequence element and an addition sequence element according to the target sub-processing code token sequence and a previous sub-processing code token sequence adjacent to the target sub-processing code token sequence; S8, updating the first similarity result according to the deletion sequence element, the addition sequence element and the target sub-correlation code token sequence, and determining the updated first similarity result as a second similarity result between the target sub-processing code token sequence and the target sub-correlation code token sequence; S10, determining whether i is less than or equal to n, if yes, increasing i by 1, determining the second similarity result as the first similarity result, and continuing to execute step S2, if no, increasing j by 1 under the condition that j is less than or equal to m, and continuing to execute step S2.
8. The target code determining method according to any one of claims 5-7, wherein the target sub-processing code and the target sub-correlation code are determined from the plurality of sub-processing codes and the plurality of sub-correlation codes according to the similarity results, comprising: sorting the obtained similarity results, and determining a target similarity result from the similarity results according to a sorting result; and determining a sub-processing code token sequence and a sub-correlation code token sequence corresponding to the target similarity result, and determining the sub-correlation code token sequence and the sub-correlation code token sequence corresponding to the target similarity result as the target sub-correlation code token sequence and the target sub-correlation code token sequence.
9. A code completion method, comprising: receiving a code completion request sent by a client, and determining a target processing code according to the code completion request, wherein the code completion request is triggered by an interaction operation of a user on a user interaction interface of the client; determining a target sub-correlation code according to the target processing code, wherein the target sub-correlation code is determined by the target code determining method according to any one of claims 1-8; inputting the target processing code and the target sub-correlation code into a code completion model, obtaining a completed code by using the code completion model, and returning the completed code to the client, so that the client displays the completed code to the user on the user interaction interface.
10. A computer program product, comprising computer programs / instructions which, when executed by a processor, implement the steps of the method according to any one of claims 1-9.
Citation Information
Patent Citations
Text feature extraction method and device and text classification method and device
CN106997339A
Code completion method and device
CN113867710A
Code generation method and device, storage medium and electronic equipment
CN116166271A
Object code determination method, code completion method and computer program product
CN118567621A
Automatic evaluation of test code quality
US20210390038A1