Dynamic code generation method and system based on interface document

By preprocessing and semantic analysis of interface documents, optimizing code templates in combination with knowledge graphs and symbol execution technology, and using the encoder-decoder network to generate target programming language code, the code generation in the existing technology does not comply with syntax specifications and lacks. Adaptability issues achieve efficient and accurate code generation.

CN118860356BActive Publication Date: 2024-11-29NINGBO XINYI INFORMATION TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411336927.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-09-25
Publication Date
2024-11-29
Estimated Expiration
2044-09-25

AI Technical Summary

Technical Problem

Existing automatic code generation technologies cannot fully utilize the semantic information in interface documents, resulting in the generated code not complying with the target language syntax specifications and lack of adaptability.

Method used

By obtaining interface documents, preprocessing and word embedding processing, a distributed vector representation is generated, and a bidirectional long and short-term memory network is input for context adjustment. Link semantic block representations with conceptual entities in the software engineering domain knowledge graph, organize them into triple representations, build a library of code templates and optimize code templates through symbolic execution and reinforcement learning. Finally, the encoder-decoder network is used to convert natural language descriptions into abstract syntax trees of the target programming language, generating syntactical correct target code.

Benefits of technology

Improve the accuracy and adaptability of code generation, ensure that the generated code complies with the target language syntax specifications, and improves the efficiency and quality of software development.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118860356B_ABST
    Figure CN118860356B_ABST
Patent Text Reader

Abstract

The present invention provides a method and system for dynamic code generation based on an interface document, which relates to the technical field of software development, and comprises the following steps: obtaining an interface document and preprocessing it, dividing the preprocessed interface document into a plurality of semantic blocks, linking the obtained semantic block representation with a conceptual entity in a pre-constructed software engineering field knowledge graph, and obtaining a triple representation of the semantic block; modeling the structural optimization problem of a code template as a multi-objective optimization model, and obtaining a code template with an optimal structure by solving the multi-objective optimization model; based on the code template with the optimal structure, parsing a natural language description into an initial abstract syntax tree and matching it with syntax templates of different programming languages, and at the same time, using a pre-trained programming language conversion model to convert the initial abstract syntax tree into an abstract syntax tree of a target programming language, and finally generating a grammatically correct target code according to the converted abstract syntax tree.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to software development technology, and in particular to a dynamic code generation method and system based on interface documents. Background Art

[0002] At present, automatic code generation technology mainly relies on static templates and rule matching, and cannot make full use of the semantic information in the document to improve the accuracy and adaptability of code generation. When processing complex interface documents, traditional methods often face the problem that the generated code does not conform to the grammatical specifications of the target language. Therefore, it is of great significance to develop an automatic code generation method that can accurately understand interface documents, generate high-quality code, and has good adaptability. The dynamic code generation method and system based on interface documents proposed in the present invention are intended to solve the above problems and improve the efficiency and quality of software development. Summary of the invention

[0003] The embodiment of the present invention provides a method and system for dynamic code generation based on an interface document, which can solve the problems in the prior art.

[0004] According to a first aspect of the embodiments of the present invention,

[0005] A method for dynamic code generation based on an interface document is provided, comprising:

[0006] Obtaining an interface document and preprocessing it, dividing the preprocessed interface document into multiple semantic blocks, including a function signature, a parameter list, and a return value description, performing word embedding processing on each semantic block to obtain a distributed vector representation of the semantic block, inputting the distributed vector representation into a pre-built bidirectional long short-term memory network, adaptively adjusting the attention paid to different semantic blocks in combination with context information, obtaining a semantic block representation that integrates context information, linking the obtained semantic block representation with a concept entity in a pre-built software engineering domain knowledge graph, organizing the semantic block representation into a triple form based on the linked concept entity information, and obtaining a triple representation of the semantic block;

[0007] A code template library is constructed based on the triple representation of semantic blocks, candidate code templates are extracted from the code template library through an adaptive mining algorithm, the candidate code templates are evaluated and screened according to coverage, accuracy and readability, and the screened code templates are parameterized using a symbolic execution algorithm to obtain a target code template, the target code template is structurally optimized, the structural optimization problem of the code template is modeled as a multi-objective optimization model, and the code template with the optimal structure is obtained by solving the multi-objective optimization model;

[0008] Based on the optimally structured code template, the natural language description is parsed into an initial abstract syntax tree and matched with syntax templates of different programming languages. At the same time, a pre-trained programming language conversion model is used to convert the initial abstract syntax tree into an abstract syntax tree of the target programming language. The programming language conversion model adopts an encoder-decoder network to integrate the grammatical constraints of the target programming language during the decoding process. Finally, a grammatically correct target code is generated based on the converted abstract syntax tree.

[0009] In an optional embodiment,

[0010] The obtained semantic block representation is linked to the concept entities in the pre-built software engineering domain knowledge graph. The semantic block representation is organized into triples based on the linked concept entity information. The obtained triple representation of the semantic block includes:

[0011] Calculate the similarity between each semantic block representation and the conceptual entities in the pre-built software engineering domain knowledge graph, filter out the knowledge graph entities corresponding to each semantic block representation, and generate preliminary triples based on the connection relationship between the semantic block representation and the filtered knowledge graph entities;

[0012] A graph structure is constructed based on the semantic block representation and the screened knowledge graph entities. The semantic blocks and knowledge graph entities are used as nodes in the graph. The connection relationship between the semantic blocks and the knowledge graph entities, as well as the relationship between entities in the knowledge graph, are used as edges connecting the nodes. Through multi-layer graph convolution operations, the feature representation of each node is aggregated with the features of its neighboring nodes, and the representation of the current node is gradually updated to obtain a node representation that integrates the knowledge graph entities and the relationships between entities. A multi-hop propagation mechanism is introduced to enhance the semantic information of the nodes. At the same time, an attention-only mechanism is used to dynamically adjust the attention of each node to its neighboring nodes, and finally a target node representation is obtained. Based on the target node representation, an initial triple representation is obtained, which includes the semantic block, the knowledge graph entity corresponding to the semantic block, and the relationship;

[0013] Based on the initial triple representation, valid triples and invalid triples are constructed as inputs for self-supervised learning, and a two-layer encoder is constructed, wherein the first-layer encoder is used to vectorize the semantic blocks and knowledge graph entities to obtain high-dimensional vector representations of the semantic blocks and knowledge graph entities, and the second-layer encoder is used to introduce a contrastive learning mechanism on the basis of the first-layer encoder, and the semantic distance of the valid triples is minimized and the semantic distance of the invalid triples is maximized by optimizing the contrastive learning objective function, and finally the target triple representation of the semantic block is generated.

[0014] In an optional embodiment,

[0015] The calculation formula of contrast learning objective function is as follows:

[0016] ;

[0017] Where L represents the objective function, E represents the valid triple set, s represents the semantic block, e represents the entity, r represents the relationship, Sim() represents the similarity function, λ represents the weight coefficient of the negative sample contrast loss, N(s) represents the negative sample set related to the semantic block s, e′ represents the entity in the negative sample, r′ represents the relationship in the negative sample, δ represents the threshold of the negative sample similarity, γ represents the regularization term coefficient, S represents the triple set containing contextual information, w s,e,r Represents the parameter vector associated with the triple (s, e, r).

[0018] In an optional embodiment,

[0019] The symbolic execution algorithm is used to parameterize the selected code templates, and the target code templates include:

[0020] Taking each decision point in the symbolic execution process as a state and selecting an execution path as an action, constructing a reinforcement learning mechanism for optimizing the symbolic execution path selection based on the state and the action, adjusting the symbolic execution path exploration strategy in real time according to the reinforcement learning mechanism, and generating an adjusted path exploration strategy;

[0021] In the initial path exploration process of symbolic execution, path selection is performed according to the adjusted path exploration strategy, and path information, path execution results, and path execution performance indicators of the selected path are collected and summarized to generate a training data set for the reinforcement learning model. The training data set is used to perform training through a preset reinforcement learning algorithm to obtain a reinforcement learning model for determining the optimal decision action for each decision point;

[0022] Using natural language processing methods to extract semantic features from unstructured information contained in the code, converting the extracted semantic features into symbolic representations that are consistent with the structured symbol types in the code, fusing the symbolic representations with the structured symbols in the code to generate a fused symbolic representation for symbolic execution, and in the reasoning and path selection process of symbolic execution, optimizing the path selection of symbolic execution based on the fused symbolic representation to obtain a set of feasible paths;

