Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🤖 MachineLearning-ChatBOT

Intent-classification chatbot built from scratch · PyTorch feedforward neural network · NLTK preprocessing · Fully retrainable · CLI interface

Python PyTorch NLTK License


A trainable intent-classification chatbot built with Python and PyTorch. It processes natural language input using NLP preprocessing techniques and a feedforward neural network to identify user intent and respond accordingly.


How It Works

User input goes through three stages:

  1. Preprocessing (nltk_utils.py) — tokenization and stemming via NLTK, then converted to a bag-of-words vector.
  2. Classification (model.py) — a 3-layer feedforward neural network (NeuralNet) maps the BoW vector to an intent class using ReLU activations and CrossEntropyLoss.
  3. Response (chat.py) — if the predicted intent has confidence above 0.75, a random response from that intent's response pool is returned. Otherwise the bot says it doesn't understand.

Project Structure

MachineLearning-ChatBOT/
├── intents.json      # Training data: intent tags, patterns, and responses
├── nltk_utils.py     # Tokenization, stemming, bag-of-words helpers
├── model.py          # NeuralNet definition (3 linear layers + ReLU)
├── train.py          # Training loop — reads intents.json, saves model to data.pth
├── chat.py           # Inference loop — loads data.pth, runs conversation
└── data.pth          # Saved model weights and vocabulary (generated after training)

Model Architecture

Input (BoW vector)  →  Linear(input_size, 8)  →  ReLU
                    →  Linear(8, 8)            →  ReLU
                    →  Linear(8, num_classes)  →  (CrossEntropyLoss at training)

Hyperparameters (defaults):

Parameter Value
Epochs 1000
Batch size 8
Learning rate 0.001
Hidden size 8
Optimizer Adam

Requirements

  • Python 3.7+
  • PyTorch
  • NLTK
  • NumPy

Install dependencies:

pip install torch nltk numpy

Download NLTK data (first time only):

import nltk
nltk.download('punkt')

Usage

1. Customize Intents

Edit intents.json to define your own intent tags, training patterns, and responses:

{
  "intents": [
    {
      "tag": "greeting",
      "patterns": ["Hi", "Hello", "Hey there"],
      "responses": ["Hello!", "Hi there!", "Hey! How can I help?"]
    }
  ]
}

2. Train the Model

python train.py

This generates data.pth containing the trained model weights and vocabulary. Training progress is printed every 100 epochs.

3. Run the Chatbot

python chat.py

Type your message and press Enter. Type quit to exit.

Let's chat! (type 'quit' to exit)
You: hello
Sam: Hi there! How can I help?
You: quit

Customization

To add new topics: Add new intent objects to intents.json, then re-run train.py. No code changes needed.

To adjust confidence threshold: In chat.py, change 0.75 in if prob.item() > 0.75 — lower values make the bot more permissive, higher values make it more conservative.

To change the bot's name: In chat.py, update bot_name = "Sam".


Limitations

  • Not generative. Responses are selected from a predefined pool — the model classifies intent only.
  • No conversation context. Each input is processed independently; the bot has no memory of prior turns.
  • Vocabulary-bound. Words not seen during training are unknown to the model. Adding new topics requires retraining.
  • Small hidden size. The default hidden size of 8 is sufficient for toy datasets. Scale up for larger intent sets.

License

MIT

About

A rule-based intent classification chatbot built with Python and PyTorch. It uses NLTK for text preprocessing (tokenization, stemming, bag-of-words), a feedforward neural network (model.py) trained on manually defined intents (intents.json), and saves/loads model weights via data.pth. The train.py script handles training; chat.py runs inference.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages