Sitemap

Build a Simple Python Chatbot from Scratch (Beginner-Friendly)

3 min readDec 30, 2025

--

Press enter or click to view image in full size

A chatbot is a software program that interacts with users through text or voice by understanding their input and responding intelligently. Chatbots are widely used in customer support, education, healthcare, banking, and HR to automate repetitive queries and provide instant assistance 24/7.

Building a chatbot is a great way to learn Natural Language Processing (NLP) and machine learning, as it demonstrates how raw text is converted into numerical data and classified to generate meaningful responses. In this project, we build a simple chatbot from scratch using Python to understand these core concepts practically.

1. What Are We Building?

We are building a rule-based + machine learning chatbot using:

  • NLTK for text preprocessing
  • Bag of Words (BoW) for text-to-number conversion
  • Neural Network (TensorFlow/Keras) for intent classification

Important intuition:
The chatbot does NOT understand language.
It learns to map user sentences → intent labels → predefined responses.

2. Chatbot Architecture (Big Picture)

User Input
↓
Text Preprocessing (tokenize + lemmatize)
↓
Bag of Words (numerical vector)
↓
Neural Network (intent classifier)
↓
Predicted Intent
↓
Random Response from intents.json

This is the same core logic used in early customer-support bots before GPT era.

Get the code here: https://colab.research.google.com/drive/1c4cTEp7osDqlvT3vDWVzPNNMSu7XCi8M?authuser=2#scrollTo=eQdSZYRFczsB

3. Dataset: intents.json (The Brain of the Bot)

What is an Intent?

An intent represents what the user wants.

Example:

{
"tag": "hello",
"patterns": ["Hi", "Hello", "Good morning"],
"responses": ["Hey!", "Hello there!"]
}
  • patterns → user questions
  • tag → class label/ output label
  • responses → bot replies

Think of it as:

“Different ways of asking the same thing”

4. Text Preprocessing (NLP Basics)

Why preprocessing?

Computers don’t understand raw text. So, we:

  1. Tokenize → split sentence into words. ‘I am Akshat’ -> [‘I’, ‘am’, ‘Akshat’]
  2. Lowercase -> ‘Akshat -> ‘akshat’
  3. Lemmatize → convert words to base form
  • “running” → “run”

4. Remove stopwords like ‘a’, ‘the’, ‘and’, etc to reduce noise.

tokens = nltk.word_tokenize(sentence)
lemmatizer.lemmatize(word.lower())

This reduces noise and improves learning.

5. Bag of Words (Text → Numbers)

Definition (Very Important for Students)

Bag of Words converts text into a binary vector based on vocabulary presence.

Example vocabulary:

["hello", "how", "you"]

Sentence:

"hello you"

BoW Vector:

[1, 0, 1]

Why BoW?

Neural networks only understand numbers.

Get Akshat Agrawal’s stories in your inbox

Join Medium for free to get updates from this writer.

We could also use word embeddings, TF-IDF.

6. Preparing Training Data

  • train_X → BoW vectors
  • train_Y → One-hot encoded intent labels

Example:

hello → [1, 0, 0, 0]
goodbye → [0, 1, 0, 0]

This turns chatbot learning into a multi-class classification problem.

7. Neural Network Model

Understand the neural network working better- Link

Model Design (Simple but Effective)

Dense(128) → Dropout
Dense(64) → Dropout
Dense(output_size, softmax)

Why this works?

  • ReLU → learns non-linear patterns
  • Dropout → prevents overfitting
  • Softmax → gives probability for each intent

Loss:

categorical_crossentropy

Optimizer:

Adam

This is the same setup used in many production intent classifiers.

8. Training the Model

model.fit(train_X, train_Y, epochs=150)
  • Small dataset → more epochs
  • Accuracy improves steadily
  • Overfitting controlled using Dropout

9. Predicting User Intent

Step-by-step:

  1. Clean user input
  2. Convert to BoW
  3. Predict probabilities
  4. Select intent above threshold (0.5)
model.predict()

If confidence is low → fallback response:

"Sorry! I don't understand."

10. Generating the Final Response

Once intent is detected:

  • Pick a random response from that intent
  • Makes the chatbot feel less robotic

11. Chatbot in Action

User: Hi
Bot: Good morning!
User: How are you?
Bot: Actually, I'm okay and you?
User: Bye
Bot: Goodbye!

12. Real-World Use Cases

This exact architecture is used for frequently asked repeated questions in:

  • FAQ bots
  • Customer support chatbots
  • College / training institute bots
  • HR onboarding bots

Industry upgrade path:

  • Replace BoW → Word Embeddings, TF-IDF
  • Replace Dense NN → LSTM / Transformers

13. Key Learning Takeaways

  • Chatbots are classification systems, not magic
  • NLP = preprocessing + numerical representation
  • Even simple models can give powerful results
  • Strong foundation before jumping to ChatGPT APIs

In or next project, we’ll improve these chatbots using Langchain, memory and LLMs.

--

--

Akshat Agrawal
Akshat Agrawal

I'm Akshat Agrawal, a data analyst and entrepreneur passionate about data, content creation, and empowering others with data insights.