Training a small Transformer-based neural network to predict the appropriate spacing between tokens in Java code.
Jupyter Notebook
1
20 commits
updated Oct 17, 2024
This project trains a small Transformer-based neural network to predict token spacing between two tokens in Java code. The task is inspired by the paper Learning to Format Coq Code Using Language Models and applied to the Java subset of the CodeSearchNet dataset.
The project focuses on determining the appropriate spacing between tokens based on both the tokens themselves and their types (e.g., keywords, operators, identifiers).
The model is designed to predict the spacing between two consecutive tokens in Java code. Each token is categorized by its type, such as keywords, annotations, identifiers, operators, punctuation, literals, and whitespace, using regular expressions. This token information is used to predict how the tokens should be spaced.
The dataset used for this project is the Java subset from the CodeSearchNet dataset, which contains a diverse collection of Java code snippets sourced from various open-source repositories on GitHub. This dataset provides a rich resource for training and evaluating machine learning models on programming tasks.
Before using the dataset for training, the Java code snippets are preprocessed as follows:
Formatting: A custom function called create_datasets is implemented to format the Java code using Google Java Format. This function ensures consistent styling and structure across the code snippets, removing formatting inconsistencies and adhering to standard Java conventions.
Data Loading: The formatted Java code is loaded into the project using custom preprocessing functions. The raw data is stored in JSON Lines format (JSONL), where each line represents a single Java code snippet.
Tokenization: Each code snippet is tokenized into individual components (tokens) using regular expressions that define various token patterns, including:
public, class, return)@Override, @Test)+, -, *, =){, }, ;, ,)The tokenization process helps in identifying the type of each token, which is crucial for predicting spacing between tokens.
The dataset is divided into two main parts (80/20):
The performance of the trained Transformer model was evaluated on a total of 10,000 Java code snippets, which includes both the training and validation sets. The model's ability to predict spacing between tokens was measured using top-1 and top-3 accuracy metrics.
These results indicate that the model effectively predicts the correct spacing between consecutive tokens in Java code.
The confusion matrix below illustrates the model's performance in predicting the various spacing classes. It provides a visual representation of the correct and incorrect predictions made by the model.
The model categorizes spacing into four classes:
You can explore and run the evaluation yourself by downloading the notebook and uploading it to your Google Collab.
To replicate the results of this project and train your own model, follow the steps below:
Start by cloning this GitHub repository to your local machine:
git clone https://github.com/Olivera2708/Space-formatting
cd Space-formatting
Install the necessary Python packages using the requirements.txt file included in the project:
pip install -r requirements.txt
Download the Java subset of the CodeSearchNet dataset from the provided link.
You can use the data.jsonl file from the repository, which consists of approximately 70,000 formatted Java codes, or generate your own, larger one:
Unzip the downloaded dataset.
Create a folder named dataset in the root directory of your project.
Copy the relevant data files from the unzipped dataset into the dataset folder. The structure should look like this:
project-directory/
├── dataset/
│ ├── train/
│ │ ├── train0.jsonl
│ │ ├── train1.jsonl
│ │ └── ...
│ ├── valid.jsonl
│ └── test.jsonl
Uncomment the create_datasets function in main.py to generate the formatted dataset. This function will apply Google Java Format to ensure consistency in the code snippets.
You can either use the pre-trained model (model.pth) provided in this repository or train your own model from scratch. Based on your choice, uncomment and comment the last lines in main.py accordingly.
20 commits
Jupyter Notebook
86.0%
Python
14.0%
Training a small Transformer-based neural network to predict the appropriate spacing between tokens in Java code.
Jupyter Notebook
1
20 commits
updated Oct 17, 2024
This project trains a small Transformer-based neural network to predict token spacing between two tokens in Java code. The task is inspired by the paper Learning to Format Coq Code Using Language Models and applied to the Java subset of the CodeSearchNet dataset.
The project focuses on determining the appropriate spacing between tokens based on both the tokens themselves and their types (e.g., keywords, operators, identifiers).
The model is designed to predict the spacing between two consecutive tokens in Java code. Each token is categorized by its type, such as keywords, annotations, identifiers, operators, punctuation, literals, and whitespace, using regular expressions. This token information is used to predict how the tokens should be spaced.
The dataset used for this project is the Java subset from the CodeSearchNet dataset, which contains a diverse collection of Java code snippets sourced from various open-source repositories on GitHub. This dataset provides a rich resource for training and evaluating machine learning models on programming tasks.
Before using the dataset for training, the Java code snippets are preprocessed as follows:
Formatting: A custom function called create_datasets is implemented to format the Java code using Google Java Format. This function ensures consistent styling and structure across the code snippets, removing formatting inconsistencies and adhering to standard Java conventions.
Data Loading: The formatted Java code is loaded into the project using custom preprocessing functions. The raw data is stored in JSON Lines format (JSONL), where each line represents a single Java code snippet.
Tokenization: Each code snippet is tokenized into individual components (tokens) using regular expressions that define various token patterns, including:
public, class, return)@Override, @Test)+, -, *, =){, }, ;, ,)The tokenization process helps in identifying the type of each token, which is crucial for predicting spacing between tokens.
The dataset is divided into two main parts (80/20):
The performance of the trained Transformer model was evaluated on a total of 10,000 Java code snippets, which includes both the training and validation sets. The model's ability to predict spacing between tokens was measured using top-1 and top-3 accuracy metrics.
These results indicate that the model effectively predicts the correct spacing between consecutive tokens in Java code.
The confusion matrix below illustrates the model's performance in predicting the various spacing classes. It provides a visual representation of the correct and incorrect predictions made by the model.
The model categorizes spacing into four classes:
You can explore and run the evaluation yourself by downloading the notebook and uploading it to your Google Collab.
To replicate the results of this project and train your own model, follow the steps below:
Start by cloning this GitHub repository to your local machine:
git clone https://github.com/Olivera2708/Space-formatting
cd Space-formatting
Install the necessary Python packages using the requirements.txt file included in the project:
pip install -r requirements.txt
Download the Java subset of the CodeSearchNet dataset from the provided link.
You can use the data.jsonl file from the repository, which consists of approximately 70,000 formatted Java codes, or generate your own, larger one:
Unzip the downloaded dataset.
Create a folder named dataset in the root directory of your project.
Copy the relevant data files from the unzipped dataset into the dataset folder. The structure should look like this:
project-directory/
├── dataset/
│ ├── train/
│ │ ├── train0.jsonl
│ │ ├── train1.jsonl
│ │ └── ...
│ ├── valid.jsonl
│ └── test.jsonl
Uncomment the create_datasets function in main.py to generate the formatted dataset. This function will apply Google Java Format to ensure consistency in the code snippets.
You can either use the pre-trained model (model.pth) provided in this repository or train your own model from scratch. Based on your choice, uncomment and comment the last lines in main.py accordingly.
20 commits
Jupyter Notebook
86.0%
Python
14.0%