Code processing method, code model training method, device and equipment

Through dependency analysis and optimization of pasted code, the problem that pasted code cannot be directly applied is solved, development efficiency and code quality are improved, and the seamless integration of code and existing environments is achieved.

CN119127298APending Publication Date: 2024-12-13BEIJING BAIDU NETCOM SCI & TECH CO LTD
View PDF 0 Cites 3 Cited by

Patent Information

Application Number
CN202411246680.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-05
Publication Date
2024-12-13

AI Technical Summary

Technical Problem

In software development environments, pasting code is often not directly applicable to the current code development environment, which causes developers to interrupt the encoding process and modify it, reducing development efficiency.

Method used

By listening to code pasting events, obtaining the target pasting code, performing dependency analysis, generating context-dependent data, and optimizing the code based on this data, outputting the optimized code to ensure its consistency with the existing code environment.

Benefits of technology

Improve development efficiency, improve code quality, ensure that the optimized code seamlessly integrates into the existing code environment, and reduce potential problems caused by code mismatch.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119127298A_ABST
    Figure CN119127298A_ABST
Patent Text Reader

Abstract

The invention provides a code processing method, a code model training method, a code model training device and code model training equipment, and relates to the field of artificial intelligence, in particular to the technical fields of software development, data processing, large models, computer communication and the like. The code processing method comprises the following steps: in response to a monitored code pasting event, obtaining a target pasting code; performing dependency analysis on the target paste code to obtain context dependency data corresponding to the target paste code; performing code optimization on the target paste code according to the context dependency data to obtain an optimized code corresponding to the target paste code; and outputting the optimized code. According to the method, the development efficiency can be greatly improved, the optimized code is seamlessly fused into the current code development environment, and the code quality is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to technical fields such as software development, data processing, large models and computer communications in the field of artificial intelligence, and in particular to a method for processing code, a code model training method, device and equipment. Background Art

[0002] In a software development environment, developers often use the copy and paste function frequently to improve coding efficiency.

[0003] Currently, developers can paste code from other places (such as code previously written by the developer or code in other files, etc.) into the Integrated Development Environment (IDE). However, the pasted code is often not directly applicable to the current code development environment. Developers need to interrupt the coding process and modify the pasted code, which greatly reduces the development efficiency. Therefore, an effective solution to optimize the pasted code is urgently needed. Summary of the invention

[0004] The present disclosure provides a code processing method, a code model training method, an apparatus and a device for optimizing pasted code.

[0005] According to a first aspect of the present disclosure, a method for processing code is provided, comprising:

[0006] In response to monitoring the code paste event, obtaining the target paste code;

[0007] Perform dependency analysis on the target pasted code to obtain context dependency data corresponding to the target pasted code;

[0008] According to the context dependency data, the target pasted code is optimized to obtain the optimized code corresponding to the target pasted code;

[0009] Output optimized code.

[0010] According to a second aspect of the present disclosure, a code model training method is provided, comprising:

[0011] Obtaining a training data set, where the training data in the training data set includes a pasted code sample, a context-dependent data sample of the pasted code sample, and a label code, where the label code is a code collected within a coordinate range of the pasted code sample when the pasted code sample is pasted into a code development environment and a preset waiting time is retained;

[0012] Based on the training data set, the code optimization model is iteratively trained until the calculated loss function value meets the preset evaluation condition or the number of iterations reaches the preset number of iterations, thereby obtaining the trained code optimization model.

[0013] According to a third aspect of the present disclosure, a device for processing code is provided, including:

[0014] An acquisition unit, configured to acquire a target paste code in response to monitoring a code paste event;

[0015] A dependency analysis unit, used to perform dependency analysis on the target pasted code to obtain context dependency data corresponding to the target pasted code;

[0016] A code optimization unit, used to optimize the target pasted code according to the context dependency data, and obtain optimized code corresponding to the target pasted code;

[0017] Output unit, used to output optimized code.

[0018] According to a fourth aspect of the present disclosure, a code model training device is provided, comprising:

[0019] an acquisition unit, configured to acquire a training data set, wherein the training data in the training data set includes a pasted code sample, a context-dependent data sample of the pasted code sample, and a label code, wherein the label code is a code collected within a coordinate range of the pasted code sample when the pasted code sample is pasted into a code development environment and a preset waiting time is retained;

[0020] The training unit is used to iteratively train the code optimization model based on the training data set until the calculated loss function value meets the preset evaluation condition or the number of iterations reaches the preset number of iterations, thereby obtaining the trained code optimization model.

[0021] According to a fifth aspect of the present disclosure, an electronic device is provided, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method for processing code described in the first aspect or execute the code model training method described in the second aspect.

[0022] According to a sixth aspect of the present disclosure, a non-transitory computer-readable storage medium storing computer instructions is provided, wherein the computer instructions are used to enable the computer to execute the method for processing code described in the first aspect or the code model training method described in the second aspect.

[0023] According to a seventh aspect of the present disclosure, a computer program product is provided, comprising: a computer program, the computer program being stored in a readable storage medium, at least one processor of an electronic device being able to read the computer program from the readable storage medium, the at least one processor executing the computer program so that the electronic device executes the method for processing code described in the first aspect or executes the code model training method described in the second aspect.

[0024] The technology disclosed in the present invention solves the problem that the pasted code is often not directly applicable to the current code development environment, and the developer needs to interrupt the coding process and modify the pasted code, which greatly reduces the development efficiency. By responding to the code paste event monitored, the target pasted code is obtained; the target pasted code is subjected to dependency analysis to obtain the context dependency data corresponding to the target pasted code, and the target pasted code is optimized according to the context dependency data to obtain the optimized code corresponding to the target pasted code; and the optimized code is output. Among them, by performing dependency analysis on the target pasted code, the overall code structure and logic can be deeply understood, and the code format, quality, context understanding and semantic adaptation are comprehensively considered, and the target pasted code is automatically optimized, which can greatly improve the development efficiency, ensure the consistency of the optimized code with the existing code in the current code development environment in style and logic, and seamlessly integrate the optimized code into the current code development environment, improve the code quality, and enhance the coding experience of developers.

[0025] It should be understood that the content described in this section is not intended to identify the key or important features of the embodiments of the present disclosure, nor is it intended to limit the scope of the present disclosure. Other features of the present disclosure will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS

[0026] The accompanying drawings are used to better understand the present solution and do not constitute a limitation of the present disclosure.

[0027] Figure 1 It is a schematic diagram of an application scenario to which the code processing method disclosed in the present invention is applicable;

[0028] Figure 2 is a schematic diagram according to a first embodiment of the present disclosure;

[0029] Figure 3 is a schematic diagram according to a second embodiment of the present disclosure;

[0030] Figure 4 is a schematic diagram according to a third embodiment of the present disclosure;

[0031] Figure 5 is a schematic diagram according to a fourth embodiment of the present disclosure;

