Creating a Basic hardcoded ChatBot using Python -NLTK
Chatbots have been around in one form or the other for quite some time now. From simple scripted bots that greet users to complex bots with machine learning intelligence that can hold advanced conversations, chatbots have come a long way. But, did you know that you can create a simple chatbot using just a few lines of Python code? In this article, we will explore how to create a simple hardcoded chatbot using Python and the Natural Language Toolkit (NLTK) library.
Pre-requisites
Before we dive into the implementation, you should have a basic understanding of Python, and it will be helpful if you have some knowledge of the NLTK library. If you don’t have NLTK installed on your system, you can install it using pip by running the following command:
pip install nltk
Understanding NLTK
The Natural Language Toolkit (NLTK) is a Python library that simplifies the process of working with human language data. It provides tools for tasks such as tokenizing, stemming, and tagging text. NLTK also includes datasets and corpora for training natural language processing (NLP) models as well as pre-trained models for tasks like part-of-speech tagging and text classification.
Creating the Chatbot
To create a chatbot, we will use NLTK to perform text preprocessing tasks such as tokenization, stemming, and part-of-speech tagging, and then use simple if-else statements to create a response.
Let’s create a new Python file and save it as chatbot.py
. Start by importing the necessary libraries and datasets:
import nltk
nltk.download('punkt')
from nltk.stem import SnowballStemmer
nltk.download('averaged_perceptron_tagger')
from nltk.corpus import wordnet
from nltk.tokenize import word_tokenize, sent_tokenize
# Define stemmer object
stemmer = SnowballStemmer('english')
In the code above, we start by importing the NLTK library and downloading the necessary datasets. We then import the SnowballStemmer
module, which will help to stem words, and wordnet
module, which provides a lexical database for the English language. Also, we imported word_tokenize
and sent_tokenize
for tokenizing sentences and words in sentences. After that, we define the stemmer object, which will help to simplify our text.
Now let’s define some hardcoded conversation rules:
# Define hardcoded responses
GREETING_INPUTS = ('hello', 'hi', 'sup', 'what\'s up', 'hey')
GREETING_RESPONSES = ['Hey!', 'Howdy!', 'Hi!', 'Hello!', 'Greetings!']
# Define possible questions about the bot
QUESTION_INPUTS = ('who are you', 'what are you', 'who created you', 'why', 'how old are you')
QUESTION_RESPONSES = ['I am a simple chatbot made using Python and NLTK', 'I am a bot, not a human', 'I was created by a human', 'I don\'t know why', 'I am ageless, I am a bot.']
The above code initializes hardcoded conversation rules. The GREETING_INPUTS
include some common greetings, while GREETING_RESPONSES
are some sample chatbot responses.
Similarly, QUESTION_INPUTS
include some possible questions about the bot, such as who are you, what are you, and so on, while QUESTION_RESPONSES
are some possible responses to these questions.
Now let’s define a function that can process user input and return an appropriate response:
# Define function to handle user input
def chatbot_response(user_input):
# Convert the user input into lowercase words
words = word_tokenize(user_input.lower())
# Identify the part of speech for each word
tagged = nltk.pos_tag(words)
# Define an empty response variable
response = ''
# Loop through each word in the input
for word, tag in tagged:
# Check if the user input contains a greeting message
if word in GREETING_INPUTS:
response = response + ' ' + random.choice(GREETING_RESPONSES)
# Check if the user input contains a question message
elif word in QUESTION_INPUTS:
response = response + ' ' + random.choice(QUESTION_RESPONSES)
# Provide a random response if no specific response was determined
if not response:
response = "Sorry, I didn't understand what you said."
return response
The function takes in user input as an argument and then performs the following steps:
- Tokenizes the user input and converts it to lowercase words.
- Identifies the part of speech for each word in the input.
- Loops through each word and checks if it matches any greeting or question rules.
- If a greeting or question is identified, it returns a corresponding response. Otherwise, it returns a generic “Sorry, I didn’t understand what you said.” response.
Now let’s put everything together in the main
function:
def main():
print('Bot: Hello, My name is ChatBot. I am here to help you. Ask me anything!')
while True:
user_input = input('You: ')
response = chatbot_response(user_input)
print('Bot:', response)
exit_words = ['bye', 'cya', 'goodbye', 'quit', 'exit']
if user_input.lower() in exit_words:
print('Bot: Goodbye!')
break
if __name__ == '__main__':
main()
The main
function welcomes the user and then enters a loop to receive and process user input. It passes the user input to the chatbot_response
function, which returns an appropriate response. The program then prints the response and checks if the user wants to exit. If the user enters any of the exit_words
, the program exits the loop and prints “Goodbye!”.
Summary
In this article, we have seen how to create a basic hardcoded chatbot using Python and NLTK. We used NLTK to preprocess user input and used simple if-else statements to generate a response. While this chatbot doesn’t use any machine learning algorithms, it is a good starting point for building more advanced chatbots.
Conclusion
In conclusion, chatbots can be a valuable addition to any website or application. They can help users get quick answers to their questions without the need for human intervention. With a little bit of Python programming knowledge and the NLTK library, you can create a basic chatbot yourself. So, go ahead and give it a try!