Code generation method and device

Through semantic analysis and artificial intelligence code generation model, combined with code generation tools and template engine, the problem of code framework generation when user needs are unclear is solved, and the code that flexibly adapts to different programming languages ​​is realized, and the code that meets the diverse needs of users is met. Generate method.

CN119088368AActive Publication Date: 2024-12-06河北网星软件有限公司

Patent Information

Application Number
CN202411159909.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-22
Publication Date
2024-12-06
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, they cannot effectively generate appropriate code frameworks and cannot flexibly adapt to the needs of different programming languages.

Method used

Through semantic analysis of the demand information input by users, combine the artificial intelligence code generation model to generate a framework prototype for simple applications, and convert the code framework into specific code based on the programming language type selected by the user. The code generation tool and template engine are used to quickly generate the code framework to meet the requirements of the user. The diverse needs of users.

Benefits of technology

It realizes the rapid generation of reasonable code frameworks when user needs are unclear, helps users obtain preliminary ideas, and directly generate code suitable for programming languages ​​when requirements are clear, improving the efficiency and accuracy of code generation and meeting the diversity of users. need.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119088368A_ABST
    Figure CN119088368A_ABST
Patent Text Reader

Abstract

The invention provides a code generation method and device, and belongs to the technical field of software development, and the method comprises the following steps: carrying out semantic analysis on demand information input by a user to obtain a user intention; the demand information is used for indicating a code implementation function; 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; responding to all specified contents contained in the user intention, and generating a code framework based on a code generation tool; and generating a code corresponding to the code framework based on a programming language type input by a user. According to the code generation method and device, diversified requirements of users can be met.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure belongs to the technical field of software development, and more specifically, to a code generation method and device. 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 based on project requirements, team skills or personal preferences. Therefore, it is necessary to make further improvements to the code generation method to meet the diverse needs of users. Summary of the invention The purpose of the present invention is to provide a code generation method and device to meet the diverse needs of users.

[0003] According to a first aspect of the present disclosure, a code generation method is provided, comprising: Perform semantic analysis on the demand information input by the user to obtain the user's intention; the demand 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; The code corresponding to the code framework is generated based on the programming language type input by the user.

[0004] According to a second aspect of the embodiments of the present disclosure, there is provided a code generation device, including: A semantic analysis module is used to perform semantic analysis on the demand information input by the user to obtain the user's intention; the demand 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; The code generation module is used to generate code corresponding to the code framework based on the programming language type input by the user.

[0005] According to a third aspect of an embodiment of the present disclosure, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and running on the processor, wherein the processor implements the steps of the above-mentioned code generation method when executing the computer program.

[0006] According to a fourth aspect of the embodiments of the present disclosure, a computer-readable storage medium is provided, wherein 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.

[0007] The code generation method and device provided by the embodiments of the present disclosure have the following beneficial effects: In the disclosed embodiment, the user intent is first obtained by semantically analyzing the user needs. If the user intent lacks necessary specified content, that is, the user needs are not clear or are 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 user's incomplete intent, helping the user to obtain some preliminary framework ideas and continuously improve on this basis. If the user intent contains all the necessary specified content, 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 a specific programming language code, thereby realizing automatic code generation.

[0008] The disclosed embodiment flexibly generates a suitable code framework according to the completeness of the information provided by the user, and finally converts it into code in the programming language desired by the user, which is conducive to meeting the diverse needs of the user. BRIEF DESCRIPTION OF THE DRAWINGS

[0009] In order to more clearly illustrate the technical solutions in the embodiments of the present disclosure, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present disclosure. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0010] Figure 1 A flowchart of a code generation method provided by an embodiment of the present disclosure; Figure 2 A structural block diagram of a code generation device provided by an embodiment of the present disclosure; Figure 3 A schematic block diagram of an electronic device provided by an embodiment of the present disclosure. DETAILED DESCRIPTION

[0011] In the following description, specific details such as specific system structures and technologies are provided for the purpose of illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present disclosure. However, it should be clear to those skilled in the art that the present disclosure may 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 obstructing the description of the present disclosure with unnecessary details.

[0012] In order to make the purpose, technical solutions and advantages of the present disclosure more clear, specific embodiments will be described below in conjunction with the accompanying drawings.

[0013] Please refer to Figure 1 , Figure 1 A flowchart of a code generation method provided by an embodiment of the present disclosure, the method comprising: S101: semantically analyzing the requirement information input by the user to obtain the user's intention; the requirement information is used to indicate the function implemented by the code.

