An API recommendation method and device, electronic equipment and storage medium
By parsing the source code into an abstract syntax tree and constructing an API ternary dataset, converting it into a vector matrix, and calculating similarity to recommend API sequence data, the problem of low API recommendation accuracy in existing technologies is solved, achieving higher recommendation accuracy and efficiency.
Patent Information
- Application Number
- CN202411291124.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-14
- Publication Date
- 2026-02-24
- Estimated Expiration
- 2044-09-14
AI Technical Summary
Existing API recommendation methods suffer from a textual semantic gap between user queries and API function descriptions, as well as a knowledge gap between developers and API designers. This results in low recommendation accuracy when encountering uncommon or newly developed APIs, and also ignores the semantic information in the API source code.
By obtaining source code files, parsing them into abstract syntax trees, constructing API ternary datasets, and using an encoder to convert the data into vector matrices, constructing association matrices, calculating the similarity between user queries and API sequences and source code, and using a recommendation model to recommend API sequence data.
It improves the accuracy of API recommendations, enhances the correlation between API query vector matrix and API features, and improves the accuracy and efficiency of the recommendation model.
Smart Images

Figure CN119397002B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of intelligent recommendation technology, and particularly relates to an API recommendation method, device, electronic device, and storage medium. Background Technology
[0002] During software development, developers typically translate their desired functionalities into natural language queries and input them into search engines. Automated API recommendation technology can find relevant API methods based on user queries, reducing user time consumption and the possibility of errors.
[0003] One existing technology is the CLEAR (Contrastive Learning for API Recommendation) method, which uses Stack Overflow (SO) posts as middleware between user queries and APIs, employing an indirect recommendation strategy. The CLEAR method calculates the similarity between the query and the question titles of SO posts through an indirect recommendation strategy, indirectly finding the API sequence associated with the query. Conversely, a direct recommendation strategy directly calculates the similarity between the query and API features, allowing the model to directly retrieve the most relevant API sequence. The CLEAR model mainly consists of two parts. The first part is filtering candidate posts. CLEAR first constructs a dataset of <query, query-related titles, query-irrelevant titles> triples, and then trains a RoBERTa-based sentence embedding model using contrastive training techniques. When CLEAR recommends for user queries, it transforms the query and all collected titles into vector matrix representations, and finds the top 50 candidate posts most semantically relevant to the query by calculating the cosine similarity between the query vector matrix and each title vector matrix. The second part is re-ranking the candidate posts. CLEAR trains a semantic similarity re-ranking model, also based on RoBERTa, using joint embedding training. After CLEAR filters candidate posts, this re-ranking model recalculates the similarity between the query and the titles of these posts, re-ranking the relevance of these candidate posts. Finally, CLEAR extracts the API sequences from the accepted answers corresponding to these titles as the corresponding API recommendations.
[0004] Existing query-based API recommendation technologies need to address the textual semantic gap between user queries and API function descriptions, as well as the knowledge gap between developers and API designers. Existing indirect recommendation strategies rely on the coverage of collected question titles from developers. When the API a user needs is uncommon or newly developed, the model may fail to make a correct recommendation because it cannot find a corresponding title. Furthermore, current technologies ignore the semantic information contained in the API source code, resulting in low accuracy in API recommendations. Summary of the Invention
[0005] This invention provides an API recommendation method, apparatus, electronic device, and storage medium that can improve the accuracy of API recommendations.
[0006] To achieve the above objectives, the present invention provides an API recommendation method, comprising:
[0007] Obtain the source code file and use a compiler to parse the source code file into an abstract syntax tree;
[0008] The API source code is obtained by traversing the preset method name nodes in the abstract syntax tree, and an API ternary dataset is formed according to the API fully qualified name matching algorithm. The ternary dataset includes API query data, API sequence data and API source code.
[0009] The encoder is used to convert each data in the API three-dimensional data set into a vector matrix, resulting in the API query vector matrix, the API sequence vector matrix, and the API source code vector matrix.
[0010] Construct a first correlation matrix between the API query vector matrix and the API sequence vector matrix, and construct a second correlation matrix between the API query vector matrix and the API source code vector matrix;
[0011] Obtain user queries and convert them into user query vector matrices. Calculate the first similarity between the user query vector matrix and the API sequence vector matrix, and calculate the second similarity between the user query vector matrix and the API source code vector matrix.
[0012] The first similarity and the second similarity are summed to obtain the final similarity. Based on the final similarity, the API sequence data in the preset question and answer information database is recommended to the user using a recommendation model.
[0013] Optionally, the step of using a compiler to parse the source code file into an abstract syntax tree includes:
[0014] The source code file is broken down into tags using a lexical analyzer;
[0015] The tags are assembled into a grammatical structure according to grammatical rules, and the grammatical structure is semantically analyzed by a compiler to obtain semantic information;
[0016] The abstract syntax tree is constructed using the grammatical structure and the semantic information.
[0017] Optionally, the step of assembling the API ternary dataset according to the API fully qualified name matching algorithm includes:
[0018] Extract the source code corresponding to the API method body node in the abstract syntax tree, and select the source code with the richest method body information from the source code to obtain the API source code;
[0019] A preset API dataset is formed by using preset API query data and API sequence data. The API source code is then associated with the preset API dataset to obtain an API ternary dataset.
[0020] Optionally, the step of using an encoder to convert each data point in the API three-dimensional data set into a vector matrix to obtain an API query vector matrix, an API sequence vector matrix, and an API source code vector matrix includes:
[0021] Extract the API query data from the API three-data set, and use the first code encoding algorithm to convert the API query data into an API query vector matrix;
[0022] Extract the API sequence data from the API three-data set, and concatenate the API sequences in the API sequence data according to the fully qualified name text to obtain the concatenated text sequence;
[0023] The second encoding algorithm is used to extract semantic features from the concatenated text sequence to obtain the API sequence vector matrix;
[0024] The API source code corresponds one-to-one with the API sequence data. The second encoding algorithm is used to convert the API source code into a feature matrix to obtain the API source code vector matrix.
[0025] Optionally, constructing the first correlation matrix of the API query vector matrix and the API sequence vector matrix includes:
[0026] Calculate the relevance score of the markers in the API query vector matrix to each marker in the API sequence vector matrix, and denote it as a column vector matrix;
[0027] Calculate the relevance score of the markers in the API sequence vector matrix to each marker in the API query vector matrix, and denote it as a row vector matrix;
[0028] The first correlation matrix is constructed using the column vector matrix and the row vector matrix.
[0029] Optionally, after constructing the first correlation matrix of the API query vector matrix and the API sequence vector matrix, and the second correlation matrix of the API query vector matrix and the API source code vector matrix, the method further includes: training the pre-built API recommendation model.
[0030] Optionally, the training of the pre-built API recommendation model includes:
[0031] Max pooling is performed on the first association matrix and converted into a first attention score association matrix using a preset activation function; and max pooling is performed on the second association matrix and converted into a second attention score association matrix using the preset activation function.
[0032] The API query vector matrix is transformed into a query attention score matrix using the first attention score association matrix and the second attention score association matrix.
[0033] The first correlation matrix is transformed into a first attention score matrix using a matrix transformation method, and the second correlation matrix is transformed into a second attention score matrix using a matrix transformation method.
[0034] The semantic fusion vector of the first attention score matrix is calculated using a preset semantic fusion vector formula to obtain the first attention semantic fusion vector; the semantic fusion vector of the second attention score matrix is calculated using a preset semantic fusion vector formula to obtain the second attention semantic fusion vector; and the semantic fusion vector of the query attention score matrix is calculated using a preset semantic fusion vector formula to obtain the query attention semantic fusion vector.
[0035] Extract irrelevant API sequence data and API source code data from the API query vector matrix, and use the irrelevant API sequence data to form a first irrelevant semantic fusion vector, and use the irrelevant API source code data to form a second irrelevant semantic fusion vector;
[0036] The pre-built API recommendation model is compared, learned, and optimized based on the first attention semantic fusion vector, the second attention semantic fusion vector, the query attention semantic fusion vector, the first irrelevant semantic fusion vector, and the second irrelevant semantic fusion vector, and the optimized API recommendation model is obtained.
[0037] To address the above problems, the present invention also provides an API recommendation device, the device comprising:
[0038] The source code file processing module is used to obtain source code files, parse the source code files into an abstract syntax tree using a compiler, traverse the preset method name nodes in the abstract syntax tree to obtain API source code, and form an API ternary dataset according to the API fully qualified name matching algorithm. The ternary dataset includes API query data, API sequence data, and API source code.
[0039] The feature extraction module is used to convert each data in the API three-dimensional data set into a vector matrix using an encoder, to obtain an API query vector matrix, an API sequence vector matrix, and an API source code vector matrix; to construct a first correlation matrix between the API query vector matrix and the API sequence vector matrix, and to construct a second correlation matrix between the API query vector matrix and the API source code vector matrix;
[0040] The API recommendation module is used to acquire user queries, convert the user queries into a user query vector matrix, calculate a first similarity between the user query vector matrix and the API sequence vector matrix, and calculate a second similarity between the user query vector matrix and the API source code vector matrix; sum the first similarity and the second similarity to obtain a final similarity; and recommend API sequence data from a preset question-and-answer information database to the user based on the final similarity using a recommendation model.
[0041] To address the above problems, the present invention also provides an electronic device, the electronic device comprising:
[0042] At least one processor; and,
[0043] A memory communicatively connected to the at least one processor; wherein,
[0044] The memory stores a computer program that can be executed by the at least one processor, the computer program being executed by the at least one processor to enable the at least one processor to perform the API recommended method described above.
[0045] To address the aforementioned problems, the present invention also provides a computer-readable storage medium storing at least one computer program, which is executed by a processor in an electronic device to implement the API recommendation method described above.
[0046] This invention obtains source code files, uses a compiler to parse the source code files into an abstract syntax tree, and uses the abstract syntax tree to construct an API triple dataset. This allows for the precise extraction of information from the source code. An encoder is then used to convert each data point in the API triple dataset into a vector matrix, enabling the subsequent recommendation model to more accurately understand the function of each API and improve recommendation accuracy. Furthermore, by constructing an association matrix, the correlation between the API query vector matrix and API features is enhanced, further improving the accuracy of the API recommendation model. Attached Figure Description
[0047] Figure 1 This is a flowchart illustrating an API recommendation method provided in an embodiment of the present invention.
[0048] Figure 2 This is a functional block diagram of an API recommendation device provided in an embodiment of the present invention;
[0049] Figure 3 This is a schematic diagram of the structure of an electronic device that implements the API recommendation method according to an embodiment of the present invention.
[0050] The realization of the objective, functional features and advantages of the present invention will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation
[0051] It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.
[0052] This application provides an API recommendation method. The execution entity of the API recommendation method includes, but is not limited to, at least one of the following: a server, a terminal, or an electronic device that can be configured to execute the method provided in this application. In other words, the API recommendation method can be executed by software or hardware installed on a terminal device or a server device, and the software can be a blockchain platform. The server includes, but is not limited to, a single server, a server cluster, a cloud server, or a cloud server cluster. The server can be an independent server or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, content delivery networks (CDNs), and big data and artificial intelligence platforms.
[0053] Reference Figure 1 The diagram shown is a flowchart illustrating an API recommendation method according to an embodiment of the present invention. In this embodiment, the API recommendation method includes:
[0054] S1. Obtain the source code file and use the compiler to parse the source code file into an abstract syntax tree.
[0055] In this embodiment of the invention, the source code file refers to a code file containing a certain programming language, wherein the source code file contains the logic and structure of the code.
[0056] In this embodiment of the invention, the compiler refers to a compilation tool that converts source code into low-level code that can be executed by a computer.
[0057] In this embodiment of the invention, the abstract syntax tree refers to the hierarchical structure representation of the source code file, which is usually represented in the form of a tree to represent the building blocks of a programming language.
[0058] As an embodiment of the present invention, the step of using a compiler to parse the source code file into an abstract syntax tree includes:
[0059] The source code file is broken down into tags using a lexical analyzer;
[0060] The tags are assembled into a grammatical structure according to grammatical rules, and the grammatical structure is semantically analyzed by a compiler to obtain semantic information;
[0061] The abstract syntax tree is constructed using the grammatical structure and the semantic information.
[0062] In this embodiment of the invention, the marker refers to the smallest meaningful segment in the source code file, including but not limited to keywords, identifiers, constants, and operators.
[0063] S2. Traverse the preset method name nodes in the abstract syntax tree to obtain the API source code, and form an API ternary dataset according to the API fully qualified name matching algorithm. The ternary dataset includes API query data, API sequence data, and API source code.
[0064] In this embodiment of the invention, the preset method name node refers to the "MethodDeclaration" node in the abstract syntax tree, where each "MethodDeclaration" node represents a specific Java method.
[0065] In this embodiment of the invention, the fully qualified name of the API refers to the complete name of a class or interface in the source code, including the package name and the class name. The package name is the name of the logical group to which the class or interface belongs, which is usually used to organize the code structure and indicate the hierarchical relationship of the class or interface in the project. The class name is the name of the object template defined in the programming language.
[0066] As an embodiment of the present invention, the API triple dataset composed according to the API fully qualified name matching algorithm includes:
[0067] Extract the source code corresponding to the API method body node in the abstract syntax tree, and select the source code with the richest method body information from the source code to obtain the API source code;
[0068] Use the preset API query data and API sequence data to form a preset API dataset, and associate the API source code with the preset API dataset to obtain the API triple dataset.
[0069] In the embodiment of the present invention, the triple dataset includes API query data, API sequence data, and API source code, and its main manifestation form is <API query, API sequence, API source code>.
[0070] S3. Use an encoder to convert each data in the API triple dataset into a vector matrix to obtain an API query vector matrix, an API sequence vector matrix, and an API source code vector matrix.
[0071] In the embodiment of the present invention, the encoder refers to a tool for converting data into a vector matrix. For example, the encoder can adopt the CodeBERT encoder.
[0072] As an embodiment of the present invention, the use of an encoder to convert each data in the API triple dataset into a vector matrix to obtain an API query vector matrix, an API sequence vector matrix, and an API source code vector matrix includes:
[0073] Extract the API query data in the API triple dataset, and use the first code encoding algorithm to convert the API query data into an API query vector matrix;
[0074] Extract the API sequence data in the API triple dataset, and splice the API sequences in the API sequence data according to the fully qualified name text to obtain a spliced text sequence;
[0075] Adopt a second encoding algorithm to extract the semantic features in the spliced text sequence to obtain an API sequence vector matrix;
[0076] The API source code corresponds to the API sequence data one by one, and use the second encoding algorithm to convert the API source code into a feature matrix to obtain the API source code vector matrix.
[0077] For example, the encoder is used to convert each data point in the API three-dimensional data set into a vector matrix to obtain the API query vector matrix, the API sequence vector matrix, and the API source code vector matrix. The specific implementation steps are as follows:
[0078] API query data Query = {q1,q2,…q s} is a sequence of tokens of length s. Using CodeBERT1, it is transformed into a fixed-length feature matrix Q∈R. s*n The word embedding dimension is specifically calculated using the following formula:
[0079] Q = COdeBERT1([Query])
[0080] The API sequence contains i APIs, i.e., APIs = {API1, API2, ..., API...} i When obtaining the semantics of the entire API sequence, the i APIs in the API sequence are concatenated according to their fully qualified names to form a text sequence. Then, CodeBERT2 is used to convert this text sequence into a feature matrix A∈R of the API sequence. p*n Where p is the number of tokens in the entire API sequence text sequence, the specific formula is as follows:
[0081] A=CodeBERT2(concat[API1,API2,...API i ])
[0082] Based on the one-to-one correspondence between API source code and APIs in the dataset, the API source code is encoded into a feature matrix C∈R using the same method as encoding the API sequence. q*n Where q is the number of tokens in the entire API source code sequence text, and the specific formula is as follows:
[0083] C=CodeBERT2(concat[Code1,Code2,...Code i ])
[0084] Through the above calculations, we obtain the API query vector matrix Q, the API sequence vector matrix A, and the API source code vector matrix C.
[0085] S4. Construct a first correlation matrix between the API query vector matrix and the API sequence vector matrix, and construct a second correlation matrix between the API query vector matrix and the API source code vector matrix.
[0086] As an embodiment of the present invention, constructing the first correlation matrix of the API query vector matrix and the API sequence vector matrix includes:
[0087] Calculate the relevance score of the markers in the API query vector matrix to each marker in the API sequence vector matrix, and denote it as a column vector matrix;
[0088] Calculate the relevance score of the markers in the API sequence vector matrix to each marker in the API query vector matrix, and denote it as a row vector matrix;
[0089] The first correlation matrix is constructed using the column vector matrix and the row vector matrix.
[0090] In this embodiment of the invention, the first correlation matrix M for constructing the API query vector matrix and the API sequence vector matrix is... Q,A The following formula can be used:
[0091] M Q,A =tanh(AUQ) T )
[0092] Here, tanh(·) is the hyperbolic tangent function operation, which aims to map the output between -1 and 1.
[0093] In this embodiment of the invention, the second correlation matrix M for constructing the API query vector matrix and the API source code vector matrix... Q,C The following formula can be used:
[0094] M Q,C =tanh(CUQ T )
[0095] Among them, M Q,C The row vector matrix represents the relevance score of the markers in the API source code vector matrix to each marker in the API query vector matrix, M. Q,C The column vector matrix represents the relevance score of the markers in the API query vector matrix to each marker in the API source code vector matrix.
[0096] Furthermore, after constructing the first correlation matrix of the API query vector matrix and the API sequence vector matrix, and the second correlation matrix of the API query vector matrix and the API source code vector matrix, the method further includes: training the pre-built API recommendation model.
[0097] Furthermore, the training of the pre-built API recommendation model includes:
[0098] Max pooling is performed on the first association matrix and converted into a first attention score association matrix using a preset activation function; and max pooling is performed on the second association matrix and converted into a second attention score association matrix using the preset activation function.
[0099] The API query vector matrix is transformed into a query attention score matrix using the first attention score association matrix and the second attention score association matrix.
[0100] The first correlation matrix is transformed into a first attention score matrix using a matrix transformation method, and the second correlation matrix is transformed into a second attention score matrix using a matrix transformation method.
[0101] The semantic fusion vector of the first attention score matrix is calculated using a preset semantic fusion vector formula to obtain the first attention semantic fusion vector; the semantic fusion vector of the second attention score matrix is calculated using a preset semantic fusion vector formula to obtain the second attention semantic fusion vector; and the semantic fusion vector of the query attention score matrix is calculated using a preset semantic fusion vector formula to obtain the query attention semantic fusion vector.
[0102] Extract irrelevant API sequence data and API source code data from the API query vector matrix, and use the irrelevant API sequence data to form a first irrelevant semantic fusion vector, and use the irrelevant API source code data to form a second irrelevant semantic fusion vector;
[0103] The pre-built API recommendation model is compared, learned, and optimized based on the first attention semantic fusion vector, the second attention semantic fusion vector, the query attention semantic fusion vector, the first irrelevant semantic fusion vector, and the second irrelevant semantic fusion vector, and the optimized API recommendation model is obtained.
[0104] In this embodiment of the invention, the preset activation function is the SoftMax function.
[0105] Further, the step of transforming the API query vector matrix into a query attention score matrix using the first attention score association matrix and the second attention score association matrix includes:
[0106] The API query vector matrix is transformed into a query attention score matrix using the following formula:
[0107] B Q,A =SoftMax(MaxPooling(M Q,A ))
[0108] B Q,C=SoftMax(MaxPooling(M Q,C ))
[0109] B Q =SoftMax(w1B Q,A +w2B Q,C )
[0110] Wherein, the B Q,A Let B be the first attention score correlation matrix. Q,C Let w1 be the second attention score correlation matrix, w2 be the trainable first parameter, and B be the trainable second parameter. Q To query the attention score matrix.
[0111] This invention embodiment converts the first correlation matrix into a first attention score matrix using a matrix transformation method, and converts the second correlation matrix into a second attention score matrix using a matrix transformation method, including:
[0112] The following formula is used for conversion:
[0113]
[0114] Among them, B A Let B be the first attention score matrix. C This is the second attention score matrix.
[0115] Further, the first attention semantic fusion vector is obtained by calculating the semantic fusion vector of the first attention score matrix using a preset semantic fusion vector formula, the second attention semantic fusion vector is obtained by calculating the semantic fusion vector of the second attention score matrix using a preset semantic fusion vector formula, and the query attention semantic fusion vector Q′ is obtained by calculating the semantic fusion vector of the query attention score matrix using a preset semantic fusion vector formula. The preset semantic fusion vector formula adopts the following formula:
[0116] Q′=B Q Q
[0117] A′=B A A
[0118] C′=B C C
[0119] Where A′ is the first attention semantic fusion vector, C′ is the second attention semantic fusion vector, and Q′ is the query attention semantic fusion vector.
[0120] For example, the specific steps for training the pre-built API recommendation model are as follows:
[0121] Construct triples <Q′,A′,A - >and <Q′,C′,C - As input to a pre-built API recommendation model, where A - C refers to the first irrelevant semantic fusion vector that is not related to the query q. - This refers to the second irrelevant semantic fusion vector that is not related to the query q. The TropletLoss function is selected as the loss function, and the specific calculation formula is as follows:
[0122] L(θ)=max(0,d(Q′,A′)-d(Q′,A - )+δ)+max(0,d(Q′,C′)-d(Q′,C - )+δ)
[0123] Where θ refers to the trainable parameters in the pre-built API recommendation model, d refers to the Euclidean distance between the two input vectors, and δ is a boundary parameter used to ensure that the distance between the query attention semantic fusion vector Q′ and the first attention semantic fusion vector A′ and the second attention semantic fusion vector C′ is less than that between the first irrelevant semantic fusion vector A′ and the second irrelevant semantic fusion vector C′. - The second unrelated semantic fusion vector C - The distance ratio between them is less than δ.
[0124] In this embodiment of the invention, the pre-built API recommendation model uses the AdamW optimization algorithm to minimize TropletLoss, thereby continuously updating and optimizing the model parameters.
[0125] S5. Obtain the user query and convert the user query into a user query vector matrix. Calculate the first similarity between the user query vector matrix and the API sequence vector matrix, and calculate the second similarity between the user query vector matrix and the API source code vector matrix.
[0126] S6. The first similarity and the second similarity are summed to obtain the final similarity. Based on the final similarity, the API sequence data in the preset question and answer information database is recommended to the user using the API recommendation model.
[0127] In this embodiment of the invention, API sequence data in a preset question-and-answer information database are sorted by the final similarity score, and API sequence data with high similarity scores in the similarity ranking results are recommended to users.
[0128] In this embodiment of the invention, the preset question and answer information database refers to an information database constructed using a large amount of question information and a large amount of answer information from question and answer websites, and the API sequence data is extracted from the answer information in the preset question and answer information database.
[0129] This invention, through obtaining source code files, uses a compiler to parse the source code files into an abstract syntax tree (AST), and uses the AST to construct an API triple dataset. This allows for the precise extraction of information from the source code. An encoder is then used to convert each data point in the API triple dataset into a vector matrix, enabling the subsequent recommendation model to more accurately understand the function of each API and improve recommendation accuracy. Furthermore, by constructing an association matrix, the correlation between the API query vector matrix and API features can be enhanced, further improving the accuracy of the API recommendation model.
[0130] Table 1 shows a comparison of the effectiveness of the API recommendation model in this invention with existing API recommendation models (BIKER and CLEAR):
[0131] Table 1
[0132]
[0133] The API recommendation model of this invention has an MRR of 0.473 and Precision@1 / 3 / 5 / 10 values of 0.36 / 0.536 / 0.632 / 0.747. The API recommendation outperforms BIKER by 392.71% and CLEAR by 130.73% in MRR, and outperforms the BIKER model by 490.16% / 342.98% / 357.97% / 334.3% and the CLEAR model by 118.18% / 132.03% / 148.82% / 172.63% in Precision@1 / 3 / 5 / 10, respectively. The experimental results show that the API recommendation achieves recommendation effectiveness exceeding that of the existing models BIKER and CLEAR.
[0134] Table 2 shows a comparison of the effectiveness of models employing different recommendation strategies:
[0135] Table 2
[0136]
[0137] Among them, CLEAR-QA's MRR value is 0.409, and its Precision@1 / 3 / 5 / 10 values are 0.279 / 0.453 / 0.562 / 0.701. CLEAR-QA surpasses CLEAR by 99.51% in MRR and by 69.09% / 96.10% / 121.26% / 155.84% in Precision@1 / 3 / 5 / 10. Furthermore, API recommendation surpasses the API recommendation model-QQ by 161.33% in MRR and by 164.71% / 155.24% / 154.84% / 161.19% in Precision@1 / 3 / 5 / 10. Experimental results show that the API recommendation model based on a direct recommendation strategy is more effective than the API recommendation model based on an indirect recommendation strategy. The direct recommendation strategy can effectively mitigate the impact of the coverage of collected question titles on the effectiveness of the recommendation model. Furthermore, experimental results show that the API recommendation model of this invention outperforms CLEAR-QA by 15.65% in MRR and by 29.03% / 18.32% / 12.46% / 6.56% in Precision@1 / 3 / 5 / 10. This indicates that even when both adopt a direct recommendation strategy, the API recommendation model of this invention is more effective than the CLEAR model.
[0138] Table 3 shows the impact of API source code and joint attention mechanism on the effectiveness of the API recommendation model in this invention.
[0139] Table 3
[0140]
[0141] Among them, the API recommendation model of this invention improved the MRR by 20.97% and the Precision@1 / 3 / 5 / 10 metrics by 22.45% / 25.53% / 20.38% / 15.81% after adding API source code as a feature. This indicates that API source code can provide richer feature information for the model's effectiveness, enabling the model to more accurately understand the API's functionality. After adding a joint attention mechanism, the API recommendation model of this invention improved the MRR by 9.49% and the Precision@1 / 3 / 5 / 10 metrics by 11.8% / 9.61% / 10.1% / 4.62%. This indicates that the joint attention mechanism can strengthen the correlation between query representation and API feature representation.
[0142] Table 4 shows a comparison of the recommendation efficiency of the API recommendation model of this invention with existing API recommendation models (BIKER and CLEAR):
[0143] Table 4
[0144]
[0145] The API recommendation model of this invention achieves a recommendation efficiency of 0.014 seconds per item, which is 2.556 seconds / item faster than BIKER and 0.002 seconds / item faster than CLEAR. The high recommendation efficiency of this API recommendation model in the above experimental results is due to the direct recommendation strategy skipping the re-ranking step and directly sorting all API sequences based on the similarity between the query and API features, thus saving time.
[0146] like Figure 3 The diagram shown is a functional block diagram of an API recommendation device provided in an embodiment of the present invention.
[0147] The API recommendation device 100 described in this invention can be installed in an electronic device. Depending on the functions implemented, the API recommendation device 100 may include a source code file processing module 101, a feature extraction module 102, and an API recommendation module 103.
[0148] The module described in this invention can also be called a unit, which refers to a series of computer program segments that can be executed by the processor of an electronic device and can perform a fixed function, and are stored in the memory of the electronic device.
[0149] In this embodiment, the functions of each module / unit are as follows:
[0150] The source code file processing module 101 is used to obtain source code files, parse the source code files into an abstract syntax tree using a compiler, traverse the preset method name nodes in the abstract syntax tree to obtain API source code, and form an API ternary dataset according to the API fully qualified name matching algorithm. The ternary dataset includes API query data, API sequence data, and API source code.
[0151] In this embodiment of the invention, the source code file refers to a code file containing a certain programming language, wherein the source code file contains the logic and structure of the code.
[0152] In this embodiment of the invention, the compiler refers to a compilation tool that converts source code into low-level code that can be executed by a computer.
[0153] In this embodiment of the invention, the abstract syntax tree refers to the hierarchical structure representation of the source code file, which is usually represented in the form of a tree to represent the building blocks of a programming language.
[0154] As an embodiment of the present invention, parsing the source code file into an abstract syntax tree by using a compiler includes:
[0155] Using a lexical analyzer to decompose the source code file into tokens;
[0156] Combining the tokens into a syntax structure according to syntax rules, and performing semantic analysis on the syntax structure by using a compiler to obtain semantic information;
[0157] Constructing the abstract syntax tree by using the syntax structure and the semantic information.
[0158] In an embodiment of the present invention, the token refers to the smallest meaningful fragment in the source code file, including but not limited to keywords, identifiers, constants, and operators.
[0159] In an embodiment of the present invention, the preset method name node refers to the "MethodDeclaration" node in the abstract syntax tree, where each "MethodDeclaration" node represents a specific Java method.
[0160] In an embodiment of the present invention, the fully qualified name of the API refers to the complete name of a class or an interface in the source code, including the package name and the class name where it is located. The package name is the name of the logical grouping to which the class or interface belongs, usually used to organize the code structure, indicating the hierarchical relationship of the class or interface in the project, and the class name is the name of the object template defined in the programming language.
[0161] As an embodiment of the present invention, forming an API triple dataset according to the API fully qualified name matching algorithm includes:
[0162] Extracting the source code corresponding to the API method body node in the abstract syntax tree, and selecting the source code with the richest method body information from the source code to obtain the API source code;
[0163] Using the preset API query data and API sequence data to form a preset API dataset, and associating the API source code with the preset API dataset to obtain the API triple dataset.
[0164] In an embodiment of the present invention, the triple dataset includes API query data, API sequence data, and API source code, and its main manifestation form is <API query, API sequence, API source code>.
[0165] The feature extraction module 102 is used to convert each data in the API three-dimensional data set into a vector matrix using an encoder, to obtain an API query vector matrix, an API sequence vector matrix, and an API source code vector matrix; to construct a first correlation matrix of the API query vector matrix and the API sequence vector matrix, and to construct a second correlation matrix of the API query vector matrix and the API source code vector matrix.
[0166] In this embodiment of the invention, the encoder refers to a tool that converts data into a vector matrix. For example, the encoder may be the CodeBERT encoder.
[0167] As an embodiment of the present invention, the step of using an encoder to convert each data in the API three-dimensional data set into a vector matrix to obtain an API query vector matrix, an API sequence vector matrix, and an API source code vector matrix includes:
[0168] Extract the API query data from the API three-data set, and use the first code encoding algorithm to convert the API query data into an API query vector matrix;
[0169] Extract the API sequence data from the API three-data set, and concatenate the API sequences in the API sequence data according to the fully qualified name text to obtain the concatenated text sequence;
[0170] The second encoding algorithm is used to extract semantic features from the concatenated text sequence to obtain the API sequence vector matrix;
[0171] The API source code corresponds one-to-one with the API sequence data. The second encoding algorithm is used to convert the API source code into a feature matrix to obtain the API source code vector matrix.
[0172] For example, the encoder is used to convert each data point in the API three-dimensional data set into a vector matrix to obtain the API query vector matrix, the API sequence vector matrix, and the API source code vector matrix. The specific implementation steps are as follows:
[0173] API query data Query = {q1, q2, ... q s} is a sequence of tokens of length s. Using CodeBERT1, it is transformed into a fixed-length feature matrix Q∈R. s*n The word embedding dimension is specifically calculated using the following formula:
[0174] Q = CodeBERT1([Query])
[0175] The API sequence contains i APIs, i.e., APIs = {API1, API2, ..., API...} i When obtaining the semantics of the entire API sequence, the i APIs in the API sequence are concatenated according to their fully qualified names to form a text sequence. Then, CodeBERT2 is used to convert this text sequence into a feature matrix A∈R of the API sequence. p*n Where p is the number of tokens in the entire API sequence text sequence, the specific formula is as follows:
[0176] A=CodeBERT2(concat[API1,API2,...API i ])
[0177] Based on the one-to-one correspondence between API source code and APIs in the dataset, the API source code is encoded into a feature matrix C∈R using the same method as encoding the API sequence. q*n Where q is the number of tokens in the entire API source code sequence text, and the specific formula is as follows:
[0178] C=CodeBERT2(concat[Code1,Code2,...Code i ])
[0179] Through the above calculations, we obtain the API query vector matrix Q, the API sequence vector matrix A, and the API source code vector matrix C.
[0180] As an embodiment of the present invention, constructing the first correlation matrix of the API query vector matrix and the API sequence vector matrix includes:
[0181] Calculate the relevance score of the markers in the API query vector matrix to each marker in the API sequence vector matrix, and denote it as a column vector matrix;
[0182] Calculate the relevance score of the markers in the API sequence vector matrix to each marker in the API query vector matrix, and denote it as a row vector matrix;
[0183] The first correlation matrix is constructed using the column vector matrix and the row vector matrix.
[0184] In this embodiment of the invention, the first correlation matrix M for constructing the API query vector matrix and the API sequence vector matrix is... Q,A The following formula can be used:
[0185] M Q,A =tanh(AUQ) T )
[0186] Here, tanh(·) is the hyperbolic tangent function operation, which aims to map the output between -1 and 1.
[0187] In this embodiment of the invention, the second correlation matrix M for constructing the API query vector matrix and the API source code vector matrix... Q,C The following formula can be used:
[0188] M Q,C =tanh(CUQ T )
[0189] Among them, M Q,C The row vector matrix represents the relevance score of the markers in the API source code vector matrix to each marker in the API query vector matrix, M. Q,C The column vector matrix represents the relevance score of the markers in the API query vector matrix to each marker in the API source code vector matrix.
[0190] Furthermore, after constructing the first correlation matrix of the API query vector matrix and the API sequence vector matrix, and the second correlation matrix of the API query vector matrix and the API source code vector matrix, the method further includes: training the pre-built API recommendation model.
[0191] Furthermore, the training of the pre-built API recommendation model includes:
[0192] Max pooling is performed on the first association matrix and converted into a first attention score association matrix using a preset activation function; and max pooling is performed on the second association matrix and converted into a second attention score association matrix using the preset activation function.
[0193] The API query vector matrix is transformed into a query attention score matrix using the first attention score association matrix and the second attention score association matrix.
[0194] The first correlation matrix is transformed into a first attention score matrix using a matrix transformation method, and the second correlation matrix is transformed into a second attention score matrix using a matrix transformation method.
[0195] The semantic fusion vector of the first attention score matrix is calculated using a preset semantic fusion vector formula to obtain the first attention semantic fusion vector; the semantic fusion vector of the second attention score matrix is calculated using a preset semantic fusion vector formula to obtain the second attention semantic fusion vector; and the semantic fusion vector of the query attention score matrix is calculated using a preset semantic fusion vector formula to obtain the query attention semantic fusion vector.
[0196] Extract irrelevant API sequence data and API source code data from the API query vector matrix, and use the irrelevant API sequence data to form a first irrelevant semantic fusion vector, and use the irrelevant API source code data to form a second irrelevant semantic fusion vector;
[0197] The pre-built API recommendation model is compared, learned, and optimized based on the first attention semantic fusion vector, the second attention semantic fusion vector, the query attention semantic fusion vector, the first irrelevant semantic fusion vector, and the second irrelevant semantic fusion vector, and the optimized API recommendation model is obtained.
[0198] In this embodiment of the invention, the preset activation function is the SoftMax function.
[0199] Further, the step of transforming the API query vector matrix into a query attention score matrix using the first attention score association matrix and the second attention score association matrix includes:
[0200] The API query vector matrix is transformed into a query attention score matrix using the following formula:
[0201] B Q,A =SoftMax(MaxPooling(M Q,A ))
[0202] B Q,C =SoftMax(MaxPooling(M Q,C ))
[0203] B Q =SoftMax(w1B Q,A +w2B Q,C )
[0204] Wherein, the B Q,A Let B be the first attention score correlation matrix. Q,C Let w1 be the second attention score correlation matrix, w2 be the trainable first parameter, and B be the trainable second parameter. Q To query the attention score matrix.
[0205] This invention embodiment converts the first correlation matrix into a first attention score matrix using a matrix transformation method, and converts the second correlation matrix into a second attention score matrix using a matrix transformation method, including:
[0206] The following formula is used for conversion:
[0207]
[0208] Among them, B A Let B be the first attention score matrix. C This is the second attention score matrix.
[0209] Further, the first attention semantic fusion vector is obtained by calculating the semantic fusion vector of the first attention score matrix using a preset semantic fusion vector formula, the second attention semantic fusion vector is obtained by calculating the semantic fusion vector of the second attention score matrix using a preset semantic fusion vector formula, and the query attention semantic fusion vector Q′ is obtained by calculating the semantic fusion vector of the query attention score matrix using a preset semantic fusion vector formula. The preset semantic fusion vector formula adopts the following formula:
[0210] Q′=B Q Q
[0211] A′=0 A A
[0212] C′=B C C
[0213] Where A′ is the first attention semantic fusion vector, C′ is the second attention semantic fusion vector, and Q′ is the query attention semantic fusion vector.
[0214] For example, the specific steps for training the pre-built API recommendation model are as follows:
[0215] Construct triples <Q′,A′,A - >and <Q′,C′,C - As input to a pre-built API recommendation model, where A - C refers to the first irrelevant semantic fusion vector that is not related to the query q. - This refers to the second irrelevant semantic fusion vector that is not related to the query q. The TropletLoss function is selected as the loss function, and the specific calculation formula is as follows:
[0216] L(θ)=max(0,d(Q′,A′)-d(Q′,A - )+δ)+max(0,d(Q′,C′)-d(Q′,C - )+δ)
[0217] Where θ refers to the trainable parameters in the pre-built API recommendation model, d refers to the Euclidean distance between the two input vectors, and δ is a boundary parameter used to ensure that the distance between the query attention semantic fusion vector Q′ and the first attention semantic fusion vector A′ and the second attention semantic fusion vector C′ is less than that between the first irrelevant semantic fusion vector A′ and the second irrelevant semantic fusion vector C′. -The second unrelated semantic fusion vector C - The distance ratio between them is less than δ.
[0218] In this embodiment of the invention, the pre-built API recommendation model uses the AdamW optimization algorithm to minimize TropletLoss, thereby continuously updating and optimizing the model parameters.
[0219] The API recommendation module 103 is used to acquire user queries, convert the user queries into a user query vector matrix, calculate a first similarity between the user query vector matrix and the API sequence vector matrix, and calculate a second similarity between the user query vector matrix and the API source code vector matrix; sum the first similarity and the second similarity to obtain a final similarity, and recommend API sequence data in a preset question-and-answer information database to the user based on the final similarity using a recommendation model.
[0220] In this embodiment of the invention, API sequence data in a preset question-and-answer information database are sorted by the final similarity score, and API sequence data with high similarity scores in the similarity ranking results are recommended to users.
[0221] In this embodiment of the invention, the preset question and answer information database refers to an information database constructed using a large amount of question information and a large amount of answer information from question and answer websites, and the API sequence data is extracted from the answer information in the preset question and answer information database.
[0222] like Figure 3 The diagram shown is a structural schematic of an electronic device that implements an API recommendation method according to an embodiment of the present invention.
[0223] The electronic device may include a processor 10, a memory 11, a communication bus 12, and a communication interface 13, and may also include a computer program, such as an API recommendation method program, stored in the memory 11 and executable on the processor 10.
[0224] In some embodiments, the processor 10 may be composed of integrated circuits, such as a single packaged integrated circuit or multiple integrated circuits with the same or different functions, including combinations of one or more central processing units (CPUs), microprocessors, digital processing chips, graphics processors, and various control chips. The processor 10 is the control unit of the electronic device, connecting various components of the entire electronic device through various interfaces and lines. It executes programs or modules stored in the memory 11 (e.g., executing an API recommendation method program) and calls data stored in the memory 11 to perform various functions of the electronic device and process data.
[0225] The memory 11 includes at least one type of readable storage medium, including flash memory, portable hard drive, multimedia card, card-type memory (e.g., SD or DX memory), magnetic memory, magnetic disk, optical disk, etc. In some embodiments, the memory 11 can be an internal storage unit of an electronic device, such as a portable hard drive. In other embodiments, the memory 11 can be an external storage device of the electronic device, such as a plug-in portable hard drive, Smart Media Card (SMC), Secure Digital (SD) card, Flash Card, etc. Furthermore, the memory 11 can include both internal and external storage units of the electronic device. The memory 11 can be used not only to store application software and various types of data installed on the electronic device, such as the code of an API recommendation method program, but also to temporarily store data that has been output or will be output.
[0226] The communication bus 12 can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This bus can be divided into an address bus, a data bus, a control bus, etc. The bus is configured to enable communication between the memory 11 and at least one processor 10, etc.
[0227] The communication interface 13 is used for communication between the aforementioned electronic device and other devices, including a network interface and a user interface. Optionally, the network interface may include a wired interface and / or a wireless interface (such as a Wi-Fi interface, Bluetooth interface, etc.), typically used to establish communication connections between the electronic device and other electronic devices. The user interface may be a display, an input unit (such as a keyboard), or, optionally, a standard wired or wireless interface. Optionally, in some embodiments, the display may be an LED display, a liquid crystal display, a touch-sensitive liquid crystal display, or an OLED (Organic Light-Emitting Diode) touchscreen, etc. The display may also be appropriately referred to as a screen or display unit, used to display information processed in the electronic device and to display a visual user interface.
[0228] Figure 3 Only electronic devices with components are shown; it will be understood by those skilled in the art that... Figure 3 The structure shown does not constitute a limitation on the electronic device and may include fewer or more components than shown, or combine certain components, or have different component arrangements.
[0229] For example, although not shown, the electronic device may also include a power supply (such as a battery) to power the various components. Preferably, the power supply can be logically connected to the at least one processor 10 through a power management device, thereby enabling functions such as charging management, discharging management, and power consumption management. The power supply may also include one or more DC or AC power supplies, recharging devices, power fault detection circuits, power converters or inverters, power status indicators, and other arbitrary components. The electronic device may also include various sensors, Bluetooth modules, Wi-Fi modules, etc., which will not be described in detail here.
[0230] It should be understood that the embodiments described are for illustrative purposes only and are not limited to this structure in the scope of the patent application.
[0231] The memory 11 in the electronic device stores an API recommendation method program, which is a combination of multiple instructions. When run in the processor 10, it can achieve the following:
[0232] Obtain the source code file and use a compiler to parse the source code file into an abstract syntax tree;
[0233] The API source code is obtained by traversing the preset method name nodes in the abstract syntax tree, and an API ternary dataset is formed according to the API fully qualified name matching algorithm. The ternary dataset includes API query data, API sequence data and API source code.
[0234] The encoder is used to convert each data in the API three-dimensional data set into a vector matrix, resulting in the API query vector matrix, the API sequence vector matrix, and the API source code vector matrix.
[0235] Construct a first correlation matrix between the API query vector matrix and the API sequence vector matrix, and construct a second correlation matrix between the API query vector matrix and the API source code vector matrix;
[0236] Obtain user queries and convert them into user query vector matrices. Calculate the first similarity between the user query vector matrix and the API sequence vector matrix, and calculate the second similarity between the user query vector matrix and the API source code vector matrix.
[0237] The first similarity and the second similarity are summed to obtain the final similarity. Based on the final similarity, the API sequence data in the preset question and answer information database is recommended to the user using the API recommendation model.
[0238] Specifically, the specific implementation method of the processor 10 for the above instructions can be referred to the description of the relevant steps in the corresponding embodiment of the accompanying drawings, and will not be repeated here.
[0239] Furthermore, if the modules / units integrated in the electronic device 1 are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. The computer-readable storage medium can be volatile or non-volatile. For example, the computer-readable medium may include: any entity or device capable of carrying the computer program code, a recording medium, a USB flash drive, a portable hard drive, a magnetic disk, an optical disk, a computer memory, or a read-only memory (ROM).
[0240] The present invention also provides a computer-readable storage medium storing a computer program, which, when executed by a processor of an electronic device, can perform the following:
[0241] Obtain the source code file and use a compiler to parse the source code file into an abstract syntax tree;
[0242] The API source code is obtained by traversing the preset method name nodes in the abstract syntax tree, and an API ternary dataset is formed according to the API fully qualified name matching algorithm. The ternary dataset includes API query data, API sequence data and API source code.
[0243] The encoder is used to convert each data in the API three-dimensional data set into a vector matrix, resulting in the API query vector matrix, the API sequence vector matrix, and the API source code vector matrix.
[0244] Construct a first correlation matrix between the API query vector matrix and the API sequence vector matrix, and construct a second correlation matrix between the API query vector matrix and the API source code vector matrix;
[0245] Obtain user queries and convert them into user query vector matrices. Calculate the first similarity between the user query vector matrix and the API sequence vector matrix, and calculate the second similarity between the user query vector matrix and the API source code vector matrix.
[0246] The first similarity and the second similarity are summed to obtain the final similarity. Based on the final similarity, the API sequence data in the preset question and answer information database is recommended to the user using the API recommendation model.
[0247] In the several embodiments provided by this invention, it should be understood that the disclosed devices, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of modules is only a logical functional division, and other division methods may be used in actual implementation.
[0248] The modules described as separate components may or may not be physically separate. The components shown as modules may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs.
[0249] Furthermore, the functional modules in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or in the form of hardware plus software functional modules.
[0250] It will be apparent to those skilled in the art that the present invention is not limited to the details of the exemplary embodiments described above, and that the present invention can be implemented in other specific forms without departing from the spirit or essential characteristics of the present invention.
[0251] Therefore, the embodiments should be considered exemplary and non-limiting in all respects, and the scope of the invention is defined by the appended claims rather than the foregoing description. Thus, all variations falling within the meaning and scope of equivalents of the claims are intended to be embraced within the invention. No appended diagram markings in the claims should be construed as limiting the scope of the claims.
[0252] The blockchain referred to in this invention is a novel application model of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanisms, and encryption algorithms. Essentially, a blockchain is a decentralized database, a chain of data blocks linked together using cryptographic methods. Each data block contains information about a batch of network transactions, used to verify the validity of the information (anti-counterfeiting) and generate the next block. A blockchain can include an underlying blockchain platform, a platform product service layer, and an application service layer.
[0253] The embodiments of this application can acquire and process relevant data based on artificial intelligence technology. Artificial intelligence (AI) refers to the theories, methods, technologies, and application systems that use digital computers or machines controlled by digital computers to simulate, extend, and expand human intelligence, perceive the environment, acquire knowledge, and use that knowledge to obtain optimal results.
[0254] Furthermore, it is clear that the word "comprising" does not exclude other units or steps, and the singular does not exclude the plural. Multiple units or devices recited in a system claim may also be implemented by a single unit or device through software or hardware. The terms "first," "second," etc., are used to indicate names and do not indicate any specific order.
[0255] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit it. Although the present invention has been described in detail with reference to preferred embodiments, those skilled in the art should understand that modifications or equivalent substitutions can be made to the technical solutions of the present invention without departing from the spirit and scope of the technical solutions of the present invention.
Claims
1. An API recommendation method, characterized in that, The method includes: Obtain the source code file and use a compiler to parse the source code file into an abstract syntax tree; The API source code is obtained by traversing the preset method name nodes in the abstract syntax tree, and an API ternary dataset is formed according to the API fully qualified name matching algorithm. The ternary dataset includes API query data, API sequence data and API source code. The encoder is used to convert each data in the API three-dimensional data set into a vector matrix, resulting in the API query vector matrix, the API sequence vector matrix, and the API source code vector matrix. The process involves constructing a first correlation matrix between the API query vector matrix and the API sequence vector matrix, and a second correlation matrix between the API query vector matrix and the API source code vector matrix. Following these steps, the process further includes training a pre-built API recommendation model. This training of the pre-built API recommendation model includes: The process involves: performing max pooling on the first association matrix and converting it into a first attention score association matrix using a preset activation function; performing max pooling on the second association matrix and converting it into a second attention score association matrix using the same preset activation function; converting the API query vector matrix into a query attention score matrix using the first and second attention score association matrices; converting the first association matrix into a first attention score matrix using a matrix transformation method, and converting the second association matrix into a second attention score matrix using the same matrix transformation method; calculating the semantic fusion vector of the first attention score matrix using a preset semantic fusion vector formula to obtain a first attention semantic fusion vector, and calculating the second attention... The semantic fusion vector of the score matrix is used to obtain the second attention semantic fusion vector, and the semantic fusion vector of the query attention score matrix is calculated using a preset semantic fusion vector formula to obtain the query attention semantic fusion vector; irrelevant API sequence data and API source code data are extracted from the API query vector matrix, and the irrelevant API sequence data is used to form the first irrelevant semantic fusion vector, and the irrelevant API source code data is used to form the second irrelevant semantic fusion vector; the pre-built API recommendation model is compared, learned, and optimized based on the first attention semantic fusion vector, the second attention semantic fusion vector, the query attention semantic fusion vector, the first irrelevant semantic fusion vector, and the second irrelevant semantic fusion vector, and the API recommendation model is obtained after optimization; Obtain user queries and convert them into user query vector matrices. Calculate the first similarity between the user query vector matrix and the API sequence vector matrix, and calculate the second similarity between the user query vector matrix and the API source code vector matrix. The first similarity and the second similarity are summed to obtain the final similarity. Based on the final similarity, the API sequence data in the preset question and answer information database is recommended to the user using the API recommendation model.
2. The API recommendation method as described in claim 1, characterized in that, The step of using a compiler to parse the source code file into an abstract syntax tree includes: The source code file is broken down into tags using a lexical analyzer; The tags are assembled into a grammatical structure according to grammatical rules, and the grammatical structure is semantically analyzed by a compiler to obtain semantic information; The abstract syntax tree is constructed using the grammatical structure and the semantic information.
3. The API recommendation method as described in claim 1, characterized in that, The API ternary dataset, constructed using the API fully qualified name matching algorithm, includes: Extract the source code corresponding to the API method body node in the abstract syntax tree, and select the source code with the richest method body information from the source code to obtain the API source code; A preset API dataset is formed by using preset API query data and API sequence data. The API source code is then associated with the preset API dataset to obtain an API ternary dataset.
4. The API recommendation method as described in claim 1, characterized in that, The process of using an encoder to convert each data point in the API three-dimensional data set into a vector matrix yields an API query vector matrix, an API sequence vector matrix, and an API source code vector matrix, including: Extract the API query data from the API three-data set, and use the first code encoding algorithm to convert the API query data into an API query vector matrix; Extract the API sequence data from the API three-data set, and concatenate the API sequences in the API sequence data according to the fully qualified name text to obtain the concatenated text sequence; The second encoding algorithm is used to extract semantic features from the concatenated text sequence to obtain the API sequence vector matrix; The API source code corresponds one-to-one with the API sequence data. The second encoding algorithm is used to convert the API source code into a feature matrix to obtain the API source code vector matrix.
5. The API recommendation method as described in claim 1, characterized in that, The first correlation matrix for constructing the API query vector matrix and the API sequence vector matrix includes: Calculate the relevance score of the markers in the API query vector matrix to each marker in the API sequence vector matrix, and denote it as a column vector matrix; Calculate the relevance score of the markers in the API sequence vector matrix to each marker in the API query vector matrix, and denote it as a row vector matrix; The first correlation matrix is constructed using the column vector matrix and the row vector matrix.
6. An API recommendation device, characterized in that, The apparatus can implement the API recommendation method as described in any one of claims 1 to 5, the apparatus comprising: The source code file processing module is used to obtain source code files, parse the source code files into an abstract syntax tree using a compiler, traverse the preset method name nodes in the abstract syntax tree to obtain API source code, and form an API ternary dataset according to the API fully qualified name matching algorithm. The ternary dataset includes API query data, API sequence data, and API source code. The feature extraction module is used to convert each data in the API three-dimensional data set into a vector matrix using an encoder, to obtain an API query vector matrix, an API sequence vector matrix, and an API source code vector matrix; to construct a first correlation matrix between the API query vector matrix and the API sequence vector matrix, and to construct a second correlation matrix between the API query vector matrix and the API source code vector matrix; The API recommendation module is used to acquire user queries, convert the user queries into a user query vector matrix, calculate a first similarity between the user query vector matrix and the API sequence vector matrix, and calculate a second similarity between the user query vector matrix and the API source code vector matrix; sum the first similarity and the second similarity to obtain a final similarity; and recommend API sequence data from a preset question-and-answer information database to the user based on the final similarity using a recommendation model.
7. An electronic device, characterized in that, The electronic device includes: At least one processor; and, A memory communicatively connected to the at least one processor; wherein, The memory stores a computer program that can be executed by the at least one processor to enable the at least one processor to perform the API recommendation method as described in any one of claims 1 to 5.
8. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the API recommendation method as described in any one of claims 1 to 5.
Citation Information
Patent Citations
Code searching method based on functional similarity matching
CN110716749A
Context-aware API recommendation method based on query statements and terminal
CN111857660A