Code generation method, device, equipment and storage medium
By obtaining the natural language description information and variable definition table of the target application, and using the encoding network and the decoding network to generate the target code, the syntax error problem caused by inconsistent variable names in traditional code generation is solved, and the semantic legitimacy and accuracy of code generation is improved.
Patent Information
- Application Number
- CN202210346225.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-31
- Publication Date
- 2025-03-14
- Estimated Expiration
- 2042-03-31
AI Technical Summary
In traditional code generation methods, variable names defined by different developers may be different, resulting in syntax errors during code integration, poor quality, low semantic legitimacy and accuracy.
By obtaining the natural language description information of the target application and the variable definition table, matching the variable name, and generating the target code using the encoding and decoding networks, ensuring that the unified variable name is used in the code.
It improves the semantic legitimacy and accuracy of code generation, avoids the situation where the same variable uses different variable names, and improves the efficiency and accuracy of code generation.
Smart Images

Figure CN115048102B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a code generation method, device, equipment and storage medium. Background Art
[0002] How to automatically generate code based on natural language description information of code functions is a research hotspot in the current computer technology field.
[0003] Traditional code generation (such as the front-end code generation of an application) is manually edited by multiple developers. Different functional modules may use the same variable, but the variable names defined by different developers may be different, resulting in syntax errors and poor quality when they are integrated and run together, making the semantic legitimacy of the generated code poor and the accuracy low. Summary of the invention
[0004] The embodiments of the present invention provide a code generation method, apparatus, device and storage medium for improving the semantic legitimacy and accuracy of generated code.
[0005] In a first aspect, an embodiment of the present invention provides a code generation method, the method comprising:
[0006] Acquire first natural language description information for describing the function of the target code in the target application, and a variable definition table corresponding to the target application, wherein the variable definition table stores second natural language description information corresponding to variable names and variable meanings;
[0007] According to the matching degree between the first natural language description information and the second natural language description information, determining a target variable name matching the first natural language description information from the variable definition table;
[0008] Encoding the first natural language description information through an encoding network to obtain a semantic encoding matrix corresponding to the first natural language description information;
[0009] The semantic encoding matrix and the word vector corresponding to the target variable name are input into a decoding network to obtain the target code corresponding to the first natural language description information through the decoding network, wherein the target code includes the target variable name.
[0010] In a second aspect, an embodiment of the present invention provides a code generation device, the device comprising:
[0011] An acquisition module, used to acquire first natural language description information for describing the function of the target code in the target application, and a variable definition table corresponding to the target application, wherein the variable definition table stores second natural language description information corresponding to the variable name and the variable meaning;
[0012] a determination module, configured to determine, from the variable definition table, a target variable name matching the first natural language description information according to a matching degree between the first natural language description information and the second natural language description information;
[0013] An encoding module, configured to encode the first natural language description information through an encoding network to obtain a semantic encoding matrix corresponding to the first natural language description information;
[0014] A decoding module is used to input the semantic encoding matrix and the word vector corresponding to the target variable name into a decoding network, so as to obtain the target code corresponding to the first natural language description information through the decoding network, wherein the target code includes the target variable name.
[0015] In a third aspect, an embodiment of the present invention provides an electronic device, comprising: a memory, a processor, and a communication interface; wherein the memory stores executable code, and when the executable code is executed by the processor, the processor can at least implement the code generation method described in the first aspect.
[0016] In a fourth aspect, an embodiment of the present invention provides a non-temporary machine-readable storage medium having executable code stored thereon. When the executable code is executed by a processor of an electronic device, the processor can at least implement the code generation method described in the first aspect.
[0017] In an embodiment of the present invention, a variable definition table corresponding to the target application is pre-set, and each variable name and its variable meaning described in natural language are stored in the variable definition table. In addition, for the target application, the function of each code is known, and the function is described in natural language. Based on the natural language description information of the known code function, the corresponding code can be automatically generated in the following manner: obtain the first natural language description information describing the function of the target code, and the variable name stored in the variable definition table and the second natural language description information describing the variable meaning, and then determine the target variable name matching the first natural language description information from the variable definition table according to the matching degree between the first natural language description information and each second natural language description information. The first natural language description information is encoded by the encoding network to obtain the semantic encoding matrix corresponding to the first natural language description information, and the word vector corresponding to the semantic encoding matrix and the target variable name is input into the decoding network to obtain the target code corresponding to the first natural language description information through the decoding network, and the target code includes the target variable name.
[0018] In the code generation scheme provided in the embodiment of the present invention, a solution is provided for integrating the standardized variable names defined in the variable definition table into the code generation process, by pre-generating the variable definition table, and storing the variable names and the second natural language description information of the variable meanings in the variable definition table, based on the unified definition of the variable names in the variable definition table, in the code generation process, based on the first natural language description information of a code function, the target variable name involved is used as auxiliary information to assist in the automatic generation of the target code corresponding to the first natural language description information, which is not only efficient, but also does not have the situation of using different variable names for the same variable, and has good accuracy. By integrating the pre-established variable name specifications into the code generation process, the semantic legitimacy of the code generation can be improved, and the accuracy of the code generation can be improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0020] Figure 1 A schematic diagram of an alternative code generation framework;
[0021] Figure 2 A flowchart of a code generation method provided by an embodiment of the present invention;
[0022] Figure 3 A schematic diagram of a code generation model provided by an embodiment of the present invention;
[0023] Figure 4 A flowchart of an optional code generation method provided by an embodiment of the present invention;
[0024] Figure 5 A schematic diagram of the structure of a code generation device provided by an embodiment of the present invention;
[0025] Figure 6 A schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0026] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0027] Some embodiments of the present invention are described in detail below in conjunction with the accompanying drawings. In the case where there is no conflict between the embodiments, the following embodiments and the features in the embodiments can be combined with each other. In addition, the step sequence in the following method embodiments is only an example and not a strict limitation.
[0028] The code generation method provided in the embodiment of the present invention may be executed by an electronic device, which may be a server or a user terminal, and the server may be a physical server or a virtual server (virtual machine) in the cloud.
[0029] First, some concepts involved in the embodiments of the present invention are described:
[0030] Natural language usually refers to a language that evolves naturally with culture. For example, Chinese, English, and Japanese are all examples of natural languages.
[0031] Transformer: refers to a neural network structure that is widely used in the field of Natural Language Process (NLP), such as machine translation, question-answering systems, text summarization, and speech recognition. Among them, the self-attention layer is the key defining feature of the Transformer model.
[0032] Requirements document: A requirements document is a document that describes the functions that the software needs to implement, the various functional modules and their importance, and the processing flow from the perspective of the user or design developer. It can be specifically broken down according to the product goals proposed by the user, and each specific functional point can be summarized. Each functional point can then be broken down into various different operating processes, and each operating process can be technically defined.
[0033] Embedding: literally means "embedding", which is actually a mapping from semantic space to vector space, while trying to keep the relationship between the original samples in the semantic space in the vector space as much as possible. For example, the positions of two semantically similar words in the vector space are also relatively close.
[0034] The multi-head attention mechanism (MHA) is to map a query matrix Q and a series of key-value pairs (key matrix K and value matrix V) together to obtain an output sequence.
[0035] At present, the methods for directly generating code for general language programs are mainly divided into: 1) code generation methods based on word sequences, for example, using a character-level Seq2seq model to generate code for card game programs in Java and Python; 2) code generation methods based on production rules. The process of generating code is regarded as selecting several actions of code production rules, thereby gradually generating an abstract syntax tree, and then converting and generating code.
[0036] Considering that in requirement documents or design documents, standardized variable names are usually formulated for variables with specific meanings in the code. Therefore, in the process of code writing or generation, for variables with such specific meanings, the variable names specified for the variables in the design documents must be used, and the variable names used for variables with the same meaning in different locations must be consistent.
[0037] In order to make the code directly generated from the general language program conform to the pre-designed variable specifications and improve the semantic legitimacy and accuracy of the generated code. Figure 1 An exemplary system architecture 100 for implementing an embodiment of a code generation apparatus and to which the code generation method according to an embodiment of the present invention can be applied is shown.
[0038] like Figure 1 As shown, the system architecture 100 may include an application front-end 101, an application back-end 102, a network 103 and a server 104 (a server for providing code generation services, which may be located in the cloud).
[0039] In the embodiment of the present invention, the application front-end and the application back-end form a C / S architecture, which corresponds to the target application mentioned in the embodiment of the present invention, that is, the application front-end refers to the front-end device of the client program running the target application, and the application back-end refers to the back-end device (for example, a back-end server) of the server program running the target application.
[0040] The network 103 is used as a medium to provide a communication link between the application front end 101, the application back end 102 and the server 104. The network 103 may include various connection types, such as wired, wireless communication links or optical fiber cables.
[0041] The user can use the application front end 101 to interact with the application back end 102 and the server 104 through the network 103 to receive or send messages, etc. Various communication client applications can be installed on the application front end 101, such as programming applications, shopping applications, search applications, instant messaging tools, email clients, social platform software, etc.
[0042] The server 104 is a server that provides code generation services, such as a background server that provides support for programming applications and code generation applications running on the application backend 102. It should be noted that the server 104 can be hardware or software. When the server is hardware, it can be implemented as a distributed server cluster composed of multiple servers, or it can be implemented as a single server. When the server is software, it can be implemented as multiple software or software modules (for example, used to provide distributed services), or it can be implemented as a single software or software module. It is not specifically limited here.
[0043] The code generation solution provided by the present invention is executed by the server 104. Taking the code generation requirement of a code function corresponding to a certain product (such as a code for describing the discount price of a coupon) triggered by the application front end as an example, the server 104 generates a target code including a variable name (such as discountPrice, meaning the discount price) for the application front end, and sends it to the application front end 101. After the generated target code is given to the application front end 101, when the application front end 101 runs the target code, it will request the real-time variable value corresponding to the variable name (such as discountPrice) from the application back end 102. The application back end 102 reads the variable value currently corresponding to the variable name from the database (the database stores the discount price information of each product counted by the application back end in real time) (such as the current coupon for the product can be discounted by 50 yuan) and feeds it back to the application front end 101. The application front end 101 assigns and renders the variable name, for example: the coupon has been discounted by 50 yuan.
[0044] Figure 2 A flowchart of a code generation method provided by an embodiment of the present invention is as follows: Figure 2 As shown, the method comprises the following steps:
[0045] 201. Obtain first natural language description information for describing the function of a target code in a target application, and a variable definition table corresponding to the target application, wherein the variable definition table stores second natural language description information corresponding to variable names and variable meanings.
[0046] 202. According to the matching degree between the first natural language description information and the second natural language description information, determine a target variable name matching the first natural language description information from a variable definition table.
[0047] 203. Encode the first natural language description information through a coding network to obtain a semantic coding matrix corresponding to the first natural language description information.
[0048] 204. Input the semantic encoding matrix and the word vector corresponding to the target variable name into the decoding network to obtain the target code corresponding to the first natural language description information through the decoding network, wherein the target code includes the target variable name.
[0049] Optionally, the code generation scenario in the embodiment of the present invention may be, but is not limited to, in the e-commerce field, for example, e-commerce shopping guide and product marketing scenarios, brand marketing scenarios, etc. The above target application may be an e-commerce application, etc.
[0050] In this embodiment, the method for generating code runs on an execution subject (eg Figure 1 The server 104) can be connected via a wired connection or a wireless connection. Figure 1 The application front end 101 obtains first natural language description information submitted by a user (e.g., a code developer) for describing the function of the target code in the target application (e.g., an e-commerce application). Taking the e-commerce application as an example, the target code optionally includes code for rendering the target interface element in the e-commerce application. The target interface element may be: product pictures, product introductions, product promotions, and other content.
[0051] Optionally, the first natural language description information may be an input sentence described in any natural language (e.g., Chinese, English, etc.). For example, it may be a sentence input into the application front end 101 by a user through an input device (e.g., a mouse, keyboard, touch screen, microphone, etc.). Taking an e-commerce application as an example, the functions of the target code may include: product interface display, product details display, monthly sales volume of the product, coupons and discount prices of the product, pre-sale prices, etc.
[0052] Taking the example of the application front end 101 triggering the code generation requirement of a certain code function corresponding to a certain product in an e-commerce application (such as a code used to describe the coupon discount price), after the user inputs the first natural language description information used to describe the function of the target code in the e-commerce application (such as the coupon discount price of a certain product) into the application front end 101, the application front end will send a code generation request to the server 104, triggering the server 104 to obtain the first natural language description information and the variable definition table corresponding to the e-commerce application.
[0053] Optionally, in an embodiment of the present invention, the variable definition table can be pre-generated. For example, the design and development personnel extract the variable meanings of these description information based on the description information in the requirement document, such as the description of the functions that the e-commerce application needs to implement, the various functional modules and their importance, and the service process. Although the same variable may have one or more description information of the variable meaning, or the same variable may appear in different functional modules, in an embodiment of the present invention, by pre-defining a unified variable name for the same variable, the situation of using different variable names for the same variable will not occur. Afterwards, the variable definition table is generated by establishing a connection between the natural language description information of the variable meaning and the variable name, such as a corresponding relationship.
[0054] As an optional embodiment, through the aforementioned pre-generation process, each item in the variable definition table obtained includes a variable name and the second natural language description information of the meaning of the variable name. For example:
[0055] If the variable name is btnStatus, the natural language description of the variable name is: button status;
[0056] If the variable name is: resultButton, the natural language description of the variable name is: result button;
[0057] If the variable name is: imageUrl, then the natural language description of the variable name is: image link; and so on.
[0058] It should be noted that after the variable definition table is pre-generated, it can be used in the subsequent code generation stage, and in order to avoid the situation where the same variable has different variable names, the data state of the variable definition table is unmodifiable in the code generation stage.
[0059] Since the variable definition table defines multiple variable names and multiple second natural language description information with corresponding variable meanings, after obtaining the first natural language description information and the multiple second natural language description information in the variable definition table, it is necessary to calculate the matching degree between the first natural language description information and the multiple second natural language description information, and then select the target variable name that matches the first natural language description information from the variable definition table based on the sorting result of the matching degree.
[0060] Optionally, in a specific implementation process, the following method may be used to calculate the matching degree between the first natural language description information and the second natural language description information: for example, determining the edit distance between the first natural language description information and the second natural language description information as the matching degree.
[0061] Alternatively, edit distance is the most basic algorithm for measuring text similarity in the NLP field. In the NLP field, using edit distance to measure text similarity is based on the assumption that semantically similar sentences are likely to have similar characters or words. Edit distance can be understood as determining a word w_1 in the first natural language description information and a similar word w_2 in the second natural language description information, and calculating the minimum number of single-character editing operations required to convert one word w_1 into another word w_2.
[0062] For another example, the degree of match between the first natural language description information and the second natural language description information can also be calculated: determine the first word vector corresponding to the first natural language description information and the second word vector corresponding to the second natural language description information; and use the distance between the first word vector and the second word vector as the degree of match.
[0063] Optionally, a word vector cosine algorithm can be used to determine the first word vector corresponding to the first natural language description information and the second word vector corresponding to the second natural language description information, and calculate the distance between the first word vector and the second word vector as the matching degree. The word vector cosine algorithm treats the natural language description information as a vector in a multidimensional space, calculates the familiarity of the two natural language description information, that is, calculates and determines whether the directions of the two vectors in this multidimensional space are the same.
[0064] After calculating the matching degree between the first natural language description information and the multiple second natural language description information according to the above optional calculation method, the multiple matching degrees are sorted according to their sizes to obtain the sorting result, and then the second natural language description information with the top k (where k≥1) matching degrees are selected according to the sorting result. The selected k second natural language description information are used as the natural language description information matching the first natural language description information. Therefore, the corresponding k variable names are retrieved from the variable definition table as the target variable names according to the selected k second natural language description information.
[0065] After determining the target variable name matching the first natural language description information from the variable definition table, the code generation model provided in the embodiment of the present invention can be used to generate the target code corresponding to the first natural language description information based on the first natural language description information and using the target variable name matching the first natural language description information as auxiliary information. By incorporating the pre-established variable name specification into the code generation process, the semantic legitimacy of the code generation can be improved, and the accuracy of the code generation can be improved.
[0066] Optionally, the code generation model provided in the embodiment of the present invention includes an encoding network and a decoding network, that is, an encoding network Encoder-decoding network Decoder framework is adopted. The Encoder-Decoder framework is a common framework mode in the field of deep learning and is widely used in the fields of text processing, language recognition, and image processing. Therefore, the above code generation model can be understood as a sequence-to-sequence model, referred to as a seq2seq model.
[0067] In the embodiment of the present invention, both the encoding network and the decoding network essentially use a neural network model. The encoding network can be composed of at least one (for example, N) encoders connected in sequence, and the decoding network can be composed of at least one (for example, N) decoders connected in sequence. The encoder and the decoder can use models with different structures in different tasks, such as the recurrent neural network model or convolutional neural network model commonly used in text processing and language recognition. For example, the code generation model in the embodiment of the present invention can adopt a structure similar to the Transformer model.
[0068] In an embodiment of the present invention, the input of the encoding network is the first natural language description information, the encoding network is used to encode the first natural language description information, and a semantic encoding matrix corresponding to the first natural language description information is obtained as output by the encoding network.
[0069] Specifically, the first natural language description information may be firstly segmented, and then the obtained multiple segmented words are input into the encoding network, and the semantic encoding matrix corresponding to the first natural language description information is finally output through the semantic encoding processing of the encoding network.
[0070] In the embodiment of the present invention, since the decoding network selects which word to output as the current word according to the probability of the corresponding word in the dictionary at each decoding step, when the dictionary in the decoding network is too large, the computational complexity is large and the memory required is also relatively large.
[0071] As an optional embodiment, in order to use the target variable name as auxiliary information in the subsequent code generation process, and to reduce the size of the dictionary in the decoding network and the computational complexity when the decoding network performs decoding calculations. After determining the k target variable names that match the above-mentioned first natural language description information from the variable definition table according to the matching degree, the word vector of the target variable name can be predetermined. Optionally, in the specific implementation process, after retrieving the k target variable names, the k target variable names are subjected to subtoken splitting processing (subtoken) to obtain multiple subtokens corresponding to the k target variable names; each subtoken is subjected to word vector (embedding) encoding to obtain the word vectors corresponding to the multiple subtokens, and the word vectors corresponding to the target variable name are formed.
[0072] For example, after inputting the first natural language description information mentioned above, two target variable names btnStatus and resultButton are retrieved from the variable definition table, and the two target variable names are divided into sub-words: btn, Status, result, Button, and then each sub-word is encoded with a word vector, and a memory unit is used to record the word vector containing the above multiple sub-words.
[0073] As described above, after obtaining the word vectors corresponding to the semantic coding matrix and the target variable name, the word vectors corresponding to the semantic coding matrix and the target variable name are input into the decoding network as the input sequence of the decoding network, so as to obtain the target code corresponding to the first natural language description information through the decoding network. Since the input sequence of the decoding network includes the word vectors corresponding to the target variable name, that is, when performing the decoding calculation, the decoding network uses the word vectors corresponding to the target variable name as auxiliary information to assist in the automatic generation of the target code corresponding to the first natural language description information. This is not only efficient, but also avoids the situation where the same variable uses different variable names, and has good accuracy.
[0074] After the server 104 generates an executable target code according to the above scheme, the target code can be sent to the application front end 101. The application front end 101 executes the target code. For example, if the target code is a code for rendering and displaying a certain text content, which contains a target variable name, then when executing the target code, the application front end 101 needs to obtain the corresponding variable value in order to display the real-time variable value. Specifically, the application front end 101 requests the real-time variable value corresponding to the target variable name from the application back end 102, and after obtaining the variable value corresponding to the target variable name from the application back end 102, the application front end 101 assigns and renders the target variable name. For example, the target variable name is discountPrice, which means the discount price. The target code is: 'Coupon has been deducted' + discountPrice + 'yuan', and assuming that the variable value obtained from the application back end is 50, the application front end 101 finally renders and displays in the menu bar: Coupon has been deducted by 50 yuan.
[0075] It should be noted that the above-mentioned variable definition table stores the second natural language description information of the variable name and the variable meaning. The variable definition table is pre-synchronized between the application front-end and the application back-end. Therefore, when the application front-end requests the variable value corresponding to the variable name, the application back-end knows the variable name.
[0076] Therefore, in the embodiment of the present invention, based on the unified definition of variable names in the variable definition table, in the code generation process, based on the first natural language description information of the function of a code, the target variable name involved is used as auxiliary information to assist in the automatic generation of the target code corresponding to the first natural language description information. Not only is the efficiency high, but also the situation of using different variable names for the same variable will not occur, and the accuracy is good. Therefore, the semantic legitimacy of code generation can be improved, and the accuracy of code generation can be improved.
[0077] In order to facilitate intuitive understanding of the input and output of the code generation model in the embodiment of the present invention. The following explanation is given by taking the target application as an e-commerce application as an example. Among them, the input of the code generation model refers to the first natural language description information of the function of the target code, and the output of the code generation model refers to the target code corresponding to the first natural language description information.
[0078] Taking the example of the application front end 101 triggering the code generation requirement for a certain code function corresponding to a certain product in the e-commerce application, after the user inputs the first natural language description information used to describe the function of the target code in the e-commerce application into the server 104, the server 104 generates a target code including a variable name for the application front end 101 and sends the target code to the application front end 101. For example, if the first natural language description information entered is: ¥XX, XX is the price after the coupon; the corresponding target code generated is: {('¥'+item.afterPromotionPrice)}; for another example, if the first natural language description information entered is: the coupon has been discounted by XX yuan, XX is the discount price; the corresponding target code generated is: {'Coupon has been discounted'+discountPrice+'yuan'}; for another example, if the first natural language description information entered is: user nickname is displayed first, and the bottom line is empty; the corresponding target code generated is: {user&&user.nick"II”}; for another example, if the first natural language description information entered is: if the product status is pre-sale, the pre-sale price is taken, otherwise the original price is taken; the corresponding target code generated is: {presale? item.preSalePrice:item.originalPrice}.
[0079] By adopting the code generation method embodiment provided in the embodiment of the present invention, the target code can be automatically generated based on the first natural language description information describing the function of the target code in the target application, which can not only greatly improve the development efficiency of code developers, but also enable non-development practitioners to develop code programs.
[0080] In the code generation scenario provided in the embodiment of the present invention, in order to better understand the architecture of the above encoding network and decoding network, Figure 3A schematic diagram of the architecture of a code generation model that optionally includes an encoding network and a decoding network is shown, such as Figure 3 As shown, the encoding network includes N encoders, and the decoding network is also composed of the same number (corresponding to the encoders) N encoders.
[0081] All encoders are structurally identical, but they do not share parameters. Each decoder can be decomposed into two sublayers. Figure 3 Each encoder shown includes: a self-attention layer (i.e., a self-attention layer) and a feed-forward neural network layer.
[0082] The first natural language description information input to the encoder will first pass through a self-attention layer, which helps the encoder pay attention to other words in the first natural language description information when encoding each word. The output of the self-attention layer is passed to the feed-forward neural network layer. When the encoding network is composed of multiple encoders, the word vector corresponding to the first natural language description information is input to the first encoder, and the output of the previous encoder is used as the input of the next encoder. The final encoder finally outputs the semantic encoding matrix of the first natural language description information).
[0083] After starting the decoding phase, the semantic encoding matrix output by the above encoding network will be used by each decoder in the decoding network in the encoding and decoding attention layers.
[0084] Still Figure 3 As shown in the figure, the above decoding network contains N decoders and a prediction output layer. Each decoder also contains a self-attention layer and a feed-forward neural network layer. In addition, there is an encoding and decoding attention layer and a memory and decoding attention layer between these two layers. Figure 3 As shown in , the input from the attention layer at the current moment is the code snippet predicted by the output layer before. The “before” refers to the code content output by the decoding network in sequence for the first natural language description information. The input of the memory and decoding attention layers includes the word vector corresponding to the target variable name and the word vector corresponding to the code snippet predicted before. The input of the encoding and decoding attention layers includes the semantic encoding matrix output by the encoding network and the first matrix and the second matrix output by the above two attention layers, as shown in Figure 3As shown in . By setting the self-attention layer, encoding and decoding attention layer, and memory and decoding attention layer in each decoder, the decoding network can simultaneously obtain the generated code snippet, the word vector corresponding to the target variable name in the memory, and the word vector corresponding to the output code snippet output by the encoding network, so as to combine this information to complete the code generation process.
[0085] Optionally, in an embodiment of the present invention, each attention layer can be implemented using a multi-head attention mechanism.
[0086] After obtaining the above semantic encoding matrix and the word vector corresponding to the target variable name, combined with Figure 3 The model structure shown in the figure can refer to the process of generating the target code through the encoding network and the decoding network. Figure 4 accomplish.
[0087] Figure 4 A flowchart of an optional code generation method provided by an embodiment of the present invention, such as Figure 4 As shown, the semantic coding matrix and the word vector corresponding to the target variable name are input into the decoding network to obtain the target code corresponding to the first natural language description information through the decoding network, which can be implemented by the following method steps:
[0088] 401. Input the word vector corresponding to the code snippet output by the prediction output layer into the self-attention layer to obtain the first matrix output by the self-attention layer.
[0089] The output code fragment is composed of the starting character corresponding to the target code to the currently acquired character.
[0090] 402. Input the word vector corresponding to the output code snippet and the word vector corresponding to the target variable name into the memory and decoding attention layer to obtain a second matrix output by the memory and decoding attention layer.
[0091] 403. The semantic coding matrix output by the coding network and the sum of the first matrix and the second matrix are input into the coding and decoding attention layer to obtain a third matrix output by the coding and decoding attention layer.
[0092] 404. Input the third matrix into the prediction output layer to obtain the next character in the output target code of the prediction output layer.
[0093] In summary, the essential function of the attention layer is to determine the weight of each input information in the input information sequence, so as to perform weighted summation processing on the input information based on the weight of each input information to obtain the corresponding output information. The process of calculating the weight mainly involves three types of matrices: query matrix, key matrix and value matrix. Once these three matrices are determined, the output of the attention layer can be obtained based on the preset calculation formula corresponding to the attention layer.
[0094] In an embodiment of the present invention, each decoder in the decoding network includes the above-mentioned three attention layers: self-attention layer, memory and decoding attention layer, encoding and decoding attention layer.
[0095] Among them, the input of the self-attention layer is the word vector corresponding to the code snippet that has been output by the prediction output layer. It can be understood that the word vector is actually a word vector sequence, which includes the word vectors corresponding to each character in the code snippet (the information output by the prediction output layer each time is called a character). Based on the product of the word vector sequence and the three weight matrices respectively, the query matrix, key matrix and value matrix corresponding to the self-attention layer can be obtained. Among them, these three weight matrices correspond to the query matrix, key matrix and value matrix respectively, and these three weight matrices are model parameters obtained after training the code generation model. The matrix finally output by the self-attention layer is called the first matrix.
[0096] The input of the memory and decoding attention layer is the word vector corresponding to the output code snippet and the word vector corresponding to the target variable name. Among them, the query matrix can be generated based on the word vector corresponding to the output code snippet, and the key matrix and value matrix can be generated based on the word vector corresponding to the target variable name. The matrix finally output by the memory and decoding attention layer is called the second matrix.
[0097] The input of the encoding and decoding attention layer is the semantic encoding matrix output by the encoding network and the sum of the first matrix and the second matrix. The query matrix is generated based on the sum, and the key matrix and the value matrix are generated based on the semantic encoding matrix. The output of the encoding and decoding attention layer is called the third matrix.
[0098] In practical applications, a feed-forward neural network layer can be connected after the encoding and decoding attention layers of each decoder.
[0099] The third matrix output by the last decoder will be input into the prediction output layer, through which the next character in the target code can be output. The prediction output layer can be composed of a linear transformation layer and a softmax layer.
[0100] In order to use the code generation model described above to complete the code generation task, the code generation model needs to be trained in advance to obtain the model parameters when trained to convergence, that is, the model is trained to convergence. Model training requires the collection of training samples. Optionally, the training samples of the encoding network and the decoding network include: sample code, natural language description information corresponding to the sample code, sample variable names contained in the sample code, and natural language description information of the variable meaning corresponding to the sample variable name.
[0101] In the specific implementation process, first, a sample code and a corpus data set of natural language description information corresponding to the sample code can be collected and organized, and the corpus data set can be used to train the parameters in the code generation model. In the process of training the code generation model, it is possible but not limited to retrieve k target variable names from the variable definition table and add them to the memory unit memory in a method similar to the code generation process described above, and the encoding network encodes the first natural language description information to obtain the semantic encoding matrix corresponding to the first natural language description information, and then uses the decoding network to decode the semantic encoding matrix and the word vector corresponding to the above target variable name to obtain the above target code corresponding to the first natural language description information, and then calculates the loss function by using the target code and the sample code in the corpus data set, and finally, trains the parameters of the code generation model by the gradient descent method.
[0102] In addition, in the process of training the code generation model, in order to obtain the word vector corresponding to the target variable name, the sample variable name and the natural language description information of the variable meaning corresponding to the sample variable name can also be added to the training data as corpus data, that is, the sample variable name contained in the sample code, and the natural language description information of the variable meaning corresponding to the above sample variable name. Then, the target variable name and the natural language description information of the variable meaning corresponding to the target variable name can be trained synchronously in the process of training the code generation model.
[0103] As an optional embodiment, in order to distinguish the code generation data and variable semantic data in the training data, a special word can be added to the front end of the input and output sequences of the two types of data to distinguish the two different types of data.
[0104] It should be noted that, in some cases, if the first natural language description information cannot retrieve a matching target variable name from the variable definition table, the word vector corresponding to the target variable name in the memory unit memory can be added to be empty. In order for the code generation model to generate a high-quality target code corresponding to the first natural description information in this case, the above-mentioned training sample can be expanded to obtain a reference training sample. For example, in the training sample, memory contains the word vector corresponding to the target variable name, and in another reference training sample, the word vector corresponding to the target variable name in memory is set to empty. By using the training sample and the corresponding reference training sample to train the code generation model, the trained code generation model enables the decoding network to generate the target code well even if it cannot refer to the word vector corresponding to the target variable name recorded in the memory. Therefore, after several rounds of training using the above training method and training data, a trained code generation model is obtained, which can be used to perform the code generation task described above.
[0105] In practical applications, the problem of code generation may be involved in many application fields, and the technical solution of the embodiment of the present invention may be used.
[0106] The following will describe in detail the code generation device of one or more embodiments of the present invention. Those skilled in the art will appreciate that these devices can be configured using commercially available hardware components through the steps taught in this solution.
[0107] Figure 5 A schematic diagram of the structure of a code generation device provided by an embodiment of the present invention, such as Figure 5 As shown, the device includes: an acquisition module 11, a determination module 12, an encoding module 13 and a decoding module 14.
[0108] The acquisition module 11 is used to obtain first natural language description information used to describe the function of the target code in the target application, and a variable definition table corresponding to the above target application, wherein the above variable definition table correspondingly stores second natural language description information of variable names and variable meanings.
[0109] The determination module 12 is used to determine the target variable name matching the first natural language description information from the variable definition table according to the matching degree between the first natural language description information and the second natural language description information.
[0110] The encoding module 13 is used to encode the first natural language description information through an encoding network to obtain a semantic encoding matrix corresponding to the first natural language description information.
[0111] The decoding module 14 is used to input the above-mentioned semantic coding matrix and the word vector corresponding to the above-mentioned target variable name into the decoding network, so as to obtain the above-mentioned target code corresponding to the above-mentioned first natural language description information through the above-mentioned decoding network, and the above-mentioned target code includes the above-mentioned target variable name.
[0112] Optionally, the above-mentioned device also includes: a sending module, used to send the above-mentioned target code to the front-end device running the above-mentioned target application, so that the above-mentioned front-end device executes the above-mentioned target code to obtain the variable value corresponding to the above-mentioned target variable name from the server corresponding to the above-mentioned target application.
[0113] Optionally, the target code includes code for rendering target interface elements in the target application.
[0114] Optionally, the above-mentioned device also includes: a word vector processing module, which is used to perform sub-word splitting processing on the above-mentioned target variable name to obtain multiple sub-words corresponding to the above-mentioned target variable name; and determine the word vectors corresponding to each of the above-mentioned multiple sub-words to form the word vector corresponding to the above-mentioned target variable name.
[0115] Optionally, the decoding network includes a self-attention layer, an encoding and decoding attention layer, a memory and decoding attention layer and a prediction output layer; the encoding module 13 is specifically used to: input the word vector corresponding to the code snippet output by the prediction output layer into the self-attention layer to obtain the first matrix output by the self-attention layer, and the code snippet is composed of the starting character corresponding to the target code to the currently acquired character; input the word vector corresponding to the code snippet output and the word vector corresponding to the target variable name into the memory and decoding attention layer to obtain the second matrix output by the memory and decoding attention layer; input the semantic encoding matrix output by the encoding network and the sum of the first matrix and the second matrix into the encoding and decoding attention layer to obtain the third matrix output by the encoding and decoding attention layer; input the third matrix into the prediction output layer to obtain the next character in the target code output by the prediction output layer.
[0116] Optionally, the determination module 12 is specifically used to: determine the edit distance between the first natural language description information and the second natural language description information as the matching degree; or, determine the first word vector corresponding to the first natural language description information, and the second word vector corresponding to the second natural language description information; determine the distance between the first word vector and the second word vector as the matching degree.
[0117] Optionally, the training samples of the encoding network and the decoding network include: sample codes, natural language description information corresponding to the sample codes, sample variable names contained in the sample codes, and natural language description information of variable meanings corresponding to the sample variable names.
[0118] Figure 5 The device shown can execute the steps provided in the aforementioned embodiments. For detailed execution process and technical effects, please refer to the description in the aforementioned embodiments, which will not be repeated here.
[0119] In one possible design, the above Figure 5 The structure of the code generating device shown can be implemented as an electronic device. Figure 6 As shown, the electronic device may include: a processor 21, a memory 22, and a communication interface 23. The memory 22 stores executable codes, and when the executable codes are executed by the processor 21, the processor 21 can at least implement the code generation method provided in the above embodiments.
[0120] In addition, an embodiment of the present invention provides a non-temporary machine-readable storage medium, on which executable code is stored. When the executable code is executed by a processor of an electronic device, the processor can at least implement the code generation method provided in the aforementioned embodiment.
[0121] The device embodiments described above are merely illustrative, wherein the network elements described as separate components may or may not be physically separated. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment. Ordinary technicians in this field can understand and implement it without creative work.
[0122] Through the description of the above embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by adding a necessary general hardware platform, and of course can also be implemented by combining hardware and software. Based on such an understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a computer product, and the present invention can be in the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.
[0123] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A code generation method, characterized in that: include: Acquire first natural language description information for describing the function of the target code in the target application, and a variable definition table corresponding to the target application, wherein the variable definition table stores second natural language description information corresponding to variable names and variable meanings; According to the matching degree between the first natural language description information and the second natural language description information, determining a target variable name matching the first natural language description information from the variable definition table; Encoding the first natural language description information through an encoding network to obtain a semantic encoding matrix corresponding to the first natural language description information; The semantic encoding matrix and the word vector corresponding to the target variable name are input into a decoding network to obtain the target code corresponding to the first natural language description information through the decoding network, wherein the target code includes the target variable name.
2. The method according to claim 1, characterized in that The method further comprises: The target code is sent to a front-end device running the target application, so that the front-end device executes the target code to obtain a variable value corresponding to the target variable name from a server corresponding to the target application.
3. The method according to claim 2, characterized in that The target code includes code for rendering a target interface element in the target application.
4. The method according to claim 1, characterized in that: The method further comprises: Performing subword splitting processing on the target variable name to obtain multiple subwords corresponding to the target variable name; Determine the word vectors corresponding to each of the multiple subwords to form a word vector corresponding to the target variable name.
5. The method according to claim 1, characterized in that The decoding network includes a self-attention layer, an encoding and decoding attention layer, a memory and decoding attention layer, and a prediction output layer; The step of inputting the semantic encoding matrix and the word vector corresponding to the target variable name into a decoding network to obtain the target code corresponding to the first natural language description information through the decoding network includes: Inputting the word vector corresponding to the code snippet output by the prediction output layer into the self-attention layer to obtain a first matrix output by the self-attention layer, wherein the code snippet is composed of the starting character corresponding to the target code to the currently acquired character; Input the word vector corresponding to the outputted code snippet and the word vector corresponding to the target variable name into the memory and decoding attention layer to obtain a second matrix outputted by the memory and decoding attention layer; Input the semantic coding matrix output by the encoding network and the sum of the first matrix and the second matrix into the encoding and decoding attention layer to obtain a third matrix output by the encoding and decoding attention layer; The third matrix is input into the prediction output layer to obtain the prediction output layer to output the next character in the target code.
6. The method according to claim 1, characterized in that The method further comprises: determining an edit distance between the first natural language description information and the second natural language description information as the matching degree; or, Determine a first word vector corresponding to the first natural language description information, and a second word vector corresponding to the second natural language description information; Determine the distance between the first word vector and the second word vector as the matching degree.
7. The method according to claim 1, characterized in that The training samples of the encoding network and the decoding network include: Sample code, natural language description information corresponding to the sample code, sample variable names contained in the sample code, and natural language description information of variable meanings corresponding to the sample variable names.
8. A code generating device, characterized in that: include: An acquisition module, used to acquire first natural language description information for describing the function of the target code in the target application, and a variable definition table corresponding to the target application, wherein the variable definition table stores second natural language description information corresponding to the variable name and the variable meaning; a determination module, configured to determine, from the variable definition table, a target variable name matching the first natural language description information according to a matching degree between the first natural language description information and the second natural language description information; An encoding module, configured to encode the first natural language description information through an encoding network to obtain a semantic encoding matrix corresponding to the first natural language description information; A decoding module is used to input the semantic encoding matrix and the word vector corresponding to the target variable name into a decoding network, so as to obtain the target code corresponding to the first natural language description information through the decoding network, wherein the target code includes the target variable name.
9. An electronic device, characterized in that: include: A memory, a processor, and a communication interface; wherein the memory stores executable code, and when the executable code is executed by the processor, the processor executes the code generation method according to any one of claims 1 to 7.
10. A non-transitory machine-readable storage medium, characterized in that: The non-transitory machine-readable storage medium stores executable code, and when the executable code is executed by a processor of an electronic device, the processor is caused to execute the code generation method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Fine-grained code automatic generation method and system based on multi-view code features
CN113342318A