Code Generation Method and Device

By conducting semantic analysis of user needs and combining artificial intelligence code generation models, the problem that existing technology is difficult to meet users' diverse needs is solved, and a flexible generation of suitable code frameworks is achieved and the user's expected programming language code is converted, improving the efficiency and accuracy of code generation.

CN119088368BActive Publication Date: 2025-07-01河北网星软件有限公司
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411159909.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-08-22
Publication Date
2025-07-01
Estimated Expiration
2044-08-22

AI Technical Summary

Technical Problem

Existing code generation methods are difficult to meet the diverse needs of users, especially when user needs are unclear or in the early stage of exploration, there is a lack of tools that can quickly generate reasonable code frameworks.

Method used

By conducting semantic analysis of the demand information entered by the user, and combining the code generation model of artificial intelligence, a code framework is generated; if the user intends to include all specified content, the existing code generation tool will be used to quickly generate the code framework and convert it into specific code according to the programming language selected by the user.

Benefits of technology

It realizes the flexibility to generate suitable code frameworks based on the completeness of user needs, and ultimately converts them into code in the programming language expected by users, meeting the diverse needs of users and improving the efficiency and accuracy of code generation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119088368B_ABST
    Figure CN119088368B_ABST
Patent Text Reader

Abstract

The present disclosure provides a code generation method and apparatus, belonging to the technical field of software development. The method includes: performing semantic analysis on the requirement information input by the user to obtain the user intention; the requirement information is used to indicate the function to be implemented by the code; in response to the lack of one or more specified contents in the user intention, generating a code framework based on a code generation model; in response to the user intention including all specified contents, generating a code framework based on a code generation tool; generating the code corresponding to the code framework based on the programming language type input by the user. The code generation method and apparatus provided by the present disclosure can meet the diverse needs of users.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This disclosure belongs to the technical field of software development, and more specifically, relates to a code generation method and apparatus. Background Art

[0002] Automatic code generation is an important direction in the field of software development. It uses automated tools and technologies to reduce the workload of manual coding and improve development efficiency and quality. Different user groups and industry fields often have different software function requirements. At the same time, users will choose the most suitable programming language according to project requirements, team skills, or personal preferences. Therefore, it is necessary to further improve the code generation method to meet the diverse needs of users. Summary of the Invention

[0003] The purpose of this disclosure is to provide a code generation method and apparatus to meet the diverse needs of users.

[0004] In the first aspect of the embodiments of this disclosure, a code generation method is provided, including:

[0005] Performing semantic analysis on the requirement information input by the user to obtain the user intention; the requirement information is used to indicate the function to be implemented by the code;

[0006] In response to the lack of one or more specified contents in the user intention, generating a code framework based on a code generation model;

[0007] In response to all specified contents being included in the user intention, generating a code framework based on a code generation tool;

[0008] Generating the code corresponding to the code framework based on the programming language type input by the user.

[0009] In the second aspect of the embodiments of this disclosure, a code generation apparatus is provided, including:

[0010] A semantic analysis module, configured to perform semantic analysis on the requirement information input by the user to obtain the user intention; the requirement information is used to indicate the function to be implemented by the code;

[0011] A first response module, configured to generate a code framework based on a code generation model in response to the lack of one or more specified contents in the user intention;

[0012] A second response module, configured to generate a code framework based on a code generation tool in response to all specified contents being included in the user intention;

[0013] A code generation module, configured to generate the code corresponding to the code framework based on the programming language type input by the user.

[0014] In a third aspect of the embodiments of the present disclosure, an electronic device is provided, including a memory, a processor, and a computer program stored in the memory and running on the processor. When the processor executes the computer program, the steps of the above-mentioned code generation method are implemented.

[0015] In a fourth aspect of the embodiments of the present disclosure, a computer-readable storage medium is provided. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the above-mentioned code generation method are implemented.

[0016] The beneficial effects of the code generation method and device provided by the embodiments of the present disclosure are as follows:

[0017] In the embodiments of the present disclosure, first, semantic analysis is performed on the user requirements to obtain the user intention. If the necessary specified content is missing in the user intention, that is, when the user requirements are not clear enough or in the early exploration stage, a code generation model combined with artificial intelligence can be used to quickly generate a framework prototype of a simple application. The code generation model can learn a large number of code examples and generate a reasonable code framework based on the incomplete intention of the user, helping the user obtain some preliminary framework ideas and continuously improve on this basis. If all the necessary specified content is included in the user intention, that is, the user can clearly describe the functional requirements of the code, existing code generation tools (such as code template engines, code snippet libraries, etc.) can be directly used to quickly generate a code framework. Then, according to the programming language selected by the user, the generated code framework is converted into specific programming language code, thereby realizing the automatic generation of code.

