一句话:提出基于草图的代码生成方法 SkCoder,通过检索相似代码、抽取草图并后编辑,显著提升生成正确率。
深度学习在自动代码生成上已取得进展,受代码复用启发的复制式方法可复制相似代码片段的内容,但它们忽略了人类开发者实际会将相似代码中相关的部分视为代码草图并进一步编辑这一事实,往往直接重复相似代码而不做必要修改,导致生成错误结果。本文提出基于草图的代码生成方法 SkCoder 来模拟开发者的代码复用行为:给定自然语言需求,先检索相似代码片段,抽取其中相关部分作为代码草图,再将草图编辑为最终代码。其动机是草图提供了“如何写”的良构模式,后编辑则把需求特有的细节补充进草图并输出完整代码。作者在两个公开数据集和自建数据集上,用 5 个常用指标与 20 个基线比较,结果表明:SkCoder 能生成更正确的程序,在三个数据集上分别超过当时最优的 CodeT5-base 30.30%、35.39% 和 29.62%;该方法对多种代码生成模型有效,最高提升 Pass@1 达 120.1%;此外还考察了三种可能的草图并讨论了草图的重要性,并通过人工评估从三个方面证明了 SkCoder 的优越性。
Recently, deep learning techniques have shown great success in automatic code generation. Inspired by the code reuse, some researchers propose copy-based approaches that can copy the content from similar code snippets to obtain better performance. Practically, human developers recognize the content in the similar code that is relevant to their needs, which can be viewed as a code sketch. The sketch is further edited to the desired code. However, existing copy-based approaches ignore the code sketches and tend to repeat the similar code without necessary modifications, which leads to generating wrong results. In this paper, we propose a sketch-based code generation approach named Skcoderto mimic developers' code reuse behavior. Given a natural language requirement, Skcoderretrieves a similar code snippet, extracts relevant parts as a code sketch, and edits the sketch into the desired code. Our motivations are that the extracted sketch provides a well-formed pattern for telling models “how to write”. The post-editing further adds requirement-specific details into the sketch and outputs the complete code. We conduct experiments on two public datasets and a new dataset collected by this work. We compare our approach to 20 baselines using 5 widely used metrics. Experimental results show that (1) Skcodercan generate more correct programs, and outperforms the state-of-the-art -CodeT5-base by 30.30%, 35.39%, and 29.62% on three datasets. (2) Our approach is effective to multiple code generation models and improves them by up to 120.1% in Pass@l. (3) We investigate three plausible code sketches and discuss the importance of sketches. (4) We manually evaluate the generated code and prove the superiority of our Skcoderin three aspects.
DOI 原文 · arXiv · PDF(开放获取) · DBLP
@inproceedings{LiLLJHH23,
title = {SkCoder: A Sketch-based Approach for Automatic Code Generation},
author = {Jia Li and Yongmin Li and Ge Li and Zhi Jin and Yiyang Hao and Xing Hu},
booktitle = {ICSE},
year = {2023},
doi = {10.1109/icse48619.2023.00179}
}