Query-based API (Application Program Interface) recommendation method combined with context enhancement
By building a triple corpus and using word vector model and BLAST algorithm, the context information modeling of API recommendations is enhanced, and the problem of traditional API recommendation methods ignore code context is solved, and more accurate and effective API recommendations are achieved.
Patent Information
- Application Number
- CN202410905144.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-08
- Publication Date
- 2024-11-08
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
Traditional API recommendation methods ignore the code context environment, resulting in the recommendation results being inaccurate enough to fully understand the semantics and relevance of the query.
By constructing a triple corpus, using word vector model and non-negative tensor decomposition, combined with the BLAST algorithm, the context information modeling of API recommendations is enhanced, and the most matching context is identified to improve the accuracy of API recommendations.
It significantly improves the accuracy and effectiveness of API recommendations, captures the deep semantics behind developers' query intent, makes up for the vocabulary gap between queries, and provides a more accurate list of API recommendations.
Smart Images

Figure CN118916540A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of API recommendation, and in particular relates to a query-based API recommendation method combined with context enhancement. Background Art
[0002] In the software development process, API (Application Programming Interface) provides a way to implement specific functions, which allows developers to use existing code to create more complex applications. As the modern software development environment becomes increasingly complex, timely and accurate recommendation of relevant APIs is essential to improving development efficiency. However, traditional API recommendation methods often rely on static code analysis or simple keyword-based searches, which ignore the code context and result in inaccurate recommendation results.
[0003] Traditional query-based methods consider extracting titles from the StackOverflow website, calculating the similarity between user queries and the title corpus, and identifying the APIs of the most similar k candidate query statements. This method cannot fully understand the semantics and context of the query. To address this problem, this study proposes a method that combines contextual information to enhance API recommendation-based methods to better understand and use the environment and semantic information related to the current development task. First, due to the differences in developers' expressions, the model is helped to better understand different expressions by replacing keywords in the query with synonyms; secondly, this study introduces contextual information to enrich query-based API recommendations, and constructs a multidimensional tensor to represent the relationship between the query, API, and context to further improve the accuracy of API recommendations; finally, in order to narrow the scope of candidate APIs, the BLAST algorithm is applied for double sequence alignment to find the most matching sequence in the current context with the matrix containing the API and context, and the APIs are re-ranked. Summary of the invention
[0004] The technical problem to be solved by the present invention is to provide a method for enhancing query-based API recommendations in combination with context. The method for enhancing query-based API recommendations in combination with context proposed by the present invention aims to model the context information of code snippets as tensors, and use it to improve query-based API recommendations, focusing on more context information, thereby improving the effectiveness of API recommendations.
[0005] To solve the above technical problems, an embodiment of the present invention provides a method for enhancing query-based API recommendation in combination with context, comprising the following steps:
[0006] S1. Use the query statements, contexts and corresponding APIs collected from the StackOverflow website to build a triple corpus.
[0007] S2. Train a word vector model (word2vec) using query statements and construct a word IDF vocabulary table;
[0008] S3. Retrieve the top-k similar tuples for a given user query using the triple corpus collected in step S1;
[0009] S4. Combine the retrieved query statements, as well as the relevant context and APIs, into a tensor;
[0010] S5. Complete the missing values in the tensor according to non-negative tensor factorization (NNTF);
[0011] S6. Use the basic local similarity alignment search tool (BLAST) algorithm to match the most similar context in the tensor, reorder the APIs according to the values of the entries in the tensor, and return the obtained API recommendation list to the user.
[0012] Preferably, in step S1, a filtering method is used to collect and process data from StackOverflow, and a triple corpus containing queries, context information, and APIs is constructed, including the following steps:
[0013] S1.1. For each answer in the posts on the StackOverflow website, extract a tuple <query, API, context>, and only retain the answers with positive scores or accepted answers;
[0014] S1.2. Extract the title of the post as the query;
[0015] S1.3. Extract the hyperlinks and plain text contained in each HTML tag in the answer;
[0016] S1.4. For the context information, focus on the statements used to instantiate classes and the statements used for method calls and use program analysis;
[0017] S1.5. Check each hyperlink in the answer and use regular expressions to identify the full name of the corresponding API method;
[0018] S1.6. Construct a triple corpus from the extracted queries, context, and APIs.
[0019] Preferably, in step S2, the similarity between two queries is measured, and a domain-specific language model is constructed, including the following steps:
[0020] S2.1. Perform synonym replacement on the queries extracted from StackOverflow, and replace the keywords in the queries with synonyms or related words;
[0021] S2.2, tokenize the reconstructed query and perform stemming to convert each word into its root form;
[0022] S2.3. Use word2vec to train a word embedding model and build a word IDF (inverse document frequency) vocabulary.
[0023] Preferably, in step S3, from the triple corpus constructed in step S1, using the word embedding model trained in step S2, top-k queries similar to the user query are retrieved, comprising the following steps:
[0024] S3.1, accept the query input by the user and perform the same preprocessing, including word segmentation and standardization;
[0025] S3.2, using the word embedding model trained in step S2 to convert the processed query into a vector;
[0026] S3.3. Use the similarity measurement method to calculate the similarity between the user query vector and all query vectors in the corpus. The formula is as follows:
[0027]
[0028] Where sim(w,Q) is the maximum value of sim(w,w') for each word w'∈Q, and sim(w,w') is the cosine similarity of the word embedding vectors of w and w';
[0029] S3.4. Sort all queries according to the similarity calculated in step S3.3, and select the top k queries with the highest similarity as results and return them to the user.
[0030] Preferably, in step S4, according to the ternary query <query-api-context>Relational representation of tensors consists of the following steps:
[0031] S4.1. Define the tensor structure and determine the dimension and structure of the tensor;
[0032] S4.2, load the triple corpus in step S1 and create a matrix for the first context;
[0033] S4.3, based on the top-k similar contexts obtained in step S3, traverse the context dimensions to create a matrix;
[0034] S4.4. Construct a tensor, wherein the slice of the tensor is the matrix obtained in step S4.3.
[0035] Preferably, in step S5, discovering potential patterns and processing missing values in the data by processing multidimensional data includes the following steps:
[0036] S5.1, construct a tensor containing known data points according to step S4.4, using 0 to represent missing data points;
[0037] S5.2. After constructing the tensor, the alternating least squares (ALS) algorithm is used to calculate the decomposition, which approximates the original tensor by gradually optimizing each factor matrix so that each element of the tensor obtains an estimated value during the decomposition and reconstruction process;
[0038] S5.3. Introduce a regularization term and use non-negative constraints to ensure that the elements of each factor matrix after decomposition are non-negative values.
[0039] Preferably, in step S6, a BLAST algorithm is used to enhance the relevance and accuracy of the API recommendation, understand and match the user's current programming context, and obtain a reordered API recommendation list, including the following steps:
[0040] S6.1. Starting from the current query and the partially completed code snippet, obtain the context according to the method described in step S1.
[0041] S6.2, tokenize and root the query;
[0042] S6.3. Create an API-context matrix M, where each element represents the data related to the API and context obtained by indexing from the tensor;
[0043] S6.4, using the BLAST algorithm, comparing the current context context with the context in the API-context matrix M;
[0044] S6.5, treating the two contexts in step S6.4 as sequences, quickly identifying potential matching regions, and selecting short segments in the current context sequence, called scode;
[0045] S6.6, identifying sequence segments that completely match the scode in step S6.5, and determining the exact locations where the scode appears in each context;
[0046] S6.7, starting from the initial matching scode position, expand to both ends to find longer similar sequences;
[0047] S6.8, record the similarity score and matching information of each comparison, and select the contexts with the highest scores as the most relevant matching results;
[0048] S6.9. Select relevant APIs based on the context in the API-context matrix that is most similar to the current context, reorder these APIs based on the matching scores, generate a recommended API list, and provide it to users as a reference when programming.
[0049] The beneficial effects of the above technical solution of the present invention are as follows:
[0050] (1) The present invention considers the importance of using context information in API recommendation and proposes a method for modeling context information using tensors and word embeddings to represent the relationship between query, API, and context. The BLAST algorithm of double sequence alignment is applied to identify the most matching context, thereby improving API recommendation.
[0051] (2) The present invention performs synonym replacement on query statements to train a word embedding model, captures the deep semantics behind the developer's query intent, introduces context information to construct a tensor, and uses the BLAST algorithm to perform a double sequence comparison to find the most matching context, thereby improving the accuracy of API recommendations;
[0052] (3) In order to make up for the vocabulary gap between queries, the present invention uses a text similarity measure based on word embedding, utilizes non-negative tensor decomposition to complete the missing values in the tensor, and uses the BLAST algorithm to identify the most matching context, thereby better realizing API recommendation. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] Figure 1 is a flow chart of the present invention;
[0054] Figure 2 An overall flowchart for API recommendations. DETAILED DESCRIPTION
[0055] In order to make the technical problems, technical solutions and advantages to be solved by the present invention more clear, a detailed description will be given below with reference to the accompanying drawings and specific embodiments.
[0056] like Figure 1 , Figure 2 As shown, an embodiment of the present invention provides a method for enhancing query-based API recommendation in combination with context, comprising the following steps:
[0057] S1. Collect query statements, contexts, and corresponding APIs from the StackOverflow website to build a triple corpus. The data collection and processing includes the following steps:
[0058] S1.1. Download the official data dump of StackOverflow. This embodiment uses the data of BIKER.
[0059] S1.1.1. Considering that the focus is on Java API recommendation, 1,347,908 questions tagged as "Java" were extracted;
[0060] S1.1.2, a filtering method was used to collect 125,847 questions;
[0061] S1.2. For each answer in the question, extract a tuple<query,API,context> ;
[0062] S1.2.1, extract the title of the question as the query;
[0063] S1.2.2. Extract each HTML tag in the answer <code>Hyperlinks and plain text contained in;
[0064] S1.2.3. For context, two types of statements are analyzed, namely, statements for instantiating classes and statements for method calls, and control flow related information related to these two types of statements is also included;
[0065] S1.2.4, encode the context, and represent the context as a string;
[0066] S1.2.5. To more comprehensively detect the API in the answer, check each hyperlink and use regular expressions to identify the full name of the corresponding API method;
[0067] S1.2.6, finally extracted 62067 forms of<query,API,context> The triplet prediction library;
[0068] S1.3,Prepare the test dataset.,This study used 458 questions collected by CARTF as the test dataset;
[0069] S1.3.1, introduce context code, take 0%, 20%, 40%, 60% and 80% of the length of each program as context, and the API used in the rest of the program is the real value;
[0070] S1.3.2. For each program length, the number of class-level recommendation queries collected was 458, 453, 429, 375 and 293, and the number of method-level recommendation queries collected was 458, 455, 445, 412 and 338, respectively, to form the test dataset.
[0071] S2. Use the query statement to train the word vector model and build a word IDF vocabulary, including the following steps:
[0072] S2.1, load the triple corpus obtained in step S1.2.6, extract only the query field, and form title.pkl;
[0073] S2.1.1. Break each query in S2.1 into words using the Natural Language Processing Toolkit (NLTK).
[0074] S2.1.2, use SnowballStemmer to convert words into their root forms;
[0075] S2.2, for each marked word, search in the synonym set provided by WordNet and replace it based on the context information;
[0076] S2.3, set the parameters of the Word2Vec model, vector size is 100, window size is 5, and minimum word frequency is 5;
[0077] S2.4. Use the preprocessed text data to train the Word2Vec model and save the trained model as skip_w2v_model_stemmed;
[0078] S2.5, build IDF vocabulary;
[0079] S2.5.1. Calculate the inverse document frequency (IDF) for each word in the training set. The IDF value is the logarithm of the inverse of the number of documents containing the word.
[0080] S2.5.2. Store the words and their IDF values in a vocabulary table named a_idf.
[0081] S3, from the triple corpus constructed in step S1, using the word embedding model trained in step S2, retrieve the top k queries similar to the user query, including the following steps:
[0082] S3.1, receiving a query input by a user, and preprocessing the user query, including word segmentation and standardization;
[0083] S3.2, using the word embedding model trained in step S2 to convert the processed query into a vector;
[0084] S3.3, calculating the similarity score;
[0085] S3.3.1. Use the word vector and IDF value to define a similarity calculation function, calculate the asymmetric similarity score between the two queries, and use the following similarity measurement method to calculate the similarity between the user query vector T and all query vectors Q in the corpus. The formula is as follows:
[0086]
[0087] Where sim(w,Q) is the maximum value of sim(w,w') for each word w'∈Q, and sim(w,w') is the cosine similarity of the word embedding vectors of w and w';
[0088] S3.3.2. Calculate the similarity score between T and Q using the following formula:
[0089]
[0090] Among them, sim(T→Q) represents the performance of the words in query T in query Q, and sim(T,Q) is calculated using the harmonic mean of these two asymmetric scores;
[0091] S3.4, retrieve the top k queries;
[0092] S3.4.1, sort all queries according to the calculated similarity;
[0093] S3.4.2, select the top k queries with the highest similarity and return them to the user as the result;
[0094] S3.5. Determine the optimal hyperparameters, where k=15 is set as the class-level API recommendation and k=10 is set as the method-level API recommendation.
[0095] S4. Given a user query, the retrieved query statement, API, and related context information are represented as a tensor, thereby effectively capturing the complex relationship between the query statement, API, and context, including the following steps:
[0096] S4.1. Determine the dimension and structure of the tensor and define a third-order tensor:
[0097] The first dimension represents the number of different query statements L;
[0098] The second dimension represents the number of APIs M associated with each query;
[0099] The third dimension represents the number of contexts N associated with each query;
[0100] S4.2. Based on the similar query set determined in S3.4.2, generate all valid query, API, and context combinations, where each valid combination forms a triple;
[0101] S4.3. Create a three-dimensional array based on the dimensions determined in S4.1 The tensor y=1 indicates an observed value, and y=0 indicates a missing value;
[0102] S4.4. Create a matrix Q(c) for each context. This matrix is a slice of the tensor y, representing the relationship between all queries and APIs under a specific context.
[0103] S4.4.1. For each context, define a matrix Q(c) whose dimension is L*M, where L is the total number of queries and M is the total number of APIs. Each entry in the matrix Q(c) is query,api Indicates whether the given context context exists, according to the corresponding entry in tensor y. If the query query and API are related in the context context, then Q(c) query,api The value of is 1, otherwise it is 0;
[0104] S4.4.2. Repeat the above process for each context in the dataset, thereby generating a corresponding matrix Q(c) for each context.
[0105] S4.5. Merge all matrices Q(c) back into the three-dimensional tensor y, where each slice represents a specific context.
[0106] S5. Use non-negative tensor decomposition technology to fill missing values in the data, including the following steps:
[0107] S5.1. Construct a three-dimensional tensor y based on the collected data (query, API, context) triples. The specific construction process is as shown in step S4. The setting of each entry of the tensor is the same as step S4.3.1.
[0108] S5.2. For non-negative CP decomposition, decompose the tensor into the sum of several rank-1 tensors. According to the given three-dimensional tensor Its CP decomposition can be expressed as follows:
[0109]
[0110] Among them, U represents the factor matrix of query modality, S represents the factor matrix of API modality, and T represents the factor matrix of context modality. where u r ,s r , t r are non-negative vectors representing factors of different modes, and R is the rank of tensor decomposition;
[0111] S5.3. Use the alternative least squares (ALS) algorithm to minimize the reconstruction error and ensure that all factor matrices are non-negative. The optimization problem can be expressed as follows:
[0112]
[0113] Among them, ||·|| F is the Frobenius norm;
[0114] S5.4. When the rank is high or sparse, it is necessary to add a regularization term to the optimization function to prevent overfitting. The company of the regularization term is as follows:
[0115]
[0116] Where λ is the regularization coefficient;
[0117] S5.5. Use the optimized decomposition model to predict the missing entries in the tensor y. For each missing element Its predicted value is given by the outer product of the corresponding factor vectors as follows:
[0118]
[0119] Here, R is set to half the minimum dimension of query, API, and context.
[0120] S6. Match the user's most similar current programming context from the tensor through the BLAST algorithm to obtain a re-ranked API recommendation list, including the following steps:
[0121] S6.1. Extract context from the current query and the partially completed code snippet;
[0122] S6.1.1. Tokenize and root the code snippets to unify the words in the code and query into basic forms to facilitate subsequent comparison and analysis;
[0123] S6.2. Using the information extracted from the dataset, construct an API-context matrix M. Each entry in the matrix M api,context Represents the relationship between an API and a context, and this relationship is indexed from the constructed tensor y;
[0124] S6.3. Since BLAST compares sequence data, the current context and the context in API-context (M) are considered as sequences;
[0125] S6.4, using the BLAST algorithm to compare the context sequence in the current context with the context sequence in the API-context (M);
[0126] S6.4.1. Find short sequence matching points in the two compared sequences of the current context context and API-context (M);
[0127] S6.4.2. Expand the sequence matching around each short sequence to find a longer continuous matching sequence;
[0128] S6.4.3, obtaining a score based on the similarity to evaluate the similarity of the two sequences;
[0129]
[0130] Among them, S(a i ,b i ) are the two characters a at position i in the comparison i and b i The score, W k is the penalty weight;
[0131] S6.5. Calculate the similarity score between the current context and each context in the API-context matrix (M) based on the output of BLAST, and determine the API context that best matches the current context;
[0132] S6.6. Re-rank the APIs based on the similarity scores;
[0133] S6.7. Extract the sorted API list from API-context (M) and return these APIs to the user as the final recommendation list; specifically, for each program length, this study collected 458, 453, 429, 375 and 293 class-level recommendation queries, and 458, 455, 445, 412 and 338 method-level recommendation queries, respectively, to form the test dataset.
[0134] For the number of class-level recommendation queries, 458, 453, 429, 375, and 293, the MRR values are 0.70, 0.68, 0.64, 0.55, and 0.51, respectively; the MAP values are 0.58, 0.58, 0.57, 0.52, and 0.50, respectively; the success rates are 0.95, 0.94, 0.89, 0.82, and 0.80, respectively; the recalls are 0.82, 0.82, 0.81, 0.78, and 0.79, respectively; the precisions are 0.15, 0.14, 0.12, 0.10, and 0.08, respectively; and the NDCGs are 0.74, 0.73, 0.70, 0.61, and 0.58, respectively; The number of method-level recommendation queries is 458, 455, 445, 412 and 338, and the MRR values are 0.60, 0.57, 0.53, 0.46 and 0.40, respectively, the MAP values are 0.45, 0.42, 0.42, 0.40 and 0.37, the success rates are 0.83, 0.82, 0.77, 0.67 and 0.61, the recalls are 0.64, 0.63, 0.62, 0.59 and 0.57, the precisions are 0.13, 0.13, 0.11, 0.08 and 0.07, and the NDCGs are 0.65, 0.62, 0.58, 0.51 and 0.44, respectively. Experiments show that the present invention can effectively provide the API recommendations required by users and recommend the APIs that users want during programming.
[0135] The above is a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.< / code>
Claims
1. A method for enhancing query-based API recommendation in combination with context, characterized in that: It includes the following steps: S1. Use the query statements, contexts, and corresponding APIs collected from the StackOverflow website to construct a triple corpus; S2. Use the query statements to train a word vector model and construct a word IDF vocabulary; S3. Use the triple corpus collected in step S1 to retrieve the top-k similar tuples for a given user query; S4. Combine the retrieved query statements, as well as the relevant contexts and APIs, into a tensor; S5. Complete the missing values in the tensor according to non-negative tensor decomposition; S6. Use the basic local similarity alignment search tool algorithm to match the most similar context in the tensor, reorder the APIs according to the values of the entries in the tensor, and return the obtained API recommendation list to the user.
2. The method for enhancing query-based API recommendation in combination with context according to claim 1, characterized in that: In step S1, a filtering method is used to collect and process data from StackOverflow to construct a triple corpus containing queries, context information, and APIs, including the following steps: S1.
1. For each answer in the posts on the StackOverflow website, extract a tuple <query, API, context>, and only retain the answers with positive scores or accepted answers; S1.
2. Extract the title of the post as the query; S1.
3. Extract the hyperlinks and plain text contained in each HTML tag in the answer; S1.
4. For the context information, focus on the statements used to instantiate classes and the statements used for method calls and use program analysis; S1.
5. Check each hyperlink in the answer and use regular expressions to identify the full name of the corresponding API method; S1.
6. Construct a triple corpus with the extracted queries, contexts, and APIs.
3. The method for enhancing query-based API recommendation in combination with context according to claim 1, characterized in that: In step S2, measure the similarity between two queries and construct a domain-specific language model, including the following steps: S2.
1. Perform synonym replacement on the queries extracted from StackOverflow, and use synonyms or related words to replace the keywords in the queries; S2.
2. Tokenize the reconstructed query statements and perform stemming to convert each word to its root form; S2.
3. Use word2vec to train a word embedding model and construct a word IDF vocabulary.
4. The method for enhancing query-based API recommendation in combination with context according to claim 1, characterized in that: In step S3, from the triple corpus constructed in step S1, use the word embedding model trained in step S2 to retrieve the top-k queries similar to the user query, including the following steps: S3.
1. Accept the query input by the user and perform the same preprocessing, including word segmentation and normalization; S3.
2. Use the word embedding model trained in step S2 to convert the processed query into a vector; S3.
3. Use a similarity metric method to calculate the similarity between the user query vector and all query vectors in the corpus. The formula is as follows: where sim(w,Q) is the maximum value of sim(w,w') for each word w'∈Q, and sim(w,w') is the cosine similarity of the word embedding vectors of w and w'; S3.
4. Sort all queries according to the similarity calculated in step S3.3, and select the top k queries with the highest similarity as results and return them to the user.
5. The method for enhancing query-based API recommendation in combination with context according to claim 1, characterized in that: In step S4, according to the ternary query <query-api-context> Relational representation of tensors consists of the following steps:< / query-api-context> S4.
1. Define the tensor structure and determine the dimension and structure of the tensor; S4.2, load the triple corpus in step S1 and create a matrix for the first context; S4.3, based on the top-k similar contexts obtained in step S3, traverse the context dimensions to create a matrix; S4.
4. Construct a tensor, wherein the slice of the tensor is the matrix obtained in step S4.
3.
6. The method for enhancing query-based API recommendation in combination with context according to claim 1, characterized in that: In step S5, by processing multidimensional data, potential patterns are discovered and missing values in the data are processed, including the following steps: S5.1, construct a tensor containing known data points according to step S4.4, using 0 to represent missing data points; S5.
2. After constructing the tensor, the alternating least squares algorithm is used to calculate the decomposition, and each factor matrix is gradually optimized to approximate the original tensor, so that each element of the tensor obtains an estimated value during the decomposition and reconstruction process; S5.
3. Introduce a regularization term and use non-negative constraints to ensure that the elements of each factor matrix after decomposition are non-negative values.
7. The method for enhancing query-based API recommendation in combination with context according to claim 1, characterized in that: In step S6, the BLAST algorithm is used to enhance the relevance and accuracy of the API recommendation, understand and match the user's current programming context, and obtain a re-ordered API recommendation list, including the following steps: S6.
1. Starting from the current query and the partially completed code snippet, obtain the context according to the method described in step S1. S6.2, tokenize and root the query; S6.
3. Create an API-context matrix M, where each element represents the data related to the API and context obtained by indexing from the tensor; S6.4, using the BLAST algorithm, comparing the current context context with the context in the API-context matrix M; S6.5, treating the two contexts in step S6.4 as sequences, quickly identifying potential matching regions, and selecting short segments in the current context sequence, called scode; S6.6, identifying sequence segments that completely match the scode in step S6.5, and determining the exact locations where the scode appears in each context; S6.7, starting from the initial matching scode position, expand to both ends to find longer similar sequences; S6.8, record the similarity score and matching information of each comparison, and select the contexts with the highest scores as the most relevant matching results; S6.
9. Select relevant APIs based on the context in the API-context matrix that is most similar to the current context, reorder these APIs based on the matching scores, generate a recommended API list, and provide it to users as a reference when programming.
Citation Information
Patent Citations
API recommendation method based on word embedding technology
CN110990003A
Solidiity annotation generation method based on context learning and large language model
CN117492825A