[0018] The embodiments of the present disclosure flexibly generate a suitable code framework according to the completeness of the information provided by the user and finally convert it into the code of the programming language expected by the user, which is beneficial to meeting the diverse needs of users. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] In order to more clearly illustrate the technical solutions in the embodiments of the present disclosure, the following will briefly introduce the drawings required for use in the embodiments or the description of the prior art. Obviously, the following drawings are only some embodiments of the present disclosure, and those of ordinary skill in the art can obtain other drawings without creative efforts based on these drawings.

[0020] Figure 1 It is a schematic flowchart of the code generation method provided by an embodiment of the present disclosure;

[0021] Figure 2 It is a structural block diagram of the code generation device provided by an embodiment of the present disclosure;

[0022] Figure 3Schematic block diagram of an electronic device provided by an embodiment of the present disclosure. Detailed implementation manners

[0023] In the following description, specific details such as specific system architectures and technologies are presented for the purpose of illustration rather than limitation, so as to thoroughly understand the embodiments of the present disclosure. However, those skilled in the art should clearly understand that the present disclosure can also be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to avoid unnecessary details from interfering with the description of the present disclosure.

[0024] To make the objectives, technical solutions, and advantages of the present disclosure clearer, the following will be described through specific embodiments in conjunction with the accompanying drawings.

[0025] Please refer to Figure 1 , Figure 1 Flow schematic diagram of a code generation method provided by an embodiment of the present disclosure. The method includes:

[0026] S101: Perform semantic analysis on the requirement information input by the user to obtain the user intention; the requirement information is used to indicate the functions to be implemented by the code.

[0027] In this embodiment, the user's requirement information is usually input in a natural language manner. Therefore, natural language processing technologies (such as semantic role labeling and dependency syntactic analysis, etc.) can be used to perform semantic analysis on the text input by the user, identify key functions, actions (or functions), input and output parameters, etc. These information can be used as the user intention.

[0028] S102: In response to the lack of one or more specified contents in the user intention, generate a code framework based on the code generation model.

[0029] In this embodiment, traditional code generation tools (such as code template engines and code snippet libraries, etc.) can automatically generate the framework or code snippets of the entire application, thereby reducing the amount of code manually written by developers and reducing the risk of bugs caused by human errors. However, these tools usually rely on predefined templates and rules and require users to specify necessary contents. However, in the early exploration stage of software development, user requirements are often not clear enough to provide predefined templates and rules.

[0030] At this time, a code generation model combined with artificial intelligence can be used to quickly generate a framework prototype of a simple application. The code generation model can learn a large number of code examples and generate a reasonable code framework based on the incomplete intention of the user, helping the user obtain some preliminary framework ideas and continuously improve on this basis.

[0031] For example, if the user input is: "I want a system that can create tasks." After semantic analysis, it is found that the specified content such as task assignment, deadline setting, and completion status marking is missing in the user's intention. At this time, a relatively simple code framework can be generated based on the code generation model. Subsequently, through continuous interaction with the user, this simple code framework can be improved.

[0032] S103: In response to all specified content being included in the user's intention, generate a code framework based on the code generation tool.

[0033] In this embodiment, if all necessary specified content is included in the user's intention, that is, when the user can clearly describe the functional requirements of the code, predefined templates and rules can be provided, and the existing code generation tool can be directly used to quickly generate a code framework.

[0034] For example, the user input requirement information is: "I want a system that can create tasks, assign tasks to different people, set task deadlines, and mark task completion status." After semantic analysis, it is determined that all specified content (create tasks, assign tasks, set deadlines, mark completion status) is included in the user's intention. At this time, a code framework can be generated based on the code generation tool.

[0035] S104: Generate the code corresponding to the code framework based on the programming language type input by the user.

[0036] In this embodiment, the commonly used programming language types can be determined in advance, including Python, Java, C++, JavaScript (including Node.js), Go, etc. Detailed language specifications (or language mapping rules) are established for each commonly used programming language, including features such as syntax rules, keywords, data types, and control structures, and are stored in a database or configuration file.

[0037] Determine the corresponding language mapping rule according to the programming language type input by the user, and then convert the generated code framework into specific programming language code according to the language mapping rule, so as to realize the automatic generation of code.

[0038] As can be seen from the above, in this embodiment, the user intention is first obtained by semantic analysis of the user requirements. If the necessary specified content is missing in the user intention, that is, when the user requirements are not clear enough or in the early exploration stage, a code generation model combined with artificial intelligence can be used to quickly generate a framework prototype of a simple application. The code generation model can learn a large number of code examples and generate a reasonable code framework based on the incomplete intention of the user, helping the user obtain some preliminary framework ideas and continuously improve on this basis. If all the necessary specified content is included in the user intention, that is, the user can clearly describe the functional requirements of the code, the existing code generation tools (such as code template engines, code snippet libraries, etc.) can be directly used to quickly generate a code framework. Then, according to the programming language selected by the user, the generated code framework is converted into specific programming language code, thus realizing the automatic generation of code.

[0039] The embodiment of the present disclosure flexibly generates a suitable code framework according to the completeness of the information provided by the user and finally converts it into the code of the programming language expected by the user, which is beneficial to meeting the diverse needs of the user.

[0040] In an embodiment of the present disclosure, in response to all specified content being included in the user intention, a code framework is generated based on a code generation tool, including:

[0041] In response to the function implemented by the code shown in the user intention being a single function, a code framework is generated based on the first type of code generation tool.

[0042] In response to the function implemented by the code shown in the user intention including multiple functions, a code framework is generated based on the second type of code generation tool.

[0043] The data processing volume of the first type of code generation tool is less than that of the second type of code generation tool.

[0044] In this embodiment, if the function implemented by the code shown in the user intention is a single function, such as creating a simple workflow application, a form collection system, a small internal management tool, etc., a lightweight first type of code generation tool can be used to generate a code framework, such as a low-code / no-code platform, etc. This type of code generation tool has a small amount of data, simple operation, and can generate a code framework quickly and conveniently.

[0045] If the user intends to display code to implement multiple functions of a system, for example, when building a large financial trading system, a second type of code generation tool is required to establish detailed business process models, data models, etc. to accurately guide the development. The second type of code generation tool can handle more complex scenarios and support modular and component-based code generation. They can provide a richer template library and configuration options, allowing users to combine different functional modules as needed to generate more complex and flexible code frameworks. The second type of code generation tool can be a model-driven development (MDD) tool, etc. Since the implementation of multiple functions often involves operations such as data integration, processing, and analysis, the second type of code generation tool has a relatively large amount of data processing to support the efficient processing of a large amount of data.

[0046] It can be concluded from the above that this embodiment selects different code generation tools based on the user's intention to generate a code framework, which is beneficial to improving the efficiency and accuracy of code framework generation.

[0047] In an embodiment of the present disclosure, generating code corresponding to a code framework based on the programming language type input by the user includes:

[0048] Extracting the key information of the code framework; the key information includes key elements and the association relationships between the key elements, and the key elements include functions, data structures, and control flows.

[0049] Constructing a general model based on the key information.

[0050] Generating a code framework template corresponding to the programming language type based on the general model and the set language mapping rules.

[0051] Generating corresponding function code based on the description information of each template function in the code framework template to obtain the code corresponding to the code framework.

[0052] In this embodiment, the code framework is in the form of a text description. Therefore, natural language processing technology can be used to analyze and understand the text description to extract key information.

[0053] For example, the requirement information input by the user is: "I want a system that can create tasks, assign tasks to different people, set task deadlines, and mark the task completion status."

[0054] For this user requirement, according to the method of the foregoing embodiment, the corresponding code framework can be obtained as follows: First, there needs to be a Task class that contains attributes such as task name, person in charge, due date, and completion status. Then, there needs to be a TaskManager class that can implement methods for creating new tasks, setting the various attributes of the task when creating the task. This class also has a method for assigning tasks that can assign tasks to different personnel. At the same time, it also has a method for setting the due date of the task to directly modify the due date attribute of the corresponding task. Next, there needs to be a method for marking the completion status of the task to change the completion status of the task. In addition, a list or database needs to be used to save all tasks. In terms of control flow, when creating a task, it is checked whether the input task information is complete and valid. When assigning a task, it is checked whether the specified person exists. When setting the due date and marking the completion status, some reasonableness checks are also performed.

[0055] Key information extraction for the above code framework can obtain the following key information:

[0056] Key elements include:

[0057] Classes: Task, TaskManager;

[0058] Functions:

[0059] createTask (create task) in the TaskManager class;

[0060] assignTask (assign task) in the TaskManager class;

[0061] setTaskDueDate (set task due date) in the TaskManager class;

[0062] markTaskCompleted (mark task completion status) in the TaskManager class;

[0063] Data structure: A list or database may be used to store tasks (specific name not specified);

[0064] Control flow:

[0065] Check the integrity and validity of task information in createTask.

[0066] Check whether the specified person exists in assignTask.

[0067] Relationships between key elements:

[0068] The TaskManager class manages objects of the Task class and operates on and sets the properties of Task objects through various functions.

[0069] The data structure for storing tasks interacts with the TaskManager class to save and retrieve task data.

[0070] Exemplarily, a template for a general model can be pre-constructed, and then the above-mentioned key information obtained by extraction is filled into the template of the general model to obtain a general model for user requirements. As shown below:

[0071] Task class:

[0072] Contains attributes: task name, person in charge, due date, and completion status.

[0073] TaskManager class:

[0074] Manages a series of Task objects.

[0075] Functions:

[0076] createTask: Receives necessary information such as task name and person in charge, creates a new Task object, and checks the integrity and validity of the information.

[0077] assignTask: Receives a task object and a new person in charge, assigns the task to the specified person, and checks if the person exists.

[0078] setTaskDueDate: Receives a task object and a new due date, and sets the due date of the task.

[0079] markTaskCompleted: Receives a task object and marks its completion status as completed.

[0080] Data storage:

[0081] Uses a list or database to store all Task objects.

[0082] Based on the obtained general model, a code framework template of the programming language type specified by the user can be generated according to the constructed general model and the set language mapping rules. The language mapping rules define how to convert the elements and relationships in the general model into the syntax and habits of the programming language specified by the user. For example, if there is a class in the general model and the programming language specified by the user is Python, then declare the class, functions, and the call relationships between functions, etc. according to the language mapping rules of Python.

[0083] The code framework template usually includes multiple template functions. At this time, there is only the corresponding description information in the template functions. The code corresponding to the description information can be generated based on the code generation engine, so as to generate the code of each template function. The code of each template function constitutes the code corresponding to the entire code framework template. Taking the createTask function in the above embodiment as an example, an example of a template function is as follows:

[0084] def createTask(task_name, assignee, due_date):

[0085] # Create a new Task object and perform information integrity and validity checks.

[0086] It can be concluded from the above that in this embodiment, the general model is constructed by extracting the key information of the code framework, and then the code framework template corresponding to the programming language type is generated according to the general model and the set language mapping rules, realizing code generation according to the programming language type required by the user and meeting the diverse needs of the user for the programming language type.

[0087] In an embodiment of the present disclosure, generating the corresponding function code based on the description information of each template function in the code framework template includes:

[0088] Perform semantic analysis on the description information of each template function to obtain the sub-functions included in each template function.

[0089] Extract the feature data of the sub-functions by extracting the description information of each sub-function.

[0090] Select the matching standard functions from the function library based on the sub-function feature data.

[0091] Generate the function code corresponding to each template function based on the standard functions.

[0092] In this embodiment, a specific implementation method for generating function code is given. In this implementation method, semantic analysis can be performed on the description information of each template function, further refining the functions of each template function into smaller sub-tasks or steps, and constructing sub-functions corresponding to the sub-tasks or steps; extracting the keywords in the description information of each sub-function as the feature data of the sub-functions. At the same time, multiple template functions are pre-stored in the code generation engine. These template functions are usually common function templates that can implement basic functions, such as formatting functions, verification functions, conditional judgment functions, loop functions, etc. Multiple library functions are also pre-stored in the function libraries of each programming language. Multiple template functions and multiple library functions can be used as the function library, and the functions in the function library are used as standard functions. The keywords of the standard functions are extracted as the feature data of the standard functions.

[0093] For any sub-function, the similarity between the feature data of the sub-function and the feature data of the standard function can be calculated. Then, based on the similarity calculation result, the standard function with the largest similarity is selected as the standard function that matches the sub-function. Based on this standard function, the code for the corresponding sub-function is generated. By filling the codes of each sub-function into the template function, the code for each template function can be obtained. Among them, the calculation of similarity can adopt existing calculation methods such as Euclidean distance and cosine similarity, which will not be elaborated here.

