Source-linked AI summary

Mapping Language to Code in Programmatic Context

Srinivasan Iyer, Ioannis Konstas, Alvin Cheung, Luke Zettlemoyer

arXiv:1808.09588v1cs.CL

TL;DR

The paper addresses generating source code from natural-language documentation while accounting for the surrounding class environment. It releases the CONCODE dataset and introduces an encoder-decoder with specialized context modeling, achieving higher performance than strong baselines. The authors conclude that richer programmatic-context models could improve results further.

  • Problem

    Existing natural-language-to-code work has used limited language or code environments, motivating generation within real-world programming contexts.

  • Method

    The paper releases CONCODE and uses a specialized encoder-decoder that models natural-language documentation together with class variables, methods, and types.

  • Results

    The model outperforms strong neural baselines by 1.95% exact match accuracy and achieves gains of 0.82 BLEU points over the next-best models.

  • Takeaways & Limitations

    The analysis suggests that richer models of programmatic context could further improve context-based code generation.

  • Takeaways & Limitations

    The dataset excludes Java classes inheriting from parent classes because generated code might use inherited variables and methods from separate source files.

Abstract

from arXiv · show

Source code is rarely written in isolation. It depends significantly on the programmatic context, such as the class that the code would reside in. To study this phenomenon, we introduce the task of generating class member functions given English documentation and the programmatic context provided by the rest of the class. This task is challenging because the desired code can vary greatly depending on the functionality the class provides (e.g., a sort function may or may not be available when we are asked to "return the smallest element" in a particular member variable list). We introduce CONCODE, a new large dataset with over 100,000 examples consisting of Java classes from online code repositories, and develop a new encoder-decoder architecture that models the interaction between the method documentation and the class environment. We also present a detailed error analysis suggesting that there is significant room for future work on this task.

1 Introduction

The paper frames code generation as mapping natural-language documentation to source code within a real-world class environment. It introduces CONCODE and a specialized encoder-decoder that models interactions between documentation and contextual variables and methods.

  • Existing natural-language-to-code approaches assume fixed templates, fixed method context, or no code context.
  • Class context creates challenges in mapping documentation to variables, APIs, and methods while choosing the resulting code structure.The context can also reduce ambiguity about developer intent.
  • The proposed encoder-decoder encodes documentation with sub-word representations of environment identifiers and types, then uses two-step attention while decoding code.The attention first targets the documentation and then the environment, supporting copying of variables and methods.
  • CONCODE contains over 100,000 Java tuples pairing class environments, natural-language documentation, and code from public GitHub repositories.The dataset is designed for diverse domains and includes open-domain challenges involving unseen identifiers at test time.
  • 8.6% exact match accuracy and 22.11 BLEU were achieved, outperforming retrieval and recent neural methods.

2 Task Definition

The task generates a method’s source-code derivation from natural-language documentation and a class environment. That environment includes member variables with types and member methods with return types, while method parameters and bodies are left for future work.

  • The task conditions source-code generation on natural-language documentation and the class environment where the method resides.
  • The environment contains member-variable names with data types and member-function names with return types.
  • Method parameters and bodies are excluded from the environment and reserved for future work.

3 Models

The models encode natural-language documentation and class-environment components, then generate syntactically valid code through production rules, attention, copying, or sequence-based baselines.

  • 3.1 Encoder: The best model separately encodes natural language, environment components, and data types with Bi-LSTMs before decoding valid production-rule sequences.Environment identifiers are broken into subword units, and the decoder produces syntactically valid source code.
  • 3.1 Encoder: Variable and method representations combine type embeddings with camel-case name encodings, followed by contextualization through another Bi-LSTM.Method return types and names use the same Bi-LSTMs and embedding matrices as variables.
  • 3.2 Decoder: The decoder expands non-terminals depth-first into production rules, using an LSTM state conditioned on syntactic history and parent-state information.Its context vector combines the decoder state with attention-derived natural-language and environment representations.
  • 3.2 Decoder: Two-step attention first compresses the natural language into z_t, then attends over environment types and names to produce e_t.The resulting context vectors support generation of valid right-hand-side actions and connect documentation words with class components.
  • 3.2 Decoder: The decoder copies environment identifiers only for IdentifierNT rules, using attention weights over environment types and names as copy probabilities.This mechanism addresses named identifiers and user-defined types that may come from previously unseen domains.
  • 3.3 Baseline Models: Baseline models include retrieval, token-level Seq2Seq, and production-sequence Seq2prod approaches that concatenate documentation with variables and methods.The retrieval baseline substitutes similarly typed test-environment entities, while Seq2prod generates AST derivations with supervised copying.

4 CONCODE

