This project is a chatbot application that uses machine learning to understand user inputs and generate appropriate responses. It is built using Python, Natural Language Processing (NLP) techniques, and a neural network model created with TensorFlow and Keras.
The project depends on the following libraries:
- Python 3.x
- TensorFlow
- Keras
- NLTK
- Numpy
You can install all dependencies using the requirements.txt file.
pip install -r requirements.txtTo use the Natural Language Toolkit (NLTK), you need to download specific datasets. In the code, there are two commented lines for downloading NLTK datasets:
- punkt_tab for tokenizing words.
- wordnet for word lemmatization.
You can manually download these using the following commands inside your script or interactively:
import nltk
nltk.downlaod('punkt')
nltk.download('wordnet')- Clone the repository to your local machine:
git clone https://github.com/yourusername/chatbot-project.git
cd chatbot-project- Install the required packages:
pip install -r requirements.txt- Download NLTK datasets as shown in the (#Requirements) section.
The project directory is structured as follows:
chatbot-project/
│
├── intents.json # Contains training data and patterns
├── train_chatbot.py # Script to preprocess data and train the chatbot model
├── chatbot_model.h5 # Saved trained model
├── words.pkl # Preprocessed words for the model
├── classes.pkl # Classes (responses) for the model
├── chatbot.py # Main script to interact with the chatbot
├── README.md # Project documentation (this file)
└── requirements.txt # Python dependenciesintents.json: The file contains predefined intents (categories) and responses that the chatbot can use to understand user input and generate appropriate replies.train_chatbot.py: Script that preprocesses theintents.jsonfile, builds and trains a neural network model, and saves the trained model and processed data.chatbot_model.h5: The trained model is saved in this file.words.pkland classes.pkl: These pickle files store processed data for words and classes that the chatbot uses during prediction.
- Training the Model
The first step is to train the chatbot model using the training script. Make sure your intents.json file is filled with proper training data.
Run the training script:
python train_chatbot.pyThis will preprocess the training data, build a neural network model, and save the trained model in chatbot_model.h5. It will also save the processed words and classes in words.pkl and classes.pkl files, respectively.
- Running the Chatbot
Once the model is trained, you can run the chatbot using the main script:
python chatbot.pyYou can now interact with the chatbot via the terminal, and it will respond based on the model’s predictions.
The model is a Sequential neural network built with TensorFlow and Keras. The architecture consists of:
- Input layer: Takes the preprocessed training data.
- Hidden layers: Dense layers with activation functions.
- Output layer: Produces a probability for each class (intent) based on the input.
- Tokenization and Lemmatization: Input sentences are tokenized and lemmatized to reduce words to their root forms.
- Bag of Words Representation: Sentences are converted into a numerical bag of words representation.
- Training the Model: The preprocessed data is used to train the neural network, and the model is saved for future use.
- Customizing Responses: You can modify or add more intents to the
intents.jsonfile to expand the chatbot's responses. After making changes, you will need to retrain the model. - Pickle Files:
words.pklandclasses.pklstore the processed vocabulary and intent classes, which allow the chatbot to map user inputs to intents more efficiently.