[0032] Figure 6 is a schematic diagram according to a fifth embodiment of the present disclosure;

[0033] Figure 7 is a schematic diagram according to a sixth embodiment of the present disclosure;

[0034] Figure 8 is a schematic diagram according to a seventh embodiment of the present disclosure;

[0035] Fig. 9 FIG. 9 is a schematic block diagram of an example electronic device 900 that may be used to implement embodiments of the present disclosure. DETAILED DESCRIPTION

[0036] The following is a description of exemplary embodiments of the present disclosure in conjunction with the accompanying drawings, including various details of the embodiments of the present disclosure to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present disclosure. Similarly, for the sake of clarity and conciseness, descriptions of well-known functions and structures are omitted in the following description.

[0037] The present invention discloses a method for processing code, a code model training method, an apparatus and a device, which are applied to technical fields such as software development, data processing, large models and computer communications in the field of artificial intelligence, so as to achieve the purpose of quickly correcting pasted code and ensuring that the pasted code can be seamlessly integrated into the existing code environment.

[0038] In the technical solution of the present disclosure, the collection, storage, use, processing, transmission, provision and disclosure of user personal information involved are in compliance with the provisions of relevant laws and regulations and do not violate public order and good morals.

[0039] First, some technical terms involved in this disclosure are explained:

[0040] High-quality code refers to software code that performs well in many aspects and usually has the following characteristics: (1) Strong readability: the code structure is clear, the logic is easy to understand, the naming is standardized, the variable and function names are meaningful, and there are appropriate comments and documentation; (2) Good maintainability: modular design, single function, low coupling and high cohesion, and compliance with design patterns and best practices; (3) High testability: high unit test coverage and easy to write automated tests; (4) Performance optimization: reasonable selection of algorithms and data structures, and efficient resource utilization; (5) Safe and reliable: considering various abnormal situations, with appropriate error handling mechanisms, and paying attention to data security and privacy protection; (6) Good scalability: reasonable architecture design, easy to expand in the future, and high code reusability; (7) Compliance with coding standards: comply with industry code style guidelines and maintain consistency.

[0041] In a software development environment, developers often frequently use the copy and paste function for codes to improve coding efficiency. At present, developers can paste codes from other places (such as codes written by developers before or codes in other files, etc.) into an integrated development environment. In particular, with the support of large language model (LLM) technology, developers are increasingly inclined to generate code by talking to artificial intelligence (AI) programming assistants, and then paste the generated code into an integrated development environment (IDE). Although the above code pasting method speeds up the initial coding speed, in many cases, the pasted code will be modified immediately after pasting, and the scope of modification includes small adjustments (such as format adjustment, variable renaming, symbol completion, etc.) or large-scale changes (such as business logic rewriting, etc.). This shows that pasted code is often not directly applicable to the current code development environment, and cannot fully conform to the business logic of the entire code file. Developers need to interrupt the coding process and modify the pasted code, which greatly reduces the development efficiency. Therefore, there is an urgent need for an effective solution to optimize pasted code.

[0042] In addition, in the field of software development, improving code reuse efficiency and reducing duplication of work have always been the focus of much attention. With the development of artificial intelligence and large language model technology, code generation and optimization methods are also evolving. In related technologies, code can be formatted based on rules, static code analysis can be performed on the code, and intelligent code completion can be performed. Among them, rule-based code formatting is a widely used technology for unifying the appearance and structure of code. This method uses a predefined set of rules to adjust format elements such as indentation, blanks or line breaks of the code. Common tools include code formatting tools such as Prettier, Black and gofmt. These tools can automatically adjust the code format so that the code complies with specific style guidelines. Static code analysis is a technology that analyzes source code without executing the program. It detects potential errors, security vulnerabilities, performance issues and style violations by scanning the code. Common tools include static code analysis tools such as SonarQube, ESLint and FindBugs. These tools can be integrated into the development environment to provide real-time feedback during the coding process. Intelligent code completion is a predictive technology that provides possible code snippet suggestions based on the current coding context. This technology uses deep learning algorithms to analyze large code bases and learn common coding patterns and best practices. Representative implementations include AI-based programming assistance tools such as GitHub Copilot and Baidu Comate. These tools can provide context-related code suggestions and significantly improve coding speed.

[0043] Although the above-mentioned related technologies have improved the efficiency of code development and optimization in different aspects, they all have some common limitations. Specifically, rule-based code formatting tools lack understanding of code logic, only focus on the surface structure of the code, and cannot understand or optimize the logical content of the code. Static code analysis tools mainly focus on problem detection rather than active optimization, which may produce a large number of false positives or false negatives, requiring developers to invest time in manual review. Although intelligent code completion tools are powerful, they mainly focus on generating code from scratch rather than optimizing existing code snippets. Most of the above-mentioned related technologies focus on local features of the code or generate new code, lack an in-depth understanding of the overall code structure and logic, are usually passive or reactive, and cannot actively adapt to complex project requirements or specific programming contexts. Especially when dealing with large blocks of pasted code, the above-mentioned related technologies often have difficulty in effectively integrating new code with existing code bases. Most importantly, the above-mentioned related technologies lack a comprehensive solution to simultaneously deal with multiple aspects of code formatting, quality, context understanding, and semantic adaptation. In the actual software development process, developers often need to switch between different tools, which not only reduces work efficiency, but also may lead to inconsistent code quality. Therefore, there is an urgent need for an effective solution to optimize pasted code by comprehensively considering code format, quality, context understanding, and semantic adaptation to make up for the shortcomings of existing technologies.

[0044] In order to solve the above problems, the present disclosure provides a method for processing code, which monitors the developer's code paste event in real time at the business layer, obtains the target paste code corresponding to the code paste event, and performs dependency analysis on the target paste code, that is, analyzes the dependency relationship and overall structure of the target paste code in the current code file, and automatically generates optimized code corresponding to the target paste code according to the context dependency data corresponding to the target paste code, which can help developers quickly correct the content of the paste code, ensure that the paste code can be seamlessly integrated into the existing code environment, and improve development efficiency and development quality.

[0045] In addition, at the model level, the code optimization model is fine-tuned based on a large amount of real developer programming data to improve the code optimization model's ability to understand the context of pasted code and the output quality.

[0046] It can be seen that the method for processing code provided by the present disclosure will bring significant value to the field of software development, can effectively improve development efficiency, and greatly improve coding speed by reducing the time investment of developers to modify the code after pasting. At the same time, the present disclosure can also enhance the quality of code, ensure that the pasted code remains consistent with the existing code base (Base), and reduce potential problems (bugs) caused by code mismatch. This is not only applicable to various programming languages ​​​​and development environments, but also provides a smarter and smoother coding experience for the development team. Especially in terms of AI-assisted programming, the present disclosure can be seamlessly integrated with code generation AI to provide smarter coding assistance, further promoting the improvement of software development efficiency and quality.

