# US 2026/0064393 A1 · Method and apparatus for processing code, and method and apparatus for training code model, and device

> Agents: this data is available as JSON from the patentgrep API and MCP server — see https://patentgrep.com/llms.txt

- Number: US 2026/0064393 A1
- Document ID: US20260064393A1
- Status: Published application
- Kind: A1
- Application number: 18/989,191
- Filed: 2024-12-20
- Published: 2026-03-05
- Earliest claimed: 2024-09-05
- Assignees: Beijing Baidu Netcom Science Technology Co., Ltd.
- Applicants: Beijing Baidu Netcom Science Technology Co., Ltd.
- Inventors: Wanpeng NIU; Hui LI; Yunpeng PENG; Junwei XING; Bingfei ZHANG; Xiangfei MIN; Ze CHEN
- Web page: https://patentgrep.com/patent/US20260064393A1

## CPC

- G06F 8/443: {Optimisation}
- G06F 8/433: {Dependency analysis; Data or control flow analysis}
- G06F 8/72: Code refactoring
- G06F: ELECTRIC DIGITAL DATA PROCESSING (computer systems based on specific computational models G06N)

## Abstract

The present disclosure provides a method and apparatus for processing code, a method and apparatus for training a code model, and a device, which relates to the field of artificial intelligence, and in particular, to the technical fields of software development, data processing, large models and computer communication. The method for processing code includes: acquiring target pasted code in response to a code paste event being monitored; performing dependency analysis for the target pasted code to obtain context-dependent data corresponding to the target pasted code; performing, according to the context-dependent data, code optimization for the target pasted code to obtain optimized code corresponding to the target pasted code; and outputting the optimized code. The present disclosure can greatly improve development efficiency, seamlessly integrate optimized code into the current code development environment, and improve code quality.

## Claims

### Claim 1

A method for processing code, comprising:
acquiring target pasted code in response to a code paste event being monitored;
performing dependency analysis for the target pasted code to obtain context-dependent data corresponding to the target pasted code;
performing, according to the context-dependent data, code optimization for the target pasted code to obtain optimized code corresponding to the target pasted code; and
outputting the optimized code.

### Claim 2 (depends on 1)

The method according to claim 1, wherein the performing, according to the context-dependent data, code optimization for the target pasted code to obtain the optimized code corresponding to the target pasted code comprises:
by inputting the target pasted code and the context-dependent data into a code optimization model, performing code optimization for the target pasted code based on the context-dependent data to obtain the optimized code;
wherein the code optimization model is acquired based on training with a pasted code sample in a code development environment, a context-dependent data sample of the pasted code sample and label code, the label code is code collected within a coordinate range of the pasted code sample in a case that the pasted code sample is pasted into the code development environment and passes a preset waiting time.

### Claim 3 (depends on 2)

The method according to claim 2, wherein the method further comprises:
an output of the code optimization model is empty in a case that the target pasted code is determined not to be optimized.

### Claim 4 (depends on 2)

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

### Claim 5 (depends on 1)

The method according to claim 1, wherein the performing dependency analysis for the target pasted code to obtain the context-dependent data corresponding to the target pasted code comprises:
performing the dependency analysis for relevant information of the target pasted code to obtain the context-dependent data corresponding to the target pasted code, wherein the relevant information comprises at least one of file information, class information or function information that the target pasted code depends on.

### Claim 6 (depends on 2)

The method according to claim 2, wherein the method further comprises:
determining first difference code between the optimized code and the target pasted code; and
outputting the first difference code.

### Claim 7 (depends on 6)

The method according to claim 6, wherein the method further comprises:
in response to an acceptance instruction for the optimized code being received, replacing second difference code comprised in the target pasted code with third difference code comprised in the optimized code, wherein the first difference code comprises the second difference code and the third difference code; and
marking the target pasted code and the optimized code as valid training data, wherein the valid training data is used for training the code optimization model.

### Claim 8 (depends on 7)

