Code search aims to retrieve accurate code snippets based on a natural language query to improve software productivity and quality. With the massive amount of available programs such as (on GitHub or Stack Overflow), identifying and localizing the precise code is critical for the software developers. In addition, Deep learning has recently been widely applied to different code-related scenarios, e.g., vulnerability detection, source code summarization. However, automated deep code search is still challenging since it requires a high-level semantic mapping between code and natural language queries. Most existing deep learning-based approaches for code search rely on the sequential text i.e., feeding the program and the query as a flat sequence of tokens to learn the program semantics while the structural information is not fully considered. Furthermore, the widely adopted Graph Neural Networks (GNNs) have proved their effectiveness in learning program semantics, however, they also suffer the problem of capturing the global dependencies in the constructed graph, which limits the model learning capacity. To address these challenges, in this paper, we design a novel neural network framework, named GraphSearchNet, to enable an effective and accurate source code search by jointly learning the rich semantics of both source code and natural language queries. Specifically, we propose to construct graphs for the source code and queries with bidirectional GGNN (BiGGNN) to capture the local structural information of the source code and queries. Furthermore, we enhance BiGGNN by utilizing the multi-head attention module to supplement the global dependencies that BiGGNN missed to improve the model learning capacity. The extensive experiments on Java and Python programming language from the public benchmark CodeSearchNet confirm that GraphSearchNet outperforms current state-of-the-art works.
翻译:代码搜索旨在基于自然语言查询检索精确代码片段,以提升软件生产效率与质量。随着GitHub或Stack Overflow等平台上可用程序数量的激增,精准识别和定位代码对软件开发人员至关重要。近年来,深度学习被广泛应用于漏洞检测、源代码摘要等代码相关场景。然而,自动化深度代码搜索仍面临挑战,因其需要建立代码与自然语言查询间的高层次语义映射。现有基于深度学习的代码搜索方法多依赖顺序文本——即将程序与查询作为扁平化的词元序列进行学习,未能充分利用结构信息。此外,广泛采用的图神经网络虽在程序语义学习方面展现了有效性,但存在难以捕获所构建图中全局依赖关系的问题,限制了模型学习能力。为应对上述挑战,本文设计了一种新颖的神经网络框架GraphSearchNet,通过联合学习源代码与自然语言查询的丰富语义,实现高效准确的源代码搜索。具体而言,我们提出为源代码和查询构建图,并采用双向门控图神经网络捕获其局部结构信息。进一步,我们利用多头注意力模块增强BiGGNN,补充其所缺失的全局依赖关系,从而提升模型学习能力。在公开基准CodeSearchNet上针对Java和Python编程语言的大量实验表明,GraphSearchNet优于当前最先进方法。