[0023] A corresponding parameterized model is constructed for each feasible path in the feasible path set, and an initial parameter value is set for each feasible path based on the constructed parameterized model. After each symbolic execution and selecting the optimal path using the reinforcement learning model, the reinforcement learning agent calculates the path execution reward value through a preset reward function according to the execution feedback information of the selected path, and adjusts the initial parameter value of the parameterized model corresponding to the selected path according to the path execution reward value to obtain the optimal parameter value of the selected path;

[0024] Each feasible path and the corresponding optimal parameter value are input into the parameterized model, each feasible path is parameterized, a parameterized code fragment is generated, and the parameterized code fragments are combined to obtain a target code template.

[0025] In an optional embodiment,

[0026] According to the execution feedback information of the selected path, the formula for calculating the path execution reward value through the preset reward function is as follows:

[0027] ;

[0028] Among them, R t represents the path execution reward value, θ1 represents the weight coefficient of the immediate reward, R i (t) represents the immediate reward for path selection at time t, θ2 represents the weight coefficient of path performance feedback, η represents the parameter that controls the smoothness of the path performance feedback term, P t represents the parameter value of the current path at time t, P z represents the target parameter value, θ3 represents the weight coefficient of the parameter adjustment reward item, and |ΔP| represents the absolute value of the path parameter adjustment amount.

[0029] In an optional embodiment,

[0030] The natural language description is parsed into an initial abstract syntax tree and matched with the syntax templates of different programming languages. At the same time, the initial abstract syntax tree is converted into an abstract syntax tree of the target programming language using a pre-trained programming language conversion model. The programming language conversion model uses an encoder-decoder network to integrate the syntax constraints of the target programming language during the decoding process. Finally, the grammatically correct target code is generated according to the converted abstract syntax tree, including:

[0031] By abstracting and summarizing the grammatical rules of different programming languages, a universal abstract syntax tree node type system containing the common grammatical structure of programming languages ​​is designed. Based on the constructed universal abstract syntax tree node type system, the input natural language description is parsed into a preliminary abstract syntax tree that is independent of the programming language. During the parsing process, the pattern matching algorithm is used to identify the words and phrases in the natural language that correspond to the basic structure of the programming language. Combined with the semantic components described by the universal semantic role labels, the identified words, phrases and semantic components are converted into corresponding abstract syntax tree nodes.

[0032] According to the predefined target language grammar template, a unified algorithm is used to compare the types, structures and attributes of the preliminary abstract syntax tree and the grammar template, match the target grammar template, extract the corresponding subtree of the preliminary abstract syntax tree and fill in the parameter position of the target grammar template, and obtain an intermediate abstract syntax tree that conforms to the grammatical structure of the target language;

[0033] The self-attention mechanism is used to model the long-distance dependencies between abstract syntax tree nodes. The node sequence of the intermediate abstract syntax tree is input into the pre-trained programming language conversion model. The candidate node sequence of the target language abstract syntax tree is generated through multi-head attention mechanism and feedforward neural network decoding. The grammatical constraints of the target language are incorporated into the decoding process to convert the intermediate abstract syntax tree into the target language abstract syntax tree.

[0034] Define an abstract syntax tree traversal, recursively visit each node of the target language abstract syntax tree, convert each visited node into a source code snippet of the target language, combine the context and heuristic rules to automatically generate names that conform to the naming conventions of the target language for the identifiers in the source code snippet of the target language, and use a templated method to generate code comments based on the structure and type information of the target language abstract syntax tree. At the same time, apply dead code elimination technology to remove code that will not be executed, and finally output syntactically correct target code.

[0035] In an optional embodiment,

[0036] The node sequence of the intermediate abstract syntax tree is input into the pre-trained programming language conversion model, and the candidate node sequence of the target language abstract syntax tree is generated through multi-head attention mechanism and feedforward neural network decoding. The grammatical constraints of the target language are incorporated into the decoding process. The conversion of the intermediate abstract syntax tree into the target language abstract syntax tree includes:

[0037] Formalizing the grammatical rules of the target language, constructing the vocabulary, grammar tree template and grammar constraint pattern of the target language, representing the grammar rules in the form of an abstract syntax tree, generating input features of a grammar perception module, and constructing a grammar rule scoring model, wherein the grammar rule scoring model receives a node sequence of an intermediate abstract syntax tree as input and outputs a grammar legality score;

[0038] The grammar-aware module is integrated into the programming language conversion model, the target language code generation task and the target language grammar structure prediction task are constructed as a joint objective of multi-task learning, and a joint loss function is constructed by combining the code generation loss and the grammar structure prediction loss. The programming language conversion model integrated with the grammar-aware module is pre-trained using the constructed joint loss function on a parallel corpus containing the grammar information of the target language code, and a trained programming language conversion model is obtained.

[0039] The node sequence of the extracted intermediate abstract syntax tree is input into the trained programming language conversion model, and the candidate node sequence of the target language abstract syntax tree is generated through multi-head attention mechanism and feedforward neural network decoding. During the decoding process, the generated candidate node sequence is checked in real time using the grammar rule scoring model, the grammar legitimacy score is calculated, and the generation strategy of the candidate node sequence is dynamically adjusted according to the scoring result;

[0040] According to the generated candidate node sequence, multiple candidate target language abstract syntax tree structures are constructed. A rule-based syntax filtering mechanism is used to check whether the structure and nodes of each candidate target language abstract syntax tree conform to the grammatical requirements of the target language. The candidate target language abstract syntax trees that do not conform to the grammatical rules are eliminated. The grammatical correctness of the remaining candidate target language abstract syntax trees is evaluated using a grammatical rule scoring model. The target language abstract syntax tree with the highest grammatical legitimacy score is selected as the target language abstract syntax tree.

[0041] According to a second aspect of the embodiments of the present invention,

[0042] A dynamic code generation system based on an interface document is provided, comprising:

[0043] The first unit is used to obtain and preprocess the interface document, divide the preprocessed interface document into multiple semantic blocks, including function signatures, parameter lists and return value descriptions, perform word embedding processing on each semantic block, obtain a distributed vector representation of the semantic block, input the distributed vector representation into a pre-built bidirectional long short-term memory network, adaptively adjust the attention to different semantic blocks in combination with context information, obtain a semantic block representation that integrates context information, link the obtained semantic block representation with a concept entity in a pre-built software engineering domain knowledge graph, organize the semantic block representation into a triple form based on the linked concept entity information, and obtain a triple representation of the semantic block;

[0044] The second unit is used to construct a code template library based on the triple representation of semantic blocks, extract candidate code templates from the code template library through an adaptive mining algorithm, evaluate and screen the candidate code templates based on coverage, accuracy and readability, and parameterize the screened code templates using a symbolic execution algorithm to obtain a target code template, perform structural optimization on the target code template, model the structural optimization problem of the code template as a multi-objective optimization model, and obtain the code template with the optimal structure by solving the multi-objective optimization model;

[0045] The third unit is used to parse the natural language description into an initial abstract syntax tree based on the optimal structure code template and match it with the syntax templates of different programming languages. At the same time, the initial abstract syntax tree is converted into an abstract syntax tree of the target programming language using a pre-trained programming language conversion model. The programming language conversion model adopts an encoder-decoder network to integrate the syntax constraints of the target programming language during the decoding process, and finally generates grammatically correct target code based on the converted abstract syntax tree.

[0046] In this embodiment, by obtaining the interface document and dividing it into multiple semantic blocks, the system can automatically parse different parts of the document, such as function signatures, parameter lists, and return value descriptions. This processing allows the structured information of the document to be effectively extracted and further used. The domain knowledge in the knowledge graph is used for reasoning and enhanced understanding, thereby improving the accuracy and intelligence level of the processing. Through the triple representation of the semantic block, the code template library is constructed and candidate code templates are extracted from it, and the system automatically completes the screening, evaluation and optimization of the code template. The symbolic execution algorithm is used to parameterize the code template and generate the optimal structure code template by solving the multi-objective optimization model. This automated code template generation and optimization process greatly improves the efficiency of code development and ensures that the generated code template has high coverage, accuracy and readability. Based on the optimal structure code template, the system can parse the natural language description into an initial abstract syntax tree and match it with the syntax templates of different programming languages. Through the pre-trained programming language conversion model, the system can automatically convert the initial abstract syntax tree into a syntax tree of the target programming language and generate correct code that conforms to the syntax of the target language. This enables the system to support multiple programming languages, expanding its applicability and flexibility. The entire process automatically mines, optimizes and generates code through adaptive mining algorithms, symbolic execution and reinforcement learning models. The generated code templates are parameterized and multi-objective optimized to ensure that their structure is optimal, with high performance and maintainability. This technology can help developers reduce the time and errors of manually writing code and improve the quality of code. BRIEF DESCRIPTION OF THE DRAWINGS

[0047] Figure 1 A schematic diagram of a flow chart of a method for dynamic code generation based on an interface document according to an embodiment of the present invention;

[0048] Figure 2 The figure is a schematic diagram of the structure of a dynamic code generation system based on an interface document according to an embodiment of the present invention. DETAILED DESCRIPTION

[0049] 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 only 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.

[0050] The technical solution of the present invention is described in detail with specific embodiments below. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described in detail in some embodiments.

[0051] Figure 1 FIG. 1 is a flow chart of a method for generating dynamic code based on an interface document according to an embodiment of the present invention. Figure 1 As shown, the method includes:

[0052] S101. Obtain an interface document and preprocess it, divide the preprocessed interface document into multiple semantic blocks, including function signatures, parameter lists, and return value descriptions, perform word embedding processing on each semantic block, obtain a distributed vector representation of the semantic block, input the distributed vector representation into a pre-built bidirectional long short-term memory network, adaptively adjust the focus on different semantic blocks in combination with context information, obtain a semantic block representation that integrates context information, link the obtained semantic block representation with a conceptual entity in a pre-built software engineering domain knowledge graph, organize the semantic block representation into a triple form based on the linked conceptual entity information, and obtain a triple representation of the semantic block.

[0053] Among them, the interface document usually contains description information in natural language, such as function signature, parameter list, return value, etc. In the preprocessing stage, it is necessary to clean up the redundant information in the document and divide it into multiple semantic blocks, which represent different functions and structural elements in the document. Specifically, the function signature represents the name of the function and the type information of its input and output, the parameter list describes the input data format of the function, and the return value describes the output of the function. The purpose of separating this information is to enable more efficient extraction and operation in subsequent steps.

[0054] Next, each semantic block is processed by word embedding. Word embedding converts words or phrases in natural language into vector representations of fixed dimensions, so that words with similar semantics have closer representations in the vector space. This step is completed through pre-trained word embedding models (such as Word2Vec, GloVe, etc.), ensuring that important terms and expressions in the document can be represented in vector form, retaining their semantic similarities and differences.

[0055] The processed semantic block vector is input into the Bidirectional Long Short-Term Memory (BiLSTM) network, which is a neural network that can effectively model long-distance dependencies in sequence data. BiLSTM can adaptively adjust the focus on different semantic blocks based on the context information of the semantic block. The bidirectional feature means that it can process the semantic block sequence from both the forward and backward directions at the same time, which enables the model to better understand the overall context of the document and generate a more accurate fusion representation of the semantic block.

[0056] After obtaining the semantic block representations that incorporate contextual information, these representations need to be linked to the conceptual entities in the knowledge graph in the software engineering domain. The knowledge graph is a structured representation that can contain important concepts and entities in the domain and their relationships. By linking the semantic blocks extracted from the document with the entities in the knowledge graph, the system can associate abstract natural language content with specific programming concepts and operations. The linking process can use pre-built dictionaries or match the corresponding conceptual entities through entity recognition technology.

[0057] Based on the linked conceptual entity information, each semantic block is represented and organized into a triple. Triples are usually represented in the form of "subject-predicate-object". In this technical solution, the triple of each semantic block can express some structural information of the function, such as "function name-call-parameter list" or "parameter-type-return value".

[0058] In an optional embodiment,

[0059] The obtained semantic block representation is linked to the concept entities in the pre-built software engineering domain knowledge graph. The semantic block representation is organized into triples based on the linked concept entity information. The obtained triple representation of the semantic block includes:

[0060] Calculate the similarity between each semantic block representation and the conceptual entities in the pre-built software engineering domain knowledge graph, filter out the knowledge graph entities corresponding to each semantic block representation, and generate preliminary triples based on the connection relationship between the semantic block representation and the filtered knowledge graph entities;

[0061] A graph structure is constructed based on the semantic block representation and the screened knowledge graph entities. The semantic blocks and knowledge graph entities are used as nodes in the graph. The connection relationship between the semantic blocks and the knowledge graph entities, as well as the relationship between entities in the knowledge graph, are used as edges connecting the nodes. Through multi-layer graph convolution operations, the feature representation of each node is aggregated with the features of its neighboring nodes, and the representation of the current node is gradually updated to obtain a node representation that integrates the knowledge graph entities and the relationships between entities. A multi-hop propagation mechanism is introduced to enhance the semantic information of the nodes. At the same time, an attention-only mechanism is used to dynamically adjust the attention of each node to its neighboring nodes, and finally a target node representation is obtained. Based on the target node representation, an initial triple representation is obtained, which includes the semantic block, the knowledge graph entity corresponding to the semantic block, and the relationship;

[0062] Based on the initial triple representation, valid triples and invalid triples are constructed as inputs for self-supervised learning, and a two-layer encoder is constructed, wherein the first-layer encoder is used to vectorize the semantic blocks and knowledge graph entities to obtain high-dimensional vector representations of the semantic blocks and knowledge graph entities, and the second-layer encoder is used to introduce a contrastive learning mechanism on the basis of the first-layer encoder, and the semantic distance of the valid triples is minimized and the semantic distance of the invalid triples is maximized by optimizing the contrastive learning objective function, and finally the target triple representation of the semantic block is generated.

[0063] Exemplarily, first, after obtaining the semantic block representation, each semantic block needs to be associated with a pre-built knowledge graph. The knowledge graph is structured data used to represent domain concepts and their relationships. In this step, the similarity between each semantic block representation and the conceptual entities in the knowledge graph is calculated. The "similarity" here can be understood as the degree of semantic association between the semantic block and the knowledge graph entity. In this way, we can filter out the knowledge graph entities that are most relevant to each semantic block. Then, each semantic block is connected to its corresponding knowledge graph entity to generate preliminary triples, which are composed of "semantic block, corresponding knowledge graph entity, and the relationship between the two."

[0064] Next, based on these preliminary triples, we start to build the graph structure. The nodes in the graph consist of two parts: semantic blocks and corresponding knowledge graph entities. The edges between nodes are composed of the connection relationship between semantic blocks and knowledge graph entities. In addition, there are inherent relationship connections between entities in the knowledge graph, which are also considered as edges in the graph. The purpose of building a graph is to further aggregate features through graph convolution operations. Graph convolution is an operation that can aggregate the features of a node and its neighboring nodes. In the multi-layer graph convolution process, the features of each node are not only updated with its own features, but also aggregated with the features of neighboring nodes to improve the richness of the representation.

[0065] A multi-hop propagation mechanism is introduced in the graph convolution operation. This mechanism means that features can be transmitted not only between directly adjacent nodes, but also between multiple connected nodes. Through this multi-hop mechanism, each node can capture the semantic information of more distant nodes, thereby enhancing the expressiveness of the overall graph. At the same time, the self-attention mechanism is used to dynamically adjust the relationship between nodes. The self-attention mechanism is a method for adjusting the importance of different nodes in graph convolution. When aggregating neighbor node features, each node will dynamically adjust the degree of attention to its neighbor nodes according to the self-attention mechanism to ensure that more relevant node features can be given priority. In this way, each node will eventually get a more expressive representation, which is called the target node representation here.

[0066] Based on these target node representations, the initial triples are updated to form the final triple representation, which contains the semantic block, the corresponding knowledge graph entity, and the relationship between the two. For example, if the semantic block describes an operation of "user login", and the concept of "user identity verification" exists in the knowledge graph entity, the association relationship between the semantic block and the entity can be determined through the triple representation, and the triple of "user login-execution-user identity verification" can be generated.

