Program code intelligent conversion method and system based on knowledge graph

Through the intelligent conversion method of program code based on knowledge graphs, a semantic topology network is constructed and cross-language semantic mapping rules are combined, the shortcomings of traditional code conversion methods in semantic consistency and complex dependency recognition are solved, and efficient and semantic consistent code generation is achieved.

CN120104113APending Publication Date: 2025-06-06山东衡昊信息技术有限公司
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202510179444.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-02-18
Publication Date
2025-06-06

AI Technical Summary

Technical Problem

Traditional code conversion methods lack universality and are difficult to deeply explore program semantics, resulting in the converted target code semantics inconsistent with the source code, and it is difficult to identify and parse complex dependencies, resulting in poor performance and maintainability of generated code.

Method used

The intelligent transformation method of program code based on knowledge graph is adopted, and the node feature vectors and edge weights are dynamically updated, combined with the cross-language semantic mapping rules of the knowledge graph, the target semantic vector is generated, and the code snippet is optimized through the recursive generation model to ensure the semantic consistency and syntactic compliance of the generated code.

Benefits of technology

It realizes deep semantic correlation capture between source code and target code, ensuring that the generated code is semantically consistent with the source code, and improving code generation efficiency and code performance and maintainability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120104113A_ABST
    Figure CN120104113A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of computer science, in particular to an intelligent program code conversion method and system based on a knowledge graph. The method comprises the following steps: acquiring a source code and performing structured analysis to generate an initial feature vector of a node; constructing a semantic topology network based on the initial feature vector of the node, dynamically updating the node feature vector and the edge weight, generating an optimized semantic topology network, and mapping the node feature vector to a target semantic space in combination with a cross-language semantic mapping rule of the knowledge graph to generate a target semantic vector; and based on the basic framework of the target language, constructing an initial code, and recursively filling the code snippets to generate a target code. The problem that a traditional code conversion method lacks universality and deep mining of program semantics is solved. A complex dependency relationship is often difficult to effectively identify and analyze, so that the performance and the maintainability of generated codes are relatively poor; and a dynamic adjustment mechanism is lacked, so that the accuracy and integrity of generated codes are relatively low.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer science and technology, and in particular to a method and system for intelligent conversion of program codes based on knowledge graphs. Background Art

[0002] With the continuous development and evolution of the field of software development, the types of programming languages ​​and application scenarios have increased rapidly. The differences in grammatical rules, semantic expressions and operating environments between different languages ​​have led to an increasingly widespread demand for multi-language collaborative development. From traditional single-language development to today's multi-language mixed development, cross-language conversion and migration of program code has become an important challenge in software development. For example, during the process of technology upgrades, enterprises need to migrate legacy systems from old languages ​​to modern programming languages, while ensuring that the migrated systems are consistent with the original systems in terms of functionality and performance. At the same time, the popularity of distributed systems and microservice architectures means that different service modules may be implemented in different languages, and how to achieve seamless collaboration between these modules has become a key issue.

[0003] At present, code conversion not only needs to ensure that the target code conforms to the target language's syntax, but also must maintain equivalence with the source code at the semantic level. This requirement involves complex program semantics understanding, including multi-level relationships such as variable scope, control flow dependency, function call chain, and cross-module semantic association. In addition, modern programming languages ​​are constantly evolving new features, such as dynamic typing, asynchronous operations, and functional programming, making it difficult for traditional rule-based code conversion methods to meet diverse and dynamically changing conversion needs.

[0004] Traditional code conversion methods have the following technical problems: they can only convert specific language pairs, lack versatility, lack deep mining of program semantics, and the converted target code is often not semantically consistent with the source code; it is often difficult to effectively identify and parse complex dependencies, resulting in poor performance and maintainability of the generated code, making it difficult to meet the high-quality requirements in actual development; there is a lack of dynamic adjustment mechanism in the code generation process, resulting in low accuracy and completeness of the generated code, which limits its promotion and use in industrial-level applications. Summary of the invention