[0047] Figure 1 It is a schematic diagram of an application scenario applicable to the method for processing codes disclosed in the present invention. In the application scenario, during the software development process, the developer generates a piece of business logic code through a large language model, for example, and copies and pastes the business logic code into an integrated development environment through the client 101. According to the method for processing codes provided in the present invention, the optimized code corresponding to the business logic code can be output through the server 102, and the optimized code can be displayed to the developer through the client 101, so that the developer can determine whether to adopt the optimized code.

[0048] It should be noted that Figure 1 This is only a schematic diagram of an application scenario provided by the embodiment of the present disclosure. Figure 1 does not limit the equipment included in Figure 1 The positional relationship between the devices is limited.

[0049] The following specific embodiments are used to describe in detail the technical solution of the present invention and how the technical solution of the present invention solves the above-mentioned technical problems. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present invention will be described below in conjunction with the accompanying drawings.

[0050] Figure 2 Schematic diagram of the first embodiment of the present disclosure. Figure 2 As shown, the method for processing codes provided in the first embodiment of the present disclosure can be applied to an electronic device, which can be a server or a server cluster, etc. The method for processing codes provided in the first embodiment of the present disclosure includes:

[0051] S201: In response to monitoring a code paste event, obtaining a target paste code.

[0052] In the disclosed embodiment, the pasted code may be code previously written by the developer, code in other files, or code generated by a large language model, etc. The disclosed embodiment does not limit the source of the pasted code. For example, in the software development process, the developer pastes part of the code in other files into the integrated development environment. Accordingly, the electronic device executing the embodiment of the method can monitor the code paste event, thereby obtaining the target paste code corresponding to the code paste event. The target paste code is, for example, one or more lines of code, and the target paste code can also be understood as the original code block to be pasted.

[0053] S202: Perform dependency analysis on the target paste code to obtain context dependency data corresponding to the target paste code.

[0054] In this step, after obtaining the target paste code, a dependency analysis can be performed on the target paste code. For example, data such as which files, classes, or functions (such as function signatures, function return values) the target paste code depends on can be obtained as key data for analyzing the context, thereby obtaining context dependency data corresponding to the target paste code.

[0055] S203: Optimize the target pasted code according to the context dependency data to obtain optimized code corresponding to the target pasted code.

[0056] In this step, after obtaining the context dependency data corresponding to the target paste code, the target paste code can be optimized according to the context dependency data to obtain the optimized code corresponding to the target paste code. It can be understood that by optimizing the target paste code according to the context dependency data corresponding to the target paste code, the overall code structure and logic can be deeply understood, and the code format, quality, context understanding and semantic adaptation can be comprehensively considered to ensure that the optimized code corresponding to the target paste code is consistent with the existing code in the current code development environment in style and logic, and the optimized code is seamlessly integrated into the current code development environment.

[0057] S204: Output optimized code.

[0058] In this step, after obtaining the optimized code corresponding to the target pasted code, the optimized code can be output. For example, the difference between the optimized code and the target pasted code can be highlighted, which helps to improve the developer's operational convenience, so that the developer can more quickly determine whether to adopt the optimized code.

[0059] In the disclosed embodiment, in response to monitoring a code paste event, the target paste code is obtained; dependency analysis is performed on the target paste code to obtain context dependency data corresponding to the target paste code; based on the context dependency data, the target paste code is optimized to obtain the optimized code corresponding to the target paste code; and the optimized code is output. Among them, by performing dependency analysis on the target paste code, the overall code structure and logic can be deeply understood, and the code format, quality, context understanding and semantic adaptation are comprehensively considered, and the target paste code is automatically optimized, which can greatly improve the development efficiency, ensure the consistency of the optimized code with the existing code in the current code development environment in style and logic, and seamlessly integrate the optimized code into the current code development environment, improve the code quality, and enhance the coding experience of developers.

[0060] Figure 3 FIG. 1 is a schematic diagram of a second embodiment of the present disclosure. Based on the above embodiment, the present disclosure further describes the method for processing codes. Figure 3 As shown, the method for processing codes provided by the second embodiment of the present disclosure may include:

[0061] S301: In response to monitoring a code paste event, obtaining a target paste code.

[0062] The implementation principle and technical effect of S301 can be referred to the above Figure 2 The embodiment of step S201 is not described in detail.

[0063] In the present disclosure, Figure 2 The step S202 may further include the following step S302:

[0064] S302: Perform dependency analysis on relevant information of the target paste code to obtain context dependency data corresponding to the target paste code, where the relevant information includes at least one of file information, class information, and function information that the target paste code depends on.

[0065] Exemplarily, dependency analysis can be performed on the file information, class information, and function information that the target pasted code depends on, such as obtaining data such as which files, classes, or functions (such as function signatures, function return values) the target pasted code depends on, as key data for analyzing the context, thereby obtaining context dependency data corresponding to the target pasted code.

[0066] In the present disclosure, Figure 2 The step S203 may further include the following step S303:

[0067] S303: Inputting the target pasted code and the context dependency data into a code optimization model, and in the case where it is determined that the target pasted code is to be optimized, optimizing the target pasted code based on the context dependency data to obtain optimized code.

[0068] Among them, the code optimization model is trained based on the pasted code samples in the code development environment, the context-dependent data samples of the pasted code samples, and the labeled code. The labeled code is the code collected within the coordinate range of the pasted code sample when the pasted code sample is pasted in the code development environment and the preset waiting time is retained.

[0069] In this step, the code optimization model is obtained by fine-tuning training based on a general large code model, wherein the training data set used to train the code optimization model is obtained based on the data actually annotated by the developer in the code development environment, which can ensure the authenticity, diversity and high quality of the training data set from the source of the data, and is extremely consistent with the programming habits of the developer, thereby ultimately ensuring the high-quality output of the code optimization model. For specific information on how to train and obtain the code optimization model, please refer to the subsequent embodiments.

[0070] Exemplarily, after obtaining the context-dependent data corresponding to the target paste code, the target paste code and the context-dependent data can be assembled into a prompt, and the prompt is input into the code optimization model. Accordingly, when the code optimization model determines to optimize the target paste code, it optimizes the target paste code based on the context-dependent data to optimize the code. It can be understood that when the code optimization model optimizes the target paste code based on the context-dependent data, it will determine whether the target paste code needs to be optimized. For example, if the optimized code corresponding to the target paste code obtained by the code optimization model based on the context-dependent data does not completely match the target paste code, it is determined to optimize the target paste code; if the optimized code corresponding to the target paste code obtained by the code optimization model based on the context-dependent data completely matches the target paste code, it is determined not to optimize the target paste code.

[0071] Optionally, when it is determined that the target pasted code is not to be optimized, the output of the code optimization model is empty.

