Abstract
Recently deep learning based Natural Language Processing (NLP) models have shown great potential in the modeling of source code. However, a major limitation of these approaches is that they take source code as simple tokens of text and ignore its contextual, syntactical and structural dependencies. In this work, we present CodeGRU, a gated recurrent unit based source code language model that is capable of capturing source code's contextual, syntactical and structural dependencies. We introduce a novel approach which can capture the source code context by leveraging the source code token types. Further, we adopt a novel approach which can learn variable size context by taking into account source code's syntax, and structural information. We evaluate CodeGRU with real-world data set and it shows that CodeGRU outperforms the state-of-the-art language models and help reduce the vocabulary size up to 24.93%. Unlike previous works, we tested CodeGRU with an independent test set which suggests that our methodology does not requisite the source code comes from the same domain as training data while providing suggestions. We further evaluate CodeGRU with two software engineering applications: source code suggestion, and source code completion. Our experiment confirms that the source code's contextual information can be vital and can help improve the software language models. The extensive evaluation of CodeGRU shows that it outperforms the state-of-the-art models. The results further suggest that the proposed approach can help reduce the vocabulary size and is of practical use for software developers.
Introduction
Traditional Natural Language Processing (NLP) models applied to source code often treat code as "flat" text. This approach has two major flaws. First, it ignores the strict syntactical and structural dependencies inherent in programming languages, such as try-catch blocks or specific statement endings. Second, it suffers from a "vocabulary explosion" due to diverse naming conventions (e.g., num vs. count), creating a massive out-of-vocabulary (OOV) problem that hinders model performance and increases training overhead.
CodeGRU addresses these challenges by treating source code as a structured entity. By abstracting identifiers into their data types and employing a variable-size context learning approach, CodeGRU captures the logic and type flow of code more effectively than standard token-based models.
Methodology
The CodeGRU framework is divided into an offline process for pre-processing and training, and an online phase for real-time prediction.
Token Abstraction
Instead of learning every unique variable name, the Code Analyzer abstracts identifiers based on their types. For instance, a variable i of type int is transformed into IntVar, and literals are simplified to their type equivalents (e.g., IntVal). This abstraction allows the model to focus on the underlying API usage and language grammar rather than specific naming choices, significantly reducing the vocabulary size.
Variable Size Context Learning
Unlike standard models that use a fixed window of $N$ tokens, CodeGRU leverages the code's syntax. It splits sequences based on logical boundaries like semicolons (;) or braces ({, }). The model then iteratively increases the context within these logical blocks, capturing dependencies that might be separated by many lines of code but are logically related.
GRU Architecture
The model utilizes Gated Recurrent Units (GRU) to solve the vanishing gradient problem common in standard RNNs. GRUs were selected over LSTMs because they expose their full hidden state, which the authors found more effective for capturing the specific contextual dependencies of source code.
Experimental Results
The study evaluated CodeGRU using 10 popular open-source Java projects (e.g., ant, maven, poi) and an independent test set (Cassandra).
- Vocabulary Reduction: CodeGRU reduced the unique vocabulary size by an average of 24.93%, with some projects seeing reductions exceeding 35%.
- Accuracy and MRR: CodeGRU achieved a top-1 accuracy (K@1) of 43-46%, outperforming standard RNNs (~37%) and N-gram models (~17-21%). It also achieved a Mean Reciprocal Rank (MRR) of 0.524, compared to 0.469 for the best baseline.
- Efficiency: The model provides suggestions in less than 30 milliseconds, making it suitable for real-time integration into IDEs.
Conclusion
CodeGRU demonstrates that incorporating contextual and structural information through type-based abstraction and variable-size context learning significantly improves source code modeling. While the current model excels at predicting required types, future work will focus on predicting specific variable names and extending the approach to dynamic languages like Python or JavaScript.
Summary updated