一句话:提出 PyART,用轻量乐观数据流和随机森林实现 Python 实时 API 推荐,优于现有工具。
针对动态语言 Python 中实时 API 推荐困难、现有方法依赖类型推断且在类型缺失或项目特有 API 上表现不佳的问题,本文提出 PyART。该方法通过轻量分析推导“乐观数据流”,虽不完备但模拟人类可获得的局部数据流信息;在推荐点提取数据流、词元相似度和词元共现三类特征,并用随机森林训练预测模型。在 8 个流行 Python 项目上评估,若有历史提交,top-1 准确率超过 50%,top-10 超过 70%,较 APIREC 和 Intellicode 分别提升 28.48%-39.05% 和 24.41%-30.49%;在无历史注释与跨项目场景也整体更优,平均推荐耗时不足 1 秒,满足实时性。
API recommendation in real-time is challenging for dynamic languages like Python. Many existing API recommendation techniques are highly effective, but they mainly support static languages. A few Python IDEs provide API recommendation functionalities based on type inference and training on a large corpus of Python libraries and third-party libraries. As such, they may fail to recommend or make poor recommendations when type information is missing or target APIs are project-specific. In this paper, we propose a novel approach, PyART, to recommend APIs for Python programs in real-time. It features a light-weight analysis to derives so-called optimistic data-flow, which is neither sound nor complete, but simulates the local data-flow information humans can derive. It extracts three kinds of features: data-flow, token similarity, and token co-occurrence, in the context of the program point where a recommendation is solicited. A predictive model is trained on these features using the Random Forest algorithm. Evaluation on 8 popular Python projects demonstrates that PyART can provide effective API recommendations. When historic commits can be leveraged, which is the target scenario of a state-of-the-art tool ARIREC, our average top-1 accuracy is over 50% and average top-10 accuracy over 70%, outperforming APIREC and Intellicode (i.e., the recommendation component in Visual Studio) by 28.48%-39.05% for top-1 accuracy and 24.41%-30.49% for top-10 accuracy. In other applications such as when historic comments are not available and cross-project recommendation, PyART also shows better overall performance. The time to make a recommendation is less than a second on average, satisfying the real-time requirement.