TL;DR
DeepAPI is a deep-learning-based tool that translates natural language queries (e.g., "how to parse XML") into relevant API usage sequences. By treating API learning as a machine translation problem, it significantly outperforms traditional keyword-based search tools by capturing the deep semantics and specific ordering of programming tasks.
What Problem Does This Paper Address?
Developers frequently struggle to navigate massive software libraries like the JDK or .NET framework. Finding the right API is hard, but finding the correct sequence of API calls to implement a specific functionality is even harder.
Existing tools typically rely on "bag-of-words" models or keyword matching. These methods have a major flaw: they ignore the order of words and the context. For example, a traditional search engine might struggle to distinguish between the queries "convert int to string" and "convert string to int" because they contain the same keywords. Furthermore, documentation is often incomplete, and general-purpose search engines like Google aren't optimized for the structural requirements of code.
Methods
The authors propose DeepAPI, which formulates API generation as a Neural Machine Translation (NMT) task. Instead of translating English to French, it translates a natural language query into a sequence of API calls.
Key technical components include:
- RNN Encoder-Decoder: The model uses a Recurrent Neural Network (specifically using Gated Recurrent Units or GRUs). The Encoder processes the user's query into a fixed-length "context vector," and the Decoder generates the API sequence based on that vector.
- Attention Mechanism: Not all words in a query are equally important. The model uses an attention-based approach to "focus" on specific parts of the input query when generating each step of the API sequence.
- API Importance (IDF Weighting): A novel contribution where the authors augment the standard loss function with Inverse Document Frequency (IDF). This penalizes ubiquitous, generic APIs (like
Logger.log) and encourages the model to prioritize the "meat" of the functional sequence. - Beam Search: During the translation phase, the model uses beam search to explore multiple possible API sequences and rank them by probability, providing the user with a list of the most likely candidates.
Datasets
The researchers built a massive corpus to train and evaluate the model:
- Source: Over 442,000 Java projects collected from GitHub (filtered for quality by selecting projects with at least one star).
- Scale: A final database of 7,519,907 pairs of API sequences and their corresponding natural language annotations.
- Extraction: API sequences were extracted by parsing source code into Abstract Syntax Trees (ASTs) using the Eclipse JDT compiler. Annotations were pulled from the first sentence of Javadoc comments.
Key Insights and Ideas
- The "Naturalness" of Code: The paper builds on the idea that software has "natural" patterns similar to human language, making it a prime candidate for NLP techniques.
- Sequence over Keywords: By using an RNN, the model learns that the order of calls (e.g.,
File.new$\rightarrow$FileOutputStream.new$\rightarrow$FileOutputStream.write) is essential for correctness. - Automatic Query Expansion: Because the model uses word embeddings, it learns that "save" and "write" are semantically related. This allows it to handle synonyms gracefully without manual rule-mapping.
- Penalizing Ubiquity: The inclusion of IDF-based weighting is a clever way to handle "noise" in code. It prevents the model from getting "distracted" by common utility calls that appear in almost every snippet but don't define the core logic.
Key Results
DeepAPI was compared against state-of-the-art tools like SWIM and a baseline of Lucene + UP-Miner:
- BLEU Score: DeepAPI achieved a BLEU score of 54.42, a massive improvement over SWIM (19.90) and Lucene+UP-Miner (11.97).
- Human Evaluation: In a test of 30 common programming queries, DeepAPI achieved an average FRank of 1.6 (meaning the first relevant result appeared at roughly the second position in the list).
- Relevancy: 80% of the top 5 results and 78% of the top 10 results were deemed relevant by human evaluators.
- Semantic Understanding: The model successfully distinguished between inverse queries (like the "string to int" vs "int to string" example) where previous models failed.
Limitations and Future Work
- JDK Focus: The current study is limited to the Java JDK. While the methodology is language-agnostic, the model would need retraining for other libraries or languages (e.g., Python, C#).
- Intra-procedural Limits: The API extraction currently only looks at sequences within a single method. It misses sequences that span across multiple method calls (inter-procedural).
- Annotation Noise: The model relies on Javadoc comments, which can sometimes be poorly written or uninformative. The authors suggest investigating better NLP techniques to extract cleaner annotations in the future.
- Code Synthesis: While DeepAPI provides the sequence of calls, it doesn't yet generate the full "boilerplate" code (variable declarations, try-catch blocks) needed to make the code runnable. This is a suggested path for future integration.
Summary updated