What is God? Make a simple chatbot with python

Chatbots are often used. This article is a memo of a chatbot that I made by reading an article on Medium. The original article is here:

https://medium.com/analytics-vidhya/building-a-simple-chatbot-in-python-using-nltk-7c8c8215ac6e


What is a chatbot?

Chatbots are used in a variety of fields for different purposes, such as i) Support bots, designed to solve customer requests related to the delivery of a service or use of a product, and ii) Financial bots, aimed to resolve inquiries about financial services. Chatbots may have some constraints regarding the requests that they can respond and the vocabulary that they can employ, which depends on the specific domain where they are serving on. Furthermore, according to the Hype Cycle for emerging technologies by Gartner [2], conversational AI platforms remain in the phases of “innovation trigger” and “peak of inflated expectations”, meaning that they are getting substantial attention from the industry.

Besides the aforementioned use cases for chatbots, cybersecurity is one of the newest where to apply this technology. Thus, there exist chatbots focused on training end-users [3] or cyber analysts [4] in security awareness and incident response. Further, there are also malicious chatbots devoted to malware distribution through a human-machine conversation [5]. In addition, there is software designed to guide the user in terms of security and privacy, such as Artemis [6], a conversational interface to perform precision-guided analytics on endpoint data. Most of these security chatbots are implemented in a question-answering context [7] using a post-reply technique. As far as we know, the use of chatbots to profile suspects in an active way of child pornography has been little investigated, existing few approaches [8, 9] employing a chatbot to emulate a victim such as a child or a teenager. Likewise, our investigation aims to emulate a vulnerable person while the suspect offers him/her illegal content.

chatbot.png


Building the chatbot

In this section, we'll cover NLP and NLG in this project.

First, we'll need to import the relevant libraries:

import io
import random
import string # to process standard python strings
import warnings
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import warnings
warnings.filterwarnings('ignore')
import nltk
from nltk.corpus import gutenberg
from nltk.stem import WordNetLemmatizer
nltk.download('popular', quiet=True) # for downloading packages

If this is your first time using nltk, don't forget to run the following line and use the GUI to download all the packages.

nltk.download()

Preprocessing

raw = gutenberg.raw('bible-kjv.txt')
#Tokenisation
sent_tokens = nltk.sent_tokenize(raw)# converts to list of sentences 
word_tokens = nltk.word_tokenize(raw)# converts to list of words

# Preprocessing
lemmer = WordNetLemmatizer()
def LemTokens(tokens):
    return [lemmer.lemmatize(token) for token in tokens]
remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)
def LemNormalize(text):
    return LemTokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))

It doesn't really matter which corpus you use. Here I use the bible from gutenberg.

Keyword Matching

GREETING_INPUTS = ("hello", "hi", "greetings", "sup", "what's up","hey")
GREETING_RESPONSES = ["hi", "hey", "*nods*", "hi there", "hello", "I am glad! You are talking to me"]

def greeting(sentence):
    """If user's input is a greeting, return a greeting response"""
    for word in sentence.split():
        if word.lower() in GREETING_INPUTS:
            return random.choice(GREETING_RESPONSES)

Generating Response TfidfVectorizer and cosine_similarity will be used to find the similarity between words entered by the user and the words in the corpus. This is the simplest possible implementation of a chatbot.

def response(user_response):
    robo_response=''
    sent_tokens.append(user_response)
    TfidfVec = TfidfVectorizer(tokenizer=LemNormalize, stop_words='english')
    tfidf = TfidfVec.fit_transform(sent_tokens)
    vals = cosine_similarity(tfidf[-1], tfidf)
    idx=vals.argsort()[0][-2]
    flat = vals.flatten()
    flat.sort()
    req_tfidf = flat[-2]
    if(req_tfidf==0):
        robo_response=robo_response+"I am sorry! I don't understand you"
        return robo_response
    else:
        robo_response = robo_response+sent_tokens[idx]
        return robo_response

Start and end of the conversation

flag=True
print("ROBO: My name is Robo. I will answer your queries about Chatbots. If you want to exit, type Bye!")
while(flag==True):
    user_response = input()
    user_response=user_response.lower()
    if(user_response!='bye'):
        if(user_response=='thanks' or user_response=='thank you' ):
            flag=False
            print("ROBO: You are welcome..")
        else:
            if(greeting(user_response)!=None):
                print("ROBO: "+greeting(user_response))
            else:
                print("ROBO: ",end="")
                print(response(user_response))
                sent_tokens.remove(user_response)
    else:
        flag=False
        print("ROBO: Bye! take care..")

This approach is very basic for a chatbot as the response from the bot is simply based on keywords matching.

Recommended Posts

What is God? Make a simple chatbot with python
[Python] What is a with statement?
[Python] Make a simple maze game with Pyxel
Make a fortune with Python
What is a python map?
Make a simple Slackbot with interactive button in python
Let's make a simple game with Python 3 and iPhone
[Python] What is a zip function?
Let's make a GUI with python.
Make a recommender system with python
[Python] Make a simple maze game with Pyxel-Make enemies appear-
Creating a simple PowerPoint file with Python
Let's make a shiritori game with Python
Rubyist tried to make a simple API with Python + bottle + MySQL
Make a simple OMR (mark sheet reader) with Python and OpenCV
Let's make a voice slowly with Python
Let's make a simple language with PLY 1
What is a dog? Python installation volume
What is python
Let's make a web framework with Python! (1)
Make a desktop app with Python with Electron
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
I made a simple blackjack with Python
What is Python
order_by ('-created_at') ← What is "ー"? ?? ?? [Beginner learns python with a reference book]
Make a Twitter trend bot with heroku + Python
[Python] Make a game with Pyxel-Use an editor-
I want to make a game with Python
Start a simple Python web server with Docker
Try to make a "cryptanalysis" cipher with Python
What are you comparing with Python is and ==?
Try to make a dihedral group with Python
A simple to-do list created with Python + Django
Tell me what a conformal map is, Python!
What is a distribution?
[Python] What is Pipeline ...
What is a terminal?
What is a hacker?
What is a pointer?
[Python] What is virtualenv
I tried to make a simple mail sending application with tkinter of Python
Make one repeating string with a Python regular expression.
Try to make a command standby tool with python
[Practice] Make a Watson app with Python! # 2 [Translation function]
[Practice] Make a Watson app with Python! # 1 [Language discrimination]
[Let's play with Python] Make a household account book
Make a breakpoint on the c layer with python
Basics of Python learning ~ What is a string literal? ~
Make a CSV formatting tool with Python Pandas PyInstaller
[Super easy] Let's make a LINE BOT with Python.
[Python] Make sure the received function is a user-defined function
[Vagrant] Set up a simple API server with python
Implement a circular expression binary search in Python. There is a comparison with a simple full search.
[Mac] I want to make a simple HTTP server that runs CGI with Python
What is a decision tree?
Python list is not a list
Make a bookmarklet in Python
What is a Context Switch?
[Python] Python and security-① What is Python?
What is a super user?