[0067] After obtaining the preliminary triples, the system constructs valid and invalid triples. Valid triples represent semantically accurate matches between semantic chunks and knowledge graph entities, while invalid triples represent incorrect or irrelevant matches. These triples are used as input for self-supervised learning. Self-supervised learning is a machine learning method that uses the structure of the data itself for training. In this process, semantic chunks and knowledge graph entities are first vectorized. Vectorized representation can be understood as converting text or concepts into high-dimensional vectors that can better express semantic information. In the first layer of the encoder, semantic chunks and knowledge graph entities are represented as high-dimensional vectors, which are convenient for comparison and optimization in subsequent operations.

[0068] In the second-layer encoder, a contrastive learning mechanism is introduced. Contrastive learning is a learning method that optimizes the model by comparing the similarities and differences between samples. By optimizing the objective function of contrastive learning, the model minimizes the semantic distance between valid triplets and maximizes the semantic distance between invalid triplets. Through this process, the model can learn how to more accurately distinguish the relationship between valid and invalid semantic blocks and knowledge graph entities, thereby generating a more accurate representation of the target triple.

[0069] In an optional embodiment,

[0070] The calculation formula of contrast learning objective function is as follows:

[0071] ;

[0072] Where L represents the objective function, E represents the valid triple set, s represents the semantic block, e represents the entity, r represents the relationship, Sim() represents the similarity function, λ represents the weight coefficient of the negative sample contrast loss, N(s) represents the negative sample set related to the semantic block s, e′ represents the entity in the negative sample, r′ represents the relationship in the negative sample, δ represents the threshold of the negative sample similarity, γ represents the regularization term coefficient, S represents the triple set containing contextual information, w s,e,r Represents the parameter vector associated with the triple (s, e, r).

[0073] In this embodiment, by calculating the similarity between the semantic block and the knowledge graph entity, the natural language description is automatically and effectively associated with the domain knowledge; through the graph convolution and self-attention mechanism, the model can gradually integrate the features of neighboring nodes and capture more semantic information, thereby improving the ability to understand complex documents; through self-supervised learning and contrastive learning mechanisms, the matching relationship between the semantic block and the knowledge graph entity is optimized to ensure that the generated triples are accurate and effective, thereby improving the overall quality of code generation or knowledge extraction tasks.

[0074] S102. Construct a code template library based on the triple representation of semantic blocks, extract candidate code templates from the code template library through an adaptive mining algorithm, evaluate and screen the candidate code templates based on coverage, accuracy and readability, and parameterize the screened code templates using a symbolic execution algorithm to obtain a target code template, perform structural optimization on the target code template, model the structural optimization problem of the code template as a multi-objective optimization model, and obtain the code template with the optimal structure by solving the multi-objective optimization model.

[0075] Among them, the adaptive mining algorithm is an intelligent search algorithm that can find the best matching code template in the template library according to the needs of the current semantic block. The key to this process is to dynamically adjust the search strategy according to different contexts and needs to ensure that the selected template is highly relevant to the current task requirements. The mined candidate code templates are some preliminary code snippets. Although they may meet most semantic requirements, they are not the final version and still need further screening and optimization.

[0076] To ensure the quality of the selected candidate code templates, the system will evaluate and screen them based on three criteria: coverage, precision, and readability. Coverage refers to whether the template covers all aspects of user needs. It measures whether the code template can fully meet all requirements of the current semantic block. Precision refers to the accuracy of the code template in actual application and whether the template can correctly solve the specified problem. Finally, readability refers to whether the code template is easy to understand and maintain. High-quality code templates not only need to be functionally correct, but also have a good code style and a clear logical structure to facilitate subsequent development and expansion.

[0077] After selecting the candidate code templates that meet the criteria, the system will then use the symbolic execution algorithm to parameterize the target code template. The symbolic execution algorithm is a program analysis technique that is used to check the code logic and generate a common input parameter representation. Through symbolic execution, the variables and constants in the code will be converted into parameterized form, making the code template more versatile and flexible. The parameterized code template is no longer limited to a specific scenario, but can be adaptively adjusted in multiple different scenarios.

[0078] After parameterization, the system needs to optimize the structure of the target code template. Here, the structural optimization problem of the code template is modeled as a multi-objective optimization model. Multi-objective optimization is a technique for optimizing multiple competing objectives at the same time. In the optimization of code templates, multiple different optimization objectives may be involved, such as code execution efficiency, memory usage, scalability, etc. The system will take these different optimization objectives into consideration and build them into an overall optimization problem.

[0079] Next, by solving the multi-objective optimization model, the system can obtain the optimally structured code template. The solution process usually involves evaluating and selecting multiple solutions, and finally selecting the optimal solution. The so-called "optimal structure" refers to a code template that best balances all optimization goals. This template not only achieves the best performance, but also achieves optimization effects in code structure and maintainability.

[0080] In an optional embodiment,

[0081] The symbolic execution algorithm is used to parameterize the selected code templates, and the target code templates include:

[0082] Taking each decision point in the symbolic execution process as a state and selecting an execution path as an action, constructing a reinforcement learning mechanism for optimizing the symbolic execution path selection based on the state and the action, adjusting the symbolic execution path exploration strategy in real time according to the reinforcement learning mechanism, and generating an adjusted path exploration strategy;

[0083] In the initial path exploration process of symbolic execution, path selection is performed according to the adjusted path exploration strategy, and path information, path execution results, and path execution performance indicators of the selected path are collected and summarized to generate a training data set for the reinforcement learning model. The training data set is used to perform training through a preset reinforcement learning algorithm to obtain a reinforcement learning model for determining the optimal decision action for each decision point;

[0084] Using natural language processing methods to extract semantic features from unstructured information contained in the code, converting the extracted semantic features into symbolic representations that are consistent with the structured symbol types in the code, fusing the symbolic representations with the structured symbols in the code to generate a fused symbolic representation for symbolic execution, and in the reasoning and path selection process of symbolic execution, optimizing the path selection of symbolic execution based on the fused symbolic representation to obtain a set of feasible paths;

[0085] A corresponding parameterized model is constructed for each feasible path in the feasible path set, and an initial parameter value is set for each feasible path based on the constructed parameterized model. After each symbolic execution and selecting the optimal path using the reinforcement learning model, the reinforcement learning agent calculates the path execution reward value through a preset reward function according to the execution feedback information of the selected path, and adjusts the initial parameter value of the parameterized model corresponding to the selected path according to the path execution reward value to obtain the optimal parameter value of the selected path;

[0086] Each feasible path and the corresponding optimal parameter value are input into the parameterized model, each feasible path is parameterized, a parameterized code fragment is generated, and the parameterized code fragments are combined to obtain a target code template.

[0087] For example, first, each decision point in the symbolic execution process is taken as a state, and each execution path is selected as an action. Symbolic execution is a program analysis technique that can represent variables, paths, and states in a program as symbols and check the behavior of the program by exploring all possible execution paths. In this step, every time the symbolic execution encounters a decision point (such as a branch condition or a loop condition), an execution path needs to be selected, and this selection process is abstracted as an action. Under this state-action relationship, a reinforcement learning mechanism is constructed to optimize the symbolic execution path selection. Reinforcement learning is a machine learning method based on feedback and rewards, which can dynamically adjust the strategy according to the feedback of the environment during the exploration process. In symbolic execution, the reinforcement learning mechanism will gradually optimize the path selection strategy by evaluating the exploration effect of the path based on the state (decision point) and action (selection path).

[0088] Next, the initial path exploration is performed based on the adjusted path exploration strategy. In this process, the system will gradually select the path for symbolic execution based on the guidance of the reinforcement learning mechanism. After each path selection, the system will collect relevant information, including the path information of the selected path (such as the structure of the path node), the results of the path execution (such as whether the expected goal is achieved), and the execution performance indicators of the path (such as the execution time and resource consumption). This information will be summarized to generate a training data set for the reinforcement learning model.

[0089] Using these training data sets, the system trains the model through a preset reinforcement learning algorithm, with the goal of obtaining a reinforcement learning model that can determine the optimal decision action at each decision point. After training, the reinforcement learning model can dynamically select the optimal path based on the current state, further optimizing the exploration process of symbolic execution.