[0014] In this embodiment, the user's demand information is usually input in natural language, so the text input by the user can be semantically analyzed through natural language processing technology (such as semantic role labeling, dependency syntax analysis, etc.) to identify key functions, actions (or functions), input and output parameters, etc., and this information can be used as user intention.

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

[0016] 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 that developers need to write manually and reducing the risk of bugs caused by human errors, but these tools usually rely on predefined templates and rules and require users to specify necessary content. However, in the early exploration stage of software development, user needs are often not clear, and predefined templates and rules cannot be provided.

[0017] At this point, you can use a code generation model combined with artificial intelligence 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 user's incomplete intentions, helping users obtain some preliminary framework ideas and continuously improve on this basis. For example, if the user inputs: "I want a system that can create tasks", after semantic analysis, it is found that the user's intention lacks specific content such as assigning tasks, setting deadlines, and marking completion status. At this time, a relatively simple code framework can be generated based on the code generation model, and then the simple code framework can be improved through continuous interaction with the user.

[0018] S103: In response to the user intention including all designated contents, a code framework is generated based on a code generation tool.

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

[0020] For example, the user inputs the requirement information: "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 the user's intention contains all the specified content (create tasks, assign tasks, set deadlines, mark completion status). At this point, the code framework can be generated based on the code generation tool.

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

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

[0023] The corresponding language mapping rule is determined according to the programming language type input by the user, and then the generated code framework is converted into a specific programming language code according to the language mapping rule, thereby realizing automatic code generation.

[0024] It can be concluded from the above that this embodiment first obtains the user intent by semantically analyzing the user needs. If the user intent lacks necessary specified content, that is, the user needs are not very clear or are 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 user's incomplete intentions to help users obtain some preliminary framework ideas and continuously improve on this basis. If the user intent contains all the necessary specified content, 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 a specific programming language code, thereby realizing automatic code generation.

[0025] The disclosed embodiment flexibly generates a suitable code framework according to the completeness of the information provided by the user, and finally converts it into code in the programming language desired by the user, which is conducive to meeting the diverse needs of the user.

[0026] In one embodiment of the present disclosure, in response to the user's intention including all specified contents, a code framework is generated based on a code generation tool, including: In response to the user's intention to display that the function implemented by the code is a single function, a code framework is generated based on a first type of code generation tool.

[0027] In response to the user's intention that the function implemented by the code includes multiple functions, a code framework is generated based on the second type of code generation tool.

[0028] The data processing amount of the first type of code generation tool is smaller than that of the second type of code generation tool.

[0029] In this embodiment, if the function that the user intends to display the code to implement is a single function, such as creating a simple workflow application, a form collection system, a small internal management tool, etc., a lightweight first-class 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, is simple to operate, and can quickly and conveniently generate a code framework.

[0030] If the user intends to display code to implement multiple functions of a system, such as building a large financial trading system, the second type of code generation tool needs to be used to establish detailed business process models, data models, etc. to accurately guide development. The second type of code generation tools can handle more complex scenarios and support modular and componentized code generation. They can provide richer template libraries 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 tools can be model-driven development (MDD) tools, etc. Since multi-functional implementations often involve operations such as data integration, processing, and analysis, the second type of code generation tools have a relatively large amount of data processing to support efficient processing of large amounts of data.

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

[0032] In one embodiment of the present disclosure, generating code corresponding to a code framework based on a programming language type input by a user includes: Extract key information of the code framework; key information includes key elements and the relationship between key elements. Key elements include functions, data structures and control flows.

[0033] Build a common model based on key information.

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

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

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

[0037] 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 the task completion status." In response to this user demand, according to the method of the aforementioned embodiment, the corresponding code framework can be obtained as follows: First, there needs to be a Task class, which contains the attributes of task name, person in charge, deadline and completion status. Then, there needs to be a TaskManager class, which can implement the method of creating a new task and set the various attributes of the task when creating the task. This class also has a method for assigning tasks, which can assign tasks to different people. At the same time, it also has a method for setting the task deadline, which directly modifies the deadline 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 is needed to save all tasks. In terms of control flow, when creating a task, it will check whether the input task information is complete and valid. When assigning a task, it will check whether the specified person exists. When setting the deadline and marking the completion status, some rationality checks will also be performed.

[0038] By extracting key information from the above code framework, we can obtain the following key information: Key elements include: Class: Task, TaskManager; function: createTask (create task) in TaskManager class; assignTask (assigning tasks) in the TaskManager class; setTaskDueDate (set task deadline) in TaskManager class; markTaskCompleted (marking task completion status) in the TaskManager class; Data structure: may use a list or database to store tasks (no specific name is given); Control Flow: Check the integrity and validity of the task information in createTask.

