Creating a Chatbot with Python
Overview
Creating a chatbot from scratch may seem like a daunting task, but with Python, it's far from it. Python possesses libraries that make it easy to create a chatbot even for beginners. This tutorial aims to guide you on how to build a basic chatbot in Python. We will start with a brief understanding of what chatbots are, and then we'll dive into the coding part.
What is a Chatbot?
A chatbot is an artificial intelligence (AI) software designed to interact with humans in their natural language. These interactions can occur on websites, mobile apps, and even through the telephone. They are great for customer service, as they can provide 24/7 assistance and even perform tasks such as online transactions.
Prerequisites
Before we proceed, there are a few prerequisites you should have:
Basic understanding of Python: You should know how Python's syntax works and be familiar with variables, loops, functions and importing libraries.
The Python environment set up: Make sure you have Python installed on your computer. You can download it from the official website (https://www.python.org/downloads/). Moreover, an integrated development environment (IDE) like PyCharm or a notebook interface like Jupyter can be of great help.
Building a Chatbot in Python
Step 1: Installing the Required Libraries
We will be using the nltk
and newspaper3k
libraries for this chatbot. To install these, open your command prompt or terminal and type:
pip install nltk newspaper3k
Step 2: Importing the Libraries
Once the libraries are installed, we import them in our Python script.
import nltk
from newspaper import Article
We also need to download 'punkt' from nltk, which is a pre-trained tokenizer. We use it to divide a text into a list of sentences.
nltk.download('punkt')
Step 3: Scraping the Data
In this step, we will scrape data from a web article which will serve as the information for the chatbot.
url = 'https://www.your-article-url.com' # replace this with the URL of the article you want to scrape
article = Article(url)
# Download the article
article.download()
# Parse the article
article.parse()
# Apply Natural Language Processing (nlp)
article.nlp()
# Get the article text
corpus = article.text
Step 4: Tokenizing the Corpus
Here we break the corpus (the entire text of the article) into sentences that can be used to feed to the chatbot.
text = corpus
sentence_list = nltk.sent_tokenize(text) # A list of sentences
Step 5: Creating the Chatbot's Response
We will create a function to return a random greeting response to a user's greeting.
import random
def greeting_response(text):
text = text.lower()
# Bots greeting response
bot_greetings = ['howdy', 'hi', 'hello', 'hola']
# Users greeting
user_greetings = ['hi', 'hey', 'hello', 'hola', 'greetings', 'wassup']
for word in text.split():
if word in user_greetings:
return random.choice(bot_greetings)
Step 6: Creating the Main Function
The main function will take in a user input and return the chatbot's response.
def bot_response(user_input):
user_input = user_input.lower()
sentence_list.append(user_input)
bot_response = ''
cm = CountVectorizer().fit_transform(sentence_list)
similarity_scores = cosine_similarity(cm[-1], cm)
similarity_scores_list = similarity_scores.flatten()
index = index_sort(similarity_scores_list)
index = index[1:]
response_flag = 0
j = 0
for i in range(len(index)):
if similarity_scores_list[index[i]] > 0.0:
bot_response = bot_response+' '+sentence_list[index[i]]
response_flag = 1
j = j+1
if j > 2:
break
if response_flag == 0:
bot_response = bot_response+' '+"I apologize, I don't understand."
sentence_list.remove(user_input)
return bot_response
That's it! You've created a simple chatbot in Python. Remember, this is a basic chatbot and it won’t recognize complex queries, but it’s a start. From here, you can use more advanced libraries like ChatterBot
to create more sophisticated chatbots. Happy coding!