NLP(Natural language Processing)-Part 6
Vidura Bandara Wijekoon
Certified AI Engineer|Product Owner & Sri Lankan Chapter Lead@Omdena| Senior Software Engineer @Virtusa | Former Cofounder & C.O.O @Trinet Innovations|Speaker|Mentor|Bsc(Hons) Electrical and information Engineering
Here we are going to talk about creating a simple foundation Chabot with natural language processing by python. We are going to create ELIZA style Chabot basic with python.
Initially I hope you have installed python idle latest version and some platform such as PyCharm, visual studio etc. Here, I am using PyCharm interpreter. I am going to write simply test Chabot.
Here what happens in above, we first created a function called respond() with a parameter called message It returns the bot’s response We are adding two types in 8th line which are ‘Hi! What’s up .You said:’ and message. Message contains the parameter value which we can assign into any text. After the concatenation (adding things), storing will be done by bot_message In last line we tell the text bot to print the defined function with the parametric value of Good morning.
Respond is the defined function which responds to a user’s input. ‘Hi! What’s up .You said: ' is the standard bot response (message).This bot is just repeating the words we said.
Let’s try to make the output chat as dialogue. We can write code as following. If you don’t use return function, it prints nothing.
Similarly, as in previous case, we first created a function called respond () with a parameter called message .It returns the bot’s response. We are doing all steps except the last line. We can insert variables into string by format method. {} acts as placeholders which can store input text. When we gave a place by the argument we passed what we called is format. To track the chat, another function can be called which is send_message.It prints the user’s message and the bot’s message.
- Line 16: Use the user_template string’s. Format() method to pass the user’s message to user’s template and get the output
- Line 18: Calling the function and with the value of message passed in and stored it.
- Line 20: Use the user_template string’s.format() method to get the output of stored value in response,
- Line 26: Send a message which is telling good morning to the bot.
To fill the human chatting environment we import time function and delays 1s in this code.
Like that, we can simply create a test bot and get a basic familiarization of how to create a chat bot. This is a long process. Today, we just looked what we can do with simple lines of code. Let’s meet in next article. Feel free to share your ideas.