[0094] It should be noted that if the similarity between the feature data of a certain sub-function and the feature data of all standard functions is less than the first set threshold, then this sub-function needs to be further refined into smaller sub-tasks or steps, and the above process is repeated until the similarity is greater than the first set threshold. Among them, the first set threshold is a preset constant, and those skilled in the art can select the specific value of the first set threshold according to actual needs.

[0095] From the above, it can be concluded that in this embodiment, by refining the functions of the template functions, the corresponding standard functions are obtained, and the codes of the template functions are automatically generated based on the standard functions. The codes of the standard functions have been pre-tested and verified, which can ensure the quality of the generated codes. Therefore, the workload of manually modifying the codes is reduced.

[0096] In an embodiment of the present disclosure, generating the corresponding function code based on the description information of each template function in the code framework template includes:

[0097] For any template function, calculate the first similarity between the description information of this any template function and the description information of N reference functions respectively; the reference functions are functions in the existing code library.

[0098] Select the reference function with the largest corresponding first similarity, and generate the function code of any template function based on this reference function.

[0099] In this embodiment, another specific implementation manner of generating the function code is given. In this implementation manner, a function (i.e., a reference function) similar to the function of the current template function can be found from the existing code library (such as an open-source library, an internal library, etc.) to automatically generate or assist in generating the required function code.

[0100] Exemplarily, key information can be extracted from the description information of the template function as the feature data of the template function, and then key information can be extracted from the description information of the reference function as the feature data of the reference function. The first similarity between the feature data of the template function and the feature data of the reference function is calculated. Furthermore, based on the calculation result of the first similarity, the reference function with the largest first similarity is selected, and the code corresponding to the template function is generated based on this reference function. Among them, the calculation of the first similarity can adopt existing calculation methods such as Euclidean distance and cosine similarity, which will not be elaborated here.

[0101] It should be noted that when selecting the reference function with the largest first similarity, it is necessary to simultaneously satisfy that the first similarity is greater than the second set threshold. Otherwise, this parameter function cannot be used to generate the code of the template function.

[0102] It can be concluded from the above that in this embodiment, selecting codes with similar functions from the existing code library for the code generation of the template function can significantly improve the efficiency and accuracy of code generation, and at the same time reduce the workload of repeatedly writing similar codes.

[0103] In an embodiment of the present disclosure, calculating the first similarity between the description information of any one template function and the description information of N reference functions respectively includes:

[0104] Calculating the second similarity between the function description information of any one template function and N reference functions, the third similarity between the number of parameters of any one template function and N reference functions, and the fourth similarity between the parameter types of any one template function and N reference functions respectively.

[0105] Calculating the first similarity based on the second similarity, the third similarity, and the fourth similarity corresponding to each reference function.

[0106] In this embodiment, the description information of the template function may include function description information (used to indicate the function to be implemented by the template function), the number of parameters, parameter types, etc. Therefore, the second similarity, the third similarity, and the fourth similarity can be calculated respectively from three dimensions of function, the number of parameters, and parameter types, and then the second similarity, the third similarity, and the fourth similarity are weighted and summed to obtain the first similarity between the description information of the template function and the description information of the reference function.

[0107] Exemplarily, the calculation of the second similarity can adopt the calculation method of cosine similarity, that is, extracting the keywords of the function description information of the template function as the feature data of the template function, and at the same time extracting the keywords of the function description information of the reference function as the feature data of the reference function, and calculating the similarity of the two feature data (that is, the feature vectors). Specifically, the following formula can be adopted:

[0108] ;

[0109] Among them, represents the second similarity, represents the feature data of the template function, represents the feature data of the reference function.

[0110] The calculation of the third similarity can be achieved by comparing the number of parameters, and the following formula can be specifically adopted:

[0111] ;

[0112] Among them, represents the third similarity, represents the number of parameters of the template function, represents the number of parameters of the reference function, represents the maximum value function.

[0113] The calculation of the fourth similarity can be achieved by separately determining the parameter type sets of the template function and the reference function, and then calculating the Jaccard similarity of the two sets. The following formula can be specifically adopted:

[0114] ;

[0115] Among them, represents the fourth similarity, represents the parameter type set of the template function, represents the parameter type set of the reference function, represents the intersection of the two sets, represents the union of the two sets.