[0090] In symbolic execution, it is necessary not only to process the structured symbols of the program, but also to analyze the unstructured information contained in the code (such as comments, variable naming, etc.). In this step, the system uses natural language processing technology to extract semantic features from unstructured information. For example, by analyzing code comments, the system can extract the semantic information and convert these semantic features into symbolic representations consistent with the structured symbol types in the code. Symbolic representation is an abstract mathematical or logical expression used to represent different variables, operations or states in the program. These symbolic representations will be fused with the structured symbols in the code to generate a fused symbolic representation. In the reasoning and path selection process of symbolic execution, the system will optimize the path selection based on this fused symbolic representation. This allows symbolic execution to not only focus on the logical structure of the program, but also use semantic information to enhance the intelligence of path selection, thereby obtaining a better set of feasible paths.

[0091] Next, for each path in the set of feasible paths, the system will build a corresponding parameterized model. A parameterized model is a model that represents adjustable parameters during program execution, which can help make the code generation process more flexible. In this step, the system sets initial parameter values ​​for each feasible path and selects the optimal path according to the reinforcement learning model during symbolic execution. The reinforcement learning agent calculates the execution reward value of each path through a preset reward function based on the feedback information of the path execution (such as execution effect and efficiency). The reward value is a quantitative indicator used in reinforcement learning to measure the effect of an action (i.e., path selection). Based on the reward value of each path, the system dynamically adjusts the initial parameter values ​​in the parameterized model until the optimal parameter value is found.

[0092] Finally, the system will input each feasible path and its optimal parameter values ​​into the parameterized model to generate parameterized code snippets. These code snippets not only contain logically correct program execution paths, but also contain optimized parameter settings to ensure the execution efficiency and effectiveness of the code snippets. Then, the system will combine these parameterized code snippets to obtain the final target code template. This target code template can not only correctly perform the expected functions, but also has an optimized structure, so that its performance and readability have reached a high level.

[0093] In an optional embodiment,

[0094] According to the execution feedback information of the selected path, the formula for calculating the path execution reward value through the preset reward function is as follows:

[0095] ;

[0096] Among them, R t represents the path execution reward value, θ1 represents the weight coefficient of the immediate reward, R i (t) represents the immediate reward for path selection at time t, θ2 represents the weight coefficient of path performance feedback, η represents the parameter that controls the smoothness of the path performance feedback term, P t represents the parameter value of the current path at time t, P z represents the target parameter value, θ3 represents the weight coefficient of the parameter adjustment reward item, and |ΔP| represents the absolute value of the path parameter adjustment amount.

[0097] In this embodiment, through the reinforcement learning mechanism, the system can dynamically adjust the path selection strategy to avoid wasting resources on a large number of invalid paths, thereby speeding up the code generation. Through the parameterization of symbolic execution, the code template not only adapts to a variety of execution environments, but also improves its execution efficiency by optimizing path selection and parameter adjustment. The intelligence of symbolic execution is enhanced by the fusion of semantic features and structured symbols, so that the generated code template not only meets the logical requirements, but also makes full use of the semantic information in the code, improving the readability and maintainability of the code. The introduction of the reinforcement learning mechanism makes the symbolic execution path selection process more intelligent, can optimize path exploration in real time, and further improves the level of intelligence in the code generation process.

[0098] S103. Based on the optimally structured code template, the natural language description is parsed into an initial abstract syntax tree and matched with syntax templates of different programming languages. At the same time, a pre-trained programming language conversion model is used to convert the initial abstract syntax tree into an abstract syntax tree of the target programming language. The programming language conversion model uses an encoder-decoder network to integrate the syntax constraints of the target programming language during the decoding process. Finally, a grammatically correct target code is generated based on the converted abstract syntax tree.

[0099] Natural language descriptions are usually texts used by humans to express program functional requirements, such as "calculate the sum of two numbers". In order to convert this natural language description into a form that can be processed by the machine, the system uses natural language processing technology to parse the text into an abstract syntax tree (AST). AST is a tree structure used to represent the grammatical structure of the code, where each node represents a grammatical element in the source code (such as a variable, operator, function call, etc.). The parsing process includes natural language processing steps such as word segmentation, part-of-speech tagging, and semantic analysis to extract key information corresponding to the code structure and construct the initial abstract syntax tree through this information.

[0100] Next, the system will match the initial abstract syntax tree with the syntax templates of different programming languages. The syntax template of a programming language is a formalized syntax rule that describes the code structure of a certain programming language. Although the syntax of different programming languages ​​is different, they can all be represented as an abstract syntax tree structure. In this step, the system finds the equivalent syntax structure in the target language by matching the nodes of the initial abstract syntax tree with the syntax template of the target programming language. For example, if the node in the initial abstract syntax tree represents "addition operation", the system will find the corresponding addition operation syntax in the language based on the syntax rules of the target language.

[0101] On this basis, the system will use the pre-trained programming language conversion model to convert the initial abstract syntax tree into the abstract syntax tree of the target programming language. The programming language conversion model is a deep learning-based model that usually adopts an encoder-decoder network architecture. The encoder part is responsible for encoding the node information in the initial abstract syntax tree into high-dimensional vector representations, which contain the structure and semantic information of the syntax nodes. The decoder part gradually generates the abstract syntax tree of the target programming language based on these vector representations. In this process, the decoder not only needs to generate a grammatically correct node sequence, but also needs to ensure that the generated tree structure conforms to the grammatical constraints of the target programming language.

[0102] In order to ensure that the generated abstract syntax tree conforms to the grammatical rules of the target programming language, the programming language conversion model dynamically integrates the grammatical constraints of the target language during the decoding process. Syntactic constraints are a series of rules defined in the target programming language that specify which grammatical structures are legal and which are illegal. By introducing these constraints in real time during decoding, the model can avoid generating code structures that do not conform to the grammar. For example, in some languages, the function definition needs to declare the return value type first. During the decoding process, the model will prioritize the generation of return value type nodes based on the grammatical constraints to ensure the legality of the code.

[0103] Finally, based on the converted abstract syntax tree, the system generates grammatically correct target code. The abstract syntax tree is an intermediate representation of programming language code. The system generates the source code of the target programming language by traversing the nodes in the abstract syntax tree in order and converting them into text form. In this step, the system adjusts the format, punctuation, indentation and other details of the code according to the grammatical rules of the target programming language to ensure that the generated code is not only logically correct, but also formally conforms to the programming specifications of the target language.

[0104] In an optional embodiment,

[0105] The natural language description is parsed into an initial abstract syntax tree and matched with the syntax templates of different programming languages. At the same time, the initial abstract syntax tree is converted into an abstract syntax tree of the target programming language using a pre-trained programming language conversion model. The programming language conversion model uses an encoder-decoder network to integrate the syntax constraints of the target programming language during the decoding process. Finally, the grammatically correct target code is generated according to the converted abstract syntax tree, including:

[0106] By abstracting and summarizing the grammatical rules of different programming languages, a universal abstract syntax tree node type system containing the common grammatical structure of programming languages ​​is designed. Based on the constructed universal abstract syntax tree node type system, the input natural language description is parsed into a preliminary abstract syntax tree that is independent of the programming language. During the parsing process, the pattern matching algorithm is used to identify the words and phrases in the natural language that correspond to the basic structure of the programming language. Combined with the semantic components described by the universal semantic role labels, the identified words, phrases and semantic components are converted into corresponding abstract syntax tree nodes.

[0107] According to the predefined target language grammar template, a unified algorithm is used to compare the types, structures and attributes of the preliminary abstract syntax tree and the grammar template, match the target grammar template, extract the corresponding subtree of the preliminary abstract syntax tree and fill in the parameter position of the target grammar template, and obtain an intermediate abstract syntax tree that conforms to the grammatical structure of the target language;

[0108] The self-attention mechanism is used to model the long-distance dependencies between abstract syntax tree nodes. The node sequence of the intermediate abstract syntax tree is input into the pre-trained programming language conversion model. The candidate node sequence of the target language abstract syntax tree is generated through multi-head attention mechanism and feedforward neural network decoding. The grammatical constraints of the target language are incorporated into the decoding process to convert the intermediate abstract syntax tree into the target language abstract syntax tree.

[0109] Define an abstract syntax tree traversal, recursively visit each node of the target language abstract syntax tree, convert each visited node into a source code snippet of the target language, combine the context and heuristic rules to automatically generate names that conform to the naming conventions of the target language for the identifiers in the source code snippet of the target language, and use a templated method to generate code comments based on the structure and type information of the target language abstract syntax tree. At the same time, apply dead code elimination technology to remove code that will not be executed, and finally output syntactically correct target code.

