Abstract
This document presents a chatbot implementation using NLP and ML. The chatbot predicts user
intents and generates
appropriate responses. Features include synonym-based sentence augmentation using WordNet,
intent classification
with a TF-IDF vectorizer and Naive Bayes classifier, and interactive real-time usage.
Explanation of the Source Code
1. Libraries:
- NLTK for lexical resources and synonym generation.
- Scikit-learn for TF-IDF vectorization and Naive Bayes classification.
2. Predefined intents include greetings, weather, and more.
3. Training data is expanded using synonyms to improve classification.
4. A TF-IDF vectorizer converts text into numerical data, which is classified using a Naive Bayes
model.
5. A command-line interface enables real-time interaction with the chatbot.
Source Code
import random
import nltk
from nltk.corpus import wordnet as wn
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline
import pickle
# Download required NLTK data
nltk.download('wordnet')
nltk.download('omw-1.4')
# Predefined Intents and Responses
intents = {
"greeting": ["hi", "hello", "hey", "good morning", "good evening", "what's up", "howdy", "hi there",
"hello there"],
"ask_bot_status": ["how are you", "how's it going", "what?s up", "how are you doing", "how?s life",
"how?s everything"],
"ask_name": ["what?s your name", "who are you", "what do you go by", "can you tell me your
name", "are you named anything"],
"farewell": ["bye", "goodbye", "see you later", "take care", "good night", "catch you later"],
"love": ["i love you", "i love you 3000", "i care about you", "you are amazing", "you're awesome"],
"marriage": ["will you marry me", "will you be my life partner", "let's get married", "will you marry
me, chatbot"],
"joke": ["tell me a joke", "make me laugh", "tell me something funny", "crack me up", "give me a
funny line"],
"help": ["help", "can you assist me", "what can you do", "can you help me with something", "I need
your help"],
"weather": ["what?s the weather like today", "is it sunny outside", "how?s the weather", "will it rain
today", "what's the temperature"],
"time": ["what time is it", "what?s the time", "do you know the time", "can you tell me the time"]
}
# Function to generate synonyms using WordNet
def get_synonyms(word):
synonyms = set()
for syn in wn.synsets(word):
for lemma in syn.lemmas():
synonyms.add(lemma.name())
return list(synonyms)
# Function to create multiple variations of a sentence
def generate_variations(sentence):
words = sentence.split()
variations = []
for i, word in enumerate(words):
# Get synonyms for each word
synonyms = get_synonyms(word)
if synonyms:
# Replace the word with a random synonym
new_word = random.choice(synonyms)
new_sentence = words[:i] + [new_word] + words[i+1:]
variations.append(" ".join(new_sentence))
return variations
# Function to expand training data
def expand_training_data():
training_data = []
# Iterate over each intent and sentence
for intent, sentences in intents.items():
for sentence in sentences:
# Add original sentence
training_data.append({"intent": intent, "text": sentence})
# Generate and add variations
variations = generate_variations(sentence)
for variation in variations:
training_data.append({"intent": intent, "text": variation})
return training_data
# Prepare the training data and labels
def prepare_data(training_data):
texts = [item["text"] for item in training_data]
labels = [item["intent"] for item in training_data]
return texts, labels
# Train the machine learning model
def train_model(texts, labels):
model = make_pipeline(TfidfVectorizer(), MultinomialNB())
model.fit(texts, labels)
return model
# Function to predict the intent of the user's message
def predict_intent(user_input, model):
prediction = model.predict([user_input])
return prediction[0]
# Responses for each intent
responses = {
"greeting": ["Hello!", "Hi there!", "Hey!", "Greetings!", "Good to see you!", "Howdy!"],
"ask_bot_status": ["I'm doing great, thanks for asking!", "I'm just a bot, but I'm doing fine.", "I'm
feeling great!"],
"ask_name": ["I'm a chatbot!", "I don't have a name, but you can call me Bot.", "I'm your friendly
assistant!"],
"help": ["I can help you with a lot of things!", "Ask me anything, and I'll try my best to assist you.",
"How can I assist you today?"],
"farewell": ["Goodbye!", "See you later!", "Take care!", "Catch you later!", "Bye for now!"],
"love": ["I love you too", "Love you too!", "I?m flattered, thank you!"],
"marriage": ["Sorry, I can't marry you. I'm just a chatbot.", "I can't marry you, but I can be a good
virtual friend."],
"joke": ["Why don't skeletons fight each other? They don't have the guts!", "Why did the chicken
cross the road? To get to the other side!"],
"weather": ["I can't fetch real-time weather, but you can check a weather website or app for the
latest forecast!"],
"time": ["Sorry, I can't tell you the exact time, but it?s always a good time to chat!"]
# Function to get a response based on the predicted intent
def get_response(intent):
return random.choice(responses.get(intent, ["Sorry, I didn't understand that."]))
# Main function for the chatbot
def chatbot(model):
print("Hello! I'm your chatbot. Type 'exit' to quit.")
while True:
# Get input from the user
user_input = input("You: ")
# Exit condition
if user_input.lower() == "exit":
print("Goodbye!")
break
# Predict the intent of the user input
intent = predict_intent(user_input, model)
# Get a response based on the predicted intent
response = get_response(intent)
# Print the response
print("Bot: " + response)
# Generate the training data
training_data = expand_training_data()
# Prepare the data for training
texts, labels = prepare_data(training_data)
# Train the model
model = train_model(texts, labels)
# Now you can start the chatbot with the trained model
chatbot(model)