[0116] It can be concluded from the above that this embodiment comprehensively considers the description information of the template function and the reference function in three dimensions of function, number of parameters, and parameter type, quantifies the similarity between them, can more comprehensively evaluate the similarity degree between the two functions, and thus more accurately find the reference function that matches the template function.

[0117] In an embodiment of the present disclosure, the code generation method further includes:

[0118] Screening multiple first reference functions from N reference functions; the first reference function is a reference function whose corresponding first similarity is greater than the second set threshold.

[0119] Adjusting the first similarity corresponding to the first reference function based on the usage frequency of each first reference function; the usage frequency is the number of times used within a set time.

[0120] In this embodiment, considering that functions with higher usage frequencies are often general solutions to common problems and have been fully tested to ensure their correctness and stability, these functions can be preferentially selected to generate the code of the template function. Therefore, based on the obtained multiple first reference functions, the first similarity corresponding to the first reference function can be adjusted according to the usage frequency of each first reference function, and the reference function with the largest first similarity can be selected based on the adjusted first similarity.

[0121] Exemplarily, the following formula can be used to adjust the first similarity:

[0122] ;

[0123] where, represents the adjusted first similarity, represents the first similarity before adjustment, represents the usage frequency of the first reference function, is a preset constant representing the reference value of the usage frequency, is a preset constant.

[0124] It can be concluded from the above that in this embodiment, the first similarity corresponding to the first reference function is adjusted according to the usage frequency of each first reference function, and the reference function with the largest first similarity is selected based on the adjusted first similarity to generate the code of the template function, further ensuring the correctness and stability of the generated code.

[0125] In an embodiment of the present disclosure, the code generation method further includes:

[0126] Selecting N reference functions from M reference functions based on the application information of the code framework.

[0127] In this embodiment, the application information of the code framework is used to indicate the application field information of the code framework (i.e., the applied business scenario). Considering that different business scenarios will also affect code generation. For example, in the financial field, strict requirements may be imposed on the accuracy and security of data, and the generated code may include a large amount of error handling and data verification logic. In game development, more attention may be paid to performance optimization and code generation related to graphics processing. Therefore, in this embodiment, first, N reference functions with the same application information as the template function are selected from M functions in the existing code library. On this basis, reference functions with similar functions are selected from the N reference functions according to the similarity, which is beneficial to obtaining reference functions that meet the actual requirements.

[0128] Corresponding to the code generation method in the above embodiment, Figure 2The block diagram of the code generation device provided by an embodiment of the present disclosure. For the sake of convenience, only the parts related to the embodiments of the present disclosure are shown. Refer to Figure 2 , the code generation device 20 includes: a semantic analysis module 21, a first response module 22, a second response module 23, and a code generation module 24.

[0129] Among them, the semantic analysis module 21 is used to perform semantic analysis on the requirement information input by the user to obtain the user intention; the requirement information is used to indicate the function implemented by the code;

[0130] The first response module 22 is used to generate a code framework based on the code generation model in response to the lack of one or more specified contents in the user intention;

[0131] The second response module 23 is used to generate a code framework based on the code generation tool in response to all specified contents being included in the user intention;

[0132] The code generation module 24 is used to generate the code corresponding to the code framework based on the programming language type input by the user.

[0133] In an embodiment of the present disclosure, the second response module 23 is specifically used for:

[0134] In response to the function implemented by the code shown in the user intention being a single function, generate a code framework based on the first type of code generation tool;

[0135] In response to the function implemented by the code shown in the user intention including multiple functions, generate a code framework based on the second type of code generation tool;

[0136] The data processing volume of the first type of code generation tool is less than that of the second type of code generation tool.

[0137] In an embodiment of the present disclosure, the code generation module 24 is specifically used for:

[0138] Extract the key information of the code framework; the key information includes the key elements and the association relationships between the key elements, and the key elements include modules, classes, functions, data structures, and control flows;

[0139] Build a general model based on the key information;

[0140] Generate a code framework template corresponding to the programming language type based on the general model and the set language mapping rules;

[0141] Generate the corresponding function code based on the description information of each template function in the code framework template to obtain the code corresponding to the code framework.

[0142] In an embodiment of the present disclosure, the code generation module 24 is specifically further used for:

[0143] Semantically analyze the description information of each template function to obtain the sub-functions included in each template function;

[0144] Extract features from the description information of each sub-function to obtain sub-function feature data;

[0145] Select matching standard functions from the function library based on the sub-function feature data;

[0146] Generate function codes corresponding to each template function based on the standard functions.