[0039] Check in assignTask whether the assigned person exists.

[0040] Relationships between key elements: The TaskManager class manages objects of the Task class and operates and sets properties of Task objects through various functions.

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

[0042] For example, a template of a general model may be pre-built, and then the extracted key information may be filled into the template of the general model to obtain a general model for user needs, as shown below: Task class: Contains properties: task name, responsible person, due date, and completion status.

[0043] TaskManager class: Manages a series of Task objects.

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

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

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

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

[0048] Data Storage: Use a list or database to store all Task objects.

[0049] On the basis of the obtained general model, a code framework template of the user-specified programming language type 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 conventions of the user-specified programming language. For example, if there is a class in the general model and the user-specified programming language is Python, the class, function, and calling relationship between functions are declared according to Python's language mapping rules.

[0050] The code framework template usually includes multiple template functions. In this case, the template function only contains the corresponding description information. The code corresponding to the description information can be generated based on the code generation engine, thereby generating 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 the template function is as follows: def createTask(task_name, assignee, due_date): #Create a new Task object and check the information integrity and validity.

[0051] From the above, it can be concluded that this embodiment constructs a general model by extracting key information of the code framework, and then generates a code framework template corresponding to the programming language type according to the general model and the set language mapping rules, thereby realizing code generation according to the programming language type required by the user and meeting the user's diverse needs for programming language types.

[0052] In one embodiment of the present disclosure, generating corresponding function codes based on description information of each template function in the code framework template includes: Semantic analysis is performed on the description information of each template function to obtain the sub-functions contained in each template function.

[0053] Feature extraction is performed on the description information of each sub-function to obtain sub-function feature data.

[0054] A matching standard function is selected from the function library based on the sub-function feature data.

[0055] Generates function codes corresponding to each template function based on the standard function.

[0056] 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, and the functions of each template function can be further refined into smaller subtasks or steps, and subfunctions corresponding to the subtasks or steps can be constructed; keywords in the description information of each subfunction are extracted as feature data of the subfunction. At the same time, a plurality of template functions are pre-stored in the code generation engine. These template functions are usually commonly used function templates that can realize basic functions, such as formatting functions, verification functions, conditional judgment functions, loop functions, etc., and a plurality of library functions are also pre-stored in the function library of each programming language. Multiple template functions and multi-library functions can be used as function libraries, and the functions in the function library can be used as standard functions. Keywords of standard functions are extracted as feature data of standard functions.

[0057] 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, and then the standard function with the greatest similarity is selected as the standard function matching the sub-function according to the similarity calculation result, and the code of the corresponding sub-function is generated based on the standard function, and the code of each sub-function is filled into the template function to obtain the code of each template function. Among them, the calculation of similarity can adopt the existing calculation methods such as Euclidean distance and cosine similarity, which will not be repeated here.

[0058] It should be noted that if the similarity between the characteristic data of a sub-function and the characteristic data of all standard functions is less than the first set threshold, the 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. 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.

[0059] From the above, it can be concluded that this embodiment refines the functions of the template function to obtain the corresponding standard functions, and automatically generates the code of the template function based on the standard functions. The code of the standard function is pre-tested and verified to ensure the quality of the generated code, thereby reducing the workload of manual code modification. In one embodiment of the present disclosure, generating corresponding function codes based on description information of each template function in the code framework template includes: For any template function, the first similarity between the description information of the template function and the description information of N reference functions is calculated respectively; the reference function is a function in an existing code library.

[0060] 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.

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

[0062] 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, and the first similarity between the feature data of the template function and the feature data of the reference function is calculated, and then the reference function with the largest first similarity is selected according to the first similarity calculation result, and the code of the corresponding template function is generated based on the reference function. Among them, the calculation of the first similarity can adopt the existing calculation methods such as Euclidean distance and cosine similarity, which will not be repeated here.

[0063] 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 a second set threshold, otherwise, the parameter function cannot be used to generate the code of the template function.

[0064] It can be concluded from the above that this embodiment selects codes with similar functions from the existing code library for code generation of template functions, which can significantly improve the efficiency and accuracy of code generation and reduce the workload of repeatedly writing similar codes.

[0065] In one embodiment of the present disclosure, respectively calculating the first similarity between the description information of any template function and the description information of N reference functions includes: The second similarity between the functional description information of any template function and the N reference functions, the third similarity between the number of parameters of any template function and the N reference functions, and the fourth similarity between the parameter types of any template function and the N reference functions are calculated respectively.

