How to build your first chatbot using Python NLTK

How to build your first chatbot using Python NLTK

Introduction

A chatbot (also known as a talkbot, chatterbot, Bot, IM bot, interactive agent, or Artificial Conversational Entity) is a computer program or an artificial intelligence which conducts a conversation via auditory or textual methods. Such programs are often designed to convincingly simulate how a human would behave as a conversational partner, thereby passing the Turing test. Chatbots are typically used in dialog systems for various practical purposes including customer service or information acquisition. Some chatterbots use sophisticated natural language processing systems, but many simpler systems scan for keywords within the input, then pull a reply with the most matching keywords, or the most similar wording pattern, from a database

The term "ChatterBot" was originally coined by Michael Mauldin (creator of the first Verbot, Julia) in 1994 to describe these conversational programs. Today, most chatbots are accessed via virtual assistants such as Google Assistant and Amazon Alexa, via messaging apps such as Facebook Messenger or WeChat, or via individual organizations' apps and websites. Chatbots can be classified into usage categories such as conversational commerce (e-commerce via chat), analytics, communication, customer support, design, developer tools, education, entertainment, finance, food, games, health, HR, marketing, news, personal, productivity, shopping, social, sports, travel and utilities.

Introduction to ELIZA:

ELIZA is an early natural language processing computer program created from 1964 to 1966 at the MIT Artificial Intelligence Laboratory by Joseph Weizenbaum.

ELIZA involves the recognition of cue words or phrases in the input, and the output of corresponding pre-prepared or pre-programmed responses that can move the conversation forward in an apparently meaningful way (e.g. by responding to any input that contains the word 'MOTHER' with 'TELL ME MORE ABOUT YOUR FAMILY'). ELIZA showed that such an illusion is surprisingly easy to generate because human judges are so ready to give the benefit of the doubt when conversational responses are capable of being interpreted as "intelligent".

Target audience

chatbots help us to engage or reengage with our customers and do push-marketing to increase our sales.

Chatbots build by Ai concept are the best ones and the intelligent ones by far. They capture the most important pieces of information about our users by the conversation this is called conversational user interface (CUI). You are not only getting the basic information of your customers but most importantly their preferences and interests that are essential for any product to improve and shine.

Let’s assume you are a beverage company and you are launching a new product through a chatbot. The success of your campaign relies on two simple factors:

How many users went through the flow and claimed the coupon (or what is the turn around rate)?

Are they loyal or new customers? Which specific age group is more engaged (or what is the target audience)?

The first part of the chatbot automation process is to define what is that you are looking for — it could be a series of questions that can set the stage for a future qualification for a marketing push.

Capture and validate the user inputs (negative or positive) by this when the campaign is over I can send them a broadcast based on the input which already has been captured as a promotional offer to the most loyal customers.

The approach is like the ones who have not completed the flow or even left the conversation after the very first message by taking them to a survey that could provide valuable insights on what went wrong. The broadcast can also redirect the user back to the chatbot.

Broadcasting is currently available for Facebook Messenger, Web, Viber, and WeChat.

Creating a basic chatbot

We are going to build our chatbot without using any ML (Machine learning) and Deep learning concepts. So, our chatbot will not be an intelligent one but it will be a decent one.

Understand the Natural Language communication:

Natural language processing (NLP) can be defined as the ability of a machine to analyze, understand, and generate human speech. The goal of NLP is to make interactions between computers and humans feel exactly like interactions between humans and humans.

And when we say interactions between humans and humans, we’re talking about how humans communicate with each other by using natural language. Natural language is a language that is native to people. English, Spanish, French, and Mandarin are all examples of a natural language.

NLTK (Natural Language Toolkit) is a suite of libraries and programs for symbolic and statistical natural language processing for English written in the Python programming language.

NLTK has a module called NLTK.chat, which simplifies building these engines by providing a generic framework.

Imports:

We are going to use two imports from NLTK.chat they are chat and reflections.

CHAT: This is a class that has all the logic that is used by the chatbot

REFLECTIONS: This dictionary contains a set of input values and its corresponding output values.

Reflections are an optional dictionary to use. You can create your own dictionary in the same format given below.