[0147] In an embodiment of the present disclosure, the code generation module 24 is further specifically configured to:

[0148] For any template function, calculate the first similarity between the description information of the any template function and the description information of N reference functions respectively; the reference functions are functions in the existing code library;

[0149] Screen the reference function with the largest corresponding first similarity, and generate the function code of any template function based on the reference function.

[0150] In an embodiment of the present disclosure, the code generation module 24 is further specifically configured to:

[0151] Calculate the second similarity between the any template function and the function description information of N reference functions, the third similarity between the any template function and the number of parameters of N reference functions, and the fourth similarity between the any template function and the parameter types of N reference functions respectively;

[0152] Calculate the first similarity based on the second similarity, the third similarity, and the fourth similarity corresponding to each reference function.

[0153] In an embodiment of the present disclosure, the code generation module 24 is further specifically configured to:

[0154] Select N reference functions from M reference functions based on the application information of the code framework.

[0155] See Figure 3 , Figure 3 is a schematic block diagram of an electronic device provided in an embodiment of the present disclosure. As Figure 3The electronic device 300 in the present embodiment shown may include: one or more processors 301, one or more input devices 302, one or more output devices 303, and one or more memories 304. The above-mentioned processors 301, input devices 302, output devices 303, and memories 304 communicate with each other through a communication bus 305. The memory 304 is used to store computer programs, and the computer programs include program instructions. The processor 301 is used to execute the program instructions stored in the memory 304. Among them, the processor 301 is configured to call the program instructions to execute the functions of each module / unit in the above-mentioned device embodiments, for example Figure 2 the functions of the modules 21 to 24 shown.

[0156] It should be understood that in the embodiments of the present disclosure, the so-called processor 301 may be a central processing unit (CPU), and this processor may also be other general-purpose processors, digital signal processors (DSPs), application specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or this processor may also be any conventional processor, etc.

[0157] The input device 302 may include a touchpad, a fingerprint acquisition sensor (for acquiring the fingerprint information and the direction information of the fingerprint of the user), a microphone, etc., and the output device 303 may include a display (such as an LCD), a speaker, etc.

[0158] The memory 304 may include a read-only memory and a random access memory, and provide instructions and data to the processor 301. A part of the memory 304 may also include a non-volatile random access memory. For example, the memory 304 may also store information about the device type.

[0159] In specific implementation, the processors 301, input devices 302, and output devices 303 described in the embodiments of the present disclosure may execute the implementation manners described in the first and second embodiments of the code generation method provided by the embodiments of the present disclosure, and may also execute the implementation manner of the electronic device described in the embodiments of the present disclosure, which will not be elaborated here.

[0160] In another embodiment of the present disclosure, a computer-readable storage medium is provided. The computer-readable storage medium stores a computer program, and the computer program includes program instructions. When the program instructions are executed by a processor, all or part of the processes in the methods of the above embodiments are implemented. It can also be completed by instructing related hardware through the computer program. The computer program can be stored in a computer-readable storage medium. When the computer program is executed by the processor, the steps of the above method embodiments can be implemented. Among them, the computer program includes computer program code, and the computer program code can be in the form of source code, object code, executable file or some intermediate form, etc. The computer-readable medium can include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disc, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electrical carrier signal, telecommunication signal, and software distribution medium, etc.

[0161] The computer-readable storage medium can be an internal storage unit of the electronic device in any of the foregoing embodiments, such as the hard disk or memory of the electronic device. The computer-readable storage medium can also be an external storage device of the electronic device, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the electronic device. Further, the computer-readable storage medium can also include both the internal storage unit and the external storage device of the electronic device. The computer-readable storage medium is used to store the computer program and other programs and data required by the electronic device. The computer-readable storage medium can also be used to temporarily store the data that has been output or will be output.

[0162] Those of ordinary skill in the art can realize that the units and algorithm steps of the examples described in combination with the embodiments disclosed herein can be implemented by electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the examples have been generally described according to functions in the above description. Whether these functions are executed in a hardware or software manner depends on the specific application and design constraints of the technical solution. Professional technicians can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of the present disclosure.

[0163] Those skilled in the art can clearly understand that for the convenience and brevity of description, the specific working processes of the above-described electronic devices and units can refer to the corresponding processes in the foregoing method embodiments and will not be elaborated herein.