[0110] For example, first, the system needs to abstract and summarize the grammatical rules of different programming languages ​​to design a universal abstract syntax tree node type system that contains common grammatical structures of various programming languages. This system aims to abstract the basic structures in programming languages ​​(such as variable declarations, conditional statements, loop structures, etc.), so as to parse natural language descriptions into preliminary abstract syntax trees that are independent of specific programming languages. Each node in the preliminary abstract syntax tree corresponds to a grammatical component, such as an operator, function, or control flow structure.

[0111] In the process of parsing natural language descriptions, the system uses pattern matching algorithms to identify words and phrases in natural language that correspond to the basic structures of programming languages. To ensure the accuracy of the parsing results, the system combines common semantic role labels that describe the semantic components in natural language. Through this combination, the system can accurately convert key semantic information mentioned in natural language (such as verbs and noun phrases) into corresponding abstract syntax tree nodes. For example, when describing "create a variable to save user input", "create" can be mapped to a "declaration" node, "variable" is mapped to a "variable node", and "user input" is mapped to an "input node".

[0112] The system will then further adjust the structure of the preliminary abstract syntax tree by matching it with the predefined syntax template of the target language. This process uses an algorithm based on unification to compare the matching degree of the preliminary abstract syntax tree with the syntax template of the target language in terms of node type, structure and attributes. Unification is a structural matching algorithm that can find the subtrees in the preliminary abstract syntax tree that match the target language template and embed them into the syntax template of the target language. In this process, the preliminary abstract syntax tree will be gradually adjusted to conform to the grammatical structure of the target language to form an intermediate abstract syntax tree. This intermediate abstract syntax tree contains the basic grammatical structure in natural language and follows the grammatical rules of the target language.

[0113] Next, the system models the long-distance dependencies between the nodes of the abstract syntax tree through the self-attention mechanism to capture the semantic and grammatical dependencies between the nodes. The system inputs the node sequence of the intermediate abstract syntax tree into the pre-trained programming language conversion model. The model uses a multi-head attention mechanism and feedforward neural network decoding to generate a candidate node sequence of the abstract syntax tree of the target language. The multi-head attention mechanism captures multiple grammatical dependencies in the code by sharing information between different attention heads. During the decoding process, the model dynamically incorporates the grammatical constraints of the target language to ensure that the generated candidate node sequence conforms to the grammatical specifications of the target language, and finally successfully converts the intermediate abstract syntax tree into the abstract syntax tree of the target language.

[0114] After generating the target language abstract syntax tree, the system uses a defined abstract syntax tree traverser to recursively visit each node in the target language abstract syntax tree. The purpose of the traverser is to gradually convert the abstract syntax tree nodes into executable target language code snippets. The specific structure represented by each node (such as function, variable declaration, expression, etc.) will be converted into the corresponding code structure in the target language. During the conversion process, the system combines context information and heuristic rules to automatically generate names for identifiers in the code that conform to the naming conventions of the target language. These naming conventions are automatically generated based on the conventions of the target language (such as camel case naming or underscore separators).

[0115] In the process of generating code, the system will also automatically generate comments for the generated code using a templated approach based on the structure and type information of the abstract syntax tree. These comments help improve the readability and maintainability of the code, and the content of the comments usually includes the function description, parameter explanation, and return value information of the function. In addition, the system will also apply dead code elimination technology to remove parts of the code that will not be executed, ensuring that the generated code is efficient and clean.

[0116] Ultimately, the system will output target code that conforms to the grammatical specifications of the target language, is semantically correct, and is optimized.

[0117] In this embodiment, the system can automatically convert the program logic described in natural language into code that conforms to the target programming language, greatly improving programming efficiency. By designing a universal abstract syntax tree node type system, the solution can be easily extended to multiple programming languages ​​and support cross-language code generation. Through syntax template matching, self-attention mechanism, multi-head attention and templated annotations, the generated code is not only syntactically correct, but also optimized, highly readable and maintainable. The system can automatically generate symbol names according to the naming conventions of the target language to ensure that the generated code meets the style requirements of the target language. Through dead code elimination technology, the system can remove redundant code to ensure that the generated code is efficient and non-redundant, thereby improving code performance.

[0118] In an optional embodiment,

[0119] The node sequence of the intermediate abstract syntax tree is input into the pre-trained programming language conversion model, and the candidate node sequence of the target language abstract syntax tree is generated through multi-head attention mechanism and feedforward neural network decoding. The grammatical constraints of the target language are incorporated into the decoding process. The conversion of the intermediate abstract syntax tree into the target language abstract syntax tree includes:

[0120] Formalizing the grammatical rules of the target language, constructing the vocabulary, grammar tree template and grammar constraint pattern of the target language, representing the grammar rules in the form of an abstract syntax tree, generating input features of a grammar perception module, and constructing a grammar rule scoring model, wherein the grammar rule scoring model receives a node sequence of an intermediate abstract syntax tree as input and outputs a grammar legality score;

[0121] The grammar-aware module is integrated into the programming language conversion model, the target language code generation task and the target language grammar structure prediction task are constructed as a joint objective of multi-task learning, and a joint loss function is constructed by combining the code generation loss and the grammar structure prediction loss. The programming language conversion model integrated with the grammar-aware module is pre-trained using the constructed joint loss function on a parallel corpus containing the grammar information of the target language code, and a trained programming language conversion model is obtained.

[0122] The node sequence of the extracted intermediate abstract syntax tree is input into the trained programming language conversion model, and the candidate node sequence of the target language abstract syntax tree is generated through multi-head attention mechanism and feedforward neural network decoding. During the decoding process, the generated candidate node sequence is checked in real time using the grammar rule scoring model, the grammar legitimacy score is calculated, and the generation strategy of the candidate node sequence is dynamically adjusted according to the scoring result;

[0123] According to the generated candidate node sequence, multiple candidate target language abstract syntax tree structures are constructed. A rule-based syntax filtering mechanism is used to check whether the structure and nodes of each candidate target language abstract syntax tree conform to the grammatical requirements of the target language. The candidate target language abstract syntax trees that do not conform to the grammatical rules are eliminated. The grammatical correctness of the remaining candidate target language abstract syntax trees is evaluated using a grammatical rule scoring model. The target language abstract syntax tree with the highest grammatical legitimacy score is selected as the target language abstract syntax tree.

[0124] Exemplarily, first, the grammatical rules of the target language are formally described. The purpose of this step is to construct the vocabulary, syntax tree template and grammatical constraint pattern of the target language. The vocabulary defines the basic elements of the target language (such as keywords, operators, etc.), the syntax tree template provides common code structures in the target language, such as function definitions, conditional statements, loops, etc., and the grammatical constraint pattern describes the relationship and rules between each node. By abstracting these grammatical rules into an abstract syntax tree, the system can form a general representation of the target language code structure. This representation will serve as the input feature of the grammar perception module.

[0125] Next, the system needs to build a grammar rule scoring model, whose function is to score the generated intermediate abstract syntax tree node sequence. Specifically, the grammar rule scoring model accepts the node sequence in the intermediate abstract syntax tree as input and outputs the grammatical legitimacy score of each node sequence to evaluate whether it meets the grammatical requirements of the target language.

[0126] Next, the system integrates the syntax-aware module into the programming language conversion model to build a joint model of multi-task learning. In this joint model, the target language code generation task and the target language grammatical structure prediction task are set as common learning goals. The system constructs a joint loss function by optimizing the code generation loss and the grammatical structure prediction loss. This loss function combines the code generation accuracy and the grammatical prediction correctness to ensure that the generated code is not only functionally correct, but also grammatically conforms to the specification. To train the joint model, the system uses a parallel corpus, that is, training data containing the target language code and the corresponding grammatical structure, to pre-train the model and obtain a trained programming language conversion model.

[0127] When generating the target language code, the system inputs the node sequence of the extracted intermediate abstract syntax tree into the pre-trained programming language conversion model. This model decodes based on the multi-head attention mechanism and feedforward neural network to generate candidate node sequences of the target language abstract syntax tree. During the decoding process, the system uses the grammar rule scoring model to perform real-time grammar checking on each generated candidate node sequence, that is, by calculating the grammar legitimacy score of each candidate node sequence, it determines whether its grammatical structure conforms to the rules of the target language. If the grammar score of the candidate node is low, the system will dynamically adjust the decoding strategy to generate a node sequence that is more in line with the grammar rules.

