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

LAMNER: Code Comment Generation Using Character Language Model and Named Entity Recognition

Rishab Sharma, Fuxiang Chen, Fatemeh FardarXiv 2022

TL;DR

The authors introduce LAMNER (LAnguage Model and Named Entity Recognition), a novel framework for automatic code comment generation that captures both sub-token semantics and token-level syntax. By combining a character-level language model with a specialized Named Entity Recognition (NER) model, LAMNER achieves state-of-the-art performance on Java code summarization and produces highly adaptable embeddings that can boost the performance of other existing models.

What Problem Does This Paper Address?

Automatic code comment generation (or code summarization) translates complex source code into high-level, natural language descriptions to help developers understand codebases quickly. However, standard Neural Machine Translation (NMT) models struggle with the unique characteristics of programming languages:

  1. Sub-token Semantics: Code often relies on compound naming conventions like camelCase or snake_case. Standard tokenizers treat these as atomic units or split them rigidly, often losing the contextual meaning.
  2. Token-Level Syntax: While many models use Abstract Syntax Trees (ASTs) to capture the overarching hierarchical structure of code, they fail to encode the immediate syntactic role of individual tokens (e.g., distinguishing whether a token is a modifier, a return type, or a function name).

LAMNER addresses these gaps by creating a unified embedding space that simultaneously captures fine-grained character-level semantics and token-level syntactic roles.

Methods

The LAMNER framework splits the task into a pre-training phase for representation learning and a downstream encoder-decoder phase for comment generation.

Figure 1: Overview of LAMNER Framework. The Language and NER models on the left box are pre-trained to provide an initial embeddings for the code tokens. The embeddings are then concatenated to serve as the input to the Semantic-Syntax encoder on the right box to generate comments.
  1. Semantic Representation (Character-Level LM): The authors employ a bidirectional LSTM trained at the character level. This allows the model to naturally handle formatting nuances and extract semantic meaning from compound identifiers without relying on rigid splitting rules.
Figure 2: Overview of the bidirectional character-level lan- guage model architecture. The input to the model is every character within each line of code, and the model is trained to predict the next character.
  1. Syntactic Representation (NER Model): To capture token-level syntax, the authors train a Bi-LSTM-CRF sequence tagger. It takes the character-level language model embeddings as input and maps each code token to a specific syntactic entity tag (e.g., modifier, function, data-type).
Figure 3: Overview of the NER model to generate the syn- tactic code embedding for LAMNER. The input to the model is the semantic embeddings of every code token, and they are further fine-tuned to produce the syntactic code embed- dings.
  1. Semantic-Syntax Fusion & Generation: The embeddings from the LM and NER models are concatenated to form a single Semantic-Syntax embedding. This combined representation is fed into a bidirectional GRU encoder, and an attentive GRU decoder (utilizing Bahdanau attention) generates the final natural language comment.

Datasets

The model was trained and evaluated on a popular, widely used Java dataset collected by Hu et al., containing 69,708 training pairs, 8,714 validation pairs, and 8,714 test pairs. Each pair consists of a Java method and its corresponding first sentence from the Javadoc.

To train the NER model, the authors used the Javalang parser to generate ground-truth syntactic labels, applying custom rule-based refinements to break down identifiers and separators into finer, more descriptive sub-types.

Key Insights and Ideas

  • Syntax is a Powerful Predictor: In an ablation study, the authors discovered that the syntactic component alone (LAMNER_NER) contributed more to the overall performance than the semantic component alone (LAMNER_LAM). Combining both yielded the best results.
  • Character-Level Context Matters: Moving away from word-level tokenization allows the model to better preserve the context of unseen or rare user-defined identifiers.
  • High Adaptability (Embedding Fusion): The pre-trained Semantic-Syntax embeddings are not locked into the LAMNER architecture. When fused into other baseline architectures (like CodeBERT or RENCOS) as an initial "warm-up" state, they consistently boosted those models' performance as well.

Key Results

LAMNER significantly outperformed all baseline models (including CodeBERT, vanilla Seq2Seq, and retrieval-based models like RENCOS) across all automatic evaluation metrics:

  • BLEU-4: Improved by 23.00% over the best baseline.
  • CIDEr: Improved by 25.86% over the best baseline.
  • ROUGE-L & METEOR: Showed relative improvements of 10.52% and 1.44%, respectively.

Fused Model Improvements: Integrating LAMNER's pre-trained embeddings into the RENCOS baseline architecture (RENCOS_LAMNER-Embeds) improved its scores by 6% to 11% across all metrics.

Qualitative & Human Evaluation: A human evaluation conducted via Amazon Mechanical Turk rated LAMNER higher than the top baseline (RENCOS) in three key categories: Informativeness, Relevance, and Fluency. Attention map analysis also revealed that LAMNER distributes its attention broadly across prominent tokens (like max or panel), whereas baseline embeddings often over-focus strictly on the start and end tokens of a code snippet.

Limitations and Future Work

  • Language Specificity: The framework was exclusively evaluated on the Java programming language. While the authors hypothesize that these embeddings will benefit other languages, further validation is required.
  • Task Scope: The current study focuses entirely on code comment generation. The authors note that these semantic-syntactic embeddings could be highly beneficial for other software engineering tasks, such as automated bug detection and code clone detection, which they plan to explore in future work.

Summary updated

BibTeX
@misc{sharma2022lamner,
  author = {Rishab Sharma and Fuxiang Chen and Fatemeh Fard},
  title = {{LAMNER: Code Comment Generation Using Character Language Model and Named Entity Recognition}},
  year = {2022},
  eprint = {2204.09654},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2204.09654},
}
Figure