一句话:提出用检索到的相似代码注释作范例,指导 seq2seq 神经网络生成代码注释,在 Java 语料上显著优于现有方法。
代码注释生成旨在自动为源代码生成自然语言描述。传统方法依赖人工模板或信息检索,近年神经网络方法虽效果显著,但仅以代码信息为输入,未利用软件复用中相似代码注释的参考价值。本文受 IR 与模板方法启发,提出神经注释生成方法:给定代码,先用 IR 检索相似代码片段,将其注释作为范例;再设计 seq2seq 神经网络,输入代码、AST、相似代码及范例注释,基于语义相似性利用范例信息辅助生成目标注释。在约 200 万样本的大规模 Java 语料上评估,结果显著超越当前最优方法。
Code comment generation which aims to automatically generate natural language descriptions for source code, is a crucial task in the field of automatic software development. Traditional comment generation methods use manually-crafted templates or information retrieval (IR) techniques to generate summaries for source code. In recent years, neural network-based methods which leveraged acclaimed encoder-decoder deep learning framework to learn comment generation patterns from a large-scale parallel code corpus, have achieved impressive results. However, these emerging methods only take code-related information as input. Software reuse is common in the process of software development, meaning that comments of similar code snippets are helpful for comment generation. Inspired by the IR-based and template-based approaches, in this paper, we propose a neural comment generation approach where we use the existing comments of similar code snippets as exemplars to guide comment generation. Specifically, given a piece of code, we first use an IR technique to retrieve a similar code snippet and treat its comment as an exemplar. Then we design a novel seq2seq neural network that takes the given code, its AST, its similar code, and its exemplar as input, and leverages the information from the exemplar to assist in the target comment generation based on the semantic similarity between the source code and the similar code. We evaluate our approach on a large-scale Java corpus, which contains about 2M samples, and experimental results demonstrate that our model outperforms the state-of-the-art methods by a substantial margin.