[0128] Once the candidate node sequences are generated, the system will construct multiple possible target language abstract syntax tree structures based on these candidate node sequences. In order to ensure that the generated target code conforms to the grammatical specifications, the system will use a rule-based grammar filtering mechanism to check each candidate abstract syntax tree and eliminate those trees that do not conform to the grammatical rules of the target language. This mechanism checks the node structure of each tree one by one according to the pre-defined grammatical rules and constraints to ensure that they conform to the grammatical requirements of the target language. After that, the system will use the grammar rule scoring model to score the remaining candidate syntax trees, and select the abstract syntax tree with the highest grammatical legitimacy score as the final target language abstract syntax tree based on the scoring results.

[0129] In this embodiment, during the code generation process, the system can check the grammatical correctness of the candidate code in real time through the grammatical rule scoring model, and dynamically adjust the decoding strategy according to the scoring results to ensure that the grammatical structure of the generated code is legal. The system jointly optimizes the code generation task and the grammatical structure prediction task through multi-task learning to ensure that the generated code is not only functionally correct, but also conforms to the specification of the target language in terms of grammatical structure. The use of rule-based grammar filtering mechanism and grammar scoring model to screen candidate grammar trees can effectively improve the quality of the generated code and ensure that the final output code meets the grammatical standards of the target language. Through the formal description of grammar rules and the integration of grammar perception modules, the system can adapt to a variety of programming languages ​​and easily realize the migration and generation of codes between different languages. The whole process from natural language description to target code generation is highly automated, reducing manual intervention. At the same time, the accuracy and readability of the generated code are also guaranteed.

[0130] Figure 2 FIG. 1 is a schematic diagram of the structure of a dynamic code generation system based on an interface document according to an embodiment of the present invention. Figure 2 As shown, the system comprises:

[0131] The first unit is used to obtain and preprocess the interface document, divide the preprocessed interface document into multiple semantic blocks, including function signatures, parameter lists and return value descriptions, perform word embedding processing on each semantic block, obtain a distributed vector representation of the semantic block, input the distributed vector representation into a pre-built bidirectional long short-term memory network, adaptively adjust the attention to different semantic blocks in combination with context information, obtain a semantic block representation that integrates context information, link the obtained semantic block representation with a concept entity in a pre-built software engineering domain knowledge graph, organize the semantic block representation into a triple form based on the linked concept entity information, and obtain a triple representation of the semantic block;

[0132] The second unit is used to construct a code template library based on the triple representation of semantic blocks, extract candidate code templates from the code template library through an adaptive mining algorithm, evaluate and screen the candidate code templates based on coverage, accuracy and readability, and parameterize the screened code templates using a symbolic execution algorithm to obtain a target code template, perform structural optimization on the target code template, model the structural optimization problem of the code template as a multi-objective optimization model, and obtain the code template with the optimal structure by solving the multi-objective optimization model;

[0133] The third unit is used to parse the natural language description into an initial abstract syntax tree based on the optimal structure code template and match it with the syntax templates of different programming languages. At the same time, the initial abstract syntax tree is converted into an abstract syntax tree of the target programming language using a pre-trained programming language conversion model. The programming language conversion model adopts an encoder-decoder network to integrate the syntax constraints of the target programming language during the decoding process, and finally generates grammatically correct target code based on the converted abstract syntax tree.

[0134] The present invention may be a method, an apparatus, a system and / or a computer program product. The computer program product may include a computer-readable storage medium carrying computer-readable program instructions for executing various aspects of the present invention.

[0135] 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 replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.

Claims

1. A dynamic code generation method based on interface document, characterized in that: include: Obtaining an interface document and preprocessing it, dividing the preprocessed interface document into multiple semantic blocks, including a function signature, a parameter list, and a return value description, performing word embedding processing on each semantic block to obtain a distributed vector representation of the semantic block, inputting the distributed vector representation into a pre-built bidirectional long short-term memory network, adaptively adjusting the attention paid to different semantic blocks in combination with context information, obtaining a semantic block representation that integrates context information, linking the obtained semantic block representation with a concept entity in a pre-built software engineering domain knowledge graph, organizing the semantic block representation into a triple form based on the linked concept entity information, and obtaining a triple representation of the semantic block; A code template library is constructed based on the triple representation of semantic blocks, candidate code templates are extracted from the code template library through an adaptive mining algorithm, the candidate code templates are evaluated and screened according to coverage, accuracy and readability, and the screened code templates are parameterized using a symbolic execution algorithm to obtain a target code template, the target code template is structurally optimized, the structural optimization problem of the code template is modeled as a multi-objective optimization model, and the code template with the optimal structure is obtained by solving the multi-objective optimization model; Based on the optimally structured code template, the natural language description is parsed into an initial abstract syntax tree and matched with syntax templates of different programming languages. At the same time, the initial abstract syntax tree is converted into an abstract syntax tree of the target programming language using a pre-trained programming language conversion model. The programming language conversion model uses an encoder-decoder network to integrate the syntax constraints of the target programming language during the decoding process. Finally, a grammatically correct target code is generated based on the converted abstract syntax tree. The obtained semantic block representation is linked to the concept entities in the pre-built software engineering domain knowledge graph. The semantic block representation is organized into triples based on the linked concept entity information. The obtained triple representation of the semantic block includes: Calculate the similarity between each semantic block representation and the conceptual entities in the pre-built software engineering domain knowledge graph, filter out the knowledge graph entities corresponding to each semantic block representation, and generate preliminary triples based on the connection relationship between the semantic block representation and the filtered knowledge graph entities; A graph structure is constructed based on the semantic block representation and the screened knowledge graph entities. The semantic blocks and knowledge graph entities are used as nodes in the graph. The connection relationship between the semantic blocks and the knowledge graph entities, as well as the relationship between entities in the knowledge graph, are used as edges connecting the nodes. Through multi-layer graph convolution operations, the feature representation of each node is aggregated with the features of its neighboring nodes, and the representation of the current node is gradually updated to obtain a node representation that integrates the knowledge graph entities and the relationships between entities. A multi-hop propagation mechanism is introduced to enhance the semantic information of the nodes. At the same time, an attention mechanism is used to dynamically adjust the attention of each node to its neighboring nodes, and finally a target node representation is obtained. Based on the target node representation, an initial triple representation is obtained, which includes the semantic block, the knowledge graph entity corresponding to the semantic block, and the relationship; Based on the initial triple representation, valid triples and invalid triples are constructed as inputs for self-supervised learning, and a two-layer encoder is constructed, wherein the first-layer encoder is used to vectorize the semantic blocks and knowledge graph entities to obtain high-dimensional vector representations of the semantic blocks and knowledge graph entities, and the second-layer encoder is used to introduce a contrastive learning mechanism on the basis of the first-layer encoder, and the semantic distance of the valid triples is minimized and the semantic distance of the invalid triples is maximized by optimizing the contrastive learning objective function, and finally the target triple representation of the semantic block is generated.

2. The method according to claim 1, characterized in that The calculation formula of contrast learning objective function is as follows: ; Where L represents the objective function, E represents the valid triple set, s represents the semantic block, e represents the entity, r represents the relationship, Sim(·) represents the similarity function, λ represents the weight coefficient of the negative sample contrast loss, N(s) represents the negative sample set related to the semantic block s, e′ represents the entity in the negative sample, r′ represents the relationship in the negative sample, δ represents the threshold of the negative sample similarity, γ represents the regularization term coefficient, S represents the triple set containing contextual information, w s,e,r Represents the parameter vector associated with the triple (s, e, r).