reflections = {

  "i am"       : "you are",

  "i was"      : "you were",

  "i"          : "you",

  "i'm"        : "you are",

  "i'd"        : "you would",

  "i've"       : "you have",

  "i'll"       : "you will",

  "my"         : "your",

  "you are"    : "I am",

  "you were"   : "I was",

  "you've"     : "I have",

  "you'll"     : "I will",

  "your"       : "my",

  "yours"      : "mine",

  "you"        : "me",

  "me"         : "you"

}

 You can also create your own reflections dictionary in the same format as above and use it in your code.

 Here is an example for this:

my_dummy_reflections= {

    "Hi"     : "Hi",

    "hello"    : "hey there"

}

Source code:

We have created an instance of Chat class containing pairs (Set of question and answers) and reflections as discussed above.

 Example: chat = Chat(response, reflections)

To trigger the conversation, we have created

Chat.converse()

As you can see we have just hardcoded the question and answers in the list pairs to make our chatbot more interactive we need to give the hardcoded conversation.

Chatbots using NLTK.chat work on the regex of keywords present in your question. You can add any number of questions in a proper format so that your chatbot doesn’t get confused in determining the regex.

import nltk

from nltk.chat.util import Chat, reflections

response = [

    [

        r"my name is (.*)",

        ["Hello %1, How are you today ?"]

    ],

     [

        r"what is your name ?",

        ["My name is DBot and I'm a chat bot ?"]

    ],

    [

        r"how are you ?",

        ["I'm doing good\nHow about You ?"]

    ],

    [

        r"quit",

        ["Bye take care. See you soon","It was nice talking to you. See you soon :)"]

    ],

]

 

def DBot():

        print("Hi, I'm DBot :)\n Please type lowercase English language to start a conversation.\n Type quit to leave ") 

        chat = Chat(response, reflections)

        chat.converse()

if __name__ == "__main__":

    DBot()

 
  

Output:

Issues and Challenges    

The major reason for bot popularity is that it came out as the savior for a lot of apps that shouldn’t exist as stand-alone apps.

For example, small businesses like restaurants, have their own stand-alone apps. But this doesn’t make sense! Single user activity is not going to be more than once or twice a week. This means low engagement and lack of user retainment. The better deal for them is to be listed on a delivery marketplace like deliveroo, ubereats or swiggy.

A lot of chatbots are not used for what it is intended. Limited AI means massive technical challenges (untrained chatbot) such as understanding user intent from free form text. So, when it comes to customer support unless a highly trained AI is implemented, chatbots once again don’t make much sense.

So, where do they work? Marketing. Using chatbots for marketing purposes had shown very clear revenue addition and success. 

Conclusion

Today, the market is adopting technology very frequently and the user of a mobile, electric gadget is increasing day by day. The chatbot is technology which makes life easy and even more convenient for users. There are no more long waits and stay on the queue to talk to the person on the phone or going through the multiple steps to research and complaint a purchase on the website.

34.5% of the globe using chatbots, the market scope of chatbots is just booming in the tech industries.

They are different kinds of chatbots which are categorized below

By type:

Flow chatbots, Artificially intelligent chatbots, One-way AI, Two-way AI, Hybrids.

By Application:

Bots for service, Bots for social media, Bots for payments or order processing, Bots for marketing ... Others.

By Industry:

Healthcare, Retail, Banking, Financial Services, Insurance, Media & Entertainment, Travel & Tourism, E-commerce ... Others.

References and Credits

·      https://blog.neospeech.com/what-is-natural-language-processing/

·      https://chatbotslife.com/the-8-chatbots-that-actually-solves-a-problem-7cae8779a2de

·      https://oursocialtimes.com/chatbots-huge-opportunity/

·      https://en.wikipedia.org/wiki/Chatbot

Nice info keep it up all the best. I can also refer you to one of the Best Chatbot Services in Hyderabad. ? <a href= “https://winnee.ai/”>Python AI Chatbot</a>

回复

Awesome! Information. Great work thank you for sharing such useful information. keep it up all the best. I can also refer you to one of the Best Chatbot Services in Hyderabad. <a href= “https://winnee.ai/”>Python AI Chatbot</a>

回复

要查看或添加评论,请登录

Sri Vatsav V.的更多文章

社区洞察

其他会员也浏览了