[0005] The present invention provides a method and system for intelligent conversion of program codes based on knowledge graph, so as to solve the problems that traditional code conversion methods can only convert specific language pairs, lack versatility, lack deep mining of program semantics, and the converted target code is often not semantically consistent with the source code; it is often difficult to effectively identify and parse complex dependencies, resulting in poor performance and maintainability of the generated code, making it difficult to meet the high-quality requirements in actual development; there is a lack of dynamic adjustment mechanism in the code generation process, resulting in low accuracy and completeness of the generated code, thereby limiting its promotion and use in industrial-level applications.

[0006] The program code intelligent conversion method and system based on knowledge graph of the present invention specifically include the following technical solutions:

[0007] The program code intelligent conversion method based on knowledge graph includes the following steps:

[0008] S1. Obtain source code through a standardized code input interface, perform structured analysis, and generate initial feature vectors of nodes; construct a semantic topology network based on the initial feature vectors of nodes, and dynamically update node feature vectors and edge weights to generate an optimized semantic topology network;

[0009] S2. Based on the optimized semantic topology network and combined with the cross-language semantic mapping rules of the knowledge graph, the node feature vector is mapped to the target semantic space to generate the target semantic vector. Based on the basic framework of the target language, the initial code is constructed. Based on the initial code and combined with the target semantic vector, the code snippets are recursively filled to generate the target code.

[0010] Preferably, the S1 specifically includes:

[0011] Input source code, where each node represents a single logical unit in the code; extract the grammatical feature vector of the node through the abstract syntax tree generator, and extract the contextual feature vector of the node through the context analysis model; merge the grammatical feature vector and the contextual feature vector to generate the initial feature vector of the node.

[0012] Preferably, the S1 specifically includes:

[0013] Based on the semantic relationship of the knowledge graph, the initial feature vectors of the nodes are used to construct a semantic topological network and initialize it. The specific implementation process is: first, the Euclidean distance between the initial feature vectors of the nodes is calculated; then the inner product of the initial feature vectors of the nodes is calculated; finally, the exponential function is used for normalization to obtain the initial edge weights of the nodes.

[0014] Preferably, the S1 specifically includes:

[0015] The node feature vectors and edge weights of the semantic topology network are dynamically updated to generate an optimized semantic topology network. During the dynamic update process, the node feature vector evolves with the time step. The node feature vector is updated based on the node feature vector and edge weight of the previous time step and the nonlinear adjustment term based on the sine function. The dynamic update formula of the node feature vector is:

[0016]

[0017] in, is the ith node n i The node feature vector at time step t+1; LN represents the normalization operation; is the ith node n i The node feature vector at time step t; The jth node n j The node feature vector at time step t; |N| is the total number of nodes in the semantic topology network; is the number of nodes n at time step t. i and the jth node n j The edge weight between agg and b agg are the weights and biases of feature aggregation; It is a nonlinear adjustment term based on the sine function.

[0018] Preferably, the S1 specifically includes:

[0019] In the dynamic update process, the edge weights are dynamically updated based on the cosine similarity and gradient changes between node feature vectors and the introduction of regularization coefficients.

[0020] Preferably, the S2 specifically includes:

[0021] Based on the optimized semantic topology network, a nonlinear activation function and a mapping feature weight matrix are introduced to generate a target semantic vector by combining each node's own feature vector and the feature vectors of its neighboring nodes.

[0022] Preferably, the S2 specifically includes:

[0023] The initial code is generated based on the generation rules through the basic framework structure of the target language; the generation rules of the initial code are: extract control flow nodes from the abstract syntax tree of the source code, and map the extracted control flow nodes to the equivalent control flow structure of the target language; analyze the variable definitions and data types in the source code, and map them to the equivalent data structure of the target language; based on the control flow skeleton and data structure mapping results, fill in the basic code framework of the target language to generate the initial code.

[0024] Preferably, the S2 specifically includes:

[0025] Based on the initial code, code snippets are recursively generated according to the semantic mapping rules and integrated into the existing code structure. In the recursive generation process, the target semantic tensor is generated based on the target semantic vector, and the Softmax function is introduced to recursively generate the target code. The recursive generation formula is as follows:

[0026]

[0027] in, is the state of the recursively generated target code at time step t+1; is the state of the recursively generated target code at time step t; is the initial code snippet of the target code; △C is the newly added code snippet during the recursive generation process; Softmax represents the normalized probability distribution function during code generation; is the target semantic tensor T map The gradient of is the semantic feature of the target code; M i is the ith node n i The target semantic vector of M j is the jth node n j The target semantic vector of represents the outer product operation; |N| is the total number of nodes in the semantic topology network.

[0028] The program code intelligent conversion system based on knowledge graph includes the following parts:

[0029] Code input and parsing module, semantic topology network construction module, network dynamic evolution module, semantic mapping module, initial code generation module, code recursive generation module;

[0030] The code input and parsing module obtains source code through a standardized code input interface; parses the syntax structure of the code through an abstract syntax tree generator to obtain the syntax feature vector of the node; extracts the context dependency information between codes through a context analysis model to obtain the context feature vector of the node; passes the syntax feature vector and context feature vector of the node to the semantic topology network construction module, and passes the abstract syntax tree to the initial code generation module;

[0031] The semantic topology network construction module fuses the grammatical feature vector and context feature vector of the node to generate the initial feature vector of the node; combines the Euclidean distance and vector inner product of the initial feature vector of the node to calculate the initial edge weight and construct the initial semantic topology network; and passes the initial semantic topology network to the network dynamic evolution module;

[0032] The network dynamic evolution module receives the initial semantic topology network and dynamically updates it, iteratively updates the node feature vectors time by time, and adjusts the edge weights based on the cosine similarity and gradient changes between the node feature vectors to generate an optimized semantic topology network; the optimized semantic topology network is passed to the semantic mapping module and the initial code generation module;

[0033] The semantic mapping module, based on the optimized semantic topology network and combined with the cross-language semantic mapping rules in the knowledge graph, introduces a mapping feature weight matrix and a nonlinear activation function to map the feature vector of each node to the target semantic space and generate a target semantic vector; the target semantic vector is passed to the code recursive generation module;

[0034] The initial code generation module receives the abstract syntax tree of the source code, generates the initial code by extracting and analyzing the control flow and data flow of the source code, and passes the initial code to the code recursive generation module;

[0035] The code recursive generation module generates a target semantic tensor based on the target semantic vector; based on the initial code and the target semantic tensor, the target code is generated through recursive optimization.

[0036] The beneficial effects of the technical solution of the present invention are:

[0037] 1. By combining the semantic topological network and the knowledge graph, the deep semantic correlation between the source code and the target code can be captured during the code conversion process, so that the generated target code is semantically completely consistent with the source code, and can fully follow the rules of the target language at the grammatical level; by using the recursive generation model and maximizing the similarity between the target semantic tensor and the semantic features of the current code, the program code intelligent conversion system can greatly improve the code generation efficiency by guiding the generation direction of the code fragments during the code generation process, avoiding invalid calculations and repeated logic.

[0038] 2. The present invention can capture the changing characteristics of the program structure by constructing a dynamically evolving semantic topological network, and dynamically update the node feature vectors and edge weights over time, so that the program code intelligent conversion system can adapt to the complex dependencies appearing in the source code, including variable scopes, function call chains, and control dependencies between code blocks, to ensure that the generated target code is always consistent with the source code; the present invention utilizes the cross-language semantic mapping rules stored in the knowledge graph, combined with multimodal information fusion, to improve the expressiveness of program nodes at the grammatical, contextual and semantic levels.

[0039] 3. Through the analysis of abstract syntax trees and context analysis models, the source code is decomposed into structured semantic information and a unified semantic topology network is constructed. The modular design not only facilitates functional expansion, but also provides a reliable foundation for subsequent code generation and optimization. The program code intelligent conversion system combines the grammatical characteristics and best practices of the target language in the code generation stage, and generates the logic and structure of the code fragments through recursive optimization, thereby generating more efficient and maintainable target code. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] Figure 1 This is a structural diagram of the program code intelligent conversion system based on knowledge graph according to the present invention;

[0041] Figure 2 This is a flow chart of the program code intelligent conversion method based on knowledge graph described in the present invention. DETAILED DESCRIPTION

[0042] In order to further explain the technical means and effects adopted by the present invention to achieve the predetermined invention purpose, the technical scheme in the embodiment of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiment of the present invention. Obviously, the described embodiment is only a part of the embodiment of the present invention, not all 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.

[0043] Unless defined otherwise, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention belongs.

[0044] The specific scheme of the program code intelligent conversion method and system based on knowledge graph provided by the present invention is described in detail below with reference to the accompanying drawings.

[0045] See attached Figure 1 , which shows a structure diagram of a program code intelligent conversion system based on a knowledge graph provided by an embodiment of the present invention, the system includes the following parts:

[0046] Code input and parsing module, semantic topology network construction module, network dynamic evolution module, semantic mapping module, initial code generation module, code recursive generation module;

[0047] The code input and parsing module is responsible for receiving source files or code snippets in various programming languages, ensuring multi-language compatibility through a standardized code input interface, parsing the grammatical structure of the code through an abstract syntax tree generator to obtain the grammatical feature vector of the node; extracting the context dependency information between codes through a context analysis model to obtain the context feature vector; passing the grammatical feature vector and context feature vector of the node to the semantic topology network construction module, and passing the abstract syntax tree to the initial code generation module;

[0048] The semantic topology network construction module fuses the grammatical feature vector and context feature vector of the node to generate the initial feature vector of the node; combines the Euclidean distance and vector inner product of the initial feature vector of the node to calculate the initial edge weight and construct the initial semantic topology network; and passes the initial semantic topology network to the network dynamic evolution module;

[0049] The network dynamic evolution module receives the initial semantic topology network and dynamically updates it, iteratively updates the node feature vectors time by time, and adjusts the edge weights based on the cosine similarity and gradient changes between the node feature vectors to generate an optimized semantic topology network; the optimized semantic topology network is passed to the semantic mapping module and the initial code generation module;

[0050] The semantic mapping module maps the feature vector of each node to the target semantic space and generates the target semantic vector based on the optimized semantic topology network and the cross-language semantic mapping rules of the knowledge graph. It introduces the mapping feature weight matrix and nonlinear activation function to enhance the expression ability of the node feature vector so that the generated target semantic vector can fully reflect the semantic correspondence between the source code and the target code. It passes the target semantic vector to the code recursive generation module.

[0051] The initial code generation module receives the abstract syntax tree of the source code, generates the initial code by extracting and analyzing the control flow and data flow of the source code, and passes the initial code to the code recursive generation module;

[0052] The code recursive generation module generates a target semantic tensor based on the target semantic vector; based on the initial code and the target semantic tensor, it generates a complete target code through recursive optimization; in the recursive process, the generation direction is adjusted by calculating the similarity between the target semantic tensor and the current code semantic features, so that the generated code fragment is closer to the target semantic expression, and finally outputs a complete target code with consistent semantics, syntactically compliant and performance optimized.

[0053] See attached Figure 2 , which shows a flow chart of a method for intelligent conversion of program code based on knowledge graph provided by an embodiment of the present invention, the method comprising the following steps:

[0054] S1. Obtain source code through a standardized code input interface, perform structured analysis, and generate initial feature vectors of nodes; construct a semantic topology network based on the initial feature vectors of nodes, and dynamically update node feature vectors and edge weights to generate an optimized semantic topology network;

[0055] The source code is obtained through a standardized code input interface. The code input interface can receive source files or code snippets in multiple programming languages ​​and supports multi-layer code structure parsing. The input code is structurally decomposed by calling the parsing tool chain, including the abstract syntax tree (AST) generator and the context analysis model.

[0056] First, the grammatical structure of the source code is parsed through an abstract syntax tree generator, and the variables, operators, function calls, and control flows in the source code are mapped into grammatical nodes and edge relationships as the basic data for building a semantic topology network.

[0057] Then, the contextual dependency information between nodes, such as variable scope, function call chain, and control dependency between code blocks, is extracted through the context analysis model, and the contextual dependency information is encoded into a context feature vector; in addition, in order to ensure the consistency of grammatical features and contextual features, the parsing tool chain includes multiple layers of verification logic so that the syntactic correctness and semantic consistency of the code can be checked, such as whether there are undeclared variables or unmatched function calls.

[0058] Specifically, the source code is input, and an abstract syntax tree is generated by an abstract syntax tree generator. Each node represents a single logical unit in the code; the syntax feature vector of the node is extracted by the abstract syntax tree generator. The syntax feature vector includes the node type (such as variable, function) and its sub-node relationship; the context feature vector of the node is extracted by the context analysis model. The context feature vector represents the association of the node in the global program structure, such as the call chain or control flow relationship; the syntax feature vector and the context feature vector are merged to generate the initial feature vector of the node; the initial feature vector of each node is represented by the formula:

[0059]

[0060] in, is the ith node n i The initial feature vector of V contains the fusion of the grammatical feature vector and the context feature vector; negative values ​​are suppressed by the ReLU function; V syn (n i ) represents the i-th node n i The grammatical feature vector of V is extracted by the abstract syntax tree generator; con (n i) represents the context feature vector of the i-th node ni, which is used to reflect the dependency relationship of the nodes in the program structure; W syn and W con are the weight matrices of the grammatical feature vector and the context feature vector, respectively, obtained by extracting features from sample data in the pre-training phase; b ini is a bias term, which is adjusted dynamically during training; ||V syn (n i )|| is the i-th node n i The Euclidean norm of the grammatical feature vector; The influence of specific grammatical feature vectors is amplified through nonlinear operations, and the amplitude of node feature vectors is limited by combining square and normalization to ensure that the feature vectors of different nodes are comparable.

[0061] Based on the semantic relationship of the knowledge graph, the initial feature vector of the node is used to build a semantic topological network and initialize it to capture the semantic dependency between program nodes; the knowledge graph provides semantic association information of program structure and function, including variable dependency, function call chain and control flow context; the edge weight of the semantic topological network represents the semantic association between nodes, and its calculation should consider both the feature difference and feature correlation between nodes; first, the Euclidean distance between the initial feature vectors of the nodes is calculated to measure the feature difference between nodes; then the inner product of the initial feature vector of the node is calculated to reflect the correlation of the node features; finally, normalization is performed in combination with the exponential function to obtain the initial edge weight of the node. The formula is:

[0062]

[0063] in, Represents the i-th node n i and the jth node n j The initial association weight between the i-th node n i and the jth node n j The initial edge weights between is the jth node n j The initial feature vector of ; α is the weight adjustment parameter, which is initially set to 0.5 and can be adjusted through training; is the ith node n i and the jth node n j The inner product of the initial feature vector of |N| is the total number of nodes in the semantic topology network. The denominator contains a normalization factor to ensure that the sum of all edge weights is a finite value, which is convenient for subsequent network optimization.

[0064] Dynamically update the node feature vectors and edge weights of the semantic topology network to generate an optimized semantic topology network to reflect the changes in semantic relationships over time. Evolving with time step t, it reflects the dynamic change characteristics of the program structure. The dynamic update formula of the node's feature vector is:

[0065]

[0066] in, is the ith node n i The node feature vector at time step t+1; LN represents the normalization operation; is the ith node n i The node feature vector at time step t; The jth node n j The node feature vector at time step t; is the number of nodes n at time step t. i and the jth node n j The edge weight between agg and b agg are the weights and biases of feature aggregation, which are obtained in advance through semantic network training; It is a nonlinear adjustment item based on the sine function, which is used to enhance the dynamic adaptability of the node feature vector;

[0067] The edge weight is dynamically updated based on the cosine similarity and gradient changes between node feature vectors, and a regularization coefficient is introduced to avoid overfitting. The dynamic update formula of edge weight is as follows:

[0068]

[0069] in, is the i-th node n at time step t+1 i and the jth n j The edge weights between is the ith node n i and the jth n j The cosine similarity between the node feature vectors of ; β is the learning step size, obtained through experiments; γ is the regularization coefficient, which is used to prevent the edge weight from growing too fast, obtained through experiments; It is a gradient change.

[0070] S2. Based on the optimized semantic topology network and combined with the cross-language semantic mapping rules of the knowledge graph, the node feature vector is mapped to the target semantic space to generate the target semantic vector. Based on the basic framework of the target language, the initial code is constructed. Based on the initial code and combined with the target semantic vector, the code snippets are recursively filled to generate the target code.

[0071] Based on the optimized semantic topology network and combined with the cross-language semantic mapping rules of the knowledge graph, the feature vector of each node is mapped to the target semantic space to generate a target semantic vector representing the semantic relationship between the source code and the target code; thereby capturing the complex nonlinear correlation between syntax, context, and semantic features, while making full use of the feature vectors of neighboring nodes in the semantic topology network, and improving the semantic expression ability by fusing multimodal information; the mapping process combines the feature vector of each node itself and the feature vector of the neighboring nodes, and generates the target semantic vector under the action of the nonlinear activation function. The mapping formula is as follows:

[0072]

[0073] Among them, Mi is the i-th node n i is the target semantic vector; Ni is the node feature vector of the i-th node; N j is the node feature vector of the jth node; W map is the mapping feature weight matrix, which is the parameter matrix generated by mapping training optimization; E ij is the ith node n i and the jth n j The edge weight between rel is the weight matrix of the relationship mapping, which is generated by training the relationship between nodes and optimizing; σ is the Sigmoid activation function; b rel is the relational bias vector obtained through experiments; the final generated eigenvector is normalized to the interval (-1, 1) through the hyperbolic tangent function tanh.

[0074] The initial code is generated through the basic framework structure of the target language. Its generation rules depend on the grammatical characteristics of the target language and the basic structure of the source language code. The initial code is a code skeleton generated by extracting and analyzing the control flow and data flow of the source code to ensure the integrity of the basic logical framework, but has not yet filled in specific function implementations or variable definitions. The generation rules of the initial code are as follows:

[0075] Extract control flow nodes (such as if, for, while, etc.) from the abstract syntax tree of the source code, and map the extracted control flow nodes to the equivalent control flow structure of the target language; analyze the variable definitions and data types in the source code, and map them to the equivalent data structures of the target language, such as converting C language int to Python int, or converting Java List to C++ std::vector; fill in the basic code framework of the target language according to the control flow skeleton and data structure mapping results, and generate the code skeleton, that is, the initial code.

[0076] Based on the initial code, code snippets are recursively generated according to the semantic mapping rules and integrated into the existing code structure to ensure that the generated target code conforms to the grammatical specifications of the target language and is semantically consistent with the source code; the key to the recursive generation process is to adjust the generation direction of the code snippet to maximize the similarity between the target semantic tensor and the semantic features of the current code; the generated code snippets are adjusted through the Softmax function and a probability distribution is generated to ensure that the selection of the code snippet has global optimization characteristics. The recursive generation formula is as follows:

[0077]

[0078] in, is the state of the recursively generated target code at time step t+1; is the state of the recursively generated target code at time step t; is the initial code snippet of the target code; ΔC is the newly added code snippet during the recursive generation process; Softmax represents the normalized probability distribution function during code generation, which is used to ensure that the selection of code snippets has global optimal characteristics; is the target semantic tensor T map The gradient of the target semantic tensor T map The rate of change in the direction of the current generated code; It is the semantic feature of the target code, which is extracted by the currently generated code fragment through the mapping function; M j is the jth node n j The target semantic vector of Represents the outer product operation, which is used to construct high-order feature interactions.

[0079] By recursively generating formulas, the code snippets generated in each iteration can effectively approach the expression of the target semantic tensor, thereby ensuring the semantic consistency and grammatical standardization of the generated code, so that the output code is not only functionally equivalent to the source code, but also can make full use of the characteristics of the target language for optimization.

[0080] In summary, a method and system for intelligent conversion of program codes based on knowledge graph have been completed.

[0081] The order of the embodiments of the invention is for description only and does not represent the advantages and disadvantages of the embodiments. The processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.

[0082] The various embodiments in this specification are described in a progressive manner, and the same or similar parts between the various embodiments can be referenced to each other, and each embodiment focuses on the differences from other embodiments.

[0083] The above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the above embodiments, those skilled in the art should understand that the technical solutions described in the above embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention, and should be included in the protection scope of the present invention.

Claims

1. A program code intelligent conversion method based on knowledge graph, characterized in that: The following steps are involved: S1. Obtain source code through a standardized code input interface, perform structured analysis, and generate initial feature vectors of nodes; construct a semantic topology network based on the initial feature vectors of nodes, and dynamically update node feature vectors and edge weights to generate an optimized semantic topology network; S2. Based on the optimized semantic topology network and the cross-language semantic mapping rules of the knowledge graph, the node feature vector is mapped to the target semantic space to generate the target semantic vector; based on the basic framework of the target language, the initial code is constructed; Based on the initial code and combined with the target semantic vector, the code snippets are recursively filled to generate the target code.

2. The method for intelligent conversion of program code based on knowledge graph according to claim 1 is characterized in that: The S1 specifically includes: Input source code, where each node represents a single logical unit in the code; extract the grammatical feature vector of the node through the abstract syntax tree generator, and extract the contextual feature vector of the node through the context analysis model; merge the grammatical feature vector and the contextual feature vector to generate the initial feature vector of the node.

3. The method for intelligent conversion of program code based on knowledge graph according to claim 2 is characterized in that: The S1 specifically includes: Based on the semantic relationship of the knowledge graph, the initial feature vectors of the nodes are used to construct a semantic topological network and initialize it. The specific implementation process is: first, the Euclidean distance between the initial feature vectors of the nodes is calculated; then the inner product of the initial feature vectors of the nodes is calculated; finally, the exponential function is used for normalization to obtain the initial edge weights of the nodes.

4. The method for intelligent conversion of program code based on knowledge graph according to claim 3 is characterized in that: The S1 specifically includes: The node feature vectors and edge weights of the semantic topology network are dynamically updated to generate an optimized semantic topology network. During the dynamic update process, the node feature vector evolves with the time step. The node feature vector is updated based on the node feature vector and edge weight of the previous time step and the nonlinear adjustment term based on the sine function. The dynamic update formula of the node feature vector is: in, is the ith node n i The node feature vector at time step t+1; LN represents the normalization operation; is the ith node n i The node feature vector at time step t; The jth node n j The node feature vector at time step t; |N| is the total number of nodes in the semantic topology network; is the number of nodes n at time step t. i and the jth node n j The edge weight between agg and b agg are the weights and biases of feature aggregation; It is a nonlinear adjustment term based on the sine function.

5. The method for intelligent conversion of program code based on knowledge graph according to claim 4 is characterized in that: The S1 specifically includes: In the dynamic update process, the edge weights are dynamically updated based on the cosine similarity and gradient changes between node feature vectors and the introduction of regularization coefficients.

6. The method for intelligent conversion of program code based on knowledge graph according to claim 1, characterized in that: The S2 specifically includes: Based on the optimized semantic topology network, a nonlinear activation function and a mapping feature weight matrix are introduced to generate a target semantic vector by combining each node's own feature vector and the feature vectors of its neighboring nodes.

7. The method for intelligent conversion of program code based on knowledge graph according to claim 6 is characterized in that: The S2 specifically includes: The initial code is generated based on the generation rules through the basic framework structure of the target language; the generation rules of the initial code are: extract control flow nodes from the abstract syntax tree of the source code, and map the extracted control flow nodes to the equivalent control flow structure of the target language; analyze the variable definitions and data types in the source code, and map them to the equivalent data structure of the target language; based on the control flow skeleton and data structure mapping results, fill in the basic code framework of the target language to generate the initial code.

8. The method for intelligent conversion of program code based on knowledge graph according to claim 7 is characterized in that: The S2 specifically includes: Based on the initial code, code snippets are recursively generated according to the semantic mapping rules and integrated into the existing code structure. In the recursive generation process, the target semantic tensor is generated based on the target semantic vector, and the Softmax function is introduced to recursively generate the target code. The recursive generation formula is as follows: in, is the state of the recursively generated target code at time step t+1; is the state of the recursively generated target code at time step t; is the initial code snippet of the target code; △C is the newly added code snippet during the recursive generation process; Softmax represents the normalized probability distribution function during code generation; is the target semantic tensor T map The gradient of is the semantic feature of the target code; M i is the ith node n i The target semantic vector of M j is the jth node n j The target semantic vector of represents the outer product operation; |N| is the total number of nodes in the semantic topology network.

9. A program code intelligent conversion system based on knowledge graph, applied to the program code intelligent conversion method based on knowledge graph as claimed in claim 1, characterized in that: Includes the following parts: Code input and parsing module, semantic topology network construction module, network dynamic evolution module, semantic mapping module, initial code generation module, code recursive generation module; Code input and parsing module, which obtains source code through a standardized code input interface; The grammatical structure of the code is parsed by the abstract syntax tree generator to obtain the grammatical feature vector of the node; the contextual dependency information between codes is extracted by the context analysis model to obtain the contextual feature vector of the node; the grammatical feature vector and the contextual feature vector of the node are passed to the semantic topology network construction module, and the abstract syntax tree is passed to the initial code generation module; The semantic topology network construction module fuses the grammatical feature vector and context feature vector of the node to generate the initial feature vector of the node; combines the Euclidean distance and vector inner product of the initial feature vector of the node to calculate the initial edge weight and construct the initial semantic topology network; and passes the initial semantic topology network to the network dynamic evolution module; The network dynamic evolution module receives the initial semantic topology network and dynamically updates it, iteratively updates the node feature vectors time by time, and adjusts the edge weights based on the cosine similarity and gradient changes between the node feature vectors to generate an optimized semantic topology network; the optimized semantic topology network is passed to the semantic mapping module and the initial code generation module; The semantic mapping module, based on the optimized semantic topology network and combined with the cross-language semantic mapping rules in the knowledge graph, introduces a mapping feature weight matrix and a nonlinear activation function to map the feature vector of each node to the target semantic space and generate a target semantic vector; the target semantic vector is passed to the code recursive generation module; An initial code generation module receives an abstract syntax tree of a source code and generates initial code by extracting and analyzing a control flow and a data flow of the source code; Pass the initial code to the code recursive generation module; The code recursive generation module generates a target semantic tensor based on the target semantic vector; based on the initial code and the target semantic tensor, the target code is generated through recursive optimization.

Citation Information

Cited By

  • Code automatic generation and optimization system based on multiple modes

    CN120276718A

  • APP performance multi-dimensional analysis method and system based on machine learning

    CN120723614A