APIR ec X : Cross-Library API Recommendation via Pre-Trained Language Model (2021)

Abstract: For programmers, learning the usage of APIs (Application Programming Interfaces) of a software library is important yet difficult. API recommendation tools can help developers use APIs by recommending which APIs to be used next given the APIs that have been written. Traditionally, language models such as N-gram are applied to API recommendation. However, because the software libraries keep changing and new libraries keep emerging, new APIs are common. These new APIs can be seen as OOV (out of vocabulary) words and cannot be handled well by existing API recommendation approaches due to the lack of training data. In this paper, we propose APIRecX, the first cross-library API recommendation approach, which uses BPE to split each API call in each API sequence and pre-trains a GPT based language model. It then recommends APIs by fine-tuning the pre-trained model. APIRecX can migrate the knowledge of existing libraries to a new library, and can recommend APIs that are previously regarded as OOV. We evaluate APIRecX on six libraries and the results confirm its effectiveness by comparing with two typical API recommendation approaches.

## introduction application programming interface (api) is an integral part of software libraries. being familiar with apis could help improve programming productivity. however, a library tends to contain a large number of apis and there could be complex dependencies among apis, and thus understanding all apis in a library is very challenging, especially for new developers. to facilitate correct and efficient usage of apis during programming, many api recommendation approaches @xcite @xcite @xcite have been proposed. more specifically, api recommendation aims to automatically recommend a correct api call at the current programming location based on its preceding part of code information. as an example, listing 1 shows a java code snippet about opening a text file. assuming a programmer forgets what to write in line 6. api recommendation tool can help the programmer by prompting the most likely api call to be used next. in this case, printstackt race() will be returned. the api recommendation tools do so by learning api usage pattern from a large code corpus. some tools @xcite use probabilistic models to learn api usage pattern , while others @xcite use data mining methods to find api usage patterns . recently, deep learning based language models are proposed to model the api sequences and have obtained promising results in recommending apis @xcite @xcite . however, the existing api recommendation tools only focus on improving the performance of api recommendation when api usage data are sufficient (i.e., the usage data of the apis to be recommended are sufficient in training data). that is, they mostly ignored the oov (out of vocabulary) problem, which could have negative impact on the performance of api recommendation. more specifically, when some apis are unseen in training data, these approaches cannot recommend them correctly. the oov problem could be more serious for a new library, since it is very difficult to collect sufficient api usage data. to conduct api recommendation for new libraries, cross-library api recommendation is a potentially feasible solution, which aims to recommend apis in new libraries based on the usage data of apis in other libraries, but it is still an open challenge due to the inherent oov problem. for example, as shown in listing 2, we may rarely (or even never) see sqlexception.printstacktrace() in the training set, but the usage of exception is very common in the training set and the usage of sqlexception and exception are similar. so if we use a word segmentation algorithm to split sqlexception.printstacktrace() into the sequence: sql-exception-.-print-stacktrace(), we can use the exception usage pattern learned during the training process to predict the printstacktrace() method and finally synthesize sqlexception.printstacktrace() as the recommendation result. ¦ ¥ ## approach in the paper, we propose apirecx, the first approach for cross-library api recommendation. with apirecx, we can recommend apis in some libraries (especially new libraries) by learning from a large amount of api usage data of some other libraries. ## conclusions we propose the first approach apirecx for crosslibrary api recommendation, which can automatically recommend api calls for new libraries. apirecx first splits each api call into a sequence of subwords to relieve the oov problem at the api level. it then pre-trains a gpt-based subword language model based on a large number of api usage data from other libraries. by finetuning the pre-trained model with a sample of api usage data of new libraries, apirecx conducts subword prediction and incorporates beam search to compose a complete api call for recommendation. we conduct an extensive study based on six libraries of three domains for mimicking new libraries and 14,000 github java projects for pre-training, demonstrating the effectiveness of apirecx. however, our work also has certain limitation, which is the generalization of our results and findings. although we invested significant time and effort to prepare datasets, conducted experiments and analyzed results, our experiments involved only one program language with three domains. the performance of our neural architecture, and especially the findings on transfer learning, could be different with other programming languages or libraries. in the future, we will try to get rid of this limitation by applying our approach to more languages/libraries. the source code of apirecx and experimental data can be found in https://github.com/ yuningkang/apirecx .