CN120872312A - Intelligent code optimization method and device, computer equipment and storage medium - Google Patents
Intelligent code optimization method and device, computer equipment and storage mediumInfo
- Publication number
- CN120872312A CN120872312A CN202510950336.2A CN202510950336A CN120872312A CN 120872312 A CN120872312 A CN 120872312A CN 202510950336 A CN202510950336 A CN 202510950336A CN 120872312 A CN120872312 A CN 120872312A
- Authority
- CN
- China
- Prior art keywords
- code
- optimization
- converted
- naming
- character
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
- G06F8/33—Intelligent editors
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F40/00—Handling natural language data
- G06F40/20—Natural language analysis
- G06F40/205—Parsing
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F40/00—Handling natural language data
- G06F40/30—Semantic analysis
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F40/00—Handling natural language data
- G06F40/40—Processing or translation of natural language
- G06F40/58—Use of machine translation, e.g. for multi-lingual retrieval, for server-side translation for client devices or for real-time translation
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
- G06F8/31—Programming languages or programming paradigms
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/44—Arrangements for executing specific programs
- G06F9/451—Execution arrangements for user interfaces
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- General Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- General Health & Medical Sciences (AREA)
- Health & Medical Sciences (AREA)
- Computational Linguistics (AREA)
- Audiology, Speech & Language Pathology (AREA)
- Artificial Intelligence (AREA)
- Human Computer Interaction (AREA)
- Computing Systems (AREA)
- Machine Translation (AREA)
Abstract
The embodiment of the application discloses a code intelligent optimization method, a code intelligent optimization device, computer equipment and a storage medium. The method is applied to a code editor, a code optimization plug-in is installed in the code editor, the method comprises the steps of displaying a code editing interface, displaying codes to be optimized in the code editing interface, receiving selected operations of a user aiming at first code fragments in the codes to be optimized, triggering naming conversion functions of the code optimization plug-in, extracting identifiers to be converted of the first code fragments through an AST (automatic test) analyzer preset in the code optimization plug-in, converting the identifiers to be converted into Chinese characters through a language translation model according to the context of the identifiers to be converted, converting the identifiers to be converted into English characters, naming standard processing is conducted on the intermediate characters according to preset naming rules, and then obtaining target characters, and modifying the identifiers to be converted in the first code fragments into the target characters. The method of the embodiment of the application can realize automatic conversion of the naming of the identifiers in the codes.
Description
Technical Field
The present application relates to the field of artificial intelligence, and in particular, to a method and apparatus for intelligent code optimization, a computer device, and a storage medium.
Background
In the actual development process, if identifier names such as variable names, function names, class names and the like in the code are not standard, the readability of the code is reduced, team members and subsequent maintainers need to spend a great deal of effort to understand the code logic, the maintainability of the code is poor, the understanding cost of the code is increased, and the Bug risk is increased.
However, because the English level of the programmer is uneven, the code written by the programmer is easy to have the problem of irregular naming such as variables in the code, the traditional translation tool is used for translating the Chinese naming of the identifiers in the code, the error is easy to translate due to lack of context understanding capability, the naming format of the identifiers is manually adjusted according to the current required naming rule after the translation is completed, and the naming conversion efficiency is low.
Disclosure of Invention
The embodiment of the application provides a code intelligent optimization method, a device, computer equipment and a storage medium, which can automatically translate Chinese names of identifiers in codes into accurate names of identifiers conforming to a naming format.
In a first aspect, an embodiment of the present application provides a method for intelligently optimizing a code, where the method is applied to a code editor, and a code optimization plug-in is installed in the code editor, and the method includes:
displaying a code editing interface, wherein codes to be optimized are displayed in the code editing interface;
Receiving the selected operation of a user aiming at a first code segment in the code to be optimized, and triggering the naming conversion function of the code optimization plugin;
Extracting identifiers to be converted of the first code fragments through an AST analyzer preset in the code optimization plug-in, wherein the identifiers to be converted are Chinese characters;
Converting the identifier to be converted into an intermediate character according to the context of the identifier to be converted through a language translation model, wherein the intermediate character is an English character;
carrying out naming standard processing on the intermediate characters according to a preset naming rule to obtain target characters;
and modifying the identifier to be converted in the first code segment into the target character.
In some embodiments, the naming rules include a large hump naming rule and a small hump naming rule, and the naming standard processing is performed on the intermediate characters according to a preset naming rule to obtain target characters, including:
determining character attributes of the intermediate characters;
If the character attribute is a variable name or a function name, carrying out naming standard processing on the intermediate character according to the small hump naming rule to obtain the target character;
and if the character attribute is a class name or an enumeration name, carrying out naming standard processing on the intermediate character according to the large hump naming rule to obtain the target character.
In some embodiments, the modifying the identifier to be converted in the first code segment into the target character includes:
Determining the positions of a plurality of target characters in the first code segment;
And replacing the identifier to be converted at the corresponding position with the corresponding target character from back to front according to the position of the target character in the first code segment.
In some embodiments, after the displaying the code editing interface, the method further comprises:
receiving the selected operation of a user aiming at a second code segment in the code to be optimized, and triggering a code interpretation function of the code optimization plugin;
And generating an analysis document of the second code segment according to the context of the second code segment by a code interpreter preset in the code optimization plug-in, wherein the analysis document comprises the function analysis of each row in the second code segment and the function analysis of each function.
In some embodiments, after the receiving user selects an operation for a second code segment in the code to be optimized and triggers a code interpretation function of the code optimization plug-in, the method further comprises:
displaying a question-answer input box, and acquiring a natural language question input by a user through the question-answer input box;
And generating a solution corresponding to the natural language problem according to the context of the second code segment and the second code segment through a question and answer device preset in the code optimization plug-in.
In some embodiments, after the displaying the code editing interface, the method further comprises:
receiving the selection operation of a user on a third code fragment in the code to be optimized, and triggering the code reconstruction function of the code optimization plugin;
Carrying out naming standardization processing and structure optimization processing on the third code fragment through a code reconstructor preset in the code optimization plug-in to obtain a reconstruction code corresponding to the third code fragment;
and replacing the third code segment in the code to be optimized with the reconstruction code.
In some embodiments, after the displaying the code editing interface, the method further comprises:
receiving the selected operation of a user aiming at a fourth code segment in the code to be optimized, and triggering the code performance optimization function of the code optimization plugin;
Performing performance analysis on the fourth code segment through a performance optimizer preset in the code optimization plug-in to obtain a performance analysis result, wherein the performance analysis comprises low-efficiency cycle analysis, repeated calculation analysis and memory leakage risk analysis;
Generating an optimized code corresponding to the fourth code segment according to the performance analysis result;
And replacing the fourth code segment in the code to be optimized with the optimized code.
In a second aspect, an embodiment of the present application further provides a code intelligent optimization device, where the code intelligent optimization device is deployed in a code editor, and a code optimization plug-in is installed in the code editor, and the code intelligent optimization device includes a transceiver unit and a processing unit, where:
the processing unit is used for displaying a code editing interface, and codes to be optimized are displayed in the code editing interface;
the receiving and transmitting unit is used for receiving the selection operation of a user on a first code fragment in the code to be optimized and triggering the naming conversion function of the code optimization plugin through the processing unit;
The processing unit is further used for extracting an identifier to be converted of the first code segment through an AST analyzer preset in the code optimization plug-in, wherein the identifier to be converted is a Chinese character, converting the identifier to be converted into an intermediate character according to the context of the identifier to be converted through a language translation model, the intermediate character is an English character, carrying out naming standard processing on the intermediate character according to a preset naming rule to obtain a target character, and modifying the identifier to be converted in the first code segment into the target character.
In a third aspect, an embodiment of the present application further provides a computer device, which includes a memory and a processor, where the memory stores a computer program, and the processor implements the method when executing the computer program.
In a fourth aspect, embodiments of the present application also provide a computer readable storage medium storing a computer program comprising program instructions which, when executed by a processor, implement the above-described method.
The embodiment of the application provides a code intelligent optimization method, a code intelligent optimization device, computer equipment and a storage medium. The method is applied to a code editor, a code optimization plug-in is installed in the code editor, the method comprises the steps of displaying a code editing interface, displaying codes to be optimized in the code editing interface, receiving a selection operation of a user on a first code segment in the codes to be optimized, triggering a naming conversion function of the code optimization plug-in, extracting identifiers to be converted of the first code segment through an AST (automatic test) analyzer preset in the code optimization plug-in, wherein the identifiers to be converted are Chinese characters, converting the identifiers to be converted into intermediate characters according to the context of the identifiers to be converted through a language translation model, wherein the intermediate characters are English characters, conducting naming standard processing on the intermediate characters according to a preset naming rule to obtain target characters, and modifying the identifiers to be converted in the first code segment into the target characters. On one hand, the embodiment of the application can automatically identify the identifier to be converted in the code and perform English conversion of the identifier by combining the context of the identifier to be converted, and has high translation accuracy.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for the description of the embodiments will be briefly described below, and it is obvious that the drawings in the following description are some embodiments of the present application, and other drawings may be obtained according to these drawings without inventive effort for a person skilled in the art.
FIG. 1 is a schematic flow chart of a code intelligent optimization method provided by an embodiment of the application;
FIG. 2 is a schematic view of a sub-flowchart of a code intelligent optimization method according to an embodiment of the present application;
FIG. 3 is a schematic diagram of another sub-flow of the intelligent code optimization method according to the embodiment of the present application;
FIG. 4 is a schematic block diagram of a code intelligent optimization device provided by an embodiment of the application;
fig. 5 is a schematic block diagram of a computer device according to an embodiment of the present application.
Detailed Description
The following description of the embodiments of the present application will be made clearly and fully with reference to the accompanying drawings, in which it is evident that the embodiments described are some, but not all embodiments of the application. All other embodiments, which can be made by those skilled in the art based on the embodiments of the application without making any inventive effort, are intended to be within the scope of the application.
It should be understood that the terms "comprises" and "comprising," when used in this specification and the appended claims, specify the presence of stated features, integers, steps, operations, elements, and/or components, but do not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and/or groups thereof.
It is also to be understood that the terminology used in the description of the application herein is for the purpose of describing particular embodiments only and is not intended to be limiting of the application. As used in this specification and the appended claims, the singular forms "a," "an," and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise.
It should be further understood that the term "and/or" as used in the present specification and the appended claims refers to any and all possible combinations of one or more of the associated listed items, and includes such combinations.
The embodiment of the application provides a code intelligent optimization method, a code intelligent optimization device, computer equipment and a storage medium.
The execution main body of the code intelligent optimization method can be the code intelligent optimization device provided by the embodiment of the application or the computer equipment integrated with the code intelligent optimization device, wherein the code intelligent optimization device can be realized in a hardware or software mode, the computer equipment can be a terminal, and the terminal can be a user terminal capable of performing code processing, such as a tablet computer, a desktop computer or a notebook computer.
The intelligent code optimizing method provided by the embodiment is applied to a code editor, wherein a code optimizing plug-in is installed in the code editor, and a user can name the identifier in the currently displayed code through the code editor when writing the code or optimizing the code.
In other embodiments, the code optimization plug-in provided in this embodiment further provides at least one of code interpretation, code reconstruction, and code performance optimization.
The following describes the code intelligent optimization method provided by the application in detail by using the user terminal as an execution subject, and fig. 1 is a flow diagram of the code intelligent optimization method provided by the embodiment of the application. As shown in fig. 1, the method includes the following steps S110 to S160.
S110, displaying a code editing interface, wherein codes to be optimized are displayed in the code editing interface.
In this embodiment, the user terminal displays a code editing interface of the code editor, and the code to be optimized is displayed in the code editing interface, where the code to be optimized may be a code currently written by the user, or may be a code to be optimized that is imported from outside the code editing interface by the user.
S120, receiving the selection operation of a user on the first code segment in the code to be optimized, and triggering the naming conversion function of the code optimization plug-in.
In this embodiment, the first code segment is a code that is selected by a user in the code to be optimized and needs to be named and converted currently, and the first code segment may be a part of the code in the code to be optimized or may be a whole section of the code to be optimized.
For example, in the process of writing a code, because the English basis of the user is poor, chinese is used for carrying out the writing of identifier naming in the process of writing, after the current section of code is written, the user selects the section of code, a naming conversion function of a code optimization plugin in a code editor is triggered through a mouse or a touch screen, specifically, an entry of the code optimization plugin can be displayed in a menu bar in the code editor, and the user invokes the code optimization plugin through a control corresponding to the code optimization plugin in the trigger menu bar so as to automatically convert Chinese naming in the code into English naming through the code optimization plugin.
S130, extracting identifiers to be converted of the first code segment through an AST analyzer preset in the code optimization plug-in, wherein the identifiers to be converted are Chinese characters.
In this embodiment, after triggering the code optimization plug-in, the code optimization plug-in identifies all Identifiers (Identifiers) to be converted in the first code segment through an abstract syntax tree (Abstract Syntax Tree, AST) parser, and character attributes of the Identifiers to be converted include variable names, function names, class names and enumeration names.
One or more identifiers to be converted are needed, and character attributes and contexts (code blocks, scope and the like) corresponding to the identifiers to be converted respectively also need to be extracted at the same time when the identifiers to be converted are extracted.
And extracting naming information of all identifiers to be converted from AST to form a naming list to be processed, wherein the naming list comprises all identifiers to be converted and naming information corresponding to all identifiers to be converted respectively, and the naming information comprises code positions (such as line numbers and column numbers) corresponding to the identifiers to be converted, character attributes and contexts (in code blocks and scope).
The code position in the naming information is used for subsequent code modification operation (the identifier to be converted is replaced by the target character obtained after conversion according to the position of the identifier to be converted), the character attribute in the naming information is used for determining the corresponding naming rule, and the context in the naming information is used for optimizing the translation of the identifier to be converted.
S140, converting the identifier to be converted into an intermediate character according to the context of the identifier to be converted through a language translation model, wherein the intermediate character is an English character.
In this embodiment, the language translation model is a large language model (such as a GPT model of OpenAI), and the language translation model in this embodiment needs to understand the semantics of each identifier according to the context of the character to be converted, determine the actual meaning of the identifier in the code, and generate a corresponding english name (intermediate character) according to the result of semantic analysis.
The first code segment is exemplified by the following code segment:
let total = 0;
for (let commodity of commodity list) {
Total + = commodity price;
};
The identifier to be converted comprises a total price, commodities, commodity lists and prices;
taking the translation of "total" as an example, the language translation model can determine that "total" is the accumulation of all commodity prices by context, and the actual meaning is "totalPrice", at which time "total" can be translated into "totalPrice" or "sumValue".
If the transliteration alone is done without context, the language translation model may translate the "total" directly to "totalCount" or "sumValue" without consideration of its actual meaning in the code (sum of prices), resulting in translation errors.
And S150, carrying out naming standard processing on the intermediate characters according to a preset naming rule to obtain target characters.
In some embodiments, referring to FIG. 2, the naming convention is performed on the intermediate characters by:
S1501, determining character attributes of the intermediate characters;
s1502, if the character attribute is a variable name or a function name, carrying out naming standard processing on the intermediate character according to the small hump naming rule to obtain the target character;
s1503, if the character attribute is a class name or an enumeration name, carrying out naming standard processing on the intermediate character according to the large hump naming rule to obtain the target character.
Specifically, for each intermediate character, corresponding naming information is first found from the naming list, and then corresponding character attributes are extracted from the corresponding naming information. And if the character attribute of the current intermediate character is a variable name or a function name, carrying out naming standard processing on the intermediate character by using a small hump naming rule to obtain the target character, wherein the intermediate character comprises a plurality of words, the small hump naming rule indicates that the initial letters in the intermediate character are capitalized, the initial letters of the subsequent words are capitalized, and the other letters are capitalized, and if the character attribute of the current intermediate character is a class name or an enumeration name, carrying out naming standard processing on the intermediate character according to the large hump naming rule to obtain the target character, and the large hump naming rule indicates that the initial letters of all words in the intermediate character are capitalized, and the other letters are capitalized.
In some embodiments, after generating the target characters corresponding to the identifiers to be converted respectively, the target characters corresponding to the identifiers to be converted respectively are displayed in the target character preview area, and after the user performs the confirmation operation of the target characters through the target character preview area, the step S160 is performed to modify the characters of the first code segment. If the user performs the cancel operation, the original text of the first code segment is maintained, and naming conversion is not needed.
S160, modifying the identifier to be converted in the first code segment into the target character.
In some implementations, referring to fig. 3, the identifier to be converted in the first code segment may be modified to the target character by:
S1601, determining positions of a plurality of target characters in the first code segment;
S1602, replacing the identifier to be converted at the corresponding position with the corresponding target character from back to front according to the position of the target character in the first code segment.
Specifically, for each intermediate character, corresponding naming information is first found from the naming list, and then corresponding code positions (positions of the target character in the first code segment, i.e., code positions corresponding to the identifier to be converted corresponding to the target character) are extracted from the corresponding naming information. And then sorting the target characters from front to back according to the positions of the target characters in the first code segment, and replacing the corresponding Chinese identifiers from back to front according to the sorting.
The present embodiment performs replacement of the identifier from back to front, and the content of each replacement does not affect the index (position) of the previously not-replaced portion. Therefore, the start and stop indexes of each position to be replaced can be ensured to be accurate in replacement, and the problem of position deviation of the position to be replaced is avoided.
The optimized first code segment is as follows:
let totalPrice=0;
for(let product of productList){
totalPrice+=product.price;
}。
further, the code optimization plug-in this embodiment further provides a code interpretation function, and after the code editing interface is displayed, the method further includes:
And generating an analysis document of the second code segment according to the context of the second code segment by a code interpreter preset in the code optimization plug-in, wherein the analysis document comprises the function analysis of each row in the second code segment and the function analysis of each function.
After the receiving user selects the operation for the second code segment in the code to be optimized and triggers the code interpretation function of the code optimization plugin, the method further includes:
and generating a solution corresponding to the natural language question according to the context of the second code segment and the second code segment by a question and answer device preset in the code optimization plug-in.
In particular, the second code segment may be the same segment as the first code segment or may be a different segment from the first code segment, and the code interpretation function is intended to help the developer quickly understand the meaning and function of the existing code, and is particularly suitable for reading other codes or maintaining legacy items.
In one aspect, the code parsing function provides an automatic annotation generation and structure mining function, wherein:
And (3) automatic annotation generation, namely carrying out semantic analysis on the selected code segments through an AI model, automatically generating Chinese or English detailed annotations, and explaining the functions of each row or each function.
And (3) structural carding, namely performing structural interpretation on complex functions, classes and modules, wherein the structural interpretation comprises input and output, main logic flows, key variables, dependency relationships and the like.
On the other hand, the code parsing function also provides an interactive question-and-answer function, and the plug-in can generate a solution based on context intelligence, for natural language questions that the user can input through a question-and-answer input box (e.g. "what is the function of this piece of code. In addition, the plug-in also supports multiple languages, namely Chinese and English interpretation, so that developers in different backgrounds can understand codes conveniently.
Further, the code optimization plug-in this embodiment further provides a code reconstruction function, and after the displaying of the code editing interface, the method further includes:
The method comprises the steps of receiving a selection operation of a user on a third code fragment in a code to be optimized, triggering a code reconstruction function of the code optimization plug-in, carrying out naming standardization processing and structure optimization processing on the third code fragment through a code reconstructor preset in the code optimization plug-in to obtain a reconstruction code corresponding to the third code fragment, and replacing the third code fragment in the code to be optimized with the reconstruction code.
In particular, the third code segment may be the same segment as the first code segment or may be a different segment than the first code segment, and the code reconstruction function aims to improve the readability, maintainability and consistency of the code while reducing technical liabilities. Code reconstruction includes naming normalization processing (and naming conversion function are both used to implement identifier unified and normalized renaming, and providing code reconstruction function to perform naming normalization processing and structure optimization processing), wherein:
and naming standardization processing, namely renaming identifiers such as variables, functions, classes and the like in a unified and standardized way by combining a naming conversion function, so as to improve code consistency.
The structure optimization processing comprises automatic recognition of redundant codes and repeated logic, extraction of redundant codes into functions or classes, simplification of code structures, and automatic repair of common bad taste processing, wherein the function/class splitting and merging processing comprises the steps of suggesting or automatically splitting overlong functions/classes into smaller units or merging highly relevant codes according to code complexity and responsibility separation principles, and automatic repair of common bad taste processing such as magic number elimination, nesting simplification, unused variable elimination, repeated code block merging and the like.
After the reconstruction code corresponding to the third code segment is obtained, the user is required to conduct reconstruction preview and confirmation, all reconstruction suggestions can be previewed, and the user can apply the reconstruction suggestions after confirmation, so that safety and controllability are ensured.
Further, the code optimization plug-in this embodiment further provides a code performance optimization function, and after the displaying of the code editing interface, the method further includes:
The method comprises the steps of receiving a selected operation of a user on a fourth code segment in codes to be optimized, triggering a code performance optimization function of a code optimization plug-in, performing performance analysis on the fourth code segment through a performance optimizer preset in the code optimization plug-in to obtain a performance analysis result, generating an optimization code corresponding to the fourth code segment according to the performance analysis result, and replacing the fourth code segment in the codes to be optimized with the optimization code, wherein the performance analysis comprises low-efficiency cycle analysis, repeated calculation analysis and memory leakage risk analysis.
Specifically, the fourth code segment may be the same segment as the first code segment, or may be a different segment from the first code segment, where the code performance optimization function focuses on improving the performance, resource utilization, and operating efficiency of the code. Performance bottlenecks (e.g., inefficient loops, repetitive calculations, memory leakage risk, etc.) in the code are automatically detected through static analysis and AI models. Specific optimization suggestions and modifications are given for the detected problem, such as using more efficient data structures, algorithm replacement, reducing unnecessary computations, etc. For common performance problems, the plug-in can automatically rewrite related codes, so that the execution efficiency is improved, and the automatic optimization of the codes is realized.
After the optimization codes corresponding to the fourth code segment are obtained through automatic optimization, the plug-in also supports the comparison of the codes before and after the optimization, supports the comparison display of the codes before and after the optimization and the performance, and helps users to intuitively know the optimization effect.
The code optimization plug-in the embodiment of the application can help a developer write more standard and easier-to-maintain codes through multidimensional services such as naming conversion, code interpretation, reconstruction, optimization and the like, thereby improving the overall development efficiency.
In summary, the method provided by the embodiment is applied to a code editor, wherein a code optimization plugin is installed in the code editor, the method comprises the steps of displaying a code editing interface, displaying codes to be optimized in the code editing interface, receiving a selection operation of a user on a first code segment in the codes to be optimized, triggering a naming conversion function of the code optimization plugin, extracting an identifier to be converted of the first code segment through an AST (automatic test) analyzer preset in the code optimization plugin, wherein the identifier to be converted is a Chinese character, converting the identifier to be converted into an intermediate character according to the context of the identifier to be converted through a language translation model, wherein the intermediate character is an English character, conducting naming standard processing on the intermediate character according to a preset naming rule to obtain a target character, and modifying the identifier to be converted in the first code segment into the target character. On one hand, the embodiment of the application can automatically identify the identifier to be converted in the code and perform English conversion of the identifier by combining the context of the identifier to be converted, and has high translation accuracy.
Fig. 4 is a schematic block diagram of a code intelligent optimization device provided by an embodiment of the application. As shown in fig. 4, the present application also provides a code intelligent optimization apparatus 400 corresponding to the above code intelligent optimization method. The code intelligent optimization apparatus 400 includes a unit for performing the above-described code intelligent optimization method, and the code intelligent optimization apparatus 400 may be configured in a desktop computer, a tablet computer, a portable computer, or the like. Specifically, referring to fig. 4, the code intelligent optimization apparatus 400 is deployed in a code editor, in which a code optimization plug-in is installed, the code intelligent optimization apparatus 400 includes a transceiver unit 401 and a processing unit 402, where:
the processing unit 402 is configured to display a code editing interface, where a code to be optimized is displayed;
The transceiver unit 401 is configured to receive a selection operation of a user on a first code segment in the code to be optimized, and trigger, by the processing unit 402, a naming conversion function of the code optimization plug-in;
The processing unit 402 is further configured to extract an identifier to be converted of the first code segment through an AST parser preset in the code optimization plug-in, where the identifier to be converted is a chinese character, convert the identifier to be converted into an intermediate character according to a context of the identifier to be converted through a language translation model, where the intermediate character is an english character, perform naming specification processing on the intermediate character according to a preset naming rule to obtain a target character, and modify the identifier to be converted in the first code segment into the target character.
In some embodiments, the naming rules include a large hump naming rule and a small hump naming rule, and the processing unit 402 is specifically configured to, when executing the step of performing naming convention processing on the intermediate character according to a preset naming rule to obtain the target character:
Determining character attributes of the intermediate characters, carrying out naming standard processing on the intermediate characters according to the small hump naming rule to obtain the target characters if the character attributes are variable names or function names, and carrying out naming standard processing on the intermediate characters according to the large hump naming rule to obtain the target characters if the character attributes are class names or enumeration names.
In some embodiments, the processing unit 402 is specifically configured to, when executing the step of modifying the identifier to be converted in the first code segment into the target character:
and replacing the identifiers to be converted at the corresponding positions with the corresponding target characters sequentially from back to front according to the positions of the target characters in the first code fragments.
In some embodiments, the displaying the code editing interface is followed by:
The transceiver unit 401 is further configured to receive a selection operation of a user on a second code segment in the code to be optimized, and trigger a code interpretation function of the code optimization plug-in;
the processing unit 402 is further configured to generate, by using a code interpreter preset in the code optimization plug-in, an analysis document of the second code segment according to the context of the second code segment, where the analysis document includes a function analysis of each line in the second code segment and a function analysis of each function.
In some embodiments, after the step of receiving a user selected operation for a second code segment in the code to be optimized and triggering a code interpretation function of the code optimization plug-in, the processing unit 402 is further configured to:
and generating a solution corresponding to the natural language question according to the context of the second code segment and the second code segment through a question and answer device preset in the code optimization plug-in.
In some embodiments, the displaying the code editing interface is followed by:
The transceiver unit 401 is further configured to receive a selection operation of a user on a third code segment in the code to be optimized, and trigger a code reconstruction function of the code optimization plug-in;
The processing unit 402 is further configured to perform naming standardization processing and structure optimization processing on the third code segment through a code reconstructor preset in the code optimization plug-in, obtain a reconstructed code corresponding to the third code segment, and replace the third code segment in the code to be optimized with the reconstructed code.
In some embodiments, the displaying the code editing interface is followed by:
The transceiver unit 401 is further configured to receive a selection operation of a user on a fourth code segment in the code to be optimized, and trigger a code performance optimization function of the code optimization plug-in;
The processing unit 402 is further configured to perform performance analysis on the fourth code segment through a performance optimizer preset in the code optimization plug-in, to obtain a performance analysis result, where the performance analysis includes low-efficiency loop analysis, repeated calculation analysis, and memory leakage risk analysis, generate an optimization code corresponding to the fourth code segment according to the performance analysis result, and replace the fourth code segment in the code to be optimized with the optimization code.
On one hand, the embodiment of the application can automatically identify the identifier to be converted in the code and perform English conversion of the identifier by combining the context of the identifier to be converted, and has high translation accuracy.
It should be noted that, as those skilled in the art can clearly understand, the specific implementation process of the foregoing code intelligent optimization device and each unit may refer to the corresponding descriptions in the foregoing method embodiments, and for convenience and brevity of description, the description is omitted herein.
The above-described code intelligent optimization apparatus may be implemented in the form of a computer program that is executable on a computer device as shown in fig. 5.
Referring to fig. 5, fig. 5 is a schematic block diagram of a computer device according to an embodiment of the present application. The computer device 500 may be a terminal, where the terminal may be an electronic device such as a tablet computer, a notebook computer, a desktop computer, etc., and a code editor is installed in the computer device 500, and a code optimization plug-in is installed in the code editor.
With reference to FIG. 5, the computer device 500 includes a processor 502, memory, and a network interface 505 connected by a system bus 501, where the memory may include a non-volatile storage medium 503 and an internal memory 504.
The non-volatile storage medium 503 may store an operating system 5031 and a computer program 5032. The computer program 5032 includes program instructions that, when executed, cause the processor 502 to perform a code intelligent optimization method.
The processor 502 is used to provide computing and control capabilities to support the operation of the overall computer device 500.
The internal memory 504 provides an environment for the execution of a computer program 5032 in the non-volatile storage medium 503, which computer program 5032, when executed by the processor 502, causes the processor 502 to perform a code intelligent optimization method.
The network interface 505 is used for network communication with other devices. It will be appreciated by those skilled in the art that the architecture shown in fig. 5 is merely a block diagram of some of the architecture relevant to the present inventive arrangements and is not limiting of the computer device 500 to which the present inventive arrangements may be implemented, as a particular computer device 500 may include more or fewer components than shown, or may combine some of the components, or have a different arrangement of components.
Wherein the processor 502 is configured to execute a computer program 5032 stored in a memory to implement the steps of:
displaying a code editing interface, wherein codes to be optimized are displayed in the code editing interface;
Receiving the selected operation of a user aiming at a first code segment in the code to be optimized, and triggering the naming conversion function of the code optimization plugin;
Extracting identifiers to be converted of the first code fragments through an AST analyzer preset in the code optimization plug-in, wherein the identifiers to be converted are Chinese characters;
Converting the identifier to be converted into an intermediate character according to the context of the identifier to be converted through a language translation model, wherein the intermediate character is an English character;
carrying out naming standard processing on the intermediate characters according to a preset naming rule to obtain target characters;
and modifying the identifier to be converted in the first code segment into the target character.
It should be appreciated that in an embodiment of the application, the Processor 502 may be a central processing unit (Central Processing Unit, CPU), the Processor 502 may also be other general purpose processors, digital signal processors (DIGITAL SIGNAL processors, DSPs), application SPECIFIC INTEGRATED Circuits (ASICs), field-Programmable gate arrays (Field-Programmable GATE ARRAY, FPGA) or other Programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or the like. Wherein the general purpose processor may be a microprocessor or the processor may be any conventional processor or the like.
Those skilled in the art will appreciate that all or part of the flow in a method embodying the above described embodiments may be accomplished by computer programs instructing the relevant hardware. The computer program comprises program instructions, and the computer program can be stored in a storage medium, which is a computer readable storage medium. The program instructions are executed by at least one processor in the computer system to implement the flow steps of the embodiments of the method described above.
Accordingly, the present application also provides a storage medium. The storage medium may be a computer readable storage medium. The storage medium stores a computer program, wherein the computer program includes program instructions. The program instructions, when executed by the processor, cause the processor to perform the steps of:
displaying a code editing interface, wherein codes to be optimized are displayed in the code editing interface;
Receiving the selected operation of a user aiming at a first code segment in the code to be optimized, and triggering the naming conversion function of the code optimization plugin;
Extracting identifiers to be converted of the first code fragments through an AST analyzer preset in the code optimization plug-in, wherein the identifiers to be converted are Chinese characters;
Converting the identifier to be converted into an intermediate character according to the context of the identifier to be converted through a language translation model, wherein the intermediate character is an English character;
carrying out naming standard processing on the intermediate characters according to a preset naming rule to obtain target characters;
and modifying the identifier to be converted in the first code segment into the target character.
The storage medium may be a U-disk, a removable hard disk, a Read-Only Memory (ROM), a magnetic disk, or an optical disk, or other various computer-readable storage media that can store program codes.
Those of ordinary skill in the art will appreciate that the elements and algorithm steps described in connection with the embodiments disclosed herein may be embodied in electronic hardware, in computer software, or in a combination of the two, and that the elements and steps of the examples have been generally described in terms of function in the foregoing description to clearly illustrate the interchangeability of hardware and software. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the solution. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present application.
In the several embodiments provided by the present application, it should be understood that the disclosed apparatus and method may be implemented in other manners. For example, the device embodiments described above are merely illustrative. For example, the division of each unit is only one logic function division, and there may be another division manner in actual implementation. For example, multiple units or components may be combined or may be integrated into another system, or some features may be omitted, or not performed.
The steps in the method of the embodiment of the application can be sequentially adjusted, combined and deleted according to actual needs. The units in the device of the embodiment of the application can be combined, divided and deleted according to actual needs. In addition, each functional unit in the embodiments of the present application may be integrated in one processing unit, or each unit may exist alone physically, or two or more units may be integrated in one unit.
The integrated unit may be stored in a storage medium if implemented in the form of a software functional unit and sold or used as a stand-alone product. Based on such understanding, the technical solution of the present application is essentially or a part contributing to the prior art, or all or part of the technical solution may be embodied in the form of a software product stored in a storage medium, comprising several instructions for causing a computer device (which may be a personal computer, a terminal, a network device, etc.) to perform all or part of the steps of the method according to the embodiments of the present application.
While the application has been described with reference to certain preferred embodiments, it will be understood by those skilled in the art that various changes and substitutions of equivalents may be made and equivalents will be apparent to those skilled in the art without departing from the scope of the application. Therefore, the protection scope of the application is subject to the protection scope of the claims.
Claims (10)
1. A method for intelligent optimization of code, the method being applied to a code editor in which a code optimization plug-in is installed, the method comprising:
displaying a code editing interface, wherein codes to be optimized are displayed in the code editing interface;
Receiving the selected operation of a user aiming at a first code segment in the code to be optimized, and triggering the naming conversion function of the code optimization plugin;
Extracting identifiers to be converted of the first code fragments through an AST analyzer preset in the code optimization plug-in, wherein the identifiers to be converted are Chinese characters;
Converting the identifier to be converted into an intermediate character according to the context of the identifier to be converted through a language translation model, wherein the intermediate character is an English character;
carrying out naming standard processing on the intermediate characters according to a preset naming rule to obtain target characters;
and modifying the identifier to be converted in the first code segment into the target character.
2. The method according to claim 1, wherein the naming rules include a large hump naming rule and a small hump naming rule, and the naming standard processing is performed on the intermediate characters according to a preset naming rule to obtain target characters, including:
determining character attributes of the intermediate characters;
If the character attribute is a variable name or a function name, carrying out naming standard processing on the intermediate character according to the small hump naming rule to obtain the target character;
and if the character attribute is a class name or an enumeration name, carrying out naming standard processing on the intermediate character according to the large hump naming rule to obtain the target character.
3. The method of claim 1, wherein the modifying the identifier to be converted in the first code segment to the target character comprises:
Determining the positions of a plurality of target characters in the first code segment;
And replacing the identifier to be converted at the corresponding position with the corresponding target character from back to front according to the position of the target character in the first code segment.
4. The method of claim 1, wherein after displaying the code editing interface, the method further comprises:
receiving the selected operation of a user aiming at a second code segment in the code to be optimized, and triggering a code interpretation function of the code optimization plugin;
And generating an analysis document of the second code segment according to the context of the second code segment by a code interpreter preset in the code optimization plug-in, wherein the analysis document comprises the function analysis of each row in the second code segment and the function analysis of each function.
5. The method of claim 4, wherein after the receiving user selects an operation for a second code segment in the code to be optimized and triggers a code interpretation function of the code optimization plug-in, the method further comprises:
displaying a question-answer input box, and acquiring a natural language question input by a user through the question-answer input box;
And generating a solution corresponding to the natural language problem according to the context of the second code segment and the second code segment through a question and answer device preset in the code optimization plug-in.
6. The method of claim 1, wherein after displaying the code editing interface, the method further comprises:
receiving the selection operation of a user on a third code fragment in the code to be optimized, and triggering the code reconstruction function of the code optimization plugin;
Carrying out naming standardization processing and structure optimization processing on the third code fragment through a code reconstructor preset in the code optimization plug-in to obtain a reconstruction code corresponding to the third code fragment;
and replacing the third code segment in the code to be optimized with the reconstruction code.
7. The method of claim 1, wherein after displaying the code editing interface, the method further comprises:
receiving the selected operation of a user aiming at a fourth code segment in the code to be optimized, and triggering the code performance optimization function of the code optimization plugin;
Performing performance analysis on the fourth code segment through a performance optimizer preset in the code optimization plug-in to obtain a performance analysis result, wherein the performance analysis comprises low-efficiency cycle analysis, repeated calculation analysis and memory leakage risk analysis;
Generating an optimized code corresponding to the fourth code segment according to the performance analysis result;
And replacing the fourth code segment in the code to be optimized with the optimized code.
8. The intelligent code optimizing device is characterized by being deployed in a code editor, wherein a code optimizing plug-in is installed in the code editor, and comprises a receiving and transmitting unit and a processing unit, wherein:
the processing unit is used for displaying a code editing interface, and codes to be optimized are displayed in the code editing interface;
the receiving and transmitting unit is used for receiving the selection operation of a user on a first code fragment in the code to be optimized and triggering the naming conversion function of the code optimization plugin through the processing unit;
The processing unit is further used for extracting an identifier to be converted of the first code segment through an AST analyzer preset in the code optimization plug-in, wherein the identifier to be converted is a Chinese character, converting the identifier to be converted into an intermediate character according to the context of the identifier to be converted through a language translation model, the intermediate character is an English character, carrying out naming standard processing on the intermediate character according to a preset naming rule to obtain a target character, and modifying the identifier to be converted in the first code segment into the target character.
9. A computer device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, characterized in that the processor implements the code intelligent optimization method according to any of claims 1-7 when executing the computer program.
10. A storage medium storing a computer program comprising program instructions which, when executed by a processor, cause the processor to perform the code intelligent optimization method of any one of claims 1-7.
Priority Applications (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202510950336.2A CN120872312A (en) | 2025-07-09 | 2025-07-09 | Intelligent code optimization method and device, computer equipment and storage medium |
Applications Claiming Priority (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202510950336.2A CN120872312A (en) | 2025-07-09 | 2025-07-09 | Intelligent code optimization method and device, computer equipment and storage medium |
Publications (1)
| Publication Number | Publication Date |
|---|---|
| CN120872312A true CN120872312A (en) | 2025-10-31 |
Family
ID=97467420
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| CN202510950336.2A Pending CN120872312A (en) | 2025-07-09 | 2025-07-09 | Intelligent code optimization method and device, computer equipment and storage medium |
Country Status (1)
| Country | Link |
|---|---|
| CN (1) | CN120872312A (en) |
-
2025
- 2025-07-09 CN CN202510950336.2A patent/CN120872312A/en active Pending
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| Fox et al. | An R companion to applied regression | |
| US8286132B2 (en) | Comparing and merging structured documents syntactically and semantically | |
| US11301643B2 (en) | String extraction and translation service | |
| US7827155B2 (en) | System for processing formatted data | |
| US20100125828A1 (en) | Data transformation based on a technical design document | |
| CN117389541B (en) | Configuration system and device for generating template based on dialogue retrieval | |
| US7711546B2 (en) | User interface for machine aided authoring and translation | |
| US11301243B2 (en) | Bidirectional evaluation for general—purpose programming | |
| CN108762743A (en) | Data table operation code generation method and device | |
| CN118573738A (en) | Industrial control protocol configuration method, device, computer equipment and storage medium | |
| WO2007124176A2 (en) | Machine declarative language for formatted data processing | |
| US8996357B2 (en) | Method for generating diagrams, and information processing apparatus for same | |
| US8171462B2 (en) | User declarative language for formatted data processing | |
| WO2007124178A2 (en) | Methods for processing formatted data | |
| Rossini et al. | Emacs speaks statistics: A multiplatform, multipackage development environment for statistical analysis | |
| CN118963765A (en) | A method, device, equipment and storage medium for automatically generating software system code | |
| CN112699642B (en) | Index extraction method and device for complex medical texts, medium and electronic equipment | |
| CN118860392A (en) | Interface editing method, device, equipment and storage medium based on Flutter component | |
| Miao et al. | Easy Dataset: A Unified and Extensible Framework for Synthesizing LLM Fine-Tuning Data from Unstructured Documents | |
| CN121008828A (en) | Permission configuration methods and devices | |
| CN120492757A (en) | Shop page decoration method and device, equipment and medium thereof | |
| CN120872312A (en) | Intelligent code optimization method and device, computer equipment and storage medium | |
| CN119166872A (en) | Visual display method of natural language data, electronic device and storage medium | |
| JP5994150B2 (en) | Document creation method, document creation apparatus, and document creation program | |
| CN116127926B (en) | Method, device, equipment and medium for generating XSLT file of data body |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| PB01 | Publication | ||
| PB01 | Publication | ||
| SE01 | Entry into force of request for substantive examination | ||
| SE01 | Entry into force of request for substantive examination |