[0072] In this embodiment, when it is determined that the target pasted code is not to be optimized, the code optimization model may not output any information to ensure that the developer is unaware and does not affect the developer's development efficiency.

[0073] Optionally, the code optimization performed by the code optimization model includes at least one of the following: comment addition, comment modification, variable modification, function name modification, code format adjustment, configuration parameter adjustment, code symbol completion, and business logic change.

[0074] It can be understood that when optimizing the code through the code optimization model, the code format, quality, context understanding and semantic adaptation are comprehensively considered, and the code optimization categories can be divided into multiple categories such as comment addition, comment modification, variable modification, function name modification, code format adjustment, configuration parameter adjustment, code symbol completion and business logic change. Specific code optimization categories may include more than 20 categories, so as to ensure the consistency of the style and logic of the optimized code with the existing code in the current code development environment, and seamlessly integrate the optimized code into the current code development environment.

[0075] In the present disclosure, Figure 2 The step S204 may further include the following two steps S304 and S305:

[0076] S304: Determine a first difference code between the optimized code and the target pasted code.

[0077] Exemplarily, the optimized code and the target pasted code may be compared in a diff manner (a technique for comparing differences between two files or code versions) to determine a first difference code between the optimized code and the target pasted code.

[0078] S305: Output a first difference code.

[0079] Exemplarily, after determining the first difference code between the optimized code and the target pasted code, the first difference code can be output. Optionally, the first difference code can be highlighted to facilitate the developer to observe and understand the code where the difference occurs. For example, the second difference code included in the target pasted code and the third difference code included in the optimized code can be highlighted, bolded, or displayed in a specific color, etc., respectively, wherein the first difference code includes the second difference code and the third difference code, and the embodiment of the present disclosure does not limit the display method of the difference code.

[0080] S306 . In response to receiving an acceptance instruction for the optimized code, replace the second difference code included in the target pasted code with the third difference code included in the optimized code, where the first difference code includes the second difference code and the third difference code.

[0081] In this step, after the developer determines to adopt the optimized code corresponding to the target pasted code, for example, the accept instruction for the optimized code can be triggered by the Tab key (a function key on the computer keyboard). Accordingly, the electronic device executing the embodiment of the method responds to the accept instruction for the optimized code, and replaces the second difference code included in the target pasted code with the third difference code included in the optimized code, and the first difference code includes the second difference code and the third difference code. It is understandable that the developer can adopt some or all of the difference codes in the optimized code. It should be noted that the embodiment of the present disclosure does not limit the triggering method of the accept instruction.

[0082] S307: Mark the target pasted code and the optimized code as valid training data, and the valid training data is used to train the code optimization model.

[0083] In this step, after the second difference code included in the target pasted code is replaced with the third difference code included in the optimized code, it indicates that the optimized code is a high-quality code. Therefore, the target pasted code and the optimized code can be marked as valid training data to continue to be used for training the code optimization model. In this way, training data can be continuously accumulated and the effect of the code optimization model can be continuously optimized.

[0084] S308: In response to receiving a cancel instruction for the optimized code, or in response to determining that the optimized code has stayed in the code development environment for a preset time, retaining the target pasted code.

[0085] In this step, after the developer determines not to adopt the optimized code corresponding to the target pasted code, the developer can trigger a cancel instruction for the optimized code, for example, by using the ESC key (a function key on a computer keyboard). Accordingly, the electronic device executing the embodiment of the present method responds to the cancel instruction for the optimized code, cancels the display of the optimized code, and retains the target pasted code. It should be noted that the embodiment of the present disclosure does not limit the triggering method of the cancel instruction. Alternatively, when the residence time of the optimized code in the code development environment reaches a preset residence time (such as 500 milliseconds), the developer does not perform any operation, indicating that the developer does not adopt the optimized code corresponding to the target pasted code, and therefore, the electronic device executing the embodiment of the present method cancels the display of the optimized code and retains the target pasted code.

[0086] S309: Mark the target pasted code and the optimized code as invalid training data.

[0087] In this step, while maintaining the target pasted code, it indicates that the quality of the optimized code is not high. Therefore, the target pasted code and the optimized code can be marked as invalid training data. The reasons for the low quality of the optimized code can be analyzed manually later.

[0088] It should be noted that the embodiment of the present disclosure does not limit the execution order of S306 and S308.

[0089] In the disclosed embodiment, by performing dependency analysis on the relevant information of the acquired target paste code, context dependency data corresponding to the target paste code is obtained, and the relevant information includes at least one of the file information, class information and function information that the target paste code depends on, so that the overall code structure and logic can be deeply understood, and the code format, quality, context understanding and semantic adaptation can be comprehensively considered. The target paste code and the context dependency data are input into the code optimization model, and when it is determined that the target paste code is to be optimized, the target paste code is optimized based on the context dependency data to obtain the optimized code; the first difference code between the optimized code and the target paste code is determined, and the first difference code is output, so as to realize automatic code optimization of the target paste code, which can greatly improve the development efficiency, ensure the consistency of the optimized code with the existing code in the current code development environment in style and logic, seamlessly integrate the optimized code into the current code development environment, improve the code quality, reduce the potential problems caused by code mismatch, and improve the coding experience of developers. In response to receiving an acceptance instruction for the optimized code, the second difference code included in the target pasted code is replaced with the third difference code included in the optimized code, the first difference code includes the second difference code and the third difference code, and the target pasted code and the optimized code are marked as valid training data, so that the training data can be continuously accumulated and the code optimization model effect can be continuously optimized.

[0090] The code processing method provided by the disclosed embodiment is not only applicable to various programming languages ​​and development environments, but also can provide a smarter and smoother coding experience for the development team. In particular, in terms of AI-assisted programming, the disclosed embodiment can be seamlessly integrated with code generation AI to provide smarter coding assistance, further promoting the improvement of software development efficiency and quality.

[0091] Figure 4 is a schematic diagram according to the third embodiment of the present disclosure. Figure 4 As shown, the code model training method provided in the third embodiment of the present disclosure can be applied to an electronic device, which may be a server or a server cluster, etc. The code model training method provided in the third embodiment of the present disclosure includes:

[0092] S401. Obtain a training data set, where the training data in the training data set includes a pasted code sample, a context-dependent data sample of the pasted code sample, and a label code, where the label code is a code collected within a coordinate range of the pasted code sample when the pasted code sample is pasted into a code development environment and a preset waiting time is retained.

[0093] In the disclosed embodiment, the training data in the training data set is obtained based on the data actually annotated by the developer in the code development environment, which can ensure the authenticity, diversity and high quality of the training data set from the source of the data, and is extremely consistent with the programming habits of the developer. Exemplarily, in response to monitoring the sample code paste event of the developer in the integrated development environment, when the pasted code sample is pasted into the integrated development environment and the preset waiting time (such as 15 seconds) is retained, the code within the coordinate range of the pasted code sample is collected. The collected code is the real use code after the developer confirms, which can be used as a label code. Based on the collected code and the pasted code sample, the training data is obtained, and the training data includes the pasted code sample, the context-dependent data sample of the pasted code sample, and the label code. For how to obtain the training data specifically, please refer to the subsequent embodiments, which will not be repeated here. In this step, a high-quality and diverse training data set derived from the developer's real annotations can be obtained.

