Summarizing Source Code with Transferred API Knowledge

Xing Hu, Ge Li, Xin Xia, David Lo, Shuai Lu, Zhi Jin

Abstract

Automatic code summarization is essential for maintaining large-scale software systems, yet existing techniques often fail to capture the underlying logic of code. This paper presents TL-CodeSum, a transfer learning framework that improves summarization by leveraging the consistency of Application Programming Interface (API) usage. By first learning to summarize API sequences, the model transfers this "API knowledge" to a dual-encoder architecture that processes both raw code tokens and API sequences. Experimental results on Java datasets show that TL-CodeSum significantly outperforms previous state-of-the-art models, achieving a BLEU score of 41.98 and more than doubling METEOR scores compared to baselines.

Introduction

High-quality code comments are vital for software maintenance and searchability. However, in practice, they are often missing or outdated. Automatic code summarization aims to generate natural language descriptions for code snippets, such as Java methods, to assist developers.

Traditional approaches rely on Information Retrieval (IR) or basic Deep Learning. IR methods fail when no similar code exists or naming conventions are poor. Early neural models treated source code as plain text, ignoring its structural properties. Developers frequently use specific API sequences to implement features (e.g., File.new followed by BufferedReader.read). These sequences are more regular and predictable than variable names, representing a latent "API knowledge" that standard models fail to capture explicitly.

The TL-CodeSum Framework

The authors propose a two-stage transfer learning framework designed to bridge the gap between raw code and functional summaries.

The TL-CodeSum pipeline: from API knowledge extraction to online generation.

Stage 1: API Sequence Summarization

The model first learns the "semantics" of API usage independently. A Sequence-to-Sequence (Seq2Seq) model with an attention mechanism is trained to map API call sequences (e.g., List.add, List.clear) to natural language summaries. This stage allows the encoder to develop a robust understanding of what specific API combinations typically achieve.

Stage 2: Code Summarization Task

The core innovation is a dual-encoder architecture:

  • Code Encoder: Processes raw source code tokens using Gated Recurrent Units (GRUs).
  • API Encoder: This is the pre-trained encoder from Stage 1. It processes the sequence of API calls extracted from the code.
  • Decoder: A single GRU-based decoder integrates attention from both encoders to generate the final summary.

The framework supports both fixed and fine-tuned versions of the transferred API encoder, with fine-tuning allowing the model to adapt the pre-trained knowledge specifically to the code summarization task.

Experimental Setup

The researchers curated two large-scale datasets from GitHub Java projects:

  1. API Knowledge Dataset (2009–2014): 340,922 pairs of API sequences and Javadoc summaries for pre-training.
  2. Code Summarization Dataset (2015–2016): 69,708 triplets of source code, API sequences, and summaries for the main task.

Preprocessing involved filtering "noisy" methods (getters/setters) and focusing on the first sentence of Javadoc comments, which typically contains the functional summary.

Results and Key Insights

TL-CodeSum significantly outperformed the baseline CODE-NN across all metrics. The fine-tuned model achieved a BLEU score of 41.98 (vs. 25.3 for CODE-NN) and a METEOR score of 18.81 (vs. 6.92).

APIs as Universal Verbs

A key insight is that while variable names vary by developer, API usage is consistent across projects. By treating APIs as a primary information source, the model gains a stable "vocabulary" of actions.

Attention weights showing how the model focuses on API calls for action words and code tokens for context.

As shown in the attention heatmaps, the model learns to look at API calls for "action" words (like "Writes") and at code tokens for "context" words (like "constant" or "stream"). This complementarity allows the model to generate more abstract and intent-focused summaries rather than simply repeating variable names.

Conclusion

TL-CodeSum demonstrates that knowledge learned from a simpler, related task (API summarization) can significantly boost performance on complex code summarization. While the model occasionally struggles with unique identifiers or technical synonyms, it represents a major step forward. Future work may involve incorporating richer structural information, such as data flow or control flow graphs, to further refine the model's understanding of program logic.

Summary updated

BibTeX
@inproceedings{hu2018summarizing,
  author = {Xing Hu and Ge Li and Xin Xia and David Lo and Shuai Lu and Zhi Jin},
  title = {{Summarizing Source Code with Transferred API Knowledge}},
  booktitle = {International Joint Conference on Artificial Intelligence},
  year = {2018},
  doi = {10.24963/ijcai.2018/314},
}
Figure