杨腾 赵逢禹 刘亚



摘 要: 代码复用可以有效缩短软件开发的时间,而代码搜索是代码复用的主要途径。提出了一种基于程序切片和BiGRU的代码搜索方法,该方法通过构建源代码的程序依赖图,以程序依赖图中出度最大的节点作为兴趣点构建前向切片。将程序切片与源代码的其他相关特征一起构成代码特征。把代码特征和代码的功能描述通过嵌入模块输入到BiGRU网络中,结合注意力机制训练BiGRU模型。用户输入功能查询语句,模型返回向量值最接近的代码。为了验证该模型的可行性和有效性,从开源代码库下载了Java项目,构建了数据集并进行实验。实验结果表明,提出的基于程序切片和BiGRU的方法在代码搜索的准确率和相关性排名等方面都有所改进。
关键词: 代码搜索; 程序切片; BiGRU; 注意力机制
Code search based on program slicing and BiGRU
YANG Teng, ZHAO Fengyu, LIU Ya
(School of Optical-Electrical & Computer Engineering, University of Shanghai for Science & Technology,
Shanghai 200093, China)
【Abstract】Code reuse can effectively shorten the time of software development, and code search is the main way of code reuse. This paper proposes a code search method based on program slicing and BiGRU. The method builds a program dependency graph of source code, and takes the node with the highest degree in the program dependency graph as the point of interest to construct program slices. The features of source code are formed by combining program slices with other relevant method features. Code features and code function descriptions are input into the BiGRU network through an embedding module, and BiGRU network model is generated by combining attention mechanism. Users enter the function query statement, and the model returns the code with the closest vector value. In order to verify the feasibility and effectiveness of the model, several Java projects are downloaded from the open source library to build the data set and conduct experiments. Experimental results show that the proposed method based on program slicing and BiGRU has some improvements in code search accuracy and relevance ranking.
【Key words】code search; program slice; BiGRU; attention
0 引 言
重用现有的代码片段可以有效缩短软件开发的时间,开发人员可以通过代码搜索重用已有的方法,提高开发效率。研究表明,代码搜索是软件开发人员使用最频繁的活动之一[1]。近年来,随着开源理念的加强,开源项目的质量和数量在不断地增加,开源代码库为人们提供了丰富的资源,但如何快速准确地搜索到需要的代码成为了一个新的问题。
早期的代码搜索方法通常采用全文检索技术,通过全文检索获得与查询关键词匹配的代码段。基于信息检索的方法通常将源代码视为纯文本,并利用信息检索模型来检索与给定查询关键字匹配的相关代码段。……