[0094] S402. Based on the training data set, iteratively train the code optimization model until the calculated loss function value meets the preset evaluation condition or the number of iterations reaches the preset number of iterations, thereby obtaining the trained code optimization model.

[0095] Exemplarily, the code optimization model can be fine-tuned and trained based on a general code model. The training data in the training data set is input into the code optimization model, and the code optimization model is iteratively trained. For example, the loss function value can be obtained based on the predicted code and label code output by the code optimization model, and the model parameters of the code optimization model can be adjusted according to the loss function value. When the loss function value meets the preset evaluation condition, the trained code optimization model is obtained. Alternatively, the training can be stopped when the number of iterations reaches the preset number of iterations to obtain the trained code optimization model.

[0096] Optionally, the code optimization performed by the code optimization model includes at least one of the following: comment addition, comment modification, variable modification, function name modification, code format adjustment, configuration parameter adjustment, code symbol completion, and business logic change.

[0097] It is understandable that in order to ensure the diversity of training data, after comprehensive consideration of code format, quality, context understanding and semantic adaptation, code optimization categories can be divided into multiple categories such as comment addition, comment modification, variable modification, function name modification, code format adjustment, configuration parameter adjustment, code symbol completion and business logic change to ensure a balanced proportion of training data in each category. Specific code optimization categories can include more than 20 categories, for example, to ensure the consistency of style and logic between the optimized code and the existing code in the current code development environment, and to seamlessly integrate the optimized code into the current code development environment.

[0098] In the disclosed embodiment, the training data in the training data set includes pasted code samples, context-dependent data samples of the pasted code samples, and labeled codes, which are obtained based on data actually annotated by developers in a code development environment. The authenticity, diversity, and high quality of the training data set can be guaranteed from the source of the data, and it is extremely consistent with the programming habits of developers. Based on the high-quality training data set, the code optimization model is iteratively trained to ensure the output quality of the trained code optimization model. Therefore, when the code optimization model is used to optimize the pasted code, the style and logic consistency of the optimized code with the existing code in the current code development environment can be ensured, and the optimized code can be seamlessly integrated into the current code development environment to improve the code quality.

[0099] Figure 5 is a schematic diagram according to the fourth embodiment of the present disclosure. Figure 5 As shown, the method for obtaining a training data set provided in the fourth embodiment of the present disclosure includes:

[0100] S501 . In response to monitoring a sample code pasting event, obtaining a pasted code sample and a starting coordinate and an ending coordinate of the pasted code sample in a code development environment.

[0101] For example, during the software development process, the developer pastes code from other places into the integrated development environment. Accordingly, the electronic device executing the embodiment of the present method can monitor the sample code paste event, thereby obtaining the pasted code sample and the starting coordinates and ending coordinates of the pasted code sample in the integrated development environment. The pasted code sample can also be understood as the original code block to be pasted. Figure 6 is a schematic diagram according to a fifth embodiment of the present disclosure, Figure 6 As shown, the pasted code sample contains three lines of code, Figure 6 The starting and ending coordinates of the pasted code sample in the integrated development environment are shown.

[0102] S502: When the retention time of the pasted code sample in the code development environment reaches a preset waiting time, obtain the first code within the range of the start coordinates and the end coordinates.

[0103] Exemplarily, the preset waiting time is 15 seconds, and the present disclosure embodiment does not limit the preset waiting time. After pasting the code sample into the code development environment, wait for 15 seconds to obtain the first code within the start coordinate and end coordinate range, which is the real use code confirmed by the developer and can be used as a label code.

[0104] S503: Determine whether the pasted code sample matches the first code.

[0105] In this step, by comparing the pasted code sample with the first code, it can be determined whether the pasted code sample matches the first code, that is, whether the pasted code sample has changed. If the pasted code sample and the first code completely match, the pasted code sample and the first code are marked as "no change"; if the pasted code sample and the first code do not completely match, the pasted code sample and the first code are marked as "change". In the above manner, the developer's real annotated data can be obtained, including situations where changes are required and where changes are not required, so that the code optimization model can accurately learn when code optimization adjustments are required. If the pasted code sample and the first code do not match, steps S504 and S505 are executed; if the pasted code sample and the first code match, step S506 is executed.

[0106] S504: In response to determining that the pasted code sample does not match the first code, obtain a context-dependent data sample corresponding to the pasted code sample; input the pasted code sample and the context-dependent data sample into a higher-level target model to obtain a second code output by the target model.

[0107] Exemplarily, after determining that the pasted code sample and the first code do not match, the pasted code sample and the first code are marked as "changed", and dependency analysis is performed on the "changed" pasted code sample, such as analyzing which files, classes, or functions (such as function signatures, function return values) the pasted code sample depends on, as key data for analyzing the context, thereby obtaining a context-dependent data sample of the pasted code sample. In order to further ensure the high quality of the training data, the pasted code sample and the context-dependent data sample can be assembled into a prompt, input into a higher-level target model for change, and obtain the second code output by the target model.

[0108] S505, obtaining the similarity between the second code and the first code; in response to determining that the similarity is greater than or equal to the similarity threshold, determining that the pasted code sample, the context-dependent data sample of the pasted code sample, and the first code are training data, and the first code is a label code; in response to determining that the similarity is less than the similarity threshold, determining that the pasted code sample is not training data.

[0109] Exemplarily, the data level can be divided according to different similarity thresholds. For example, the data can be graded in order of similarity from high to low, and the data can be divided into four levels: P0, P1, P2, and P3, where the similarity threshold corresponding to the P0 level is 90%. In this step, the similarity between the second code and the first code can be obtained. If the similarity is greater than or equal to the similarity threshold of 90%, the pasted code sample, the context-dependent data sample of the pasted code sample, and the first code are determined to be training data, and the first code is a label code, that is, P0 level data is preferentially selected as valid training data. This step can be understood as data cleaning processing of the training data to obtain high-quality training data.

[0110] S506: In response to determining that the pasted code sample matches the first code, determine that the pasted code sample, the context-dependent data sample of the pasted code sample, and the first code are training data, and the first code is a label code.

[0111] In this step, after determining that the pasted code sample and the first code completely match and marking the pasted code sample and the first code as "unchanged", the pasted code sample, the context-dependent data sample of the pasted code sample and the first code can be used as training data, wherein the first code is the label code.

