Script
Hello everyone, welcome to IntelliPath's live webinar on creating a chatbot using Python, NLP, and Data Science. Today's webinar will be conducted by Mr. Ankit Tanmay, who is a Senior Data Scientist with eight years of experience in providing data-driven solutions to complex business problems. Presently, he is working at Microsoft as a Data and Applied Scientist. Let's welcome Mr. Ankit into the session.
Thank you for the warm welcome. Hi, everyone. It's great to meet and interact with AI and ML enthusiasts, as well as experts in this field. Today, we will delve into chatbots and cover key concepts, practical implementation, and coding, all while being mindful of the time and content demands.
A chatbot is a software application used to conduct online chat conversations via text or text-to-speech without contact with a human agent. This technology is a significant example of AI in action, capable of solving complex business problems.
The first chatbot, Eliza, was created in 1966. Eliza recognized keywords or phrases in input to provide a meaningful conversational flow. The efficiency of AI systems like chatbots is measured by the Turing test, helping determine their human-like accuracy.
Chatbots have numerous applications, including:
Our focus will be primarily on rule-based chatbots and self-learning (reinforcement) chatbots.
The process involves:
Here’s an example of creating a rule-based chatbot using Python:
import re
import random
class RuleBot:
def __init__(self):
self.negative_responses = ("no", "nope", "nah", "not a chance")
self.exit_commands = ("quit", "pause", "exit", "goodbye", "bye")
self.rand_questions = (
"How are you?", "What's up?", "How are you doing?"
)
self.alien_babble = (
'describe_planet_intent': r'.*\s*your planet.*',
'answer_why_intent': r'.*\s*why.*',
'about_intellipaat_intent': r'.*\s*intellipaat.*'
)
def greet(self):
self.name = input("What's your name?")
return f"Hi (self.name), I'm a robot. Will you help me learn about your planet?"
def make_exit(self, reply):
for exit_command in self.exit_commands:
if exit_command in reply:
print("Okay, have a nice Earth day!")
return True
return False
def match_reply(self, reply):
for intent, pattern in self.alien_babble.items():
if re.match(pattern, reply):
if intent == 'describe_planet_intent':
return random.choice(["My planet is Elara", "I come from a distant galaxy"])
elif intent == 'answer_why_intent':
return random.choice(["I want to learn more about humans", "I seek knowledge"])
elif intent == 'about_intellipaat_intent':
return random.choice(["Intellipaat is a great place to learn AI!", "Intellipaat offers great courses!"])
return random.choice(["Tell me more!", "Why do you think so?", "I need more information"])
Let’s now explore building a more intelligent chatbot using Python and NLTK. This chatbot will read a corpus of text, preprocess it, and use cosine similarity for fetching responses.
import nltk
from nltk.stem import WordNetLemmatizer
import numpy as np
import random
import string
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
## Introduction
raw_text = open('<path-to-your-data>.txt', 'r', errors='ignore').read().lower()
## Introduction
sent_tokens = nltk.sent_tokenize(raw_text)
word_tokens = nltk.word_tokenize(raw_text)
lemmatizer = WordNetLemmatizer()
def LemTokens(tokens):
return [lemmatizer.lemmatize(token) for token in tokens if token not in punctuation_remover]
def LemNormalize(text):
return LemTokens(nltk.word_tokenize(text.lower().translate(punctuation_remover)))
## Introduction
def response(user_response):
robo_response = ''
TfidfVec = TfidfVectorizer(tokenizer=LemNormalize, stop_words='english')
tfidf = TfidfVec.fit_transform(sent_tokens)
vals = cosine_similarity(tfidf[-1], tfidf)
idx = vals.argsort()[0][-2]
flat = vals.flatten()
flat.sort()
req_tfidf = flat[-2]
if req_tfidf == 0:
robo_response = "I'm sorry! I don't understand you."
return robo_response
else:
robo_response = robo_response + sent_tokens[idx]
return robo_response
## Introduction
flag = True
print("ChatBot: I'm your Bot. Type 'bye' to exit")
while flag:
user_response = input()
user_response = user_response.lower()
if user_response != 'bye':
if user_response == 'thanks' or user_response == 'thank you':
flag = False
print("ChatBot: You're welcome!")
else:
if greeting(user_response) is not None:
print("ChatBot: " + greeting(user_response))
else:
sent_tokens.append(user_response)
word_tokens = word_tokens + nltk.word_tokenize(user_response)
final_words = set(word_tokens)
print("ChatBot: ", end="")
print(response(user_response))
sent_tokens.remove(user_response)
else:
flag = False
print("ChatBot: Goodbye!")
This chatbot uses a text file for learning and responds based on the closest match using cosine similarity.
Conclusion Chatbots are versatile applications of AI and NLP that can significantly improve customer service and automation within organizations. By understanding both rule-based and self-learning chatbots, you can leverage Python and NLTK to build your own intelligent conversational agents.
A chatbot is a software application designed to simulate conversation with human users, especially over the Internet.
Chatbots can be classified into simple chatbots, smart chatbots, hybrid chatbots, social messaging chatbots, and menu-based chatbots.
Commonly used libraries include nltk
, re
, random
, numpy
, sklearn
for different functionalities in the chatbot pipeline.
Tokenization is the process of splitting text into individual words or phrases, which are called tokens.
Cosine similarity measures the similarity between two vectors of an inner product space, which helps in identifying the most relevant sentence from a text corpus in response to the user's query.
TF-IDF (Term Frequency-Inverse Document Frequency) Vectorizer converts a collection of raw documents into a matrix of TF-IDF features, giving more importance to rare words in the document.
In addition to the incredible tools mentioned above, for those looking to elevate their video creation process even further, Topview.ai stands out as a revolutionary online AI video editor.
TopView.ai provides two powerful tools to help you make ads video in one click.
Materials to Video: you can upload your raw footage or pictures, TopView.ai will edit video based on media you uploaded for you.
Link to Video: you can paste an E-Commerce product link, TopView.ai will generate a video for you.