From the course: Advanced Python Projects: Build AI Applications
Processing user input - Python Tutorial
From the course: Advanced Python Projects: Build AI Applications
Processing user input
- [Instructor] In just 30 lines of code, we are going to build a simple chatbot using Python and TextBlob library. This chatbot will be capable of understanding user input and providing appropriate responses based on predefined intents. Like for example, if the user is asking for hours that a store is open, we would have intents defined, and we would then have a predefined response for those particular keywords. We'll also analyze the sentiment of the user's messages to generate responses that fit the emotional tone. By the end of this lesson, you'll understand how to implement a basic chatbot and how it processes user input. Now let's get started. First we'll need to import the TextBlob library, which will help us analyze the sentiment of the user's messages. Let's get started. Open the code for chapter one in GitHub Codespaces. Our first step is to install the TextBlob package. Go to your terminal and type (keyboard clacking) pip install TextBlob. Once the package is installed, go ahead and type from TextBlob, (keyboard clacking) import TextBlob. This line imports the TextBlob class from the TextBlob module, and allows us to leverage its functionality within our code. Now let's define the intents our chatbot will recognize. Intents are the goals or purposes behind user messages, and we will map specific keywords to corresponding responses. Now let's go ahead and type intents (keyboard clacking) equals (keyboard clacking) hours. Add a colon. Open another curly braces. (keyboard clacking) (keyboard clacking) Keywords. So now let's define the first keywords. Now let's imagine a customer or a chat bot user is asking us for the hours that a store is open. We will go ahead and define certain keywords that will signal the chat bot to provide responses related to the hours. So over here we've defined three keywords, hours, open, and close. When a user types these words as part of their message, the response will be, (keyboard clacking) we are open from 9:00 AM to 5:00 PM (keyboard clacking) Monday to Friday. Let's define one more intent. We're going to label this return. Now, anytime we have keywords related to returns such as refund, money back, (keyboard clacking) or return, we will have the chatbot respond with a certain response. Add a comma on the previous line. And in the response, type, (keyboard clacking) I'd be happy to help you with the return process. (keyboard clacking) Let me transfer you to a live agent. Based on my personal experience, I find it helpful to always have a live agent help me with my returns, so I will go ahead and enter this message here. So to summarize, here we are creating a dictionary called intents. Each intent, like hours and return, has a set of keywords that the chatbot will look for and use our input and response that it will provide when those keywords are detected.