[Paper Review] Retrieve and Refine: Exemplar-based Neural Comment Generation
This paper proposes a novel exemplar-based neural comment generation model that retrieves semantically similar code-comment pairs from a codebase and refines them using a sequence-to-sequence model with attention and similarity gating. The method significantly outperforms state-of-the-art approaches, achieving a 43.78 BLEU and 28.12 METEOR score on a large Java dataset.
Code comment generation is a crucial task in the field of automatic software development. Most previous neural comment generation systems used an encoder-decoder neural network and encoded only information from source code as input. Software reuse is common in software development. However, this feature has not been introduced to existing systems. Inspired by the traditional IR-based approaches, we propose to use the existing comments of similar source code as exemplars to guide the comment generation process. Based on an open source search engine, we first retrieve a similar code and treat its comment as an exemplar. Then we applied a seq2seq neural network to conduct an exemplar-based comment generation. We evaluate our approach on a large-scale Java corpus, and experimental results demonstrate that our model significantly outperforms the state-of-the-art methods.
Motivation & Objective
- To address the limitation of existing neural comment generation models that produce short, degenerate, or low-quality comments by incorporating software reuse principles.
- To improve comment generation control and quality by leveraging comments from semantically similar code as exemplars.
- To reduce reliance on manual template design by using real-world code comments as dynamic templates.
- To develop a method that combines retrieval with neural refinement to generate more accurate and contextually relevant comments.
Proposed method
- Uses an open-source search engine (Lucene) to retrieve the most similar code snippet from a training corpus based on token-level similarity.
- Treats the comment of the retrieved code as an exemplar to guide the generation process.
- Employs a three-encoder seq2seq model with LSTMs to encode the input code, similar code, and exemplar comment into contextual feature vectors.
- Computes a similarity score $ s $ between the input code and the similar code using a nonlinear sigmoid function to modulate attention.
- Uses a gated initial hidden state for the decoder: $ extbf{h}^x(1-s) + extbf{h}^y s $, where $ extbf{h}^x $ and $ extbf{h}^y $ are the final hidden states of the input code and exemplar comment.
- Applies an attention mechanism that computes context vectors from both input code and exemplar comment tokens, weighted by the similarity score $ s $.
Experimental results
Research questions
- RQ1Can retrieving semantically similar code and using its comment as an exemplar improve neural comment generation quality?
- RQ2How does exemplar-based refinement compare to end-to-end seq2seq models in generating longer, more meaningful comments?
- RQ3To what extent does the similarity score between input and similar code influence the quality of generated comments?
- RQ4Can exemplar-based generation reduce degenerate outputs such as repeated tokens or very short comments?
Key findings
- The proposed model achieves a BLEU score of 43.78 and a METEOR score of 28.12 on the Java dataset, significantly outperforming all baselines.
- The model improves BLEU by approximately 10 percentage points over HAD, the previous state-of-the-art method.
- The model reduces degenerate outputs: unlike HAD, which produced over 70 predictions with repeated tokens, the proposed method generates more diverse and coherent comments.
- The model's performance is substantially better than vanilla seq2seq, confirming that exemplars are a key factor in improving generation quality.
- Human evaluation is recommended in future work, as current metrics (BLEU, METEOR) may not fully reflect real-world utility in software development.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.