arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2402.00519cs.SE
收藏

基于预训练Transformer的代码片段摘要生成研究

Towards Summarizing Code Snippets Using Pre-Trained Transformers

Antonio Mastropaolo, Matteo Ciniselli, Luca Pascarella, Rosalia Tufano, Emad Aghajani, Gabriele Bavota

AI总结:

本研究针对细粒度代码片段摘要生成数据稀缺问题,通过构建注释数据集、训练多任务注释分类关联模型,构建大规模带文档代码片段数据集并训练摘要模型,性能优于现有基线方法。

摘要:

在理解代码时,记录代码的自然语言注释可以提供帮助,但遗憾的是这类注释并非始终存在。为在这种场景下为开发者提供支持,已有多种技术被提出,用于为给定代码自动生成自然语言摘要。近年来的大多数方法利用深度学习(DL)自动为类或函数生成文档,但针对更细粒度文档(例如为代码片段甚至单条语句生成文档)的研究投入较少。这种设计选择受限于训练数据的可用性:例如在Java语言场景中,很容易构建由<方法,Javadoc>对组成的数据集,可将其输入深度学习模型以训练模型生成方法摘要的能力。而对于记录少量语句的内部注释而言,这种注释与代码的关联工作并非易事。\n在本研究中,我们完成了训练深度学习模型以生成代码片段文档所需的全部步骤。首先,我们手动构建了一个包含6600条注释的数据集,这些注释已完成两项处理:(i)按类型分类(例如代码摘要、待办事项TODO),(ii)与它们所记录的代码语句建立关联。其次,我们使用该数据集训练了一个多任务深度学习模型,该模型以一条注释为输入,能够实现两项功能:(i)分类判断该注释是否属于“代码摘要”,(ii)将其与所记录的代码语句建立关联。我们的模型识别代码摘要的准确率达84%,将其与所记录的代码行关联的召回率和精确率均高于80%。第三,我们在10000个项目上运行该模型,识别代码摘要并将其与所记录的代码建立关联。这为构建大规模的带文档代码片段数据集提供了可能,该数据集随后被用于训练一个新的深度学习模型,以实现代码片段的文档生成。与现有最优基线方法的对比表明,所提方法具有更优的性能。

英文摘要:

When comprehending code, a helping hand may come from the natural language comments documenting it that, unfortunately, are not always there. To support developers in such a scenario, several techniques have been presented to automatically generate natural language summaries for a given code. Most recent approaches exploit deep learning (DL) to automatically document classes or functions, while little effort has been devoted to more fine-grained documentation (e.g., documenting code snippets or even a single statement). Such a design choice is dictated by the availability of training data: For example, in the case of Java, it is easy to create datasets composed of pairs <Method, Javadoc> that can be fed to DL models to teach them how to summarize a method. Such a comment-to-code linking is instead non-trivial when it comes to inner comments documenting a few statements. In this work, we take all the steps needed to train a DL model to document code snippets. First, we manually built a dataset featuring 6.6k comments that have been (i) classified based on their type (e.g., code summary, TODO), and (ii) linked to the code statements they document. Second, we used such a dataset to train a multi-task DL model, taking as input a comment and being able to (i) classify whether it represents a "code summary" or not and (ii) link it to the code statements it documents. Our model identifies code summaries with 84% accuracy and is able to link them to the documented lines of code with recall and precision higher than 80%. Third, we run this model on 10k projects, identifying and linking code summaries to the documented code. This unlocked the possibility of building a large-scale dataset of documented code snippets that have then been used to train a new DL model able to document code snippets. A comparison with state-of-the-art baselines shows the superiority of the proposed approach.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