CONCODE is a repository-derived Java dataset for generating method code from documentation and class context under near-zero-shot domain conditions. Its construction includes preprocessing choices that standardize examples while excluding inherited-class cases for future work.

  • Dataset construction: CONCODE contains environment information, Javadoc-style natural-language documentation, and target code gathered from public Java projects.The data was collected from approximately 33,000 repositories and split by repository rather than randomly.
  • Dataset construction: Repository-based splitting separates test domains from training domains, creating near-zero-shot conditions for associating unseen language tokens with unseen environment elements.Development and test examples whose documentation exactly appears in training are also removed.
  • Scope boundary: Classes inheriting from parent classes are excluded because generated code could depend on inherited members stored in separate source files.The authors leave inheritance handling for future work.
  • Preprocessing: Preprocessing strips selected Javadoc fields, canonicalizes local variables and arguments, replaces method names with “function,” and replaces string literals with constants.Methods that do not parse are eliminated before conversion to production-rule sequences.
  • Dataset statistics: The dataset contains 100,000 training examples plus 2,000 development and 2,000 test examples.On average, environments contain approximately 5 variables and 11 methods; 68% of target code uses member variables and 16% uses member methods.

5 Experimental Setup

The experiments evaluate syntax-constrained and token-based code generators on previously unseen repositories using exact match and BLEU. Inference encodes documentation and context, then uses beam search to generate source-code derivations or tokens.

  • Training: The experimental setup trains two-layer LSTMs with hidden dimensionality 1024, dropout p = 0.5, and Adam optimization for 30 epochs.The initial learning rate is 0.001 and is decayed by 80% based on development performance after each epoch.
  • Inference: Inference encodes each test example’s natural language and context, then incrementally generates source code from a stack of grammar symbols.Neural models use beam search with beam size 3 and output limits of 150 tokens or 500 production rules.
  • Metrics: The evaluation uses exact match accuracy and BLEU, with BLEU providing partial credit for matching predicted and reference code subparts.Table 2 covers 2,000 test and development examples from previously unseen repositories.
  • Qualitative analysis: Figure 5 qualitatively distinguishes exact matches, practically reasonable outputs, a solution better than the reference, insufficient context, and incorrect element selection.The figure analyzes development-set examples.

6 Results

The proposed context-based generator outperforms the evaluated baselines, while syntax-constrained decoding and context-aware components materially affect performance. Qualitative analysis also identifies cases where context is insufficient or encoder representations select the wrong element.

  • Overall results: The best model gains 1.95% exact match accuracy and 0.82 BLEU points over the next-best models.The authors attribute the improvement to independently encoded subword units and two-step attention linking language with context elements.
  • Decoder comparison: Syntax-tree-rule decoding achieves 6.65 exact match accuracy for Seq2prod versus 3.2 for Seq2seq.All neural models outperform the retrieval baseline with member substitution.
  • Ablation study: Removing the two-step attention mechanism causes a 1.3% drop in development-set accuracy.Removing variables causes a larger exact-match reduction, while removing methods causes a smaller reduction.

7 Error Analysis

The error analysis finds that most predictions differ substantially from the references, while some mismatches remain semantically reasonable. Other failures arise from insufficient knowledge of member methods and limited encoder representations.

  • Some non-exact outputs were semantically equivalent or practically reasonable, but exact-match evaluation penalized these predictions.Examples included alternative string concatenations, cleaner equivalent code, and List<?> replacing Transformer[].
  • The model failed when it lacked documentation of methods callable on class members, such as evictAll on TimestampsRegion.Augmenting the environment with additional member-type documentation is identified as future work.
  • Richer encoder representations are needed when the model cannot associate documentation words such as “registry” with the correct class elements.In the cited example, the model selected values instead of register.
  • 71% of development-set predictions differed significantly from their references, while 11% were exactly correct.Another 16% differed by only 1–2 tokens, and 2% were semantically equivalent but not exactly equal.

8 Related Work

Prior work maps natural language to logical forms, domain-specific programs, and general-purpose code, but existing datasets and methods provide narrower settings than CONCODE. CONCODE instead combines code with class environments across many domains.

  • Existing research has mapped natural language to logical forms, λ-DCS, regular expressions, database queries, and general-purpose programs.
  • Earlier source-code generation work used sequence-to-sequence models with attention and copying, while newer methods constrain decoding through grammars or AST-like modular structures.
  • CONCODE contains over 100,000 natural-language and code pairs with corresponding class environments, spanning thousands of domains.Prior datasets described here contain approximately 20,000 programs and are generally tied to a single domain or categorical setting.

9 Conclusion

The paper introduces CONCODE and a context-aware encoder-decoder for generating source code from natural-language documentation within real-world code bases. The specialized model outperforms strong neural baselines, while the analysis points to richer programmatic context as future work.

  • The paper introduces CONCODE, a large dataset of natural-language, code, and class-context tuples collected from online repositories across varied domains.
  • The proposed encoder-decoder uses a specialized context encoder to model source-code generation within real-world code bases.
  • 1.95% exact match accuracy separates the proposed model from strong neural baselines.
  • The error analysis suggests that richer models of programmatic context could further improve the results.
Loading 1808.09588v1…