[0066] The first similarity is calculated based on the second similarity, the third similarity, and the fourth similarity corresponding to each reference function. In this embodiment, the description information of the template function may include functional description information (used to indicate the function to be implemented by the template function), the number of parameters, the parameter type, etc. Therefore, the second similarity, the third similarity and the fourth similarity can be calculated from the three dimensions of function, number of parameters and parameter type respectively, and then the second similarity, the third similarity and the fourth similarity are weightedly summed to obtain the first similarity between the description information of the template function and the description information of the reference function.

[0067] Exemplarily, the second similarity can be calculated by using the cosine similarity calculation method, that is, extracting keywords from the functional description information of the template function as the feature data of the template function, and extracting keywords from the functional 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, feature vectors). Specifically, the following formula can be used: ; in, represents the second similarity, Represents the characteristic data of the template function, Represents the characteristic data of the reference function.

[0068] The third similarity can be calculated by comparing the number of parameters, and the following formula can be used: ; in, represents the third similarity, Indicates the number of parameters of the template function, Indicates the number of parameters of the reference function, Represents the maximum value function.

[0069] The calculation of the fourth similarity can be achieved by respectively determining the parameter type sets of the template function and the reference function, and then calculating the Jaccard similarity of the two sets. Specifically, the following formula can be used: ; in, 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 two sets, Represents the union of two sets.

[0070] From the above, it can be concluded that this embodiment comprehensively considers the three dimensions of function, number of parameters, and parameter type to quantify the similarity between the description information of the template function and the description information of the reference function, which can more comprehensively evaluate the similarity between the two functions, thereby more accurately finding a reference function that matches the template function. In one embodiment of the present disclosure, the code generation method further includes: A plurality of first reference functions are selected from the N reference functions; the first reference functions are reference functions whose corresponding first similarities are greater than a second set threshold.

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

[0072] In this embodiment, considering that functions with higher usage frequencies are often general solutions to common problems, these functions have been fully tested to ensure their correctness and stability, and these functions can be used to generate the code of the template function. Therefore, on the basis of obtaining multiple first reference functions, the first similarity corresponding to the first reference function can be adjusted based on 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. Exemplarily, the first similarity may be adjusted using the following formula: ; in, represents the adjusted first similarity, represents the first similarity before adjustment, represents the usage frequency of the first reference function, is a preset constant, indicating the reference value of the frequency of use. is a preset constant.

[0073] From the above, it can be concluded that this embodiment adjusts the first similarity corresponding to the first reference function based on the usage frequency of each first reference function, and selects the reference function with the largest first similarity based on the adjusted first similarity to generate the code of the template function, thereby further ensuring the correctness and stability of the generated code.

[0074] In one embodiment of the present disclosure, the code generation method further includes: N reference functions are selected from the M reference functions based on application information of the code framework.

[0075] In this embodiment, the application information of the code framework is used to indicate the application field information of the code framework (that is, the business scenario of the application). Taking into account that different business scenarios will also have an impact on code generation, for example, in the financial field, strict requirements may be required for the accuracy and security of the data, and the generated code may contain a large amount of error handling and data verification logic. In game development, more attention may be paid to code generation related to performance optimization and graphics processing. Therefore, this embodiment first selects N reference functions with the same application information as the template function from the M functions of 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 conducive to obtaining reference functions that meet actual needs.

[0076] Corresponding to the code generation method of the above embodiment, Figure 2 This is a structural block diagram of a code generation device provided by an embodiment of the present disclosure. For ease of explanation, only the parts related to the embodiment of the present disclosure are shown. 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. The semantic analysis module 21 is used to perform semantic analysis on the demand information input by the user to obtain the user's intention; the demand information is used to indicate the function implemented by the code; A first response module 22, for generating 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 23, configured to generate a code framework based on a code generation tool in response to the user intention containing all specified contents; The code generation module 24 is used to generate code corresponding to the code framework based on the programming language type input by the user.

[0077] In one embodiment of the present disclosure, the second response module 23 is specifically used to: In response to the user's intention to display that the function implemented by the code is a single function, a code framework is generated based on a first type of code generation tool; In response to the user's intention to display that the function implemented by the code includes multiple functions, a code framework is generated based on the second type of code generation tool; The data processing amount of the first type of code generation tool is smaller than that of the second type of code generation tool.

[0078] In one embodiment of the present disclosure, the code generation module 24 is specifically used to: 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 the description information of each template function in the code framework template to obtain codes corresponding to the code framework.

