What is a python bot

Programming a chatbot with Python

Finally chat with your own programmed Python program. The English word "chatter" means nothing else than chatting or babbling. Chatbots with text input by the user and intelligent (more or less) answers have a long tradition. In 1964, Eliza was programmed by Joseph Weizenbaum.

And that's exactly what we want to program a chatbot with Python. For this we need the basic functionality.

This chapter uses:

  • Lists
  • Dictionary
  • while loops
  • for loops
  • the query "in"

How the chatbot works

The user is greeted by the chatbot and immediately informed that he can end the conversation with "bye". Then the user input is waited for.

If the user input is available, this sentence will be broken down into individual parts (i.e. words).

The individual words are harmonized (upper and lower case are matched).

A check is then made as to whether there are individual words in the database that are available as “suitable” answers.

If there is a "suitable" answer, it will be given as feedback.

If there is no suitable answer, a random answer is returned by the chatbot to keep the conversation going.

The next user input is awaited.

We will now program this process step by step in Python. To do this, we will use all the elements of Python that we have learned so far.

Step 1: Input with loop until finished by the user

In the first step, after the greeting, we want to ask our users to enter a text.

Before we work with German texts in which umlauts such as "öäü" can occur again and again (regardless of whether they are input or output), we should prepare Python for this. We save our file with the UTF-8 encoding. So we tell Python that too. This happens via:

It is important that this happens in the first line of our program code. If you want to read more about it, you can simply enter “Encoding Cookies Python” in a search engine

There is other code that is not listed here.

Now we greet our users and give you a hint how the program can be closed again:

We now want to capture the text input of the user. This is saved in the variable "" and displayed on the screen for checking:

If you get an error message in the form of a "NameError", please make sure that we are also using a current Python version (at least Python version 3 or higher)! The instruction is handled differently in the old Python2 versions. In Python3, the input is processed as a string.

As you can see in our flowchart, the user should be able to make further entries until they type in "bye" to exit.

So we put our input in a loop:

We need the instruction twice. The first instruction initializes the variable "" and the second empties any existing content, which does not yet occur in the first pass.

When we test our program now, we can enter something and it will simply be output again. However, we cannot enter anything and simply pass the entry empty with the return key. This is of course not very helpful for the following program sequence. Therefore we should still make sure that an empty entry is not possible and the user is asked further. So we build a second loop in our loop to ensure input.

We have now done the first strand of our flowchart.

Step 2: random response to user input

Let’s now jump to the output of a random answer. To do this, we need a collection of answers that somehow always fit. We can then select one of these at random later. We save the answer collection in a list:

Since we need the random module for a random answer, we include it at the beginning of our program (as well as our list with the possible random answers).

We get a random answer via the instruction. We can now output these after the user input:

Step 3: appropriate answers to user input

The previous conversation with only random answers is rather strange for the user. Therefore, we want to provide the most suitable answer to the user input. To do this, we record a number of suitable answers to certain stop words using the data type dictionary. In addition to the stop word, our dictionary contains an answer provided for this purpose (see more about the dictionary data type in the chapter https://www.python-lernen.de/python-dictionary.htm)

Here is a small selection - the bigger, the better for entertainment. We also put our dictionary at the beginning of our program.

Now we need to check if there is a stop word in the user input. In order not to have problems with upper and lower case letters, we convert the entry completely into lower case letters

And then we create a list from this implementation:

We can go through this list word for word and if a word appears in the stop words, use this possible answer:

The use of the inconspicuous Python command is important here. We check whether words from our “single words” list appear as keys in our “reaction responses” dictionary. In response, we receive a “True” if available or a “False” if not available. This does the above statement:

Now our “reaction answer” is given.

We just have to remember, then a "suitable" answer has been found. Otherwise a random answer must be selected. We save this via "intelligent answers = True", which must be set to "False" at the beginning.

We can now query this and then output our random answer:

That was a very simple chatbot in Python. This can of course still be refined.

Recommend • Social Bookmarks • Thank you