Skip to main content
QUICK REVIEW

[论文解读] CoCoSum: Contextual Code Summarization with Multi-Relational Graph Neural Network

Yanlin Wang, Ensheng Shi|arXiv (Cornell University)|Jul 5, 2021
Software Engineering Research参考文献 64被引用 9
一句话总结

CoCoSUM 提出了一种上下文代码摘要模型,通过使用多关系图神经网络(MRGNN)整合类内和类间全局上下文,从而提升代码摘要质量。该模型利用类名进行语义嵌入,利用 UML 类图进行关系嵌入,在序列到序列框架中通过两级注意力机制超越了最先进方法。

ABSTRACT

Source code summaries are short natural language descriptions of code snippets that help developers better understand and maintain source code. There has been a surge of work on automatic code summarization to reduce the burden of writing summaries manually. However, most contemporary approaches mainly leverage the information within the boundary of the method being summarized (i.e., local context), and ignore the broader context that could assist with code summarization. This paper explores two global contexts, namely intra-class and inter-class contexts, and proposes the model CoCoSUM: Contextual Code Summarization with Multi-Relational Graph Neural Networks. CoCoSUM first incorporates class names as the intra-class context to generate the class semantic embeddings. Then, relevant Unified Modeling Language (UML) class diagrams are extracted as inter-class context and are encoded into the class relational embeddings using a novel Multi-Relational Graph Neural Network (MRGNN). Class semantic embeddings and class relational embeddings, together with the outputs from code token encoder and AST encoder, are passed to a decoder armed with a two-level attention mechanism to generate high-quality, context-aware code summaries. We conduct extensive experiments to evaluate our approach and compare it with other automatic code summarization models. The experimental results show that CoCoSUM is effective and outperforms state-of-the-art methods. Our source code and experimental data are available in the supplementary materials and will be made publicly available.

研究动机与目标

  • 解决现有代码摘要模型仅依赖局部代码上下文、忽略更广泛结构和语义关系的局限性。
  • 探究类内上下文(类名语义)和类间上下文(来自 UML 图的类依赖关系)对代码摘要质量的影响。
  • 设计一种能够有效编码并融合全局上下文到神经序列到序列框架中的模型,以提升摘要生成质量。
  • 证明整合结构化和语义化全局上下文可生成更准确且上下文相关的代码摘要。

提出的方法

  • 模型使用基于 Transformer 的编码器,从类名生成类语义嵌入,捕捉类内上下文。
  • 提取 UML 类图作为类间上下文,并通过一种新型多关系图神经网络(MRGNN)编码,学习类关系嵌入。
  • 通过代码标记编码器和抽象语法树(AST)编码器对代码片段进行编码,以捕捉语法和结构特征。
  • 两级注意力机制在解码过程中整合代码表示、类语义嵌入和类关系嵌入。
  • 解码器通过关注局部代码特征和全局上下文表示,生成自然语言摘要。
  • 模型采用端到端的序列到序列架构进行训练,使用交叉熵损失进行摘要生成。

实验结果

研究问题

  • RQ1整合类内上下文(类名语义)是否能提升代码摘要质量?
  • RQ2从 UML 类图中提取的类间上下文是否能增强模型生成准确且语义丰富摘要的能力?
  • RQ3将多关系图学习(MRGNN)与代码表示结合,对摘要性能有何影响?
  • RQ4所提出的两级注意力机制是否能有效利用局部和全局上下文,实现更优的摘要生成?

主要发现

  • CoCoSUM 在基准数据集上显著优于最先进代码摘要模型,证明了引入全局上下文的有效性。
  • 引入类名嵌入(类内上下文)可生成更具语义准确性的摘要,尤其在方法语义与类名紧密关联时表现更优。
  • 利用 UML 类图作为类间上下文,显著提升了模型捕捉方法行为与其他类关系的能力,尤其在复杂的面向对象设计中表现突出。
  • 基于 MRGNN 的关系编码能有效捕捉复杂的类依赖关系,并提升模型在多样化代码库中的泛化能力。
  • 两级注意力机制成功平衡了局部代码特征与全局上下文,生成了更连贯且上下文感知的摘要。

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。