Identifier type determination method and device, electronic equipment, medium and program product
By determining the identifier and its context in the target code, and using the identification model to identify unknown types and other identifiers, the problem of low accuracy in identification type determination is solved, and the accuracy and efficiency of software maintenance is improved.
Patent Information
- Application Number
- CN202411823898.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-11
- Publication Date
- 2025-04-08
- Estimated Expiration
- 2044-12-11
AI Technical Summary
In the prior art, the accuracy of determining the identifier type is low, resulting in more misjudgment and affecting the complexity of software maintenance.
By determining multiple identifiers and their contexts in the object code, the type of identifiers is initially determined, and the identification model is used to identify identifiers of unknown types, union types and object classes, thereby determining their specific type.
Improve the accuracy of identification of identifier types, reduce the overall misjudgment rate of identifiers in the code, and simplify the software maintenance process.
Smart Images

Figure CN119781748A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data processing, and in particular to an identifier type determination method, device, electronic device, medium and program product. Background Art
[0002] As a dynamic programming language, Javascript is widely used in browsers and servers due to its flexibility. One of the greatest flexibility of Javascript is that identifiers do not need to be defined with types, and the specific type is determined dynamically at runtime. This feature makes type inference, automatic completion, error checking, etc. impossible to apply during coding, making the maintenance of large software very complicated.
[0003] In order to solve the problem of difficult software maintenance, TypeScript is set up. TypeScript is a superset of Javascript. TypeScript supports the determination of identifier types and provides rich features such as interfaces, classes, and generics. The determination of identifier types undoubtedly reduces the complexity of software development. However, to use this feature, it is necessary to make an explicit declaration of the identifier type. An important prerequisite for explicit declaration is to determine the type, that is, to infer the type of the identifier. Infer whether it is a native class such as string, number, or a complex type such as a user-defined class.
[0004] In the exemplary technology, the code is input into the model to obtain the type of the identifier output by the model. When the type of the identifier output by the model is a probabilistic result, the probabilistic result may be misjudged, resulting in low accuracy in determining the type of the identifier. Summary of the invention
[0005] The present application provides a method, device, electronic device, medium and program product for determining an identifier type, which solves the problem of low accuracy in determining the identifier type.
[0006] In a first aspect, the present application provides a method for determining an identifier type, the method comprising:
[0007] determining a plurality of first identifiers and corresponding identifier contexts in the target code;
[0008] Determining, according to the identifier context, a first type to which the first identifier belongs;
[0009] Determine a second identifier and a third identifier in each of the first identifiers according to a first type of the first identifier, wherein the first type of the second identifier is an unknown type, a union type or an object class, and the first type of the third identifier is a native class;
[0010] The type of each second identifier is identified based on the identification model to obtain the second type to which the second identifier belongs, where the second type is a public library class or a user-defined class.
[0011] In some embodiments, the identifying the type of each of the second identifiers based on the identification model includes:
[0012] determining a fourth identifier among each of the second identifiers, the fourth identifier being used to indicate the second identifier of an object class;
[0013] determining a frequency of the fourth identifier, the frequency comprising a reference frequency or a definition frequency;
[0014] The second identifier of unknown type, the second identifier of joint type, the fourth identifier and the frequency corresponding to the fourth identifier are input into the recognition model to obtain the second type to which each second identifier belongs.
[0015] In some embodiments, determining the frequency of the fourth identifier comprises:
[0016] Constructing a target feature vector corresponding to the fourth identifier, and determining a similarity between the target feature vector and a preset feature vector;
[0017] In response to the similarity being greater than a preset similarity, the frequency associated with the identifier corresponding to the preset feature vector is used as the frequency of the fourth identifier.
[0018] In some embodiments, determining a plurality of first identifiers and corresponding identifier contexts in the target code includes:
[0019] Parsing the target code to obtain an abstract syntax tree;
[0020] A first identifier is extracted from the abstract syntax tree, and an identifier context of the first identifier is constructed according to a statement or expression corresponding to the first identifier in the abstract syntax tree.
[0021] In some embodiments, constructing an identifier context of the first identifier according to a statement corresponding to the first identifier in the abstract syntax tree includes:
[0022] In response to the statement corresponding to the first identifier in the abstract syntax tree being a declaration statement, obtaining a character located on the left side of the first identifier in the abstract syntax tree;
[0023] In response to the character on the right side of the first identifier being a constant value or a single variable, an identifier context corresponding to the first identifier is constructed according to the characters on the left side and the characters on the right side.
[0024] In some embodiments, after obtaining the character located on the left side of the first identifier, the method further includes:
[0025] In response to the character on the right side of the first identifier not being a fixed value and not being a single variable, constructing an identifier context corresponding to the first identifier according to the character on the left side
[0026] In some embodiments, constructing the identifier context of the first identifier according to the expression corresponding to the first identifier in the abstract syntax tree includes:
[0027] In response to the expression corresponding to the first identifier in the abstract syntax tree being an object expression or an index expression, obtaining a root function corresponding to the first identifier;
[0028] An identifier context corresponding to the first identifier is constructed according to the root function.
[0029] In some embodiments, determining the first type to which the first identifier belongs according to the identifier context includes:
[0030] In response to the identifier context containing a field indicating an object fetch operation, the object class is determined to be the first type of the first identifier.
[0031] In some embodiments, determining the first type to which the first identifier belongs according to the identifier context includes:
[0032] In response to the identifier context containing a field representing a data value operation, determining a native class as a first type to which the first identifier belongs;
[0033] In response to the identifier context containing a field representing an assignment operation, determining an assignment object according to the field of the assignment operation;
[0034] According to the type of the assignment object, a first type to which the first identifier belongs is determined.
[0035] In some embodiments, determining the first type to which the first identifier belongs according to the type to which the assignment object belongs includes:
[0036] In response to the type to which the assignment object belongs being a single type, determining the type to which the assignment object belongs to be the first type to which the first identifier belongs;
[0037] In response to the type to which the assignment object belongs being multiple types, determining the union type to be the first type to which the first identifier belongs;
[0038] In response to the type to which the assignment object belongs being an unknown type, it is determined that the first type to which the first identifier belongs is an unknown type.
[0039] In some embodiments, before identifying the type of each second identifier based on the recognition model, the method further includes:
[0040] Acquire a plurality of training samples, wherein each of the training samples comprises a training identifier and a label, wherein the label is used to indicate that the training identifier is a user-defined class or a public library class;
[0041] The preset model is trained according to each of the training samples to obtain the recognition type.
[0042] In a second aspect, the present application provides an identifier type determination device, comprising:
[0043] A first determination module, configured to determine a plurality of first identifiers and corresponding identifier contexts in a target code;
[0044] A second determination module, configured to determine a first type to which the first identifier belongs according to the identifier context;
[0045] A third determination module, configured to determine a second identifier and a third identifier from each of the first identifiers according to a first type of the first identifier, wherein the first type of the second identifier is an unknown type, a union type or an object class, and the first type of the third identifier is a native class;
[0046] The identification module is used to identify the type of each second identifier based on the identification model to obtain the second type to which the second identifier belongs, where the second type is a public library class or a user-defined class.
[0047] In a third aspect, the present application provides an electronic device, comprising: a processor, and a memory and a communication interface communicatively connected to the processor;
[0048] The communication interface is used to communicate with other communication devices;
[0049] The memory is used to store computer-executable instructions;
[0050] The processor is used to execute the computer-executable instructions stored in the memory to implement the identifier type determination method provided in the first aspect.
[0051] In a fourth aspect, the present application provides a computer-readable storage medium, in which computer-executable instructions are stored. When the computer-executable instructions are executed by a processor, the identifier type determination method provided in the first aspect is implemented.
[0052] In a fifth aspect, the present application provides a computer program product, including a computer program, which, when executed by a processor, implements the identifier type determination method provided in the first aspect.
[0053] The identifier type determination method, device, electronic device, medium and program product provided in the present application first determine the identification class of the native class through the identifier context corresponding to the identifier, and for identifiers of unknown types, object classes, and union types that are difficult to identify as specific types, they are identified through a model. Since there is no misjudgment of the simple native type, the overall misjudgment rate of identifiers in the code is reduced, thereby improving the accuracy of determining the identifier type. BRIEF DESCRIPTION OF THE DRAWINGS
[0054] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0055] Figure 1 A schematic diagram of the scenario involved in the method for determining the identifier type of this application;
[0056] Figure 2 The following is a schematic diagram of the steps of the method for determining the identifier type provided in the embodiment of the present application. Figure 1 ;
[0057] Figure 3 The following is a schematic diagram of the steps of the method for determining the identifier type provided in the embodiment of the present application. Figure 2 ;
[0058] Figure 4 The following is a schematic diagram of the steps of the method for determining the identifier type provided in the embodiment of the present application. Figure 3 ;
[0059] Figure 5 The following is a schematic diagram of the steps of the method for determining the identifier type provided in the embodiment of the present application. Figure 4 ;
[0060] Figure 6 The following is a schematic diagram of the steps of the method for determining the identifier type provided in the embodiment of the present application. Figure 5 ;
[0061] Figure 7 A schematic diagram of a program module of an identifier type determination device provided in an embodiment of the present application;
[0062] Figure 8 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present application.
[0063] The above drawings have shown clear embodiments of the present application, which will be described in more detail later. These drawings and text descriptions are not intended to limit the scope of the present application in any way, but to illustrate the concept of the present application to those skilled in the art by referring to specific embodiments. DETAILED DESCRIPTION
[0064] In order to make the purpose, technical scheme and advantages of the embodiments of the present application clearer, the technical scheme in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present application. In addition, although the disclosure in the present application is introduced according to one or several exemplary examples, it should be understood that each aspect of these disclosures can also constitute a complete implementation method separately.
[0065] It should be noted that the brief description of terms in this application is only for the convenience of understanding the embodiments described below, and is not intended to limit the embodiments of this application. Unless otherwise specified, these terms should be understood according to their common and usual meanings.
[0066] In addition, the terms "include" and "have" and any variations thereof are intended to cover but not exclude inclusion, for example, a product or device comprising a list of components is not necessarily limited to those components expressly listed but may include other components not expressly listed or inherent to such products or devices.
[0067] The term "module" used in the embodiments of the present application refers to any known or later developed hardware, software, firmware, artificial intelligence, fuzzy logic, or a combination of hardware and / or software code that can perform the functions associated with the element.
[0068] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with relevant laws, regulations and standards, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0069] As a dynamic programming language, Javascript is widely used in browsers and servers due to its flexibility. One of the greatest flexibility of Javascript is that identifiers do not need to be defined with types, and the specific type is determined dynamically at runtime. This feature makes type inference, automatic completion, error checking, etc. impossible to apply during coding, making the maintenance of large software very complicated.
[0070] In order to solve the problem of difficult software maintenance, TypeScript is set up. TypeScript is a superset of Javascript. TypeScript supports the determination of identifier types and provides rich features such as interfaces, classes, and generics. The determination of identifier types undoubtedly reduces the complexity of software development. However, to use this feature, it is necessary to make an explicit declaration of the identifier type. An important prerequisite for explicit declaration is to determine the type, that is, to infer the type of the identifier. Infer whether it is a native class such as string, number, or a complex type such as a user-defined class.
[0071] The inventor of the present application has found that when the code is input into the model, the type of the identifier output by the model is obtained. When the type of the identifier output by the model is a probability result, there may be misjudgment of the probability result, resulting in low accuracy in determining the type of the identifier.
[0072] The inventor of the present application therefore came up with the idea of first determining the identification class of the native class through the identifier context corresponding to the identifier, and identifying identifiers of unknown types, object classes, union types, etc. that are difficult to identify into specific types through a model. Since there is no misjudgment of a simple type like the native type, the overall misjudgment rate of identifiers in the code is reduced, thereby improving the accuracy of determining the identifier type.
[0073] Reference Figure 1 , Figure 1 Schematic diagram of the application scenario of the identifier type determination method of the present application. The identifier type determination device 100 obtains the target code, and obtains the first identifier and the corresponding identifier context in the target code. The identifier type determination device 100 determines the first type to which the first identifier belongs based on the identifier context, and determines the third identifier of the native class in each first identifier based on the first type, and determines the second identifier of the unknown type, union type and object class. The identifier type determination device 100 is provided with a recognition model, and the second identifier of complex types such as unknown types, union types and object classes is recognized by the recognition model, so as to determine the second type of each second identifier, and the second type is a public library class or a user-defined class. The device outputs the first type of the third identifier and the second type of the second identifier.
[0074] The following Figure 1The technical solutions shown in the present application are described in detail in conjunction with specific embodiments. It should be noted that the following embodiments can exist independently or in combination with each other, and the same or similar contents will not be described repeatedly in different embodiments.
[0075] Reference Figure 2 , Figure 2 Schematic diagram of the process of determining the identifier type provided in the embodiment of the present application Figure 1 , the identifier type determination method includes:
[0076] Step S201, determining a plurality of first identifiers and corresponding identifier contexts in the target code.
[0077] In this embodiment, the execution subject is an identifier type determination device. For the convenience of description, the term "device" is used below to refer to the identifier type determination device. The device may be a server or a terminal device with an identifier type determination function.
[0078] The device obtains the target code. The target code can be input by the user, that is, the user inputs the code file into the device, and the device obtains the target code.
[0079] After obtaining the target code, the device determines multiple identifiers in the target code, which are defined as first identifiers. An identifier refers to a name used to identify a variable, function, class, object or other programming element in a programming language. After obtaining each first identifier, the device determines an identifier context corresponding to the first identifier in the target code.
[0080] Exemplarily, the device inputs the target code into the construct() function, that is, inputs the target code into the brackets of the function, and then outputs each first identifier and the corresponding identifier context. For example, the device inputs the target code into the construct() function, uses a code parsing tool to obtain the data stream in the output information of the construct() function, and constructs the identifier context through the data stream.
[0081] Step S202: Determine the first type to which the first identifier belongs according to the identifier context.
[0082] After the identifier context is determined, a first type to which the first identifier belongs is determined through the identifier context.
[0083] In one example, the device determines a dependency from an identifier, and determines the type of the first identifier through the dependency. For example, the first identifier is x, and the dependency determined through the identifier context is: x->a, and a is a number type, then it is determined that the type of the first identifier x is also a number type, and the number type belongs to a native class. In addition, native classes also include string type, boolean type, and function type.
[0084] In another example, the identifier context is input into the brackets of the infer() function, and the output is the first type to which the first identifier corresponding to the identifier context belongs. The first type includes string, number, boolean, function, object, UNKNOWN, and UNION, among which string, number, boolean, and function belong to primitive classes, which are simple types that do not need to be clarified; object is an object class, UNKNOWN is an unknown type, and UNION is a union type. Object, UNKNOWN, and UNION are complex types that need to be clarified again.
[0085] In a sub-example, when the infer() function recognizes that the identifier context contains a field representing an object retrieval operation, object is returned, that is, the object class is determined to be the first type of the first identifier corresponding to the identifier context.
[0086] In another sub-example, when the infer() function recognizes that the identifier context contains a field representing a data value operation, string is returned, that is, the native class is taken as the first type to which the first identifier belongs. In addition, after obtaining the field of the data value operation, the field of the data value operation is parsed, and if the field of the data value operation represents a string concatenation operation, string is returned.
[0087] In another sub-example, the infer() function recognizes that the identifier context contains a field that represents an assignment operation, determines the assignment object based on the field of the assignment operation, and determines the first type to which the first identifier belongs based on the type to which the assignment object belongs. Exemplarily, when the type to which the assignment object belongs is a single type, the type to which the assignment type belongs is determined as the first type to which the first identifier belongs. For example, when the assignment object is a number, a string, or an object, the returned number, string, or object is used as the first type of the first identifier. When the type to which the assignment object belongs is multiple types, that is, there are multiple types to which the assignment object belongs, UNION is returned, that is, the first type to which the first identifier belongs is a union type. When the type of the assignment object cannot be determined, the type to which the assignment object belongs is an unknown type, and the unknown type is determined as the first type to which the first identifier belongs.
[0088] Step S203, determining a second identifier and a third identifier in each first identifier according to the first type of the first identifier, the first type of the second identifier is an unknown type, a union type or an object class, and the first type of the third identifier is a native class.
[0089] After the device determines the first type to which each first identifier belongs, the device needs to classify the first identifiers of unknown type, joint type and object class again.
[0090] Exemplarily, the device determines the second identifier and the third identifier in each first identifier based on the first type of each first identifier. The first type of the second identifier is an unknown type, a union type or an object class, and the third identifier is a first identifier of a native class.
[0091] Step S204: Identify the type of each second identifier based on the identification model to obtain the second type to which the second identifier belongs, where the second type is a public library class or a user-defined class.
[0092] The device is provided with a recognition model. The device recognizes the type of each second identifier through the recognition model to obtain the second type to which the second identifier belongs. The second type is a public library class or a user-defined class.
[0093] The recognition model is provided with a bidirectional recurrent neural network, which can process the identifier context well. The encoder EnCoder and decoder DeCoder of the bidirectional recurrent network can respectively encode and decode the identifier, so as to better identify the second type to which the second identifier belongs.
[0094] The device obtains multiple training samples, each of which includes a training identifier and a label, where the label is used to indicate that the training identifier is a user-defined class or a public library class. The device trains the preset model based on each training sample to obtain a recognition model. The specific training process of the recognition model is as follows:
[0095] 1. Data processing, obtain training identifiers and corresponding types, the types are public library classes or user-defined classes. The device downloads TypeScript files of multiple open source projects from the distributed platform, traverses each TypeScript file, and extracts the identifiers and corresponding types. When extracting identifiers, it is represented by {project name_path name_identifier name}; when extracting types, it contains two forms, declare:{type} and define:{type}, to distinguish whether it is a declaration or a definition. Save identifiers and types to different files respectively. Since identifiers include not only variables but also program keywords, the types corresponding to program keywords are set to a special "O".
[0096] 2. Clear the extracted types and remove the types defined in the original file to facilitate training and verification. When the identifier is followed by a ":", remove the ":", that is, remove the type after the colon, thereby removing the type.
[0097] 3. Process the extracted identifier type and remove the prefixes of declare and define in the type. Specifically, if the type is a native class, directly remove declare; if the type is a non-native class, perform type replacement using the classification method of public library classes and user-defined classes. For example, determine the frequency of the identifier. When the reference frequency of the identifier is greater than the first threshold, the type of the identifier is determined to be a public library class; when the definition frequency of the identifier is greater than the second threshold, the type of the identifier is determined to be a user-defined class.
[0098] 4. Code representation. This is convenient for subsequent models to train a single code file each time. The previously processed identifier files are taken out in order according to the dimension of {project name_path name} to form a complete code file. The {project name_path name_identifier name} and type files are processed as word vectors to obtain training samples.
[0099] 5. Train the model based on each training sample. Specifically, the full data set is divided into training set, test set and validation set in a ratio of 8:1:1. The input data is an identifier in the code, which is converted into a 200-dimensional vector after word vector processing and input into the model. The model consists of two layers, EnCoder and DeCoder, each of which is a bidirectional RNN network. Each unit in the RNN network is a GRU (Gated Recurrent Unit), 300 dimensions in the forward direction and 300 dimensions in the reverse direction. The vector output by DeCoder decoding is subjected to dropout processing to prevent overfitting, and then softmax processing is performed to obtain the type probability distribution. Finally, the cross entropy function is used to calculate the loss, and then the network is iteratively updated.
[0100] In this embodiment, by combining code static analysis (identifier context determines native class) and recognition model, more accurate identifier classification results can be obtained; at the same time, the object class is further subdivided into public library classes and user-defined classes at a finer granularity, and the application scenarios are more focused.
[0101] In this embodiment, the identification class of the native class is first determined by the identifier context corresponding to the identifier, and for identifiers of unknown types, object classes, union types, etc. that are difficult to identify as specific types, they are identified through a model. Since there is no misjudgment of a simple type like the native type, the overall misjudgment rate of identifiers in the code is reduced, thereby improving the accuracy of determining the identifier type.
[0102] Reference Figure 3 , Figure 3 Schematic diagram of the process of determining the identifier type for this application Figure 2 ,based on Figure 2 In the illustrated embodiment, step S204 includes:
[0103] Step S301: determine a fourth identifier from each second identifier, where the fourth identifier is used to indicate a second identifier of an object class.
[0104] In this embodiment, due to the particularity of the object class, it is impossible to directly determine whether the object class is a public library class or a user-defined class, so it is necessary to obtain the frequency of the first identifier belonging to the object class, and the frequency includes the reference frequency and the definition frequency. The reference frequency is, for example, the number of references or the frequency of references, and the definition frequency is, for example, the number of definitions or the frequency of definitions.
[0105] The device determines a fourth identifier from each of the second identifiers, the fourth identifier referring to the second identifier of the object class.
[0106] Step S302: determine the frequency of the fourth identifier, where the frequency includes a reference frequency or a definition frequency.
[0107] The device determines the frequency of the fourth identifier, the frequency includes a reference frequency or a definition frequency. In one example, the target code is associated with an attachment file, the attachment file contains the frequency of each identifier, and the device obtains the frequency corresponding to the fourth identifier from the attachment file in the attachment.
[0108] In another example, a plurality of preset identifiers are stored in the device, each preset identifier is associated with a frequency, the device determines a preset identifier similar to the fourth identifier, and the frequency associated with the preset identifier is used as the frequency of the fourth identifier. Exemplarily, the device constructs a target feature vector corresponding to the fourth identifier, and determines the similarity between the target feature vector and the preset feature vector, the preset feature vector being the feature vector of the preset identifier. When the similarity is greater than the preset similarity, the frequency associated with the preset feature vector corresponding to the preset feature vector is used as the frequency of the fourth identifier. The preset similarity can be any suitable value, for example, the preset similarity is 90%.
[0109] Step S303: input the second identifier of the unknown type, the second identifier of the joint type, the fourth identifier, and the frequency corresponding to the fourth identifier into the recognition model to obtain the second type to which each second identifier belongs.
[0110] After determining the frequency of the fourth identifier, the second identifier of the unknown type, the second identifier of the joint type, the fourth identifier, and the frequency corresponding to the fourth identifier are input into the recognition model to obtain the second type to which each second identifier belongs. The frequency of the fourth identifier is used to assist the recognition model in determining whether the fourth identifier belongs to a user-defined class or a public library class.
[0111] In this embodiment, by determining the frequency of the second identifier of the object class, the recognition model can more accurately determine whether the second identifier of the object class belongs to the public library class or the user-defined class.
[0112] Reference Figure 4 , Figure 4 Schematic diagram of the process of determining the identifier type for this application Figure 3 ,based on Figure 2 In the illustrated embodiment, step S201 includes:
[0113] Step S401, parsing the target code to obtain an abstract syntax tree.
[0114] In this embodiment, after obtaining the target code, the device parses the target code to obtain an abstract syntax tree. The identifier context of the first identifier can be quickly constructed through the abstract syntax tree.
[0115] Step S402: extract a first identifier from the abstract syntax tree, and construct an identifier context of the first identifier according to a statement or expression corresponding to the first identifier in the abstract syntax tree.
[0116] After obtaining the abstract syntax tree, the device extracts the first identifier from the abstract syntax tree, for example, using {current function name_identifier} as the first identifier. The device obtains the statement or expression where the first identifier is located in the abstract syntax tree, so that the identifier context of the first identifier can be constructed through the statement or expression.
[0117] In this embodiment, the device parses the target code to obtain an abstract syntax tree, extracts the first identifier in the abstract syntax tree, and determines the statement or expression corresponding to the first identifier in the abstract syntax tree, thereby quickly constructing the identifier context of the first identifier through the statement or expression.
[0118] Reference Figure 5 , Figure 5 Schematic diagram of the process of determining the identifier type for this application Figure 4 ,based on Figure 4 In the illustrated embodiment, step S402 includes:
[0119] Step S501: in response to the statement corresponding to the first identifier in the abstract syntax tree being a declaration statement, a character located on the left side of the first identifier is obtained in the abstract syntax tree.
[0120] Step S502: in response to the character on the right side of the first identifier being a fixed value or a single variable, construct an identifier context corresponding to the first identifier according to the characters on the left side and the characters on the right side.
[0121] In this embodiment, when the statement corresponding to the first identifier in the abstract syntax tree is a declaration statement, the character on the left side of the first identifier is obtained in the declaration statement.
[0122] When the character on the right side of the first identifier in the declaration statement is a fixed value or a single variable (single variable), an identifier context corresponding to the first identifier is constructed based on the characters on the left side and the characters on the right side.
[0123] When the characters on the right side of the first identifier in the declaration statement are not a definite value and are not a single variable, the identifier context corresponding to the first identifier is constructed based only on the characters on the left side.
[0124] In this embodiment, when the statement corresponding to the first identifier in the abstract syntax tree is a declaration statement, an identifier context corresponding to the first identifier is constructed based on characters on the left and right of the first identifier in the abstract syntax tree.
[0125] Reference Figure 6 , Figure 6 Schematic diagram of the process of determining the identifier type for this application Figure 5 ,based on Figure 4 In the illustrated embodiment, step S402 includes:
[0126] Step S601: In response to the expression corresponding to the first identifier in the abstract syntax tree being an object expression or an index expression, a root function corresponding to the first identifier is obtained.
[0127] Step S602: construct an identifier context corresponding to the first identifier according to the root function.
[0128] In this embodiment, when the expression corresponding to the first identifier in the abstract syntax tree is an object expression or an index expression, the device obtains the root function corresponding to the first identifier, thereby constructing the identifier context corresponding to the first identifier through the root function. Exemplarily, the device generates a root identifier corresponding to the object expression and the index expression. In addition, isAssigned or isIndexed of the first identifier is set to true as part of the identifier context.
[0129] In this embodiment, when the expression corresponding to the first identifier in the abstract syntax tree is an object expression or an index expression, an identifier context corresponding to the first identifier is constructed based on the root function.
[0130] Based on the contents described in the above embodiments, an identifier type determination device is also provided in the embodiments of the present application. Figure 7 , Figure 7 This is a schematic diagram of a program module of an identifier type determination device provided in an embodiment of the present application. In some implementations, the identifier type determination device 700 includes:
[0131] A first determination module 710, configured to determine a plurality of first identifiers and corresponding identifier contexts in a target code;
[0132] A second determination module 720, configured to determine a first type to which the first identifier belongs according to the identifier context;
[0133] A third determination module 730 is used to determine a second identifier and a third identifier from each of the first identifiers according to a first type of the first identifier, the first type of the second identifier being an unknown type, a union type or an object class, and the first type of the third identifier being a native class;
[0134] The identification module 740 is used to identify the type of each second identifier based on the identification model to obtain the second type to which the second identifier belongs, where the second type is a public library class or a user-defined class.
[0135] In some embodiments, the identifier type determination device 700 is specifically used to:
[0136] determining a fourth identifier among the second identifiers, the fourth identifier being used to indicate the second identifier of the object class;
[0137] determining a frequency of the fourth identifier, the frequency comprising a reference frequency or a definition frequency;
[0138] The second identifier of the unknown type, the second identifier of the joint type, the fourth identifier and the frequency corresponding to the fourth identifier are input into the recognition model to obtain the second type to which each second identifier belongs.
[0139] In some embodiments, the identifier type determination device 700 is specifically used to:
[0140] Constructing a target feature vector corresponding to the fourth identifier, and determining a similarity between the target feature vector and a preset feature vector;
[0141] In response to the similarity being greater than a preset similarity, the frequency associated with the identifier corresponding to the preset feature vector is used as the frequency of the fourth identifier.
[0142] In some embodiments, the identifier type determination device 700 is specifically used to:
[0143] Parse the target code to get the abstract syntax tree;
[0144] A first identifier is extracted from the abstract syntax tree, and an identifier context of the first identifier is constructed according to a statement or expression corresponding to the first identifier in the abstract syntax tree.
[0145] In some embodiments, the identifier type determination device 700 is specifically used to:
[0146] In response to the statement corresponding to the first identifier in the abstract syntax tree being a declaration statement, obtaining a character located to the left of the first identifier in the abstract syntax tree;
[0147] In response to the character on the right side of the first identifier being a fixed value or a single variable, an identifier context corresponding to the first identifier is constructed according to the characters on the left side and the characters on the right side.
[0148] In some embodiments, the identifier type determination device 700 is specifically used to:
[0149] In response to the character on the right side of the first identifier being not a constant and not a single variable, construct an identifier context corresponding to the first identifier based on the character on the left side
[0150] In some embodiments, the identifier type determination device 700 is specifically used to:
[0151] In response to the expression corresponding to the first identifier in the abstract syntax tree being an object expression or an index expression, obtaining a root function corresponding to the first identifier;
[0152] A corresponding identifier context for the first identifier is constructed according to the root function.
[0153] In some embodiments, the identifier type determination device 700 is specifically used to:
[0154] In response to the identifier context containing a field indicative of a get object operation, the object class is determined to be a first type of the first identifier.
[0155] In some embodiments, the identifier type determination device 700 is specifically used to:
[0156] In response to the identifier context containing a field representing a data value operation, determining the native class as a first type to which the first identifier belongs;
[0157] In response to the identifier context containing a field representing an assignment operation, determining an assignment object according to the field of the assignment operation;
[0158] According to the type of the assigned object, a first type to which the first identifier belongs is determined.
[0159] In some embodiments, the identifier type determination device 700 is specifically used to:
[0160] In response to the type to which the assignment object belongs being a single type, determining the type to which the assignment object belongs to be the first type to which the first identifier belongs;
[0161] In response to the type to which the assignment object belongs being multiple types, determining the union type as the first type to which the first identifier belongs;
[0162] In response to the type to which the assignment object belongs being an unknown type, it is determined that the first type to which the first identifier belongs is an unknown type.
[0163] In some embodiments, the identifier type determination device 700 is specifically used to:
[0164] Acquire multiple training samples, wherein each training sample includes a training identifier and a label, and the label is used to indicate that the training identifier is a user-defined class or a public library class;
[0165] The preset model is trained according to each training sample to obtain the recognition type.
[0166] It should be noted that the various steps in the identifier type determination method executed by the identifier type determination device refer to the above embodiments for details, and will not be described in detail here.
[0167] Furthermore, based on the contents described in the above embodiments, an electronic device is also provided in an embodiment of the present application, which includes at least one processor, and a communication interface and a memory to which the processor is communicatively connected; wherein the communication interface is used to communicate with other communication devices, and the memory stores computer-executable instructions; the above at least one processor executes the computer-executable instructions stored in the memory to implement each step in the identifier type determination method as described in the above embodiments.
[0168] In order to better understand the embodiments of the present application, refer to Figure 8 , Figure 8 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present application.
[0169] like Figure 8 As shown, the electronic device 800 of this embodiment includes: a processor 801, a memory 802, and a communication interface 804; wherein:
[0170] Memory 802, used to store computer-executable instructions;
[0171] The communication interface 804 is used to communicate with other communication devices;
[0172] The processor 801 is used to execute the computer-executable instructions stored in the memory to implement the various steps in the query optimization method described in the above embodiment.
[0173] Optionally, the memory 802 may be independent or integrated with the processor 801 .
[0174] When the memory 802 is independently provided, the device further includes a bus 803 for connecting the memory 802 , the communication interface 804 and the processor 801 .
[0175] An embodiment of the present application provides a computer-readable storage medium, in which computer-executable instructions are stored. When a processor executes the computer-executable instructions, each step of the identifier type determination method described in the above embodiment is implemented.
[0176] An embodiment of the present application provides a computer program product, including a computer program. When the computer program is executed by a processor, the computer program implements the various steps in the identifier type determination method described in the above embodiment.
[0177] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic, for example, the division of modules is only a logical function division, and there may be other division methods in actual implementation, such as multiple modules can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or modules, which can be electrical, mechanical or other forms.
[0178] The modules described as separate components may or may not be physically separated, and the components shown as modules may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0179] In addition, each functional module in each embodiment of the present application can be integrated into one processing unit, or each module can exist physically separately, or two or more modules can be integrated into one unit. The above-mentioned module-composed unit can be implemented in the form of hardware or in the form of hardware plus software functional units.
[0180] The above-mentioned integrated module implemented in the form of a software function module can be stored in a computer-readable storage medium. The above-mentioned software function module is stored in a storage medium, including a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to perform some steps of the methods of various embodiments of the present application.
[0181] It should be understood that the processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), etc. A general-purpose processor may be a microprocessor or any conventional processor, etc. The steps of the method disclosed in the application may be directly implemented as being executed by a hardware processor, or may be executed by a combination of hardware and software modules in the processor.
[0182] The memory may include high-speed memory, and may also include non-volatile storage, such as at least one disk memory, and may also be a USB flash drive, a mobile hard disk, a read-only memory, a magnetic disk or an optical disk, etc.
[0183] The bus may be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, the bus in the drawings of the present application is not limited to only one bus or one type of bus.
[0184] The storage medium may be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory, electrically erasable programmable read-only memory, erasable programmable read-only memory, programmable read-only memory, read-only memory, magnetic memory, flash memory, magnetic disk or optical disk. The storage medium may be any available medium that can be accessed by a general or special purpose computer.
[0185] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present application.
Claims
1. A method for determining an identifier type, characterized in that: include: determining a plurality of first identifiers and corresponding identifier contexts in the target code; Determining, according to the identifier context, a first type to which the first identifier belongs; Determine a second identifier and a third identifier in each of the first identifiers according to a first type of the first identifier, wherein the first type of the second identifier is an unknown type, a union type or an object class, and the first type of the third identifier is a native class; The type of each second identifier is identified based on the identification model to obtain the second type to which the second identifier belongs, where the second type is a public library class or a user-defined class.
2. The method for determining the identifier type according to claim 1, characterized in that: The identifying the type of each of the second identifiers based on the identification model includes: determining a fourth identifier among each of the second identifiers, the fourth identifier being used to indicate the second identifier of an object class; determining a frequency of the fourth identifier, the frequency comprising a reference frequency or a definition frequency; The second identifier of unknown type, the second identifier of joint type, the fourth identifier and the frequency corresponding to the fourth identifier are input into the recognition model to obtain the second type to which each second identifier belongs.
3. The method for determining the identifier type according to claim 2, characterized in that: The determining the frequency of the fourth identifier comprises: Constructing a target feature vector corresponding to the fourth identifier, and determining a similarity between the target feature vector and a preset feature vector; In response to the similarity being greater than a preset similarity, the frequency associated with the identifier corresponding to the preset feature vector is used as the frequency of the fourth identifier.
4. The method for determining the identifier type according to claim 1, characterized in that: The step of determining a plurality of first identifiers and corresponding identifier contexts in the target code comprises: Parsing the target code to obtain an abstract syntax tree; A first identifier is extracted from the abstract syntax tree, and an identifier context of the first identifier is constructed according to a statement or expression corresponding to the first identifier in the abstract syntax tree.
5. The method for determining the identifier type according to claim 4, characterized in that: The constructing an identifier context of the first identifier according to a statement corresponding to the first identifier in the abstract syntax tree includes: In response to the statement corresponding to the first identifier in the abstract syntax tree being a declaration statement, obtaining a character located on the left side of the first identifier in the abstract syntax tree; In response to the character on the right side of the first identifier being a constant value or a single variable, an identifier context corresponding to the first identifier is constructed according to the characters on the left side and the characters on the right side.
6. The method for determining the identifier type according to claim 5, characterized in that: After acquiring the character located on the left side of the first identifier, the method further includes: In response to the character on the right side of the first identifier being not a constant value and not a single variable, an identifier context corresponding to the first identifier is constructed according to the character on the left side.
7. The method for determining the identifier type according to claim 4, characterized in that: The constructing the identifier context of the first identifier according to the expression corresponding to the first identifier in the abstract syntax tree includes: In response to the expression corresponding to the first identifier in the abstract syntax tree being an object expression or an index expression, obtaining a root function corresponding to the first identifier; An identifier context corresponding to the first identifier is constructed according to the root function.
8. The method for determining the identifier type according to claim 1, characterized in that: The determining, according to the identifier context, the first type to which the first identifier belongs includes: In response to the identifier context containing a field indicating an object fetch operation, the object class is determined to be the first type of the first identifier.
9. The method for determining the identifier type according to claim 1, characterized in that: The determining, according to the identifier context, the first type to which the first identifier belongs includes: In response to the identifier context containing a field representing a data value operation, determining a native class as a first type to which the first identifier belongs; In response to the identifier context containing a field representing an assignment operation, determining an assignment object according to the field of the assignment operation; According to the type of the assignment object, a first type to which the first identifier belongs is determined.
10. The method for determining the identifier type according to claim 9, characterized in that: The determining, according to the type of the assignment object, the first type to which the first identifier belongs, includes: In response to the type to which the assignment object belongs being a single type, determining the type to which the assignment object belongs to be the first type to which the first identifier belongs; In response to the type to which the assignment object belongs being multiple types, determining the union type to be the first type to which the first identifier belongs; In response to the type to which the assignment object belongs being an unknown type, it is determined that the first type to which the first identifier belongs is an unknown type.
11. The method for determining the identifier type according to any one of claims 1 to 10, characterized in that: Before identifying the type of each second identifier based on the identification model, the method further includes: Acquire a plurality of training samples, wherein each of the training samples comprises a training identifier and a label, wherein the label is used to indicate that the training identifier is a user-defined class or a public library class; The preset model is trained according to each of the training samples to obtain the recognition type.
12. An identifier type determination device, characterized in that: include: A first determination module, configured to determine a plurality of first identifiers and corresponding identifier contexts in a target code; A second determination module, configured to determine a first type to which the first identifier belongs according to the identifier context; A third determination module, configured to determine a second identifier and a third identifier from each of the first identifiers according to a first type of the first identifier, wherein the first type of the second identifier is an unknown type, a union type or an object class, and the first type of the third identifier is a native class; The identification module is used to identify the type of each second identifier based on the identification model to obtain the second type to which the second identifier belongs, where the second type is a public library class or a user-defined class.
13. An electronic device, characterized in that: include: A processor, and a memory and a communication interface communicatively connected to the processor; The communication interface is used to communicate with other communication devices; The memory is used to store computer-executable instructions; The processor is used to execute the computer-executable instructions stored in the memory to implement the identifier type determination method according to any one of claims 1-11.
14. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are executed by a processor, the identifier type determination method according to any one of claims 1 to 11 is implemented.
15. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the identifier type determination method according to any one of claims 1 to 11 is implemented.
Citation Information
Patent Citations
Intelligent contract code completion method based on deep neural network and global cache
CN118092889A