Code conversion method and device, electronic equipment and medium
By generating component trees and using neural network models for cross-platform code conversion, the accuracy and efficiency of JSON file conversion between low-code platforms are solved, and efficient cross-platform code conversion is achieved.
Patent Information
- Application Number
- CN202510669326.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-22
- Publication Date
- 2025-08-01
AI Technical Summary
The JSON files generated by different low-code platforms vary in content and format, resulting in project migration and conversion relying on developers' manual operations, and the possibility of errors is high.
By generating a component tree, the source code is divided into multiple component nodes, the neural network model is used for code conversion, and the location information of the component tree is spliced to realize cross-platform code conversion.
Reduces conversion errors caused by excessive code length, improves the accuracy and efficiency of code conversion, and reduces dependence on developer experience.
Smart Images

Figure CN120406957A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and particularly to a method, apparatus, electronic device, and computer-readable storage medium for code conversion. Background Art
[0002] A low-code platform is a development tool for quickly building application programs through a graphical interface, visual drag-and-drop components, and preset modules. It aims to lower the programming threshold, enabling non-professional developers to participate in application development and greatly improving the work efficiency of application development. In low-code development, developers do not need to write a large amount of code, but complete the development through operations such as dragging and configuring on a graphical interface. These operations will be automatically converted into code files in JSON format by the platform and saved.
[0003] Since the JSON files generated by different low-code platforms vary in content and format, JSON files cannot be directly reused across different platforms. If you want to reuse a project from one low-code platform on another low-code platform, it requires manual conversion by developers. Developers need to analyze the various functions in the original platform project and then perform operations such as dragging components and configuring properties again on the target platform to gradually restore an application with the same functions.
[0004] The above conversion operations completely rely on the development experience of developers, so the possibility of errors is relatively high. Summary of the Invention
[0005] In view of the above problems, embodiments of the present invention are proposed to provide a method, apparatus, electronic device, and computer-readable storage medium for code conversion that can overcome or at least partially solve the above problems. [[ID=2,1]]
[0006] On the one hand, embodiments of the present invention provide a method for code conversion, the method comprising:
[0007] Obtain source code and generate a component tree according to the source code; wherein, the component tree includes a plurality of component nodes, and each component node corresponds to a segment of component code in the source code;
[0008] Perform code conversion on the component code corresponding to the component nodes of the component tree to obtain the target component code corresponding to the component nodes;
[0009] Determine the position information of the component nodes corresponding to the target component code in the component tree;
[0010] According to the position information corresponding to each component node of the component tree, splice the target component code corresponding to each component node to obtain target code.
[0011] Optionally, the code conversion of the component code corresponding to the component node of the component tree to obtain the target component code corresponding to the component node includes:
[0012] Determine the leaf nodes of the component tree; the component code corresponding to the leaf nodes is the smallest semantic unit;
[0013] Perform code conversion on the component code of the leaf nodes to obtain the target component code corresponding to the component code of the leaf nodes.
[0014] Optionally, the code conversion of the component code corresponding to the component node of the component tree to obtain the target component code corresponding to the component node includes:
[0015] Determine the upper-level nodes of the leaf nodes of the component tree;
[0016] Determine the source identifiers corresponding to the child nodes included in the upper-level nodes;
[0017] According to the source identifiers corresponding to the child nodes included in the upper-level nodes, replace the component code corresponding to the child nodes in the component code of the upper-level nodes;
[0018] Perform code conversion on the component code of the replaced upper-level nodes to obtain the target component code corresponding to the component code of the upper-level nodes.
[0019] Optionally, the component code of the replaced upper-level nodes includes a code part and the source identifier of the child node; the code conversion of the component code of the replaced upper-level nodes to obtain the target component code corresponding to the component code of the upper-level nodes includes:
[0020] Perform code conversion on the code part in the component code of the upper-level nodes to obtain the target code part corresponding to the code part;
[0021] Determine the target identifier corresponding to the source identifier of the child node according to the source identifier of the child node in the component code of the upper-level nodes;
[0022] Obtain the target component code corresponding to the upper-level node according to the target code part corresponding to the code part and the target identifier corresponding to the source identifier of the child node.
[0023] Optionally, the determining the target identifier corresponding to the source identifier of the child node according to the source identifier of the child node in the component code of the upper-level nodes includes:
[0024] Determine the target identifier corresponding to the target component code of the child node;
[0025] Determine the mapping relationship between the source identifier of the child node and the target identifier of the child node;
[0026] According to the mapping relationship between the source identifier of the child node and the target identifier of the child node, determine the target identifier corresponding to the source identifier of the child node.
[0027] Optionally, the generating the component tree according to the source code includes:
[0028] Determine the component code in the source code and determine the nesting relationship between the component codes;
[0029] According to the nesting relationship between the component codes, determine the component tree.
[0030] Optionally, the determining the target identifier corresponding to the source identifier of the child node in the component code of the upper-level node according to the mapping relationship between the source identifier of the child node and the target identifier of the child node includes:
[0031] Replace the source identifier of the child node with a source placeholder;
[0032] According to the mapping relationship between the source identifier of the child node and the target identifier of the child node, perform code conversion on the source placeholder of the child node to obtain the target placeholder of the child node;
[0033] Replace the target placeholder with the target identifier of the child node.
[0034] Optionally, the method further includes:
[0035] Identify the comment part in the source code;
[0036] According to the position of the component node corresponding to the comment part in the component tree, determine the comment position information of the comment part;
[0037] According to the comment position information, add the comment part to the target code.
[0038] Optionally, the performing code conversion on the component code corresponding to the component node of the component tree to obtain the target component code corresponding to the component node includes:
[0039] Perform code conversion on the component code corresponding to the component node of the component tree through a pre-trained code conversion model to obtain the target component code corresponding to the component node.
[0040] Optionally, the code conversion model is trained in the following manner:
[0041] Obtain the training code and the verification code;
[0042] Input the training code into the code conversion model to obtain the converted code output by the code conversion model;
[0043] Determine the loss value according to the verification code and the converted code;
[0044] Iteratively adjust the parameters of the code conversion model according to the loss value to complete the model training.
[0045] On the other hand, an embodiment of the present invention provides a code conversion device, and the device includes:
[0046] A component tree generation module, configured to obtain the source code and generate a component tree according to the source code; wherein, the component tree includes a plurality of component nodes, and each component node corresponds to a section of component code in the source code;
[0047] A code conversion module, configured to perform code conversion on the component code corresponding to the component nodes of the component tree to obtain the target component code corresponding to the component nodes;
[0048] A node position determination module, configured to determine the position information of the component nodes corresponding to the target component code in the component tree;
[0049] A code splicing module, configured to splice the target component codes corresponding to the respective component nodes according to the position information corresponding to the respective component nodes of the component tree to obtain the target code.
[0050] On the other hand, an embodiment of the present invention provides an electronic device, including: a processor, a memory, and a computer program stored on the memory and capable of running on the processor, and when the computer program is executed by the processor, the steps of the above-mentioned code conversion method are implemented.
[0051] On the other hand, an embodiment of the present invention provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the above-mentioned code conversion method are implemented.
[0052] The embodiments of the present invention include the following advantages:
[0053] In an embodiment of the present invention, a component tree is generated according to source code. The component tree includes multiple component nodes, and each component node corresponds to a segment of component code in the source code. Then, the component code corresponding to the component node is subjected to code conversion to obtain the target component code corresponding to each component node. According to the position information of the component node in the component tree, the target component codes corresponding to the component nodes are spliced to obtain the final target code. In this way, the long source code can be decomposed into component codes corresponding to each component node, the component codes are subjected to code conversion, and then the target code is spliced according to the component tree structure, reducing the possibility of errors in code conversion caused by too long code. Description of the Drawings
[0054] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for the description of the embodiments of the present invention will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present invention, and those of ordinary skill in the art can also obtain other drawings according to these drawings without creative efforts.
[0055] Figure 1 is a flowchart of the steps of a code conversion method provided by an embodiment of the present invention;
[0056] Figure 2 is a structural block diagram of a code conversion device provided by an embodiment of the present invention. Detailed Embodiments
[0057] To make the above objects, features, and advantages of the present invention more obvious and understandable, the present invention will be further described in detail below with reference to the drawings and specific embodiments.
[0058] A low-code platform is a development tool for quickly building application programs through a graphical interface, visual drag-and-drop components, and pre-set modules. Its aim is to lower the programming threshold, enabling non-professional developers to participate in application development, while also greatly improving the work efficiency of application development. In low-code development, developers do not need to write a large amount of code, but instead complete development through operations such as drag-and-drop and configuration on a graphical interface. These operations will be automatically converted into JSON format files by the platform and saved.
[0059] Due to the lack of a unified standard, the JSON files generated by different low-code platforms vary in content and format, and there are obstacles to project migration and conversion between different platforms. If you want to reuse a project from one low-code platform on another low-code platform, it requires developers to perform manual reproduction. Developers analyze the various functions in the original platform project and then perform operations such as dragging components and configuring properties again on the target platform to gradually restore an application with the same functions. However, the above operations completely rely on the development experience of developers, so the possibility of errors is relatively high.
[0060] The conversion of JSON files between two low-code platforms is essentially the conversion between two sequential texts, which is formally similar to the translation conversion between two languages. Therefore, it is considered to introduce machine translation technology based on neural networks to achieve code conversion. However, machine translation is essentially a one-to-one translation conversion between short sentences in two languages. Even for longer texts, the long text is split into multiple short sentences according to full stops, and then each short sentence is translated in turn. That is, machine translation is essentially the conversion between two short sequences.
[0061] However, for the JSON files of low-code platforms, the number of lines of their code texts is basically above the order of a hundred lines, and it includes a large number of various symbols such as parentheses, colons, quotation marks, spaces, and line breaks. That is, the sequence corresponding to the JSON file code is an extremely long sequence with a length exceeding a thousand, and the machine translation model cannot directly translate such an ultra-long sequence. In addition, the semantic structure of the JSON file code is not the same as that of natural language. If, according to the division logic of natural language, the position of a certain symbol (such as a full stop or other symbol) is determined by traversing the text in the order from the beginning to the end, and multiple short sequences are obtained by using this symbol as the delimiter, the logical relationship of the code will be lost, and the semantics of the code will be destroyed, resulting in incorrect conversion.
[0062] The present invention generates a component tree according to the source code of the JSON file. The component tree includes multiple component nodes, and each component node corresponds to a section of component code in the source code. That is, according to the tree structure, the code of the ultra-long sequence is divided into short sequences that retain the logical relationship, and then these short sequences are subjected to code conversion, and according to the positions of the component nodes in the component tree, these short sequences are spliced to obtain the complete target code. Thus, the code can be converted more correctly, and the possibility of code conversion errors caused by too long code is reduced.
[0063] Figure 1 It is a step flowchart of a code conversion method provided by an embodiment of the present invention.
[0064] As Figure 1 shown, the method may specifically include the following steps:
[0065] Step 101, obtain the source code, and generate a component tree according to the source code.
[0066] Wherein, the component tree includes multiple component nodes, and each component node corresponds to a section of component code in the source code.
[0067] The tree structure is an important non-linear data structure in computer applications. It mimics the trees in nature and is composed of nodes and edges. The node types include: root node, parent node, child node, and leaf node. Specifically, the root node refers to the topmost node of the tree. The root node has no parent node and is the starting point of the entire tree. A tree has exactly one root node. The parent node refers to the node in the tree that has child nodes. A tree can have multiple parent nodes, and a parent node can have multiple child nodes. Each non-root node has exactly one parent node. The child node refers to the lower-level node directly pointed to by the parent node. The leaf node refers to the node that has no child nodes and is located at the end of the tree.
[0068] The JSON file code can be regarded as a tree structure composed of layers of "{}". The component tree in the embodiments of the present invention is obtained by dividing the source code into a tree structure with the component code that can represent a complete component as the unit. Each component node represents a corresponding segment of component code in the source code. Therefore, the hierarchical relationship between component nodes corresponds one-to-one with the nesting relationship between component codes.
[0069] As an example, if the component code A' corresponding to node A includes the component code B' corresponding to node B, then node A in the component tree is the parent node of node B. If there is exactly one component corresponding to the component code in the component code B', that is, no other components are nested in the component code B', then node B is a leaf node of the component tree.
[0070] In some embodiments, step 101 specifically includes the following sub-steps:
[0071] Sub-step S11, determining the component code in the source code and determining the nesting relationship between the component codes.
[0072] The component code refers to the code in the source code that can represent one or more complete components. By searching for the component name in the code and then determining the code included and only included by the "{}" that contains it as the component code of the component.
[0073] As an example, as shown in the following code, by traversing the code, when detecting ""type":"Page"", it is determined that there is a component A representing a page. Then, the first "{" symbol before ""type":"Page"" and closest to it, that is, the first "{" in the following code, is determined as the starting part of the component code of component A. Then, the "}" symbol corresponding to this "{" is determined, that is, the last "}" in the following code, as the ending part of the component code of component A. That is, the code from the determined "{" to "}" is the component code of component A.
[0074]
[0075] Continue to traverse the code. When detecting ""type":"Text"", it is determined that there is a component B representing a text box, and according to the same method, the code encompassed by the "{}" that includes and only includes component B is determined, that is, the component code of component B as shown below.
[0076]
[0077] In addition, by traversing the code, it can also be detected that the component code of component B is located within the component code of component A, that is, the nesting relationship between component A and component B can be determined.
[0078] Sub-step S12, determine the component tree according to the nesting relationship between the component codes.
[0079] The component tree is composed of component nodes, and each component node corresponds to a section of component code in the source code. The hierarchical relationship between component nodes corresponds one-to-one with the nesting relationship between component codes. The nesting relationship of component codes refers to the hierarchical inclusion structure between different components. By the nesting relationship between component codes, the hierarchical relationship between the corresponding component nodes is determined, so that the component tree can be constructed.
[0080] As an example, after determining that the nesting relationship between component A and component B in the component code is that component A includes component B, it can be determined that the component node of component A is the parent node of the component node of component B, and thus the component tree is constructed according to the hierarchical relationship between component nodes.
[0081] Step 102, perform code conversion on the component code corresponding to the component nodes of the component tree to obtain the target component code corresponding to the component nodes;
[0082] Dividing the long sequence of code into short sequences of component codes according to the component tree and then performing code conversion on these short sequences reduces the possibility of errors in code conversion caused by overly long code.
[0083] It should be noted that most of the component codes divided according to the component tree are short sequences, but due to retaining the logical relationship between component codes, that is, there is a nesting relationship between component codes, the sequence lengths of some component codes are still long. For example, the component code corresponding to the root node of the component tree is actually the code of the entire file. In order to correctly perform code conversion on these component codes, the present invention introduces a source identifier. By replacing the component code of the child node included in the component code of the parent node with the source identifier, the length of the component code of the parent node is shortened, so as to facilitate code conversion.
[0084] In some embodiments, step 102 specifically includes the following sub-steps:
[0085] Sub-step S21: Through the pre-trained code conversion model, perform code conversion on the component code corresponding to each of the component nodes to obtain the target component code corresponding to each of the component nodes.
[0086] As an example, adopt a neural network machine translation model based on Transformer, and construct a data set as training data to train the code conversion model.
[0087] In some embodiments, in order to avoid the overfitting problem caused by an overly large model, adopt the relatively small Transformer-Base model in Transformer. In other embodiments, it is also possible to adopt Transformer-Big, Transformer-Huge or other models to train the code conversion model, and the present invention does not limit this.
[0088] In some embodiments, the code conversion model is trained in the following manner:
[0089] (1) Obtain training code and obtain validation code.
[0090] In the preparation stage of model training, it is necessary to obtain training code as the training set and obtain validation code as the validation set.
[0091] As an example, separately construct an input box component on the low-code platform A, and also separately construct an input box component on the low-code platform B. Then, respectively obtain the JSON file codes of the two platforms as training code and validation code.
[0092] Obtain codes with the same component functions on different platforms as training code and validation code in the above manner. For components with the same function, the attributes can be changed to expand the code data. For example, for the same table component, the same changes are made on both platforms, such as changing the number of rows / columns and changing the table style, etc., to obtain more code data for this type of component.
[0093] In addition, there may be a situation where the component functions on different platforms do not exactly correspond one by one. For example, there is an advanced form component in the low-code platform A, while there is only a basic form component in the low-code platform B. Since the purpose of code conversion is to implement as similar functions as possible on different platforms, in this case, the advanced form component of the low-code platform A and the basic component of the low-code platform B are also used as objects for mutual conversion, and the corresponding codes are extracted for training, so as to avoid the component functions of the low-code platform A being completely unable to be converted to the low-code platform B.
[0094] (2) Input the training code into the code conversion model to obtain the converted code output by the code conversion model.
[0095] Input the preprocessed training code into the code conversion model. The code conversion model will use its internal complex neural network structure to deeply analyze and process the input training code, extract the key features and patterns therein, and generate the corresponding converted code based on this information.
[0096] (3) Determine the loss value according to the verification code and the converted code.
[0097] Compare the converted code output by the code conversion model with the pre-prepared verification code in multiple dimensions. Through a specific loss function, quantify the difference degree between the two, and then obtain the loss value. The size of the loss value can reflect the accuracy and effect of code conversion.
[0098] (4) Iteratively adjust the parameters of the code conversion model according to the loss value to complete model training.
[0099] According to the calculated loss value, adopt optimization methods such as the backpropagation algorithm to iteratively adjust the parameters of the code conversion model. By continuously optimizing the parameters, gradually reduce the loss value, make the converted code output by the model gradually approach the verification code, and finally complete the training of the model to achieve high-quality code conversion.
[0100] In some embodiments, step 102 specifically includes the following sub-steps:
[0101] Sub-step S31, determine the leaf nodes of the component tree.
[0102] Among them, the component code corresponding to the leaf node is the minimum semantic unit.
[0103] The minimum semantic unit refers to a component code that includes and only includes one component, that is, there are no other components nested in the component code corresponding to the minimum semantic unit. Therefore, the component node corresponding to the minimum semantic unit has no sub-nodes and is the leaf node of the component tree.
[0104] Sub-step S32, perform code conversion on the component code of the leaf node to obtain the target component code corresponding to the component code of the leaf node.
[0105] By determining the leaf nodes of the component tree, it is possible to determine the component code that includes only one component. The structure of this type of component code is simple, so it is possible to more accurately determine the code corresponding to this component on another platform, that is, the code conversion model can directly perform code conversion on it to obtain the corresponding target component code.
[0106] In some embodiments, step 102 specifically includes the following sub-steps:
[0107] Sub-step S41, determining the upper-level nodes of the leaf nodes of the component tree.
[0108] By traversing the component tree, all the parent nodes, that is, the upper-level nodes of the leaf nodes, can be determined.
[0109] Sub-step S42, determining the source identifiers corresponding to the child nodes included in the upper-level nodes.
[0110] Sub-step S43, replacing the component code corresponding to the child nodes in the component code of the upper-level node according to the source identifiers corresponding to the child nodes included in the upper-level node.
[0111] To ensure the accuracy of code conversion, the structure of the component code should be kept as simple as possible. Therefore, the present invention adopts the method of assigning source identifiers to the component code, and replaces the component code of the child nodes included in the parent node with the source identifiers, so as to shorten the length of the component code of the parent node and simplify the structure of the component code of the parent node, so as to ensure the accuracy of code conversion.
[0112] In this process, first, code conversion is performed on the component code of the leaf nodes. Since the component code of the leaf nodes only contains the components themselves, code conversion can be directly performed. For the parent nodes of the leaf nodes, their component codes contain the component codes of the leaf nodes. Then, source identifiers can be assigned to the leaf nodes, and the component codes of the leaf nodes are replaced with the source identifiers. After replacement, the component codes of the parent nodes only contain one component of themselves and the source identifiers of the leaf nodes. Therefore, both the length and the structure are simplified. On this basis, code conversion is performed on the component codes of the parent nodes, which can ensure the accuracy of code conversion.
[0113] As an example, the following is the component code of the parent node A, which includes the component code of the leaf node B.
[0114]
[0115]
[0116] Assign the source label "##Source 001##" to the leaf node B and perform replacement to obtain the component code of the parent node A after replacement, as shown below:
[0117]
[0118] Obviously, after replacement, the component code of the parent node A only contains one component of the component node A itself and the source identifier of the leaf node B, and the component code of the parent node A is simplified in both length and structure.
[0119] Similarly, if the component node A is a child node of the component node C and code conversion needs to be performed on the component code of the component node C, it is necessary to first assign a source identifier to the component node A, replace the component code of the component node A in the component code of the component node C, and obtain the component code of the replaced component node C, which only contains one component of the component node C itself and the source identifier of the child node A.
[0120] By analogy, source identifiers can be assigned to all child nodes in the component tree, and the component codes of all parent nodes can be replaced, so that the component codes of all parent nodes can be converted into a structure including the code of one component and the source identifier of the child node.
[0121] Sub-step S44: Perform code conversion on the component code of the upper-layer node after replacement to obtain the target component code corresponding to the component code of the upper-layer node.
[0122] As described above, after replacement with the source identifier of the child node, the component codes of all upper-layer nodes can be converted into a structure including the code of one component and the source identifier of the child node. On this basis, performing code conversion on the component codes of these upper-layer nodes can ensure the accuracy of code conversion.
[0123] In some embodiments, sub-step S44 includes the following specific steps:
[0124] Sub-step S441: Perform code conversion on the code part in the component code of the upper-layer node to obtain the target code part corresponding to the code part.
[0125] In some embodiments, the component code of the upper-layer node after replacement includes a code part and the source identifier of the child node. When performing code conversion, these two parts are processed separately.
[0126] For the code part in the component code of the upper-layer node, its conversion process is similar to that of the leaf node. The code part only represents one component and has a simple structure. Therefore, it is possible to relatively accurately determine the code corresponding to this component on another platform, that is, the code conversion model can directly perform code conversion on it to obtain the corresponding target code part.
[0127] Sub-step S442: Determine the target identifier corresponding to the source identifier of the child node according to the source identifier of the child node in the component code of the upper-layer node.
[0128] For the source identifier of the child node in the component code of the upper-layer node, the corresponding target identifier needs to be determined according to the source identifier of the child node, which will be described in detail later.
[0129] Sub-step S443: Obtain the target component code corresponding to the upper-layer node according to the target code part corresponding to the code part and the target identifier corresponding to the source identifier of the child node.
[0130] The target identifier of the child node can correspondingly obtain the target component code after code conversion of the child node. Therefore, according to the target code part corresponding to the code part and the target identifier corresponding to the source identifier of the child node, the target component code corresponding to the upper-layer node can be obtained.
[0131] In some embodiments, the above sub-step S442 includes the following specific steps:
[0132] Sub-step S4421: Determine the target identifier corresponding to the target component code of the child node.
[0133] During the code conversion process, the leaf nodes are first converted, and then the upper-layer nodes are converted level by level upwards. That is, when performing code conversion on a certain upper-layer node, the child nodes of this component node have completed code conversion, and the target component codes of the child nodes have been determined. Therefore, when performing code conversion on the upper-layer node, first a target identifier is assigned to the target component code of its child nodes for the code conversion of the upper-layer node.
[0134] Sub-step S4422: Determine the mapping relationship between the source identifier and the target identifier of the child node.
[0135] The source identifier is assigned to the component code of the child node, and the target identifier is assigned to the target component code of the child node. That is, each source identifier and each target identifier have a uniquely corresponding single child node. Therefore, the mapping relationship between the source identifier and the target identifier of the child node can be determined according to the child node.
[0136] Sub-step S4423: Determine the target identifier corresponding to the source identifier of the child node according to the mapping relationship between the source identifier and the target identifier of the child node.
[0137] Since the order of code conversion is from bottom to top, before performing code conversion on the upper-layer node, its child nodes have completed code conversion. Therefore, during the process of performing code conversion on the upper-layer node, directly determine the target identifier corresponding to the source identifier of the child node according to the mapping relationship between the source identifier and the target identifier of the child node, without having to perform code conversion on the code of the child nodes included therein again, improving the efficiency of code conversion.
[0138] In some embodiments, the above sub-step S4423 includes the following specific steps:
[0139] Sub-step S44231: Replace the source identifier of the sub-node with a source placeholder.
[0140] Similar to term protection in natural language translation technology, placeholder replacement can solve the problem of possible incorrect translation or loss during the model translation process.
[0141] Replacing the source identifier with a source placeholder can mark the position of the source identifier and prevent the code conversion model from misprocessing the source identifier.
[0142] Sub-step S44232: Perform code conversion on the source placeholder of the sub-node according to the mapping relationship between the source identifier and the target identifier of the sub-node to obtain the target placeholder of the sub-node.
[0143] Input the component code of the upper-layer node after replacement into the code conversion model for code conversion. Since the source identifier has been replaced by a placeholder, the code conversion model will not translate the source identifier, thus avoiding the problem of incorrect translation of the source identifier. During the conversion process, the source placeholder of the sub-node will be converted into the target placeholder of the target platform.
[0144] Sub-step S44233: Replace the target placeholder with the target identifier of the sub-node.
[0145] After the code part of the component code is converted, then replace the target placeholder back with the target identifier according to the preset dictionary. The preset dictionary is a pre-defined mapping table that stores the mapping relationship between the source identifier and the target identifier of the sub-node.
[0146] Step 103: Determine the position information of the component node corresponding to the target component code in the component tree;
[0147] During the process of constructing the component tree, the position information of each component node in the component tree can be determined. As an example, the position of component node A is the first node in the fourth layer, and the position of component node B is the first node in the fifth layer.
[0148] Step 104: Concatenate the target component codes corresponding to the respective component nodes of the component tree according to the position information corresponding to each component node of the component tree to obtain the target code.
[0149] For ease of understanding, as an example, when converting the project code of the source platform to the project code of the target platform, the following table can be determined according to steps 101 to 103:
[0150]
[0151] Due to space limitations, the component codes in the table are replaced with "{****}", and the component codes in actual applications are obtained according to the specific content in the source code. According to the above table, based on the position information corresponding to each component node of the component tree, that is, in accordance with the structure of the component tree, the nesting level and order of each segment of component code in the overall structure can be determined, so that the corresponding target component code can be traced and spliced, and finally the complete target code can be obtained.
[0152] In some embodiments, the method further includes:
[0153] Identifying the comment part in the source code; determining the comment position information of the comment part according to the position of the component node corresponding to the comment part in the component tree; and adding the comment part to the target code according to the comment position information.
[0154] In an actual application scenario, the project code usually includes a comment part. The comment part is non-executable text content in the program code used to explain the code function, logic, design idea or provide additional explanations. The comment part is of great significance and should be retained during the code conversion process.
[0155] In some embodiments, the comment part in the source code can be identified and confirmed through the comment identifier in the code.
[0156] In addition, since the comment part is included in the source code, when creating the component tree, the comment position information of the comment part can be determined according to the position of the component node corresponding to the comment part in the component tree.
[0157] In some embodiments, before performing code conversion on the component code corresponding to the component node, preprocessing can be performed to remove the comment part in the component code to avoid affecting the conversion effect of the code conversion model.
[0158] After completing the code conversion, when splicing the target component code, the comment part is added to the target code according to the comment position information, thus ensuring the integrity and accuracy of the comment part.
[0159] In an embodiment of the present invention, a component tree is generated based on source code. The component tree includes multiple component nodes, and each component node corresponds to a segment of component code in the source code. Then, code conversion is performed on the component code corresponding to the component node to obtain the target component code corresponding to each component node. According to the position information of the component node in the component tree, the target component codes corresponding to the component nodes are spliced to obtain the final target code. In this way, the long source code can be decomposed into component codes corresponding to each component node, code conversion is performed on the component codes, and then the target code is spliced according to the component tree structure, reducing the possibility of errors in code conversion caused by overly long code.
[0160] It should be noted that for method embodiments, for the sake of simple description, they are all expressed as a series of action combinations. However, those skilled in the art should know that the embodiments of the present invention are not limited by the described action sequence, because according to the embodiments of the present invention, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also know that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily essential to the embodiments of the present invention.
[0161] Figure 2 It is a structural block diagram of a code conversion device provided by an embodiment of the present invention.
[0162] As Figure 2 shown, a code conversion device provided by an embodiment of the present invention may specifically include the following modules:
[0163] A component tree generation module 201, configured to obtain source code and generate a component tree according to the source code; wherein, the component tree includes multiple component nodes, and each component node corresponds to a segment of component code in the source code;
[0164] A code conversion module 202, configured to perform code conversion on the component code corresponding to the component nodes of the component tree to obtain the target component code corresponding to the component nodes;
[0165] A node position determination module 203, configured to determine the position information of the component node corresponding to the target component code in the component tree;
[0166] A code splicing module 204, configured to splice the target component codes corresponding to the respective component nodes according to the position information corresponding to the respective component nodes of the component tree to obtain a target code.
[0167] In some embodiments, the code conversion module 202 includes the following sub-modules:
[0168] A leaf node determination sub-module, configured to determine the leaf nodes of the component tree; the component code corresponding to the leaf node is the smallest semantic unit.
[0169] A first code conversion sub-module, configured to perform code conversion on the component code of the leaf node to obtain the target component code corresponding to the component code of the leaf node.
[0170] In some embodiments, the code conversion module 202 includes the following sub-modules:
[0171] An upper layer node determination sub-module, configured to determine the upper layer nodes of the leaf nodes of the component tree;
[0172] A source identifier determination sub-module, configured to determine the source identifiers corresponding to the child nodes included in the upper layer node;
[0173] A code replacement sub-module, configured to replace the component code corresponding to the child node in the component code of the upper layer node according to the source identifier corresponding to the child node included in the upper layer node;
[0174] A second code conversion sub-module, configured to perform code conversion on the component code of the upper layer node after replacement to obtain the target component code corresponding to the component code of the upper layer node.
[0175] In some embodiments, the component code of the upper layer node after replacement includes a code part and the source identifier of the child node, and the second code conversion sub-module includes the following units:
[0176] A third code conversion unit, configured to perform code conversion on the code part in the component code of the upper layer node to obtain the target code part corresponding to the code part;
[0177] A first target identifier determination unit, configured to determine the target identifier corresponding to the source identifier of the child node according to the source identifier of the child node in the component code of the upper layer node;
[0178] A target component code determination unit, configured to obtain the target component code corresponding to the upper layer node according to the target code part corresponding to the code part and the target identifier corresponding to the source identifier of the child node.
[0179] In some embodiments, the target identifier determination unit includes the following sub-units:
[0180] A second target identifier determination sub-unit, configured to determine the target identifier corresponding to the target component code of the child node;
[0181] A mapping relationship determination sub-unit, configured to determine the mapping relationship between the source identifier of the child node and the target identifier of the child node;
[0182] The third target identifier determination subunit is configured to determine the target identifier corresponding to the source identifier of the child node according to the mapping relationship between the source identifier and the target identifier of the child node.
[0183] In some embodiments, the component tree generation module 301 includes the following sub-modules:
[0184] The component code determination sub-module is configured to determine the component code in the source code and determine the nesting relationship between the component codes;
[0185] The component tree determination sub-module is configured to determine the component tree according to the nesting relationship between the component codes.
[0186] In some embodiments, the third target identifier determination subunit is configured to replace the source identifier of the child node with a source placeholder; perform code conversion on the source placeholder of the child node according to the mapping relationship between the source identifier and the target identifier of the child node to obtain a target placeholder of the child node; and replace the target placeholder with the target identifier of the child node.
[0187] In some embodiments, the apparatus further includes the following modules:
[0188] The annotation recognition module is configured to recognize the annotation part in the source code;
[0189] The annotation position determination module is configured to determine the annotation position information of the annotation part according to the position of the component node corresponding to the annotation part in the component tree;
[0190] The annotation addition module is configured to add the annotation part to the target code according to the annotation position information.
[0191] In some embodiments, the code conversion module 202 includes the following sub-modules:
[0192] The fourth code conversion sub-module is configured to perform code conversion on the component code corresponding to each component node through a pre-trained code conversion model to obtain the target component code corresponding to each component node.
[0193] In some embodiments, the fourth code conversion sub-module includes the following units:
[0194] The code acquisition unit is configured to acquire training code and acquire verification code;
[0195] The converted code acquisition unit is configured to input the training code into the code conversion model to obtain the converted code output by the code conversion model;
[0196] A loss value determination unit, configured to determine a loss value according to the verification code and the conversion code;
[0197] A parameter adjustment unit, configured to iteratively adjust the parameters of the code conversion model according to the loss value to complete model training.
[0198] In an embodiment of the present invention, a component tree is generated according to source code. The component tree includes multiple component nodes, and each component node corresponds to a section of component code in the source code. Then, the component code corresponding to the component node is subjected to code conversion to obtain the target component code corresponding to each component node. According to the position information of the component node in the component tree, the target component codes corresponding to the component nodes are spliced to obtain the final target code. In this way, the long source code can be decomposed into component codes corresponding to each component node, the component codes are subjected to code conversion, and then the target code is spliced according to the component tree structure, reducing the possibility of errors in code conversion caused by overly long code.
[0199] For the apparatus embodiment, since it is basically similar to the method embodiment, the description is relatively simple. For related parts, refer to the partial description of the method embodiment.
[0200] An embodiment of the present invention further provides an electronic device, including: a processor, a memory, and a computer program stored on the memory and capable of running on the processor. When the computer program is executed by the processor, it implements each process of the above-mentioned code conversion method embodiment and can achieve the same technical effect. To avoid repetition, it will not be elaborated here.
[0201] An embodiment of the present invention further provides a computer-readable storage medium. A computer program is stored on the computer-readable storage medium. When the computer program is executed by the processor, it implements each process of the above-mentioned code conversion method embodiment and can achieve the same technical effect. To avoid repetition, it will not be elaborated here.
[0202] Each embodiment in this specification is described in a progressive manner. The key point of each embodiment is to illustrate the differences from other embodiments. For the same or similar parts among the embodiments, reference can be made to each other.
[0203] Those skilled in the art should understand that the embodiments of the present invention can be provided as a method, an apparatus, or a computer program product. Therefore, the embodiments of the present invention can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the embodiments of the present invention can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0204] Embodiments of the present invention are described with reference to the flowcharts and / or block diagrams of methods, terminal devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram can be implemented by computer program instructions, and the combination of processes and / or blocks in the flowchart and / or block diagram can also be implemented. These computer program instructions can be provided to the processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing terminal devices to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal devices generate a device for implementing the functions specified in one process Figure 1 one process or multiple processes and / or blocks Figure 1 one block or multiple blocks.
[0205] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing terminal device to work in a specific manner, so that the instructions stored in the computer-readable memory generate a manufactured article including an instruction device, and the instruction device implements the functions specified in one process Figure 1 one process or multiple processes and / or blocks Figure 1 one block or multiple blocks.
[0206] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device, so that a series of operation steps are executed on the computer or other programmable terminal device to generate a computer-implemented process, and thus the instructions executed on the computer or other programmable terminal device provide steps for implementing the functions specified in one process Figure 1 one process or multiple processes and / or blocks Figure 1 one block or multiple blocks.
[0207] Although the preferred embodiments of the embodiments of the present invention have been described, those skilled in the art can make additional changes and modifications to these embodiments once they learn the basic creative concepts. Therefore, the appended claims are intended to be construed as including the preferred embodiments and all changes and modifications falling within the scope of the embodiments of the present invention.
[0208] Finally, it should also be noted that in this text, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, article or terminal device comprising a series of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article or terminal device. Without further limitation, an element defined by the statement "comprising an..." does not exclude the presence of additional identical elements in the process, method, article or terminal device comprising the said element.
[0209] The above has introduced in detail a method, apparatus, electronic device and computer-readable storage medium for code conversion provided by the present invention. Specific examples are used in this text to elaborate on the principle and implementation manner of the present invention. The description of the above embodiments is only used to help understand the method and its core idea of the present invention; at the same time, for those of ordinary skill in the art, according to the idea of the present invention, there will be changes in the specific implementation manner and application scope. In summary, the content of this specification should not be construed as a limitation to the present invention.
Claims
1. A method for code conversion, characterized in that, The method includes: Obtain the source code and generate a component tree according to the source code; wherein, the component tree includes a plurality of component nodes, and each component node corresponds to a segment of component code in the source code; Perform code conversion on the component code corresponding to the component nodes of the component tree to obtain the target component code corresponding to the component nodes; Determine the position information of the component nodes corresponding to the target component code in the component tree; According to the position information corresponding to each component node of the component tree, splice the target component code corresponding to each component node to obtain the target code.
2. The method for converting the code according to claim 1, characterized in that, The performing code conversion on the component code corresponding to the component nodes of the component tree to obtain the target component code corresponding to the component nodes includes: Determine the leaf nodes of the component tree; the component code corresponding to the leaf nodes is the minimum semantic unit; Perform code conversion on the component code of the leaf nodes to obtain the target component code corresponding to the component code of the leaf nodes.
3. The method for converting the code according to claim 2, characterized in that, The performing code conversion on the component code corresponding to the component nodes of the component tree to obtain the target component code corresponding to the component nodes includes: Determine the upper-level nodes of the leaf nodes of the component tree; Determine the source identifiers corresponding to the child nodes included in the upper-level nodes; According to the source identifiers corresponding to the child nodes included in the upper-level nodes, replace the component code corresponding to the child nodes in the component code of the upper-level nodes; Perform code conversion on the replaced component code of the upper-level nodes to obtain the target component code corresponding to the component code of the upper-level nodes.
4. The code conversion method according to claim 3, wherein The replaced component code of the upper-level nodes includes a code part and the source identifier of the child nodes; The performing code conversion on the replaced component code of the upper-level nodes to obtain the target component code corresponding to the component code of the upper-level nodes includes: Perform code conversion on the code part in the component code of the upper-level nodes to obtain the target code part corresponding to the code part; According to the source identifier of the child nodes in the component code of the upper-level nodes, determine the target identifier corresponding to the source identifier of the child nodes; According to the target code part corresponding to the code part and the target identifier corresponding to the source identifier of the child nodes, obtain the target component code corresponding to the upper-level nodes.
5. The method for converting the code according to claim 4, wherein The determining the target identifier corresponding to the source identifier of the child nodes according to the source identifier of the child nodes in the component code of the upper-level nodes includes: Determine the target identifier corresponding to the target component code of the child nodes; Determine the mapping relationship between the source identifier and the target identifier of the child nodes; According to the mapping relationship between the source identifier and the target identifier of the child nodes, determine the target identifier corresponding to the source identifier of the child nodes.
6. The method for converting the code according to claim 1, wherein The generating a component tree according to the source code includes: Determine the component code in the source code and determine the nesting relationship between the component codes; Determine the component tree according to the nesting relationship between the component codes.
7. The method for converting the code according to claim 5, wherein Determining, according to the mapping relationship between the source identifier and the target identifier of the sub-node, the target identifier corresponding to the source identifier of the sub-node in the component code of the upper-layer node includes: Replacing the source identifier of the sub-node with a source placeholder; Performing code conversion on the source placeholder of the sub-node according to the mapping relationship between the source identifier and the target identifier of the sub-node to obtain a target placeholder of the sub-node; Replacing the target placeholder with the target identifier of the sub-node.
8. The method for converting the code according to claim 1, wherein The method further includes: Identifying the comment part in the source code; Determining the comment position information of the comment part according to the position of the component node corresponding to the comment part in the component tree; 9. The conversion method of the code according to claim 1, wherein 10. The method for converting the code according to claim 9, characterized in that, 11. A code conversion device, characterized in that, 12. An electronic device, characterized in that, 13. A computer-readable storage medium, characterized in that,