3. The method according to claim 1, characterized in that The symbolic execution algorithm is used to parameterize the selected code templates, and the target code templates include: Taking each decision point in the symbolic execution process as a state and selecting an execution path as an action, constructing a reinforcement learning mechanism for optimizing the symbolic execution path selection based on the state and the action, adjusting the symbolic execution path exploration strategy in real time according to the reinforcement learning mechanism, and generating an adjusted path exploration strategy; In the initial path exploration process of symbolic execution, path selection is performed according to the adjusted path exploration strategy, and path information, path execution results, and path execution performance indicators of the selected path are collected and summarized to generate a training data set for the reinforcement learning model. The training data set is used to perform training through a preset reinforcement learning algorithm to obtain a reinforcement learning model for determining the optimal decision action for each decision point; Using natural language processing methods to extract semantic features from unstructured information contained in the code, converting the extracted semantic features into symbolic representations that are consistent with the structured symbol types in the code, fusing the symbolic representations with the structured symbols in the code to generate a fused symbolic representation for symbolic execution, and in the reasoning and path selection process of symbolic execution, optimizing the path selection of symbolic execution based on the fused symbolic representation to obtain a set of feasible paths; A corresponding parameterized model is established for each feasible path in the feasible path set. Based on the constructed parameterized model, an initial parameter value is set for each feasible path. After each symbolic execution and the optimal path is selected using the reinforcement learning model, the reinforcement learning agent calculates the path execution reward value through a preset reward function according to the execution feedback information of the selected path. According to the path execution reward value, the initial parameter value of the parameterized model corresponding to the selected path is adjusted to obtain the optimal parameter value of the selected path. Each feasible path and the corresponding optimal parameter value are input into the parameterized model, each feasible path is parameterized, a parameterized code fragment is generated, and the parameterized code fragments are combined to obtain a target code template.

4. The method according to claim 3, characterized in that According to the execution feedback information of the selected path, the formula for calculating the path execution reward value through the preset reward function is as follows: ; Among them, R t represents the path execution reward value, θ1 represents the weight coefficient of the immediate reward, R i (t) represents the immediate reward for path selection at time t, θ2 represents the weight coefficient of path performance feedback, η represents the parameter that controls the smoothness of the path performance feedback term, and P t represents the parameter value of the current path at time t, P z represents the target parameter value, θ3 represents the weight coefficient of the parameter adjustment reward item, and |ΔP| represents the absolute value of the path parameter adjustment amount.

5. The method according to claim 1, characterized in that The natural language description is parsed into an initial abstract syntax tree and matched with the syntax templates of different programming languages. At the same time, the initial abstract syntax tree is converted into an abstract syntax tree of the target programming language using a pre-trained programming language conversion model. The programming language conversion model uses an encoder-decoder network to integrate the syntax constraints of the target programming language during the decoding process. Finally, the grammatically correct target code is generated according to the converted abstract syntax tree, including: By abstracting and summarizing the grammatical rules of different programming languages, a universal abstract syntax tree node type system containing the common grammatical structure of programming languages ​​is designed. Based on the constructed universal abstract syntax tree node type system, the input natural language description is parsed into a preliminary abstract syntax tree that is independent of the programming language. During the parsing process, the pattern matching algorithm is used to identify the words and phrases in the natural language that correspond to the basic structure of the programming language. Combined with the semantic components described by the universal semantic role labels, the identified words, phrases and semantic components are converted into corresponding abstract syntax tree nodes. According to the predefined target language grammar template, a unified algorithm is used to compare the types, structures and attributes of the preliminary abstract syntax tree and the grammar template, match the target grammar template, extract the corresponding subtree of the preliminary abstract syntax tree and fill in the parameter position of the target grammar template, and obtain an intermediate abstract syntax tree that conforms to the grammatical structure of the target language; The self-attention mechanism is used to model the long-distance dependencies between abstract syntax tree nodes. The node sequence of the intermediate abstract syntax tree is input into the pre-trained programming language conversion model. The candidate node sequence of the target language abstract syntax tree is generated through multi-head attention mechanism and feedforward neural network decoding. The grammatical constraints of the target language are incorporated into the decoding process to convert the intermediate abstract syntax tree into the target language abstract syntax tree. Define an abstract syntax tree traversal, recursively visit each node of the target language abstract syntax tree, convert each visited node into a source code snippet of the target language, combine the context and heuristic rules to automatically generate names that conform to the naming conventions of the target language for the identifiers in the source code snippet of the target language, and use a templated method to generate code comments based on the structure and type information of the target language abstract syntax tree. At the same time, apply dead code elimination technology to remove code that will not be executed, and finally output syntactically correct target code.

6. The method according to claim 5, characterized in that The node sequence of the intermediate abstract syntax tree is input into the pre-trained programming language conversion model, and the candidate node sequence of the target language abstract syntax tree is generated through multi-head attention mechanism and feedforward neural network decoding. The grammatical constraints of the target language are incorporated into the decoding process. The conversion of the intermediate abstract syntax tree into the target language abstract syntax tree includes: Formalizing the grammatical rules of the target language, constructing the vocabulary, grammar tree template and grammar constraint pattern of the target language, representing the grammar rules in the form of an abstract syntax tree, generating input features of a grammar perception module, and constructing a grammar rule scoring model, wherein the grammar rule scoring model receives a node sequence of an intermediate abstract syntax tree as input and outputs a grammar legality score; The grammar-aware module is integrated into the programming language conversion model, the target language code generation task and the target language grammar structure prediction task are constructed as a joint objective of multi-task learning, and a joint loss function is constructed by combining the code generation loss and the grammar structure prediction loss. The programming language conversion model integrated with the grammar-aware module is pre-trained using the constructed joint loss function on a parallel corpus containing the grammar information of the target language code, and a trained programming language conversion model is obtained. The node sequence of the extracted intermediate abstract syntax tree is input into the trained programming language conversion model, and the candidate node sequence of the target language abstract syntax tree is generated through multi-head attention mechanism and feedforward neural network decoding. During the decoding process, the generated candidate node sequence is checked in real time using the grammar rule scoring model, the grammar legitimacy score is calculated, and the generation strategy of the candidate node sequence is dynamically adjusted according to the scoring result; According to the generated candidate node sequence, multiple candidate target language abstract syntax tree structures are constructed. A rule-based syntax filtering mechanism is used to check whether the structure and nodes of each candidate target language abstract syntax tree conform to the grammatical requirements of the target language. The candidate target language abstract syntax trees that do not conform to the grammatical rules are eliminated. The grammatical correctness of the remaining candidate target language abstract syntax trees is evaluated using a grammatical rule scoring model. The target language abstract syntax tree with the highest grammatical legitimacy score is selected as the target language abstract syntax tree.

7. A dynamic code generation system based on an interface document, used to implement the method according to any one of claims 1 to 6, characterized in that: include: The first unit is used to obtain and preprocess the interface document, divide the preprocessed interface document into multiple semantic blocks, including function signatures, parameter lists and return value descriptions, perform word embedding processing on each semantic block, obtain a distributed vector representation of the semantic block, input the distributed vector representation into a pre-built bidirectional long short-term memory network, adaptively adjust the attention to different semantic blocks in combination with context information, obtain a semantic block representation that integrates context information, link the obtained semantic block representation with a concept entity in a pre-built software engineering domain knowledge graph, organize the semantic block representation into a triple form based on the linked concept entity information, and obtain a triple representation of the semantic block; The second unit is used to construct a code template library based on the triple representation of semantic blocks, extract candidate code templates from the code template library through an adaptive mining algorithm, evaluate and screen the candidate code templates based on coverage, accuracy and readability, and parameterize the screened code templates using a symbolic execution algorithm to obtain a target code template, perform structural optimization on the target code template, model the structural optimization problem of the code template as a multi-objective optimization model, and obtain the code template with the optimal structure by solving the multi-objective optimization model; The third unit is used to parse the natural language description into an initial abstract syntax tree based on the optimal structure code template and match it with the syntax templates of different programming languages. At the same time, the initial abstract syntax tree is converted into an abstract syntax tree of the target programming language using a pre-trained programming language conversion model. The programming language conversion model adopts an encoder-decoder network to integrate the syntax constraints of the target programming language during the decoding process, and finally generates grammatically correct target code based on the converted abstract syntax tree.

8. An electronic device, characterized in that: include: processor; a memory for storing processor-executable instructions; The processor is configured to call the instructions stored in the memory to execute the method according to any one of claims 1 to 6.

9. A computer-readable storage medium having computer program instructions stored thereon, characterized in that: When the computer program instructions are executed by a processor, the method according to any one of claims 1 to 6 is implemented.

Citation Information

Patent Citations

  • Code generation method based on abstract syntax tree structure information enhancement

    CN114385178A

  • Python language code snippet generation method based on graph learning

    CN114489669A