Chatbot_Abstract_and_Source_Code
Chatbot_Abstract_and_Source_Code
This document presents a chatbot implementation using NLP and ML. The chatbot predicts user
intent classification
with a TF-IDF vectorizer and Naive Bayes classifier, and interactive real-time usage.
1. Libraries:
4. A TF-IDF vectorizer converts text into numerical data, which is classified using a Naive Bayes
model.
Source Code
import random
import nltk
import pickle
nltk.download('wordnet')
nltk.download('omw-1.4')
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
"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
"time": ["what time is it", "what?s the time", "do you know the time", "can you tell me the time"]
}
def get_synonyms(word):
synonyms = set()
synonyms.add(lemma.name())
return list(synonyms)
def generate_variations(sentence):
words = sentence.split()
variations = []
synonyms = get_synonyms(word)
if synonyms:
new_word = random.choice(synonyms)
variations.append(" ".join(new_sentence))
return variations
training_data = []
variations = generate_variations(sentence)
return training_data
def prepare_data(training_data):
model.fit(texts, labels)
return model
# Function to predict the intent of the user's message
prediction = model.predict([user_input])
return prediction[0]
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.",
"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
"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!"]
def chatbot(model):
while True:
# Exit condition
if user_input.lower() == "exit":
print("Goodbye!")
break
response = get_response(intent)
training_data = expand_training_data()
# Prepare the data for training
# Now you can start the chatbot with the trained model
chatbot(model)