[0079] In one embodiment of the present disclosure, the code generation module 24 is further configured to: Perform semantic analysis on the description information of each template function to obtain the sub-functions contained in each template function; Extract features from the description information of each sub-function to obtain sub-function feature data; Selecting a matching standard function from a function library based on the sub-function feature data; Generates function codes corresponding to each template function based on the standard function.

[0080] In one embodiment of the present disclosure, the code generation module 24 is further configured to: 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.

[0081] In one embodiment of the present disclosure, the code generation module 24 is further configured to: 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.

[0082] In one embodiment of the present disclosure, the code generation module 24 is further configured to: N reference functions are selected from the M reference functions based on application information of the code framework.

[0083] See also Figure 3 , Figure 3 A schematic block diagram of an electronic device provided by an embodiment of the present disclosure. Figure 3The electronic device 300 in the 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 processors 301, input devices 302, output devices 303 and memories 304 communicate with each other via a communication bus 305. The memory 304 is used to store computer programs, which include program instructions. The processor 301 is used to execute the program instructions stored in the memory 304. The processor 301 is configured to call the program instructions to execute the functions of each module / unit in the above-mentioned device embodiments, such as Figure 2 The functions of modules 21 to 24 are shown.

[0084] It should be understood that in the embodiment of the present disclosure, the processor 301 may be a central processing unit (CPU), and the processor may also be other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.

[0085] The input device 302 may include a touch panel, a fingerprint collection sensor (for collecting the user's fingerprint information and fingerprint direction information), a microphone, etc., and the output device 303 may include a display (LCD, etc.), a speaker, etc.

[0086] The memory 304 may include a read-only memory and a random access memory, and provide instructions and data to the processor 301. A portion 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.

[0087] In a specific implementation, the processor 301, input device 302, and output device 303 described in the embodiments of the present disclosure can execute the implementation methods described in the first and second embodiments of the code generation method provided in the embodiments of the present disclosure, and can also execute the implementation methods of the electronic device described in the embodiments of the present disclosure, which will not be repeated here.

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

[0089] The computer-readable storage medium may be an internal storage unit of the electronic device of any of the aforementioned embodiments, such as a hard disk or memory of the electronic device. The computer-readable storage medium may also be an external storage device of the electronic device, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (SecureDigital, SD) card, a flash card (Flash Card), etc. equipped on the electronic device. Furthermore, the computer-readable storage medium may also include both an internal storage unit of the electronic device and an external storage device. The computer-readable storage medium is used to store computer programs and other programs and data required by the electronic device. The computer-readable storage medium may also be used to temporarily store data that has been output or is to be output.

[0090] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in terms of function in the above description. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this disclosure.

[0091] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the electronic devices and units described above can refer to the corresponding processes in the aforementioned method embodiments, and will not be repeated here.

[0092] In the several embodiments provided in 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 only schematic, for example, the division of units is only a logical function division, and there may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces or units, or it can be an electrical, mechanical or other form of connection.

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

[0094] In addition, each functional unit in each embodiment of the present disclosure may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0095] The above are only specific embodiments of the present disclosure, but the protection scope of the present disclosure is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed in the present disclosure, and these modifications or replacements should be included in the protection scope of the present disclosure. Therefore, the protection scope of the present disclosure should be based on 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; The code corresponding to the code framework is generated based on the programming language type input by the user.

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 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; Based on the description information of each template function in the code framework template, a corresponding function code is generated to obtain a code corresponding to the code framework.

4. The code generation method according to claim 3, characterized in that: The generating corresponding function code based on the description information of each template function in the code framework template includes: Perform semantic analysis on the description information of each template function to obtain the sub-functions contained in each template function; Extract features from the description information of each sub-function to obtain sub-function feature data; Selecting a matching standard function from a function library based on the sub-function feature data; Generate function codes corresponding to each template function based on the standard function.

5. The code generation method according to claim 3, characterized in that: 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.

6. The code generation method according to claim 5, 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.

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

8. 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; The code generation module is used to generate code corresponding to the code framework based on the programming language type input by the user.

9. 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 7 are implemented.

10. 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 7 are implemented.

Citation Information

Patent Citations

  • Method and system for converting natural language into program code

    CN112306497A

  • Deep code searching method, system and device based on code structure semantic information

    CN113761163A

  • Code generation method and device, electronic equipment and storage medium

    CN114741070A

  • Code search system and method based on graph serialization

    CN115268869A

  • Code data processing method and device, computer equipment and storage medium

    CN116974554A

Cited By

  • Method and device for generating programming language according to clinical DVP table

    CN120929062A

  • Code generation method, code generation model training method, and apparatus, device and medium

    WO2026124664A1