The method according to claim 7, wherein the method further comprises:
in response to a cancellation instruction for the optimized code being received, or in response to determining that a stay time of the optimized code in the code development environment reaches a preset stay time, retaining the target pasted code; and
marking the target pasted code and the optimized code as invalid training data.

### Claim 9

A method for training a code model, comprising:
acquiring a training data set, wherein training data in the training data set comprises a pasted code sample, a context-dependent data sample of the pasted code sample and label code, the label code is code collected within a coordinate range of the pasted code sample in a case that the pasted code sample is pasted into a code development environment and passes a preset waiting time;
iteratively training, based on the training data set, a code optimization model until a calculated loss function value meets a preset evaluation condition or a number of iterations reaches a preset number of iterations, to obtain a trained code optimization model.

### Claim 10 (depends on 9)

The method according to claim 9, wherein the acquiring the training data set comprises:
in response to a sample code paste event being monitored, acquiring the pasted code sample and a start coordinate and an end coordinate of the pasted code sample in the code development environment;
when the pasted code sample in the code development environment passes the preset waiting time, acquiring first code within a range of the start coordinate and the end coordinate;
in response to determining that the pasted code sample does not match the first code, acquiring the 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 second code outputted from the target model; acquiring 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 the training data, and the first code is the label code;
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 the training data, and the first code is the label code.

### Claim 11 (depends on 9)

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

### Claim 12

An apparatus for processing code, comprising:
at least one processor; and
a memory communicating with the at least one processor; wherein
the memory stores instructions that can be executed 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:
acquire target pasted code in response to a code paste event being monitored;
perform dependency analysis for the target pasted code to obtain context-dependent data corresponding to the target pasted code;
perform, according to the context-dependent data, code optimization for the target pasted code to obtain optimized code corresponding to the target pasted code; and
output the optimized code.

### Claim 13 (depends on 12)

The apparatus according to claim 12, wherein the instructions are executed by the at least one processor to enable the at least one processor further to:
input the target pasted code and the context-dependent data into a code optimization model, perform code optimization for the target pasted code based on the context-dependent data to obtain the optimized code;
wherein the code optimization model is acquired based on training with a pasted code sample in a code development environment, a context-dependent data sample of the pasted code sample and label code, wherein the label code is code collected within a coordinate range of the pasted code sample in a case that the pasted code sample is pasted into the code development environment and passes a preset waiting time.

### Claim 14 (depends on 13)

The apparatus according to claim 13, wherein the instructions are executed by the at least one processor to enable the at least one processor further to:
empty the output of the code optimization model in a case that the target pasted code is determined not to be optimized.

### Claim 15 (depends on 13)

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

### Claim 16 (depends on 12)

The apparatus according to claim 12, wherein the instructions are executed by the at least one processor to enable the at least one processor further to:
perform the dependency analysis for relevant information of the target pasted code to obtain the context-dependent data corresponding to the target pasted code, wherein the relevant information comprises at least one of file information, class information or function information that the target pasted code depends on.

### Claim 17 (depends on 13)

The apparatus according to claim 13, wherein the instructions are executed by the at least one processor to enable the at least one processor further to determine first difference code between the optimized code and the target pasted code; and output the first difference code.

### Claim 18 (depends on 17)

The apparatus according to claim 17, wherein the instructions are executed by the at least one processor to enable the at least one processor further to in response to an acceptance instruction for the optimized code being received, replace second difference code comprised in the target pasted code with third difference code comprised in the optimized code, wherein the first difference code comprises 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 for training the code optimization model;
in response to a cancellation instruction for the optimized code being received, or in response to determining that a stay time of the optimized code in the code development environment reaches a preset stay time, retain the target pasted code; and mark the target pasted code and the optimized code as invalid training data.

### Claim 19 (depends on 9)

An apparatus for training a code model, comprising:
at least one processor; and
a memory communicating with the at least one processor; wherein
the memory stores instructions that can be executed 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 the method according to claim 9.

### Claim 20 (depends on 1)

A non-transitory computer-readable storage medium storing computer instructions, wherein the computer instructions are used to cause a computer to execute the method according to claim 1.
