---
# System prepended metadata

title: Code Completion CS5100

---



Code autocompletion is a common feature in many integrated development environments (IDEs) and code editors. These tools use various techniques to provide suggestions for completing code as developers type, which can include:

1. **Static Analysis**: This involves analyzing the codebase to understand the structure, types, and relationships between different elements. Based on this analysis, the IDE can offer suggestions for completing code based on what's commonly used or what makes sense syntactically.

2. **Machine Learning Models**: Some modern IDEs incorporate machine learning models, such as language models like GPT, to predict and suggest code completions. These models are trained on large datasets of code to learn patterns and context, allowing them to provide more accurate suggestions.

3. **User Patterns**: IDEs can learn from a developer's usage patterns and preferences. For example, if a developer frequently uses certain libraries or follows specific coding conventions, the autocompletion feature can prioritize suggestions accordingly.

4. **API Documentation**: Autocompletion can also be based on API documentation. IDEs can parse documentation for libraries or frameworks being used and suggest relevant methods, functions, or classes as developers type.

5. **Intelligent Contextual Suggestions**: Autocompletion systems can take into account the current context of the code being written. For instance, if a developer is within a loop, suggestions might prioritize loop-related constructs or methods.

6. **Feedback Mechanisms**: Some autocompletion systems incorporate feedback mechanisms where developers can provide explicit feedback on the relevance and accuracy of suggestions. This helps improve the system's performance over time.



## **Papers Related to Code Completion**
1. Automated Source Code Generation and Auto-Completion Using Deep Learning: Comparing and Discussing Current Language Model-Related Approaches

2.

3. "Attention Is All You Need" or "CodeBERT: A Pre-Trained Model for Programming and Natural Languages" 

## General Steps to implement code autocompletion:

1. **Data Collection**: Gather a dataset of code samples. This dataset should ideally cover a wide range of programming languages, libraries, and coding styles.

2. **Preprocessing**: Clean and preprocess the code data. This may involve tokenization, removing comments, normalizing identifiers, and other steps to prepare the data for training.

3. **Feature Extraction**: Extract features from the code data that will be used as input to your autocompletion model. This could include token sequences, abstract syntax trees (ASTs), or other representations of code.

4. **Model Selection**: Choose a suitable machine learning model for code autocompletion. This could be a neural network-based model (e.g., recurrent neural networks, transformers) or a more traditional statistical language model.

5. **Training**: Train your chosen model using the preprocessed code data. This step involves optimizing the model's parameters to minimize a loss function, typically measured against a validation set.

6. **Evaluation**: Evaluate the trained model's performance on a separate test set to assess its accuracy and effectiveness at code autocompletion.

7. **Integration**: Integrate the trained model into your IDE or code editor to provide real-time autocompletion suggestions as developers type.

8. **Feedback and Iteration**: Gather feedback from users and monitor the performance of your autocompletion system. Use this feedback to improve the model and update it periodically with new data.

Code autocompletion using Python and the `Keras` library for neural networks. 
## Basic character-level recurrent neural network (RNN) for autocompletion. 

```python
import numpy as np
from keras.models import Sequential
from keras.layers import LSTM, Dense

# Sample code data
code_samples = [
    "def add_numbers(a, b):",
    "    return a + b",
    "def multiply_numbers(a, b):",
    "    return a * b",
    "def greet(name):",
    "    return 'Hello, ' + name"
]

# Preprocessing: Convert characters to numerical representation
chars = sorted(list(set(''.join(code_samples))))
char_indices = dict((c, i) for i, c in enumerate(chars))
indices_char = dict((i, c) for i, c in enumerate(chars))

# Generate training data
maxlen = 40
step = 3
sentences = []
next_chars = []
for sample in code_samples:
    for i in range(0, len(sample) - maxlen, step):
        sentences.append(sample[i: i + maxlen])
        next_chars.append(sample[i + maxlen])
        
# Vectorization
x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool_)
y = np.zeros((len(sentences), len(chars)), dtype=np.bool_)
for i, sentence in enumerate(sentences):
    for t, char in enumerate(sentence):
        x[i, t, char_indices[char]] = 1
    y[i, char_indices[next_chars[i]]] = 1

# Define the model: simple LSTM
model = Sequential()
model.add(LSTM(128, input_shape=(maxlen, len(chars))))
model.add(Dense(len(chars), activation='softmax'))

# Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam')

# Train the model
model.fit(x, y, batch_size=128, epochs=30)

# Function to generate code completion suggestions
def complete_code(prefix):
    completed_code = prefix
    for _ in range(100):  # Generate 100 characters
        x_pred = np.zeros((1, maxlen, len(chars)))
        for t, char in enumerate(prefix):
            x_pred[0, t, char_indices[char]] = 1.
        preds = model.predict(x_pred, verbose=0)[0]
        next_index = np.random.choice(len(chars), p=preds)
        next_char = indices_char[next_index]
        if next_char == '\n':
            break
        completed_code += next_char
        prefix = prefix[1:] + next_char
    return completed_code

# Test the code completion function
prefix = "def "
completion = complete_code(prefix)
print("Autocompleted code:\n", completion)
```

This code snippet demonstrates a character-level LSTM-based autocompletion model for Python code. It preprocesses a set of code samples, trains an LSTM model to predict the next character given a sequence of characters, and generates autocompletion suggestions based on a given prefix.


## For comments
**1. Will your model work for many programming languages or will you be focusing on one? I'd recommend starting with one language before moving to others.**
- Only focus one, python.

**2. What will you data look like? Do you also some ideas on where you will get some data? Will your data include different lines of code that do the same thing?**

- Our datasets would consist of multiple .py files. Each file contains functions, projects, comments, or syntax. We need to tokenized.
- open-source projects, online code repositories, public code snippets(whole project, snippet)
- Yes, our data would include duplicate line of codes. Diverse examples of code performing similar tasks enriches the training data and enhances the AI model's ability to provide effective code autocompletion suggestions

**3. Will the output of your model provide the "best" implementation when completing some code?  How early will the model auto complete your code?**

- Our model won't provide the best implementation for sure, since we might have a really small datasets to train for the sake of computing units. However, we might try to finetune pretrained models to try to provide "best" autocompletion. 
- Factors that influence "best": This may depends on the model's traning set, the method we applied, and the specific semantic it depends on. 
- "How early" factors: Usually, we would assume that the more keywords we give (before autocomplete), the higher the accuracy of autocompletion. However, significant patterns or accurate keywords would definitely increase the speed of autocompletion. Similarly, larger model, better training set, user interaction,...


## n-gram models
n-gram models are among the stochastic language model forms that can be used for predicting the next item based on the corpus. 
Different n-gram models such as bigram, trigram, skip-gram , and GloVe are all statistical language models that are very useful in language modeling tasks.

## RNN
Recurrent neural network (RNN) model, which is capable of retaining longer source code context than traditional n-gram and other language models, has achieved mentionable success in language modeling. However, the RNN model faces limitations when it comes to representing the context of longer source codes due to gradient vanishing or exploding.
![Screenshot 2024-03-18 at 22.48.40](https://hackmd.io/_uploads/SkWIPoLAp.png)

source: Md. Mostafizer Rahman , Yutaka Watanobe , and Keita Nakamura, "A Neural Network Based Intelligent Support Model for Program Code Completion" 

## LSTM

The LSTM model has a recurrent neural network architecture that allows it to infer inputs from t-0 to t-n, where t represents the current time step and n represents the sequence number.

LSTM is well-suited for sequence prediction tasks and excels in capturing long-term dependencies. Its applications extend to tasks involving time series and sequences. LSTM’s strength lies in its ability to grasp the order dependence crucial for solving intricate problems, such as machine translation and speech recognition.

An LSTM network is a special kind of RNN that can remember longer source code sequences due to its extraordinary internal structure.

> One of the key limitations of the existing n-gram, probabilistic grammar, and even to an extent RNN approaches is their inability to encode, in whole or in part, complex long range dependencies.


![image](https://hackmd.io/_uploads/SkuSQDLC6.png)

## Comparison of neural network-based language models


| Model | Usage| advantage| limitations |
| ----- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|Transformer  | machine translation, text generation, language modeling, question answering, sentiment analysis, named entity recognition, summarization, and document classification|  Captures global dependencies effectively with self-attention. Parallelizable computation for efficiency. State-of-the-art performance in various tasks.                                                                                                                                                     |  Higher computational requirements. Large number of parameters may lead to overfitting. May struggle with very long sequences.                                                                                                                                                     |
| RNN   | modeling sequential dependencies in code, capturing the context of previous tokens to predict the next token                                                            | Capable of capturing sequential dependencies. Flexible architecture for variable-length input sequences.Effective in modeling contextual information.       | Prone to vanishing and exploding gradient problems. Sequential processing can be slow. Limited memory capacity for long sequences.                                 |
| LSTM  | modeling sequential dependencies in code, capturing the context of previous tokens to predict the next token                                                            | Addresses vanishing gradient problem of RNNs. Captures long-range dependencies effectively. Suitable for modeling short-term and long-term context.|More complex architecture and longer training times. Susceptible to exploding gradient problem. Requires careful hyperparameter tuning.|
| CNN  |capture local patterns and features in the input code, while transformer-based architectures are effective at capturing global dependencies and contextual information.|Efficient at capturing local patterns and spatial dependencies. Parallel processing for faster training and inference. Learns hierarchical representations.            |Limited in capturing sequential dependencies. Fixed-size receptive fields may struggle with variable-length context. Requires careful design and parameter tuning.      |

