EditSum: A Retrieve-and-Edit Framework for Source Code Summarization

Jia Allen Li, Yongmin Li, Ge Li, Xing Hu, Xin Xia, Zhi Jin · DOI

Abstract

Automatic code summarization is a critical task for software maintenance, yet existing neural models often struggle with the "keyword problem"—the inability to accurately generate low-frequency words that describe specific function logic. We present EditSum, a novel retrieve-and-edit framework that treats existing summaries as templates. By retrieving a similar code snippet's summary as a "prototype" and editing it based on the semantic differences of the new code, EditSum effectively combines the structural reliability of retrieval-based methods with the flexibility of generation-based models. Our evaluation on a 2-million-sample Java corpus demonstrates that EditSum significantly outperforms state-of-the-art baselines in both automated metrics and human evaluation.

Introduction

Writing documentation for source code is essential but time-consuming. While deep learning has advanced automated summarization, current models face a significant hurdle: they are excellent at learning common linguistic patterns (e.g., "returns true if...") but fail to capture rare, informative keywords (e.g., "prefix" or "iterator"). These keywords are often omitted because they appear infrequently in training data, resulting in summaries that are grammatically sound but semantically hollow.

Conversely, traditional Information Retrieval (IR) methods, which copy summaries from similar code, often produce "hallucinations" when the retrieved summary doesn't perfectly align with the new code's specific logic. EditSum addresses this by bridging the gap between these two paradigms.

The EditSum Framework

EditSum operates on a three-step "retrieve-and-edit" pipeline designed to separate the linguistic structure of a summary from its specific semantic content.

  1. Retrieval: For a given input code snippet, the system uses the Lucene IR engine to find the most similar snippet in a massive training corpus. The summary associated with this similar snippet is designated as the prototype summary.
  2. Extracting Semantic Differences: The model identifies the unique characteristics of the new code compared to the retrieved code. It calculates an Edit Vector by analyzing "insertion" words (new code features) and "deletion" words (old code features). This vector serves as a semantic "diff."
  3. Neural Editing: A sequence-to-sequence (Seq2Seq) model takes the prototype summary and the Edit Vector as inputs. Using an attention mechanism, it revises the prototype, preserving the structural "how to say" while injecting the specific "what to say" keywords derived from the new code.
The three-step EditSum workflow: Selecting a similar pair, extracting semantic information, and combining the pattern with new keywords.

Neural Architecture

The core of EditSum is a sophisticated encoder-decoder architecture. The Prototype Encoder processes the retrieved summary into a hidden representation, while the Attention Mechanism focuses on the Edit Vector to identify which parts of the prototype need modification. The Summary Decoder then generates the final distribution of words, prioritizing the inclusion of relevant keywords identified during the semantic extraction phase.

Detailed neural architecture of EditSum, showing the interaction between the Edit Vector, Prototype Encoder, and the final Summary Decoder.

Experimental Evaluation

Dataset

The researchers utilized a massive Java corpus containing approximately 2 million method-summary pairs, pre-processed to remove duplicates and auto-generated code. To ensure rigorous testing, the dataset was partitioned by project, preventing the model from simply memorizing summaries from the same codebase.

Key Results

EditSum significantly outperformed both traditional IR baselines and state-of-the-art neural models:

  • Quantitative Performance: EditSum improved upon the previous best model (Re2Com) by 9.93% in BLEU1 and 11.58% in ROUGE-L.
  • Keyword Accuracy: In tests involving low-frequency words (frequency < 100), EditSum correctly predicted 5,260 words, nearly doubling the 2,801 words predicted by standard models like ast-attendgru.
  • Efficiency: Despite its superior performance, EditSum is remarkably efficient. With only 26.4M parameters, it outperformed the 57.3M-parameter Rencos model, proving that architectural intelligence can outweigh raw scale.

Conclusion and Future Work

EditSum demonstrates that the "retrieve-and-edit" approach is a powerful solution to the keyword problem in code summarization. By leveraging prototypes, the model avoids the need to relearn basic grammar for every summary and can focus its capacity on accurately placing functional keywords.

While currently validated on Java, the framework is theoretically language-agnostic. Future work will explore its performance on other languages like Python and C++, as well as methods to adapt summaries to project-specific coding styles.

Summary updated

BibTeX
@inproceedings{li2021editsum,
  author = {Jia Allen Li and Yongmin Li and Ge Li and Xing Hu and Xin Xia and Zhi Jin},
  title = {{EditSum: A Retrieve-and-Edit Framework for Source Code Summarization}},
  booktitle = {ASE},
  year = {2021},
  doi = {10.1109/ase51524.2021.9678724},
}
Figure