NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

A Topic Guided Pointer-Generator Model for Generating Natural Language Code Summaries

Xin Wang, Xin Peng, Jun Sun, Yifan Zhao, Chi Chen, Jinkai FanarXiv 2021

Introduction

Generating accurate natural language summaries for source code is a critical task for software maintenance and documentation. While recent deep learning approaches have treated this as a Neural Machine Translation (NMT) task, they often struggle with two primary issues: a lack of broader context beyond the immediate method and the "Out-of-Vocabulary" (OOV) problem, where domain-specific terms or custom variable names (e.g., speex) are not present in standard training dictionaries. This paper introduces ToPNN, a novel neural network architecture that addresses these challenges by incorporating class-level topic modeling and a pointer-generator mechanism.

Proposed Methodology

The authors propose the CoSTop approach, which utilizes the ToPNN (Topic Guided Pointer-Generator Network) architecture. This model introduces two major innovations to the standard NMT pipeline:

  1. Topic Modeling for Context: Using Latent Dirichlet Allocation (LDA), the system extracts latent topics from the entire class containing the target method. This provides high-level semantic context that disambiguates generic method names (e.g., identifying that a writeTo method in a "JSON parsing" class refers to JSON representation).
  2. Dual Encoders & Pointer-Generator Decoder: The model employs two GRU-based encoders: one for code topics and one for code tokens. The decoder features a pointer-generator network—a "soft switch" that allows the model to either generate a word from a fixed vocabulary or copy a token directly from the input source code, effectively solving the OOV problem.
The ToPNN architecture featuring dual encoders for topics and tokens, and a pointer-generator decoder for handling OOV words.

Experimental Setup

The model was evaluated using a massive, newly constructed dataset of 4,203,565 commented Java methods from over 8,200 open-source GitHub projects. Unlike previous datasets, this corpus retains complete class-level information, which is essential for the topic modeling component. The data was split into training (90%), validation (5%), and testing (5%) sets.

Evaluation and Results

CoSTop was compared against a standard Seq2Seq model and Funcom, a state-of-the-art AST-based model. Quantitative results show that CoSTop significantly outperforms these baselines across multiple metrics, including BLEU, ROUGE-L, METEOR, and CIDEr.

Performance comparison of CoSTop against Seq2Seq and Funcom baselines across various metrics as the length of code topics varies.

Key Findings

  • Contextual Accuracy: By integrating class topics, the model generates more specific summaries (e.g., "writes the json representation") compared to the generic descriptions produced by baseline models.
  • The Power of Copying: Nearly 33% of words in human-written method comments appear in the source code. The pointer mechanism leverages this, boosting scores like ROUGE-L and CIDEr that reward exact matches.
  • Human Evaluation: In blind studies, developers rated CoSTop summaries higher in expressiveness and conciseness than baselines. Interestingly, while humans often explain the rationale behind code, CoSTop excels at summarizing the internal processing.

Conclusion and Future Work

ToPNN demonstrates that combining global semantic context with local token copying significantly improves automated code summarization. While currently focused on Java, future work includes extending this approach to other programming languages and integrating topic-guided mechanisms with graph-based neural architectures or Abstract Syntax Trees (ASTs) for even richer code representation.

Summary updated

BibTeX
@misc{wang2021topic,
  author = {Xin Wang and Xin Peng and Jun Sun and Yifan Zhao and Chi Chen and Jinkai Fan},
  title = {{A Topic Guided Pointer-Generator Model for Generating Natural Language Code Summaries}},
  year = {2021},
  eprint = {2107.01642},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2107.01642},
}
Figure