[0164] In several embodiments provided by the present application, it should be understood that the disclosed electronic devices and methods can be implemented in other ways. For example, the device embodiments described above are merely illustrative. For example, the division of units is only a logical function division. In actual implementation, there may be other division methods. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the displayed or discussed couplings or direct couplings or communication connections to each other can be indirect couplings or communication connections through some interfaces or units, and can also be electrical, mechanical or other forms of connection.

[0165] The units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they can be located in one place, or can be distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiments of the present disclosure.

[0166] In addition, each functional unit in various embodiments of the present disclosure can be integrated in a processing unit, or each unit can exist physically alone, or two or more units can be integrated in one unit. The above integrated unit can be implemented in the form of hardware or in the form of a software functional unit.

[0167] The above is only the specific implementation manner of the present disclosure, but the protection scope of the present disclosure is not limited thereto. Any person skilled in the art within the technical scope disclosed by the present disclosure can easily think of various equivalent modifications or replacements, and these modifications or replacements should all be covered within the protection scope of the present disclosure. Therefore, the protection scope of the present disclosure should be subject to the protection scope of the claims.

Claims

1. A code generation method, characterized in that: include: Perform semantic analysis on the demand information input by the user to obtain the user's intention; The requirement information is used to indicate the function implemented by the code; In response to the lack of one or more specified contents in the user intention, generating a code framework based on a code generation model; In response to the user intention including all the specified contents, generating a code framework based on a code generation tool; Generate code corresponding to the code framework based on the programming language type input by the user; The generating the code corresponding to the code framework based on the programming language type input by the user includes: Extracting key information of the code framework; the key information includes key elements and associations between the key elements, and the key elements include modules, classes, functions, data structures and control flows; Building a general model based on the key information; Generate a code framework template corresponding to the programming language type based on the general model and the set language mapping rules; Generate corresponding function codes based on description information of each template function in the code framework template to obtain codes corresponding to the code framework; The generating corresponding function code based on the description information of each template function in the code framework template includes: For any template function, respectively calculate the first similarity between the description information of the any template function and the description information of N reference functions; the reference function is a function in an existing code library; A reference function with the largest corresponding first similarity is selected, and a function code of any template function is generated based on the reference function.

2. The code generation method according to claim 1, characterized in that: In response to the user intention including all the specified contents, generating a code framework based on a code generation tool includes: In response to the user intending to display that the function implemented by the code is a single function, generating a code framework based on a first type of code generation tool; In response to the user's intention that the function implemented by the code includes multiple functions, generating a code framework based on the second type of code generation tool; The data processing amount of the first type of code generation tool is smaller than the data processing amount of the second type of code generation tool.

3. The code generation method according to claim 1, characterized in that: The respectively calculating the first similarity between the description information of any template function and the description information of N reference functions includes: Respectively calculating a second similarity between the functional description information of the any template function and the N reference functions, a third similarity between the number of parameters of the any template function and the N reference functions, and a fourth similarity between the parameter types of the any template function and the N reference functions; The first similarity is calculated based on the second similarity, the third similarity and the fourth similarity corresponding to each reference function.

4. The code generation method according to claim 1, characterized in that: Also includes: N reference functions are selected from the M reference functions based on application information of the code framework.

5. A code generating device, characterized in that: include: Semantic analysis module, used to perform semantic analysis on the demand information input by the user to obtain the user's intention; The requirement information is used to indicate the function implemented by the code; A first response module, configured to generate a code framework based on a code generation model in response to the lack of one or more specified contents in the user intention; A second response module, configured to generate a code framework based on a code generation tool in response to the user intention containing all specified contents; A code generation module, used to generate code corresponding to the code framework based on the programming language type input by the user; The code generation module is specifically used for: Extract key information of the code framework; key information includes key elements and the relationship between key elements. Key elements include modules, classes, functions, data structures and control flows; Build a common model based on key information; Generate a code framework template corresponding to the programming language type based on the general model and the set language mapping rules; Generate corresponding function codes based on description information of each template function in the code framework template to obtain codes corresponding to the code framework; The code generation module is also specifically used for: For any template function, respectively calculate the first similarity between the description information of the any template function and the description information of N reference functions; the reference function is a function in an existing code library; A reference function with the largest first similarity is selected, and a function code of any template function is generated based on the reference function.

6. An electronic device comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 4 are implemented.

7. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 4 are implemented.

Citation Information

Patent Citations

  • Method and system for converting natural language into program code

    CN112306497A

  • Code management method and related device

    WO2024078000A1