Creating a Simple Chatbot Using Python
Step 1: Introduction
Open your preferred Integrated Development Environment (IDE) and create a new Python file.
Save the file with a meaningful name like "chatbot.py."
Step 2: Importing Libraries
Begin by importing the necessary libraries. In this case, add the following code to your file:
import nltk
import numpy as np
import random
import string
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
Step 3: Preprocessing the Corpus
Next, load and preprocess the corpus, which is the set of sentences you'll be training the chatbot with.
You can use the nltk library for this task. Add the following code:
nltk.download('punkt') # Download the necessary resources
# Load the corpus
with open("corpus.txt", "r", encoding="utf-8") as file:
corpus = file.read()
# Tokenize the corpus into sentences
sentences = nltk.sent_tokenize(corpus)
# Preprocess the sentences
lemmatizer = nltk.stem.WordNetLemmatizer()
remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)
# Preprocess function to tokenize, lemmatize, and convert text to lowercase
def preprocess(text):
tokens = nltk.word_tokenize(text.lower())
tokens = [lemmatizer.lemmatize(token) for token in tokens]
tokens = [token for token in tokens if token not in string.punctuation]
return tokens
# Initialize TF-IDF Vectorizer
vectorizer = TfidfVectorizer(tokenizer=preprocess)
tfidf_matrix = vectorizer.fit_transform(sentences)
Step 4: Building the Chatbot
Now, it's time to implement the chatbot's logic. Add the following code:
# Define a function to generate a response from the chatbot
def generate_response(user_input):
user_input = user_input.lower()
response = ''
# Update the TF-IDF matrix with user input
tfidf_matrix_input = vectorizer.transform([user_input])
# Compute the cosine similarity between user input and the corpus sentences
similarity_scores = cosine_similarity(tfidf_matrix_input, tfidf_matrix)[0]
max_index = np.argmax(similarity_scores)
if similarity_scores[max_index] == 0:
response = "I'm sorry, but I don't understand."
else:
response = sentences[max_index]
return response
Step 5: Interacting with the Chatbot
Finally, create a loop that allows users to interact with the chatbot. Add the following code:
while True:
user_input = input("User: ")
if user_input.lower() == "quit":
break
response = generate_response(user_input)
print("Chatbot:", response)
Step 6: Testing the Chatbot
Save the file and run the script. You can now test the chatbot by typing in different inputs and observing
the responses.
Keep in mind that this is a simple chatbot implementation based on the video you provided. You can
further enhance it by incorporating more advanced techniques or using other libraries. But I must
remind you that deploying chatbots that mimic human conversation poses ethical concerns, so be
cautious about how you use the knowledge gained from this tutorial.
I hope this helps you get started on creating your own chatbot! Let me know if you have any further
questions.