[0112] In the disclosed embodiment, in response to monitoring a sample code paste event, the paste code sample and the starting coordinates and ending coordinates of the paste code sample in the code development environment are obtained. When the retention time of the paste code sample in the code development environment reaches a preset waiting time, the first code within the range of the starting coordinates and the ending coordinates is obtained, and it is determined whether the paste code sample and the first code match, so that the training data truly annotated by the developer in the code development environment can be obtained, and the authenticity of the training data can be guaranteed from the source of the data. In response to determining that the paste code sample and the first code do not match, the context-dependent data sample corresponding to the paste code sample is obtained, and the paste code sample and the context-dependent data sample are input into a higher-level target model to obtain a second code output by the target model. Based on the similarity between the second code and the first code, the training data is cleaned, which can further improve the quality of the training data, so that the training data is extremely consistent with the programming habits of the developer, and finally a high-quality and diversified training data set derived from the developer's true annotation is obtained, which is used for the training of the code optimization model, and finally the high-quality output of the code optimization model is guaranteed.

[0113] Based on the above embodiments, it can be understood that the method for processing code provided by the embodiments of the present disclosure will bring significant competitive advantages to the intelligent coding assistant, and can greatly enhance the market value and leading position of the product. By analyzing the context of the pasted code in real time and automatically optimizing the pasted code, not only can the development efficiency be greatly improved, but also the code quality and consistency can be ensured, so that the intelligent coding assistant can attract more developers and enterprise users, thereby expanding market share and consolidating the leading position of the product. Especially in the context of the increasing popularity of AI-assisted programming, the method for processing code provided by the embodiments of the present disclosure will become a key differentiating feature of the intelligent coding assistant, which will help attract and retain high-value customers and further consolidate the product's market position in the field of AI-assisted programming.

[0114] In addition, the code processing method provided by the embodiments of the present disclosure can build a strong technical barrier for the intelligent coding assistant and lay a solid foundation for future technological innovation and product iteration. Among them, the code optimization model trained based on a large amount of real development data, combined with the ability of real-time context analysis and code optimization for pasted code, can protect the core competitiveness of the product, and can continuously improve the developer experience and enhance developer stickiness. Based on the code processing method provided by the embodiments of the present disclosure, the intelligent coding assistant will establish obvious advantages in code quality, development efficiency, and developer experience, thereby maintaining a long-term leading position in the fierce market competition.

[0115] The following are embodiments of the device disclosed herein, which can be used to execute the method embodiments disclosed herein. For details not disclosed in the device embodiments disclosed herein, please refer to the method embodiments disclosed herein.

[0116] Figure 7 is a schematic diagram according to the sixth embodiment of the present disclosure. Figure 7 As shown, the code processing device 700 provided in the sixth embodiment of the present disclosure includes: an acquisition unit 701, a dependency analysis unit 702, a code optimization unit 703 and an output unit 704. Among them:

[0117] The acquisition unit 701 is used to acquire the target paste code in response to monitoring the code paste event.

[0118] The dependency analysis unit 702 is used to perform dependency analysis on the target paste code to obtain context dependency data corresponding to the target paste code.

[0119] The code optimization unit 703 is used to optimize the target pasted code according to the context dependency data to obtain the optimized code corresponding to the target pasted code.

[0120] The output unit 704 is used to output the optimized code.

[0121] In some embodiments, the code optimization unit 703 includes: a code optimization module (not shown in the figure), which is used to input the target pasted code and context dependency data into a code optimization model, and when it is determined that the target pasted code is to be optimized, optimize the target pasted code based on the context dependency data to obtain optimized code; wherein the code optimization model is obtained by training based on pasted code samples in the code development environment, context dependency data samples of the pasted code samples, and label codes, and the label code is the code collected within the coordinate range of the pasted code sample when the pasted code sample is pasted into the code development environment and a preset waiting time is retained.

[0122] In some embodiments, the code optimization module is further configured to: when it is determined that the target pasted code is not to be optimized, the output of the code optimization model is empty.

[0123] In some embodiments, code optimization includes at least one of the following: comment addition, comment modification, variable modification, function name modification, code format adjustment, configuration parameter adjustment, code symbol completion, and business logic change.

[0124] In some embodiments, the dependency analysis unit 702 includes: a dependency analysis module (not shown in the figure), which is used to perform dependency analysis on the relevant information of the target paste code to obtain context dependency data corresponding to the target paste code, and the relevant information includes at least one of the file information, class information and function information that the target paste code depends on.

[0125] In some embodiments, the code processing apparatus 700 further includes a first processing unit (not shown in the figure) configured to determine a first difference code between the optimized code and the target pasted code; and output the first difference code.

[0126] In some embodiments, the device 700 for processing code also includes a second processing unit (not shown in the figure), which is used to replace the second difference code included in the target pasted code with the third difference code included in the optimized code in response to receiving an acceptance instruction for the optimized code, and the first difference code includes the second difference code and the third difference code; mark the target pasted code and the optimized code as valid training data, and the valid training data is used to train the code optimization model.

[0127] In some embodiments, the device 700 for processing code also includes a third processing unit (not shown in the figure), which is used to retain the target pasted code in response to receiving a cancel instruction for the optimized code, or in response to determining that the optimized code has stayed in the code development environment for a preset stay time; and mark the target pasted code and the optimized code as invalid training data.

[0128] Figure 7The provided device for processing code can execute the steps in the method embodiment corresponding to the above-mentioned method for processing code. Its implementation principle and technical effect are similar and will not be repeated here.

[0129] Figure 8 is a schematic diagram according to the seventh embodiment of the present disclosure. Figure 8 As shown, the code model training device 800 provided in the seventh embodiment of the present disclosure includes: an acquisition unit 801 and a training unit 802.

[0130] in:

[0131] The acquisition unit 801 is used to acquire a training data set, where the training data in the training data set includes a pasted code sample, a context-dependent data sample of the pasted code sample, and a label code. The label code is a code collected within a coordinate range of the pasted code sample when the pasted code sample is pasted into a code development environment and a preset waiting time is retained.

[0132] The training unit 802 is used to iteratively train the code optimization model based on the training data set until the calculated loss function value meets the preset evaluation condition or the number of iterations reaches the preset number of iterations, thereby obtaining the trained code optimization model.

[0133] In some embodiments, the acquisition unit 801 includes: a first acquisition module (not shown in the figure), which is used to obtain the pasted code sample and the starting coordinates and ending coordinates of the pasted code sample in the code development environment in response to monitoring the sample code paste event; a second acquisition module (not shown in the figure), which is used to obtain the first code within the range of the starting coordinates and the ending coordinates when the retention time of the pasted code sample in the code development environment reaches a preset waiting time; a first processing module (not shown in the figure), which is used to obtain the context-dependent data sample corresponding to the pasted code sample in response to determining that the pasted code sample and the first code do not match; and the pasted code sample and the context-dependent data sample are combined. The method inputs a higher-level target model to obtain a second code output by the target model; obtains the similarity between the second code and the first code; in response to determining that the similarity is greater than or equal to a similarity threshold, determines that the pasted code sample, the context-dependent data sample of the pasted code sample, and the first code are training data, and the first code is a label code; in response to determining that the similarity is less than the similarity threshold, determines that the pasted code sample is not training data; a second processing module (not shown in the figure) is used to determine that the pasted code sample, the context-dependent data sample of the pasted code sample, and the first code are training data, and the first code is a label code in response to determining that the pasted code sample matches the first code.

[0134] In some embodiments, code optimization includes at least one of the following: comment addition, comment modification, variable modification, function name modification, code format adjustment, configuration parameter adjustment, code symbol completion, and business logic change.

[0135] Figure 8 The provided code model training device can execute the steps in the method embodiment corresponding to the above-mentioned code model training method. Its implementation principle and technical effects are similar and will not be repeated here.

[0136] According to an embodiment of the present disclosure, the present disclosure also provides an electronic device, which includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the solution provided by any of the above embodiments.

[0137] According to an embodiment of the present disclosure, the present disclosure further provides a non-transitory computer-readable storage medium storing computer instructions, wherein the computer instructions are used to enable a computer to execute a solution provided by any of the above embodiments.

[0138] According to an embodiment of the present disclosure, the present disclosure also provides a computer program product, which includes: a computer program, the computer program is stored in a readable storage medium, at least one processor of an electronic device can read the computer program from the readable storage medium, and at least one processor executes the computer program so that the electronic device executes the solution provided by any of the above embodiments.

[0139] Fig. 9 Schematic block diagram of an example electronic device 900 that can be used to implement an embodiment of the present disclosure. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital assistants, cellular phones, smart phones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present disclosure described and / or claimed herein.

[0140] like Fig. 9 As shown, the electronic device 900 includes a computing unit 901, which can store data stored in a read-only memory (ROM) ( Fig. 9 A computer program in ROM 902 is loaded from storage unit 908 to random access memory (RAM) ( Fig. 9 The computer program in the RAM 903 (for example) can be used to perform various appropriate actions and processes. The RAM 903 can also store various programs and data required for the operation of the electronic device 900. The computing unit 901, the ROM 902, and the RAM 903 are connected to each other via a bus 904. The input / output (I / O) interface ( Fig. 9 The I / O interface 905 (for example) is also connected to the bus 904 .

[0141] Multiple components in the electronic device 900 are connected to the I / O interface 905, including: an input unit 906, such as a keyboard, a mouse, etc.; an output unit 907, such as various types of displays, speakers, etc.; a storage unit 908, such as a disk, an optical disk, etc.; and a communication unit 909, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 909 allows the electronic device 900 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.

[0142] The computing unit 901 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the computing unit 901 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, digital signal processors (DSP), and any appropriate processors, controllers, microcontrollers, etc. The computing unit 901 performs the various methods and processes described above, such as methods for processing codes. For example, in some embodiments, the method for processing codes may be implemented as a computer software program, which is tangibly included in a machine-readable medium, such as a storage unit 908. In some embodiments, part or all of the computer program may be loaded and / or installed on the electronic device 900 via the ROM 902 and / or the communication unit 909. When the computer program is loaded into the RAM 903 and executed by the computing unit 901, one or more steps of the method for processing codes described above may be executed. Alternatively, in other embodiments, the computing unit 901 may be configured to execute the method of processing codes in any other appropriate manner (eg, by means of firmware).

[0143] Various embodiments of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard parts (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.

[0144] The program code for implementing the method of the present disclosure may be written in any combination of one or more programming languages. These program codes may be provided to a processor or controller of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that the program code, when executed by the processor or controller, enables the functions / operations specified in the flow chart and / or block diagram to be implemented. The program code may be executed entirely on the machine, partially on the machine, partially on the machine and partially on a remote machine as a stand-alone software package, or entirely on a remote machine or server.

[0145] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.

[0146] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user can provide input to the computer. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).

[0147] The systems and techniques described herein can be implemented in a computing system that includes back-end components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes front-end components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such back-end components, middleware components, or front-end components. The components of the system can be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: Local Area Networks (LANs), Wide Area Networks (WANs), and the Internet.

[0148] A computer system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The relationship between the client and the server is generated by computer programs running on the corresponding computers and having a client-server relationship with each other. The server may be a cloud server, also known as a cloud computing server or cloud host, which is a host product in the cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and VPS services ("Virtual Private Server", or "VPS" for short). The server may also be a server of a distributed system, or a server combined with a blockchain.

[0149] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps recorded in this disclosure can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solutions disclosed in this disclosure can be achieved, and this document does not limit this.

[0150] The above specific implementations do not constitute a limitation on the protection scope of the present disclosure. It should be understood by those skilled in the art that various modifications, fusions, sub-fusions and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present disclosure should be included in the protection scope of the present disclosure.

Claims

1. A method for processing code, comprising: In response to monitoring the code paste event, obtaining the target paste code; Performing dependency analysis on the target paste code to obtain context dependency data corresponding to the target paste code; According to the context dependency data, code optimization is performed on the target pasted code to obtain optimized code corresponding to the target pasted code; The optimized code is output.

2. The method according to claim 1, wherein: The step of optimizing the target pasted code according to the context dependent data to obtain optimized code corresponding to the target pasted code includes: By inputting the target paste code and the context dependency data into a code optimization model, when it is determined that the target paste code is to be optimized, the target paste code is optimized based on the context dependency data to obtain the optimized code; The code optimization model is obtained by training based on a pasted code sample in a code development environment, a context-dependent data sample of the pasted code sample, and a label code, wherein the label code is the code collected within the coordinate range of the pasted code sample when the pasted code sample is pasted into the code development environment and a preset waiting time is retained.

3. The method according to claim 2, wherein: The method further comprises: When it is determined that the target pasted code is not to be optimized, the output of the code optimization model is empty.

4. The method according to claim 2, wherein: The code optimization includes at least one of the following: Comment addition, comment modification, variable modification, function name modification, code format adjustment, configuration parameter adjustment, code symbol completion and business logic change.

5. The method according to any one of claims 1 to 4, wherein: The performing dependency analysis on the target paste code to obtain context dependency data corresponding to the target paste code includes: Dependency analysis is performed on the relevant information of the target paste code to obtain context dependency data corresponding to the target paste code, wherein the relevant information includes at least one of file information, class information and function information that the target paste code depends on.

6. The method according to claim 2, wherein: The method further comprises: Determining a first difference code between the optimized code and the target pasted code; The first difference code is output.

7. The method according to claim 6, wherein: The method further comprises: In response to receiving an acceptance instruction for the optimized code, replacing a second difference code included in the target pasted code with a third difference code included in the optimized code, wherein the first difference code includes the second difference code and the third difference code; The target pasted code and the optimized code are marked as valid training data, and the valid training data is used to train the code optimization model.

8. The method according to claim 7, wherein: The method further comprises: In response to receiving a cancel instruction for the optimized code, or in response to determining that the optimized code has stayed in the code development environment for a preset stay time, retaining the target pasted code; The target pasted code and the optimized code are marked as invalid training data.

9. A code model training method, comprising: Acquire a training data set, wherein the training data in the training data set includes a pasted code sample, a context-dependent data sample of the pasted code sample, and a label code, wherein the label code is a code collected within a coordinate range of the pasted code sample when the pasted code sample is pasted into a code development environment and a preset waiting time is retained; Based on the training data set, the code optimization model is iteratively trained until the calculated loss function value meets the preset evaluation condition or the number of iterations reaches the preset number of iterations, thereby obtaining the trained code optimization model.

10. The method according to claim 9, wherein: The step of obtaining a training data set includes: In response to monitoring a sample code pasting event, obtaining a pasted code sample and a starting coordinate and an ending coordinate of the pasted code sample in the code development environment; When the retention time of the pasted code sample in the code development environment reaches the preset waiting time, acquiring the first code within the range of the starting coordinates and the ending coordinates; In response to determining that the pasted code sample and the first code do not match, obtaining a context-dependent data sample corresponding to the pasted code sample; inputting the pasted code sample and the context-dependent data sample into a higher-level target model to obtain a second code output by the target model; obtaining a similarity between the second code and the first code; in response to determining that the similarity is greater than or equal to a similarity threshold, determining that the pasted code sample, the context-dependent data sample of the pasted code sample, and the first code are training data, and the first code is a label code; In response to determining that the pasted code sample matches the first code, it is determined that the pasted code sample, the context-dependent data sample of the pasted code sample, and the first code are training data, and the first code is a label code.

11. The method according to claim 9 or 10, wherein: The code optimization includes at least one of the following: Comment addition, comment modification, variable modification, function name modification, code format adjustment, configuration parameter adjustment, code symbol completion and business logic change.

12. A device for processing code, comprising: An acquisition unit, configured to acquire a target paste code in response to monitoring a code paste event; A dependency analysis unit, configured to perform dependency analysis on the target paste code to obtain context dependency data corresponding to the target paste code; A code optimization unit, configured to optimize the target pasted code according to the context dependency data to obtain an optimized code corresponding to the target pasted code; An output unit is used to output the optimized code.

13. The device according to claim 12, wherein: The code optimization unit comprises: A code optimization module, configured to input the target pasted code and the context dependency data into a code optimization model, and when it is determined that the target pasted code is to be optimized, optimize the target pasted code based on the context dependency data to obtain the optimized code; The code optimization model is obtained by training based on a pasted code sample in a code development environment, a context-dependent data sample of the pasted code sample, and a label code, wherein the label code is the code collected within the coordinate range of the pasted code sample when the pasted code sample is pasted into the code development environment and a preset waiting time is retained.

14. The device according to claim 13, wherein: The code optimization module is also used for: When it is determined that the target pasted code is not to be optimized, the output of the code optimization model is empty.

15. The device according to claim 13, wherein: The code optimization includes at least one of the following: Comment addition, comment modification, variable modification, function name modification, code format adjustment, configuration parameter adjustment, code symbol completion and business logic change.

16. The device according to any one of claims 12 to 15, wherein: The dependency analysis unit comprises: The dependency analysis module is used to perform dependency analysis on the relevant information of the target paste code to obtain context dependency data corresponding to the target paste code, wherein the relevant information includes at least one of file information, class information and function information that the target paste code depends on.

17. The device according to claim 13, wherein: The device also includes a first processing unit, which is used to determine a first difference code between the optimized code and the target pasted code; and output the first difference code.

18. The device according to claim 17, wherein: The device also includes a second processing unit, which is used to replace the second difference code included in the target pasted code with the third difference code included in the optimized code in response to receiving an acceptance instruction for the optimized code, wherein the first difference code includes the second difference code and the third difference code; and mark the target pasted code and the optimized code as valid training data, wherein the valid training data is used to train the code optimization model.

19. The device according to claim 18, wherein: The device also includes a third processing unit, which is used to retain the target pasted code in response to receiving a cancellation instruction for the optimized code, or in response to determining that the stay time of the optimized code in the code development environment reaches a preset stay time; and mark the target pasted code and the optimized code as invalid training data.

20. A code model training device, comprising: an acquisition unit, configured to acquire a training data set, wherein the training data in the training data set includes a pasted code sample, a context-dependent data sample of the pasted code sample, and a label code, wherein the label code is a code collected within a coordinate range of the pasted code sample when the pasted code sample is pasted into a code development environment and a preset waiting time is retained; A training unit is used to iteratively train the code optimization model based on the training data set until the calculated loss function value meets the preset evaluation condition or the number of iterations reaches the preset number of iterations, thereby obtaining a trained code optimization model.

21. The device according to claim 20, wherein: The acquisition unit comprises: A first acquisition module, configured to acquire a pasted code sample and a starting coordinate and an ending coordinate of the pasted code sample in the code development environment in response to monitoring a sample code pasting event; A second acquisition module is used to acquire a first code within the range of the starting coordinates and the ending coordinates when the retention time of the pasted code sample in the code development environment reaches the preset waiting time; a first processing module, configured to, in response to determining that the pasted code sample and the first code do not match, obtain a context-dependent data sample corresponding to the pasted code sample; input the pasted code sample and the context-dependent data sample into a higher-level target model to obtain a second code output by the target model; obtain a similarity between the second code and the first code; and, in response to determining that the similarity is greater than or equal to a similarity threshold, determine that the pasted code sample, the context-dependent data sample of the pasted code sample, and the first code are training data, and that the first code is a label code; The second processing module is used for, in response to determining that the pasted code sample matches the first code, determining that the pasted code sample, the context-dependent data sample of the pasted code sample, and the first code are training data, and the first code is a label code.

22. The device according to claim 20 or 21, wherein: The code optimization includes at least one of the following: Comment addition, comment modification, variable modification, function name modification, code format adjustment, configuration parameter adjustment, code symbol completion and business logic change.

23. An electronic device, comprising: at least one processor; as well as a memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the method according to any one of claims 1 to 11.

24. A non-transitory computer-readable storage medium storing computer instructions, wherein: The computer instructions are used to cause the computer to execute the method according to any one of claims 1 to 11.

25. A computer program product comprising a computer program, which, when executed by a processor, implements the steps of the method according to any one of claims 1 to 11.

Citation Information

Cited By

  • Code annotation analysis method, computer equipment, storage medium and product

    CN120122988A

  • Code generation model training method, device and equipment

    CN120873595A

  • Content generation method and apparatus, device, and storage medium

    WO2026188435A1