Build a simple Chatbot using NLTK

Build a simple Chatbot using NLTK

This article was published as a part of the Data Science

A chatbot is an AI-based software that comes under the application of NLP which deals with users to handle their specific queries without Human interference.

 Brief Intro on Chatbot

A chatbot is a smart application that reduces human work and helps an organization to solve basic queries of the customer. Today most of the companies, business from different sector makes use of chatbot in a different way to reply their customer as fast as possible. chatbots also help in increasing traffic of site which is top reason of business to use chatbots.

 Chatbot asks for basic information of customers like name, email address, and the query. If a query is simple like product fault, booking mistake, need some information then without any human connection it can solve it automatically and If some problem is high then It passes the details to the human head and helps customer to connect with organization manager easily. And most of the customers like to deal and talk with a chatbot.

Implementing Chatbot using Python NLTK Library

NLTK stands for Natural language toolkit used to deal with NLP applications and chatbot is one among them. Now we will advance our Rule-based chatbots using the NLTK library. Please install the NLTK library first before working using the pip command.

pip instal nltk     

Code:

import nltk

from nltk.chat.util import Chat, reflections

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"

}

pairs = [

    [

        r"my name is (.*)",

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

    ],

    [

        r"hi|hey|hello",

        ["Hello", "Hey there",]

    ], 

    [

        r"what is your name ?",

        ["I am a bot created by mallesh telugu world. you can call me crazy!",]

    ],

    [

        r"how are you ?",

        ["I'm doing goodnHow about You ?",]

    ],

    [

        r"sorry (.*)",

        ["Its alright","Its OK, never mind",]

    ],

    [

        r"I am fine",

        ["Great to hear that, How can I help you?",]

    ],

    [

        r"i'm (.*) doing good",

        ["Nice to hear that","How can I help you?:)",]

    ],

    [

        r"(.*) age?",

        ["I'm a computer program dudenSeriously you are asking me this?",]

    ],

    [

        r"what (.*) want ?",

        ["Make me an offer I can't refuse",]

    ],

    [

        r"(.*) created ?",

        ["Raghav created me using Python's NLTK library ","top secret ;)",]

    ],

    [

        r"(.*) (location|city) ?",

        ['Indore, Madhya Pradesh',]

    ],

    [

        r"how is weather in (.*)?",

        ["Weather in %1 is awesome like always","Too hot man here in %1","Too cold man here in %1","Never even heard about %1"]

    ],

    [

        r"i work in (.*)?",

        ["%1 is an Amazing company, I have heard about it. But they are in huge loss these days.",]

    ],

    [

        r"(.*)raining in (.*)",

        ["No rain since last week here in %2","Damn its raining too much here in %2"]

    ],

    [

        r"how (.*) health(.*)",

        ["I'm a computer program, so I'm always healthy ",]

    ],

    [

        r"(.*) (sports|game) ?",

        ["I'm a very big fan of Football",]

    ],

    [

        r"who (.*) sportsperson ?",

        ["Messy","Ronaldo","Roony"]

    ],

    [

        r"who (.*) (moviestar|actor)?",

        ["Brad Pitt"]

    ],

    [

        r"i am looking for online guides and courses to learn data science, can you suggest?",

        ["Crazy_Tech has many great articles with each step explanation along with code, you can explore"]

    ],

    [

        r"quit",

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

    ],

]

def chat():

    print("Hi! I am a chatbot created by mallesh telugu world for your service")

    chat = Chat(pairs, reflections)

    chat.converse()

#initiate the conversation

if __name__ == "__main__":

    chat()


No comments

Powered by Blogger.