In this Python Chatbot article we are going to learn about How to Build Python Chatbot with NLTK, Chatbots have become increasingly popular in recent years, and Python is one of the most popular programming languages for building them. one of the most popular libraries for building chatbots with Python is NLTK (Natural Language Toolkit). In this article we are going to talk walk How to Build Python Chatbot with NLTK.
What is NLTK ?
NLTK (Natural Language Toolkit) is leading platform for building Python programs to work with human language data. it provides simple interfaces to over 50 corpora and lexical resources such as WordNet, along with different text processing libraries for classification, tokenization, stemming, tagging, parsing, semantic reasoning and wrappers for industrial strength NLP libraries.
NLTK was developed at the University of Pennsylvania and was first released in 2001. since then it has become one of the most popular and widely used NLP libraries for Python. it is easy and extensive features make it a great tool for both beginners and experienced programmers.
First of all we need to install these libraries
1 2 |
import nltk import re |
So now it is time to preprocess the data. this involves removing any unwanted characters and converting the text to lowercase. we will also tokenize the text into words. we can use this code:
1 2 3 4 5 6 |
def preprocess(sentence): sentence = sentence.lower() sentence = re.sub(r'[^a-zA-Z0-9\s]', ' ', sentence) sentence = re.sub(r'\s+', ' ', sentence) words = nltk.word_tokenize(sentence) return words |
The next step is to create response. we will be using NLTK’s built in chatbot module to do this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
from nltk.chat.util import Chat, reflections pairs = [ [ r"my name is (.*)", ["Hello %1, how can I help you today?"] ], [ r"hi|hey|hello", ["Hello!", "Hi there!"] ], [ r"what can you do|what are your capabilities", ["I can help you with a variety of tasks. Just let me know what you need."] ], [ r"(.*) help (.*)", ["Sure, I can help you with %2. What do you need help with specifically?"] ], [ r"(.*) (thanks|thank you)(.*)", ["You're welcome!"] ], [ r"quit", ["Bye! Take care!"] ] ] chatbot = Chat(pairs, reflections) def chatbot_response(message): return chatbot.respond(message) |
And now we can test the chatbot.
1 2 3 4 5 6 7 8 |
print("Hello, I'm a chatbot. How can I help you today?") while True: message = input("You: ") response = chatbot_response(message) print("Chatbot: " + response) if message == "quit": break |
Note: also you need to download nltk punkt
1 |
nltk.download('punkt') |
This is the complete code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import nltk import re from nltk.chat.util import Chat, reflections # Step 1: Install NLTK # Uncomment the following line if you haven't installed NLTK #nltk.download('punkt') # Step 2: Import Libraries # We'll be using the re library for regular expressions # and the NLTK library for natural language processing import nltk import re # Step 3: Preprocess Data # We'll preprocess the data by removing any unwanted characters, # converting the text to lowercase, and tokenizing the text into words def preprocess(sentence): sentence = sentence.lower() sentence = re.sub(r'[^a-zA-Z0-9\s]', ' ', sentence) sentence = re.sub(r'\s+', ' ', sentence) words = nltk.word_tokenize(sentence) return words # Step 4: Create Response # We'll create a response using NLTK's built-in chatbot module pairs = [ [ r"my name is (.*)", ["Hello %1, how can I help you today?"] ], [ r"hi|hey|hello", ["Hello!", "Hi there!"] ], [ r"what can you do|what are your capabilities", ["I can help you with a variety of tasks. Just let me know what you need."] ], [ r"(.*) help (.*)", ["Sure, I can help you with %2. What do you need help with specifically?"] ], [ r"(.*) (thanks|thank you)(.*)", ["You're welcome!"] ], [ r"quit", ["Bye! Take care!"] ] ] chatbot = Chat(pairs, reflections) def chatbot_response(message): tokens = preprocess(message) response = chatbot.respond(message) return response # Step 5: Test the Chatbot # We'll test the chatbot by starting a conversation print("Hello, I'm a chatbot. How can I help you today?") while True: message = input("You: ") response = chatbot_response(message) print("Chatbot: " + response) if message == "quit": break |
When you run this code, you should see prompt that says Hello, I’m a chatbot. How can I help you today? you can then type in message and the chatbot will respond. you can continue the conversation until you type quit to exit.
This will be the output