Today, if you are about to order some foods on a restaurant’s website or you need assistance because your router is not working properly, you will probably get in touch with a chatbot. They appear to you like instant messaging chats, in one of the corners of the screen, and gently ask you whether you need help.

But what is a chatbot? It is a computer program whose aim is interacting with humans as they receive, as inputs, texts or audios. The most sophisticated chatbots are based on Natural Language Processing, that field of AI in which models aim at interpreting the way humans interact among each other, hence their natural language, which might be written or spoken (if you want to read more about NLP, you can read my former article here).

However, many chatbots are programmed so that they capture some keywords which are directly related to pre-defined responses. This process is based on a first training of the algorithm on given libraries, where it will be able to size the connections between answers and questions, as well as some emotional reactions typical of human beings. And this is the idea behind ELIZA, the chatbot programmed by Joseph Weizenbaum: it recognizes clue words as input and returns pre-programmed sentences as output. This method might sound kind of naive, but there are two further elements that need to be considered:

  • First, when I say ‘some clue words’ and ‘some pre-programmed responses’, I’m referring to the enormous amount data on which the chatbot has been trained. Namely, if you are about to build a chatbot which answers questions about the Empire State Building, you could train it on the Wikipedia page about that building, then on some interviews about the topic, and even on books which takes place in NYC and involves some speeches about it. So, even though responses are ‘copied’ and not originally created, it doesn’t mean your chatbot is not able to fulfill its goal.
  • The second element is a bit more psychological. It turned out that chatbots like ELIZA, not intelligent by definition, are capable of being interpreted intelligent by humans. Indeed, they generate an illusion of understanding which humans tend to appreciate, often forgetting they are not talking with another human being. That’s one of the reasons why these naive chatbots are so popular today: they are without doubts useful for several purposes, ranging from online assistance to personal use.

With that being said, let’s jump to the practical application. Here, I will show a very easy implementation of a naive chatbot in Python, using a pre-compiled library and training environment called ChatterBot (you can install it via pip install chatterbot in your terminal).

#importing the library
from chatterbot import ChatBot
 bot = ChatBot('Chatbot')

#importing a corpus of sample sentences and conversations so that our chatbot
#can learn 
from chatterbot.trainers import ChatterBotCorpusTrainer

# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(bot)

# Train the chatbot based on the english corpus (other languages are available too)


As you can see, the corpus library contains many domains of sample conversations or sentences, like sports, science and so forth. To give you the intuition, I will replicate the matching procedure manually for the domain ‘greetings’. Indeed, looking at the following code:

GREETING_INPUTS = ("hello", "hi", "good morning", "what's up","hey",)
GREETING_RESPONSES = ["hi", "hey","good morning", "hello", "thanks for asking my help"]

def greeting(sentence):
    for word in sentence.split():
        if word.lower() in GREETING_INPUTS:
            return random.choice(GREETING_RESPONSES)
            return random.choice(GREETING_RESPONSES)

You can see that I linked some greetings written by the users to other greetings which will be the chatbot’s response. Basically, I defined a function ‘greeting’ which return, for any input in GREETING_INPUTS, a random output within the GREETING_OUTPUTS. This procedure is replicated, more widely, for each category of the chatterbot.corpus.

Now let’s initialize out chatbot:

while True:
        # Input message from user
        #if message is different from Bye
    if message.strip()!='Bye':
             #if message is equal to thanks or thank you
        if(message.strip()=='thanks' or message.strip()=='thank you' ):
            print('Chatbot: You are welcome..')
             #if message is withing greetings
                print('Chatbot: '+greeting(message.strip()))
             #if none of the above is true, use the embedded method '.get_response'
        # if message is Bye
    if message.strip()=='Bye':
        print('Chatbot: Bye')

It wasn’t too bad. Again, if you want your chatbot to be an expert in a specific domain, you should train it on that topic more deeply. Here, I just picked some random domains, poorly assorted compared to the huge range of potential conversations, in order to show the idea behind a naive chatbot.

Of course, intelligent chatbots – those which use NLP techniques to understand human interactions and create original, smart responses – are far more interesting, and this field of study is evolving rapidly. Nevertheless, naive chatbots are still widely used in industries, thanks to their capability of being seen as intelligent by humans.

Published by valentinaalto

I'm a 22-years-old student based in Milan, passionate about everything related to Statistics, Data Science and Machine Learning. I'm eager to learn new concepts and techniques as well as share them with whoever is interested in the topic.

Leave a comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with
Get started
%d bloggers like this: