API recommendation method and device, electronic equipment and storage medium

By parsing source code files, building API ternary data sets and converting them into vector matrix, the problem of text semantics and knowledge gaps in existing API recommendation technologies is solved, and the accuracy and accuracy of API recommendations are improved.

CN119397002AActive Publication Date: 2025-02-07DIGITAL CHONGQING BIG DATA APPL DEV CO LTD
View PDF 7 Cites 0 Cited by

Patent Information

Application Number
CN202411291124.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-14
Publication Date
2025-02-07
Estimated Expiration
2044-09-14

AI Technical Summary

Technical Problem

Existing API recommendation technology is difficult to effectively resolve the text semantic gap between user query and API function description and the knowledge gap between developers and API designers, especially when users need uncommon or newly developed APIs, the model is difficult to complete. Correct recommendations and ignore semantic information in the API source code, resulting in low recommendation accuracy.

Method used

By obtaining the source code file, using the compiler to parse it into an abstract syntax tree, building an API ternary data set, and using the encoder to convert the data into a vector matrix, building an association matrix to enhance the correlation between the API query vector matrix and API features. , and finally calculate the similarity according to user query to make API recommendations.

Benefits of technology

It improves the accuracy of API recommendations, can understand the functions of each API more accurately, and enhances the correlation between API query vector matrix and API features, thereby improving the accuracy of recommendations.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119397002A_ABST
    Figure CN119397002A_ABST
Patent Text Reader

Abstract

The invention relates to an intelligent recommendation technology, and discloses an API (Application Program Interface) recommendation method, which comprises the following steps of: analyzing a source code file into an abstract syntax tree; after traversing the abstract syntax tree, forming an API ternary data set according to an API full-defined name matching algorithm, and converting the API ternary data set into a vector matrix; constructing a first incidence matrix of the API query vector matrix and the API sequence vector matrix, and constructing a second incidence matrix of the API query vector matrix and the API source code vector matrix; obtaining a user query, converting the user query into a user query vector matrix, calculating a first similarity between the user query vector matrix and the API sequence vector matrix, and calculating a second similarity between the user query vector matrix and the API source code vector matrix; and performing summation calculation on the first similarity and the second similarity to obtain a final similarity, and recommending the API sequence data to the user by utilizing an API recommendation model according to the final similarity. The invention further provides an API recommendation device, electronic equipment and a storage medium. According to the invention, the accuracy of API recommendation can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of intelligent recommendation, and in particular relates to an API recommendation method, device, electronic device and storage medium. Background Art

[0002] In the process of software development, developers usually write the functions they want to implement into natural language queries and input them into search engines. Automated API recommendation technology can find relevant API methods based on user queries to reduce users' time and the possibility of errors.

[0003] In the prior art, there is a contrastive learning API recommendation method, CLEAR (Contrastive Learning for API Recommendation), which uses Stack Overflow (SO) posts as the middleware between user queries and APIs and adopts an indirect recommendation strategy to complete API recommendations. The CLEAR method calculates the similarity between the query and the question title of the SO post through an indirect recommendation strategy, and indirectly finds the API sequence associated with the query. In contrast, the direct recommendation strategy means that by directly calculating the similarity between the query and the API features, the model can directly retrieve the most relevant API sequence. The CLEAR model mainly consists of two parts. The first part is to filter candidate posts. CLEAR first constructs a <query, query related title, query irrelevant title> triple dataset, and then trains a sentence embedding model based on RoBERTa using contrastive training technology. When CLEAR recommends user queries, the embedding model converts the query and all the collected titles into vector matrix representations, and calculates the cosine similarity between the query vector matrix and each title vector matrix to find the top 50 candidate posts most relevant to the query semantics. The second part is to re-rank the candidate posts. CLEAR uses joint embedding training to train a semantic similarity re-ranking model based on RoBERTa. After CLEAR screens candidate posts, the re-ranking model calculates the similarity between the query and the titles of these posts again and re-ranks the relevance of these candidate posts. Finally, CLEAR extracts the API sequences in the accepted answers corresponding to these titles as the corresponding API recommendation results.

[0004] The query-based API recommendation technology in the existing technology needs to solve the text semantic gap between user queries and API function descriptions and the knowledge gap between developers and API designers. The indirect recommendation strategy in the existing technology relies on the coverage of the collected question titles raised by developers. When the API required by the user is an uncommon API or a newly developed API, the model may not be able to complete the correct recommendation because it cannot find the corresponding title. In addition, the current existing technology ignores the semantic information contained in the API source code, which makes the accuracy of API recommendation low. Summary of the invention

[0005] The present invention provides an API recommendation method, device, electronic device and storage medium, which can improve the accuracy of API recommendation.

[0006] To achieve the above object, the present invention provides an API recommendation method, comprising:

[0007] Obtain a source code file, and parse the source code file into an abstract syntax tree using a compiler;

[0008] Traversing the method name nodes preset in the abstract syntax tree to obtain the API source code, and forming an API ternary data set according to an API fully qualified name matching algorithm, wherein the ternary data set includes API query data, API sequence data, and API source code;

[0009] Using an encoder to convert each data in the API triple 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;

[0010] Constructing a first association matrix between the API query vector matrix and the API sequence vector matrix, and constructing a second association matrix between the API query vector matrix and the API source code vector matrix;

[0011] Obtaining a user query, and converting the user query into a user query vector matrix, calculating a first similarity between the user query vector matrix and the API sequence vector matrix, and calculating a 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 a final similarity, and the API sequence data in a preset question and answer information library is recommended to the user using a recommendation model according to the final similarity.

[0013] Optionally, the using a compiler to parse the source code file into an abstract syntax tree includes:

[0014] Decomposing the source code file into tokens using a lexical analyzer;

[0015] The tags are combined into a grammatical structure according to grammatical rules, and a compiler is used to perform semantic analysis on the grammatical structure to obtain semantic information;

[0016] The abstract syntax tree is constructed using the grammatical structure and the semantic information.

[0017] Optionally, the API ternary data set is formed according to the API fully qualified name matching algorithm, including:

[0018] Extracting source code corresponding to the API method body node in the abstract syntax tree, and selecting source code with the richest method body information from the source code to obtain the API source code;

[0019] The preset API query data and API sequence data are used to form a preset API data set, and the API source code is associated with the preset API data set to obtain an API ternary data set.

[0020] Optionally, the step of converting each data in the API triple data set into a vector matrix by using an encoder to obtain an API query vector matrix, an API sequence vector matrix, and an API source code vector matrix includes:

[0021] Extracting API query data from the API triple data set, and converting the API query data into an API query vector matrix using a first code encoding algorithm;

[0022] Extracting API sequence data from the API triple data set, and splicing the API sequences in the API sequence data according to the fully qualified name text to obtain a spliced ​​text sequence;

[0023] Using a second encoding algorithm to extract semantic features in the concatenated text sequence to obtain an API sequence vector matrix;

[0024] The API source code corresponds to the API sequence data one by one, and the API source code is converted into a feature matrix using the second encoding algorithm to obtain the API source code vector matrix.

[0025] Optionally, constructing a first association matrix of the API query vector matrix and the API sequence vector matrix includes:

[0026] Calculate the relevance score of the token in the API query vector matrix to each token in the API sequence vector matrix, recorded as a column vector matrix;

[0027] Calculate the relevance score of the token in the API sequence vector matrix to each token in the API query vector matrix, recorded as a row vector matrix;

[0028] The first incidence matrix is ​​constructed using the column vector matrix and the row vector matrix.

[0029] Optionally, after constructing the first association matrix between the API query vector matrix and the API sequence vector matrix, and constructing the second association matrix between the API query vector matrix and the API source code vector matrix, it also includes: training a pre-built API recommendation model.

[0030] Optionally, the training of the pre-built API recommendation model includes:

[0031] Performing a maximum pooling operation on the first association matrix and converting it into a first attention score association matrix using a preset activation function, and performing a maximum pooling operation on the second association matrix and converting it into a second attention score association matrix using the preset activation function;

[0032] Converting 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;

[0033] Converting the first correlation matrix into a first attention score matrix using a matrix conversion method, and converting the second correlation matrix into a second attention score matrix using a matrix conversion method;

[0034] 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 semantic fusion vector of the second attention score matrix using a preset semantic fusion vector formula to obtain a second attention semantic fusion vector, and calculating the semantic fusion vector of the query attention score matrix using a preset semantic fusion vector formula to obtain a query attention semantic fusion vector;

[0035] Extracting irrelevant API sequence data and API source code data from the API query vector matrix, and using the irrelevant API sequence data to form a first irrelevant semantic fusion vector, and using the irrelevant API source code data to form a second irrelevant semantic fusion vector;

[0036] The pre-built API recommendation model is optimized through comparative learning 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 the optimization is completed.

[0037] In order to solve the above problems, the present invention also provides an API recommendation device, which includes:

[0038] A source code file processing module is used to obtain a source code file, parse the source code file into an abstract syntax tree using a compiler; traverse the method name nodes preset in the abstract syntax tree to obtain the API source code, and form an API ternary data set according to an API fully qualified name matching algorithm, wherein the ternary data set includes API query data, API sequence data, and API source code;

[0039] A feature extraction module is used to convert each data in the API triple 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; construct a first association matrix between the API query vector matrix and the API sequence vector matrix, and construct a second association matrix between the API query vector matrix and the API source code vector matrix;

[0040] The API recommendation module is used to obtain user queries and convert the user queries 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; sum the first similarity and the second similarity to obtain a final similarity, and recommend the API sequence data in a preset question and answer information library to the user using a recommendation model based on the final similarity.

[0041] In order to solve the above problem, the present invention further 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, and the computer program is executed by the at least one processor so that the at least one processor can perform the API recommendation method described above.

[0045] In order to solve the above problem, the present invention also provides a computer-readable storage medium, in which at least one computer program is stored. The at least one computer program is executed by a processor in an electronic device to implement the above-mentioned API recommendation method.

[0046] The present invention obtains source code files, uses a compiler to parse the source code files into abstract syntax trees, and uses the abstract syntax trees to construct API ternary data sets, so as to accurately extract information in the source code. By using an encoder to convert each data in the API ternary data set into a vector matrix, the subsequent recommendation model can understand the function of each API more accurately and improve the accuracy of the recommendation. In addition, by constructing an association matrix, the correlation between the API query vector matrix and the API features can be enhanced, further improving the accuracy of the API recommendation model recommendation. BRIEF DESCRIPTION OF THE DRAWINGS

[0047] Figure 1 A flowchart of an API recommendation method provided by an embodiment of the present invention;

[0048] Figure 2 A functional module diagram of an API recommendation device provided by an embodiment of the present invention;

[0049] Figure 3 A schematic diagram of the structure of an electronic device for implementing the API recommendation method provided by an embodiment of the present invention.

[0050] The realization of the purpose, functional features and advantages of the present invention will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION

[0051] It should be understood that the specific embodiments described herein are only used to explain the present invention, and are not used to limit the present invention.

[0052] The embodiment of the present application provides an API recommendation method. The execution subject of the API recommendation method includes but is not limited to at least one of the electronic devices such as a server, a terminal, etc. that can be configured to execute the method provided by the embodiment of the present 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, etc. The server can be an independent server, or it can be a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, content delivery networks (Content Delivery Network, CDN), and big data and artificial intelligence platforms.

[0053] Reference Figure 1 FIG. 1 is a flow chart of an API recommendation method provided by an embodiment of the present invention. In this embodiment, the API recommendation method includes:

[0054] S1. Obtain a source code file, and use a compiler to parse the source code file into an abstract syntax tree.

[0055] In the embodiment of the present 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 the embodiment of the present 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 the embodiment of the present invention, the abstract syntax tree refers to a hierarchical representation of a source code file, and usually represents a building block of a programming language in the form of a tree.

[0058] As an embodiment of the present invention, the step of parsing the source code file into an abstract syntax tree by using a compiler includes:

[0059] Decomposing the source code file into tokens using a lexical analyzer;

[0060] The tags are combined into a grammatical structure according to grammatical rules, and a compiler is used to perform semantic analysis on the grammatical structure to obtain semantic information;

[0061] The abstract syntax tree is constructed using the grammatical structure and the semantic information.

[0062] In the embodiment of the present invention, the tag 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 data set according to the API fully qualified name matching algorithm, wherein the ternary data set includes API query data, API sequence data and API source code.

[0064] In the embodiment of the present invention, the preset method name node refers to a "MethodDeclaration" node in an abstract syntax tree, wherein each "MethodDeclaration" node represents a specific Java method.

[0065] In an embodiment of the present invention, the API fully qualified name refers to the complete name of a class or an interface in the source code, including the package name and class name, wherein the package name is the name of the logical grouping to which the class or interface belongs, and is usually used to organize the code structure and indicate 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.

[0066] As an embodiment of the present invention, the API triple dataset formed 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 step of using 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. Use the second encoding algorithm to convert the API source code into a feature matrix to obtain the API source code vector matrix.

[0077] Exemplarily, the encoder is used to convert each data in the API triple 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. The specific implementation steps are as follows:

[0078] API query data Query = {q1,q2,…q s} is a token sequence of length s, which is converted into a fixed-length feature matrix Q∈R using CodeBERT1 s*n , where the word embedding dimension is as follows:

[0079] Q = COdeBERT1 ([Query])

[0080] The API sequence contains i APIs, that is, 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 the fully qualified names to form a text sequence, and then CodeBERT2 is used to convert this text sequence into the feature matrix A∈R of the API sequence. p*n , where p is the number of tokens in the text sequence of the entire API sequence. The specific formula is as follows:

[0081] A=CodeBERT2(concat[API1,API2,...API i ])

[0082] According to the one-to-one correspondence between API source code and API in the dataset, the API source code is encoded into a feature matrix C∈R in the same way as encoding the API sequence. q*n , where q is the number of tokens in the entire API source code sequence text. 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, API sequence vector matrix A, and API source code vector matrix C.

[0085] S4. Construct a first association matrix between the API query vector matrix and the API sequence vector matrix, and construct a second association matrix between the API query vector matrix and the API source code vector matrix.

[0086] As an embodiment of the present invention, the step of constructing a first association matrix between the API query vector matrix and the API sequence vector matrix includes:

[0087] Calculate the relevance score of the token in the API query vector matrix to each token in the API sequence vector matrix, recorded as a column vector matrix;

[0088] Calculate the relevance score of the token in the API sequence vector matrix to each token in the API query vector matrix, recorded as a row vector matrix;

[0089] The first incidence matrix is ​​constructed using the column vector matrix and the row vector matrix.

[0090] In the embodiment of the present invention, the first association 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] Among them, tanh(·) is the hyperbolic tangent function operation, which aims to map the output between -1 and 1.

[0093] In the embodiment of the present invention, the second association matrix M for constructing the API query vector matrix and the API source code vector matrix is 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 M represents the relevance score of the token in the API source code vector matrix to each token in the API query vector matrix. Q,C The column vector matrix represents the relevance score of the token in the API query vector matrix to each token in the API source code vector matrix.

[0096] Furthermore, after constructing the first association matrix between the API query vector matrix and the API sequence vector matrix, and constructing the second association matrix between the API query vector matrix and the API source code vector matrix, it also includes: training a pre-built API recommendation model.

[0097] Furthermore, the training of the pre-built API recommendation model includes:

[0098] Performing a maximum pooling operation on the first association matrix and converting it into a first attention score association matrix using a preset activation function, and performing a maximum pooling operation on the second association matrix and converting it into a second attention score association matrix using the preset activation function;

[0099] Converting 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;

[0100] Converting the first correlation matrix into a first attention score matrix using a matrix conversion method, and converting the second correlation matrix into a second attention score matrix using a matrix conversion method;

[0101] 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 semantic fusion vector of the second attention score matrix using a preset semantic fusion vector formula to obtain a second attention semantic fusion vector, and calculating the semantic fusion vector of the query attention score matrix using a preset semantic fusion vector formula to obtain a query attention semantic fusion vector;

[0102] Extracting irrelevant API sequence data and API source code data from the API query vector matrix, and using the irrelevant API sequence data to form a first irrelevant semantic fusion vector, and using the irrelevant API source code data to form a second irrelevant semantic fusion vector;

[0103] The pre-built API recommendation model is optimized through comparative learning 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 the optimization is completed.

[0104] In the embodiment of the present invention, the preset activation function adopts the SoftMax function.

[0105] Further, the using the first attention score association matrix and the second attention score association matrix to convert the API query vector matrix into a query attention score matrix includes:

[0106] The API query vector matrix is ​​converted 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] Among them, the B Q,A is the first attention score association matrix, B Q,C is the second attention score association matrix, w1 is the first trainable parameter, w2 is the second trainable parameter, B Q is the query attention score matrix.

[0111] The embodiment of the present invention converts the first correlation matrix into a first attention score matrix using a matrix conversion method, and converts the second correlation matrix into a second attention score matrix using a matrix conversion method, including:

[0112] Use the following formula for conversion:

[0113]

[0114] Among them, B A is the first attention score matrix, B C is the second attention score matrix.

[0115] Furthermore, the semantic fusion vector of the first attention score matrix is ​​calculated using a preset semantic fusion vector formula to obtain a first attention semantic fusion vector, and the semantic fusion vector of the second attention score matrix is ​​calculated using a preset semantic fusion vector formula to obtain a 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 a query attention semantic fusion vector Q′, and 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] Among them, 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] Exemplarily, the training of the pre-built API recommendation model is specifically implemented in the following steps:

[0121] Constructing triples <Q′,A′,A - > and <Q′,C′,C - >As input to a pre-built API recommendation model, where A - refers to the first irrelevant semantic fusion vector that is irrelevant to the query q, C - It refers to the second irrelevant semantic fusion vector that is irrelevant to the query q. The TropletLoss function is selected as the Loss function. 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 smaller than that between the irrelevant first irrelevant semantic fusion vector A - and the irrelevant second irrelevant semantic fusion vector C - The distance ratio between them is less than δ.

[0124] In the embodiment of the present invention, the pre-built API recommendation model uses the optimization algorithm AdamW to minimize TropletLoss, so that the model parameters are continuously updated and optimized.

[0125] S5. Obtain a user query and convert the user query 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.

[0126] S6. Calculate the sum of the first similarity and the second similarity to obtain a final similarity, and recommend the API sequence data in the preset question and answer information library to the user by using the API recommendation model according to the final similarity.

[0127] The embodiment of the present invention sorts the API sequence data in the preset question and answer information library by similarity according to the final similarity, and recommends the API sequence data with high similarity in the similarity sorting result to the user.

[0128] In an embodiment of the present invention, the preset question and answer information library refers to an information library constructed using a large amount of question information and a large amount of answer information in a question and answer website, and the API sequence data is extracted from the answer information in the preset question and answer information library.

[0129] The embodiment of the present invention obtains a source code file, uses a compiler to parse the source code file into an abstract syntax tree, and uses the abstract syntax tree to construct an API ternary data set, so as to accurately extract information in the source code. By using an encoder to convert each data in the API ternary data set into a vector matrix, a subsequent recommendation model can more accurately understand the function of each API and improve the accuracy of the recommendation. In addition, by constructing an association matrix, the correlation between the API query vector matrix and the API features can be enhanced, further improving the accuracy of the API recommendation model recommendation.

[0130] As shown in Table 1, the effectiveness of the API recommendation model in the present invention is compared with the existing API recommendation models (BIKER and CLEAR):

[0131] Table 1

[0132]

[0133] Among them, the MRR index value of the API recommendation model of the present invention is 0.473, and the Precision@1 / 3 / 5 / 10 index values ​​are 0.36 / 0.536 / 0.632 / 0.747. API recommendation exceeds BIKER by 392.71% and CLEAR by 130.73% in MRR index, and exceeds BIKER model by 490.16% / 342.98% / 357.97% / 334.3% and CLEAR model by 118.18% / 132.03% / 148.82% / 172.63% in Precision@1 / 3 / 5 / 10 index values. From the experimental results, it can be seen that API recommendation has achieved recommendation effectiveness that exceeds the existing models BIKER and CLEAR.

[0134] As shown in Table 2, the effectiveness of models using different recommendation strategies is compared:

[0135] Table 2

[0136]

[0137] Among them, the MRR index value of CLEAR-QA is 0.409, and the Precision@1 / 3 / 5 / 10 index values ​​are 0.279 / 0.453 / 0.562 / 0.701. CLEAR-QA exceeds CLEAR by 99.51% in MRR index and 69.09% / 96.10% / 121.26% / 155.84% in Precision@1 / 3 / 5 / 10 index. In addition, API recommendation exceeds API recommendation model-QQ by 161.33% in MRR index and exceeds API recommendation model-QQ by 164.71% / 155.24% / 154.84% / 161.19% in Precision@1 / 3 / 5 / 10 index. The experimental results show that the effectiveness of the API recommendation model based on the direct recommendation strategy exceeds that of the API recommendation model based on the indirect recommendation strategy. The direct recommendation strategy can effectively alleviate the impact of the coverage of the collected question titles on the effectiveness of the recommendation model. In addition, the experimental results also show that the API recommendation model of the present invention exceeds CLEAR-QA by 15.65% in MRR and 29.03% / 18.32% / 12.46% / 6.56% in Precision@1 / 3 / 5 / 10. This shows that even if both adopt the direct recommendation strategy, the effectiveness of the API recommendation model of the present invention is better than the CLEAR model.

[0138] As shown in Table 3, the effects of the API source code and the joint attention mechanism on the effectiveness of the API recommendation model in the present invention are shown in Table 3.

[0139] Table 3

[0140]

[0141] Among them, after adding API source code as a feature, the API recommendation model of the present invention improved by 20.97% in MRR indicators and 22.45% / 25.53% / 20.38% / 15.81% in Precision@1 / 3 / 5 / 10 indicators. This shows that the API source code can provide richer feature information for the effectiveness of the model, enabling the model to understand the functions of the API more accurately. After adding the joint attention mechanism, the API recommendation model of the present invention improved by 9.49% in MRR indicators and 11.8% / 9.61% / 10.1% / 4.62% in Precision@1 / 3 / 5 / 10 indicators. This shows that the joint attention mechanism can strengthen the correlation between query representation and API feature representation.

[0142] As shown in Table 4, the recommendation efficiency of the API recommendation model of the present invention is compared with the existing API recommendation models (BIKER and CLEAR):

[0143] Table 4

[0144]

[0145] The recommendation efficiency of the API recommendation model of the present invention is 0.014 seconds per item, which is 2.556 seconds per item and 0.002 seconds per item faster than BIKER and CLEAR respectively. The reason why the API recommendation model of the present invention has a high recommendation efficiency in the above experimental results is that the direct recommendation strategy skips the re-ranking step and directly ranks all API sequences according to the similarity between the query and the API features, thereby saving time.

[0146] like Figure 3 , which is a functional module diagram of an API recommendation device provided by an embodiment of the present invention.

[0147] The API recommendation device 100 of the present invention may be installed in an electronic device. According to the functions to be 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 the present invention may also be referred to as a unit, which refers to a series of computer program segments that can be executed by a processor of an electronic device and can complete fixed functions, and is stored in a 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 a source code file, parse the source code file into an abstract syntax tree using a compiler; traverse the preset method name nodes in the abstract syntax tree to obtain the API source code, and form an API ternary data set according to an API fully qualified name matching algorithm, wherein the ternary data set includes API query data, API sequence data and API source code.

[0151] In the embodiment of the present 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 the embodiment of the present 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 the embodiment of the present invention, the abstract syntax tree refers to a hierarchical representation of a source code file, and usually represents a building block of a programming language in the form of a tree.

[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 API name 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. 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 representation form is <API query, API sequence, API source code>.

[0165] The feature extraction module 102 is used to use an encoder to convert each data in the API triple 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; construct a first association matrix between the API query vector matrix and the API sequence vector matrix, and construct a second association matrix between the API query vector matrix and the API source code vector matrix.

[0166] In the embodiment of the present invention, the encoder refers to a tool for converting data into a vector matrix. For example, the encoder may be a CodeBERT encoder.

[0167] As an embodiment of the present invention, the encoder is used to convert each data in the API triple 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, including:

[0168] Extracting API query data from the API triple data set, and converting the API query data into an API query vector matrix using a first code encoding algorithm;

[0169] Extracting API sequence data from the API triple data set, and splicing the API sequences in the API sequence data according to the fully qualified name text to obtain a spliced ​​text sequence;

[0170] Using a second encoding algorithm to extract semantic features in the concatenated text sequence to obtain an API sequence vector matrix;

[0171] The API source code corresponds to the API sequence data one by one, and the API source code is converted into a feature matrix using the second encoding algorithm to obtain the API source code vector matrix.

[0172] Exemplarily, the encoder is used to convert each data in the API triple 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. The specific implementation steps are as follows:

[0173] API query data Query = {q1, q2, ...q s} is a token sequence of length s, which is converted into a fixed-length feature matrix Q∈R using CodeBERT1 s*n , where the word embedding dimension is as follows:

[0174] Q = CodeBERT1([Query])

[0175] The API sequence contains i APIs, that is, 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 the fully qualified names to form a text sequence, and then CodeBERT2 is used to convert this text sequence into the feature matrix A∈R of the API sequence. p*n , where p is the number of tokens in the text sequence of the entire API sequence. The specific formula is as follows:

[0176] A=CodeBERT2(concat[API1,API2,...API i ])

[0177] According to the one-to-one correspondence between API source code and API in the dataset, the API source code is encoded into a feature matrix C∈R in the same way as encoding the API sequence. q*n , where q is the number of tokens in the entire API source code sequence text. 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, API sequence vector matrix A, and API source code vector matrix C.

[0180] As an embodiment of the present invention, the step of constructing a first association matrix between the API query vector matrix and the API sequence vector matrix includes:

[0181] Calculate the relevance score of the token in the API query vector matrix to each token in the API sequence vector matrix, recorded as a column vector matrix;

[0182] Calculate the relevance score of the token in the API sequence vector matrix to each token in the API query vector matrix, recorded as a row vector matrix;

[0183] The first incidence matrix is ​​constructed using the column vector matrix and the row vector matrix.

[0184] In the embodiment of the present invention, the first association 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] Among them, tanh(·) is the hyperbolic tangent function operation, which aims to map the output between -1 and 1.

[0187] In the embodiment of the present invention, the second association matrix M for constructing the API query vector matrix and the API source code vector matrix is 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 M represents the relevance score of the token in the API source code vector matrix to each token in the API query vector matrix. Q,C The column vector matrix represents the relevance score of the token in the API query vector matrix to each token in the API source code vector matrix.

[0190] Furthermore, after constructing the first association matrix between the API query vector matrix and the API sequence vector matrix, and constructing the second association matrix between the API query vector matrix and the API source code vector matrix, it also includes: training a pre-built API recommendation model.

[0191] Furthermore, the training of the pre-built API recommendation model includes:

[0192] Performing a maximum pooling operation on the first association matrix and converting it into a first attention score association matrix using a preset activation function, and performing a maximum pooling operation on the second association matrix and converting it into a second attention score association matrix using the preset activation function;

[0193] Converting 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;

[0194] Converting the first correlation matrix into a first attention score matrix using a matrix conversion method, and converting the second correlation matrix into a second attention score matrix using a matrix conversion method;

[0195] 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 semantic fusion vector of the second attention score matrix using a preset semantic fusion vector formula to obtain a second attention semantic fusion vector, and calculating the semantic fusion vector of the query attention score matrix using a preset semantic fusion vector formula to obtain a query attention semantic fusion vector;

[0196] Extracting irrelevant API sequence data and API source code data from the API query vector matrix, and using the irrelevant API sequence data to form a first irrelevant semantic fusion vector, and using the irrelevant API source code data to form a second irrelevant semantic fusion vector;

[0197] The pre-built API recommendation model is optimized through comparative learning 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 the optimization is completed.

[0198] In the embodiment of the present invention, the preset activation function adopts the SoftMax function.

[0199] Further, the using the first attention score association matrix and the second attention score association matrix to convert the API query vector matrix into a query attention score matrix includes:

[0200] The API query vector matrix is ​​converted 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] Among them, the B Q,A is the first attention score association matrix, B Q,C is the second attention score association matrix, w1 is the first trainable parameter, w2 is the second trainable parameter, B Q is the query attention score matrix.

[0205] The embodiment of the present invention converts the first correlation matrix into a first attention score matrix using a matrix conversion method, and converts the second correlation matrix into a second attention score matrix using a matrix conversion method, including:

[0206] Use the following formula for conversion:

[0207]

[0208] Among them, B A is the first attention score matrix, B C is the second attention score matrix.

[0209] Furthermore, the semantic fusion vector of the first attention score matrix is ​​calculated using a preset semantic fusion vector formula to obtain a first attention semantic fusion vector, and the semantic fusion vector of the second attention score matrix is ​​calculated using a preset semantic fusion vector formula to obtain a 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 a query attention semantic fusion vector Q′, and 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] Among them, 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] Exemplarily, the training of the pre-built API recommendation model is specifically implemented in the following steps:

[0215] Constructing triples <Q′,A′,A - > and <Q′,C′,C - >As input to a pre-built API recommendation model, where A - refers to the first irrelevant semantic fusion vector that is irrelevant to the query q, C - It refers to the second irrelevant semantic fusion vector that is irrelevant to the query q. The TropletLoss function is selected as the Loss function. 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 smaller than that between the irrelevant first irrelevant semantic fusion vector A -and the irrelevant second irrelevant semantic fusion vector C - The distance ratio between them is less than δ.

[0218] In the embodiment of the present invention, the pre-built API recommendation model uses the optimization algorithm AdamW to minimize TropletLoss, so that the model parameters are continuously updated and optimized.

[0219] The API recommendation module 103 is used to obtain user queries and convert the user queries 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; sum the first similarity and the second similarity to obtain a final similarity, and recommend the API sequence data in a preset question and answer information library to the user using a recommendation model based on the final similarity.

[0220] The embodiment of the present invention sorts the API sequence data in the preset question and answer information library by similarity according to the final similarity, and recommends the API sequence data with high similarity in the similarity sorting result to the user.

[0221] In an embodiment of the present invention, the preset question and answer information library refers to an information library constructed using a large amount of question information and a large amount of answer information in a question and answer website, and the API sequence data is extracted from the answer information in the preset question and answer information library.

[0222] like Figure 3 FIG. 1 is a schematic diagram of the structure of an electronic device for implementing an API recommendation method provided by 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 stored in the memory 11 and executable on the processor 10, such as an API recommendation method program.

[0224] In some embodiments, the processor 10 may be composed of an integrated circuit, for example, a single packaged integrated circuit, or a plurality of packaged integrated circuits with the same or different functions, including one or more central processing units (CPUs), microprocessors, digital processing chips, graphics processors, and combinations of various control chips. The processor 10 is the control core (ControlUnit) of the electronic device, and uses various interfaces and lines to connect various components of the entire electronic device, and executes or executes programs or modules stored in the memory 11 (for example, executing an API recommendation method program, etc.), and calls data stored in the memory 11 to execute various functions of the electronic device and process data.

[0225] The memory 11 includes at least one type of readable storage medium, and the readable storage medium includes a flash memory, a mobile hard disk, a multimedia card, a card-type memory (for example, an SD or DX memory, etc.), a magnetic memory, a disk, an optical disk, etc. In some embodiments, the memory 11 may be an internal storage unit of an electronic device, such as a mobile hard disk of the electronic device. In other embodiments, the memory 11 may also be an external storage device of an electronic device, such as a plug-in mobile hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc. equipped on the electronic device. Further, the memory 11 may also include both an internal storage unit of the electronic device and an external storage device. The memory 11 can not only be used to store application software and various types of data installed in the electronic device, such as the code of an API recommendation method program, but also can be used to temporarily store data that has been output or is to be output.

[0226] The communication bus 12 may be 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. The bus is configured to realize connection and communication between the memory 11 and at least one processor 10, etc.

[0227] The communication interface 13 is used for communication between the above-mentioned 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, a Bluetooth interface, etc.), which is generally used to establish a communication connection between the electronic device and other electronic devices. The user interface may be a display (Display), an input unit (such as a keyboard (Keyboard)), and optionally, the user interface may also be a standard wired interface, a wireless interface. Optionally, in some embodiments, the display may be an LED display, a liquid crystal display, a touch-sensitive liquid crystal display, and an OLED (Organic Light-Emitting Diode, organic light-emitting diode) touch device, etc. Among them, the display may also be appropriately referred to as a display screen or a display unit, which is used to display information processed in the electronic device and to display a visual user interface.

[0228] Figure 3 Only an electronic device with components is shown, and those skilled in the art will understand that Figure 3 The structure shown does not constitute a limitation on the electronic device, and may include fewer or more components than shown in the figure, or combine certain components, or arrange the components differently.

[0229] For example, although not shown, the electronic device may also include a power source (such as a battery) for supplying power to each component. Preferably, the power source may be logically connected to the at least one processor 10 through a power management device, so that the power management device can realize functions such as charging management, discharging management, and power consumption management. The power source may also include one or more DC or AC power sources, recharging devices, power failure detection circuits, power converters or inverters, power status indicators, and other arbitrary components. The electronic device may also include a variety of sensors, Bluetooth modules, Wi-Fi modules, etc., which will not be repeated here.

[0230] It should be understood that the embodiment is for illustration only and the scope of the patent application is not limited to this structure.

[0231] The API recommendation method program stored in the memory 11 of the electronic device is a combination of multiple instructions. When running in the processor 10, it can achieve:

[0232] Obtain a source code file, and parse the source code file into an abstract syntax tree using a compiler;

[0233] Traversing the method name nodes preset in the abstract syntax tree to obtain the API source code, and forming an API ternary data set according to an API fully qualified name matching algorithm, wherein the ternary data set includes API query data, API sequence data, and API source code;

[0234] Using an encoder to convert each data in the API triple 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;

[0235] Constructing a first association matrix between the API query vector matrix and the API sequence vector matrix, and constructing a second association matrix between the API query vector matrix and the API source code vector matrix;

[0236] Obtaining a user query, and converting the user query into a user query vector matrix, calculating a first similarity between the user query vector matrix and the API sequence vector matrix, and calculating a 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 a final similarity, and the API sequence data in a preset question and answer information library is recommended to the user using an API recommendation model according to the final similarity.

[0238] Specifically, the specific implementation method of the processor 10 for the above instructions can refer to the description of the relevant steps in the corresponding embodiment of the accompanying drawings, which will not be repeated here.

[0239] Furthermore, if the module / unit integrated in the electronic device 1 is implemented in the form of a software functional unit and sold or used as an independent product, it 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 can include: any entity or device capable of carrying the computer program code, a recording medium, a USB flash drive, a mobile hard disk, a magnetic disk, an optical disk, a computer memory, and a read-only memory (ROM).

[0240] The present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor of an electronic device, the computer program can implement:

[0241] Obtain a source code file, and parse the source code file into an abstract syntax tree using a compiler;

[0242] Traversing the method name nodes preset in the abstract syntax tree to obtain the API source code, and forming an API ternary data set according to an API fully qualified name matching algorithm, wherein the ternary data set includes API query data, API sequence data, and API source code;

[0243] Using an encoder to convert each data in the API triple 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;

[0244] Constructing a first association matrix between the API query vector matrix and the API sequence vector matrix, and constructing a second association matrix between the API query vector matrix and the API source code vector matrix;

[0245] Obtaining a user query, and converting the user query into a user query vector matrix, calculating a first similarity between the user query vector matrix and the API sequence vector matrix, and calculating a 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 a final similarity, and the API sequence data in a preset question and answer information library is recommended to the user using an API recommendation model according to the final similarity.

[0247] In the several embodiments provided by the present invention, it should be understood that the disclosed devices, apparatuses and methods can be implemented in other ways. For example, the device embodiments described above are only schematic, for example, the division of the modules is only a logical function division, and there may be other division methods in actual implementation.

[0248] 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.

[0249] In addition, each functional module in each embodiment of the present invention may be integrated into one processing unit, each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of hardware plus software functional modules.

[0250] It is obvious to those skilled in the art that the present invention is not limited to the details of the above exemplary embodiments, 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, no matter from which point of view, the embodiments should be regarded as illustrative and non-restrictive, and the scope of the present invention is limited by the appended claims rather than the above description, so it is intended that all changes falling within the meaning and scope of the equivalent elements of the claims are included in the present invention. Any attached figure mark in the claims should not be regarded as limiting the claims involved.

[0252] The blockchain referred to in this invention is a new application mode of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanism, encryption algorithm, etc. Blockchain is essentially a decentralized database, a string of data blocks generated by cryptographic methods. Each data block contains a batch of network transaction information, which is used to verify the validity of its information (anti-counterfeiting) and generate the next block. Blockchain can include the underlying blockchain platform, platform product service layer, and application service layer.

[0253] The embodiments of the present application can acquire and process relevant data based on artificial intelligence technology. Among them, artificial intelligence (AI) is the theory, method, technology and application system that uses digital computers or machines controlled by digital computers to simulate, extend and expand human intelligence, perceive the environment, acquire knowledge and use knowledge to obtain the best results.

[0254] In addition, 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 stated in the system claim can also be implemented by one unit or device through software or hardware. The words first, second, etc. are used to indicate names, and do not indicate any particular order.

[0255] Finally, it should be noted that the above embodiments are only used to illustrate the technical solution of the present invention rather than to limit it. Although the present invention has been described in detail with reference to the preferred embodiments, those skilled in the art should understand that the technical solution of the present invention can be modified or replaced by equivalents without departing from the spirit and scope of the technical solution of the present invention.

Claims

1. An API recommendation method, characterized in that: The method comprises: Obtain a source code file, and parse the source code file into an abstract syntax tree using a compiler; Traversing the method name nodes preset in the abstract syntax tree to obtain the API source code, and forming an API ternary data set according to an API fully qualified name matching algorithm, wherein the ternary data set includes API query data, API sequence data, and API source code; Using an encoder to convert each data in the API triple 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; Constructing a first association matrix between the API query vector matrix and the API sequence vector matrix, and constructing a second association matrix between the API query vector matrix and the API source code vector matrix; Obtaining a user query, and converting the user query into a user query vector matrix, calculating a first similarity between the user query vector matrix and the API sequence vector matrix, and calculating a 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 a final similarity, and the API sequence data in a preset question and answer information library is recommended to the user using an API recommendation model according to the final similarity.

2. The API recommendation method according to claim 1, characterized in that: The method of using a compiler to parse the source code file into an abstract syntax tree includes: Decomposing the source code file into tokens using a lexical analyzer; The tags are combined into a grammatical structure according to grammatical rules, and a compiler is used to perform semantic analysis on the grammatical structure to obtain semantic information; The abstract syntax tree is constructed using the grammatical structure and the semantic information.

3. The API recommendation method according to claim 1, characterized in that: The API triple data set is formed according to the API fully qualified name matching algorithm, including: Extracting source code corresponding to the API method body node in the abstract syntax tree, and selecting source code with the richest method body information from the source code to obtain the API source code; The preset API query data and API sequence data are used to form a preset API data set, and the API source code is associated with the preset API data set to obtain an API ternary data set.

4. The API recommendation method according to claim 1, characterized in that: The encoder is used to convert each data in the API triple 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, including: Extracting API query data from the API triple data set, and converting the API query data into an API query vector matrix using a first code encoding algorithm; Extracting API sequence data from the API triple data set, and splicing the API sequences in the API sequence data according to the fully qualified name text to obtain a spliced ​​text sequence; Using a second encoding algorithm to extract semantic features in the concatenated text sequence to obtain an API sequence vector matrix; The API source code corresponds to the API sequence data one by one, and the API source code is converted into a feature matrix using the second encoding algorithm to obtain the API source code vector matrix.

5. The API recommendation method according to claim 1, characterized in that: The step of constructing a first association matrix between the API query vector matrix and the API sequence vector matrix includes: Calculate the relevance score of the token in the API query vector matrix to each token in the API sequence vector matrix, recorded as a column vector matrix; Calculate the relevance score of the token in the API sequence vector matrix to each token in the API query vector matrix, recorded as a row vector matrix; The first incidence matrix is ​​constructed using the column vector matrix and the row vector matrix.

6. The API recommendation method according to claim 1, characterized in that: After constructing the first association matrix between the API query vector matrix and the API sequence vector matrix, and constructing the second association matrix between the API query vector matrix and the API source code vector matrix, the method further includes: training a pre-built API recommendation model.

7. The API recommendation method according to claim 6, characterized in that: The training of the pre-built API recommendation model includes: Performing a maximum pooling operation on the first association matrix and converting it into a first attention score association matrix using a preset activation function, and performing a maximum pooling operation on the second association matrix and converting it into a second attention score association matrix using the preset activation function; Converting 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; Converting the first correlation matrix into a first attention score matrix using a matrix conversion method, and converting the second correlation matrix into a second attention score matrix using a matrix conversion 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 semantic fusion vector of the second attention score matrix using a preset semantic fusion vector formula to obtain a second attention semantic fusion vector, and calculating the semantic fusion vector of the query attention score matrix using a preset semantic fusion vector formula to obtain a query attention semantic fusion vector; Extracting irrelevant API sequence data and API source code data from the API query vector matrix, and using the irrelevant API sequence data to form a first irrelevant semantic fusion vector, and using the irrelevant API source code data to form a second irrelevant semantic fusion vector; The pre-built API recommendation model is optimized through comparative learning 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 the optimization is completed.

8. An API recommendation device, characterized in that: The device can implement the API recommendation method according to any one of claims 1 to 7, and the device includes: A source code file processing module is used to obtain a source code file, parse the source code file into an abstract syntax tree using a compiler; traverse the method name nodes preset in the abstract syntax tree to obtain the API source code, and form an API ternary data set according to an API fully qualified name matching algorithm, wherein the ternary data set includes API query data, API sequence data, and API source code; A feature extraction module is used to convert each data in the API triple 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; construct a first association matrix between the API query vector matrix and the API sequence vector matrix, and construct a second association matrix between the API query vector matrix and the API source code vector matrix; The API recommendation module is used to obtain user queries and convert the user queries 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; sum the first similarity and the second similarity to obtain a final similarity, and recommend the API sequence data in a preset question and answer information library to the user using a recommendation model based on the final similarity.

9. An electronic device, characterized in that: The electronic device comprises: at least one processor; and, a memory communicatively connected to the at least one processor; wherein, The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the API recommendation method as described in any one of claims 1 to 7.

10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the API recommendation method according to any one of claims 1 to 7 is implemented.

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

  • Semantic code searching method based on fine-grained common attention mechanism

    CN114237621A

  • Recommending web API's and associated endpoints

    US20200372088A1

  • Recommendation engine for application programming interface (API) in a multi-cloud environment

    US20220012118A1