Generative API Recommendation Based on Global Semantics and Local Context
Shuoming Li, Dongjin Yu, Xin Chen, Xulin Fan, Dengfa Luo, Tong Wu, Wangliang Yan · International Journal of Software Engineering and Knowledge Engineering · 2025
During software development, developers often need appropriate but unfamiliar APIs to implement a specific functionality. Under such circumstances, developers tend to leverage search tools to seek for the relevant APIs. However, there are always semantic gaps between query words and APIs, which negatively affects the performance of these tools. In this study, we introduce Glo-APIRec, a method that combines global semantics with local context to estimate the semantic relevance between query words and APIs to recommend APIs. In this method, the Transformer model is employed to obtain global semantics, while the Word2Vec model is utilized to capture local context using a fixed-size window. First, Glo-APIRec collects millions of Java projects from GitHub to construct the corpus. Afterward, a set of tuples consisting of words and APIs is built by extracting comments and API sequences from the source code files. Finally, Transformer is employed to capture long distance semantics about API sequences and code comments. Meanwhile, Word2Vec is used to generate word vectors to capture the local context by introducing the random shuffling strategy to break the positions of words and APIs in the tuples. We evaluate the performance of Glo-APIRec with 30 sentence-level queries. Experimental results show that Glo-APIRec can achieve 0.600 in terms of SuccessRate for top-1 recommendation and 0.900 for top-10 recommendation. When recommending 10 APIs, Glo-APIRec can achieve 0.480, 0.703 and 0.717 in terms of precision, Mean Reciprocal Rank ([Formula: see text]) and Normalized Discounted Cumulative Gain ([Formula: see text]), and outperforms the state-of-the-art method by 26.2%, 31.7% and 27.9%, respectively.