Complete project: Create a chatbot that understands context, handles NLU, and maintains conversations.
Learn to build a chatbot that understands user intent and maintains meaningful conversations.
Project Structure
Components:
├─ Intent Recognizer (what does user want?)
├─ Entity Extractor (what are they talking about?)
├─ Dialogue Manager (manage conversation flow)
├─ Response Generator (formulate answers)
└─ Learning System (improve over time)
Step 1: Intent Recognition
import spacy
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction.text import TfidfVectorizer
# Load NLP model
nlp = spacy.load("en_core_web_sm")
# Define intents
intents = [
"greet",
"goodbye",
"help",
"product_info",
"complaint"
]
# Training data
training_data = [
("Hello, how are you?", "greet"),
("Hi there!", "greet"),
("I want to leave", "goodbye"),
("Can you help me?", "help"),
("Tell me about product X", "product_info"),
("This product is broken", "complaint")
]
# Feature extraction
vectorizer = TfidfVectorizer(max_features=100)
X = vectorizer.fit_transform([text for text, _ in training_data])
y = [intent for _, intent in training_data]
# Train classifier
intent_model = RandomForestClassifier(n_estimators=100)
intent_model.fit(X, y)
def recognize_intent(user_message):
X_test = vectorizer.transform([user_message])
intent = intent_model.predict(X_test)[0]
confidence = intent_model.predict_proba(X_test).max()
return intent, confidence
def extract_entities(user_message):
doc = nlp(user_message)
entities = {}
for ent in doc.ents:
if ent.label_ not in entities:
entities[ent.label_] = []
entities[ent.label_].append(ent.text)
return entities
# Usage
entities = extract_entities("I'm looking for products in New York")
# Output: {'GPE': ['New York']}
Step 3: Dialogue Manager
class DialogueManager:
def __init__(self):
self.context = {}
self.history = []
def handle_message(self, user_message):
# Recognize intent
intent, confidence = recognize_intent(user_message)
if confidence < 0.6:
return "I'm not sure what you're asking. Can you rephrase?"
# Extract entities
entities = extract_entities(user_message)
# Update context
self.context.update(entities)
self.history.append({
"user": user_message,
"intent": intent,
"entities": entities
})
# Route to handler
response = self.handle_intent(intent, entities)
return response
def handle_intent(self, intent, entities):
if intent == "greet":
return "Hello! How can I help you today?"
elif intent == "product_info":
product = entities.get('PRODUCT', [''])[0]
return f"Here's information about {product}..."
elif intent == "help":
return "I can help with:\n1. Product info\n2. Complaints\n3. Orders"
elif intent == "goodbye":
return "Goodbye! Have a great day!"
else:
return "I'm not sure how to help with that."
Step 4: Response Generation
class ResponseGenerator:
def __init__(self):
self.response_templates = {
"greet": [
"Hello! How are you?",
"Hi there! What can I help?",
"Welcome! What can I do for you?"
],
"product_info": [
"Product {product} has the following features: {features}",
"Here's what I know about {product}: {features}"
],
"goodbye": [
"Goodbye! Have a great day!",
"Thanks for chatting! See you later!",
"Bye! Come back soon!"
]
}
def generate_response(self, intent, **kwargs):
import random
templates = self.response_templates.get(intent, ["I'm not sure."])
template = random.choice(templates)
return template.format(**kwargs)
# Usage
gen = ResponseGenerator()
response = gen.generate_response("greet")
Step 5: Complete Chatbot
class Chatbot:
def __init__(self):
self.dialogue_manager = DialogueManager()
self.response_gen = ResponseGenerator()
self.intent_model = intent_model
self.vectorizer = vectorizer
def chat(self, user_message):
# Process message
response = self.dialogue_manager.handle_message(user_message)
# Log conversation
print(f"User: {user_message}")
print(f"Bot: {response}\n")
return response
# Interactive chat
chatbot = Chatbot()
while True:
user_input = input("You: ")
if user_input.lower() in ["quit", "exit"]:
break
chatbot.chat(user_input)
Step 6: Integration with Flask API
from flask import Flask, request, jsonify
app = Flask(__name__)
chatbot = Chatbot()
@app.route('/chat', methods=['POST'])
def chat():
data = request.get_json()
user_message = data.get('message')
response = chatbot.chat(user_message)
return jsonify({
'user_message': user_message,
'bot_response': response
})
if __name__ == '__main__':
app.run(debug=True)
Testing & Evaluation
import pytest
class TestChatbot:
def setup_method(self):
self.chatbot = Chatbot()
def test_greet(self):
response = self.chatbot.chat("Hello")
assert "how" in response.lower() or "help" in response.lower()
def test_entity_extraction(self):
entities = extract_entities("I want a laptop")
assert any("laptop" in e.lower() for vals in entities.values() for e in vals)
def test_intent_recognition(self):
intent, conf = recognize_intent("Tell me about your products")
assert intent == "product_info"
assert conf > 0.5
# Run tests
pytest.main([__file__, '-v'])
Summary
This project teaches:
- Intent recognition
- Entity extraction
- Dialogue management
- Response generation
- Chatbot deployment
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Build a Conversational Chatbot.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, projects, chatbot, project, build a conversational chatbot